File: partition_handler.h

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (1144 lines) | stat: -rw-r--r-- 38,386 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
#ifndef PARTITION_HANDLER_INCLUDED
#define PARTITION_HANDLER_INCLUDED

/*
   Copyright (c) 2005, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
*/

#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <memory>
#include <string>
#include <vector>

#include "my_base.h"  // ha_rows.
#include "my_bitmap.h"
#include "my_compiler.h"

#include "my_inttypes.h"
#include "my_sys.h"
#include "mysql/psi/mysql_mutex.h"
#include "priority_queue.h"
#include "sql/handler.h"        // Handler_share
#include "sql/key.h"            // key_rec_cmp
#include "sql/sql_partition.h"  // part_id_range

class Field;
class THD;
class partition_info;
struct mysql_mutex_t;
template <class Key, class Value>
class collation_unordered_map;

namespace dd {
class Table;
}  // namespace dd
struct TABLE;
struct TABLE_SHARE;

#define PARTITION_BYTES_IN_POS 2

struct MEM_ROOT;

static const uint NO_CURRENT_PART_ID = UINT_MAX32;

/**
  bits in Partition_handler::alter_flags():

  HA_PARTITION_FUNCTION_SUPPORTED indicates that the function is
  supported at all.
  HA_INPLACE_CHANGE_PARTITION means that changes to partitioning can be done
  through in-place ALTER TABLE API but special mark-up in partition_info
  object is required for this.
*/
#define HA_PARTITION_FUNCTION_SUPPORTED (1L << 0)
#define HA_INPLACE_CHANGE_PARTITION (1L << 1)

enum enum_part_operation {
  OPTIMIZE_PARTS = 0,
  ANALYZE_PARTS,
  CHECK_PARTS,
  REPAIR_PARTS,
  ASSIGN_KEYCACHE_PARTS,
  PRELOAD_KEYS_PARTS
};

/** Struct used for partition_name_hash */
struct PART_NAME_DEF {
  uchar *partition_name;
  uint length;
  uint32 part_id;
  bool is_subpart;
};

/**
  Initialize partitioning (currently only PSI keys).
*/
void partitioning_init();

/**
  Partition specific Handler_share.
*/
class Partition_share : public Handler_share {
 public:
  Partition_share();
  ~Partition_share() override;

  /** Set if auto increment is used an initialized. */
  bool auto_inc_initialized;
  /**
    Mutex protecting next_auto_inc_val.
    Initialized if table uses auto increment.
  */
  mysql_mutex_t *auto_inc_mutex;
  /** First non reserved auto increment value. */
  ulonglong next_auto_inc_val;
  /**
    Hash of partition names. Initialized by the first handler instance of a
    table_share calling populate_partition_name_hash().
    After that it is read-only, i.e. no locking required for reading.
  */
  std::unique_ptr<
      collation_unordered_map<std::string, unique_ptr_my_free<PART_NAME_DEF>>>
      partition_name_hash;

  /**
    Initializes and sets auto_inc_mutex.
    Only needed to be called if the table have an auto increment.
    Must hold TABLE_SHARE::LOCK_ha_data when calling.
  */
  bool init_auto_inc_mutex(TABLE_SHARE *table_share);
  /**
    Release reserved auto increment values not used.
    @param thd             Thread.
    @param table_share     Table Share
    @param next_insert_id  Next insert id (first non used auto inc value).
    @param max_reserved    End of reserved auto inc range.
  */
  void release_auto_inc_if_possible(THD *thd, TABLE_SHARE *table_share,
                                    const ulonglong next_insert_id,
                                    const ulonglong max_reserved);

  /** lock mutex protecting auto increment value next_auto_inc_val. */
  inline void lock_auto_inc() {
    assert(auto_inc_mutex);
    mysql_mutex_lock(auto_inc_mutex);
  }
  /** unlock mutex protecting auto increment value next_auto_inc_val. */
  inline void unlock_auto_inc() {
    assert(auto_inc_mutex);
    mysql_mutex_unlock(auto_inc_mutex);
  }
  /**
    Populate partition_name_hash with partition and subpartition names
    from part_info.
    @param part_info  Partition info containing all partitions metadata.

    @return Operation status.
      @retval false Success.
      @retval true  Failure.
  */
  bool populate_partition_name_hash(partition_info *part_info);
  /** Get partition name.

  @param part_id  Partition id (for subpartitioned table only subpartition
                  names will be returned.)

  @return partition name or NULL if error.
  */
  const char *get_partition_name(size_t part_id) const;

