-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathpartition_info.cc
2874 lines (2543 loc) · 94.6 KB
/
partition_info.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) 2006, 2024, 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/partition_info.h" // LIST_PART_ENTRY
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include "lex_string.h"
#include "map_helpers.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/plugin.h"
#include "mysql/strings/int2str.h"
#include "mysql/strings/m_ctype.h"
#include "mysql/udf_registration_types.h"
#include "mysqld_error.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/auth_common.h" // *_ACL
#include "sql/create_field.h"
#include "sql/derror.h" // ER_THD
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/item.h"
#include "sql/partitioning/partition_handler.h" // PART_DEF_NAME, Partition_share
#include "sql/set_var.h"
#include "sql/sql_base.h" // fill_record
#include "sql/sql_class.h" // THD
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_parse.h" // test_if_data_home_dir
#include "sql/sql_partition.h"
#include "sql/sql_tablespace.h" // validate_tablespace_name
#include "sql/system_variables.h"
#include "sql/table.h" // Table_ref
#include "sql/table_trigger_dispatcher.h" // Table_trigger_dispatcher
#include "sql/thr_malloc.h"
#include "sql/trigger_chain.h" // Trigger_chain
#include "sql/trigger_def.h"
#include "sql_string.h"
#include "string_with_len.h"
using std::string;
// TODO: Create ::get_copy() for getting a deep copy.
partition_info *partition_info::get_clone(THD *thd, bool reset /* = false */) {
DBUG_TRACE;
List_iterator<partition_element> part_it(partitions);
partition_element *part;
partition_info *clone = new (thd->mem_root) partition_info(*this);
if (!clone) {
mem_alloc_error(sizeof(partition_info));
return nullptr;
}
new (&(clone->read_partitions)) MY_BITMAP;
new (&(clone->lock_partitions)) MY_BITMAP;
clone->bitmaps_are_initialized = false;
clone->partitions.clear();
clone->temp_partitions.clear();
while ((part = (part_it++))) {
List_iterator<partition_element> subpart_it(part->subpartitions);
partition_element *subpart;
partition_element *part_clone =
new (thd->mem_root) partition_element(*part);
if (!part_clone) {
mem_alloc_error(sizeof(partition_element));
return nullptr;
}
/* Explicitly copy the tablespace name, use the thd->mem_root. */
if (part->tablespace_name != nullptr)
part_clone->tablespace_name =
strmake_root(thd->mem_root, part->tablespace_name,
strlen(part->tablespace_name) + 1);
/*
Mark that RANGE and LIST values needs to be fixed so that we don't
use old values. fix_column_value_functions would evaluate the values
from Item expression.
*/
if (reset) {
clone->defined_max_value = false;
List_iterator<part_elem_value> list_it(part_clone->list_val_list);
while (part_elem_value *list_value = list_it++) {
part_column_list_val *col_val = list_value->col_val_array;
for (uint i = 0; i < num_columns; col_val++, i++) {
col_val->fixed = 0;
}
}
}
part_clone->subpartitions.clear();
while ((subpart = (subpart_it++))) {
partition_element *subpart_clone =
new (thd->mem_root) partition_element(*subpart);
if (!subpart_clone) {
mem_alloc_error(sizeof(partition_element));
return nullptr;
}
/* Explicitly copy the tablespace name, use the thd->mem_root. */
if (subpart->tablespace_name != nullptr)
subpart_clone->tablespace_name =
strmake_root(thd->mem_root, subpart->tablespace_name,
strlen(subpart->tablespace_name) + 1);
part_clone->subpartitions.push_back(subpart_clone);
}
clone->partitions.push_back(part_clone);
}
return clone;
}
partition_info *partition_info::get_full_clone(THD *thd) {
partition_info *clone;
DBUG_TRACE;
clone = get_clone(thd);
if (!clone) return nullptr;
memcpy(&clone->read_partitions, &read_partitions, sizeof(read_partitions));
memcpy(&clone->lock_partitions, &lock_partitions, sizeof(lock_partitions));
clone->bitmaps_are_initialized = bitmaps_are_initialized;
return clone;
}
/**
Mark named [sub]partition to be used/locked.
@param part_name Partition name to match.
@param length Partition name length.
@return Success if partition found
@retval true Partition found
@retval false Partition not found
*/
bool partition_info::add_named_partition(const char *part_name, size_t length) {
PART_NAME_DEF *part_def;
Partition_share *part_share;
DBUG_TRACE;
assert(table && table->s && table->s->ha_share);
part_share = static_cast<Partition_share *>((table->s->ha_share));
assert(part_share->partition_name_hash != nullptr);
auto part_name_hash = part_share->partition_name_hash.get();
assert(!part_name_hash->empty());
part_def = find_or_nullptr(*part_name_hash, string(part_name, length));
if (!part_def) {
my_error(ER_UNKNOWN_PARTITION, MYF(0), part_name, table->alias);
return true;
}
if (part_def->is_subpart) {
bitmap_set_bit(&read_partitions, part_def->part_id);
} else {
if (is_sub_partitioned()) {
/* Mark all subpartitions in the partition */
uint j, start = part_def->part_id;
uint end = start + num_subparts;
for (j = start; j < end; j++) bitmap_set_bit(&read_partitions, j);
} else
bitmap_set_bit(&read_partitions, part_def->part_id);
}
DBUG_PRINT("info", ("Found partition %u is_subpart %d for name %s",
part_def->part_id, part_def->is_subpart, part_name));
return false;
}
/**
Mark named [sub]partition to be used/locked.
*/
bool partition_info::set_named_partition_bitmap(const char *part_name,
size_t length) {
DBUG_TRACE;
bitmap_clear_all(&read_partitions);
if (add_named_partition(part_name, length)) return true;
bitmap_copy(&lock_partitions, &read_partitions);
return false;
}
/**
Prune away partitions not mentioned in the PARTITION () clause,
if used.
@return Operation status
@retval false Success
@retval true Failure
*/
bool partition_info::set_read_partitions(List<String> *partition_names) {
DBUG_TRACE;
if (!partition_names || !partition_names->elements) {
return true;
}
uint num_names = partition_names->elements;
List_iterator<String> partition_names_it(*partition_names);
uint i = 0;
/*
TODO: When adding support for FK in partitioned tables, the referenced
table must probably lock all partitions for read, and also write depending
of ON DELETE/UPDATE.
*/
bitmap_clear_all(&read_partitions);
/* No check for duplicate names or overlapping partitions/subpartitions. */
DBUG_PRINT("info", ("Searching through partition_name_hash"));
do {
String *part_name_str = partition_names_it++;
if (add_named_partition(part_name_str->c_ptr(), part_name_str->length()))
return true;
} while (++i < num_names);
return false;
}
/**
Set read/lock_partitions bitmap over non pruned partitions
@param table_list Possible Table_ref which can contain
list of partition names to query
@return Operation status
@retval false OK
@retval true Failed to allocate memory for bitmap or list of partitions
did not match
@note OK to call multiple times without the need for free_bitmaps.
*/
bool partition_info::set_partition_bitmaps(Table_ref *table_list) {
DBUG_TRACE;
assert(bitmaps_are_initialized);
assert(table);
is_pruning_completed = false;
if (!bitmaps_are_initialized) return true;
if (table_list && table_list->partition_names &&
table_list->partition_names->elements) {
if (table->s->db_type()->partition_flags() & HA_USE_AUTO_PARTITION) {
/*
Don't allow PARTITION () clause on a NDB tables yet.
TODO: Add partition name handling to NDB/partition_info.
which is currently ha_partition specific.
*/
my_error(ER_PARTITION_CLAUSE_ON_NONPARTITIONED, MYF(0));
return true;
}
if (set_read_partitions(table_list->partition_names)) return true;
} else {
bitmap_set_all(&read_partitions);
DBUG_PRINT("info", ("Set all partitions"));
}
bitmap_copy(&lock_partitions, &read_partitions);
assert(bitmap_get_first_set(&lock_partitions) != MY_BIT_NONE);
return false;
}
/**
Checks if possible to do prune partitions on insert.
@param thd Thread context
@param duplic How to handle duplicates
@param update In case of ON DUPLICATE UPDATE, default function fields
@param update_fields In case of ON DUPLICATE UPDATE, which fields to update
@param fields Listed fields
@param empty_values True if values is empty (only defaults)
@param[out] prune_needs_default_values Set on return if copying of default
values is needed
@param[out] can_prune_partitions Enum showing if possible to prune
@param[in,out] used_partitions If possible to prune the bitmap
is initialized and cleared
@return Operation status
@retval false Success
@retval true Failure
*/
bool partition_info::can_prune_insert(
THD *thd, enum_duplicates duplic, COPY_INFO &update,
const mem_root_deque<Item *> &update_fields,
const mem_root_deque<Item *> &fields, bool empty_values,
enum_can_prune *can_prune_partitions, bool *prune_needs_default_values,
MY_BITMAP *used_partitions) {
*can_prune_partitions = PRUNE_NO;
assert(bitmaps_are_initialized);
DBUG_TRACE;
if (table->s->db_type()->partition_flags() & HA_USE_AUTO_PARTITION)
return false; /* Should not insert prune NDB tables */
/*
If under LOCK TABLES pruning will skip start_stmt instead of external_lock
for unused partitions.
Cannot prune if there are BEFORE INSERT triggers that changes any
partitioning column, since they may change the row to be in another
partition.
*/
if (table->triggers) {
Trigger_chain *trigger_chain =
table->triggers->get_triggers(TRG_EVENT_INSERT, TRG_ACTION_BEFORE);
if (trigger_chain &&
trigger_chain->has_updated_trigger_fields(&full_part_field_set))
return false;
}
/*
Can't prune partitions over generated columns, as their values are
calculated much later.
*/
if (table->vfield) {
Field **fld;
for (fld = table->vfield; *fld; fld++) {
if (bitmap_is_set(&full_part_field_set, (*fld)->field_index()))
return false;
}
}
/*
Can't prune partitions over generated default expressions, as their values
are calculated much later.
*/
if (table->gen_def_fields_ptr) {
Field **fld;
for (fld = table->gen_def_fields_ptr; *fld; fld++) {
if (bitmap_is_set(&full_part_field_set, (*fld)->field_index()))
return false;
}
}
if (table->found_next_number_field) {
/*
If the field is used in the partitioning expression, we cannot prune.
TODO: If all rows have not null values and
is not 0 (with NO_AUTO_VALUE_ON_ZERO sql_mode), then pruning is possible!
*/
if (bitmap_is_set(&full_part_field_set,
table->found_next_number_field->field_index()))
return false;
}
/*
If updating a field in the partitioning expression, we cannot prune.
Note: TIMESTAMP_AUTO_SET_ON_INSERT is handled by converting Item_null
to the start time of the statement. Which will be the same as in
write_row(). So pruning of TIMESTAMP DEFAULT CURRENT_TIME will work.
But TIMESTAMP_AUTO_SET_ON_UPDATE cannot be pruned if the timestamp
column is a part of any part/subpart expression.
*/
if (duplic == DUP_UPDATE) {
/*
Cannot prune if any field in the partitioning expression can
be updated by ON DUPLICATE UPDATE.
*/
if (update.function_defaults_apply_on_columns(&full_part_field_set))
return false;
/*
TODO: add check for static update values, which can be pruned.
*/
if (is_fields_in_part_expr(update_fields)) return false;
/*
Cannot prune if there are BEFORE UPDATE triggers that changes any
partitioning column, since they may change the row to be in another
partition.
*/
if (table->triggers) {
Trigger_chain *trigger_chain =
table->triggers->get_triggers(TRG_EVENT_UPDATE, TRG_ACTION_BEFORE);
if (trigger_chain &&
trigger_chain->has_updated_trigger_fields(&full_part_field_set))
return false;
}
}
/*
If not all partitioning fields are given,
we also must set all non given partitioning fields
to get correct defaults.
TODO: If any gain, we could enhance this by only copy the needed default
fields by
1) check which fields needs to be set.
2) only copy those fields from the default record.
*/
*prune_needs_default_values = false;
if (empty_values) {
*prune_needs_default_values = true; // like 'INSERT INTO t () VALUES ()'
} else if (!fields.empty()) {
if (!is_full_part_expr_in_fields(fields))
*prune_needs_default_values = true;
} else {
/*
In case of INSERT INTO t VALUES (...) we must get values for
all fields in table from VALUES (...) part, so no defaults
are needed.
*/
}
/*
Pruning possible, have to initialize the used_partitions bitmap.
This also clears all bits.
*/
if (init_partition_bitmap(used_partitions, thd->mem_root)) return true;
/*
If no partitioning field in set (e.g. defaults) check pruning only once.
*/
if (!fields.empty() && !is_fields_in_part_expr(fields))
*can_prune_partitions = PRUNE_DEFAULTS;
else
*can_prune_partitions = PRUNE_YES;
return false;
}
/**
Mark the partition, the record belongs to, as used.
@param thd Thread handler
@param fields Fields to set
@param values Values to use
@param info COPY_INFO used for default values handling
@param copy_default_values True if we should copy default values
@param used_partitions Bitmap to set
@returns Operational status
@retval false Success
@retval true Failure
A return value of 'true' may indicate conversion error,
so caller must check thd->is_error().
*/
bool partition_info::set_used_partition(THD *thd,
const mem_root_deque<Item *> &fields,
const mem_root_deque<Item *> &values,
COPY_INFO &info,
bool copy_default_values,
MY_BITMAP *used_partitions) {
uint32 part_id;
longlong func_value;
assert(thd);
/* Only allow checking of constant values */
for (Item *item : values) {
if (!item->const_item()) return true;
}
if (copy_default_values) restore_record(table, s->default_values);
if (!fields.empty() || values.empty()) {
if (fill_record(thd, table, fields, values, &full_part_field_set, nullptr,
false))
return true;
} else {
if (fill_record(thd, table, table->field, values, &full_part_field_set,
nullptr, false))
return true;
}
/*
Evaluate DEFAULT functions like CURRENT_TIMESTAMP.
TODO: avoid setting non partitioning fields default value, to avoid
overhead. Not yet done, since mostly only one DEFAULT function per
table, or at least very few such columns.
*/
if (info.function_defaults_apply_on_columns(&full_part_field_set)) {
if (info.set_function_defaults(table)) return true;
}
{
/*
This function is used in INSERT; 'values' are supplied by user,
or are default values, not values read from a table, so read_set is
irrelevant.
*/
my_bitmap_map *old_map = dbug_tmp_use_all_columns(table, table->read_set);
const int rc = get_partition_id(this, &part_id, &func_value);
dbug_tmp_restore_column_map(table->read_set, old_map);
if (rc) return true;
}
DBUG_PRINT("info", ("Insert into partition %u", part_id));
bitmap_set_bit(used_partitions, part_id);
return false;
}
/*
Create a memory area where default partition names are stored and fill it
up with the names.
SYNOPSIS
create_default_partition_names()
num_parts Number of partitions
start_no Starting partition number
subpart Is it subpartitions
RETURN VALUE
A pointer to the memory area of the default partition names
DESCRIPTION
A support routine for the partition code where default values are
generated.
The external routine needing this code is check_partition_info
*/
#define MAX_PART_NAME_SIZE 8
char *partition_info::create_default_partition_names(uint num_parts_arg,
uint start_no) {
char *ptr = (char *)sql_calloc(num_parts_arg * MAX_PART_NAME_SIZE);
char *move_ptr = ptr;
uint i = 0;
DBUG_TRACE;
if (likely(ptr != nullptr)) {
do {
sprintf(move_ptr, "p%u", (start_no + i));
move_ptr += MAX_PART_NAME_SIZE;
} while (++i < num_parts_arg);
} else {
mem_alloc_error(num_parts_arg * MAX_PART_NAME_SIZE);
}
return ptr;
}
/*
Generate a version string for partition expression
This function must be updated every time there is a possibility for
a new function of a higher version number than 5.5.0.
SYNOPSIS
set_show_version_string()
RETURN VALUES
None
*/
void partition_info::set_show_version_string(String *packet) {
int version = 0;
if (column_list)
packet->append(STRING_WITH_LEN("\n/*!50500"));
else {
if (part_expr)
part_expr->walk(&Item::intro_version, enum_walk::POSTFIX,
(uchar *)&version);
if (subpart_expr)
subpart_expr->walk(&Item::intro_version, enum_walk::POSTFIX,
(uchar *)&version);
if (version == 0) {
/* No new functions in partition function */
packet->append(STRING_WITH_LEN("\n/*!50100"));
} else {
packet->append(STRING_WITH_LEN("\n/*!"));
packet->append_longlong(version);
}
}
}
/*
Create a unique name for the subpartition as part_name'sp''subpart_no'
SYNOPSIS
create_default_subpartition_name()
subpart_no Number of subpartition
part_name Name of partition
RETURN VALUES
>0 A reference to the created name string
0 Memory allocation error
*/
char *partition_info::create_default_subpartition_name(uint subpart_no,
const char *part_name) {
size_t size_alloc = strlen(part_name) + MAX_PART_NAME_SIZE;
char *ptr = (char *)sql_calloc(size_alloc);
DBUG_TRACE;
if (likely(ptr != nullptr)) {
snprintf(ptr, size_alloc, "%ssp%u", part_name, subpart_no);
} else {
mem_alloc_error(size_alloc);
}
return ptr;
}
/*
Set up all the default partitions not set-up by the user in the SQL
statement. Also perform a number of checks that the user hasn't tried
to use default values where no defaults exists.
SYNOPSIS
set_up_default_partitions()
file A reference to a handler of the table
info Create info
start_no Starting partition number
RETURN VALUE
true Error, attempted default values not possible
false Ok, default partitions set-up
DESCRIPTION
The routine uses the underlying handler of the partitioning to define
the default number of partitions. For some handlers this requires
knowledge of the maximum number of rows to be stored in the table.
This routine only accepts HASH and KEY partitioning and thus there is
no subpartitioning if this routine is successful.
The external routine needing this code is check_partition_info
*/
bool partition_info::set_up_default_partitions(Partition_handler *part_handler,
HA_CREATE_INFO *info,
uint start_no) {
uint i;
char *default_name;
bool result = true;
DBUG_TRACE;
if (part_type != partition_type::HASH) {
const char *error_string;
if (part_type == partition_type::RANGE)
error_string = partition_keywords[PKW_RANGE].str;
else
error_string = partition_keywords[PKW_LIST].str;
my_error(ER_PARTITIONS_MUST_BE_DEFINED_ERROR, MYF(0), error_string);
goto end;
}
if (num_parts == 0) {
if (!part_handler) {
num_parts = 1;
} else {
num_parts = part_handler->get_default_num_partitions(info);
}
if (num_parts == 0) {
my_error(ER_PARTITION_NOT_DEFINED_ERROR, MYF(0), "partitions");
goto end;
}
}
if (unlikely(num_parts > MAX_PARTITIONS)) {
my_error(ER_TOO_MANY_PARTITIONS_ERROR, MYF(0));
goto end;
}
if (unlikely((!(default_name =
create_default_partition_names(num_parts, start_no)))))
goto end;
i = 0;
do {
partition_element *part_elem = new (*THR_MALLOC) partition_element();
if (likely(part_elem != nullptr && (!partitions.push_back(part_elem)))) {
part_elem->engine_type = default_engine_type;
part_elem->partition_name = default_name;
default_name += MAX_PART_NAME_SIZE;
} else {
mem_alloc_error(sizeof(partition_element));
goto end;
}
} while (++i < num_parts);
result = false;
end:
return result;
}
/*
Set up all the default subpartitions not set-up by the user in the SQL
statement. Also perform a number of checks that the default partitioning
becomes an allowed partitioning scheme.
SYNOPSIS
set_up_default_subpartitions()
file A reference to a handler of the table
info Create info
RETURN VALUE
true Error, attempted default values not possible
false Ok, default partitions set-up
DESCRIPTION
The routine uses the underlying handler of the partitioning to define
the default number of partitions. For some handlers this requires
knowledge of the maximum number of rows to be stored in the table.
This routine is only called for RANGE or LIST partitioning and those
need to be specified so only subpartitions are specified.
The external routine needing this code is check_partition_info
*/
bool partition_info::set_up_default_subpartitions(
Partition_handler *part_handler, HA_CREATE_INFO *info) {
uint i, j;
bool result = true;
partition_element *part_elem;
List_iterator<partition_element> part_it(partitions);
DBUG_TRACE;
if (num_subparts == 0) {
if (!part_handler) {
num_subparts = 1;
} else {
num_subparts = part_handler->get_default_num_partitions(info);
}
}
if (unlikely((num_parts * num_subparts) > MAX_PARTITIONS)) {
my_error(ER_TOO_MANY_PARTITIONS_ERROR, MYF(0));
goto end;
}
i = 0;
do {
part_elem = part_it++;
j = 0;
do {
partition_element *subpart_elem =
new (*THR_MALLOC) partition_element(part_elem);
if (likely(subpart_elem != nullptr &&
(!part_elem->subpartitions.push_back(subpart_elem)))) {
char *ptr =
create_default_subpartition_name(j, part_elem->partition_name);
if (!ptr) goto end;
subpart_elem->engine_type = default_engine_type;
subpart_elem->partition_name = ptr;
} else {
mem_alloc_error(sizeof(partition_element));
goto end;
}
} while (++j < num_subparts);
} while (++i < num_parts);
result = false;
end:
return result;
}
/*
Support routine for check_partition_info
SYNOPSIS
set_up_defaults_for_partitioning()
file A reference to a handler of the table
info Create info
start_no Starting partition number
RETURN VALUE
true Error, attempted default values not possible
false Ok, default partitions set-up
DESCRIPTION
Set up defaults for partition or subpartition (cannot set-up for both,
this will return an error.
*/
bool partition_info::set_up_defaults_for_partitioning(
Partition_handler *part_handler, HA_CREATE_INFO *info, uint start_no) {
DBUG_TRACE;
if (!default_partitions_setup) {
default_partitions_setup = true;
if (use_default_partitions)
return set_up_default_partitions(part_handler, info, start_no);
if (is_sub_partitioned() && use_default_subpartitions)
return set_up_default_subpartitions(part_handler, info);
}
return false;
}
/*
Support routine for check_partition_info
SYNOPSIS
find_duplicate_field
no parameters
RETURN VALUE
Erroneous field name Error, there are two fields with same name
NULL Ok, no field defined twice
DESCRIPTION
Check that the user haven't defined the same field twice in
key or column list partitioning.
*/
char *partition_info::find_duplicate_field() {
char *field_name_outer, *field_name_inner;
List_iterator<char> it_outer(part_field_list);
uint num_fields = part_field_list.elements;
uint i, j;
DBUG_TRACE;
for (i = 0; i < num_fields; i++) {
field_name_outer = it_outer++;
List_iterator<char> it_inner(part_field_list);
for (j = 0; j < num_fields; j++) {
field_name_inner = it_inner++;
if (i >= j) continue;
if (!(my_strcasecmp(system_charset_info, field_name_outer,
field_name_inner))) {
return field_name_outer;
}
}
}
return nullptr;
}
/**
@brief Get part_elem and part_id from partition name
@param partition_name Name of partition to search for.
@param [out] part_id Id of found partition or NOT_A_PARTITION_ID.
@retval Pointer to part_elem of [sub]partition, if not found NULL
@note Since names of partitions AND subpartitions must be unique,
this function searches both partitions and subpartitions and if name of
a partition is given for a subpartitioned table, part_elem will be
the partition, but part_id will be NOT_A_PARTITION_ID and file_name not set.
*/
partition_element *partition_info::get_part_elem(const char *partition_name,
uint32 *part_id) {
List_iterator<partition_element> part_it(partitions);
uint i = 0;
DBUG_TRACE;
assert(part_id);
*part_id = NOT_A_PARTITION_ID;
do {
partition_element *part_elem = part_it++;
if (is_sub_partitioned()) {
List_iterator<partition_element> sub_part_it(part_elem->subpartitions);
uint j = 0;
do {
partition_element *sub_part_elem = sub_part_it++;
if (!my_strcasecmp(system_charset_info, sub_part_elem->partition_name,
partition_name)) {
*part_id = j + (i * num_subparts);
return sub_part_elem;
}
} while (++j < num_subparts);
/* Naming a partition (first level) on a subpartitioned table. */
if (!my_strcasecmp(system_charset_info, part_elem->partition_name,
partition_name))
return part_elem;
} else if (!my_strcasecmp(system_charset_info, part_elem->partition_name,
partition_name)) {
*part_id = i;
return part_elem;
}
} while (++i < num_parts);
return nullptr;
}
/*
A support function to check partition names for duplication in a
partitioned table
SYNOPSIS
find_duplicate_name()
RETURN VALUES
NULL Has unique part and subpart names
!NULL Pointer to duplicated name
DESCRIPTION
Checks that the list of names in the partitions doesn't contain any
duplicated names.
*/
const char *partition_info::find_duplicate_name() {
collation_unordered_set<string> partition_names{system_charset_info,
PSI_INSTRUMENT_ME};
List_iterator<partition_element> parts_it(partitions);
partition_element *p_elem;
DBUG_TRACE;
/*
TODO: If table->s->ha_part_data->partition_name_hash.elements is > 0,
then we could just return NULL, but that has not been verified.
And this only happens when in ALTER TABLE with full table copy.
*/
while ((p_elem = (parts_it++))) {
const char *partition_name = p_elem->partition_name;
if (!partition_names.insert(partition_name).second) return partition_name;
if (!p_elem->subpartitions.is_empty()) {
List_iterator<partition_element> subparts_it(p_elem->subpartitions);
partition_element *subp_elem;
while ((subp_elem = (subparts_it++))) {
const char *subpartition_name = subp_elem->partition_name;
if (!partition_names.insert(subpartition_name).second)
return subpartition_name;
}
}
}
return nullptr;
}
/*
Check that the partition/subpartition is setup to use the correct
storage engine
SYNOPSIS
check_engine_condition()
p_elem Partition element
table_engine_set Have user specified engine on table level
inout::engine_type Current engine used
inout::first Is it first partition
RETURN VALUE
true Failed check
false Ok
DESCRIPTION
Specified engine for table and partitions p0 and pn
Must be correct both on CREATE and ALTER commands
table p0 pn res (0 - OK, 1 - FAIL)
- - - 0
- - x 1
- x - 1
- x x 0
x - - 0
x - x 0
x x - 0
x x x 0
i.e:
- All subpartitions must use the same engine
AND it must be the same as the partition.
- All partitions must use the same engine
AND it must be the same as the table.
- if one does NOT specify an engine on the table level
then one must either NOT specify any engine on any
partition/subpartition OR for ALL partitions/subpartitions
Note:
When ALTER a table, the engines are already set for all levels
(table, all partitions and subpartitions). So if one want to
change the storage engine, one must specify it on the table level
*/
static bool check_engine_condition(partition_element *p_elem,
bool table_engine_set,
handlerton **engine_type, bool *first) {
DBUG_TRACE;
DBUG_PRINT("enter", ("p_eng %s t_eng %s t_eng_set %u first %u state %u",
ha_resolve_storage_engine_name(p_elem->engine_type),
ha_resolve_storage_engine_name(*engine_type),
table_engine_set, *first, p_elem->part_state));
if (*first && !table_engine_set) {
*engine_type = p_elem->engine_type;
DBUG_PRINT("info", ("setting table_engine = %s",
ha_resolve_storage_engine_name(*engine_type)));
}
*first = false;
if ((table_engine_set &&
(p_elem->engine_type != (*engine_type) && p_elem->engine_type)) ||
(!table_engine_set && p_elem->engine_type != (*engine_type))) {
return true;
}
return false;
}
/*
Check engine mix that it is correct
Current limitation is that all partitions and subpartitions
must use the same storage engine.
SYNOPSIS
check_engine_mix()
inout::engine_type Current engine used
table_engine_set Have user specified engine on table level
RETURN VALUE