~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_list.h

  • Committer: Brian Aker
  • Date: 2010-01-22 00:53:13 UTC
  • Revision ID: brian@gaz-20100122005313-jmizcbcdi1lt4tcx
Revert db patch.

Show diffs side-by-side

added added

removed removed

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