20
20
#ifndef DRIZZLED_SQL_SELECT_H
21
21
#define DRIZZLED_SQL_SELECT_H
23
#include <drizzled/cached_item.h>
24
#include <drizzled/field/varstring.h>
25
#include <drizzled/item/null.h>
23
#include "drizzled/cached_item.h"
24
#include "drizzled/session.h"
25
#include "drizzled/field/varstring.h"
26
#include "drizzled/item/null.h"
26
27
#include <drizzled/enum_nested_loop_state.h>
27
28
#include <drizzled/optimizer/position.h>
28
29
#include <drizzled/optimizer/sargable_param.h>
29
#include <drizzled/optimizer/key_use.h>
30
#include <drizzled/join_cache.h>
31
#include <drizzled/join_table.h>
32
#include <drizzled/records.h>
33
#include <drizzled/stored_key.h>
30
#include "drizzled/join_cache.h"
31
#include "drizzled/join_table.h"
41
class Select_Lex_Unit;
42
36
class select_result;
43
class st_dynamic_array;
46
39
* @file API and Classes to use when handling where clause
49
42
/* PREV_BITS only used in sql_select.cc */
50
43
#define PREV_BITS(type,A) ((type) (((type) 1 << (A)) -1))
45
#include <plugin/myisam/myisam.h>
46
#include <drizzled/sql_array.h>
52
48
/* Values in optimize */
53
49
#define KEY_OPTIMIZE_EXISTS 1
54
50
#define KEY_OPTIMIZE_REF_OR_NULL 2
58
enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records);
59
enum_nested_loop_state sub_select(Join *join,JoinTable *join_tab, bool end_of_records);
60
enum_nested_loop_state end_send_group(Join *join, JoinTable *join_tab, bool end_of_records);
61
enum_nested_loop_state end_write_group(Join *join, JoinTable *join_tab, bool end_of_records);
55
Table *table; /**< Pointer to the table this key belongs to */
56
Item *val; /**< or value if no field */
57
table_map used_tables;
60
uint32_t optimize; /**< 0, or KEY_OPTIMIZE_* */
61
key_part_map keypart_map;
62
ha_rows ref_table_rows;
64
If true, the comparison this value was created from will not be
65
satisfied if val has NULL 'value'.
69
!NULL - This KeyUse was created from an equality that was wrapped into
70
an Item_func_trig_cond. This means the equality (and validity of
71
this KeyUse element) can be turned on and off. The on/off state
72
is indicted by the pointed value:
73
*cond_guard == true <=> equality condition is on
74
*cond_guard == false <=> equality condition is off
76
NULL - Otherwise (the source equality can't be turned off)
83
enum_nested_loop_state sub_select_cache(JOIN *join, JoinTable *join_tab, bool end_of_records);
84
enum_nested_loop_state sub_select(JOIN *join,JoinTable *join_tab, bool end_of_records);
85
enum_nested_loop_state end_send_group(JOIN *join, JoinTable *join_tab, bool end_of_records);
86
enum_nested_loop_state end_write_group(JOIN *join, JoinTable *join_tab, bool end_of_records);
88
typedef struct st_rollup
66
90
enum State { STATE_NONE, STATE_INITED, STATE_READY };
72
ref_pointer_arrays(NULL),
76
Rollup(State in_state,
77
Item_null_result **in_null_items,
78
Item ***in_ref_pointer_arrays,
79
List<Item> *in_fields)
82
null_items(in_null_items),
83
ref_pointer_arrays(in_ref_pointer_arrays),
87
State getState() const
92
void setState(State in_state)
97
Item_null_result **getNullItems() const
102
void setNullItems(Item_null_result **in_null_items)
104
null_items= in_null_items;
107
Item ***getRefPointerArrays() const
109
return ref_pointer_arrays;
112
void setRefPointerArrays(Item ***in_ref_pointer_arrays)
114
ref_pointer_arrays= in_ref_pointer_arrays;
117
List<Item> *getFields() const
122
void setFields(List<Item> *in_fields)
129
92
Item_null_result **null_items;
130
93
Item ***ref_pointer_arrays;
131
94
List<Item> *fields;
134
} /* namespace drizzled */
136
/** @TODO why is this in the middle of the file??? */
138
#include <drizzled/join.h>
97
#include "drizzled/join.h"
143
99
/*****************************************************************************
144
100
Make som simple condition optimization:
148
104
item->marker should be 0 for all items on entry
149
105
Return in cond_value false if condition is impossible (1 = 2)
150
106
*****************************************************************************/
151
typedef std::pair<Item*, Item_func*> COND_CMP;
110
COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
153
void TEST_join(Join *join);
113
void TEST_join(JOIN *join);
155
115
/* Extern functions in sql_select.cc */
156
116
bool store_val_in_field(Field *field, Item *val, enum_check_fields check_flag);
157
117
Table *create_tmp_table(Session *session,Tmp_Table_Param *param,List<Item> &fields,
158
Order *group, bool distinct, bool save_sum_fields,
118
order_st *group, bool distinct, bool save_sum_fields,
159
119
uint64_t select_options, ha_rows rows_limit,
160
120
const char* alias);
121
void free_tmp_table(Session *session, Table *entry);
161
122
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param,
162
123
List<Item> &fields, bool reset_with_sum_func);
163
124
bool setup_copy_fields(Session *session, Tmp_Table_Param *param,
165
126
List<Item> &new_list1, List<Item> &new_list2,
166
127
uint32_t elements, List<Item> &fields);
167
128
void copy_fields(Tmp_Table_Param *param);
168
bool copy_funcs(Item **func_ptr, const Session *session);
129
void copy_funcs(Item **func_ptr);
169
130
Field* create_tmp_field_from_field(Session *session, Field* org_field,
170
131
const char *name, Table *table,
171
132
Item_field *item, uint32_t convert_blob_length);
172
133
bool test_if_ref(Item_field *left_item,Item *right_item);
173
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value);
134
COND *optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value);
174
135
COND *make_cond_for_table(COND *cond,table_map table, table_map used_table, bool exclude_expensive_cond);
175
136
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx);
176
137
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data);
177
138
bool find_field_in_order_list (Field *field, void *data);
178
139
bool find_field_in_item_list (Field *field, void *data);
179
bool test_if_skip_sort_order(JoinTable *tab,Order *order,ha_rows select_limit, bool no_changes, const key_map *map);
180
Order *create_distinct_group(Session *session,
140
bool test_if_skip_sort_order(JoinTable *tab,order_st *order,ha_rows select_limit, bool no_changes, const key_map *map);
141
order_st *create_distinct_group(Session *session,
181
142
Item **ref_pointer_array,
143
order_st *order_list,
183
144
List<Item> &fields,
185
146
bool *all_order_by_fields_used);
190
151
List<Item> &res_all_fields,
191
152
uint32_t elements,
192
153
List<Item> &all_fields);
193
int do_select(Join *join, List<Item> *fields, Table *tmp_table);
154
int do_select(JOIN *join, List<Item> *fields, Table *tmp_table);
194
155
bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
195
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by);
156
int create_sort_index(Session *session, JOIN *join, order_st *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by);
196
157
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where);
197
158
Item *remove_additional_cond(Item* conds);
198
159
bool setup_sum_funcs(Session *session, Item_sum **func_ptr);
205
166
List<Item> &res_all_fields,
206
167
uint32_t elements,
207
168
List<Item> &all_fields);
208
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed);
209
bool check_interleaving_with_nj(JoinTable *next);
210
void update_const_equal_items(COND *cond, JoinTable *tab);
169
void select_describe(JOIN *join, bool need_tmp_table,bool need_order, bool distinct, const char *message= NULL);
170
bool change_group_ref(Session *session, Item_func *expr, order_st *group_list, bool *changed);
171
bool check_interleaving_with_nj(JoinTable *last, JoinTable *next);
173
int join_read_const_table(JoinTable *tab, drizzled::optimizer::Position *pos);
174
int join_read_system(JoinTable *tab);
211
175
int join_read_const(JoinTable *tab);
212
176
int join_read_key(JoinTable *tab);
213
177
int join_read_always_key(JoinTable *tab);
214
178
int join_read_last_key(JoinTable *tab);
215
int join_no_more_records(ReadRecord *info);
216
int join_read_next(ReadRecord *info);
217
int join_read_next_different(ReadRecord *info);
179
int join_no_more_records(READ_RECORD *info);
180
int join_read_next(READ_RECORD *info);
181
int join_read_next_different(READ_RECORD *info);
218
182
int join_init_quick_read_record(JoinTable *tab);
219
183
int init_read_record_seq(JoinTable *tab);
220
184
int test_if_quick_select(JoinTable *tab);
221
185
int join_init_read_record(JoinTable *tab);
222
186
int join_read_first(JoinTable *tab);
223
int join_read_next_same(ReadRecord *info);
224
int join_read_next_same_diff(ReadRecord *info);
187
int join_read_next_same(READ_RECORD *info);
188
int join_read_next_same_diff(READ_RECORD *info);
225
189
int join_read_last(JoinTable *tab);
226
int join_read_prev_same(ReadRecord *info);
227
int join_read_prev(ReadRecord *info);
190
int join_read_prev_same(READ_RECORD *info);
191
int join_read_prev(READ_RECORD *info);
228
192
int join_read_always_key_or_null(JoinTable *tab);
229
int join_read_next_same_or_null(ReadRecord *info);
193
int join_read_next_same_or_null(READ_RECORD *info);
231
195
void calc_used_field_length(Session *, JoinTable *join_tab);
232
196
StoredKey *get_store_key(Session *session,
233
optimizer::KeyUse *keyuse,
234
198
table_map used_tables,
235
KeyPartInfo *key_part,
199
KEY_PART_INFO *key_part,
236
200
unsigned char *key_buff,
237
201
uint32_t maybe_null);
238
int join_tab_cmp(const void* ptr1, const void* ptr2);
239
int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
202
extern "C" int join_tab_cmp(const void* ptr1, const void* ptr2);
203
extern "C" int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
240
204
void push_index_cond(JoinTable *tab, uint32_t keyno, bool other_tbls_ok);
241
void add_not_null_conds(Join *join);
205
void add_not_null_conds(JOIN *join);
242
206
uint32_t max_part_bit(key_part_map bits);
243
207
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab);
244
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab);
208
order_st *create_distinct_group(Session *session,
209
Item **ref_pointer_array,
212
List<Item> &all_fields,
213
bool *all_order_by_fields_used);
214
bool eq_ref_table(JOIN *join, order_st *start_order, JoinTable *tab);
215
int join_tab_cmp(const void* ptr1, const void* ptr2);
245
216
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having);
246
217
int remove_dup_with_hash_index(Session *session,
258
229
table_map normal_tables,
259
230
Select_Lex *select_lex,
260
std::vector<optimizer::SargableParam> &sargables);
261
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit);
262
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array);
263
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab);
231
std::vector<drizzled::optimizer::SargableParam> &sargables);
232
ha_rows get_quick_record_count(Session *session, SQL_SELECT *select, Table *table, const key_map *keys,ha_rows limit);
233
void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
234
void add_group_and_distinct_keys(JOIN *join, JoinTable *join_tab);
264
235
void read_cached_record(JoinTable *tab);
265
bool select_query(Session *session, Item ***rref_pointer_array,
266
TableList *tables, uint32_t wild_num, List<Item> &list,
267
COND *conds, uint32_t og_num, Order *order, Order *group,
268
Item *having, uint64_t select_type,
269
select_result *result, Select_Lex_Unit *unit,
270
Select_Lex *select_lex);
271
236
// Create list for using with tempory table
272
237
void init_tmptable_sum_functions(Item_sum **func);
273
238
void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
274
bool only_eq_ref_tables(Join *join, Order *order, table_map tables);
275
bool create_ref_for_key(Join *join, JoinTable *j,
276
optimizer::KeyUse *org_keyuse,
277
table_map used_tables);
239
bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
240
bool create_ref_for_key(JOIN *join, JoinTable *j, KeyUse *org_keyuse, table_map used_tables);
242
/* functions from opt_sum.cc */
243
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order);
244
int opt_sum_query(TableList *tables, List<Item> &all_fields,COND *conds);
246
/* from sql_delete.cc, used by opt_range.cc */
247
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b);
249
#include "drizzled/stored_key.h"
279
251
bool cp_buffer_from_ref(Session *session, table_reference_st *ref);
280
252
int safe_index_read(JoinTable *tab);