~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.h

  • Committer: Brian Aker
  • Date: 2009-07-11 05:59:19 UTC
  • mfrom: (1089.1.9 merge)
  • Revision ID: brian@gaz-20090711055919-m4px3crrdgta5lie
Collection of patches from new-cleanup (includes asserts for field in debug)

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include "drizzled/session.h"
25
25
#include "drizzled/field/varstring.h"
26
26
#include "drizzled/item/null.h"
 
27
#include <drizzled/enum_nested_loop_state.h>
 
28
#include "drizzled/join_cache.h"
 
29
#include "drizzled/join_table.h"
 
30
 
27
31
 
28
32
class select_result;
29
33
 
41
45
#define KEY_OPTIMIZE_EXISTS             1
42
46
#define KEY_OPTIMIZE_REF_OR_NULL        2
43
47
 
44
 
typedef struct keyuse_t 
 
48
class KeyUse 
45
49
{
 
50
public:
46
51
  Table *table; /**< Pointer to the table this key belongs to */
47
52
  Item *val;    /**< or value if no field */
48
53
  table_map used_tables;
57
62
  */
58
63
  bool null_rejecting;
59
64
  /**
60
 
    !NULL - This KEYUSE was created from an equality that was wrapped into
 
65
    !NULL - This KeyUse was created from an equality that was wrapped into
61
66
            an Item_func_trig_cond. This means the equality (and validity of
62
 
            this KEYUSE element) can be turned on and off. The on/off state
 
67
            this KeyUse element) can be turned on and off. The on/off state
63
68
            is indicted by the pointed value:
64
69
              *cond_guard == true <=> equality condition is on
65
70
              *cond_guard == false <=> equality condition is off
72
77
     MAX_UINT  Otherwise
73
78
  */
74
79
  uint32_t sj_pred_no;
75
 
} KEYUSE;
76
 
 
77
 
class StoredKey;
78
 
 
79
 
typedef struct st_table_ref
80
 
{
81
 
  bool key_err;
82
 
  uint32_t key_parts; /**< num of key parts */
83
 
  uint32_t key_length; /**< length of key_buff */
84
 
  int32_t key; /**< key no (index) */
85
 
  unsigned char *key_buff; /**< value to look for with key */
86
 
  unsigned char *key_buff2; /**< key_buff+key_length */
87
 
  StoredKey **key_copy; /**< No idea what this does... */
88
 
  Item **items; /**< val()'s for each keypart */
89
 
  /**
90
 
    Array of pointers to trigger variables. Some/all of the pointers may be
91
 
    NULL.  The ref access can be used iff
92
 
 
93
 
      for each used key part i, (!cond_guards[i] || *cond_guards[i])
94
 
 
95
 
    This array is used by subquery code. The subquery code may inject
96
 
    triggered conditions, i.e. conditions that can be 'switched off'. A ref
97
 
    access created from such condition is not valid when at least one of the
98
 
    underlying conditions is switched off (see subquery code for more details)
99
 
  */
100
 
  bool **cond_guards;
101
 
  /**
102
 
    (null_rejecting & (1<<i)) means the condition is '=' and no matching
103
 
    rows will be produced if items[i] IS NULL (see add_not_null_conds())
104
 
  */
105
 
  key_part_map  null_rejecting;
106
 
  table_map     depend_map; /**< Table depends on these tables. */
107
 
  /** null byte position in the key_buf. Used for REF_OR_NULL optimization */
108
 
  unsigned char *null_ref_key;
109
 
  /**
110
 
    true <=> disable the "cache" as doing lookup with the same key value may
111
 
    produce different results (because of Index Condition Pushdown)
112
 
  */
113
 
  bool disable_cache;
114
 
} TABLE_REF;
 
80
};
115
81
 
116
82
class JOIN;
117
83
 
118
 
#include "drizzled/join_cache.h"
119
 
 
120
 
/** The states in which a nested loop join can be in */
121
 
enum enum_nested_loop_state
122
 
{
123
 
  NESTED_LOOP_KILLED= -2,
124
 
  NESTED_LOOP_ERROR= -1,
125
 
  NESTED_LOOP_OK= 0,
126
 
  NESTED_LOOP_NO_MORE_ROWS= 1,
127
 
  NESTED_LOOP_QUERY_LIMIT= 3,
128
 
  NESTED_LOOP_CURSOR_LIMIT= 4
129
 
};
130
 
 
131
 
/** Description of a join type */
132
 
enum join_type 
133
 
134
 
  JT_UNKNOWN,
135
 
  JT_SYSTEM,
136
 
  JT_CONST,
137
 
  JT_EQ_REF,
138
 
  JT_REF,
139
 
  JT_MAYBE_REF,
140
 
        JT_ALL,
141
 
  JT_RANGE,
142
 
  JT_NEXT,
