~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.h

  • Committer: Brian Aker
  • Date: 2009-08-15 00:59:30 UTC
  • mfrom: (1115.1.7 merge)
  • Revision ID: brian@gaz-20090815005930-q47yenjrq1esiwsz
Merge of Trond + Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include "drizzled/field/varstring.h"
26
26
#include "drizzled/item/null.h"
27
27
#include <drizzled/enum_nested_loop_state.h>
28
 
#include "drizzled/optimizer/position.h"
29
 
#include "drizzled/optimizer/sargable_param.h"
30
 
#include "drizzled/optimizer/key_use.h"
31
28
#include "drizzled/join_cache.h"
32
29
#include "drizzled/join_table.h"
33
30
 
34
 
#include <vector>
35
 
 
36
 
namespace drizzled
37
 
{
38
31
 
39
32
class select_result;
40
33
 
45
38
/* PREV_BITS only used in sql_select.cc */
46
39
#define PREV_BITS(type,A)       ((type) (((type) 1 << (A)) -1))
47
40
 
 
41
#include <plugin/myisam/myisam.h>
 
42
#include <drizzled/sql_array.h>
 
43
 
48
44
/* Values in optimize */
49
45
#define KEY_OPTIMIZE_EXISTS             1
50
46
#define KEY_OPTIMIZE_REF_OR_NULL        2
51
47
 
 
48
class KeyUse 
 
49
{
 
50
public:
 
51
  Table *table; /**< Pointer to the table this key belongs to */
 
52
  Item *val;    /**< or value if no field */
 
53
  table_map used_tables;
 
54
  uint32_t key;
 
55
  uint32_t keypart;
 
56
  uint32_t optimize; /**< 0, or KEY_OPTIMIZE_* */
 
57
  key_part_map keypart_map;
 
58
  ha_rows ref_table_rows;
 
59
  /**
 
60
    If true, the comparison this value was created from will not be
 
61
    satisfied if val has NULL 'value'.
 
62
  */
 
63
  bool null_rejecting;
 
64
  /**
 
65
    !NULL - This KeyUse was created from an equality that was wrapped into
 
66
            an Item_func_trig_cond. This means the equality (and validity of
 
67
            this KeyUse element) can be turned on and off. The on/off state
 
68
            is indicted by the pointed value:
 
69
              *cond_guard == true <=> equality condition is on
 
70
              *cond_guard == false <=> equality condition is off
 
71
 
 
72
    NULL  - Otherwise (the source equality can't be turned off)
 
73
  */
 
74
  bool *cond_guard;
 
75
};
 
76
 
52
77
class JOIN;
53
78
 
54
79
enum_nested_loop_state sub_select_cache(JOIN *join, JoinTable *join_tab, bool end_of_records);
56
81
enum_nested_loop_state end_send_group(JOIN *join, JoinTable *join_tab, bool end_of_records);
57
82
enum_nested_loop_state end_write_group(JOIN *join, JoinTable *join_tab, bool end_of_records);
58
83
 
 
84
/**
 
85
 * Information about a position of table within a join order. Used in join
 
86
 * optimization.
 
87
 */
 
88
class Position
 
89
{
 
90
public:
 
91
 
 
92
  Position()
 
93
    :
 
94
      records_read(0),
 
95
      read_time(0),
 
96
      table(NULL),
 
97
      key(NULL),
 
98
      ref_depend_map(0),
 
99
      use_insideout_scan(false)
 
100
  {}
 
101
 
 
102
  /**
 
103
   * Determine whether the table this particular position is representing in
 
104
   * the query plan is a const table or not. A constant table is defined as
 
105
   * (taken from the MySQL optimizer internals document on MySQL forge):
 
106
   *
 
107
   * 1) A table with zero rows, or with only one row
 
108
   * 2) A table expression that is restricted with a WHERE condition
 
109
   *
 
110
   * Based on the definition above, when records_read is set to 1.0 in the
 
111
   * Position class, it infers that this position in the partial plan
 
112
   * represents a const table.
 
113
   *
 
114
   * @return true if this position represents a const table; false otherwise
 
115
   */
 
116
  bool isConstTable() const
 
117
  {
 
118
    return (records_read < 2.0);
 
119
  }
 
120
 
 
121
  /**
 
122
    The "fanout": number of output rows that will be produced (after
 
123
    pushed down selection condition is applied) per each row combination of
 
124
    previous tables. The value is an in-precise estimate.
 
125
  */
 
126
  double records_read;
 
127
 
 
128
  /**
 
129
    Cost accessing the table in course of the entire complete join execution,
 
130
    i.e. cost of one access method use (e.g. 'range' or 'ref' scan ) times
 
131
    number the access method will be invoked.
 
132
  */
 
133
  double read_time;
 
134
  JoinTable *table;
 
135
 
 
136
  /**
 
137
    NULL  -  'index' or 'range' or 'index_merge' or 'ALL' access is used.
 
138
    Other - [eq_]ref[_or_null] access is used. Pointer to {t.keypart1 = expr}
 
139
  */
 
140
  KeyUse *key;
 
141
 
 
142
  /** If ref-based access is used: bitmap of tables this table depends on  */
 
143
  table_map ref_depend_map;
 
144
 
 
145
  bool use_insideout_scan;
 
146
};
 
147
 
59
148
typedef struct st_rollup
60
149
{
61
150
  enum State { STATE_NONE, STATE_INITED, STATE_READY };
65
154
  List<Item> *fields;
66
155
} ROLLUP;
67
156
 
68
 
} /* namespace drizzled */
69
 
 
70
 
