MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
table.h
1 #ifndef TABLE_INCLUDED
2 #define TABLE_INCLUDED
3 
4 /* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; version 2 of the License.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18 
19 #include "my_global.h" /* NO_EMBEDDED_ACCESS_CHECKS */
20 #include "sql_plist.h"
21 #include "sql_alloc.h"
22 #include "mdl.h"
23 #include "datadict.h"
24 
25 #ifndef MYSQL_CLIENT
26 
27 #include "hash.h" /* HASH */
28 #include "handler.h" /* row_type, ha_choice, handler */
29 #include "mysql_com.h" /* enum_field_types */
30 #include "thr_lock.h" /* thr_lock_type */
31 #include "filesort_utils.h"
32 #include "parse_file.h"
33 #include "table_id.h"
34 
35 /* Structs that defines the TABLE */
36 
37 class Item; /* Needed by ORDER */
38 class Item_subselect;
39 class Item_field;
40 class GRANT_TABLE;
41 class st_select_lex_unit;
42 class st_select_lex;
43 class partition_info;
44 class COND_EQUAL;
45 class Security_context;
46 struct TABLE_LIST;
49 class Field;
52 
53 /*
54  Used to identify NESTED_JOIN structures within a join (applicable to
55  structures representing outer joins that have not been simplified away).
56 */
57 typedef ulonglong nested_join_map;
58 
59 
60 #define tmp_file_prefix "#sql"
61 #define tmp_file_prefix_length 4
62 #define TMP_TABLE_KEY_EXTRA 8
63 
79 enum enum_table_ref_type
80 {
82  TABLE_REF_NULL= 0,
83  TABLE_REF_VIEW,
84  TABLE_REF_BASE_TABLE,
85  TABLE_REF_I_S_TABLE,
86  TABLE_REF_TMP_TABLE
87 };
88 
93 enum enum_ident_name_check
94 {
95  IDENT_NAME_OK,
96  IDENT_NAME_WRONG,
97  IDENT_NAME_TOO_LONG
98 };
99 
100 /*************************************************************************/
101 
110 {
111 public:
112  Object_creation_ctx *set_n_backup(THD *thd);
113 
114  void restore_env(THD *thd, Object_creation_ctx *backup_ctx);
115 
116 protected:
118  virtual Object_creation_ctx *create_backup_ctx(THD *thd) const = 0;
119 
120  virtual void change_env(THD *thd) const = 0;
121 
122 public:
123  virtual ~Object_creation_ctx()
124  { }
125 };
126 
127 /*************************************************************************/
128 
135 {
136 public:
137  const CHARSET_INFO *get_client_cs()
138  {
139  return m_client_cs;
140  }
141 
142  const CHARSET_INFO *get_connection_cl()
143  {
144  return m_connection_cl;
145  }
146 
147 protected:
148  Default_object_creation_ctx(THD *thd);
149 
150  Default_object_creation_ctx(const CHARSET_INFO *client_cs,
151  const CHARSET_INFO *connection_cl);
152 
153 protected:
154  virtual Object_creation_ctx *create_backup_ctx(THD *thd) const;
155 
156  virtual void change_env(THD *thd) const;
157 
158 protected:
169 
179 };
180 
181 
187  public Sql_alloc
188 {
189 public:
190  static View_creation_ctx *create(THD *thd);
191 
192  static View_creation_ctx *create(THD *thd,
193  TABLE_LIST *view);
194 
195 private:
196  View_creation_ctx(THD *thd)
198  { }
199 };
200 
201 /*************************************************************************/
202 
205 typedef struct st_order {
206  struct st_order *next;
207  Item **item; /* Point at item in select fields */
208  Item *item_ptr; /* Storage for initial item */
209  int counter; /* position in SELECT list, correct
210  only if counter_used is true */
211  enum enum_order {
212  ORDER_NOT_RELEVANT,
213  ORDER_ASC,
214  ORDER_DESC
215  };
216 
217  enum_order direction; /* Requested direction of ordering */
218  bool in_field_list; /* true if in select field list */
219  bool counter_used; /* parameter was counter of columns */
227  Field *field; /* If tmp-table group */
228  char *buff; /* If tmp-table group */
229  table_map used, depend_map;
230 } ORDER;
231 
239 {
248 };
250 
265 typedef struct st_grant_info
266 {
289  uint version;
301  ulong privilege;
314 } GRANT_INFO;
315 
316 enum tmp_table_type
317 {
318  NO_TMP_TABLE, NON_TRANSACTIONAL_TMP_TABLE, TRANSACTIONAL_TMP_TABLE,
319  INTERNAL_TMP_TABLE, SYSTEM_TMP_TABLE
320 };
321 enum release_type { RELEASE_NORMAL, RELEASE_WAIT_FOR_DROP };
322 
323 
325 {
327  Filesort_buffer filesort_buffer;
328 
329 public:
330  IO_CACHE *io_cache; /* If sorted through filesort */
331  uchar *buffpek; /* Buffer for buffpek structures */
332  uint buffpek_len; /* Max number of buffpeks in the buffer */
333  uchar *addon_buf; /* Pointer to a buffer if sorted with fields */
334  size_t addon_length; /* Length of the buffer */
335  struct st_sort_addon_field *addon_field; /* Pointer to the fields info */
336  void (*unpack)(struct st_sort_addon_field *, uchar *); /* To unpack back */
337  uchar *record_pointers; /* If sorted in memory */
338  ha_rows found_records; /* How many records in sort */
339 
340  Filesort_info(): record_pointers(0) {};
342  void sort_buffer(Sort_param *param, uint count)
343  { filesort_buffer.sort_buffer(param, count); }
344 
348  uchar *get_record_buffer(uint idx)
349  { return filesort_buffer.get_record_buffer(idx); }
350 
351  uchar **get_sort_keys()
352  { return filesort_buffer.get_sort_keys(); }
353 
354  uchar **alloc_sort_buffer(uint num_records, uint record_length)
355  { return filesort_buffer.alloc_sort_buffer(num_records, record_length); }
356 
357  std::pair<uint, uint> sort_buffer_properties() const
358  { return filesort_buffer.sort_buffer_properties(); }
359 
360  void free_sort_buffer()
361  { filesort_buffer.free_sort_buffer(); }
362 
363  void init_record_pointers()
364  { filesort_buffer.init_record_pointers(); }
365 
366  size_t sort_buffer_size() const
367  { return filesort_buffer.sort_buffer_size(); }
368 };
369 
370 class Field_blob;
371 class Table_triggers_list;
372 
376 enum enum_table_category
377 {
381  TABLE_UNKNOWN_CATEGORY=0,
382 
394  TABLE_CATEGORY_TEMPORARY=1,
395 
404  TABLE_CATEGORY_USER=2,
405 
416  TABLE_CATEGORY_SYSTEM=3,
417 
433  /*
434  TODO: Fixing the performance issues of I_S will lead
435  to I_S tables in the table cache, which should use
436  this table type.
437  */
438  TABLE_CATEGORY_INFORMATION=4,
439 
460  TABLE_CATEGORY_LOG=5,
461 
482  TABLE_CATEGORY_PERFORMANCE=6,
483 
501  TABLE_CATEGORY_RPL_INFO=7
502 };
503 typedef enum enum_table_category TABLE_CATEGORY;
504 
505 TABLE_CATEGORY get_table_category(const LEX_STRING *db,
506  const LEX_STRING *name);
507 
508 
509 extern ulong refresh_version;
510 
511 typedef struct st_table_field_type
512 {
513  LEX_STRING name;
514  LEX_STRING type;
517 
518 
519 typedef struct st_table_field_def
520 {
521  uint count;
522  const TABLE_FIELD_TYPE *field;
524 
525 
527 {
528 protected:
529  virtual void report_error(uint code, const char *fmt, ...)= 0;
530 
531 public:
532  Table_check_intact() {}
533  virtual ~Table_check_intact() {}
534 
536  bool check(TABLE *table, const TABLE_FIELD_DEF *table_def);
537 };
538 
539 
547 {
548  MDL_context *m_ctx;
549  TABLE_SHARE *m_share;
550  uint m_deadlock_weight;
551 public:
552  Wait_for_flush(MDL_context *ctx_arg, TABLE_SHARE *share_arg,
553  uint deadlock_weight_arg)
554  : m_ctx(ctx_arg), m_share(share_arg),
555  m_deadlock_weight(deadlock_weight_arg)
556  {}
557 
558  MDL_context *get_ctx() const { return m_ctx; }
559 
560  virtual bool accept_visitor(MDL_wait_for_graph_visitor *dvisitor);
561 
562  virtual uint get_deadlock_weight() const;
563 
568  Wait_for_flush **prev_in_share;
569 };
570 
571 
572 typedef I_P_List <Wait_for_flush,
573  I_P_List_adapter<Wait_for_flush,
575  &Wait_for_flush::prev_in_share> >
577 
578 
585 {
586  TABLE_SHARE() {} /* Remove gcc warning */
587 
589  TABLE_CATEGORY table_category;
590 
591  /* hash of field names (contains pointers to elements of field array) */
592  HASH name_hash; /* hash of field names */
593  MEM_ROOT mem_root;
594  TYPELIB keynames; /* Pointers to keynames */
595  TYPELIB fieldnames; /* Pointer to fieldnames */
596  TYPELIB *intervals; /* pointer to interval info */
597  mysql_mutex_t LOCK_ha_data; /* To protect access to ha_data */
598  TABLE_SHARE *next, **prev; /* Link to unused shares */
608 
609  /* The following is copied to each TABLE on OPEN */
610  Field **field;
611  Field **found_next_number_field;
612  KEY *key_info; /* data of keys defined for the table */
613  uint *blob_field; /* Index to blobs in Field arrray*/
614 
615  uchar *default_values; /* row with default values */
616  LEX_STRING comment; /* Comment about table */
617  const CHARSET_INFO *table_charset; /* Default charset of string fields */
618 
619  MY_BITMAP all_set;
620  /*
621  Key which is used for looking-up table in table cache and in the list
622  of thread's temporary tables. Has the form of:
623  "database_name\0table_name\0" + optional part for temporary tables.
624 
625  Note that all three 'table_cache_key', 'db' and 'table_name' members
626  must be set (and be non-zero) for tables in table cache. They also
627  should correspond to each other.
628  To ensure this one can use set_table_cache() methods.
629  */
630  LEX_STRING table_cache_key;
631  LEX_STRING db; /* Pointer to db */
632  LEX_STRING table_name; /* Table name (for open) */
633  LEX_STRING path; /* Path to .frm file (from datadir) */
634  LEX_STRING normalized_path; /* unpack_filename(path) */
635  LEX_STRING connect_string;
636 
637  /*
638  Set of keys in use, implemented as a Bitmap.
639  Excludes keys disabled by ALTER TABLE ... DISABLE KEYS.
640  */
641  key_map keys_in_use;
642  key_map keys_for_keyread;
643  ha_rows min_rows, max_rows; /* create information */
644  ulong avg_row_length; /* create information */
645  ulong version;
646  ulong mysql_version; /* 0 if .frm is created before 5.0 */
647  ulong reclength; /* Recordlength */
648 
649  plugin_ref db_plugin; /* storage engine plugin */
650  inline handlerton *db_type() const /* table_type for handler */
651  {
652  // DBUG_ASSERT(db_plugin);
653  return db_plugin ? plugin_data(db_plugin, handlerton*) : NULL;
654  }
655  enum row_type row_type; /* How rows are stored */
656  enum tmp_table_type tmp_table;
657 
658  uint ref_count; /* How many TABLE objects uses this */
659  uint key_block_size; /* create key_block_size, if used */
660  uint stats_sample_pages; /* number of pages to sample during
661  stats estimation, if used, otherwise 0. */
662  enum_stats_auto_recalc stats_auto_recalc; /* Automatic recalc of stats. */
663  uint null_bytes, last_null_bit_pos;
664  uint fields; /* Number of fields */
665  uint rec_buff_length; /* Size of table->record[] buffer */
666  uint keys; /* Number of keys defined for the table*/
667  uint key_parts; /* Number of key parts of all keys
668  defined for the table
669  */
670  uint max_key_length; /* Length of the longest key */
671  uint max_unique_length; /* Length of the longest unique key */
672  uint total_key_length;
673  uint uniques; /* Number of UNIQUE index */
674  uint null_fields; /* number of null fields */
675  uint blob_fields; /* number of blob fields */
676  uint varchar_fields; /* number of varchar fields */
677  uint db_create_options; /* Create options from database */
678  uint db_options_in_use; /* Options in use */
679  uint db_record_offset; /* if HA_REC_IN_SEQ */
680  uint rowid_field_offset; /* Field_nr +1 to rowid field */
681  /* Primary key index number, used in TABLE::key_info[] */
682  uint primary_key;
683  uint next_number_index; /* autoincrement key number */
684  uint next_number_key_offset; /* autoinc keypart offset in a key */
685  uint next_number_keypart; /* autoinc keypart number in a key */
686  uint error, open_errno, errarg; /* error from open_table_def() */
687  uint column_bitmap_size;
688  uchar frm_version;
689  bool null_field_first;
690  bool system; /* Set if system table (one record) */
691  bool crypted; /* If .frm file is crypted */
692  bool db_low_byte_first; /* Portable row format */
693  bool crashed;
694  bool is_view;
695  Table_id table_map_id; /* for row-based replication */
696 
697  /*
698  Cache for row-based replication table share checks that does not
699  need to be repeated. Possible values are: -1 when cache value is
700  not calculated yet, 0 when table *shall not* be replicated, 1 when
701  table *may* be replicated.
702  */
703  int cached_row_logging_check;
704 
705  /*
706  Storage media to use for this table (unless another storage
707  media has been specified on an individual column - in versions
708  where that is supported)
709  */
710  enum ha_storage_media default_storage_media;
711 
712  /* Name of the tablespace used for this table */
713  char *tablespace;
714 
715 #ifdef WITH_PARTITION_STORAGE_ENGINE
716  /* filled in when reading from frm */
717  bool auto_partitioned;
718  char *partition_info_str;
719  uint partition_info_str_len;
720  uint partition_info_buffer_size;
721  handlerton *default_part_db_type;
722 #endif
723 
735 
738 
740  PSI_table_share *m_psi;
741 
745  Wait_for_flush_list m_flush_tickets;
746 
752 
753 
754  /*
755  Set share's table cache key and update its db and table name appropriately.
756 
757  SYNOPSIS
758  set_table_cache_key()
759  key_buff Buffer with already built table cache key to be
760  referenced from share.
761  key_length Key length.
762 
763  NOTES
764  Since 'key_buff' buffer will be referenced from share it should has same
765  life-time as share itself.
766  This method automatically ensures that TABLE_SHARE::table_name/db have
767  appropriate values by using table cache key as their source.
768  */
769 
770  void set_table_cache_key(char *key_buff, uint key_length)
771  {
772  table_cache_key.str= key_buff;
773  table_cache_key.length= key_length;
774  /*
775  Let us use the fact that the key is "db/0/table_name/0" + optional
776  part for temporary tables.
777  */
778  db.str= table_cache_key.str;
779  db.length= strlen(db.str);
780  table_name.str= db.str + db.length + 1;
781  table_name.length= strlen(table_name.str);
782  }
783 
784 
785  /*
786  Set share's table cache key and update its db and table name appropriately.
787 
788  SYNOPSIS
789  set_table_cache_key()
790  key_buff Buffer to be used as storage for table cache key
791  (should be at least key_length bytes).
792  key Value for table cache key.
793  key_length Key length.
794 
795  NOTE
796  Since 'key_buff' buffer will be used as storage for table cache key
797  it should has same life-time as share itself.
798  */
799 
800  void set_table_cache_key(char *key_buff, const char *key, uint key_length)
801  {
802  memcpy(key_buff, key, key_length);
803  set_table_cache_key(key_buff, key_length);
804  }
805 
806  inline bool honor_global_locks()
807  {
808  return ((table_category == TABLE_CATEGORY_USER)
809  || (table_category == TABLE_CATEGORY_SYSTEM));
810  }
811 
812  inline ulonglong get_table_def_version()
813  {
814  return table_map_id;
815  }
816 
817 
819  inline bool has_old_version() const
820  {
821  return version != refresh_version;
822  }
829  enum enum_table_ref_type get_table_ref_type() const
830  {
831  if (is_view)
832  return TABLE_REF_VIEW;
833  switch (tmp_table) {
834  case NO_TMP_TABLE:
835  return TABLE_REF_BASE_TABLE;
836  case SYSTEM_TMP_TABLE:
837  return TABLE_REF_I_S_TABLE;
838  default:
839  return TABLE_REF_TMP_TABLE;
840  }
841  }
897  ulonglong get_table_ref_version() const
898  {
899  return (tmp_table == SYSTEM_TMP_TABLE) ? 0 : table_map_id.id();
900  }
901 
902  bool visit_subgraph(Wait_for_flush *waiting_ticket,
903  MDL_wait_for_graph_visitor *gvisitor);
904 
905  bool wait_for_old_version(THD *thd, struct timespec *abstime,
906  uint deadlock_weight);
908  void destroy();
909 };
910 
911 
919 {
920 private:
921  MEM_ROOT storage;
926  bool truncated_value;
927 public:
928  Blob_mem_storage() :truncated_value(false)
929  {
930  init_alloc_root(&storage, MAX_FIELD_VARCHARLENGTH, 0);
931  }
933  {
934  free_root(&storage, MYF(0));
935  }
936  void reset()
937  {
938  free_root(&storage, MYF(MY_MARK_BLOCKS_FREE));
939  truncated_value= false;
940  }
951  char *store(const char *from, uint length)
952  {
953  return (char*) memdup_root(&storage, from, length);
954  }
955  void set_truncated_value(bool is_truncated_value)
956  {
957  truncated_value= is_truncated_value;
958  }
959  bool is_truncated_value() { return truncated_value; }
960 };
961 
962 
963 /* Information for one open table */
964 enum index_hint_type
965 {
966  INDEX_HINT_IGNORE,
967  INDEX_HINT_USE,
968  INDEX_HINT_FORCE
969 };
970 
971 /* Bitmap of table's fields */
973 
974 struct TABLE
975 {
976  TABLE() {} /* Remove gcc warning */
977  /*
978  Since TABLE instances are often cleared using memset(), do not
979  add virtual members and do not inherit from TABLE.
980  Otherwise memset() will start overwriting the vtable pointer.
981  */
982 
983  TABLE_SHARE *s;
984  handler *file;
985  TABLE *next, *prev;
986 
987 private:
995  TABLE *cache_next, **cache_prev;
996 
997  /*
998  Give Table_cache_element access to the above two members to allow
999  using them for linking TABLE objects in a list.
1000  */
1001  friend class Table_cache_element;
1002 
1003 public:
1004 
1005  THD *in_use; /* Which thread uses this */
1006  Field **field; /* Pointer to fields */
1007 
1008  uchar *record[2]; /* Pointer to records */
1009  uchar *write_row_record; /* Used as optimisation in
1010  THD::write_row */
1011  uchar *insert_values; /* used by INSERT ... UPDATE */
1012  /*
1013  Map of keys that can be used to retrieve all data from this table
1014  needed by the query without reading the row.
1015  */
1016  key_map covering_keys;
1017  key_map quick_keys, merge_keys;
1018  key_map used_keys; /* Indexes that cover all fields used by the query */
1019 
1020  /*
1021  possible_quick_keys is a superset of quick_keys to use with EXPLAIN of
1022  JOIN-less commands (single-table UPDATE and DELETE).
1023 
1024  When explaining regular JOINs, we use JOIN_TAB::keys to output the
1025  "possible_keys" column value. However, it is not available for
1026  single-table UPDATE and DELETE commands, since they don't use JOIN
1027  optimizer at the top level. OTOH they directly use the range optimizer,
1028  that collects all keys usable for range access here.
1029  */
1030  key_map possible_quick_keys;
1031 
1032  /*
1033  A set of keys that can be used in the query that references this
1034  table.
1035 
1036  All indexes disabled on the table's TABLE_SHARE (see TABLE::s) will be
1037  subtracted from this set upon instantiation. Thus for any TABLE t it holds
1038  that t.keys_in_use_for_query is a subset of t.s.keys_in_use. Generally we
1039  must not introduce any new keys here (see setup_tables).
1040 
1041  The set is implemented as a bitmap.
1042  */
1043  key_map keys_in_use_for_query;
1044  /* Map of keys that can be used to calculate GROUP BY without sorting */
1045  key_map keys_in_use_for_group_by;
1046  /* Map of keys that can be used to calculate ORDER BY without sorting */
1047  key_map keys_in_use_for_order_by;
1048  KEY *key_info; /* data of keys defined for the table */
1049 
1050  Field *next_number_field; /* Set if next_number is activated */
1051  Field *found_next_number_field; /* Set on open */
1052 
1053  /* Table's triggers, 0 if there are no of them */
1054  Table_triggers_list *triggers;
1055  TABLE_LIST *pos_in_table_list;/* Element referring to this table */
1056  /* Position in thd->locked_table_list under LOCK TABLES */
1057  TABLE_LIST *pos_in_locked_tables;
1058  ORDER *group;
1059  const char *alias; /* alias or table name */
1060  uchar *null_flags;
1061  my_bitmap_map *bitmap_init_value;
1062  MY_BITMAP def_read_set, def_write_set, tmp_set; /* containers */
1063  MY_BITMAP *read_set, *write_set; /* Active column sets */
1064  /*
1065  The ID of the query that opened and is using this table. Has different
1066  meanings depending on the table type.
1067 
1068  Temporary tables:
1069 
1070  table->query_id is set to thd->query_id for the duration of a statement
1071  and is reset to 0 once it is closed by the same statement. A non-zero
1072  table->query_id means that a statement is using the table even if it's
1073  not the current statement (table is in use by some outer statement).
1074 
1075  Non-temporary tables:
1076 
1077  Under pre-locked or LOCK TABLES mode: query_id is set to thd->query_id
1078  for the duration of a statement and is reset to 0 once it is closed by
1079  the same statement. A non-zero query_id is used to control which tables
1080  in the list of pre-opened and locked tables are actually being used.
1081  */
1082  query_id_t query_id;
1083 
1084  /*
1085  For each key that has quick_keys.is_set(key) == TRUE: estimate of #records
1086  and max #key parts that range access would use.
1087  */
1088  ha_rows quick_rows[MAX_KEY];
1089 
1090  /* Bitmaps of key parts that =const for the entire join. */
1091  key_part_map const_key_parts[MAX_KEY];
1092 
1093  uint quick_key_parts[MAX_KEY];
1094  uint quick_n_ranges[MAX_KEY];
1095 
1096  /*
1097  Estimate of number of records that satisfy SARGable part of the table
1098  condition, or table->file->records if no SARGable condition could be
1099  constructed.
1100  This value is used by join optimizer as an estimate of number of records
1101  that will pass the table condition (condition that depends on fields of
1102  this table and constants)
1103  */
1104  ha_rows quick_condition_rows;
1105  table_map map; /* ID bit of table (1,2,4,8,16...) */
1106 
1107  uint lock_position; /* Position in MYSQL_LOCK.table */
1108  uint lock_data_start; /* Start pos. in MYSQL_LOCK.locks */
1109  uint lock_count; /* Number of locks */
1110  uint tablenr,used_fields;
1111  uint temp_pool_slot; /* Used by intern temp tables */
1112  uint db_stat; /* mode of file as in handler.h */
1113  int current_lock; /* Type of lock on table */
1114 
1115  /*
1116  0 or JOIN_TYPE_{LEFT|RIGHT}. Currently this is only compared to 0.
1117  If maybe_null !=0, this table is inner w.r.t. some outer join operation,
1118  and null_row may be true.
1119  */
1120  uint maybe_null;
1121  /*
1122  If true, the current table row is considered to have all columns set to
1123  NULL, including columns declared as "not null" (see maybe_null).
1124  */
1125  my_bool null_row;
1126 
1127  uint8 status; /* What's in record[0] */
1128  my_bool copy_blobs; /* copy_blobs when storing */
1129 
1130  /*
1131  TODO: Each of the following flags take up 8 bits. They can just as easily
1132  be put into one single unsigned long and instead of taking up 18
1133  bytes, it would take up 4.
1134  */
1135  my_bool force_index;
1136 
1142 
1148  my_bool distinct,const_table,no_rows;
1149 
1154  my_bool key_read;
1155  my_bool no_keyread;
1156  my_bool locked_by_logger;
1160  my_bool no_replicate;
1161  my_bool locked_by_name;
1162  my_bool fulltext_searched;
1163  my_bool no_cache;
1164  /* To signal that the table is associated with a HANDLER statement */
1165  my_bool open_by_handler;
1166  /*
1167  To indicate that a non-null value of the auto_increment field
1168  was provided by the user or retrieved from the current record.
1169  Used only in the MODE_NO_AUTO_VALUE_ON_ZERO mode.
1170  */
1171  my_bool auto_increment_field_not_null;
1172  my_bool insert_or_update; /* Can be used by the handler */
1173  my_bool alias_name_used; /* true if table_name is alias */
1174  my_bool get_fields_in_item_tree; /* Signal to fix_field */
1175  my_bool m_needs_reopen;
1176 private:
1177  bool created; /* For tmp tables. TRUE <=> tmp table has been instantiated.*/
1178 public:
1179  uint max_keys; /* Size of allocated key_info array. */
1180 
1181  REGINFO reginfo; /* field connections */
1194  GRANT_INFO grant;
1195  Filesort_info sort;
1196 #ifdef WITH_PARTITION_STORAGE_ENGINE
1197  partition_info *part_info; /* Partition related information */
1198  /* If true, all partitions have been pruned away */
1199  bool all_partitions_pruned_away;
1200 #endif
1201  MDL_ticket *mdl_ticket;
1202 
1203  void init(THD *thd, TABLE_LIST *tl);
1204  bool fill_item_list(List<Item> *item_list) const;
1205  void reset_item_list(List<Item> *item_list) const;
1206  void clear_column_bitmaps(void);
1207  void prepare_for_position(void);
1208  void mark_columns_used_by_index_no_reset(uint index, MY_BITMAP *map);
1209  void mark_columns_used_by_index(uint index);
1210  void mark_auto_increment_column(void);
1211  void mark_columns_needed_for_update(void);
1212  void mark_columns_needed_for_delete(void);
1213  void mark_columns_needed_for_insert(void);
1215  inline void column_bitmaps_set(MY_BITMAP *read_set_arg,
1216  MY_BITMAP *write_set_arg)
1217  {
1218  read_set= read_set_arg;
1219  write_set= write_set_arg;
1220  if (file && created)
1221  file->column_bitmaps_signal();
1222  }
1223  inline void column_bitmaps_set_no_signal(MY_BITMAP *read_set_arg,
1224  MY_BITMAP *write_set_arg)
1225  {
1226  read_set= read_set_arg;
1227  write_set= write_set_arg;
1228  }
1229  inline void use_all_columns()
1230  {
1231  column_bitmaps_set(&s->all_set, &s->all_set);
1232  }
1233  inline void default_column_bitmaps()
1234  {
1235  read_set= &def_read_set;
1236  write_set= &def_write_set;
1237  }
1239  inline bool needs_reopen()
1240  { return !db_stat || m_needs_reopen; }
1241  bool alloc_keys(uint key_count);
1242  bool add_tmp_key(Field_map *key_parts, char *key_name);
1243  void use_index(int key_to_save);
1244 
1245  void set_keyread(bool flag)
1246  {
1247  DBUG_ASSERT(file);
1248  if (flag && !key_read)
1249  {
1250  key_read= 1;
1251  if (is_created())
1252  file->extra(HA_EXTRA_KEYREAD);
1253  }
1254  else if (!flag && key_read)
1255  {
1256  key_read= 0;
1257  if (is_created())
1258  file->extra(HA_EXTRA_NO_KEYREAD);
1259  }
1260  }
1261 
1262  bool update_const_key_parts(Item *conds);
1263 
1264  bool check_read_removal(uint index);
1265 
1267  bool is_created() const { return created; }
1268 
1274  {
1275  if (created)
1276  return;
1277  if (key_read)
1278  file->extra(HA_EXTRA_KEYREAD);
1279  created= true;
1280  }
1286  {
1287  created= false;
1288  }
1289 };
1290 
1291 
1292 enum enum_schema_table_state
1293 {
1294  NOT_PROCESSED= 0,
1295  PROCESSED_BY_CREATE_SORT_INDEX,
1296  PROCESSED_BY_JOIN_EXEC
1297 };
1298 
1299 typedef struct st_foreign_key_info
1300 {
1301  LEX_STRING *foreign_id;
1302  LEX_STRING *foreign_db;
1303  LEX_STRING *foreign_table;
1304  LEX_STRING *referenced_db;
1305  LEX_STRING *referenced_table;
1306  LEX_STRING *update_method;
1307  LEX_STRING *delete_method;
1308  LEX_STRING *referenced_key_name;
1309  List<LEX_STRING> foreign_fields;
1310  List<LEX_STRING> referenced_fields;
1312 
1313 #define MY_I_S_MAYBE_NULL 1
1314 #define MY_I_S_UNSIGNED 2
1315 
1316 
1317 #define SKIP_OPEN_TABLE 0 // do not open table
1318 #define OPEN_FRM_ONLY 1 // open FRM file only
1319 #define OPEN_FULL_TABLE 2 // open FRM,MYD, MYI files
1320 
1321 typedef struct st_field_info
1322 {
1326  const char* field_name;
1337  enum enum_field_types field_type;
1338  int value;
1347  uint field_flags; // Field atributes(maybe_null, signed, unsigned etc.)
1348  const char* old_name;
1354 } ST_FIELD_INFO;
1355 
1356 
1357 struct TABLE_LIST;
1358 
1359 typedef struct st_schema_table
1360 {
1361  const char* table_name;
1362  ST_FIELD_INFO *fields_info;
1363  /* Create information_schema table */
1364  TABLE *(*create_table) (THD *thd, TABLE_LIST *table_list);
1365  /* Fill table with data */
1366  int (*fill_table) (THD *thd, TABLE_LIST *tables, Item *cond);
1367  /* Handle fileds for old SHOW */
1368  int (*old_format) (THD *thd, struct st_schema_table *schema_table);
1369  int (*process_table) (THD *thd, TABLE_LIST *tables, TABLE *table,
1370  bool res, LEX_STRING *db_name, LEX_STRING *table_name);
1371  int idx_field1, idx_field2;
1372  bool hidden;
1373  uint i_s_requested_object; /* the object we need to open(TABLE | VIEW) */
1374 } ST_SCHEMA_TABLE;
1375 
1376 
1377 #define JOIN_TYPE_LEFT 1
1378 #define JOIN_TYPE_RIGHT 2
1379 
1380 enum enum_derived_type {
1381  VIEW_ALGORITHM_UNDEFINED = 0,
1382  VIEW_ALGORITHM_TMPTABLE,
1383  VIEW_ALGORITHM_MERGE,
1384  DERIVED_ALGORITHM_TMPTABLE
1385 };
1386 
1387 #define VIEW_SUID_INVOKER 0
1388 #define VIEW_SUID_DEFINER 1
1389 #define VIEW_SUID_DEFAULT 2
1390 
1391 /* view WITH CHECK OPTION parameter options */
1392 #define VIEW_CHECK_NONE 0
1393 #define VIEW_CHECK_LOCAL 1
1394 #define VIEW_CHECK_CASCADED 2
1395 
1396 /* result of view WITH CHECK OPTION parameter check */
1397 #define VIEW_CHECK_OK 0
1398 #define VIEW_CHECK_ERROR 1
1399 #define VIEW_CHECK_SKIP 2
1400 
1402 #define MAX_TDC_BLOB_SIZE 65536
1403 
1404 class select_union;
1405 class TMP_TABLE_PARAM;
1406 
1408 {
1409  Item *item;
1410  const char *name;
1411 };
1412 
1413 
1414 /*
1415  Column reference of a NATURAL/USING join. Since column references in
1416  joins can be both from views and stored tables, may point to either a
1417  Field (for tables), or a Field_translator (for views).
1418 */
1419 
1421 {
1422 public:
1423  Field_translator *view_field; /* Column reference of merge view. */
1424  Item_field *table_field; /* Column reference of table or temp view. */
1425  TABLE_LIST *table_ref; /* Original base table/view reference. */
1426  /*
1427  True if a common join column of two NATURAL/USING join operands. Notice
1428  that when we have a hierarchy of nested NATURAL/USING joins, a column can
1429  be common at some level of nesting but it may not be common at higher
1430  levels of nesting. Thus this flag may change depending on at which level
1431  we are looking at some column.
1432  */
1433  bool is_common;
1434 public:
1435  Natural_join_column(Field_translator *field_param, TABLE_LIST *tab);
1436  Natural_join_column(Item_field *field_param, TABLE_LIST *tab);
1437  const char *name();
1438  Item *create_item(THD *thd);
1439  Field *field();
1440  const char *table_name();
1441  const char *db_name();
1442  GRANT_INFO *grant();
1443 };
1444 
1445 
1450 enum enum_open_type
1451 {
1452  OT_TEMPORARY_OR_BASE= 0, OT_TEMPORARY_ONLY, OT_BASE_ONLY
1453 };
1454 
1464 class Derived_key: public Sql_alloc {
1465 public:
1466  table_map referenced_by;
1467  Field_map used_fields;
1468 };
1469 
1470 class Index_hint;
1471 class Item_exists_subselect;
1472 
1473 
1474 /*
1475  Table reference in the FROM clause.
1476 
1477  These table references can be of several types that correspond to
1478  different SQL elements. Below we list all types of TABLE_LISTs with
1479  the necessary conditions to determine when a TABLE_LIST instance
1480  belongs to a certain type.
1481 
1482  1) table (TABLE_LIST::view == NULL)
1483  - base table
1484  (TABLE_LIST::derived == NULL)
1485  - subquery - TABLE_LIST::table is a temp table
1486  (TABLE_LIST::derived != NULL)
1487  - information schema table
1488  (TABLE_LIST::schema_table != NULL)
1489  NOTICE: for schema tables TABLE_LIST::field_translation may be != NULL
1490  2) view (TABLE_LIST::view != NULL)
1491  - merge (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_MERGE)
1492  also (TABLE_LIST::field_translation != NULL)
1493  - tmptable (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_TMPTABLE)
1494  also (TABLE_LIST::field_translation == NULL)
1495  3) nested table reference (TABLE_LIST::nested_join != NULL)
1496  - table sequence - e.g. (t1, t2, t3)
1497  TODO: how to distinguish from a JOIN?
1498  - general JOIN
1499  TODO: how to distinguish from a table sequence?
1500  - NATURAL JOIN
1501  (TABLE_LIST::natural_join != NULL)
1502  - JOIN ... USING
1503  (TABLE_LIST::join_using_fields != NULL)
1504  - semi-join
1505  ;
1506 */
1507 
1509 struct LEX;
1511 {
1512  TABLE_LIST() {} /* Remove gcc warning */
1513 
1518  inline void init_one_table(const char *db_name_arg,
1519  size_t db_length_arg,
1520  const char *table_name_arg,
1521  size_t table_name_length_arg,
1522  const char *alias_arg,
1523  enum thr_lock_type lock_type_arg)
1524  {
1525  memset(this, 0, sizeof(*this));
1526  db= (char*) db_name_arg;
1527  db_length= db_length_arg;
1528  table_name= (char*) table_name_arg;
1529  table_name_length= table_name_length_arg;
1530  alias= (char*) alias_arg;
1531  lock_type= lock_type_arg;
1532  mdl_request.init(MDL_key::TABLE, db, table_name,
1533  (lock_type >= TL_WRITE_ALLOW_WRITE) ?
1534  MDL_SHARED_WRITE : MDL_SHARED_READ,
1535  MDL_TRANSACTION);
1536  callback_func= 0;
1537  }
1538 
1540  static TABLE_LIST *new_nested_join(MEM_ROOT *allocator,
1541  const char *alias,
1542  TABLE_LIST *embedding,
1543  List<TABLE_LIST> *belongs_to,
1544  class st_select_lex *select);
1545 
1546  /*
1547  List of tables local to a subquery or the top-level SELECT (used by
1548  SQL_I_List). Considers views as leaves (unlike 'next_leaf' below).
1549  Created at parse time in st_select_lex::add_table_to_list() ->
1550  table_list.link_in_list().
1551  */
1552  TABLE_LIST *next_local;
1553  /* link in a global list of all queries tables */
1554  TABLE_LIST *next_global, **prev_global;
1555  char *db, *alias, *table_name, *schema_table_name;
1556  char *option; /* Used by cache index */
1564 
1565 private:
1566  Item *m_join_cond; /* Used with outer join */
1567 public:
1568  Item **join_cond_ref() { return &m_join_cond; }
1569  Item *join_cond() const { return m_join_cond; }
1570  Item *set_join_cond(Item *val)
1571  { return m_join_cond= val; }
1572  /*
1573  The structure of the join condition presented in the member above
1574  can be changed during certain optimizations. This member
1575  contains a snapshot of AND-OR structure of the join condition
1576  made after permanent transformations of the parse tree, and is
1577  used to restore the join condition before every reexecution of a prepared
1578  statement or stored procedure.
1579  */
1580  Item *prep_join_cond;
1581 
1582  Item *sj_on_expr; /* Synthesized semijoin condition */
1583  /*
1584  (Valid only for semi-join nests) Bitmap of tables that are within the
1585  semi-join (this is different from bitmap of all nest's children because
1586  tables that were pulled out of the semi-join nest remain listed as
1587  nest's children).
1588  */
1589  table_map sj_inner_tables;
1590 
1591  COND_EQUAL *cond_equal; /* Used with outer join */
1592  /*
1593  During parsing - left operand of NATURAL/USING join where 'this' is
1594  the right operand. After parsing (this->natural_join == this) iff
1595  'this' represents a NATURAL or USING join operation. Thus after
1596  parsing 'this' is a NATURAL/USING join iff (natural_join != NULL).
1597  */
1598  TABLE_LIST *natural_join;
1599  /*
1600  True if 'this' represents a nested join that is a NATURAL JOIN.
1601  For one of the operands of 'this', the member 'natural_join' points
1602  to the other operand of 'this'.
1603  */
1604  bool is_natural_join;
1605  /* Field names in a USING clause for JOIN ... USING. */
1606  List<String> *join_using_fields;
1607  /*
1608  Explicitly store the result columns of either a NATURAL/USING join or
1609  an operand of such a join.
1610  */
1611  List<Natural_join_column> *join_columns;
1612  /* TRUE if join_columns contains all columns of this table reference. */
1613  bool is_join_columns_complete;
1614 
1615  /*
1616  List of nodes in a nested join tree, that should be considered as
1617  leaves with respect to name resolution. The leaves are: views,
1618  top-most nodes representing NATURAL/USING joins, subqueries, and
1619  base tables. All of these TABLE_LIST instances contain a
1620  materialized list of columns. The list is local to a subquery.
1621  */
1622  TABLE_LIST *next_name_resolution_table;
1623  /* Index names in a "... JOIN ... USE/IGNORE INDEX ..." clause. */
1624  List<Index_hint> *index_hints;
1625  TABLE *table; /* opened table */
1626  Table_id table_id; /* table id (from binlog) for opened table */
1627  /*
1628  select_result for derived table to pass it from table creation to table
1629  filling procedure
1630  */
1631  select_union *derived_result;
1632  /*
1633  Reference from aux_tables to local list entry of main select of
1634  multi-delete statement:
1635  delete t1 from t2,t1 where t1.a<'B' and t2.b=t1.b;
1636  here it will be reference of first occurrence of t1 to second (as you
1637  can see this lists can't be merged)
1638  */
1639  TABLE_LIST *correspondent_table;
1661  st_select_lex_unit *derived; /* SELECT_LEX_UNIT of derived table */
1662  /*
1663  TRUE <=> all possible keys for a derived table were collected and
1664  could be re-used while statement re-execution.
1665  */
1666  bool derived_keys_ready;
1667  ST_SCHEMA_TABLE *schema_table; /* Information_schema table */
1668  st_select_lex *schema_select_lex;
1669  /*
1670  True when the view field translation table is used to convert
1671  schema table fields for backwards compatibility with SHOW command.
1672  */
1673  bool schema_table_reformed;
1674  TMP_TABLE_PARAM *schema_table_param;
1675  /* link to select_lex where this table was used */
1676  st_select_lex *select_lex;
1677  LEX *view; /* link on VIEW lex for merging */
1678  Field_translator *field_translation; /* array of VIEW fields */
1679  /* pointer to element after last one in translation table above */
1680  Field_translator *field_translation_end;
1681  /*
1682  List (based on next_local) of underlying tables of this view. I.e. it
1683  does not include the tables of subqueries used in the view. Is set only
1684  for merged views.
1685  */
1686  TABLE_LIST *merge_underlying_list;
1687  /*
1688  - 0 for base tables
1689  - in case of the view it is the list of all (not only underlying
1690  tables but also used in subquery ones) tables of the view.
1691  */
1692  List<TABLE_LIST> *view_tables;
1693  /* most upper view this table belongs to */
1694  TABLE_LIST *belong_to_view;
1695  /*
1696  The view directly referencing this table
1697  (non-zero only for merged underlying tables of a view).
1698  */
1699  TABLE_LIST *referencing_view;
1700  /* Ptr to parent MERGE table list item. See top comment in ha_myisammrg.cc */
1701  TABLE_LIST *parent_l;
1702  /*
1703  Security context (non-zero only for tables which belong
1704  to view with SQL SECURITY DEFINER)
1705  */
1706  Security_context *security_ctx;
1707  /*
1708  This view security context (non-zero only for views with
1709  SQL SECURITY DEFINER)
1710  */
1711  Security_context *view_sctx;
1712  /*
1713  List of all base tables local to a subquery including all view
1714  tables. Unlike 'next_local', this in this list views are *not*
1715  leaves. Created in setup_tables() -> make_leaves_list().
1716  */
1717  bool allowed_show;
1718  TABLE_LIST *next_leaf;
1719  Item *where; /* VIEW WHERE clause condition */
1720  Item *check_option; /* WITH CHECK OPTION condition */
1721  LEX_STRING select_stmt; /* text of (CREATE/SELECT) statement */
1722  LEX_STRING md5; /* md5 of query text */
1723  LEX_STRING source; /* source of CREATE VIEW */
1724  LEX_STRING view_db; /* saved view database */
1725  LEX_STRING view_name; /* saved view name */
1726  LEX_STRING timestamp; /* GMT time stamp of last operation */
1727  st_lex_user definer; /* definer of view */
1728  ulonglong file_version; /* version of file's field set */
1729  ulonglong updatable_view; /* VIEW can be updated */
1738  ulonglong algorithm;
1739  ulonglong view_suid; /* view is suid (TRUE dy default) */
1740  ulonglong with_check; /* WITH CHECK OPTION */
1741  /*
1742  effective value of WITH CHECK OPTION (differ for temporary table
1743  algorithm)
1744  */
1745  uint8 effective_with_check;
1754  enum_derived_type effective_algorithm;
1755  GRANT_INFO grant;
1756  /* data need by some engines in query cache*/
1757  ulonglong engine_data;
1758  /* call back function for asking handler about caching in query cache */
1759  qc_engine_callback callback_func;
1760  thr_lock_type lock_type;
1761  uint outer_join; /* Which join type */
1762  uint shared; /* Used in multi-upd */
1763  size_t db_length;
1764  size_t table_name_length;
1765  bool updatable; /* VIEW/TABLE can be updated now */
1766  bool straight; /* optimize with prev table */
1767  bool updating; /* for replicate-do/ignore table */
1768  bool force_index; /* prefer index over table scan */
1769  bool ignore_leaves; /* preload only non-leaf nodes */
1770  table_map dep_tables; /* tables the table depends on */
1771  table_map on_expr_dep_tables; /* tables on expression depends on */
1772  struct st_nested_join *nested_join; /* if the element is a nested join */
1773  TABLE_LIST *embedding; /* nested join containing the table */
1774  List<TABLE_LIST> *join_list;/* join list the table belongs to */
1775  bool cacheable_table; /* stop PS caching */
1776  /* used in multi-upd/views privilege check */
1777  bool table_in_first_from_clause;
1782  enum enum_open_type open_type;
1783  /* TRUE if this merged view contain auto_increment field */
1784  bool contain_auto_increment;
1785  bool multitable_view; /* TRUE iff this is multitable view */
1786  bool compact_view_format; /* Use compact format for SHOW CREATE VIEW */
1787  /* view where processed */
1788  bool where_processed;
1789  /* TRUE <=> VIEW CHECK OPTION expression has been processed */
1790  bool check_option_processed;
1791  /* FRMTYPE_ERROR if any type is acceptable */
1792  enum frm_type_enum required_type;
1793  char timestamp_buffer[20]; /* buffer for timestamp (19+1) */
1794  /*
1795  This TABLE_LIST object is just placeholder for prelocking, it will be
1796  used for implicit LOCK TABLES only and won't be used in real statement.
1797  */
1798  bool prelocking_placeholder;
1803  enum
1804  {
1805  /* Normal open. */
1806  OPEN_NORMAL= 0,
1807  /* Associate a table share only if the the table exists. */
1808  OPEN_IF_EXISTS,
1809  /*
1810  Associate a table share only if the the table exists.
1811  Also upgrade metadata lock to exclusive if table doesn't exist.
1812  */
1813  OPEN_FOR_CREATE,
1814  /* Don't associate a table share. */
1815  OPEN_STUB
1816  } open_strategy;
1817  bool internal_tmp_table;
1819  bool is_alias;
1823  bool is_fqtn;
1824 
1825 
1826  /* View creation context. */
1827 
1828  View_creation_ctx *view_creation_ctx;
1829 
1830  /*
1831  Attributes to save/load view creation context in/from frm-file.
1832 
1833  Ther are required only to be able to use existing parser to load
1834  view-definition file. As soon as the parser parsed the file, view
1835  creation context is initialized and the attributes become redundant.
1836 
1837  These attributes MUST NOT be used for any purposes but the parsing.
1838  */
1839 
1840  LEX_STRING view_client_cs_name;
1841  LEX_STRING view_connection_cl_name;
1842 
1843  /*
1844  View definition (SELECT-statement) in the UTF-form.
1845  */
1846 
1847  LEX_STRING view_body_utf8;
1848 
1849  /* End of view definition context. */
1850  /* List of possible keys. Valid only for materialized derived tables/views. */
1851  List<Derived_key> derived_key_list;
1852 
1859  /* TRUE <=> this table is a const one and was optimized away. */
1860  bool optimized_away;
1861  uint i_s_requested_object;
1862  bool has_db_lookup_value;
1863  bool has_table_lookup_value;
1864  uint table_open_method;
1865  enum enum_schema_table_state schema_table_state;
1866 
1867  MDL_request mdl_request;
1868 
1869 #ifdef WITH_PARTITION_STORAGE_ENGINE
1870  /* List to carry partition names from PARTITION (...) clause in statement */
1871  List<String> *partition_names;
1872 #endif /* WITH_PARTITION_STORAGE_ENGINE */
1873 
1874  void calc_md5(char *buffer);
1875  void set_underlying_merge();
1876  int view_check_option(THD *thd, bool ignore_failure) const;
1877  bool setup_underlying(THD *thd);
1878  void cleanup_items();
1879  bool placeholder()
1880  {
1881  return derived || view || schema_table || !table;
1882  }
1883  void print(THD *thd, String *str, enum_query_type query_type);
1884  bool check_single_table(TABLE_LIST **table, table_map map,
1885  TABLE_LIST *view);
1886  bool set_insert_values(MEM_ROOT *mem_root);
1887  void hide_view_error(THD *thd);
1888  TABLE_LIST *find_underlying_table(TABLE *table);
1889  TABLE_LIST *first_leaf_for_name_resolution();
1890  TABLE_LIST *last_leaf_for_name_resolution();
1891  bool is_leaf_for_name_resolution();
1892  inline const TABLE_LIST *top_table() const
1893  { return belong_to_view ? belong_to_view : this; }
1894 
1895  inline TABLE_LIST *top_table()
1896  {
1897  return
1898  const_cast<TABLE_LIST*>(const_cast<const TABLE_LIST*>(this)->top_table());
1899  }
1900 
1901  inline bool prepare_check_option(THD *thd)
1902  {
1903  bool res= FALSE;
1904  if (effective_with_check)
1905  res= prep_check_option(thd, effective_with_check);
1906  return res;
1907  }
1908  inline bool prepare_where(THD *thd, Item **conds,
1909  bool no_where_clause)
1910  {
1911  if (effective_algorithm == VIEW_ALGORITHM_MERGE)
1912  return prep_where(thd, conds, no_where_clause);
1913  return FALSE;
1914  }
1920  inline bool uses_materialization() const
1921  {
1922  return (effective_algorithm == VIEW_ALGORITHM_TMPTABLE ||
1923  effective_algorithm == DERIVED_ALGORITHM_TMPTABLE);
1924  }
1925  inline bool is_view_or_derived() const
1926  {
1927  return (effective_algorithm != VIEW_ALGORITHM_UNDEFINED);
1928  }
1935  bool materializable_is_const() const;
1936 
1937  void register_want_access(ulong want_access);
1938  bool prepare_security(THD *thd);
1939 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1940  Security_context *find_view_security_context(THD *thd);
1941  bool prepare_view_securety_context(THD *thd);
1942 #endif
1943  /*
1944  Cleanup for re-execution in a prepared statement or a stored
1945  procedure.
1946  */
1947  void reinit_before_use(THD *thd);
1948  Item_subselect *containing_subselect();
1949 
1950  /*
1951  Compiles the tagged hints list and fills up TABLE::keys_in_use_for_query,
1952  TABLE::keys_in_use_for_group_by, TABLE::keys_in_use_for_order_by,
1953  TABLE::force_index and TABLE::covering_keys.
1954  */
1955  bool process_index_hints(TABLE *table);
1956 
1964  inline
1966  {
1967  return (m_table_ref_type == s->get_table_ref_type() &&
1968  m_table_ref_version == s->get_table_ref_version());
1969  }
1970 
1977  inline
1980 
1981  inline
1982  void set_table_ref_id(enum_table_ref_type table_ref_type_arg,
1983  ulonglong table_ref_version_arg)
1984  {
1985  m_table_ref_type= table_ref_type_arg;
1986  m_table_ref_version= table_ref_version_arg;
1987  }
1988 
1993  bool is_anonymous_derived_table() const { return derived && !view; }
1994 
1996  uint query_block_id() const;
1997 
2002  char *get_db_name() const { return view != NULL ? view_db.str : db; }
2003 
2010  char *get_table_name() const { return view != NULL ? view_name.str : table_name; }
2011  int fetch_number_of_rows();
2012  bool update_derived_keys(Field*, Item**, uint);
2013  bool generate_keys();
2014  bool handle_derived(LEX *lex, bool (*processor)(THD*, LEX*, TABLE_LIST*));
2015  st_select_lex_unit *get_unit() const;
2016 
2035  {
2036  if (!embedding)
2037  return NULL;
2038  if (embedding->sj_on_expr)
2039  return embedding->embedding;
2040  return embedding;
2041  }
2042 
2043 private:
2044  bool prep_check_option(THD *thd, uint8 check_opt_type);
2045  bool prep_where(THD *thd, Item **conds, bool no_where_clause);
2047  enum enum_table_ref_type m_table_ref_type;
2049  ulonglong m_table_ref_version;
2050 };
2051 
2052 
2053 struct st_position;
2054 
2055 class Item;
2056 
2057 /*
2058  Iterator over the fields of a generic table reference.
2059 */
2060 
2062 {
2063 public:
2064  Field_iterator() {} /* Remove gcc warning */
2065  virtual ~Field_iterator() {}
2066  virtual void set(TABLE_LIST *)= 0;
2067  virtual void next()= 0;
2068  virtual bool end_of_fields()= 0; /* Return 1 at end of list */
2069  virtual const char *name()= 0;
2070  virtual Item *create_item(THD *)= 0;
2071  virtual Field *field()= 0;
2072 };
2073 
2074 
2075 /*
2076  Iterator over the fields of a base table, view with temporary
2077  table, or subquery.
2078 */
2079 
2081 {
2082  Field **ptr;
2083 public:
2084  Field_iterator_table() :ptr(0) {}
2085  void set(TABLE_LIST *table) { ptr= table->table->field; }
2086  void set_table(TABLE *table) { ptr= table->field; }
2087  void next() { ptr++; }
2088  bool end_of_fields() { return *ptr == 0; }
2089  const char *name();
2090  Item *create_item(THD *thd);
2091  Field *field() { return *ptr; }
2092 };
2093 
2094 
2095 /* Iterator over the fields of a merge view. */
2096 
2098 {
2099  Field_translator *ptr, *array_end;
2100  TABLE_LIST *view;
2101 public:
2102  Field_iterator_view() :ptr(0), array_end(0) {}
2103  void set(TABLE_LIST *table);
2104  void next() { ptr++; }
2105  bool end_of_fields() { return ptr == array_end; }
2106  const char *name();
2107  Item *create_item(THD *thd);
2108  Item **item_ptr() {return &ptr->item; }
2109  Field *field() { return 0; }
2110  inline Item *item() { return ptr->item; }
2111  Field_translator *field_translator() { return ptr; }
2112 };
2113 
2114 
2115 /*
2116  Field_iterator interface to the list of materialized fields of a
2117  NATURAL/USING join.
2118 */
2119 
2121 {
2123  Natural_join_column *cur_column_ref;
2124 public:
2125  Field_iterator_natural_join() :cur_column_ref(NULL) {}
2127  void set(TABLE_LIST *table);
2128  void next();
2129  bool end_of_fields() { return !cur_column_ref; }
2130  const char *name() { return cur_column_ref->name(); }
2131  Item *create_item(THD *thd) { return cur_column_ref->create_item(thd); }
2132  Field *field() { return cur_column_ref->field(); }
2133  Natural_join_column *column_ref() { return cur_column_ref; }
2134 };
2135 
2136 
2137 /*
2138  Generic iterator over the fields of an arbitrary table reference.
2139 
2140  DESCRIPTION
2141  This class unifies the various ways of iterating over the columns
2142  of a table reference depending on the type of SQL entity it
2143  represents. If such an entity represents a nested table reference,
2144  this iterator encapsulates the iteration over the columns of the
2145  members of the table reference.
2146 
2147  IMPLEMENTATION
2148  The implementation assumes that all underlying NATURAL/USING table
2149  references already contain their result columns and are linked into
2150  the list TABLE_LIST::next_name_resolution_table.
2151 */
2152 
2154 {
2155  TABLE_LIST *table_ref, *first_leaf, *last_leaf;
2156  Field_iterator_table table_field_it;
2157  Field_iterator_view view_field_it;
2158  Field_iterator_natural_join natural_join_it;
2159  Field_iterator *field_it;
2160  void set_field_iterator();
2161 public:
2162  Field_iterator_table_ref() :field_it(NULL) {}
2163  void set(TABLE_LIST *table);
2164  void next();
2165  bool end_of_fields()
2166  { return (table_ref == last_leaf && field_it->end_of_fields()); }
2167  const char *name() { return field_it->name(); }
2168  const char *get_table_name();
2169  const char *get_db_name();
2170  GRANT_INFO *grant();
2171  Item *create_item(THD *thd) { return field_it->create_item(thd); }
2172  Field *field() { return field_it->field(); }
2173  Natural_join_column *get_or_create_column_ref(THD *thd, TABLE_LIST *parent_table_ref);
2174  Natural_join_column *get_natural_column_ref();
2175 };
2176 
2184 {
2201 };
2202 
2211 typedef struct st_nested_join
2212 {
2213  List<TABLE_LIST> join_list; /* list of elements in the nested join */
2214  table_map used_tables; /* bitmap of tables in the nested join */
2215  table_map not_null_tables; /* tables that rejects nulls */
2227  uint nj_total;
2240  nested_join_map nj_map;
2245  table_map sj_depends_on;
2249  table_map sj_corr_tables;
2254  /*
2255  Lists of trivially-correlated expressions from the outer and inner tables
2256  of the semi-join, respectively.
2257  */
2258  List<Item> sj_outer_exprs, sj_inner_exprs;
2260 } NESTED_JOIN;
2261 
2262 
2264 {
2265  struct st_changed_table_list *next;
2266  char *key;
2267  uint32 key_length;
2269 
2270 
2271 typedef struct st_open_table_list{
2272  struct st_open_table_list *next;
2273  char *db,*table;
2274  uint32 in_use,locked;
2275 } OPEN_TABLE_LIST;
2276 
2277 
2278 static inline my_bitmap_map *tmp_use_all_columns(TABLE *table,
2279  MY_BITMAP *bitmap)
2280 {
2281  my_bitmap_map *old= bitmap->bitmap;
2282  bitmap->bitmap= table->s->all_set.bitmap;// does not repoint last_word_ptr
2283  return old;
2284 }
2285 
2286 
2287 static inline void tmp_restore_column_map(MY_BITMAP *bitmap,
2288  my_bitmap_map *old)
2289 {
2290  bitmap->bitmap= old;
2291 }
2292 
2293 /* The following is only needed for debugging */
2294 
2295 static inline my_bitmap_map *dbug_tmp_use_all_columns(TABLE *table,
2296  MY_BITMAP *bitmap)
2297 {
2298 #ifndef DBUG_OFF
2299  return tmp_use_all_columns(table, bitmap);
2300 #else
2301  return 0;
2302 #endif
2303 }
2304 
2305 static inline void dbug_tmp_restore_column_map(MY_BITMAP *bitmap,
2306  my_bitmap_map *old)
2307 {
2308 #ifndef DBUG_OFF
2309  tmp_restore_column_map(bitmap, old);
2310 #endif
2311 }
2312 
2313 
2314 /*
2315  Variant of the above : handle both read and write sets.
2316  Provide for the possiblity of the read set being the same as the write set
2317 */
2318 static inline void dbug_tmp_use_all_columns(TABLE *table,
2319  my_bitmap_map **save,
2320  MY_BITMAP *read_set,
2321  MY_BITMAP *write_set)
2322 {
2323 #ifndef DBUG_OFF
2324  save[0]= read_set->bitmap;
2325  save[1]= write_set->bitmap;
2326  (void) tmp_use_all_columns(table, read_set);
2327  (void) tmp_use_all_columns(table, write_set);
2328 #endif
2329 }
2330 
2331 
2332 static inline void dbug_tmp_restore_column_maps(MY_BITMAP *read_set,
2333  MY_BITMAP *write_set,
2334  my_bitmap_map **old)
2335 {
2336 #ifndef DBUG_OFF
2337  tmp_restore_column_map(read_set, old[0]);
2338  tmp_restore_column_map(write_set, old[1]);
2339 #endif
2340 }
2341 
2342 
2343 size_t max_row_length(TABLE *table, const uchar *data);
2344 
2345 
2346 void init_mdl_requests(TABLE_LIST *table_list);
2347 
2348 int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias,
2349  uint db_stat, uint prgflag, uint ha_open_flags,
2350  TABLE *outparam, bool is_create_table);
2351 TABLE_SHARE *alloc_table_share(TABLE_LIST *table_list, const char *key,
2352  uint key_length);
2353 void init_tmp_table_share(THD *thd, TABLE_SHARE *share, const char *key,
2354  uint key_length,
2355  const char *table_name, const char *path);
2356 void free_table_share(TABLE_SHARE *share);
2357 int open_table_def(THD *thd, TABLE_SHARE *share, uint db_flags);
2358 void open_table_error(TABLE_SHARE *share, int error, int db_errno, int errarg);
2359 void update_create_info_from_table(HA_CREATE_INFO *info, TABLE *form);
2360 enum_ident_name_check check_and_convert_db_name(LEX_STRING *db,
2361  bool preserve_lettercase);
2362 bool check_column_name(const char *name);
2363 enum_ident_name_check check_table_name(const char *name, size_t length,
2364  bool check_for_path_chars);
2365 int rename_file_ext(const char * from,const char * to,const char * ext);
2366 char *get_field(MEM_ROOT *mem, Field *field);
2367 bool get_field(MEM_ROOT *mem, Field *field, class String *res);
2368 
2369 int closefrm(TABLE *table, bool free_share);
2370 int read_string(File file, uchar* *to, size_t length);
2371 void free_blobs(TABLE *table);
2372 void free_field_buffers_larger_than(TABLE *table, uint32 size);
2373 int set_zone(int nr,int min_zone,int max_zone);
2374 ulong get_form_pos(File file, uchar *head, TYPELIB *save_names);
2375 ulong make_new_entry(File file,uchar *fileinfo,TYPELIB *formnames,
2376  const char *newname);
2377 ulong next_io_size(ulong pos);
2378 void append_unescaped(String *res, const char *pos, uint length);
2379 File create_frm(THD *thd, const char *name, const char *db,
2380  const char *table, uint reclength, uchar *fileinfo,
2381  HA_CREATE_INFO *create_info, uint keys, KEY *key_info);
2382 char *fn_rext(char *name);
2383 
2384 /* performance schema */
2385 extern LEX_STRING PERFORMANCE_SCHEMA_DB_NAME;
2386 
2387 extern LEX_STRING GENERAL_LOG_NAME;
2388 extern LEX_STRING SLOW_LOG_NAME;
2389 
2390 /* information schema */
2391 extern LEX_STRING INFORMATION_SCHEMA_NAME;
2392 extern LEX_STRING MYSQL_SCHEMA_NAME;
2393 
2394 /* replication's tables */
2395 extern LEX_STRING RLI_INFO_NAME;
2396 extern LEX_STRING MI_INFO_NAME;
2397 extern LEX_STRING WORKER_INFO_NAME;
2398 
2399 inline bool is_infoschema_db(const char *name, size_t len)
2400 {
2401  return (INFORMATION_SCHEMA_NAME.length == len &&
2402  !my_strcasecmp(system_charset_info,
2403  INFORMATION_SCHEMA_NAME.str, name));
2404 }
2405 
2406 inline bool is_infoschema_db(const char *name)
2407 {
2408  return !my_strcasecmp(system_charset_info,
2409  INFORMATION_SCHEMA_NAME.str, name);
2410 }
2411 
2412 TYPELIB *typelib(MEM_ROOT *mem_root, List<String> &strings);
2413 
2417 inline bool is_user_table(TABLE * table)
2418 {
2419  const char *name= table->s->table_name.str;
2420  return strncmp(name, tmp_file_prefix, tmp_file_prefix_length);
2421 }
2422 
2423 inline void mark_as_null_row(TABLE *table)
2424 {
2425  table->null_row=1;
2426  table->status|=STATUS_NULL_ROW;
2427  memset(table->null_flags, 255, table->s->null_bytes);
2428 }
2429 
2430 bool is_simple_order(ORDER *order);
2431 
2432 #endif /* MYSQL_CLIENT */
2433 
2434 #endif /* TABLE_INCLUDED */