 private:
  const uchar **partition_names;
  /**
    Insert [sub]partition name into  partition_name_hash
    @param name        Partition name.
    @param part_id     Partition id.
    @param is_subpart  True if subpartition else partition.

    @return Operation status.
      @retval false Success.
      @retval true  Failure.
  */
  bool insert_partition_name_in_hash(const char *name, uint part_id,
                                     bool is_subpart);
};

/**
  Class for partitioning specific operations.

  Returned from handler::get_partition_handler().
*/
class Partition_handler {
 public:
  Partition_handler() = default;
  virtual ~Partition_handler() = default;

  /**
    Get dynamic table information from partition.

    @param[out] stat_info  Statistics struct to fill in.
    @param[out] check_sum  Check sum value to fill in if supported.
    @param[in]  part_id    Partition to report for.

    @note stat_info and check_sum are initialized by caller.
    check_sum is only expected to be updated if HA_HAS_CHECKSUM.
  */
  virtual void get_dynamic_partition_info(ha_statistics *stat_info,
                                          ha_checksum *check_sum,
                                          uint part_id) = 0;
  /**
    Get default number of partitions.

    Used during creating a partitioned table.

    @param info  Create info.
    @return Number of default partitions.
  */
  virtual int get_default_num_partitions(HA_CREATE_INFO *info
                                         [[maybe_unused]]) {
    return 1;
  }
  /**
    Setup auto partitioning.

    Called for engines with HA_USE_AUTO_PARTITION to setup the partition info
    object

    @param[in,out] part_info  Partition object to setup.
  */
  virtual void set_auto_partitions(partition_info *part_info [[maybe_unused]]) {
  }
  /**
    Get number of partitions for table in SE

    @param name normalized path(same as open) to the table

    @param[out] num_parts Number of partitions

    @retval false for success
    @retval true for failure, for example table didn't exist in engine
  */
  virtual bool get_num_parts(const char *name [[maybe_unused]],
                             uint *num_parts) {
    *num_parts = 0;
    return false;
  }
  /**
    Set the partition info object to be used by the handler.

    @param part_info  Partition info to be used by the handler.
    @param early      True if called when part_info only created and parsed,
                      but not setup, checked or fixed.
  */
  virtual void set_part_info(partition_info *part_info, bool early) = 0;

  /**
    Truncate partitions.

    Truncate all partitions matching table->part_info->read_partitions.
    Handler level wrapper for truncating partitions, will ensure that
    mark_trx_read_write() is called and also checks locking assertions.

    @param[in,out]  table_def    dd::Table object for the table. Engines
                                 which support atomic DDL are allowed to
                                 adjust this object. Changes will be saved
                                 to the data-dictionary.

    @return Operation status.
      @retval    0  Success.
      @retval != 0  Error code.
  */
  int truncate_partition(dd::Table *table_def);

  /**
    Exchange partition.

    @param[in]      part_id           Id of partition to be exchanged.
    @param[in,out]  part_table_def    dd::Table object for partitioned table.
    @param[in,out]  swap_table_def    dd::Table object for non-partitioned
                                      table.

    @note   Both tables are locked in exclusive mode.

    @note   Changes to dd::Table object done by this method will be saved
            to data-dictionary only if storage engine supporting atomic
            DDL (i.e. with HTON_SUPPORTS_ATOMIC_DDL flag).

    @return Operation status.
      @retval    0  Success.
      @retval != 0  Error code.
  */
  int exchange_partition(uint part_id, dd::Table *part_table_def,
                         dd::Table *swap_table_def);

  /**
    Alter flags.

    Given a set of alter table flags, return which is supported.

    @param flags  Alter table operation flags.

    @return Supported alter table flags.
  */
  virtual uint alter_flags(uint flags [[maybe_unused]]) const { return 0; }

  /**
    Get partition row type from SE
    @param       table      partition table
    @param       part_id    Id of partition for which row type to be retrieved
    @return      Partition row type.
  */
  virtual enum row_type get_partition_row_type(const dd::Table *table,
                                               uint part_id) = 0;

