MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ha_partition.h
1 #ifndef HA_PARTITION_INCLUDED
2 #define HA_PARTITION_INCLUDED
3 
4 /*
5  Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; version 2 of the License.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
19 
20 #include "sql_partition.h" /* part_id_range, partition_element */
21 #include "queues.h" /* QUEUE */
22 
23 enum partition_keywords
24 {
25  PKW_HASH= 0, PKW_RANGE, PKW_LIST, PKW_KEY, PKW_MAXVALUE, PKW_LINEAR,
26  PKW_COLUMNS, PKW_ALGORITHM
27 };
28 
29 
30 #define PARTITION_BYTES_IN_POS 2
31 
32 
34 typedef struct st_part_name_def
35 {
36  uchar *partition_name;
37  uint length;
38  uint32 part_id;
39  my_bool is_subpart;
41 
44 {
45 public:
46  uint num_parts;
49  {
50  num_parts= 0;
51  ha_shares= NULL;
52  }
54  {
55  uint i;
56  for (i= 0; i < num_parts; i++)
57  if (ha_shares[i])
58  delete ha_shares[i];
59  if (ha_shares)
60  delete [] ha_shares;
61  }
62  bool init(uint arg_num_parts)
63  {
64  DBUG_ASSERT(!num_parts && !ha_shares);
65  num_parts= arg_num_parts;
66  /* Allocate an array of Handler_share pointers */
68  if (!ha_shares)
69  {
70  num_parts= 0;
71  return true;
72  }
73  memset(ha_shares, 0, sizeof(Handler_share*) * num_parts);
74  return false;
75  }
76 };
77 
78 
83 {
84 public:
85  bool auto_inc_initialized;
87  ulonglong next_auto_inc_val;
93  HASH partition_name_hash;
96  Partition_share() {}
98  {
99  DBUG_ENTER("Partition_share::~Partition_share");
102  my_hash_free(&partition_name_hash);
104  delete partitions_share_refs;
105  DBUG_VOID_RETURN;
106  }
107  bool init(uint num_parts);
108  void lock_auto_inc()
109  {
111  }
112  void unlock_auto_inc()
113  {
115  }
116 };
117 
118 
119 class ha_partition :public handler
120 {
121 private:
122  enum partition_index_scan_type
123  {
124  partition_index_read= 0,
125  partition_index_first= 1,
126  partition_index_first_unordered= 2,
127  partition_index_last= 3,
128  partition_index_read_last= 4,
129  partition_read_range = 5,
130  partition_no_index_scan= 6
131  };
132  /* Data for the partition handler */
133  int m_mode; // Open mode
134  uint m_open_test_lock; // Open test_if_locked
135  char *m_file_buffer; // Content of the .par file
136  char *m_name_buffer_ptr; // Pointer to first partition name
137  plugin_ref *m_engine_array; // Array of types of the handlers
138  handler **m_file; // Array of references to handler inst.
139  uint m_file_tot_parts; // Debug
140  handler **m_new_file; // Array of references to new handlers
141  handler **m_reorged_file; // Reorganised partitions
142  handler **m_added_file; // Added parts kept for errors
143  partition_info *m_part_info; // local reference to partition
144  Field **m_part_field_array; // Part field array locally to save acc
145  uchar *m_ordered_rec_buffer; // Row and key buffer for ord. idx scan
146  /*
147  Current index.
148  When used in key_rec_cmp: If clustered pk, index compare
149  must compare pk if given index is same for two rows.
150  So normally m_curr_key_info[0]= current index and m_curr_key[1]= NULL,
151  and if clustered pk, [0]= current index, [1]= pk, [2]= NULL
152  */
153  KEY *m_curr_key_info[3]; // Current index
154  uchar *m_rec0; // table->record[0]
155  const uchar *m_err_rec; // record which gave error
156  QUEUE m_queue; // Prio queue used by sorted read
157  /*
158  Since the partition handler is a handler on top of other handlers, it
159  is necessary to keep information about what the underlying handler
160  characteristics is. It is not possible to keep any handler instances
161  for this since the MySQL Server sometimes allocating the handler object
162  without freeing them.
163  */
164  ulong m_low_byte_first;
165  enum enum_handler_status
166  {
167  handler_not_initialized= 0,
168  handler_initialized,
169  handler_opened,
170  handler_closed
171  };
172  enum_handler_status m_handler_status;
173 
174  uint m_reorged_parts; // Number of reorganised parts
175  uint m_tot_parts; // Total number of partitions;
176  uint m_num_locks; // For engines like ha_blackhole, which needs no locks
177  uint m_last_part; // Last file that we update,write,read
178  part_id_range m_part_spec; // Which parts to scan
179  uint m_scan_value; // Value passed in rnd_init
180  // call
181  uint m_ref_length; // Length of position in this
182  // handler object
183  key_range m_start_key; // index read key range
184  enum partition_index_scan_type m_index_scan_type;// What type of index
185  // scan
186  uint m_top_entry; // Which partition is to
187  // deliver next result
188  uint m_rec_length; // Local copy of record length
189 
190  bool m_ordered; // Ordered/Unordered index scan
191  bool m_pkey_is_clustered; // Is primary key clustered
192  bool m_create_handler; // Handler used to create table
193  bool m_is_sub_partitioned; // Is subpartitioned
194  bool m_ordered_scan_ongoing;
195 
196  /*
197  If set, this object was created with ha_partition::clone and doesn't
198  "own" the m_part_info structure.
199  */
200  ha_partition *m_is_clone_of;
201  MEM_ROOT *m_clone_mem_root;
202 
203  /*
204  We keep track if all underlying handlers are MyISAM since MyISAM has a
205  great number of extra flags not needed by other handlers.
206  */
207  bool m_myisam; // Are all underlying handlers
208  // MyISAM
209  /*
210  We keep track of InnoDB handlers below since it requires proper setting
211  of query_id in fields at index_init and index_read calls.
212  */
213  bool m_innodb; // Are all underlying handlers
214  // InnoDB
215  /*
216  When calling extra(HA_EXTRA_CACHE) we do not pass this to the underlying
217  handlers immediately. Instead we cache it and call the underlying
218  immediately before starting the scan on the partition. This is to
219  prevent allocating a READ CACHE for each partition in parallel when
220  performing a full table scan on MyISAM partitioned table.
221  This state is cleared by extra(HA_EXTRA_NO_CACHE).
222  */
223  bool m_extra_cache;
224  uint m_extra_cache_size;
225  /* The same goes for HA_EXTRA_PREPARE_FOR_UPDATE */
226  bool m_extra_prepare_for_update;
227  /* Which partition has active cache */
228  uint m_extra_cache_part_id;
229 
230  void init_handler_variables();
231  /*
232  Variables for lock structures.
233  */
234  THR_LOCK_DATA lock; /* MySQL lock */
235 
236  bool auto_increment_lock;
241  bool auto_increment_safe_stmt_log_lock;
243  MY_BITMAP m_bulk_insert_started;
244  ha_rows m_bulk_inserted_rows;
246  enum_monotonicity_info m_part_func_monotonicity_info;
248  MY_BITMAP m_locked_partitions;
250  Partition_share *part_share;
252  List<Parts_share_refs> m_new_partitions_share_refs;
254  uint32 *m_part_ids_sorted_by_num_of_records;
255  /* Compare function for my_qsort2, for reversed order. */
256  static int compare_number_of_records(ha_partition *me,
257  const uint32 *a,
258  const uint32 *b);
260  MY_BITMAP m_partitions_to_reset;
262  MY_BITMAP m_key_not_found_partitions;
263  bool m_key_not_found;
264 public:
265  Partition_share *get_part_share() { return part_share; }
266  handler *clone(const char *name, MEM_ROOT *mem_root);
267  virtual void set_part_info(partition_info *part_info, bool early)
268  {
269  m_part_info= part_info;
270  m_is_sub_partitioned= part_info->is_sub_partitioned();
271  }
272  /*
273  -------------------------------------------------------------------------
274  MODULE create/delete handler object
275  -------------------------------------------------------------------------
276  Object create/delete methode. The normal called when a table object
277  exists. There is also a method to create the handler object with only
278  partition information. This is used from mysql_create_table when the
279  table is to be created and the engine type is deduced to be the
280  partition handler.
281  -------------------------------------------------------------------------
282  */
283  ha_partition(handlerton *hton, TABLE_SHARE * table);
284  ha_partition(handlerton *hton, partition_info * part_info);
285  ha_partition(handlerton *hton, TABLE_SHARE *share,
286  partition_info *part_info_arg,
287  ha_partition *clone_arg,
288  MEM_ROOT *clone_mem_root_arg);
289  ~ha_partition();
290  /*
291  A partition handler has no characteristics in itself. It only inherits
292  those from the underlying handlers. Here we set-up those constants to
293  enable later calls of the methods to retrieve constants from the under-
294  lying handlers. Returns false if not successful.
295  */
296  bool initialize_partition(MEM_ROOT *mem_root);
297 
298  /*
299  -------------------------------------------------------------------------
300  MODULE meta data changes
301  -------------------------------------------------------------------------
302  Meta data routines to CREATE, DROP, RENAME table and often used at
303  ALTER TABLE (update_create_info used from ALTER TABLE and SHOW ..).
304 
305  update_table_comment is used in SHOW TABLE commands to provide a
306  chance for the handler to add any interesting comments to the table
307  comments not provided by the users comment.
308 
309  create_handler_files is called before opening a new handler object
310  with openfrm to call create. It is used to create any local handler
311  object needed in opening the object in openfrm
312  -------------------------------------------------------------------------
313  */
314  virtual int delete_table(const char *from);
315  virtual int rename_table(const char *from, const char *to);
316  virtual int create(const char *name, TABLE *form,
317  HA_CREATE_INFO *create_info);
318  virtual int create_handler_files(const char *name,
319  const char *old_name, int action_flag,
320  HA_CREATE_INFO *create_info);
321  virtual void update_create_info(HA_CREATE_INFO *create_info);
322  virtual char *update_table_comment(const char *comment);
323  virtual int change_partitions(HA_CREATE_INFO *create_info,
324  const char *path,
325  ulonglong * const copied,
326  ulonglong * const deleted,
327  const uchar *pack_frm_data,
328  size_t pack_frm_len);
329  virtual int drop_partitions(const char *path);
330  virtual int rename_partitions(const char *path);
331  bool get_no_parts(const char *name, uint *num_parts)
332  {
333  DBUG_ENTER("ha_partition::get_no_parts");
334  *num_parts= m_tot_parts;
335  DBUG_RETURN(0);
336  }
337  virtual void change_table_ptr(TABLE *table_arg, TABLE_SHARE *share);
338  virtual bool check_if_incompatible_data(HA_CREATE_INFO *create_info,
339  uint table_changes);
340 private:
341  int copy_partitions(ulonglong * const copied, ulonglong * const deleted);
342  void cleanup_new_partition(uint part_count);
343  int prepare_new_partition(TABLE *table, HA_CREATE_INFO *create_info,
344  handler *file, const char *part_name,
345  partition_element *p_elem);
346  /*
347  delete_table and rename_table uses very similar logic which
348  is packed into this routine.
349  */
350  int del_ren_table(const char *from, const char *to);
351  /*
352  One method to create the table_name.par file containing the names of the
353  underlying partitions, their engine and the number of partitions.
354  And one method to read it in.
355  */
356  bool create_handler_file(const char *name);
357  bool setup_engine_array(MEM_ROOT *mem_root);
358  bool read_par_file(const char *name);
359  bool get_from_handler_file(const char *name, MEM_ROOT *mem_root,
360  bool is_clone);
361  bool new_handlers_from_part_info(MEM_ROOT *mem_root);
362  bool create_handlers(MEM_ROOT *mem_root);
363  void clear_handler_file();
364  int set_up_table_before_create(TABLE *table_arg,
365  const char *partition_name_with_path,
366  HA_CREATE_INFO *info,
367  partition_element *p_elem);
368  partition_element *find_partition_element(uint part_id);
369  bool insert_partition_name_in_hash(const char *name, uint part_id,
370  bool is_subpart);
371  bool populate_partition_name_hash();
372  Partition_share *get_share();
373  bool set_ha_share_ref(Handler_share **ha_share);
374  void fix_data_dir(char* path);
375  bool init_partition_bitmaps();
376  void free_partition_bitmaps();
377 
378 public:
379 
380  /*
381  -------------------------------------------------------------------------
382  MODULE open/close object
383  -------------------------------------------------------------------------
384  Open and close handler object to ensure all underlying files and
385  objects allocated and deallocated for query handling is handled
386  properly.
387  -------------------------------------------------------------------------
388 
389  A handler object is opened as part of its initialisation and before
390  being used for normal queries (not before meta-data changes always.
391  If the object was opened it will also be closed before being deleted.
392  */
393  virtual int open(const char *name, int mode, uint test_if_locked);
394  virtual int close(void);
395 
396  /*
397  -------------------------------------------------------------------------
398  MODULE start/end statement
399  -------------------------------------------------------------------------
400  This module contains methods that are used to understand start/end of
401  statements, transaction boundaries, and aid for proper concurrency
402  control.
403  The partition handler need not implement abort and commit since this
404  will be handled by any underlying handlers implementing transactions.
405  There is only one call to each handler type involved per transaction
406  and these go directly to the handlers supporting transactions
407  currently InnoDB, BDB and NDB).
408  -------------------------------------------------------------------------
409  */
410  virtual THR_LOCK_DATA **store_lock(THD * thd, THR_LOCK_DATA ** to,
411  enum thr_lock_type lock_type);
412  virtual int external_lock(THD * thd, int lock_type);
413  /*
414  When table is locked a statement is started by calling start_stmt
415  instead of external_lock
416  */
417  virtual int start_stmt(THD * thd, thr_lock_type lock_type);
418  /*
419  Lock count is number of locked underlying handlers (I assume)
420  */
421  virtual uint lock_count(void) const;
422  /*
423  Call to unlock rows not to be updated in transaction
424  */
425  virtual void unlock_row();
426  /*
427  Check if semi consistent read
428  */
429  virtual bool was_semi_consistent_read();
430  /*
431  Call to hint about semi consistent read
432  */
433  virtual void try_semi_consistent_read(bool);
434 
435  /*
436  NOTE: due to performance and resource issues with many partitions,
437  we only use the m_psi on the ha_partition handler, excluding all
438  partitions m_psi.
439  */
440 #ifdef HAVE_M_PSI_PER_PARTITION
441  /*
442  Bind the table/handler thread to track table i/o.
443  */
444  virtual void unbind_psi();
445  virtual void rebind_psi();
446 #endif
447  /*
448  -------------------------------------------------------------------------
449  MODULE change record
450  -------------------------------------------------------------------------
451  This part of the handler interface is used to change the records
452  after INSERT, DELETE, UPDATE, REPLACE method calls but also other
453  special meta-data operations as ALTER TABLE, LOAD DATA, TRUNCATE.
454  -------------------------------------------------------------------------
455 
456  These methods are used for insert (write_row), update (update_row)
457  and delete (delete_row). All methods to change data always work on
458  one row at a time. update_row and delete_row also contains the old
459  row.
460  delete_all_rows will delete all rows in the table in one call as a
461  special optimisation for DELETE from table;
462 
463  Bulk inserts are supported if all underlying handlers support it.
464  start_bulk_insert and end_bulk_insert is called before and after a
465  number of calls to write_row.
466  */
467  virtual int write_row(uchar * buf);
468  virtual int update_row(const uchar * old_data, uchar * new_data);
469  virtual int delete_row(const uchar * buf);
470  virtual int delete_all_rows(void);
471  virtual int truncate();
472  virtual void start_bulk_insert(ha_rows rows);
473  virtual int end_bulk_insert();
474 private:
475  ha_rows guess_bulk_insert_rows();
476  void start_part_bulk_insert(THD *thd, uint part_id);
477  long estimate_read_buffer_size(long original_size);
478 public:
479 
480  /*
481  Method for truncating a specific partition.
482  (i.e. ALTER TABLE t1 TRUNCATE PARTITION p).
483 
484  @remark This method is a partitioning-specific hook
485  and thus not a member of the general SE API.
486  */
487  int truncate_partition(Alter_info *, bool *binlog_stmt);
488 
489  virtual bool is_fatal_error(int error, uint flags)
490  {
491  if (!handler::is_fatal_error(error, flags) ||
492  error == HA_ERR_NO_PARTITION_FOUND ||
493  error == HA_ERR_NOT_IN_LOCK_PARTITIONS)
494  return FALSE;
495  return TRUE;
496  }
497 
498 
499  /*
500  -------------------------------------------------------------------------
501  MODULE full table scan
502  -------------------------------------------------------------------------
503  This module is used for the most basic access method for any table
504  handler. This is to fetch all data through a full table scan. No
505  indexes are needed to implement this part.
506  It contains one method to start the scan (rnd_init) that can also be
507  called multiple times (typical in a nested loop join). Then proceeding
508  to the next record (rnd_next) and closing the scan (rnd_end).
509  To remember a record for later access there is a method (position)
510  and there is a method used to retrieve the record based on the stored
511  position.
512  The position can be a file position, a primary key, a ROWID dependent
513  on the handler below.
514  -------------------------------------------------------------------------
515  */
516  /*
517  unlike index_init(), rnd_init() can be called two times
518  without rnd_end() in between (it only makes sense if scan=1).
519  then the second call should prepare for the new table scan
520  (e.g if rnd_init allocates the cursor, second call should
521  position it to the start of the table, no need to deallocate
522  and allocate it again
523  */
524  virtual int rnd_init(bool scan);
525  virtual int rnd_end();
526  virtual int rnd_next(uchar * buf);
527  virtual int rnd_pos(uchar * buf, uchar * pos);
528  virtual int rnd_pos_by_record(uchar *record);
529  virtual void position(const uchar * record);
530 
531  /*
532  -------------------------------------------------------------------------
533  MODULE index scan
534  -------------------------------------------------------------------------
535  This part of the handler interface is used to perform access through
536  indexes. The interface is defined as a scan interface but the handler
537  can also use key lookup if the index is a unique index or a primary
538  key index.
539  Index scans are mostly useful for SELECT queries but are an important
540  part also of UPDATE, DELETE, REPLACE and CREATE TABLE table AS SELECT
541  and so forth.
542  Naturally an index is needed for an index scan and indexes can either
543  be ordered, hash based. Some ordered indexes can return data in order
544  but not necessarily all of them.
545  There are many flags that define the behavior of indexes in the
546  various handlers. These methods are found in the optimizer module.
547  -------------------------------------------------------------------------
548 
549  index_read is called to start a scan of an index. The find_flag defines
550  the semantics of the scan. These flags are defined in
551  include/my_base.h
552  index_read_idx is the same but also initializes index before calling doing
553  the same thing as index_read. Thus it is similar to index_init followed
554  by index_read. This is also how we implement it.
555 
556  index_read/index_read_idx does also return the first row. Thus for
557  key lookups, the index_read will be the only call to the handler in
558  the index scan.
559 
560  index_init initializes an index before using it and index_end does
561  any end processing needed.
562  */
563  virtual int index_read_map(uchar * buf, const uchar * key,
564  key_part_map keypart_map,
565  enum ha_rkey_function find_flag);
566  virtual int index_init(uint idx, bool sorted);
567  virtual int index_end();
568 
575  virtual int index_read_idx_map(uchar *buf, uint index, const uchar *key,
576  key_part_map keypart_map,
577  enum ha_rkey_function find_flag);
578  /*
579  These methods are used to jump to next or previous entry in the index
580  scan. There are also methods to jump to first and last entry.
581  */
582  virtual int index_next(uchar * buf);
583  virtual int index_prev(uchar * buf);
584  virtual int index_first(uchar * buf);
585  virtual int index_last(uchar * buf);
586  virtual int index_next_same(uchar * buf, const uchar * key, uint keylen);
587  virtual int index_read_last_map(uchar * buf, const uchar * key,
588  key_part_map keypart_map);
589 
590  /*
591  read_first_row is virtual method but is only implemented by
592  handler.cc, no storage engine has implemented it so neither
593  will the partition handler.
594 
595  virtual int read_first_row(uchar *buf, uint primary_key);
596  */
597 
598  /*
599  We don't implement multi read range yet, will do later.
600  virtual int read_multi_range_first(KEY_MULTI_RANGE **found_range_p,
601  KEY_MULTI_RANGE *ranges, uint range_count,
602  bool sorted, HANDLER_BUFFER *buffer);
603  virtual int read_multi_range_next(KEY_MULTI_RANGE **found_range_p);
604  */
605 
606 
607  virtual int read_range_first(const key_range * start_key,
608  const key_range * end_key,
609  bool eq_range, bool sorted);
610  virtual int read_range_next();
611 
612 private:
613  bool init_record_priority_queue();
614  void destroy_record_priority_queue();
615  int common_index_read(uchar * buf, bool have_start_key);
616  int common_first_last(uchar * buf);
617  int partition_scan_set_up(uchar * buf, bool idx_read_flag);
618  int handle_unordered_next(uchar * buf, bool next_same);
619  int handle_unordered_scan_next_partition(uchar * buf);
620  int handle_ordered_index_scan(uchar * buf, bool reverse_order);
621  int handle_ordered_index_scan_key_not_found();
622  int handle_ordered_next(uchar * buf, bool next_same);
623  int handle_ordered_prev(uchar * buf);
624  void return_top_record(uchar * buf);
625 public:
626  /*
627  -------------------------------------------------------------------------
628  MODULE information calls
629  -------------------------------------------------------------------------
630  This calls are used to inform the handler of specifics of the ongoing
631  scans and other actions. Most of these are used for optimisation
632  purposes.
633  -------------------------------------------------------------------------
634  */
635  virtual int info(uint);
636  void get_dynamic_partition_info(PARTITION_STATS *stat_info,
637  uint part_id);
638  virtual int extra(enum ha_extra_function operation);
639  virtual int extra_opt(enum ha_extra_function operation, ulong cachesize);
640  virtual int reset(void);
641  /*
642  Do not allow caching of partitioned tables, since we cannot return
643  a callback or engine_data that would work for a generic engine.
644  */
645  virtual my_bool register_query_cache_table(THD *thd, char *table_key,
646  uint key_length,
647  qc_engine_callback
648  *engine_callback,
649  ulonglong *engine_data)
650  {
651  *engine_callback= NULL;
652  *engine_data= 0;
653  return FALSE;
654  }
655 
656 private:
657  static const uint NO_CURRENT_PART_ID;
658  int loop_extra(enum ha_extra_function operation);
659  int loop_extra_alter(enum ha_extra_function operations);
660  void late_extra_cache(uint partition_id);
661  void late_extra_no_cache(uint partition_id);
662  void prepare_extra_cache(uint cachesize);
663 public:
664 
665  /*
666  -------------------------------------------------------------------------
667  MODULE optimiser support
668  -------------------------------------------------------------------------
669  -------------------------------------------------------------------------
670  */
671 
672  /*
673  NOTE !!!!!!
674  -------------------------------------------------------------------------
675  -------------------------------------------------------------------------
676  One important part of the public handler interface that is not depicted in
677  the methods is the attribute records
678 
679  which is defined in the base class. This is looked upon directly and is
680  set by calling info(HA_STATUS_INFO) ?
681  -------------------------------------------------------------------------
682  */
683 
684 private:
685  /* Helper functions for optimizer hints. */
686  ha_rows min_rows_for_estimate();
687  uint get_biggest_used_partition(uint *part_index);
688 public:
689 
690  /*
691  keys_to_use_for_scanning can probably be implemented as the
692  intersection of all underlying handlers if mixed handlers are used.
693  This method is used to derive whether an index can be used for
694  index-only scanning when performing an ORDER BY query.
695  Only called from one place in sql_select.cc
696  */
697  virtual const key_map *keys_to_use_for_scanning();
698 
699  /*
700  Called in test_quick_select to determine if indexes should be used.
701  */
702  virtual double scan_time();
703 
704  /*
705  The next method will never be called if you do not implement indexes.
706  */
707  virtual double read_time(uint index, uint ranges, ha_rows rows);
708  /*
709  For the given range how many records are estimated to be in this range.
710  Used by optimiser to calculate cost of using a particular index.
711  */
712  virtual ha_rows records_in_range(uint inx, key_range * min_key,
713  key_range * max_key);
714 
715  /*
716  Upper bound of number records returned in scan is sum of all
717  underlying handlers.
718  */
719  virtual ha_rows estimate_rows_upper_bound();
720 
721  /*
722  table_cache_type is implemented by the underlying handler but all
723  underlying handlers must have the same implementation for it to work.
724  */
725  virtual uint8 table_cache_type();
726  virtual ha_rows records();
727 
728  /* Calculate hash value for PARTITION BY KEY tables. */
729  uint32 calculate_key_hash_value(Field **field_array);
730 
731  /*
732  -------------------------------------------------------------------------
733  MODULE print messages
734  -------------------------------------------------------------------------
735  This module contains various methods that returns text messages for
736  table types, index type and error messages.
737  -------------------------------------------------------------------------
738  */
739  /*
740  The name of the index type that will be used for display
741  Here we must ensure that all handlers use the same index type
742  for each index created.
743  */
744  virtual const char *index_type(uint inx);
745 
746  /* The name of the table type that will be used for display purposes */
747  virtual const char *table_type() const;
748 
749  /* The name of the row type used for the underlying tables. */
750  virtual enum row_type get_row_type() const;
751 
752  /*
753  Handler specific error messages
754  */
755  virtual void print_error(int error, myf errflag);
756  virtual bool get_error_message(int error, String * buf);
757  /*
758  -------------------------------------------------------------------------
759  MODULE handler characteristics
760  -------------------------------------------------------------------------
761  This module contains a number of methods defining limitations and
762  characteristics of the handler. The partition handler will calculate
763  this characteristics based on underlying handler characteristics.
764  -------------------------------------------------------------------------
765 
766  This is a list of flags that says what the storage engine
767  implements. The current table flags are documented in handler.h
768  The partition handler will support whatever the underlying handlers
769  support except when specifically mentioned below about exceptions
770  to this rule.
771  NOTE: This cannot be cached since it can depend on TRANSACTION ISOLATION
772  LEVEL which is dynamic, see bug#39084.
773 
774  HA_READ_RND_SAME:
775  Not currently used. (Means that the handler supports the rnd_same() call)
776  (MyISAM, HEAP)
777 
778  HA_TABLE_SCAN_ON_INDEX:
779  Used to avoid scanning full tables on an index. If this flag is set then
780  the handler always has a primary key (hidden if not defined) and this
781  index is used for scanning rather than a full table scan in all
782  situations.
783  (InnoDB, BDB, Federated)
784 
785  HA_REC_NOT_IN_SEQ:
786  This flag is set for handlers that cannot guarantee that the rows are
787  returned accroding to incremental positions (0, 1, 2, 3...).
788  This also means that rnd_next() should return HA_ERR_RECORD_DELETED
789  if it finds a deleted row.
790  (MyISAM (not fixed length row), BDB, HEAP, NDB, InooDB)
791 
792  HA_CAN_GEOMETRY:
793  Can the storage engine handle spatial data.
794  Used to check that no spatial attributes are declared unless
795  the storage engine is capable of handling it.
796  (MyISAM)
797 
798  HA_FAST_KEY_READ:
799  Setting this flag indicates that the handler is equally fast in
800  finding a row by key as by position.
801  This flag is used in a very special situation in conjunction with
802  filesort's. For further explanation see intro to init_read_record.
803  (BDB, HEAP, InnoDB)
804 
805  HA_NULL_IN_KEY:
806  Is NULL values allowed in indexes.
807  If this is not allowed then it is not possible to use an index on a
808  NULLable field.
809  (BDB, HEAP, MyISAM, NDB, InnoDB)
810 
811  HA_DUPLICATE_POS:
812  Tells that we can the position for the conflicting duplicate key
813  record is stored in table->file->dupp_ref. (insert uses rnd_pos() on
814  this to find the duplicated row)
815  (MyISAM)
816 
817  HA_CAN_INDEX_BLOBS:
818  Is the storage engine capable of defining an index of a prefix on
819  a BLOB attribute.
820  (BDB, Federated, MyISAM, InnoDB)
821 
822  HA_AUTO_PART_KEY:
823  Auto increment fields can be part of a multi-part key. For second part
824  auto-increment keys, the auto_incrementing is done in handler.cc
825  (BDB, Federated, MyISAM, NDB)
826 
827  HA_REQUIRE_PRIMARY_KEY:
828  Can't define a table without primary key (and cannot handle a table
829  with hidden primary key)
830  (No handler has this limitation currently)
831 
832  HA_STATS_RECORDS_IS_EXACT:
833  Does the counter of records after the info call specify an exact
834  value or not. If it does this flag is set.
835  Only MyISAM and HEAP uses exact count.
836 
837  HA_CAN_INSERT_DELAYED:
838  Can the storage engine support delayed inserts.
839  To start with the partition handler will not support delayed inserts.
840  Further investigation needed.
841  (HEAP, MyISAM)
842 
843  HA_PRIMARY_KEY_IN_READ_INDEX:
844  This parameter is set when the handler will also return the primary key
845  when doing read-only-key on another index.
846 
847  HA_NOT_DELETE_WITH_CACHE:
848  Seems to be an old MyISAM feature that is no longer used. No handler
849  has it defined but it is checked in init_read_record.
850  Further investigation needed.
851  (No handler defines it)
852 
853  HA_NO_PREFIX_CHAR_KEYS:
854  Indexes on prefixes of character fields is not allowed.
855  (NDB)
856 
857  HA_CAN_FULLTEXT:
858  Does the storage engine support fulltext indexes
859  The partition handler will start by not supporting fulltext indexes.
860  (MyISAM)
861 
862  HA_CAN_SQL_HANDLER:
863  Can the HANDLER interface in the MySQL API be used towards this
864  storage engine.
865  (MyISAM, InnoDB)
866 
867  HA_NO_AUTO_INCREMENT:
868  Set if the storage engine does not support auto increment fields.
869  (Currently not set by any handler)
870 
871  HA_HAS_CHECKSUM:
872  Special MyISAM feature. Has special SQL support in CREATE TABLE.
873  No special handling needed by partition handler.
874  (MyISAM)
875 
876  HA_FILE_BASED:
877  Should file names always be in lower case (used by engines
878  that map table names to file names.
879  Since partition handler has a local file this flag is set.
880  (BDB, Federated, MyISAM)
881 
882  HA_CAN_BIT_FIELD:
883  Is the storage engine capable of handling bit fields?
884  (MyISAM, NDB)
885 
886  HA_PRIMARY_KEY_REQUIRED_FOR_POSITION:
887  Does the storage engine need a PK for position?
888  (InnoDB)
889 
890  HA_FILE_BASED is always set for partition handler since we use a
891  special file for handling names of partitions, engine types.
892  HA_REC_NOT_IN_SEQ is always set for partition handler since we cannot
893  guarantee that the records will be returned in sequence.
894  HA_CAN_GEOMETRY, HA_CAN_FULLTEXT, HA_CAN_SQL_HANDLER, HA_DUPLICATE_POS,
895  HA_CAN_INSERT_DELAYED, HA_PRIMARY_KEY_REQUIRED_FOR_POSITION is disabled
896  until further investigated.
897  */
898  virtual Table_flags table_flags() const;
899 
900  /*
901  This is a bitmap of flags that says how the storage engine
902  implements indexes. The current index flags are documented in
903  handler.h. If you do not implement indexes, just return zero
904  here.
905 
906  part is the key part to check. First key part is 0
907  If all_parts it's set, MySQL want to know the flags for the combined
908  index up to and including 'part'.
909 
910  HA_READ_NEXT:
911  Does the index support read next, this is assumed in the server
912  code and never checked so all indexes must support this.
913  Note that the handler can be used even if it doesn't have any index.
914  (BDB, HEAP, MyISAM, Federated, NDB, InnoDB)
915 
916  HA_READ_PREV:
917  Can the index be used to scan backwards.
918  (BDB, HEAP, MyISAM, NDB, InnoDB)
919 
920  HA_READ_ORDER:
921  Can the index deliver its record in index order. Typically true for
922  all ordered indexes and not true for hash indexes.
923  In first step this is not true for partition handler until a merge
924  sort has been implemented in partition handler.
925  Used to set keymap part_of_sortkey
926  This keymap is only used to find indexes usable for resolving an ORDER BY
927  in the query. Thus in most cases index_read will work just fine without
928  order in result production. When this flag is set it is however safe to
929  order all output started by index_read since most engines do this. With
930  read_multi_range calls there is a specific flag setting order or not
931  order so in those cases ordering of index output can be avoided.
932  (BDB, InnoDB, HEAP, MyISAM, NDB)
933 
934  HA_READ_RANGE:
935  Specify whether index can handle ranges, typically true for all
936  ordered indexes and not true for hash indexes.
937  Used by optimiser to check if ranges (as key >= 5) can be optimised
938  by index.
939  (BDB, InnoDB, NDB, MyISAM, HEAP)
940 
941  HA_ONLY_WHOLE_INDEX:
942  Can't use part key searches. This is typically true for hash indexes
943  and typically not true for ordered indexes.
944  (Federated, NDB, HEAP)
945 
946  HA_KEYREAD_ONLY:
947  Does the storage engine support index-only scans on this index.
948  Enables use of HA_EXTRA_KEYREAD and HA_EXTRA_NO_KEYREAD
949  Used to set key_map keys_for_keyread and to check in optimiser for
950  index-only scans. When doing a read under HA_EXTRA_KEYREAD the handler
951  only have to fill in the columns the key covers. If
952  HA_PRIMARY_KEY_IN_READ_INDEX is set then also the PRIMARY KEY columns
953  must be updated in the row.
954  (BDB, InnoDB, MyISAM)
955  */
956  virtual ulong index_flags(uint inx, uint part, bool all_parts) const
957  {
958  /*
959  TODO: sergefp: Support Index Condition Pushdown in this table handler.
960  */
961  return m_file[0]->index_flags(inx, part, all_parts) &
962  ~HA_DO_INDEX_COND_PUSHDOWN;
963  }
964 
969  virtual uint alter_table_flags(uint flags);
970  /*
971  extensions of table handler files
972  */
973  virtual const char **bas_ext() const;
974  /*
975  unireg.cc will call the following to make sure that the storage engine
976  can handle the data it is about to send.
977 
978  The maximum supported values is the minimum of all handlers in the table
979  */
980  uint min_of_the_max_uint(uint (handler::*operator_func)(void) const) const;
981  virtual uint max_supported_record_length() const;
982  virtual uint max_supported_keys() const;
983  virtual uint max_supported_key_parts() const;
984  virtual uint max_supported_key_length() const;
985  virtual uint max_supported_key_part_length() const;
986 
987  /*
988  All handlers in a partitioned table must have the same low_byte_first
989  */
990  virtual bool low_byte_first() const
991  { return m_low_byte_first; }
992 
993  /*
994  The extra record buffer length is the maximum needed by all handlers.
995  The minimum record length is the maximum of all involved handlers.
996  */
997  virtual uint extra_rec_buf_length() const;
998  virtual uint min_record_length(uint options) const;
999 
1000  /*
1001  Primary key is clustered can only be true if all underlying handlers have
1002  this feature.
1003  */
1004  virtual bool primary_key_is_clustered()
1005  { return m_pkey_is_clustered; }
1006 
1007  /*
1008  -------------------------------------------------------------------------
1009  MODULE compare records
1010  -------------------------------------------------------------------------
1011  cmp_ref checks if two references are the same. For most handlers this is
1012  a simple memcmp of the reference. However some handlers use primary key
1013  as reference and this can be the same even if memcmp says they are
1014  different. This is due to character sets and end spaces and so forth.
1015  For the partition handler the reference is first two bytes providing the
1016  partition identity of the referred record and then the reference of the
1017  underlying handler.
1018  Thus cmp_ref for the partition handler always returns FALSE for records
1019  not in the same partition and uses cmp_ref on the underlying handler
1020  to check whether the rest of the reference part is also the same.
1021  -------------------------------------------------------------------------
1022  */
1023  virtual int cmp_ref(const uchar * ref1, const uchar * ref2);
1024  /*
1025  -------------------------------------------------------------------------
1026  MODULE auto increment
1027  -------------------------------------------------------------------------
1028  This module is used to handle the support of auto increments.
1029 
1030  This variable in the handler is used as part of the handler interface
1031  It is maintained by the parent handler object and should not be
1032  touched by child handler objects (see handler.cc for its use).
1033 
1034  auto_increment_column_changed
1035  -------------------------------------------------------------------------
1036  */
1037  virtual void get_auto_increment(ulonglong offset, ulonglong increment,
1038  ulonglong nb_desired_values,
1039  ulonglong *first_value,
1040  ulonglong *nb_reserved_values);
1041  virtual void release_auto_increment();
1042 private:
1043  virtual int reset_auto_increment(ulonglong value);
1044  virtual void lock_auto_increment()
1045  {
1046  /* lock already taken */
1047  if (auto_increment_safe_stmt_log_lock)
1048  return;
1049  DBUG_ASSERT(!auto_increment_lock);
1050  if(table_share->tmp_table == NO_TMP_TABLE)
1051  {
1052  auto_increment_lock= TRUE;
1053  part_share->lock_auto_inc();
1054  }
1055  }
1056  virtual void unlock_auto_increment()
1057  {
1058  /*
1059  If auto_increment_safe_stmt_log_lock is true, we have to keep the lock.
1060  It will be set to false and thus unlocked at the end of the statement by
1061  ha_partition::release_auto_increment.
1062  */
1063  if(auto_increment_lock && !auto_increment_safe_stmt_log_lock)
1064  {
1065  part_share->unlock_auto_inc();
1066  auto_increment_lock= FALSE;
1067  }
1068  }
1069  virtual void set_auto_increment_if_higher(Field *field)
1070  {
1071  ulonglong nr= (((Field_num*) field)->unsigned_flag ||
1072  field->val_int() > 0) ? field->val_int() : 0;
1073  lock_auto_increment();
1074  DBUG_ASSERT(part_share->auto_inc_initialized);
1075  /* must check when the mutex is taken */
1076  if (nr >= part_share->next_auto_inc_val)
1077  part_share->next_auto_inc_val= nr + 1;
1078  unlock_auto_increment();
1079  }
1080 
1081 public:
1082 
1083  /*
1084  -------------------------------------------------------------------------
1085  MODULE initialize handler for HANDLER call
1086  -------------------------------------------------------------------------
1087  This method is a special InnoDB method called before a HANDLER query.
1088  -------------------------------------------------------------------------
1089  */
1090  virtual void init_table_handle_for_HANDLER();
1091 
1092  /*
1093  The remainder of this file defines the handler methods not implemented
1094  by the partition handler
1095  */
1096 
1097  /*
1098  -------------------------------------------------------------------------
1099  MODULE foreign key support
1100  -------------------------------------------------------------------------
1101  The following methods are used to implement foreign keys as supported by
1102  InnoDB. Implement this ??
1103  get_foreign_key_create_info is used by SHOW CREATE TABLE to get a textual
1104  description of how the CREATE TABLE part to define FOREIGN KEY's is done.
1105  free_foreign_key_create_info is used to free the memory area that provided
1106  this description.
1107  can_switch_engines checks if it is ok to switch to a new engine based on
1108  the foreign key info in the table.
1109  -------------------------------------------------------------------------
1110 
1111  virtual char* get_foreign_key_create_info()
1112  virtual void free_foreign_key_create_info(char* str)
1113 
1114  virtual int get_foreign_key_list(THD *thd,
1115  List<FOREIGN_KEY_INFO> *f_key_list)
1116  virtual uint referenced_by_foreign_key()
1117  */
1118  virtual bool can_switch_engines();
1119  /*
1120  -------------------------------------------------------------------------
1121  MODULE fulltext index
1122  -------------------------------------------------------------------------
1123  Fulltext stuff not yet.
1124  -------------------------------------------------------------------------
1125  virtual int ft_init() { return HA_ERR_WRONG_COMMAND; }
1126  virtual FT_INFO *ft_init_ext(uint flags,uint inx,const uchar *key,
1127  uint keylen)
1128  { return NULL; }
1129  virtual int ft_read(uchar *buf) { return HA_ERR_WRONG_COMMAND; }
1130  */
1131 
1132  /*
1133  -------------------------------------------------------------------------
1134  MODULE restart full table scan at position (MyISAM)
1135  -------------------------------------------------------------------------
1136  The following method is only used by MyISAM when used as
1137  temporary tables in a join.
1138  virtual int restart_rnd_next(uchar *buf, uchar *pos);
1139  */
1140 
1141  /*
1142  -------------------------------------------------------------------------
1143  MODULE in-place ALTER TABLE
1144  -------------------------------------------------------------------------
1145  These methods are in the handler interface. (used by innodb-plugin)
1146  They are used for in-place alter table:
1147  -------------------------------------------------------------------------
1148  */
1149  virtual enum_alter_inplace_result
1150  check_if_supported_inplace_alter(TABLE *altered_table,
1151  Alter_inplace_info *ha_alter_info);
1152  virtual bool prepare_inplace_alter_table(TABLE *altered_table,
1153  Alter_inplace_info *ha_alter_info);
1154  virtual bool inplace_alter_table(TABLE *altered_table,
1155  Alter_inplace_info *ha_alter_info);
1156  virtual bool commit_inplace_alter_table(TABLE *altered_table,
1157  Alter_inplace_info *ha_alter_info,
1158  bool commit);
1159  virtual void notify_table_changed();
1160 
1161  /*
1162  -------------------------------------------------------------------------
1163  MODULE tablespace support
1164  -------------------------------------------------------------------------
1165  Admin of table spaces is not applicable to the partition handler (InnoDB)
1166  This means that the following method is not implemented:
1167  -------------------------------------------------------------------------
1168  virtual int discard_or_import_tablespace(my_bool discard)
1169  */
1170 
1171  /*
1172  -------------------------------------------------------------------------
1173  MODULE admin MyISAM
1174  -------------------------------------------------------------------------
1175 
1176  -------------------------------------------------------------------------
1177  OPTIMIZE TABLE, CHECK TABLE, ANALYZE TABLE and REPAIR TABLE are
1178  mapped to a routine that handles looping over a given set of
1179  partitions and those routines send a flag indicating to execute on
1180  all partitions.
1181  -------------------------------------------------------------------------
1182  */
1183  virtual int optimize(THD* thd, HA_CHECK_OPT *check_opt);
1184  virtual int analyze(THD* thd, HA_CHECK_OPT *check_opt);
1185  virtual int check(THD* thd, HA_CHECK_OPT *check_opt);
1186  virtual int repair(THD* thd, HA_CHECK_OPT *check_opt);
1187  virtual bool check_and_repair(THD *thd);
1188  virtual bool auto_repair() const;
1189  virtual bool is_crashed() const;
1190  virtual int check_for_upgrade(HA_CHECK_OPT *check_opt);
1191 
1192  private:
1193  int handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt, uint flags);
1194  int handle_opt_part(THD *thd, HA_CHECK_OPT *check_opt, uint part_id,
1195  uint flag);
1200  int check_misplaced_rows(uint read_part_id, bool repair);
1201  void append_row_to_str(String &str);
1202  public:
1203  /*
1204  -------------------------------------------------------------------------
1205  Admin commands not supported currently (almost purely MyISAM routines)
1206  This means that the following methods are not implemented:
1207  -------------------------------------------------------------------------
1208 
1209  virtual int backup(TD* thd, HA_CHECK_OPT *check_opt);
1210  virtual int restore(THD* thd, HA_CHECK_OPT *check_opt);
1211  virtual int dump(THD* thd, int fd = -1);
1212  virtual int net_read_dump(NET* net);
1213  */
1214  virtual uint checksum() const;
1215  /* Enabled keycache for performance reasons, WL#4571 */
1216  virtual int assign_to_keycache(THD* thd, HA_CHECK_OPT *check_opt);
1217  virtual int preload_keys(THD* thd, HA_CHECK_OPT* check_opt);
1218 
1219  /*
1220  -------------------------------------------------------------------------
1221  MODULE enable/disable indexes
1222  -------------------------------------------------------------------------
1223  Enable/Disable Indexes are only supported by HEAP and MyISAM.
1224  -------------------------------------------------------------------------
1225  */
1226  virtual int disable_indexes(uint mode);
1227  virtual int enable_indexes(uint mode);
1228  virtual int indexes_are_disabled(void);
1229 
1230  /*
1231  -------------------------------------------------------------------------
1232  MODULE append_create_info
1233  -------------------------------------------------------------------------
1234  append_create_info is only used by MyISAM MERGE tables and the partition
1235  handler will not support this handler as underlying handler.
1236  Implement this??
1237  -------------------------------------------------------------------------
1238  virtual void append_create_info(String *packet)
1239  */
1240 };
1241 
1242 #endif /* HA_PARTITION_INCLUDED */