143
 
  JT_REF_OR_NULL,
144
 
  JT_UNIQUE_SUBQUERY,
145
 
  JT_INDEX_SUBQUERY,
146
 
  JT_INDEX_MERGE
147
 
};
148
 
 
149
 
class SJ_TMP_TABLE;
150
 
 
151
 
typedef enum_nested_loop_state (*Next_select_func)(JOIN *, struct st_join_table *, bool);
152
 
typedef int (*Read_record_func)(struct st_join_table *tab);
153
 
Next_select_func setup_end_select_func(JOIN *join);
154
 
 
155
 
#include "drizzled/join_tab.h"
156
 
 
157
 
enum_nested_loop_state sub_select_cache(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
158
 
enum_nested_loop_state sub_select(JOIN *join,JOIN_TAB *join_tab, bool end_of_records);
159
 
enum_nested_loop_state end_send_group(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
160
 
enum_nested_loop_state end_write_group(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
84
class SemiJoinTable;
 
85
 
 
86
 
 
87
enum_nested_loop_state sub_select_cache(JOIN *join, JoinTable *join_tab, bool end_of_records);
 
88
enum_nested_loop_state sub_select(JOIN *join,JoinTable *join_tab, bool end_of_records);
 
89
enum_nested_loop_state end_send_group(JOIN *join, JoinTable *join_tab, bool end_of_records);
 
90
enum_nested_loop_state end_write_group(JOIN *join, JoinTable *join_tab, bool end_of_records);
161
91
 
162
92
/**
163
93
 * Information about a position of table within a join order. Used in join
178
108
    number the access method will be invoked.
179
109
  */
180
110
  double read_time;
181
 
  JOIN_TAB *table;
 
111
  JoinTable *table;
182
112
 
183
113
  /**
184
114
    NULL  -  'index' or 'range' or 'index_merge' or 'ALL' access is used.
185
115
    Other - [eq_]ref[_or_null] access is used. Pointer to {t.keypart1 = expr}
186
116
  */
187
 
  KEYUSE *key;
 
117
  KeyUse *key;
188
118
 
189
119
  /** If ref-based access is used: bitmap of tables this table depends on  */
190
120
  table_map ref_depend_map;
236
166
    when val IS NULL.
237
167
  */
238
168
  bool null_rejecting;
239
 
  bool *cond_guard; /**< @see KEYUSE::cond_guard */
240
 
  uint32_t sj_pred_no; /**< @see KEYUSE::sj_pred_no */
 
169
  bool *cond_guard; /**< @see KeyUse::cond_guard */
 
170
  uint32_t sj_pred_no; /**< @see KeyUse::sj_pred_no */
241
171
} KEY_FIELD;
242
172
 
243
173
/*****************************************************************************
282
212
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data);
283
213
bool find_field_in_order_list (Field *field, void *data);
284
214
bool find_field_in_item_list (Field *field, void *data);
285
 
bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,ha_rows select_limit, bool no_changes, const key_map *map);
 
215
bool test_if_skip_sort_order(JoinTable *tab,order_st *order,ha_rows select_limit, bool no_changes, const key_map *map);
286
216
order_st *create_distinct_group(Session *session,
287
217
                                Item **ref_pointer_array,
288
218
                                order_st *order_list,
299
229
int do_select(JOIN *join, List<Item> *fields, Table *tmp_table);
300
230
bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
301
231
int create_sort_index(Session *session, JOIN *join, order_st *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by);
302
 
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
303
 
void restore_prev_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
304
 
void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where);
 
232
void advance_sj_state(const table_map remaining_tables, const JoinTable *tab);
 
233
void restore_prev_sj_state(const table_map remaining_tables, const JoinTable *tab);
 
234
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where);
305
235
Item *remove_additional_cond(Item* conds);
306
236
bool setup_sum_funcs(Session *session, Item_sum **func_ptr);
307
237
bool init_sum_functions(Item_sum **func, Item_sum **end);
317
247
int subq_sj_candidate_cmp(Item_in_subselect* const *el1, Item_in_subselect* const *el2);
318
248
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred);
319
249
bool change_group_ref(Session *session, Item_func *expr, order_st *group_list, bool *changed);
320
 
bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
 
250
bool check_interleaving_with_nj(JoinTable *last, JoinTable *next);
321
251
 
322
 
int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
323
 
int join_read_system(JOIN_TAB *tab);
324
 
int join_read_const(JOIN_TAB *tab);
325
 
int join_read_key(JOIN_TAB *tab);
326
 
int join_read_always_key(JOIN_TAB *tab);
327
 
int join_read_last_key(JOIN_TAB *tab);
 
252
int join_read_const_table(JoinTable *tab, POSITION *pos);
 
253
int join_read_system(JoinTable *tab);
 
254
int join_read_const(JoinTable *tab);
 
255
int join_read_key(JoinTable *tab);
 
256
int join_read_always_key(JoinTable *tab);
 
257
int join_read_last_key(JoinTable *tab);
328
258
int join_no_more_records(READ_RECORD *info);
329
259
int join_read_next(READ_RECORD *info);
330
260
int join_read_next_different(READ_RECORD *info);
331
 
int join_init_quick_read_record(JOIN_TAB *tab);
332
 
int test_if_quick_select(JOIN_TAB *tab);
333
 
int join_init_read_record(JOIN_TAB *tab);
334
 
int join_read_first(JOIN_TAB *tab);
 
261
int join_init_quick_read_record(JoinTable *tab);
 
262
int init_read_record_seq(JoinTable *tab);
 
263
int test_if_quick_select(JoinTable *tab);
 
264
int join_init_read_record(JoinTable *tab);
 
265
int join_read_first(JoinTable *tab);
335
266
int join_read_next_same(READ_RECORD *info);
336
267
int join_read_next_same_diff(READ_RECORD *info);
337
 
int join_read_last(JOIN_TAB *tab);
 
268
int join_read_last(JoinTable *tab);
338
269
int join_read_prev_same(READ_RECORD *info);
339
270
int join_read_prev(READ_RECORD *info);
340
 
int join_read_always_key_or_null(JOIN_TAB *tab);
 
271
int join_read_always_key_or_null(JoinTable *tab);
341
272
int join_read_next_same_or_null(READ_RECORD *info);
342
273
 
343
 
void calc_used_field_length(Session *, JOIN_TAB *join_tab);
 
274
void calc_used_field_length(Session *, JoinTable *join_tab);
344
275
StoredKey *get_store_key(Session *session, 
345
 
                         KEYUSE *keyuse,
 
276
                         KeyUse *keyuse,
346
277
                         table_map used_tables,
347
278
                         KEY_PART_INFO *key_part,
348
279
                         unsigned char *key_buff,
349
280
                         uint32_t maybe_null);
350
281
extern "C" int join_tab_cmp(const void* ptr1, const void* ptr2);
351
282
extern "C" int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
352
 
void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok);
 
283
void push_index_cond(JoinTable *tab, uint32_t keyno, bool other_tbls_ok);
353
284
void add_not_null_conds(JOIN *join);
354
285
uint32_t max_part_bit(key_part_map bits);
355
 
COND *add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab);
 
286
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab);
356
287
order_st *create_distinct_group(Session *session,
357
288
                                Item **ref_pointer_array,
358
289
                                order_st *order,
359
290
                                List<Item> &fields,
360
291
                                List<Item> &all_fields,
361
292
                                bool *all_order_by_fields_used);
362
 
bool eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab);
 
293
bool eq_ref_table(JOIN *join, order_st *start_order, JoinTable *tab);
363
294
uint64_t get_bound_sj_equalities(TableList *sj_nest, table_map remaining_tables);
364
295
int join_tab_cmp(const void* ptr1, const void* ptr2);
365
296
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having);
371
302
                               Item *having);
372
303
bool update_ref_and_keys(Session *session,
373
304
                         DYNAMIC_ARRAY *keyuse,
374
 
                         JOIN_TAB *join_tab,
 
305
                         JoinTable *join_tab,
375
306
                         uint32_t tables,
376
307
                         COND *cond, 
377
308
                         COND_EQUAL *,
380
311
                         SARGABLE_PARAM **sargables);
381
312
ha_rows get_quick_record_count(Session *session, SQL_SELECT *select, Table *table, const key_map *keys,ha_rows limit);
382
313
void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
383
 
void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
384
 
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
385
 
void read_cached_record(JOIN_TAB *tab);
 
314
void add_group_and_distinct_keys(JOIN *join, JoinTable *join_tab);
 
315
int do_sj_reset(SemiJoinTable *sj_tbl);
 
316
void read_cached_record(JoinTable *tab);
386
317
// Create list for using with tempory table
387
318
void init_tmptable_sum_functions(Item_sum **func);
388
319
void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
389
320
bool find_eq_ref_candidate(Table *table, table_map sj_inner_tables);
390
321
bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
391
 
bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse, table_map used_tables);
 
322
bool create_ref_for_key(JOIN *join, JoinTable *j, KeyUse *org_keyuse, table_map used_tables);
392
323
 
393
324
/* functions from opt_sum.cc */
394
325
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order);
401
332
 
402
333
bool cp_buffer_from_ref(Session *session, TABLE_REF *ref);
403
334
bool error_if_full_join(JOIN *join);
404
 
int safe_index_read(JOIN_TAB *tab);
 
335
int safe_index_read(JoinTable *tab);
405
336
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value);
406
337
int test_if_item_cache_changed(List<Cached_item> &list);
407
338