-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsql_partition.cc
6148 lines (5500 loc) · 223 KB
/
sql_partition.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2005, 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 */
/*
This file is a container for general functionality related
to partitioning. It contains functionality used by all handlers that
support partitioning, such as the partitioning handler itself and the NDB
handler. (Much of the code in this file has been split into partition_info.cc
and the header files partition_info.h + partition_element.h + sql_partition.h)
The first version supports RANGE partitioning, LIST partitioning, HASH
partitioning and composite partitioning (hereafter called subpartitioning)
where each RANGE/LIST partitioning is HASH partitioned. The hash function
can either be supplied by the user or by only a list of fields (also
called KEY partitioning), where the MySQL server will use an internal
hash function.
There are quite a few defaults that can be used as well.
The second version introduces a new variant of RANGE and LIST partitioning
which is often referred to as column lists in the code variables. This
enables a user to specify a set of columns and their concatenated value
as the partition value. By comparing the concatenation of these values
the proper partition can be chosen.
*/
#include "sql/sql_partition.h"
#include <algorithm>
#include <cassert>
#include <climits>
#include <cstring>
#include "field_types.h" // enum_field_types
#include "my_alloc.h" // operator new
#include "my_bitmap.h"
#include "my_byteorder.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_io.h"
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "mysql/components/services/bits/my_io_bits.h" // File
#include "mysql/components/services/bits/psi_statement_bits.h"
#include "mysql/plugin.h"
#include "mysql/psi/mysql_file.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/strings/int2str.h"
#include "mysql/strings/m_ctype.h" // system_charset_info
#include "mysql/udf_registration_types.h"
#include "mysql_com.h"
#include "mysql_time.h"
#include "mysqld_error.h"
#include "nulls.h"
#include "sql/create_field.h"
#include "sql/current_thd.h"
#include "sql/debug_sync.h" // DEBUG_SYNC
#include "sql/derror.h" // ER_THD
#include "sql/enum_query_type.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/item.h" // enum_monotoncity_info
#include "sql/item_func.h" // Item_func
#include "sql/key.h"
#include "sql/mdl.h"
#include "sql/mysqld.h" // mysql_tmpdir
#include "sql/parse_tree_node_base.h"
#include "sql/partition_info.h" // partition_info
#include "sql/partitioning/partition_handler.h" // Partition_handler
#include "sql/psi_memory_key.h"
#include "sql/query_options.h"
#include "sql/range_optimizer/range_optimizer.h" // store_key_image_to_rec
#include "sql/sql_alter.h"
#include "sql/sql_base.h" // wait_while_table_is_used
#include "sql/sql_class.h" // THD
#include "sql/sql_const.h"
#include "sql/sql_digest_stream.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_parse.h" // parse_sql
#include "sql/sql_show.h"
#include "sql/sql_table.h" // build_table_filename
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/thd_raii.h"
#include "sql/thr_malloc.h" // sql_calloc
#include "sql_string.h"
#include "string_with_len.h"
#include "strxmov.h"
using std::max;
using std::min;
/*
Partition related functions declarations and some static constants;
*/
const LEX_CSTRING partition_keywords[] = {{STRING_WITH_LEN("HASH")},
{STRING_WITH_LEN("RANGE")},
{STRING_WITH_LEN("LIST")},
{STRING_WITH_LEN("KEY")},
{STRING_WITH_LEN("MAXVALUE")},
{STRING_WITH_LEN("LINEAR ")},
{STRING_WITH_LEN(" COLUMNS")},
{STRING_WITH_LEN("ALGORITHM")}
};
static const char *part_str = "PARTITION";
static const char *sub_str = "SUB";
static const char *by_str = "BY";
static const char *space_str = " ";
static const char *equal_str = "=";
static const char *end_paren_str = ")";
static const char *begin_paren_str = "(";
static const char *comma_str = ",";
static int get_partition_id_list_col(partition_info *part_info, uint32 *part_id,
longlong *func_value);
static int get_partition_id_list(partition_info *part_info, uint32 *part_id,
longlong *func_value);
static int get_partition_id_range_col(partition_info *part_info,
uint32 *part_id, longlong *func_value);
static int get_partition_id_range(partition_info *part_info, uint32 *part_id,
longlong *func_value);
static int get_part_id_charset_func_part(partition_info *part_info,
uint32 *part_id, longlong *func_value);
static int get_part_id_charset_func_subpart(partition_info *part_info,
uint32 *part_id);
static int get_partition_id_hash_nosub(partition_info *part_info,
uint32 *part_id, longlong *func_value);
static int get_partition_id_key_nosub(partition_info *part_info,
uint32 *part_id, longlong *func_value);
static int get_partition_id_linear_hash_nosub(partition_info *part_info,
uint32 *part_id,
longlong *func_value);
static int get_partition_id_linear_key_nosub(partition_info *part_info,
uint32 *part_id,
longlong *func_value);
static int get_partition_id_with_sub(partition_info *part_info, uint32 *part_id,
longlong *func_value);
static int get_partition_id_hash_sub(partition_info *part_info,
uint32 *part_id);
static int get_partition_id_key_sub(partition_info *part_info, uint32 *part_id);
static int get_partition_id_linear_hash_sub(partition_info *part_info,
uint32 *part_id);
static int get_partition_id_linear_key_sub(partition_info *part_info,
uint32 *part_id);
static uint32 get_next_partition_via_walking(PARTITION_ITERATOR *);
static void set_up_range_analysis_info(partition_info *part_info);
static uint32 get_next_subpartition_via_walking(PARTITION_ITERATOR *);
static uint32 get_partition_id_range_for_endpoint(partition_info *part_info,
bool left_endpoint,
bool include_endpoint);
static uint32 get_next_partition_id_list(PARTITION_ITERATOR *part_iter);
static int get_part_iter_for_interval_via_mapping(
partition_info *part_info, bool is_subpart, uint32 *store_length_array,
uchar *min_value, uchar *max_value, uint min_len, uint max_len, uint flags,
PARTITION_ITERATOR *part_iter);
static int get_part_iter_for_interval_cols_via_map(
partition_info *part_info, bool is_subpart, uint32 *store_length_array,
uchar *min_value, uchar *max_value, uint min_len, uint max_len, uint flags,
PARTITION_ITERATOR *part_iter);
static int get_part_iter_for_interval_via_walking(
partition_info *part_info, bool is_subpart, uint32 *store_length_array,
uchar *min_value, uchar *max_value, uint min_len, uint max_len, uint flags,
PARTITION_ITERATOR *part_iter);
static int cmp_rec_and_tuple(part_column_list_val *val, uint32 nvals_in_rec);
static int cmp_rec_and_tuple_prune(part_column_list_val *val,
uint32 n_vals_in_rec, bool is_left_endpoint,
bool include_endpoint);
static void set_field_ptr(Field **ptr, const uchar *new_buf,
const uchar *old_buf);
static uint32 get_list_array_idx_for_endpoint(partition_info *part_info,
bool left_endpoint,
bool include_endpoint);
/*
Convert constants in VALUES definition to the character set the
corresponding field uses.
SYNOPSIS
convert_charset_partition_constant()
item Item to convert
cs Character set to convert to
RETURN VALUE
NULL Error
item New converted item
*/
Item *convert_charset_partition_constant(Item *item, const CHARSET_INFO *cs) {
THD *thd = current_thd;
Name_resolution_context *context = &thd->lex->current_query_block()->context;
Table_ref *save_list = context->table_list;
const char *save_where = thd->where;
item = item->convert_charset(thd, cs);
if (item == nullptr) return nullptr;
context->table_list = nullptr;
thd->where = "convert character set partition constant";
if (item->fix_fields(thd, nullptr)) return nullptr;
thd->where = save_where;
context->table_list = save_list;
return item;
}
/**
A support function to check if a name is in a list of strings.
@param name String searched for
@param list_names A list of names searched in
@return True if if the name is in the list.
@retval true String found
@retval false String not found
*/
static bool is_name_in_list(const char *name, List<String> list_names) {
List_iterator<String> names_it(list_names);
uint num_names = list_names.elements;
uint i = 0;
do {
String *list_name = names_it++;
if (!(my_strcasecmp(system_charset_info, name, list_name->c_ptr())))
return true;
} while (++i < num_names);
return false;
}
/*
Set-up defaults for partitions.
SYNOPSIS
partition_default_handling()
table Table object
part_info Partition info to set up
is_create_table_ind Is this part of a table creation
normalized_path Normalized path name of table and database
RETURN VALUES
true Error
false Success
*/
static bool partition_default_handling(TABLE *table, partition_info *part_info,
bool is_create_table_ind,
const char *normalized_path) {
Partition_handler *part_handler = table->file->get_partition_handler();
DBUG_TRACE;
if (!part_handler) {
assert(0);
my_error(ER_PARTITION_CLAUSE_ON_NONPARTITIONED, MYF(0));
return true;
}
if (!is_create_table_ind) {
if (part_info->use_default_num_partitions) {
if (part_handler->get_num_parts(normalized_path, &part_info->num_parts)) {
return true;
}
} else if (part_info->is_sub_partitioned() &&
part_info->use_default_num_subpartitions) {
uint num_parts;
if (part_handler->get_num_parts(normalized_path, &num_parts)) {
return true;
}
assert(part_info->num_parts > 0);
assert((num_parts % part_info->num_parts) == 0);
part_info->num_subparts = num_parts / part_info->num_parts;
}
}
part_info->set_up_defaults_for_partitioning(part_handler, nullptr, 0U);
return false;
}
/*
A useful routine used by update_row for partition handlers to calculate
the partition ids of the old and the new record.
SYNOPSIS
get_parts_for_update()
old_data Buffer of old record
new_data Buffer of new record
rec0 Reference to table->record[0]
part_info Reference to partition information
out:old_part_id The returned partition id of old record
out:new_part_id The returned partition id of new record
RETURN VALUE
0 Success
> 0 Error code
*/
int get_parts_for_update(const uchar *old_data,
const uchar *new_data [[maybe_unused]],
const uchar *rec0, partition_info *part_info,
uint32 *old_part_id, uint32 *new_part_id,
longlong *new_func_value) {
Field **part_field_array = part_info->full_part_field_array;
int error;
longlong old_func_value;
DBUG_TRACE;
assert(new_data == rec0); // table->record[0]
set_field_ptr(part_field_array, old_data, rec0);
error = part_info->get_partition_id(part_info, old_part_id, &old_func_value);
set_field_ptr(part_field_array, rec0, old_data);
if (unlikely(error)) {
part_info->err_value = old_func_value;
return error;
}
if (unlikely((error = part_info->get_partition_id(part_info, new_part_id,
new_func_value)))) {
part_info->err_value = *new_func_value;
return error;
}
return 0;
}
/*
A useful routine used by delete_row for partition handlers to calculate
the partition id.
SYNOPSIS
get_part_for_delete()
buf Buffer of old record
rec0 Reference to table->record[0]
part_info Reference to partition information
out:part_id The returned partition id to delete from
RETURN VALUE
0 Success
> 0 Error code
DESCRIPTION
Dependent on whether buf is not record[0] we need to prepare the
fields. Then we call the function pointer get_partition_id to
calculate the partition id.
*/
int get_part_for_delete(const uchar *buf, const uchar *rec0,
partition_info *part_info, uint32 *part_id) {
int error;
longlong func_value;
DBUG_TRACE;
if (likely(buf == rec0)) {
if (unlikely((error = part_info->get_partition_id(part_info, part_id,
&func_value)))) {
part_info->err_value = func_value;
return error;
}
DBUG_PRINT("info", ("Delete from partition %d", *part_id));
} else {
Field **part_field_array = part_info->full_part_field_array;
set_field_ptr(part_field_array, buf, rec0);
error = part_info->get_partition_id(part_info, part_id, &func_value);
set_field_ptr(part_field_array, rec0, buf);
if (unlikely(error)) {
part_info->err_value = func_value;
return error;
}
DBUG_PRINT("info", ("Delete from partition %d (path2)", *part_id));
}
return 0;
}
/*
This method is used to set-up both partition and subpartitioning
field array and used for all types of partitioning.
It is part of the logic around fix_partition_func.
SYNOPSIS
set_up_field_array()
table TABLE object for which partition fields are set-up
sub_part Is the table subpartitioned as well
RETURN VALUE
true Error, some field didn't meet requirements
false Ok, partition field array set-up
DESCRIPTION
A great number of functions below here is part of the fix_partition_func
method. It is used to set up the partition structures for execution from
openfrm. It is called at the end of the openfrm when the table struct has
been set-up apart from the partition information.
It involves:
1) Setting arrays of fields for the partition functions.
2) Setting up binary search array for LIST partitioning
3) Setting up array for binary search for RANGE partitioning
4) Setting up key_map's to assist in quick evaluation whether one
can deduce anything from a given index of what partition to use
5) Checking whether a set of partitions can be derived from a range on
a field in the partition function.
As part of doing this there is also a great number of error controls.
This is actually the place where most of the things are checked for
partition information when creating a table.
Things that are checked includes
1) All fields of partition function in Primary keys and unique indexes
(if not supported)
Create an array of partition fields (NULL terminated). Before this method
is called fix_fields or find_table_in_sef has been called to set
GET_FIXED_FIELDS_FLAG on all fields that are part of the partition
function.
*/
static bool set_up_field_array(TABLE *table, bool is_sub_part) {
Field **ptr, *field, **field_array;
uint num_fields = 0;
uint size_field_array;
uint i = 0;
uint inx;
partition_info *part_info = table->part_info;
bool result = false;
DBUG_TRACE;
ptr = table->field;
while ((field = *(ptr++))) {
if (field->is_flag_set(GET_FIXED_FIELDS_FLAG)) num_fields++;
}
if (num_fields > MAX_REF_PARTS) {
const char *err_str;
if (is_sub_part)
err_str = "subpartition function";
else
err_str = "partition function";
my_error(ER_TOO_MANY_PARTITION_FUNC_FIELDS_ERROR, MYF(0), err_str);
return true;
}
if (num_fields == 0) {
/*
We are using hidden key as partitioning field
*/
assert(!is_sub_part);
return result;
}
size_field_array = (num_fields + 1) * sizeof(Field *);
field_array = (Field **)sql_calloc(size_field_array);
if (unlikely(!field_array)) {
mem_alloc_error(size_field_array);
result = true;
}
ptr = table->field;
while ((field = *(ptr++))) {
if (field->is_flag_set(GET_FIXED_FIELDS_FLAG)) {
field->clear_flag(GET_FIXED_FIELDS_FLAG);
field->set_flag(FIELD_IN_PART_FUNC_FLAG);
if (likely(!result)) {
if (!is_sub_part && part_info->column_list) {
List_iterator<char> it(part_info->part_field_list);
char *field_name;
assert(num_fields == part_info->part_field_list.elements);
inx = 0;
do {
field_name = it++;
if (!my_strcasecmp(system_charset_info, field_name,
field->field_name))
break;
} while (++inx < num_fields);
if (inx == num_fields) {
/*
Should not occur since it should already been checked in either
add_column_list_values, handle_list_of_fields,
check_partition_info etc.
*/
assert(0);
my_error(ER_FIELD_NOT_FOUND_PART_ERROR, MYF(0));
result = true;
continue;
}
} else
inx = i;
field_array[inx] = field;
i++;
/*
We check that the fields are proper. It is required for each
field in a partition function to:
1) Not be a BLOB of any type
A BLOB takes too long time to evaluate so we don't want it for
performance reasons.
*/
if (field->real_type() == MYSQL_TYPE_VECTOR) {
/* vector column as partition key is not supported */
my_error(ER_FIELD_TYPE_NOT_ALLOWED_AS_PARTITION_FIELD, MYF(0),
field->field_name);
result = true;
}
if (field->is_flag_set(BLOB_FLAG)) {
my_error(ER_BLOB_FIELD_IN_PART_FUNC_ERROR, MYF(0));
result = true;
}
}
}
}
field_array[num_fields] = nullptr;
if (!is_sub_part) {
part_info->part_field_array = field_array;
part_info->num_part_fields = num_fields;
} else {
part_info->subpart_field_array = field_array;
part_info->num_subpart_fields = num_fields;
}
return result;
}
/*
Create a field array including all fields of both the partitioning and the
subpartitioning functions.
SYNOPSIS
create_full_part_field_array()
thd Thread handle
table TABLE object for which partition fields are set-up
part_info Reference to partitioning data structure
RETURN VALUE
true Memory allocation of field array failed
false Ok
DESCRIPTION
If there is no subpartitioning then the same array is used as for the
partitioning. Otherwise a new array is built up using the flag
FIELD_IN_PART_FUNC in the field object.
This function is called from fix_partition_func
*/
static bool create_full_part_field_array(THD *thd, TABLE *table,
partition_info *part_info) {
bool result = false;
Field **ptr;
my_bitmap_map *bitmap_buf;
DBUG_TRACE;
if (!part_info->is_sub_partitioned()) {
part_info->full_part_field_array = part_info->part_field_array;
part_info->num_full_part_fields = part_info->num_part_fields;
} else {
Field *field, **field_array;
uint num_part_fields = 0, size_field_array;
ptr = table->field;
while ((field = *(ptr++))) {
if (field->is_flag_set(FIELD_IN_PART_FUNC_FLAG)) num_part_fields++;
}
size_field_array = (num_part_fields + 1) * sizeof(Field *);
field_array = (Field **)sql_calloc(size_field_array);
if (unlikely(!field_array)) {
mem_alloc_error(size_field_array);
result = true;
goto end;
}
num_part_fields = 0;
ptr = table->field;
while ((field = *(ptr++))) {
if (field->is_flag_set(FIELD_IN_PART_FUNC_FLAG))
field_array[num_part_fields++] = field;
}
field_array[num_part_fields] = nullptr;
part_info->full_part_field_array = field_array;
part_info->num_full_part_fields = num_part_fields;
}
/*
Initialize the set of all fields used in partition and subpartition
expression. Required for testing of partition fields in write_set
when updating. We need to set all bits in read_set because the row
may need to be inserted in a different [sub]partition.
*/
if (!(bitmap_buf = (my_bitmap_map *)thd->alloc(
bitmap_buffer_size(table->s->fields)))) {
mem_alloc_error(bitmap_buffer_size(table->s->fields));
result = true;
goto end;
}
if (bitmap_init(&part_info->full_part_field_set, bitmap_buf,
table->s->fields)) {
mem_alloc_error(table->s->fields);
result = true;
goto end;
}
/*
full_part_field_array may be NULL if storage engine supports native
partitioning.
*/
if ((ptr = part_info->full_part_field_array))
for (; *ptr; ptr++)
bitmap_set_bit(&part_info->full_part_field_set, (*ptr)->field_index());
end:
return result;
}
/*
Clear flag GET_FIXED_FIELDS_FLAG in all fields of a key previously set by
set_indicator_in_key_fields (always used in pairs).
SYNOPSIS
clear_indicator_in_key_fields()
key_info Reference to find the key fields
RETURN VALUE
NONE
DESCRIPTION
These support routines is used to set/reset an indicator of all fields
in a certain key. It is used in conjunction with another support routine
that traverse all fields in the PF to find if all or some fields in the
PF is part of the key. This is used to check primary keys and unique
keys involve all fields in PF (unless supported) and to derive the
key_map's used to quickly decide whether the index can be used to
derive which partitions are needed to scan.
*/
static void clear_indicator_in_key_fields(KEY *key_info) {
KEY_PART_INFO *key_part;
const uint key_parts = key_info->user_defined_key_parts;
uint i;
for (i = 0, key_part = key_info->key_part; i < key_parts; i++, key_part++)
key_part->field->clear_flag(GET_FIXED_FIELDS_FLAG);
}
/*
Set flag GET_FIXED_FIELDS_FLAG in all fields of a key.
SYNOPSIS
set_indicator_in_key_fields
key_info Reference to find the key fields
RETURN VALUE
NONE
*/
static void set_indicator_in_key_fields(KEY *key_info) {
KEY_PART_INFO *key_part;
const uint key_parts = key_info->user_defined_key_parts;
uint i;
for (i = 0, key_part = key_info->key_part; i < key_parts; i++, key_part++)
key_part->field->set_flag(GET_FIXED_FIELDS_FLAG);
}
/*
Check if all or some fields in partition field array is part of a key
previously used to tag key fields.
SYNOPSIS
check_fields_in_PF()
ptr Partition field array
out:all_fields Is all fields of partition field array used in key
out:some_fields Is some fields of partition field array used in key
RETURN VALUE
all_fields, some_fields
*/
static void check_fields_in_PF(Field **ptr, bool *all_fields,
bool *some_fields) {
DBUG_TRACE;
*all_fields = true;
*some_fields = false;
if ((!ptr) || !(*ptr)) {
*all_fields = false;
return;
}
do {
/* Check if the field of the PF is part of the current key investigated */
if ((*ptr)->is_flag_set(GET_FIXED_FIELDS_FLAG))
*some_fields = true;
else
*all_fields = false;
} while (*(++ptr));
}
/*
Clear flag GET_FIXED_FIELDS_FLAG in all fields of the table.
This routine is used for error handling purposes.
SYNOPSIS
clear_field_flag()
table TABLE object for which partition fields are set-up
RETURN VALUE
NONE
*/
static void clear_field_flag(TABLE *table) {
Field **ptr;
DBUG_TRACE;
for (ptr = table->field; *ptr; ptr++)
(*ptr)->clear_flag(GET_FIXED_FIELDS_FLAG);
}
/*
find_field_in_table_sef finds the field given its name. All fields get
GET_FIXED_FIELDS_FLAG set.
SYNOPSIS
handle_list_of_fields()
it A list of field names for the partition function
table TABLE object for which partition fields are set-up
part_info Reference to partitioning data structure
sub_part Is the table subpartitioned as well
RETURN VALUE
true Fields in list of fields not part of table
false All fields ok and array created
DESCRIPTION
This routine sets-up the partition field array for KEY partitioning, it
also verifies that all fields in the list of fields is actually a part of
the table.
*/
static bool handle_list_of_fields(List_iterator<char> it, TABLE *table,
partition_info *part_info, bool is_sub_part) {
bool result;
char *field_name;
bool is_list_empty = true;
DBUG_TRACE;
while ((field_name = it++)) {
is_list_empty = false;
Field *field = find_field_in_table_sef(table, field_name);
if (likely(field != nullptr))
field->set_flag(GET_FIXED_FIELDS_FLAG);
else {
my_error(ER_FIELD_NOT_FOUND_PART_ERROR, MYF(0));
clear_field_flag(table);
result = true;
goto end;
}
}
if (is_list_empty && part_info->part_type == partition_type::HASH) {
const uint primary_key = table->s->primary_key;
if (primary_key != MAX_KEY) {
const uint num_key_parts =
table->key_info[primary_key].user_defined_key_parts;
uint i;
/*
In the case of an empty list we use primary key as partition key.
*/
for (i = 0; i < num_key_parts; i++) {
Field *field = table->key_info[primary_key].key_part[i].field;
field->set_flag(GET_FIXED_FIELDS_FLAG);
}
} else {
if (table->s->db_type()->partition_flags &&
(table->s->db_type()->partition_flags() & HA_USE_AUTO_PARTITION)) {
/*
This engine can handle automatic partitioning and there is no
primary key. In this case we rely on that the engine handles
partitioning based on a hidden key. Thus we allocate no
array for partitioning fields.
*/
return false;
}
my_error(ER_FIELD_NOT_FOUND_PART_ERROR, MYF(0));
return true;
}
}
result = set_up_field_array(table, is_sub_part);
end:
return result;
}
/*
Support function to check if all VALUES * (expression) is of the
right sign (no signed constants when unsigned partition function)
SYNOPSIS
check_signed_flag()
part_info Partition info object
RETURN VALUES
0 No errors due to sign errors
>0 Sign error
*/
static int check_signed_flag(partition_info *part_info) {
int error = 0;
uint i = 0;
if (part_info->part_type != partition_type::HASH &&
part_info->part_expr->unsigned_flag) {
List_iterator<partition_element> part_it(part_info->partitions);
do {
partition_element *part_elem = part_it++;
if (part_elem->signed_flag) {
my_error(ER_PARTITION_CONST_DOMAIN_ERROR, MYF(0));
error = ER_PARTITION_CONST_DOMAIN_ERROR;
break;
}
} while (++i < part_info->num_parts);
}
return error;
}
/**
Initialize lex object for use in fix_fields and parsing.
@param thd The thread object
@param table The table object
@param lex The LEX object, must be initialized and contain query_block.
@returns false if success, true if error
@details
This function is used to set up a lex object on the
stack for resolving of fields from a single table.
*/
static bool init_lex_with_single_table(THD *thd, TABLE *table, LEX *lex) {
Query_block *query_block = lex->query_block;
Name_resolution_context *context = &query_block->context;
/*
We will call the parser to create a part_info struct based on the
partition string stored in the frm file.
We will use a local lex object for this purpose. However we also
need to set the Name_resolution_object for this lex object. We
do this by using add_table_to_list where we add the table that
we're working with to the Name_resolution_context.
*/
thd->lex = lex;
auto table_ident = new (thd->mem_root) Table_ident(
thd->get_protocol(), table->s->db, table->s->table_name, true);
if (table_ident == nullptr) return true;
Table_ref *table_list =
query_block->add_table_to_list(thd, table_ident, nullptr, 0);
if (table_list == nullptr) return true;
context->resolve_in_table_list_only(table_list);
lex->use_only_table_context = true;
table->get_fields_in_item_tree = true;
table_list->table = table;
table_list->cacheable_table = false;
return false;
}
/**
End use of local lex with single table
SYNOPSIS
end_lex_with_single_table()
@param thd The thread object
@param table The table object
@param old_lex The real lex object connected to THD
DESCRIPTION
This function restores the real lex object after calling
init_lex_with_single_table and also restores some table
variables temporarily set.
*/
static void end_lex_with_single_table(THD *thd, TABLE *table, LEX *old_lex) {
LEX *lex = thd->lex;
table->get_fields_in_item_tree = false;
lex_end(lex);
thd->lex = old_lex;
}
/*
The function uses a new feature in fix_fields where the flag
GET_FIXED_FIELDS_FLAG is set for all fields in the item tree.
This field must always be reset before returning from the function
since it is used for other purposes as well.
SYNOPSIS
fix_fields_part_func()
thd The thread object
func_expr The item tree reference of the partition function
table The table object
part_info Reference to partitioning data structure
is_sub_part Is the table subpartitioned as well
is_create_table_ind Indicator of whether openfrm was called as part of
CREATE or ALTER TABLE
RETURN VALUE
true An error occurred, something was wrong with the
partition function.
false Ok, a partition field array was created
DESCRIPTION
This function is used to build an array of partition fields for the
partitioning function and subpartitioning function. The partitioning
function is an item tree that must reference at least one field in the
table. This is checked first in the parser that the function doesn't
contain non-cacheable parts (like a random function) and by checking
here that the function isn't a constant function.
Calculate the number of fields in the partition function.
Use it allocate memory for array of Field pointers.
Initialise array of field pointers. Use information set when
calling fix_fields and reset it immediately after.
The get_fields_in_item_tree activates setting of bit in flags
on the field object.
The function must be called with thd->mem_root set to the memory
allocator associated with the TABLE object. Thus, any memory allocated
during resolving and other actions have the same lifetime as the TABLE.
Notice that memory allocations made during evaluation calls is NOT
supported, thus any item class that performs memory allocations during
evaluation calls must be disallowed as partition functions.
SEE Bug #21658
*/
static bool fix_fields_part_func(THD *thd, Item *func_expr, TABLE *table,
bool is_sub_part, bool is_create_table_ind) {
partition_info *part_info = table->part_info;
bool result = true;
int error;
LEX *old_lex = thd->lex;
LEX lex;
Query_expression unit(CTX_NONE);
Query_block select(thd->mem_root, nullptr, nullptr);
lex.new_static_query(&unit, &select);
DBUG_TRACE;
if (init_lex_with_single_table(thd, table, &lex)) goto end;
{
Item_ident::Change_context ctx(&lex.query_block->context);
func_expr->walk(&Item::change_context_processor, enum_walk::POSTFIX,
(uchar *)&ctx);
}
thd->where = "partition function";
if (unlikely(func_expr->fix_fields(thd, &func_expr))) {
DBUG_PRINT("info", ("Field in partition function not part of table"));
clear_field_flag(table);
goto end;
}
if (unlikely(func_expr->const_item())) {
my_error(ER_WRONG_EXPR_IN_PARTITION_FUNC_ERROR, MYF(0));
clear_field_flag(table);
goto end;
}
/*
We don't allow creating partitions with expressions with non matching
arguments as a (sub)partitioning function,
but we want to allow such expressions when opening existing tables for
easier maintenance. This exception should be deprecated at some point
in future so that we always throw an error.
*/
if (func_expr->walk(&Item::check_valid_arguments_processor,
enum_walk::POSTFIX, nullptr)) {
if (is_create_table_ind) {
my_error(ER_WRONG_EXPR_IN_PARTITION_FUNC_ERROR, MYF(0));
goto end;
} else
push_warning(thd, Sql_condition::SL_WARNING,
ER_WRONG_EXPR_IN_PARTITION_FUNC_ERROR,
ER_THD(thd, ER_WRONG_EXPR_IN_PARTITION_FUNC_ERROR));
}
if ((!is_sub_part) && (error = check_signed_flag(part_info))) goto end;
result = set_up_field_array(table, is_sub_part);
end:
end_lex_with_single_table(thd, table, old_lex);
#if !defined(NDEBUG)
Item_ident::Change_context nul_ctx(nullptr);
func_expr->walk(&Item::change_context_processor, enum_walk::POSTFIX,
(uchar *)&nul_ctx);