 private:
  /**
    Truncate partition.

    Low-level primitive for handler, implementing
    Partition_handler::truncate_partition().

    @sa Partition_handler::truncate_partition().
  */
  virtual int truncate_partition_low(dd::Table *) {
    return HA_ERR_WRONG_COMMAND;
  }

  /**
    Exchange partition.

    Low-level primitive which implementation to be provided by SE.

    @sa Partition_handler::exchange_partition().
  */
  virtual int exchange_partition_low(uint part_id [[maybe_unused]],
                                     dd::Table *part_table_def [[maybe_unused]],
                                     dd::Table *swap_table_def
                                     [[maybe_unused]]) {
    return HA_ERR_WRONG_COMMAND;
  }

  /**
    Return the table handler.

    For some partitioning specific functions it is still needed to access
    the handler directly for transaction handling (mark_trx_read_write())
    and to assert correct locking.

    @return handler or NULL if not supported.
  */
  virtual handler *get_handler() { return nullptr; }
};

/// Maps compare function to strict weak ordering required by Priority_queue.
struct Key_rec_less {
  typedef int (*key_compare_fun)(KEY **, uchar *, uchar *);

  explicit Key_rec_less(KEY **keys)
      : m_keys(keys), m_fun(key_rec_cmp), m_max_at_top(false) {}

  bool operator()(uchar *first, uchar *second) {
    const int cmpval =
        (*m_fun)(m_keys, first + m_rec_offset, second + m_rec_offset);
    return m_max_at_top ? cmpval < 0 : cmpval > 0;
  }

  KEY **m_keys;
  key_compare_fun m_fun;
  uint m_rec_offset;
  bool m_max_at_top;
};

/**
  Partition_helper is a helper class that implements most generic partitioning
  functionality such as:
  table scan, index scan (both ordered and non-ordered),
  insert (write_row()), delete and update.
  And includes ALTER TABLE ... ADD/COALESCE/DROP/REORGANIZE/... PARTITION
  support.
  It also implements a cache for the auto increment value and check/repair for
  rows in wrong partition.

  How to use it:
  Inherit it and implement:
  - *_in_part() functions for row operations.
  - write_row_in_new_part() for handling 'fast' alter partition.
*/
class Partition_helper {
  typedef Priority_queue<uchar *, std::vector<uchar *>, Key_rec_less>
      Prio_queue;

 public:
  Partition_helper(handler *main_handler);
  virtual ~Partition_helper();

  /**
    Set partition info.

    To be called from Partition_handler.

    @param  part_info  Partition info to use.
    @param  early      True if called when part_info only created and parsed,
                       but not setup, checked or fixed.
  */
  virtual void set_part_info_low(partition_info *part_info, bool early);
  /**
    Initialize variables used before the table is opened.

    @param mem_root  Memory root to allocate things from (not yet used).

    @return Operation status.
      @retval false success.
      @retval true  failure.
  */
  bool init_partitioning(MEM_ROOT *mem_root [[maybe_unused]]) {
#ifndef NDEBUG
    m_key_not_found_partitions.bitmap = nullptr;
#endif
    return false;
  }

  /**
    INSERT/UPDATE/DELETE functions.
    @see handler.h
    @{
  */

  /**
    Insert a row to the partitioned table.
      @returns Operation status.
      @returns    0 Success
      @returns != 0 Error code
  */
  int ph_write_row(uchar *buf);
  /**
    Update an existing row in the partitioned table.

    Yes, update_row() does what you expect, it updates a row. old_data will
    have the previous row record in it, while new_data will have the newest
    data in it.
    Keep in mind that the server can do updates based on ordering if an
    ORDER BY clause was used. Consecutive ordering is not guaranteed.

    If the new record belongs to a different partition than the old record
    then it will be inserted into the new partition and deleted from the old.

    new_data is always record[0]
    old_data is always record[1]

    @return Operation status.
      @returns    0 Success
      @returns != 0 Error code
  */
  int ph_update_row(const uchar *old_data, uchar *new_data);
  /**
    Delete an existing row in the partitioned table.

    This will delete a row. buf will contain a copy of the row to be deleted.
    The server will call this right after the current row has been read
    (from either a previous rnd_xxx() or index_xxx() call).
    If you keep a pointer to the last row or can access a primary key it will
    make doing the deletion quite a bit easier.
    Keep in mind that the server does no guarantee consecutive deletions.
    ORDER BY clauses can be used.

    buf is either record[0] or record[1]

    @param buf  The record in MySQL Row Format.

    @return Operation status.
      @retval    0 Success
      @retval != 0 Error code
  */
  int ph_delete_row(const uchar *buf);