/** @TODO why is this in the middle of the file??? */
71
 
 
72
157
#include "drizzled/join.h"
73
158
 
74
 
namespace drizzled
75
 
{
 
159
typedef struct st_select_check {
 
160
  uint32_t const_ref,reg_ref;
 
161
} SELECT_CHECK;
 
162
 
 
163
/*
 
164
   This structure is used to collect info on potentially sargable
 
165
   predicates in order to check whether they become sargable after
 
166
   reading const tables.
 
167
   We form a bitmap of indexes that can be used for sargable predicates.
 
168
   Only such indexes are involved in range analysis.
 
169
*/
 
170
typedef struct st_sargable_param
 
171
{
 
172
  Field *field;              /* field against which to check sargability */
 
173
  Item **arg_value;          /* values of potential keys for lookups     */
 
174
  uint32_t num_values;           /* number of values in the above array      */
 
175
} SARGABLE_PARAM;
 
176
 
 
177
/**
 
178
 * Structure used when finding key fields
 
179
 */
 
180
typedef struct key_field_t 
 
181
{
 
182
  Field *field;
 
183
  Item *val; /**< May be empty if diff constant */
 
184
  uint32_t level;
 
185
  uint32_t optimize; /**< KEY_OPTIMIZE_* */
 
186
  bool eq_func;
 
187
  /**
 
188
    If true, the condition this struct represents will not be satisfied
 
189
    when val IS NULL.
 
190
  */
 
191
  bool null_rejecting;
 
192
  bool *cond_guard; /**< @see KeyUse::cond_guard */
 
193
} KEY_FIELD;
76
194
 
77
195
/*****************************************************************************
78
196
  Make som simple condition optimization:
144
262
                               List<Item> &res_all_fields,
145
263
                               uint32_t elements,
146
264
                                                 List<Item> &all_fields);
 
265
void select_describe(JOIN *join, bool need_tmp_table,bool need_order, bool distinct, const char *message= NULL);
147
266
bool change_group_ref(Session *session, Item_func *expr, order_st *group_list, bool *changed);
148
267
bool check_interleaving_with_nj(JoinTable *last, JoinTable *next);
149
268
 
150
 
int join_read_const_table(JoinTable *tab, optimizer::Position *pos);
 
269
int join_read_const_table(JoinTable *tab, Position *pos);
151
270
int join_read_system(JoinTable *tab);
152
271
int join_read_const(JoinTable *tab);
153
272
int join_read_key(JoinTable *tab);
171
290
 
172
291
void calc_used_field_length(Session *, JoinTable *join_tab);
173
292
StoredKey *get_store_key(Session *session, 
174
 
                         optimizer::KeyUse *keyuse,
 
293
                         KeyUse *keyuse,
175
294
                         table_map used_tables,
176
295
                         KEY_PART_INFO *key_part,
177
296
                         unsigned char *key_buff,
178
297
                         uint32_t maybe_null);
179
 
int join_tab_cmp(const void* ptr1, const void* ptr2);
180
 
int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
 
298
extern "C" int join_tab_cmp(const void* ptr1, const void* ptr2);
 
299
extern "C" int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
181
300
void push_index_cond(JoinTable *tab, uint32_t keyno, bool other_tbls_ok);
182
301
void add_not_null_conds(JOIN *join);
183
302
uint32_t max_part_bit(key_part_map bits);
189
308
                                List<Item> &all_fields,
190
309
                                bool *all_order_by_fields_used);
191
310
bool eq_ref_table(JOIN *join, order_st *start_order, JoinTable *tab);
 
311
int join_tab_cmp(const void* ptr1, const void* ptr2);
192
312
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having);
193
313
int remove_dup_with_hash_index(Session *session, 
194
314
                               Table *table,
204
324
                         COND_EQUAL *,
205
325
                         table_map normal_tables,
206
326
                         Select_Lex *select_lex,
207
 
                         std::vector<optimizer::SargableParam> &sargables);
208
 
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit);
 
327
                         SARGABLE_PARAM **sargables);
 
328
ha_rows get_quick_record_count(Session *session, SQL_SELECT *select, Table *table, const key_map *keys,ha_rows limit);
209
329
void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
210
330
void add_group_and_distinct_keys(JOIN *join, JoinTable *join_tab);
211
331
void read_cached_record(JoinTable *tab);
212
 
bool mysql_select(Session *session, Item ***rref_pointer_array,
213
 
                  TableList *tables, uint32_t wild_num,  List<Item> &list,
214
 
                  COND *conds, uint32_t og_num, order_st *order, order_st *group,
215
 
                  Item *having, uint64_t select_type,
216
 
                  select_result *result, Select_Lex_Unit *unit,
217
 
                  Select_Lex *select_lex);
218
332
// Create list for using with tempory table
219
333
void init_tmptable_sum_functions(Item_sum **func);
220
334
void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
221
335
bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
222
 
bool create_ref_for_key(JOIN *join, JoinTable *j, 
223
 
                        optimizer::KeyUse *org_keyuse, 
224
 
                        table_map used_tables);
225
 
 
226
 
} /* namespace drizzled */
227
 
 
228
 
/** @TODO why is this in the middle of the file??? */
 
336
bool create_ref_for_key(JOIN *join, JoinTable *j, KeyUse *org_keyuse, table_map used_tables);
 
337
 
 
338
/* functions from opt_sum.cc */
 
339
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order);
 
340
int opt_sum_query(TableList *tables, List<Item> &all_fields,COND *conds);
 
341
 
 
342
/* from sql_delete.cc, used by opt_range.cc */
 
343
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b);
229
344
 
230
345
#include "drizzled/stored_key.h"
231
346
 
232
 
namespace drizzled
233
 
{
234
 
 
235
347
bool cp_buffer_from_ref(Session *session, table_reference_st *ref);
236
348
int safe_index_read(JoinTable *tab);
237
349
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value);
240
352
void print_join(Session *session, String *str,
241
353
                List<TableList> *tables, enum_query_type);
242
354
 
243
 
} /* namespace drizzled */
244
 
 
245
355
#endif /* DRIZZLED_SQL_SELECT_H */