~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_list.h

  • Committer: Padraig O'Sullivan
  • Date: 2009-09-13 01:03:01 UTC
  • mto: (1126.9.2 captain-20090915-01)
  • mto: This revision was merged to the branch mainline in revision 1133.
  • Revision ID: osullivan.padraig@gmail.com-20090913010301-tcvvezipx1124acy
Added calls to the dtrace delete begin/end probes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
 
21
 
21
22
#ifndef DRIZZLED_TABLE_LIST_H
22
23
#define DRIZZLED_TABLE_LIST_H
23
24
 
 
25
/*
 
26
  Table reference in the FROM clause.
 
27
 
 
28
  These table references can be of several types that correspond to
 
29
  different SQL elements. Below we list all types of TableLists with
 
30
  the necessary conditions to determine when a TableList instance
 
31
  belongs to a certain type.
 
32
 
 
33
  1) table (TableList::view == NULL)
 
34
     - base table
 
35
       (TableList::derived == NULL)
 
36
     - subquery - TableList::table is a temp table
 
37
       (TableList::derived != NULL)
 
38
     - information schema table
 
39
       (TableList::schema_table != NULL)
 
40
       NOTICE: for schema tables TableList::field_translation may be != NULL
 
41
  2) Was VIEW 
 
42
  3) nested table reference (TableList::nested_join != NULL)
 
43
     - table sequence - e.g. (t1, t2, t3)
 
44
       TODO: how to distinguish from a JOIN?
 
45
     - general JOIN
 
46
       TODO: how to distinguish from a table sequence?
 
47
     - NATURAL JOIN
 
48
       (TableList::natural_join != NULL)
 
49
       - JOIN ... USING
 
50
         (TableList::join_using_fields != NULL)
 
51
     - semi-join
 
52
       ;
 
53
*/
 
54
 
 
55
 
24
56
#include <drizzled/table.h>
25
57
 
26
 
namespace drizzled
27
 