  /** @} */

  /** Release unused auto increment values. */
  void ph_release_auto_increment();
  /**
    Calculate key hash value from an null terminated array of fields.
    Support function for KEY partitioning.

    @param field_array   An array of the fields in KEY partitioning

    @return hash_value calculated

    @note Uses the hash function on the character set of the field.
    Integer and floating point fields use the binary character set by default.
  */
  static uint32 ph_calculate_key_hash_value(Field **field_array);

  /**
    MODULE full table scan

    This module is used for the most basic access method for any table
    handler. This is to fetch all data through a full table scan. No
    indexes are needed to implement this part.
    It contains one method to start the scan (rnd_init) that can also be
    called multiple times (typical in a nested loop join). Then proceeding
    to the next record (rnd_next) and closing the scan (rnd_end).
    To remember a record for later access there is a method (position)
    and there is a method used to retrieve the record based on the stored
    position.
    The position can be a file position, a primary key, a ROWID dependent
    on the handler below.

    unlike index_init(), rnd_init() can be called two times
    without rnd_end() in between (it only makes sense if scan=1).
    then the second call should prepare for the new table scan
    (e.g if rnd_init allocates the cursor, second call should
    position it to the start of the table, no need to deallocate
    and allocate it again.
    @see handler.h
    @{
  */

  int ph_rnd_init(bool scan);
  int ph_rnd_end();
  int ph_rnd_next(uchar *buf);
  void ph_position(const uchar *record);

  /** @} */

  /**
    MODULE index scan

    This part of the handler interface is used to perform access through
    indexes. The interface is defined as a scan interface but the handler
    can also use key lookup if the index is a unique index or a primary
    key index.
    Index scans are mostly useful for SELECT queries but are an important
    part also of UPDATE, DELETE, REPLACE and CREATE TABLE table AS SELECT
    and so forth.
    Naturally an index is needed for an index scan and indexes can either
    be ordered, hash based. Some ordered indexes can return data in order
    but not necessarily all of them.
    There are many flags that define the behavior of indexes in the
    various handlers. These methods are found in the optimizer module.
    -------------------------------------------------------------------------

    index_read is called to start a scan of an index. The find_flag defines
    the semantics of the scan. These flags are defined in
    include/my_base.h
    index_read_idx is the same but also initializes index before calling doing
    the same thing as index_read. Thus it is similar to index_init followed
    by index_read. This is also how we implement it.

    index_read/index_read_idx does also return the first row. Thus for
    key lookups, the index_read will be the only call to the handler in
    the index scan.

    index_init initializes an index before using it and index_end does
    any end processing needed.
    @{
  */

  int ph_index_init_setup(uint key_nr, bool sorted);
  /*
    These methods are used to jump to next or previous entry in the index
    scan. There are also methods to jump to first and last entry.
  */
  int ph_index_first(uchar *buf);
  int ph_index_last(uchar *buf);
  int ph_index_next(uchar *buf);
  int ph_index_next_same(uchar *buf, uint keylen);
  int ph_index_prev(uchar *buf);
  int ph_index_read_map(uchar *buf, const uchar *key, key_part_map keypart_map,
                        enum ha_rkey_function find_flag);
  int ph_index_read_last_map(uchar *buf, const uchar *key,
                             key_part_map keypart_map);
  int ph_index_read_idx_map(uchar *buf, uint index, const uchar *key,
                            key_part_map keypart_map,
                            enum ha_rkey_function find_flag);
  int ph_read_range_first(const key_range *start_key, const key_range *end_key,
                          bool eq_range_arg, bool sorted);
  int ph_read_range_next();
  /** @} */

  /**
    Functions matching Partition_handler API.
    @{
  */

  /**
    Get statistics from a specific partition.
    @param[out] stat_info  Area to report values into.
    @param[out] check_sum  Check sum of partition.
    @param[in]  part_id    Partition to report from.
  */
  virtual void get_dynamic_partition_info_low(ha_statistics *stat_info,
                                              ha_checksum *check_sum,
                                              uint part_id);

  /**
    Prepare for reorganizing partitions by setting up
    partition_info::read_partitions according to the partition_info
    mark-up.

    This is helper method which can also be used by SEs implementing
    support for reorganizing partitions through ALTER TABLE INPLACE
    SE API.
  */
  void prepare_change_partitions();

