1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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; either version 2 of the License, or
9
* (at your option) any later version.
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.
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
21
/* Structs that defines the Table */
23
#ifndef DRIZZLED_TABLE_H
24
#define DRIZZLED_TABLE_H
26
#include <storage/myisam/myisam.h>
27
#include <drizzled/order.h>
28
#include <drizzled/filesort_info.h>
29
#include <drizzled/natural_join_column.h>
30
#include <drizzled/nested_join.h>
31
#include <drizzled/field_iterator.h>
33
class Item; /* Needed by order_st */
1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
/* Structs that defines the TABLE */
19
class Item; /* Needed by ORDER */
34
20
class Item_subselect;
35
21
class st_select_lex_unit;
36
22
class st_select_lex;
38
24
class Security_context;
41
26
/*************************************************************************/
28
/* Order clause list element */
30
typedef struct st_order {
31
struct st_order *next;
32
Item **item; /* Point at item in select fields */
33
Item *item_ptr; /* Storage for initial item */
34
Item **item_copy; /* For SPs; the original item ptr */
35
int counter; /* position in SELECT list, correct
36
only if counter_used is true*/
37
bool asc; /* true if ascending */
38
bool free_me; /* true if item isn't shared */
39
bool in_field_list; /* true if in select field list */
40
bool counter_used; /* parameter was counter of columns */
41
Field *field; /* If tmp-table group */
42
char *buff; /* If tmp-table group */
43
table_map used, depend_map;
43
46
enum tmp_table_type
45
48
NO_TMP_TABLE, NON_TRANSACTIONAL_TMP_TABLE, TRANSACTIONAL_TMP_TABLE,
46
49
INTERNAL_TMP_TABLE, SYSTEM_TMP_TABLE, TMP_TABLE_FRM_FILE_ONLY
49
bool mysql_frm_type(THD *thd, char *path, enum legacy_db_type *dbt);
58
frm_type_enum mysql_frm_type(THD *thd, char *path, enum legacy_db_type *dbt);
52
61
enum release_type { RELEASE_NORMAL, RELEASE_WAIT_FOR_DROP };
63
typedef struct st_filesort_info
65
IO_CACHE *io_cache; /* If sorted through filesort */
66
uchar **sort_keys; /* Buffer for sorting keys */
67
uchar *buffpek; /* Buffer for buffpek structures */
68
uint buffpek_len; /* Max number of buffpeks in the buffer */
69
uchar *addon_buf; /* Pointer to a buffer if sorted with fields */
70
size_t addon_length; /* Length of the buffer */
71
struct st_sort_addon_field *addon_field; /* Pointer to the fields info */
72
void (*unpack)(struct st_sort_addon_field *, uchar *); /* To unpack back */
73
uchar *record_pointers; /* If sorted in memory */
74
ha_rows found_records; /* How many records in sort */
55
79
Values in this enum are used to indicate how a tables TIMESTAMP field
56
80
should be treated. It can be set to the current timestamp on insert or
210
244
enum ha_choice transactional;
211
245
enum ha_choice page_checksum;
213
uint32_t ref_count; /* How many Table objects uses this */
214
uint32_t open_count; /* Number of tables in open list */
215
uint32_t blob_ptr_size; /* 4 or 8 */
216
uint32_t key_block_size; /* create key_block_size, if used */
217
uint32_t null_bytes, last_null_bit_pos;
218
uint32_t fields; /* Number of fields */
219
uint32_t stored_fields; /* Number of stored fields
220
(i.e. without generated-only ones) */
221
uint32_t rec_buff_length; /* Size of table->record[] buffer */
222
uint32_t keys, key_parts;
223
uint32_t max_key_length, max_unique_length, total_key_length;
224
uint32_t uniques; /* Number of UNIQUE index */
225
uint32_t null_fields; /* number of null fields */
226
uint32_t blob_fields; /* number of blob fields */
227
uint32_t timestamp_field_offset; /* Field number for timestamp field */
228
uint32_t varchar_fields; /* number of varchar fields */
229
uint32_t db_create_options; /* Create options from database */
230
uint32_t db_options_in_use; /* Options in use */
231
uint32_t db_record_offset; /* if HA_REC_IN_SEQ */
232
uint32_t rowid_field_offset; /* Field_nr +1 to rowid field */
247
uint ref_count; /* How many TABLE objects uses this */
248
uint open_count; /* Number of tables in open list */
249
uint blob_ptr_size; /* 4 or 8 */
250
uint key_block_size; /* create key_block_size, if used */
251
uint null_bytes, last_null_bit_pos;
252
uint fields; /* Number of fields */
253
uint rec_buff_length; /* Size of table->record[] buffer */
254
uint keys, key_parts;
255
uint max_key_length, max_unique_length, total_key_length;
256
uint uniques; /* Number of UNIQUE index */
257
uint null_fields; /* number of null fields */
258
uint blob_fields; /* number of blob fields */
259
uint timestamp_field_offset; /* Field number for timestamp field */
260
uint varchar_fields; /* number of varchar fields */
261
uint db_create_options; /* Create options from database */
262
uint db_options_in_use; /* Options in use */
263
uint db_record_offset; /* if HA_REC_IN_SEQ */
264
uint raid_type, raid_chunks;
265
uint rowid_field_offset; /* Field_nr +1 to rowid field */
233
266
/* Index of auto-updated TIMESTAMP field in field array */
234
uint32_t primary_key;
235
uint32_t next_number_index; /* autoincrement key number */
236
uint32_t next_number_key_offset; /* autoinc keypart offset in a key */
237
uint32_t next_number_keypart; /* autoinc keypart number in a key */
238
uint32_t error, open_errno, errarg; /* error from open_table_def() */
239
uint32_t column_bitmap_size;
240
unsigned char frm_version;
241
uint32_t vfields; /* Number of virtual fields */
268
uint next_number_index; /* autoincrement key number */
269
uint next_number_key_offset; /* autoinc keypart offset in a key */
270
uint next_number_keypart; /* autoinc keypart number in a key */
271
uint error, open_errno, errarg; /* error from open_table_def() */
272
uint column_bitmap_size;
242
274
bool null_field_first;
275
bool system; /* Set if system table (one record) */
276
bool crypted; /* If .frm file is crypted */
243
277
bool db_low_byte_first; /* Portable row format */
245
279
bool name_lock, replace_with_name_lock;
246
280
bool waiting_on_cond; /* Protection against free */
247
uint32_t table_map_id; /* for row-based replication */
281
ulong table_map_id; /* for row-based replication */
248
282
uint64_t table_map_version;
721
Column reference of a NATURAL/USING join. Since column references in
722
joins can be both from views and stored tables, may point to either a
723
Field (for tables), or a Field_translator (for views).
726
class Natural_join_column: public Sql_alloc
729
Field_translator *view_field; /* Column reference of merge view. */
730
Field *table_field; /* Column reference of table or temp view. */
731
TABLE_LIST *table_ref; /* Original base table/view reference. */
733
True if a common join column of two NATURAL/USING join operands. Notice
734
that when we have a hierarchy of nested NATURAL/USING joins, a column can
735
be common at some level of nesting but it may not be common at higher
736
levels of nesting. Thus this flag may change depending on at which level
737
we are looking at some column.
741
Natural_join_column(Field_translator *field_param, TABLE_LIST *tab);
742
Natural_join_column(Field *field_param, TABLE_LIST *tab);
744
Item *create_item(THD *thd);
746
const char *table_name();
747
const char *db_name();
752
Table reference in the FROM clause.
754
These table references can be of several types that correspond to
755
different SQL elements. Below we list all types of TABLE_LISTs with
756
the necessary conditions to determine when a TABLE_LIST instance
757
belongs to a certain type.
759
1) table (TABLE_LIST::view == NULL)
761
(TABLE_LIST::derived == NULL)
762
- subquery - TABLE_LIST::table is a temp table
763
(TABLE_LIST::derived != NULL)
764
- information schema table
765
(TABLE_LIST::schema_table != NULL)
766
NOTICE: for schema tables TABLE_LIST::field_translation may be != NULL
767
2) view (TABLE_LIST::view != NULL)
768
- merge (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_MERGE)
769
also (TABLE_LIST::field_translation != NULL)
770
- tmptable (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_TMPTABLE)
771
also (TABLE_LIST::field_translation == NULL)
772
3) nested table reference (TABLE_LIST::nested_join != NULL)
773
- table sequence - e.g. (t1, t2, t3)
774
TODO: how to distinguish from a JOIN?
776
TODO: how to distinguish from a table sequence?
778
(TABLE_LIST::natural_join != NULL)
780
(TABLE_LIST::join_using_fields != NULL)
788
TABLE_LIST() {} /* Remove gcc warning */
791
Prepare TABLE_LIST that consists of one table instance to use in
792
simple_open_and_lock_tables
794
inline void init_one_table(const char *db_name_arg,
795
const char *table_name_arg,
796
enum thr_lock_type lock_type_arg)
798
bzero((char*) this, sizeof(*this));
799
db= (char*) db_name_arg;
800
table_name= alias= (char*) table_name_arg;
801
lock_type= lock_type_arg;
805
List of tables local to a subquery (used by SQL_LIST). Considers
806
views as leaves (unlike 'next_leaf' below). Created at parse time
807
in st_select_lex::add_table_to_list() -> table_list.link_in_list().
809
TABLE_LIST *next_local;
810
/* link in a global list of all queries tables */
811
TABLE_LIST *next_global, **prev_global;
812
char *db, *alias, *table_name, *schema_table_name;
813
char *option; /* Used by cache index */
814
Item *on_expr; /* Used with outer join */
817
(Valid only for semi-join nests) Bitmap of tables that are within the
818
semi-join (this is different from bitmap of all nest's children because
819
tables that were pulled out of the semi-join nest remain listed as
822
table_map sj_inner_tables;
823
/* Number of IN-compared expressions */
826
The structure of ON expression presented in the member above
827
can be changed during certain optimizations. This member
828
contains a snapshot of AND-OR structure of the ON expression
829
made after permanent transformations of the parse tree, and is
830
used to restore ON clause before every reexecution of a prepared
831
statement or stored procedure.
834
COND_EQUAL *cond_equal; /* Used with outer join */
836
During parsing - left operand of NATURAL/USING join where 'this' is
837
the right operand. After parsing (this->natural_join == this) iff
838
'this' represents a NATURAL or USING join operation. Thus after
839
parsing 'this' is a NATURAL/USING join iff (natural_join != NULL).
841
TABLE_LIST *natural_join;
843
True if 'this' represents a nested join that is a NATURAL JOIN.
844
For one of the operands of 'this', the member 'natural_join' points
845
to the other operand of 'this'.
847
bool is_natural_join;
848
/* Field names in a USING clause for JOIN ... USING. */
849
List<String> *join_using_fields;
851
Explicitly store the result columns of either a NATURAL/USING join or
852
an operand of such a join.
854
List<Natural_join_column> *join_columns;
855
/* TRUE if join_columns contains all columns of this table reference. */
856
bool is_join_columns_complete;
859
List of nodes in a nested join tree, that should be considered as
860
leaves with respect to name resolution. The leaves are: views,
861
top-most nodes representing NATURAL/USING joins, subqueries, and
862
base tables. All of these TABLE_LIST instances contain a
863
materialized list of columns. The list is local to a subquery.
865
TABLE_LIST *next_name_resolution_table;
866
/* Index names in a "... JOIN ... USE/IGNORE INDEX ..." clause. */
867
List<Index_hint> *index_hints;
868
TABLE *table; /* opened table */
869
uint table_id; /* table id (from binlog) for opened table */
871
select_result for derived table to pass it from table creation to table
874
select_union *derived_result;
876
Reference from aux_tables to local list entry of main select of
877
multi-delete statement:
878
delete t1 from t2,t1 where t1.a<'B' and t2.b=t1.b;
879
here it will be reference of first occurrence of t1 to second (as you
880
can see this lists can't be merged)
882
TABLE_LIST *correspondent_table;
883
st_select_lex_unit *derived; /* SELECT_LEX_UNIT of derived table */
884
ST_SCHEMA_TABLE *schema_table; /* Information_schema table */
885
st_select_lex *schema_select_lex;
887
True when the view field translation table is used to convert
888
schema table fields for backwards compatibility with SHOW command.
890
bool schema_table_reformed;
891
TMP_TABLE_PARAM *schema_table_param;
892
/* link to select_lex where this table was used */
893
st_select_lex *select_lex;
894
Field_translator *field_translation; /* array of VIEW fields */
895
/* pointer to element after last one in translation table above */
896
Field_translator *field_translation_end;
898
List (based on next_local) of underlying tables of this view. I.e. it
899
does not include the tables of subqueries used in the view. Is set only
902
TABLE_LIST *merge_underlying_list;
905
- in case of the view it is the list of all (not only underlying
906
tables but also used in subquery ones) tables of the view.
908
List<TABLE_LIST> *view_tables;
909
/* most upper view this table belongs to */
910
TABLE_LIST *belong_to_view;
911
/* Ptr to parent MERGE table list item. See top comment in ha_myisammrg.cc */
912
TABLE_LIST *parent_l;
914
List of all base tables local to a subquery including all view
915
tables. Unlike 'next_local', this in this list views are *not*
916
leaves. Created in setup_tables() -> make_leaves_list().
918
TABLE_LIST *next_leaf;
919
/* data need by some engines in query cache*/
920
uint64_t engine_data;
921
/* call back function for asking handler about caching in query cache */
922
qc_engine_callback callback_func;
923
thr_lock_type lock_type;
924
uint outer_join; /* Which join type */
925
uint shared; /* Used in multi-upd */
927
size_t table_name_length;
928
bool straight; /* optimize with prev table */
929
bool updating; /* for replicate-do/ignore table */
930
bool force_index; /* prefer index over table scan */
931
bool ignore_leaves; /* preload only non-leaf nodes */
932
table_map dep_tables; /* tables the table depends on */
933
table_map on_expr_dep_tables; /* tables on expression depends on */
934
struct st_nested_join *nested_join; /* if the element is a nested join */
935
TABLE_LIST *embedding; /* nested join containing the table */
936
List<TABLE_LIST> *join_list;/* join list the table belongs to */
937
bool cacheable_table; /* stop PS caching */
938
/* FRMTYPE_ERROR if any type is acceptable */
939
enum frm_type_enum required_type;
940
handlerton *db_type; /* table_type for handler */
941
char timestamp_buffer[20]; /* buffer for timestamp (19+1) */
943
This TABLE_LIST object corresponds to the table to be created
944
so it is possible that it does not exist (used in CREATE TABLE
945
... SELECT implementation).
948
/* For transactional locking. */
949
int lock_timeout; /* NOWAIT or WAIT [X] */
950
bool lock_transactional; /* If transactional lock requested. */
951
bool internal_tmp_table;
952
/** TRUE if an alias for this table was specified in the SQL. */
954
/** TRUE if the table is referred to in the statement using a fully
955
qualified name (<db_name>.<table_name>).
959
uint i_s_requested_object;
960
bool has_db_lookup_value;
961
bool has_table_lookup_value;
962
uint table_open_method;
963
enum enum_schema_table_state schema_table_state;
964
void set_underlying_merge();
965
bool setup_underlying(THD *thd);
966
void cleanup_items();
968
If you change placeholder(), please check the condition in
969
check_transactional_lock() too.
973
return derived || schema_table || (create && !table->db_stat) || !table;
975
void print(THD *thd, String *str, enum_query_type query_type);
976
bool set_insert_values(MEM_ROOT *mem_root);
977
TABLE_LIST *find_underlying_table(TABLE *table);
978
TABLE_LIST *first_leaf_for_name_resolution();
979
TABLE_LIST *last_leaf_for_name_resolution();
980
bool is_leaf_for_name_resolution();
981
inline TABLE_LIST *top_table()
982
{ return belong_to_view ? belong_to_view : this; }
985
Cleanup for re-execution in a prepared statement or a stored
988
void reinit_before_use(THD *thd);
989
Item_subselect *containing_subselect();
992
Compiles the tagged hints list and fills up st_table::keys_in_use_for_query,
993
st_table::keys_in_use_for_group_by, st_table::keys_in_use_for_order_by,
994
st_table::force_index and st_table::covering_keys.
996
bool process_index_hints(TABLE *table);
999
bool prep_where(THD *thd, Item **conds, bool no_where_clause);
1001
Cleanup for re-execution in a prepared statement or a stored
1009
Iterator over the fields of a generic table reference.
1012
class Field_iterator: public Sql_alloc
1015
Field_iterator() {} /* Remove gcc warning */
1016
virtual ~Field_iterator() {}
1017
virtual void set(TABLE_LIST *)= 0;
1018
virtual void next()= 0;
1019
virtual bool end_of_fields()= 0; /* Return 1 at end of list */
1020
virtual const char *name()= 0;
1021
virtual Item *create_item(THD *)= 0;
1022
virtual Field *field()= 0;
1027
Iterator over the fields of a base table, view with temporary
1031
class Field_iterator_table: public Field_iterator
1035
Field_iterator_table() :ptr(0) {}
1036
void set(TABLE_LIST *table) { ptr= table->table->field; }
1037
void set_table(TABLE *table) { ptr= table->field; }
1038
void next() { ptr++; }
1039
bool end_of_fields() { return *ptr == 0; }
1041
Item *create_item(THD *thd);
1042
Field *field() { return *ptr; }
1046
/* Iterator over the fields of a merge view. */
1048
class Field_iterator_view: public Field_iterator
1050
Field_translator *ptr, *array_end;
1053
Field_iterator_view() :ptr(0), array_end(0) {}
1054
void set(TABLE_LIST *table);
1055
void next() { ptr++; }
1056
bool end_of_fields() { return ptr == array_end; }
1058
Item *create_item(THD *thd);
1059
Item **item_ptr() {return &ptr->item; }
1060
Field *field() { return 0; }
1061
inline Item *item() { return ptr->item; }
1062
Field_translator *field_translator() { return ptr; }
1067
Field_iterator interface to the list of materialized fields of a
1071
class Field_iterator_natural_join: public Field_iterator
1073
List_iterator_fast<Natural_join_column> column_ref_it;
1074
Natural_join_column *cur_column_ref;
1076
Field_iterator_natural_join() :cur_column_ref(NULL) {}
1077
~Field_iterator_natural_join() {}
1078
void set(TABLE_LIST *table);
1080
bool end_of_fields() { return !cur_column_ref; }
1081
const char *name() { return cur_column_ref->name(); }
1082
Item *create_item(THD *thd) { return cur_column_ref->create_item(thd); }
1083
Field *field() { return cur_column_ref->field(); }
1084
Natural_join_column *column_ref() { return cur_column_ref; }
1089
Generic iterator over the fields of an arbitrary table reference.
1092
This class unifies the various ways of iterating over the columns
1093
of a table reference depending on the type of SQL entity it
1094
represents. If such an entity represents a nested table reference,
1095
this iterator encapsulates the iteration over the columns of the
1096
members of the table reference.
1099
The implementation assumes that all underlying NATURAL/USING table
1100
references already contain their result columns and are linked into
1101
the list TABLE_LIST::next_name_resolution_table.
1104
class Field_iterator_table_ref: public Field_iterator
1106
TABLE_LIST *table_ref, *first_leaf, *last_leaf;
1107
Field_iterator_table table_field_it;
1108
Field_iterator_view view_field_it;
1109
Field_iterator_natural_join natural_join_it;
1110
Field_iterator *field_it;
1111
void set_field_iterator();
1113
Field_iterator_table_ref() :field_it(NULL) {}
1114
void set(TABLE_LIST *table);
1116
bool end_of_fields()
1117
{ return (table_ref == last_leaf && field_it->end_of_fields()); }
1118
const char *name() { return field_it->name(); }
1119
const char *table_name();
1120
const char *db_name();
1121
Item *create_item(THD *thd) { return field_it->create_item(thd); }
1122
Field *field() { return field_it->field(); }
1123
Natural_join_column *get_or_create_column_ref(TABLE_LIST *parent_table_ref);
1124
Natural_join_column *get_natural_column_ref();
1128
typedef struct st_nested_join
1130
List<TABLE_LIST> join_list; /* list of elements in the nested join */
1131
table_map used_tables; /* bitmap of tables in the nested join */
1132
table_map not_null_tables; /* tables that rejects nulls */
1133
struct st_join_table *first_nested;/* the first nested table in the plan */
1135
Used to count tables in the nested join in 2 isolated places:
1136
1. In make_outerjoin_info().
1137
2. check_interleaving_with_nj/restore_prev_nj_state (these are called
1138
by the join optimizer.
1139
Before each use the counters are zeroed by reset_nj_counters.
1142
nested_join_map nj_map; /* Bit used to identify this nested join*/
1144
(Valid only for semi-join nests) Bitmap of tables outside the semi-join
1145
that are used within the semi-join's ON condition.
1147
table_map sj_depends_on;
1148
/* Outer non-trivially correlated tables */
1149
table_map sj_corr_tables;
1150
List<Item> sj_outer_expr_list;
731
1154
typedef struct st_changed_table_list
733
1156
struct st_changed_table_list *next;
739
typedef struct st_open_table_list
1159
} CHANGED_TABLE_LIST;
1162
typedef struct st_open_table_list{
741
1163
struct st_open_table_list *next;
742
1164
char *db,*table;
743
uint32_t in_use,locked;
747
#endif /* DRIZZLED_TABLE_H */
1165
uint32 in_use,locked;
1168
typedef struct st_table_field_w_type
1173
} TABLE_FIELD_W_TYPE;
1177
table_check_intact(TABLE *table, const uint table_f_count,
1178
const TABLE_FIELD_W_TYPE *table_def);
1180
static inline my_bitmap_map *tmp_use_all_columns(TABLE *table,
1183
my_bitmap_map *old= bitmap->bitmap;
1184
bitmap->bitmap= table->s->all_set.bitmap;
1189
static inline void tmp_restore_column_map(MY_BITMAP *bitmap,
1192
bitmap->bitmap= old;
1195
/* The following is only needed for debugging */
1197
static inline my_bitmap_map *dbug_tmp_use_all_columns(TABLE *table __attribute__((__unused__)),
1198
MY_BITMAP *bitmap __attribute__((__unused__)))
1203
static inline void dbug_tmp_restore_column_map(MY_BITMAP *bitmap __attribute__((__unused__)),
1204
my_bitmap_map *old __attribute__((__unused__)))
1209
size_t max_row_length(TABLE *table, const uchar *data);