{
28
 
 
29
58
class Index_hint;
30
59
class COND_EQUAL;
31
60
class Natural_join_column;
32
61
class select_union;
33
62
class Select_Lex_Unit;
 
63
class InfoSchemaTable;
34
64
class Select_Lex;
35
65
class Tmp_Table_Param;
36
66
class Item_subselect;
37
67
class Table;
38
68
 
39
 
namespace plugin
40
 
{
41
 
  class StorageEngine;
42
 
}
43
 
 
44
69
struct nested_join_st;
45
70
 
46
 
/**
47
 
 * A Table referenced in the FROM clause.
48
 
 *
49
 
 * These table references can be of several types that correspond to
50
 
 * different SQL elements. Below we list all types of TableLists with
51
 
 * the necessary conditions to determine when a TableList instance
52
 
 * belongs to a certain type.
53
 
 *
54
 
 * 1) table (TableList::view == NULL)
55
 
 *    - base table
56
 
 *    (TableList::derived == NULL)
57
 
 *    - subquery - TableList::table is a temp table
58
 
 *    (TableList::derived != NULL)
59
 
 *    
60
 
 *    @note
61
 
 *
62
 
 *    for schema tables TableList::field_translation may be != NULL
63
 
 *
64
 
 * 2) Was VIEW 
65
 
 * 3) nested table reference (TableList::nested_join != NULL)
66
 
 *     - table sequence - e.g. (t1, t2, t3)
67
 
 *     @todo how to distinguish from a JOIN?
68
 
 *     - general JOIN
69
 
 *     @todo how to distinguish from a table sequence?
70
 
 *     - NATURAL JOIN
71
 
 *     (TableList::natural_join != NULL)
72
 
 *     - JOIN ... USING
73
 
 *     (TableList::join_using_fields != NULL)
74
 
 *     - semi-join
75
 
 */
76
71
class TableList
77
72
{
78
73
public:
83
78
    db(NULL),
84
79
    alias(NULL),
85
80
    table_name(NULL),
 
81
    schema_table_name(NULL),
86
82
    option(NULL),
87
83
    on_expr(NULL),
 
84
    table_id(0),
88
85
    table(NULL),
89
86
    prep_on_expr(NULL),
90
87
    cond_equal(NULL),
92
89
    is_natural_join(false),
93
90
    is_join_columns_complete(false),
94
91
    straight(false),
 
92
    updating(false), 
95
93
    force_index(false),
96
94
    ignore_leaves(false),
97
95
    create(false),
101
99
    index_hints(NULL),
102
100
    derived_result(NULL),
103
101
    derived(NULL),
 
102
    schema_table(NULL),
104
103
    schema_select_lex(NULL),
 
104
    schema_table_param(NULL),
105
105
    select_lex(NULL),
106
106
    next_leaf(NULL),
 
107
    // lock_type
107
108
    outer_join(0),
108
 
    shared(0),
 
109
    shared(0),  
109
110
    i_s_requested_object(0),
110
111
    db_length(0),
111
112
    table_name_length(0),
115
116
    embedding(NULL),
116
117
    join_list(NULL),
117
118
    db_type(NULL),
 
119
    // timestamp_buffer[20];
118
120
    internal_tmp_table(false),
119
121
    is_alias(false),
120
122
    is_fqtn(false),
121
123
    has_db_lookup_value(false),
122
 
    has_table_lookup_value(false)
123
 
  {}
 
124
    has_table_lookup_value(false),
 
125
    table_open_method(0)
 
126
    // schema_table_state(0)
 
127
    {}                          /* Remove gcc warning */
124
128
 
125
 
  /**
126
 
   * List of tables local to a subquery (used by SQL_LIST). Considers
127
 
   * views as leaves (unlike 'next_leaf' below). Created at parse time
128
 
   * in Select_Lex::add_table_to_list() -> table_list.link_in_list().
129
 
   */
 
129
  /*
 
130
    List of tables local to a subquery (used by SQL_LIST). Considers
 
131
    views as leaves (unlike 'next_leaf' below). Created at parse time
 
132
    in Select_Lex::add_table_to_list() -> table_list.link_in_list().
 
133
  */
130
134
  TableList *next_local;
131
135
 
132
 
  /** link in a global list of all queries tables */
 
136
  /* link in a global list of all queries tables */
133
137
  TableList *next_global; 
134
138
  TableList **prev_global;
135
139
 
136
 
  char *db;
137
 
  const char *alias;
138
 
  char *table_name;
139
 
  char *option; ///< Used by cache index
140
 
  Item *on_expr; ///< Used with outer join
141
 
  Table *table; ///< opened table
142
 
  /**
143
 
   * The structure of ON expression presented in the member above
144
 
   * can be changed during certain optimizations. This member
145
 
   * contains a snapshot of AND-OR structure of the ON expression
146
 
   * made after permanent transformations of the parse tree, and is
147
 
   * used to restore ON clause before every reexecution of a prepared
148
 
   * statement or stored procedure.
149
 
   */
150
 
  Item *prep_on_expr;
151
 
  COND_EQUAL *cond_equal; ///< Used with outer join
152
 
  /**
153
 
   * During parsing - left operand of NATURAL/USING join where 'this' is
154
 
   * the right operand. After parsing (this->natural_join == this) iff
155
 
   * 'this' represents a NATURAL or USING join operation. Thus after
156
 
   * parsing 'this' is a NATURAL/USING join iff (natural_join != NULL).
157
 
   */
 
140
  char          *db;
 
141
  const char            *alias;
 
142
  char          *table_name;
 
143
  char          *schema_table_name;
 
144
  char    *option;                /* Used by cache index  */
 
145
  Item          *on_expr;               /* Used with outer join */
 
146
  uint32_t          table_id; /* table id (from binlog) for opened table */
 
147
  Table        *table;    /* opened table */
 
148
  /*
 
149
    The structure of ON expression presented in the member above
 
150
    can be changed during certain optimizations. This member
 
151
    contains a snapshot of AND-OR structure of the ON expression
 
152
    made after permanent transformations of the parse tree, and is
 
153
    used to restore ON clause before every reexecution of a prepared
 
154
    statement or stored procedure.
 
155
  */
 
156
  Item          *prep_on_expr;
 
157
  COND_EQUAL    *cond_equal;            /* Used with outer join */
 
158
  /*
 
159
    During parsing - left operand of NATURAL/USING join where 'this' is
 
160
    the right operand. After parsing (this->natural_join == this) iff
 
161
    'this' represents a NATURAL or USING join operation. Thus after
 
162
    parsing 'this' is a NATURAL/USING join iff (natural_join != NULL).
 
163
  */
158
164
  TableList *natural_join;
159
 
  /**
160
 
   * True if 'this' represents a nested join that is a NATURAL JOIN.
161
 
   * For one of the operands of 'this', the member 'natural_join' points
162
 
   * to the other operand of 'this'.
163
 
   */
 
165
  /*
 
166
    True if 'this' represents a nested join that is a NATURAL JOIN.
 
167
    For one of the operands of 'this', the member 'natural_join' points
 
168
    to the other operand of 'this'.
 
169
  */
164
170
  bool is_natural_join;
165
171
 
166
 
  /** true if join_columns contains all columns of this table reference. */
 
172
  /* true if join_columns contains all columns of this table reference. */
167
173
  bool is_join_columns_complete;
168
174
 
169
 
  bool straight; ///< optimize with prev table
170
 
  bool force_index; ///< prefer index over table scan
171
 
  bool ignore_leaves; ///< preload only non-leaf nodes
172
 
 
173
 
  /**
174
 
   * This TableList object corresponds to the table to be created
175
 
   * so it is possible that it does not exist (used in CREATE TABLE
176
 
   * ... SELECT implementation).
177
 
   */
178
 
  bool create;
179
 
 
180
 
  /** Field names in a USING clause for JOIN ... USING. */
 
175
  bool          straight;               /* optimize with prev table */
 
176
  bool          updating;               /* for replicate-do/ignore table */
 
177
  bool          force_index;            /* prefer index over table scan */
 
178
  bool          ignore_leaves;          /* preload only non-leaf nodes */
 
179
 
 
180
  /*
 
181
    This TableList object corresponds to the table to be created
 
182
    so it is possible that it does not exist (used in CREATE TABLE
 
183
    ... SELECT implementation).
 
184
  */
 
185
  bool          create;
 
186
 
 
187
  /* Field names in a USING clause for JOIN ... USING. */
181
188
  List<String> *join_using_fields;
182
 
  /**
183
 
   * Explicitly store the result columns of either a NATURAL/USING join or
184
 
   * an operand of such a join.
185
 
   */
 
189
  /*
 
190
    Explicitly store the result columns of either a NATURAL/USING join or
 
191
    an operand of such a join.
 
192
  */
186
193
  List<Natural_join_column> *join_columns;
187
194
 
188
 
  /**
189
 
   * List of nodes in a nested join tree, that should be considered as
190
 
   * leaves with respect to name resolution. The leaves are: views,
191
 
   * top-most nodes representing NATURAL/USING joins, subqueries, and
192
 
   * base tables. All of these TableList instances contain a
193
 
   * materialized list of columns. The list is local to a subquery.
194
 
   */
 
195
  /*
 
196
    List of nodes in a nested join tree, that should be considered as
 
197
    leaves with respect to name resolution. The leaves are: views,
 
198
    top-most nodes representing NATURAL/USING joins, subqueries, and
 
199
    base tables. All of these TableList instances contain a
 
200
    materialized list of columns. The list is local to a subquery.
 
201
  */
195
202
  TableList *next_name_resolution_table;
196
 
  /** Index names in a "... JOIN ... USE/IGNORE INDEX ..." clause. */
 
203
  /* Index names in a "... JOIN ... USE/IGNORE INDEX ..." clause. */
197
204
  List<Index_hint> *index_hints;
198
 
  /**
199
 
   * select_result for derived table to pass it from table creation to table
200
 
   * filling procedure
201
 
   */
202
 
  select_union *derived_result;
203
 
  Select_Lex_Unit *derived; ///< Select_Lex_Unit of derived table */
204
 
  Select_Lex *schema_select_lex;
205
 
  /** link to select_lex where this table was used */
206
 
  Select_Lex *select_lex;
207
 
  /**
208
 
   * List of all base tables local to a subquery including all view
209
 
   * tables. Unlike 'next_local', this in this list views are *not*
210
 
   * leaves. Created in setup_tables() -> make_leaves_list().
211
 
   */
212
 
  TableList *next_leaf;
 
205
  /*
 
206
    select_result for derived table to pass it from table creation to table
 
207
    filling procedure
 
208
  */
 
209
  select_union  *derived_result;
 
210
  Select_Lex_Unit *derived;             /* Select_Lex_Unit of derived table */
 
211
  InfoSchemaTable *schema_table;        /* Information_schema table */
 
212
  Select_Lex    *schema_select_lex;
 
213
  Tmp_Table_Param *schema_table_param;
 
214
  /* link to select_lex where this table was used */
 
215
  Select_Lex    *select_lex;
 
216
  /*
 
217
    List of all base tables local to a subquery including all view
 
218
    tables. Unlike 'next_local', this in this list views are *not*
 
219
    leaves. Created in setup_tables() -> make_leaves_list().
 
220
  */
 
221
  TableList     *next_leaf;
213
222
  thr_lock_type lock_type;
214
 
  uint32_t outer_join; ///< Which join type
215
 
  uint32_t shared; ///<Used in multi-upd
 
223
  uint32_t              outer_join;             /* Which join type */
 
224
  uint32_t              shared;                 /* Used in multi-upd */
216
225
  uint32_t i_s_requested_object;
217
 
  size_t db_length;
218
 
  size_t table_name_length;
219
 
  table_map dep_tables; ///< tables the table depends on
220
 
  table_map on_expr_dep_tables; ///< tables on expression depends on
221
 
  nested_join_st *nested_join; ///< if the element is a nested join
222
 
  TableList *embedding; ///< nested join containing the table
223
 
  List<TableList> *join_list; ///< join list the table belongs to
224
 
  plugin::StorageEngine *db_type; ///< table_type for handler
225
 
  char timestamp_buffer[20]; ///< buffer for timestamp (19+1)
226
 
  bool internal_tmp_table;
 
226
  size_t        db_length;
 
227
  size_t        table_name_length;
 
228
  table_map     dep_tables;             /* tables the table depends on      */
 
229
  table_map     on_expr_dep_tables;     /* tables on expression depends on  */
 
230
  nested_join_st *nested_join;   /* if the element is a nested join  */
 
231
  TableList *embedding;             /* nested join containing the table */
 
232
  List<TableList> *join_list;/* join list the table belongs to   */
 
233
  StorageEngine *db_type;               /* table_type for handler */
 
234
  char          timestamp_buffer[20];   /* buffer for timestamp (19+1) */
 
235
  bool          internal_tmp_table;
227
236
  /** true if an alias for this table was specified in the SQL. */
228
 
  bool is_alias;
229
 
  /** 
230
 
   * true if the table is referred to in the statement using a fully
231
 
   * qualified name (<db_name>.<table_name>).
232
 
   */
233
 
  bool is_fqtn;
 
237
  bool          is_alias;
 
238
  /** true if the table is referred to in the statement using a fully
 
239
      qualified name (<db_name>.<table_name>).
 
240
  */
 
241
  bool          is_fqtn;
234
242
 
235
243
  bool has_db_lookup_value;
236
244
  bool has_table_lookup_value;
 
245
  uint32_t table_open_method;
 
246
  enum enum_schema_table_state schema_table_state;
237
247
 
238
248
  void set_underlying_merge();
239
249
  bool setup_underlying(Session *session);
240
250
 
241
 
  /**
242
 
   * If you change placeholder(), please check the condition in
243
 
   * check_transactional_lock() too.
244
 
   */
 
251
  /*
 
252
    If you change placeholder(), please check the condition in
 
253
    check_transactional_lock() too.
 
254
  */
245
255
  bool placeholder();
246
 
  /**
247
 
   * Print table as it should be in join list.
248
 
   * 
249
 
   * @param str   string where table should be printed
250
 
   */
251
256
  void print(Session *session, String *str, enum_query_type query_type);
252
 
  /**
253
 
   * Sets insert_values buffer
254
 
   *
255
 
   * @param[in] memory pool for allocating
256
 
   *
257
 
   * @retval
258
 
   *  false - OK
259
 
   * @retval
260
 
   *  true - out of memory
261
 
   */
262
 
  bool set_insert_values(memory::Root *mem_root);
263
 
  /**
264
 
   * Find underlying base tables (TableList) which represent given
265
 
   * table_to_find (Table)
266
 
   *
267
 
   * @param[in] table to find
268
 
   *
269
 
   * @retval
270
 
   *  NULL if table is not found
271
 
   * @retval
272
 
   *  Pointer to found table reference
273
 
   */
 
257
  bool set_insert_values(MEM_ROOT *mem_root);
274
258
  TableList *find_underlying_table(Table *table);
275
 
  /**
276
 
   * Retrieve the first (left-most) leaf in a nested join tree with
277
 
   * respect to name resolution.
278
 
   *
279
 
   * @details
280
 
   *
281
 
   * Given that 'this' is a nested table reference, recursively walk
282
 
   * down the left-most children of 'this' until we reach a leaf
283
 
   * table reference with respect to name resolution.
284
 
   *
285
 
   * @retval
286
 
   *  If 'this' is a nested table reference - the left-most child of
287
 
   *  the tree rooted in 'this',
288
 
   *  else return 'this'
289
 
   */
290
259
  TableList *first_leaf_for_name_resolution();
291
 
  /**
292
 
   * Retrieve the last (right-most) leaf in a nested join tree with
293
 
   * respect to name resolution.
294
 
   *
295
 
   * @details
296
 
   *
297
 
   * Given that 'this' is a nested table reference, recursively walk
298
 
   * down the right-most children of 'this' until we reach a leaf
299
 
   * table reference with respect to name resolution.
300
 
   *
301
 
   * @retval
302
 
   *  If 'this' is a nested table reference - the right-most child of
303
 
   *  the tree rooted in 'this',
304
 
   *  else 'this'
305
 
   */
306
260
  TableList *last_leaf_for_name_resolution();
307
 
  /**
308
 
   * Test if this is a leaf with respect to name resolution.
309
 
   *
310
 
   * @details
311
 
   * 
312
 
   * A table reference is a leaf with respect to name resolution if
313
 
   * it is either a leaf node in a nested join tree (table, view,
314
 
   * schema table, subquery), or an inner node that represents a
315
 
   * NATURAL/USING join, or a nested join with materialized join
316
 
   * columns.
317
 
   *
318
 
   * @retval
319
 
   *  true if a leaf, false otherwise.
320
 
   */
321
261
  bool is_leaf_for_name_resolution();
322
262
  inline TableList *top_table()
323
263
  { return this; }
324
264
 
325
 
  /**
326
 
   * Return subselect that contains the FROM list this table is taken from
327
 
   *
328
 
   * @retval
329
 
   *  Subselect item for the subquery that contains the FROM list
330
 
   *  this table is taken from if there is any
331
 
   * @retval
332
 
   *  NULL otherwise
333
 
   */
334
265
  Item_subselect *containing_subselect();
335
266
 
336
 
  /**
337
 
   * Compiles the tagged hints list and fills up st_table::keys_in_use_for_query,
338
 
   * st_table::keys_in_use_for_group_by, st_table::keys_in_use_for_order_by,
339
 
   * st_table::force_index and st_table::covering_keys.
340
 
   *
341
 
   * @param the Table to operate on.
342
 
   *
343
 
   * @details
344
 
   *
345
 
   * The parser collects the index hints for each table in a "tagged list"
346
 
   * (TableList::index_hints). Using the information in this tagged list
347
 
   * this function sets the members Table::keys_in_use_for_query,
348
 
   * Table::keys_in_use_for_group_by, Table::keys_in_use_for_order_by,
349
 
   * Table::force_index and Table::covering_keys.
350
 
   *
351
 
   * Current implementation of the runtime does not allow mixing FORCE INDEX
352
 
   * and USE INDEX, so this is checked here. Then the FORCE INDEX list
353
 
   * (if non-empty) is appended to the USE INDEX list and a flag is set.
354
 
   * 
355
 
   * Multiple hints of the same kind are processed so that each clause
356
 
   * is applied to what is computed in the previous clause.
357
 
   * 
358
 
   * For example:
359
 
   *       USE INDEX (i1) USE INDEX (i2)
360
 
   *    is equivalent to
361
 
   *       USE INDEX (i1,i2)
362
 
   *    and means "consider only i1 and i2".
363
 
   *
364
 
   * Similarly
365
 
   *       USE INDEX () USE INDEX (i1)
366
 
   *    is equivalent to
367
 
   *       USE INDEX (i1)
368
 
   *    and means "consider only the index i1"
369
 
   *
370
 
   * It is OK to have the same index several times, e.g. "USE INDEX (i1,i1)" is
371
 
   * not an error.
372
 
   *
373
 
   * Different kind of hints (USE/FORCE/IGNORE) are processed in the following
374
 
   * order:
375
 
   *    1. All indexes in USE (or FORCE) INDEX are added to the mask.
376
 
   *    2. All IGNORE INDEX
377
 
   *       e.g. "USE INDEX i1, IGNORE INDEX i1, USE INDEX i1" will not use i1 at all
378
 
   *       as if we had "USE INDEX i1, USE INDEX i1, IGNORE INDEX i1".
379
 
   *       As an optimization if there is a covering index, and we have
380
 
   *       IGNORE INDEX FOR GROUP/order_st, and this index is used for the JOIN part,
381
 
   *       then we have to ignore the IGNORE INDEX FROM GROUP/order_st.
382
 
   *
383
 
   * @retval
384
 
   *   false no errors found
385
 
   * @retval
386
 
   *   true found and reported an error.
387
 
   */
 
267
  /*
 
268
    Compiles the tagged hints list and fills up st_table::keys_in_use_for_query,
 
269
    st_table::keys_in_use_for_group_by, st_table::keys_in_use_for_order_by,
 
270
    st_table::force_index and st_table::covering_keys.
 
271
  */
388
272
  bool process_index_hints(Table *table);
389
 
  /**
390
 
   * Creates a table definition cache key for this table entry.
391
 
   *
392
 
   * @param[out] Create key here (must be of size MAX_DBKEY_LENGTH)
393
 
   *
394
 
   * @note
395
 
   *
396
 
   * The table cache_key is created from:
397
 
   *   db_name + \0
398
 
   *   table_name + \0
399
 
   *
400
 
   * if the table is a tmp table, we add the following to make each tmp table
401
 
   * unique on the slave:
402
 
   *
403
 
   * 4 bytes for master thread id
404
 
   * 4 bytes pseudo thread id
405
 
   *
406
 
   * @retval
407
 
   *  Length of key
408
 
   */
409
273
  uint32_t create_table_def_key(char *key);
410
274
};
411
275
 
412
276
void close_thread_tables(Session *session);
413
277
 
414
 
} /* namespace drizzled */
415
 
 
416
278
#endif /* DRIZZLED_TABLE_LIST_H */