  /** @} */

 protected:
  /* Common helper functions to be used by inheriting engines. */

  /*
    open/close functions.
  */

  /**
    Set m_part_share, Allocate internal bitmaps etc. used by open tables.

    @return Operation status.
      @returns false success.
      @returns true  failure.
  */
  bool open_partitioning(Partition_share *part_share);
  /**
    Close partitioning for a table.

    Frees memory and release other resources.
  */
  void close_partitioning();

  /**
    Lock auto increment value if needed.
  */
  void lock_auto_increment();

  /**
    unlock auto increment.
  */
  inline void unlock_auto_increment() {
    /*
      If m_auto_increment_safe_stmt_log_lock is true, we have to keep the lock.
      It will be set to false and thus unlocked at the end of the statement by
      ha_partition::release_auto_increment.
    */
    if (m_auto_increment_lock && !m_auto_increment_safe_stmt_log_lock) {
      m_part_share->unlock_auto_inc();
      m_auto_increment_lock = false;
    }
  }

  /**
    Get a range of auto increment values.

    Can only be used if the auto increment field is the first field in an index.

    This method is called by update_auto_increment which in turn is called
    by the individual handlers as part of write_row. We use the
    part_share->next_auto_inc_val, or search all
    partitions for the highest auto_increment_value if not initialized or
    if auto_increment field is a secondary part of a key, we must search
    every partition when holding a mutex to be sure of correctness.

    @param[in]   increment           Increment value.
    @param[in]   nb_desired_values   Number of desired values.
    @param[out]  first_value         First auto inc value reserved
                                        or MAX if failure.
    @param[out]  nb_reserved_values  Number of values reserved.
   */
  void get_auto_increment_first_field(ulonglong increment,
                                      ulonglong nb_desired_values,
                                      ulonglong *first_value,
                                      ulonglong *nb_reserved_values);

  /**
    Initialize the record priority queue used for sorted index scans.
    @return Operation status.
      @retval    0   Success.
      @retval != 0   Error code.
  */
  int init_record_priority_queue();
  /**
    Destroy the record priority queue used for sorted index scans.
  */
  void destroy_record_priority_queue();
  /*
    Administrative support functions.
  */

  /** Print partitioning specific error.
    @param error   Error code.
    @return false if error is printed else true.
  */
  bool print_partition_error(int error);
  /**
    Print a message row formatted for ANALYZE/CHECK/OPTIMIZE/REPAIR TABLE.

    Modeled after mi_check_print_msg.

    @param thd         Thread context.
    @param len         Needed length for message buffer.
    @param msg_type    Message type.
    @param db_name     Database name.
    @param table_name  Table name.
    @param op_name     Operation name.
    @param fmt         Message (in printf format with additional arguments).

    @return Operation status.
      @retval false for success else true.
  */
  bool print_admin_msg(THD *thd, uint len, const char *msg_type,
                       const char *db_name, const char *table_name,
                       const char *op_name, const char *fmt, ...)
      MY_ATTRIBUTE((format(printf, 8, 9)));

  /**
    Check/fix misplaced rows.

    @param read_part_id  Partition to check/fix.
    @param repair   If true, move misplaced rows to correct partition.

    @return Operation status.
      @retval    0  Success
      @retval != 0  Error
  */
  int check_misplaced_rows(uint read_part_id, bool repair);
  /**
    Set used partitions bitmap from Alter_info.

    @return false if success else true.
  */
  bool set_altered_partitions();

  /**
    Copy partitions as part of ALTER TABLE of partitions.

    SE and prepare_change_partitions has done all the preparations,
    now it is time to actually copy the data from the reorganized
    partitions to the new partitions.

    @param[out] deleted  Number of records deleted.

    @return Operation status
      @retval  0  Success
      @retval >0  Error code
  */
  virtual int copy_partitions(ulonglong *const deleted);

 private:
  enum partition_index_scan_type {
    PARTITION_INDEX_READ = 1,
    PARTITION_INDEX_FIRST,
    PARTITION_INDEX_FIRST_UNORDERED,
    PARTITION_INDEX_LAST,
    PARTITION_INDEX_READ_LAST,
    PARTITION_READ_RANGE,
    PARTITION_NO_INDEX_SCAN
  };

