/
sql_partition.cc
8231 lines (7396 loc) · 275 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, 2017, Oracle and/or its affiliates.
Copyright (c) 2009, 2017, SkySQL Ab.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
/*
This file is a container for general functionality related
to partitioning introduced in MySQL version 5.1. 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 was written by Mikael Ronstrom 2004-2006.
Various parts of the optimizer code was written by Sergey Petrunia.
Code have been maintained by Mattias Jonsson.
The second version was written by Mikael Ronstrom 2006-2007 with some
final fixes for partition pruning in 2008-2009 with assistance from Sergey
Petrunia and Mattias Jonsson.
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 choosen.
*/
/* Some general useful functions */
#define MYSQL_LEX 1
#include "sql_priv.h"
#include "unireg.h" // REQUIRED: for other includes
#include "sql_partition.h"
#include "key.h" // key_restore
#include "sql_parse.h" // parse_sql
#include "sql_cache.h" // query_cache_invalidate3
#include "lock.h" // mysql_lock_remove
#include "sql_show.h" // append_identifier
#include <errno.h>
#include <m_ctype.h>
#include "my_md5.h"
#include "transaction.h"
#include "sql_base.h" // close_all_tables_for_name
#include "sql_table.h" // build_table_filename,
// build_table_shadow_filename,
// table_to_filename
// mysql_*_alter_copy_data
#include "opt_range.h" // store_key_image_to_rec
#include "sql_analyse.h" // append_escaped
#ifdef WITH_PARTITION_STORAGE_ENGINE
#include "ha_partition.h"
#define ERROR_INJECT_CRASH(code) \
DBUG_EVALUATE_IF(code, (DBUG_SUICIDE(), 0), 0)
#define ERROR_INJECT_ERROR(code) \
DBUG_EVALUATE_IF(code, (my_error(ER_UNKNOWN_ERROR, MYF(0)), TRUE), 0)
/*
Partition related functions declarations and some static constants;
*/
const LEX_STRING partition_keywords[]=
{
{ C_STRING_WITH_LEN("HASH") },
{ C_STRING_WITH_LEN("RANGE") },
{ C_STRING_WITH_LEN("LIST") },
{ C_STRING_WITH_LEN("KEY") },
{ C_STRING_WITH_LEN("MAXVALUE") },
{ C_STRING_WITH_LEN("LINEAR ") },
{ C_STRING_WITH_LEN(" COLUMNS") },
{ C_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= ",";
int get_partition_id_list_col(partition_info *part_info,
uint32 *part_id,
longlong *func_value);
int get_partition_id_list(partition_info *part_info,
uint32 *part_id,
longlong *func_value);
int get_partition_id_range_col(partition_info *part_info,
uint32 *part_id,
longlong *func_value);
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);
int get_partition_id_hash_nosub(partition_info *part_info,
uint32 *part_id,
longlong *func_value);
int get_partition_id_key_nosub(partition_info *part_info,
uint32 *part_id,
longlong *func_value);
int get_partition_id_linear_hash_nosub(partition_info *part_info,
uint32 *part_id,
longlong *func_value);
int get_partition_id_linear_key_nosub(partition_info *part_info,
uint32 *part_id,
longlong *func_value);
int get_partition_id_with_sub(partition_info *part_info,
uint32 *part_id,
longlong *func_value);
int get_partition_id_hash_sub(partition_info *part_info,
uint32 *part_id);
int get_partition_id_key_sub(partition_info *part_info,
uint32 *part_id);
int get_partition_id_linear_hash_sub(partition_info *part_info,
uint32 *part_id);
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*);
#endif
uint32 get_next_partition_id_range(PARTITION_ITERATOR* part_iter);
uint32 get_next_partition_id_list(PARTITION_ITERATOR* part_iter);
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);
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);
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);
#ifdef WITH_PARTITION_STORAGE_ENGINE
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);
/*
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, CHARSET_INFO *cs)
{
THD *thd= current_thd;
Name_resolution_context *context= &thd->lex->current_select->context;
TABLE_LIST *save_list= context->table_list;
const char *save_where= thd->where;
item= item->safe_charset_converter(cs);
context->table_list= NULL;
thd->where= "convert character set partition constant";
if (!item || item->fix_fields(thd, (Item**)NULL))
item= NULL;
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
SYNOPSIS
is_name_in_list()
name String searched for
list_names A list of names searched in
RETURN VALUES
TRUE String found
FALSE String not found
*/
bool is_name_in_list(char *name,
List<char> list_names)
{
List_iterator<char> names_it(list_names);
uint num_names= list_names.elements;
uint i= 0;
do
{
char *list_name= names_it++;
if (!(my_strcasecmp(system_charset_info, name, list_name)))
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
*/
bool partition_default_handling(TABLE *table, partition_info *part_info,
bool is_create_table_ind,
const char *normalized_path)
{
DBUG_ENTER("partition_default_handling");
if (!is_create_table_ind)
{
if (part_info->use_default_num_partitions)
{
if (table->file->get_no_parts(normalized_path, &part_info->num_parts))
{
DBUG_RETURN(TRUE);
}
}
else if (part_info->is_sub_partitioned() &&
part_info->use_default_num_subpartitions)
{
uint num_parts;
if (table->file->get_no_parts(normalized_path, &num_parts))
{
DBUG_RETURN(TRUE);
}
DBUG_ASSERT(part_info->num_parts > 0);
DBUG_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(table->file,
(ulonglong)0, (uint)0);
DBUG_RETURN(FALSE);
}
/*
Check that the reorganized table will not have duplicate partitions.
SYNOPSIS
check_reorganise_list()
new_part_info New partition info
old_part_info Old partition info
list_part_names The list of partition names that will go away and
can be reused in the new table.
RETURN VALUES
TRUE Inacceptable name conflict detected.
FALSE New names are OK.
DESCRIPTION
Can handle that the 'new_part_info' and 'old_part_info' the same
in which case it checks that the list of names in the partitions
doesn't contain any duplicated names.
*/
bool check_reorganise_list(partition_info *new_part_info,
partition_info *old_part_info,
List<char> list_part_names)
{
uint new_count, old_count;
uint num_new_parts= new_part_info->partitions.elements;
uint num_old_parts= old_part_info->partitions.elements;
List_iterator<partition_element> new_parts_it(new_part_info->partitions);
bool same_part_info= (new_part_info == old_part_info);
DBUG_ENTER("check_reorganise_list");
new_count= 0;
do
{
List_iterator<partition_element> old_parts_it(old_part_info->partitions);
char *new_name= (new_parts_it++)->partition_name;
new_count++;
old_count= 0;
do
{
char *old_name= (old_parts_it++)->partition_name;
old_count++;
if (same_part_info && old_count == new_count)
break;
if (!(my_strcasecmp(system_charset_info, old_name, new_name)))
{
if (!is_name_in_list(old_name, list_part_names))
DBUG_RETURN(TRUE);
}
} while (old_count < num_old_parts);
} while (new_count < num_new_parts);
DBUG_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_part_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, uchar *new_data,
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_ENTER("get_parts_for_update");
DBUG_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)) // Should never happen
{
DBUG_ASSERT(0);
DBUG_RETURN(error);
}
#ifdef NOT_NEEDED
if (new_data == rec0)
#endif
{
if (unlikely(error= part_info->get_partition_id(part_info,
new_part_id,
new_func_value)))
{
DBUG_RETURN(error);
}
}
#ifdef NOT_NEEDED
else
{
/*
This branch should never execute but it is written anyways for
future use. It will be tested by ensuring that the above
condition is false in one test situation before pushing the code.
*/
set_field_ptr(part_field_array, new_data, rec0);
error= part_info->get_partition_id(part_info, new_part_id,
new_func_value);
set_field_ptr(part_field_array, rec0, new_data);
if (unlikely(error))
{
DBUG_RETURN(error);
}
}
#endif
DBUG_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_ENTER("get_part_for_delete");
if (likely(buf == rec0))
{
if (unlikely((error= part_info->get_partition_id(part_info, part_id,
&func_value))))
{
DBUG_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))
{
DBUG_RETURN(error);
}
DBUG_PRINT("info", ("Delete from partition %d (path2)", *part_id));
}
DBUG_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;
int result= FALSE;
DBUG_ENTER("set_up_field_array");
ptr= table->field;
while ((field= *(ptr++)))
{
if (field->flags & GET_FIXED_FIELDS_FLAG)
num_fields++;
}
if (num_fields > MAX_REF_PARTS)
{
char *err_str;
if (is_sub_part)
err_str= (char*)"subpartition function";
else
err_str= (char*)"partition function";
my_error(ER_TOO_MANY_PARTITION_FUNC_FIELDS_ERROR, MYF(0), err_str);
DBUG_RETURN(TRUE);
}
if (num_fields == 0)
{
/*
We are using hidden key as partitioning field
*/
DBUG_ASSERT(!is_sub_part);
DBUG_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->flags & GET_FIXED_FIELDS_FLAG)
{
field->flags&= ~GET_FIXED_FIELDS_FLAG;
field->flags|= 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;
DBUG_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)
{
mem_alloc_error(1);
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 (unlikely(field->flags & BLOB_FLAG))
{
my_error(ER_BLOB_FIELD_IN_PART_FUNC_ERROR, MYF(0));
result= TRUE;
}
}
}
}
field_array[num_fields]= 0;
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;
}
DBUG_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_ENTER("create_full_part_field_array");
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->flags & 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->flags & FIELD_IN_PART_FUNC_FLAG)
field_array[num_part_fields++]= field;
}
field_array[num_part_fields]=0;
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, FALSE))
{
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:
DBUG_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;
uint key_parts= key_info->key_parts, i;
for (i= 0, key_part=key_info->key_part; i < key_parts; i++, key_part++)
key_part->field->flags&= (~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;
uint key_parts= key_info->key_parts, i;
for (i= 0, key_part=key_info->key_part; i < key_parts; i++, key_part++)
key_part->field->flags|= 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_ENTER("check_fields_in_PF");
*all_fields= TRUE;
*some_fields= FALSE;
if ((!ptr) || !(*ptr))
{
*all_fields= FALSE;
DBUG_VOID_RETURN;
}
do
{
/* Check if the field of the PF is part of the current key investigated */
if ((*ptr)->flags & GET_FIXED_FIELDS_FLAG)
*some_fields= TRUE;
else
*all_fields= FALSE;
} while (*(++ptr));
DBUG_VOID_RETURN;
}
/*
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_ENTER("clear_field_flag");
for (ptr= table->field; *ptr; ptr++)
(*ptr)->flags&= (~GET_FIXED_FIELDS_FLAG);
DBUG_VOID_RETURN;
}
/*
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)
{
Field *field;
bool result;
char *field_name;
bool is_list_empty= TRUE;
DBUG_ENTER("handle_list_of_fields");
while ((field_name= it++))
{
is_list_empty= FALSE;
field= find_field_in_table_sef(table, field_name);
if (likely(field != 0))
field->flags|= 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 == HASH_PARTITION)
{
uint primary_key= table->s->primary_key;
if (primary_key != MAX_KEY)
{
uint num_key_parts= table->key_info[primary_key].key_parts, 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->flags|= GET_FIXED_FIELDS_FLAG;
}
}
else
{
if (table->s->db_type()->partition_flags &&
(table->s->db_type()->partition_flags() & HA_USE_AUTO_PARTITION) &&
(table->s->db_type()->partition_flags() & HA_CAN_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.
*/
DBUG_RETURN(FALSE);
}
else
{
my_error(ER_FIELD_NOT_FOUND_PART_ERROR, MYF(0));
DBUG_RETURN(TRUE);
}
}
}
result= set_up_field_array(table, is_sub_part);
end:
DBUG_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
*/
int check_signed_flag(partition_info *part_info)
{
int error= 0;
uint i= 0;
if (part_info->part_type != HASH_PARTITION &&
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;
}
/*
init_lex_with_single_table and end_lex_with_single_table
are now in sql_lex.cc
*/
/*
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.
*/
static bool fix_fields_part_func(THD *thd, Item* func_expr, TABLE *table,