  /** handler to use (ha_innopart etc.) */
  handler *m_handler;
  /*
    Access methods to protected areas in handler to avoid adding
    friend class Partition_helper in class handler.
  */
  virtual THD *get_thd() const = 0;
  virtual TABLE *get_table() const = 0;
  virtual bool get_eq_range() const = 0;
  virtual void set_eq_range(bool eq_range) = 0;
  virtual void set_range_key_part(KEY_PART_INFO *key_part) = 0;

  /*
    Implementation of per partition operation by instantiated engine.
    These must be implemented in the 'real' partition_helper subclass.
  */

  /**
    Write a row in the specified partition.

    @see handler::write_row().

    @param  part_id  Partition to write to.
    @param  buf      Buffer with data to write.

    @return Operation status.
      @retval    0  Success.
      @retval != 0  Error code.
  */
  virtual int write_row_in_part(uint part_id, uchar *buf) = 0;
  /**
    Update a row in the specified partition.

    @see handler::update_row().

    @param  part_id   Partition to update in.
    @param  old_data  Buffer containing old row.
    @param  new_data  Buffer containing new row.

    @return Operation status.
      @retval    0  Success.
      @retval != 0  Error code.
  */
  virtual int update_row_in_part(uint part_id, const uchar *old_data,
                                 uchar *new_data) = 0;
  /**
    Delete an existing row in the specified partition.

    @see handler::delete_row().

    @param  part_id  Partition to delete from.
    @param  buf      Buffer containing row to delete.

    @return Operation status.
      @retval    0  Success.
      @retval != 0  Error code.
  */
  virtual int delete_row_in_part(uint part_id, const uchar *buf) = 0;
  /**
    Initialize the shared auto increment value.

    @param no_lock  If HA_STATUS_NO_LOCK should be used in info(HA_STATUS_AUTO).

    Also sets stats.auto_increment_value.
  */
  virtual int initialize_auto_increment(bool no_lock) = 0;
  /** Release auto_increment in all underlying partitions. */
  virtual void release_auto_increment_all_parts() {}
  /** Save or persist the current max auto increment. */
  virtual void save_auto_increment(ulonglong nr [[maybe_unused]]) {}
  /**
    Per partition equivalent of rnd_* and index_* functions.

    @see class handler.
  */
  virtual int rnd_init_in_part(uint part_id, bool table_scan) = 0;
  int ph_rnd_next_in_part(uint part_id, uchar *buf);
  virtual int rnd_next_in_part(uint part_id, uchar *buf) = 0;
  virtual int rnd_end_in_part(uint part_id, bool scan) = 0;
  virtual void position_in_last_part(uchar *ref, const uchar *row) = 0;
  virtual int index_first_in_part(uint part, uchar *buf) = 0;
  virtual int index_last_in_part(uint part, uchar *buf) = 0;
  virtual int index_prev_in_part(uint part, uchar *buf) = 0;
  virtual int index_next_in_part(uint part, uchar *buf) = 0;
  virtual int index_next_same_in_part(uint part, uchar *buf, const uchar *key,
                                      uint length) = 0;
  virtual int index_read_map_in_part(uint part, uchar *buf, const uchar *key,
                                     key_part_map keypart_map,
                                     enum ha_rkey_function find_flag) = 0;
  virtual int index_read_last_map_in_part(uint part, uchar *buf,
                                          const uchar *key,
                                          key_part_map keypart_map) = 0;
  /**
    Do read_range_first in the specified partition.
    If buf is set, then copy the result there instead of table->record[0].
  */
  virtual int read_range_first_in_part(uint part, uchar *buf,
                                       const key_range *start_key,
                                       const key_range *end_key,
                                       bool sorted) = 0;
  /**
    Do read_range_next in the specified partition.
    If buf is set, then copy the result there instead of table->record[0].
  */
  virtual int read_range_next_in_part(uint part, uchar *buf) = 0;
  virtual int index_read_idx_map_in_part(uint part, uchar *buf, uint index,
                                         const uchar *key,
                                         key_part_map keypart_map,
                                         enum ha_rkey_function find_flag) = 0;
  /**
    Initialize engine specific resources for the record priority queue
    used duing ordered index reads for multiple partitions.

    @param used_parts  Number of partitions used in query
                       (number of set bits in m_part_info->read_partitions).

    @return Operation status.
      @retval    0   Success.
      @retval != 0   Error code.
  */
  virtual int init_record_priority_queue_for_parts(uint used_parts
                                                   [[maybe_unused]]) {
    return 0;
  }
  /**
    Destroy and release engine specific resources used by the record
    priority queue.
  */
  virtual void destroy_record_priority_queue_for_parts() {}
  /**
    Checksum for a partition.

    @param part_id  Partition to checksum.
  */
  virtual ha_checksum checksum_in_part(uint part_id [[maybe_unused]]) const {
    assert(0);
    return 0;
  }
  /**
    Copy a cached row.

    Used when copying a row from the record priority queue to the return buffer.
    For some engines, like InnoDB, only marked columns must be copied,
    to preserve non-read columns.

    @param[out] to_rec    Buffer to copy to.
    @param[in]  from_rec  Buffer to copy from.
  */
  virtual void copy_cached_row(uchar *to_rec, const uchar *from_rec) {
    memcpy(to_rec, from_rec, m_rec_length);
  }

  /**
    write row to new partition.
    @param  new_part   New partition to write to.

    @return Operation status.
      @retval    0  Success.
      @retval != 0  Error code.
  */
  virtual int write_row_in_new_part(uint new_part) = 0;

  /* Internal helper functions*/
  /**
    Update auto increment value if current row contains a higher value.
  */
  inline void set_auto_increment_if_higher();
  /**
    Common routine to set up index scans.

    Find out which partitions we'll need to read when scanning the specified
    range.

    If we need to scan only one partition, set m_ordered_scan_ongoing=false
    as we will not need to do merge ordering.

    @return Operation status.
      @returns   0  Success
      @returns !=0  Error code
  */
  int partition_scan_set_up(uchar *buf, bool idx_read_flag);
  /**
    Common routine to handle index_next with unordered results.

    These routines are used to scan partitions without considering order.
    This is performed in two situations.
    1) In read_multi_range this is the normal case
    2) When performing any type of index_read, index_first, index_last where
    all fields in the partition function is bound. In this case the index
    scan is performed on only one partition and thus it isn't necessary to
    perform any sort.

    @return Operation status.
      @returns HA_ERR_END_OF_FILE  End of scan
      @returns 0                   Success
      @returns other               Error code
  */
  int handle_unordered_next(uchar *buf, bool is_next_same);
  /**
    Handle index_next when changing to new partition.

    This routine is used to start the index scan on the next partition.
    Both initial start and after completing scan on one partition.

    @param[out] buf  Read row in MySQL Row Format

    @return Operation status.
      @retval HA_ERR_END_OF_FILE  End of scan
      @retval 0                   Success
      @retval other               Error code
  */
  int handle_unordered_scan_next_partition(uchar *buf);
  /**
    Common routine to start index scan with ordered results.


      @returns Operation status
      @returns HA_ERR_END_OF_FILE    End of scan
      @returns HA_ERR_KEY_NOT_FOUND  End of scan
      @returns 0                     Success
      @returns other                 Error code
  */
  int handle_ordered_index_scan(uchar *buf);
  /**
    Add index_next/prev results from partitions without exact match.

    If there where any partitions that returned HA_ERR_KEY_NOT_FOUND when
    ha_index_read_map was done, those partitions must be included in the
    following index_next/prev call.

      @returns Operation status
      @returns HA_ERR_END_OF_FILE    End of scan
      @returns 0                     Success
      @returns other                 Error code
  */
  int handle_ordered_index_scan_key_not_found();
  /**
    Common routine to handle index_prev with ordered results.

    @param[out] buf  Read row in MySQL Row Format.

    @return Operation status.
      @retval HA_ERR_END_OF_FILE  End of scan
      @retval 0                   Success
      @retval other               Error code
  */
  int handle_ordered_prev(uchar *buf);
  /**
    Common routine to handle index_next with ordered results.

    @param[out] buf        Read row in MySQL Row Format.
    @param[in]  is_next_same  Called from index_next_same.

    @return Operation status.
      @retval HA_ERR_END_OF_FILE  End of scan
      @retval 0                   Success
      @retval other               Error code
  */
  int handle_ordered_next(uchar *buf, bool is_next_same);
  /**
    Common routine for a number of index_read variants.

    @param[out] buf             Buffer where the record should be returned.
    @param[in]  have_start_key  true <=> the left endpoint is available, i.e.
                                we're in index_read call or in read_range_first
                                call and the range has left endpoint.
                                false <=> there is no left endpoint (we're in
                                read_range_first() call and the range has no
                                left endpoint).

    @return Operation status
      @retval 0                    OK
      @retval HA_ERR_END_OF_FILE   Whole index scanned, without finding the
    record.
      @retval HA_ERR_KEY_NOT_FOUND Record not found, but index cursor
    positioned.
      @retval other                Error code.

    @details
      Start scanning the range (when invoked from read_range_first()) or doing
      an index lookup (when invoked from index_read_XXX):
       - If possible, perform partition selection
       - Find the set of partitions we're going to use
       - Depending on whether we need ordering:
          NO:  Get the first record from first used partition (see
               handle_unordered_scan_next_partition)
          YES: Fill the priority queue and get the record that is the first in
               the ordering
   */
  int common_index_read(uchar *buf, bool have_start_key);
  /**
    Common routine for index_first/index_last.

    @param[out] buf  Read row in MySQL Row Format.

    @return Operation status.
      @retval    0  Success
      @retval != 0  Error code
  */
  int common_first_last(uchar *buf);
  /**
    Return the top record in sort order.

    @param[out] buf  Row returned in MySQL Row Format.
  */
  void return_top_record(uchar *buf);

  /**
    Set table->read_set taking partitioning expressions into account.
  */
  void set_partition_read_set();

  /*
    These could be private as well,
    but easier to expose them to derived classes to use.
  */
 protected:
  /** Convenience pointer to table from m_handler (i.e. m_handler->table). */
  TABLE *m_table;

  /** All internal partitioning data! @{ */
  /** Tables partitioning info (same as table->part_info) */
  partition_info *m_part_info;
  /** Is primary key clustered. */
  bool m_pkey_is_clustered;
  /** Cached value of m_part_info->is_sub_partitioned(). */
  bool m_is_sub_partitioned;
  /** Total number of partitions. */
  uint m_tot_parts;
  uint m_last_part;        // Last accessed partition.
  const uchar *m_err_rec;  // record which gave error.
  bool m_auto_increment_safe_stmt_log_lock;
  bool m_auto_increment_lock;
  part_id_range m_part_spec;                         // Which parts to scan
  uint m_scan_value;                                 // Value passed in rnd_init
                                                     // call
  key_range m_start_key;                             // index read key range
  enum partition_index_scan_type m_index_scan_type;  // What type of index
                                                     // scan
  uint m_rec_length;  // Local copy of record length

  bool m_ordered;               // Ordered/Unordered index scan.
  bool m_ordered_scan_ongoing;  // Ordered index scan ongoing.
  bool m_reverse_order;         // Scanning in reverse order (prev).
  /** Row and key buffer for ordered index scan. */
  uchar *m_ordered_rec_buffer;
  /** Prio queue used by sorted read. */
  Prio_queue *m_queue;
  /** Which partition is to deliver next result. */
  uint m_top_entry;
  /** Offset in m_ordered_rec_buffer from part buffer to its record buffer. */
  uint m_rec_offset;
  /**
    Current index used for sorting.
    If clustered PK exists, then it will be used as secondary index to
    sort on if the first is equal in key_rec_cmp.
    So if clustered pk: m_curr_key_info[0]= current index and
    m_curr_key_info[1]= pk and [2]= NULL.
    Otherwise [0]= current index, [1]= NULL, and we will
    sort by rowid as secondary sort key if equal first key.
  */
  KEY *m_curr_key_info[3];
  enum enum_using_ref {
    /** handler::ref is not copied to the PQ. */
    REF_NOT_USED = 0,
    /**
      handler::ref is copied to the PQ but does not need to be used in sorting.
    */
    REF_STORED_IN_PQ,
    /** handler::ref is copied to the PQ and must be used during sorting. */
    REF_USED_FOR_SORT
  };
  /** How handler::ref is used in the priority queue. */
  enum_using_ref m_ref_usage;
  /** Set if previous index_* call returned HA_ERR_KEY_NOT_FOUND. */
  bool m_key_not_found;
  /** Partitions that returned HA_ERR_KEY_NOT_FOUND. */
  MY_BITMAP m_key_not_found_partitions;
  /** @} */

 private:
  /** Partition share for auto_inc handling. */
  Partition_share *m_part_share;
};
#endif /* PARTITION_HANDLER_INCLUDED */