drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
sql_select.h
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008-2009 Sun Microsystems, Inc.
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; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#pragma once
21
22#include <drizzled/cached_item.h>
23#include <drizzled/field/varstring.h>
24#include <drizzled/item/null.h>
25#include <drizzled/enum_nested_loop_state.h>
26#include <drizzled/optimizer/position.h>
27#include <drizzled/optimizer/sargable_param.h>
28#include <drizzled/optimizer/key_use.h>
29#include <drizzled/join_cache.h>
30#include <drizzled/join_table.h>
31#include <drizzled/records.h>
32#include <drizzled/stored_key.h>
33
34#include <vector>
35
36namespace drizzled {
37
41
42/* PREV_BITS only used in sql_select.cc */
43#define PREV_BITS(type,A) ((type) (((type) 1 << (A)) -1))
44
45/* Values in optimize */
46#define KEY_OPTIMIZE_EXISTS 1
47#define KEY_OPTIMIZE_REF_OR_NULL 2
48
49enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records);
50enum_nested_loop_state sub_select(Join *join,JoinTable *join_tab, bool end_of_records);
51enum_nested_loop_state end_send_group(Join *join, JoinTable *join_tab, bool end_of_records);
52enum_nested_loop_state end_write_group(Join *join, JoinTable *join_tab, bool end_of_records);
53
54class Rollup
55{
56public:
57 enum State { STATE_NONE, STATE_INITED, STATE_READY };
58
59 Rollup()
60 :
61 state(),
62 null_items(NULL),
63 ref_pointer_arrays(NULL),
64 fields()
65 {}
66
67 Rollup(State in_state,
68 Item_null_result **in_null_items,
69 Item ***in_ref_pointer_arrays,
70 List<Item> *in_fields)
71 :
72 state(in_state),
73 null_items(in_null_items),
74 ref_pointer_arrays(in_ref_pointer_arrays),
75 fields(in_fields)
76 {}
77
78 State getState() const
79 {
80 return state;
81 }
82
83 void setState(State in_state)
84 {
85 state= in_state;
86 }
87
88 Item_null_result **getNullItems() const
89 {
90 return null_items;
91 }
92
93 void setNullItems(Item_null_result **in_null_items)
94 {
95 null_items= in_null_items;
96 }
97
98 Item ***getRefPointerArrays() const
99 {
100 return ref_pointer_arrays;
101 }
102
103 void setRefPointerArrays(Item ***in_ref_pointer_arrays)
104 {
105 ref_pointer_arrays= in_ref_pointer_arrays;
106 }
107
108 List<Item> *getFields() const
109 {
110 return fields;
111 }
112
113 void setFields(List<Item> *in_fields)
114 {
115 fields= in_fields;
116 }
117
118private:
119 State state;
120 Item_null_result **null_items;
121 Item ***ref_pointer_arrays;
122 List<Item> *fields;
123};
124
125} /* namespace drizzled */
126
128
129#include <drizzled/join.h>
130
131namespace drizzled
132{
133
134/*****************************************************************************
135 Make som simple condition optimization:
136 If there is a test 'field = const' change all refs to 'field' to 'const'
137 Remove all dummy tests 'item = item', 'const op const'.
138 Remove all 'item is NULL', when item can never be null!
139 item->marker should be 0 for all items on entry
140 Return in cond_value false if condition is impossible (1 = 2)
141*****************************************************************************/
142typedef std::pair<Item*, Item_func*> COND_CMP;
143
144void TEST_join(Join *join);
145
146/* Extern functions in sql_select.cc */
147bool store_val_in_field(Field *field, Item *val, enum_check_fields check_flag);
148Table *create_tmp_table(Session *session,Tmp_Table_Param *param,List<Item> &fields,
149 Order *group, bool distinct, bool save_sum_fields,
150 uint64_t select_options, ha_rows rows_limit,
151 const char* alias);
152void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param,
153 List<Item> &fields, bool reset_with_sum_func);
154bool setup_copy_fields(Session *session, Tmp_Table_Param *param,
155 Item **ref_pointer_array,
156 List<Item> &new_list1, List<Item> &new_list2,
157 uint32_t elements, List<Item> &fields);
158void copy_fields(Tmp_Table_Param *param);
159bool copy_funcs(Item **func_ptr, const Session *session);
160Field* create_tmp_field_from_field(Session *session, Field* org_field,
161 const char *name, Table *table,
162 Item_field *item, uint32_t convert_blob_length);
163bool test_if_ref(Item_field *left_item,Item *right_item);
164COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value);
165COND *make_cond_for_table(COND *cond,table_map table, table_map used_table, bool exclude_expensive_cond);
166COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx);
167bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data);
168bool find_field_in_order_list (Field *field, void *data);
169bool find_field_in_item_list (Field *field, void *data);
170bool test_if_skip_sort_order(JoinTable *tab,Order *order,ha_rows select_limit, bool no_changes, const key_map *map);
171Order *create_distinct_group(Session *session,
172 Item **ref_pointer_array,
173 Order *order_list,
174 List<Item> &fields,
175 List<Item> &,
176 bool *all_order_by_fields_used);
177// Create list for using with tempory table
178bool change_to_use_tmp_fields(Session *session,
179 Item **ref_pointer_array,
180 List<Item> &res_selected_fields,
181 List<Item> &res_all_fields,
182 uint32_t elements,
183 List<Item> &all_fields);
184int do_select(Join *join, List<Item> *fields, Table *tmp_table);
185bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
186int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by);
187void save_index_subquery_explain_info(JoinTable *join_tab, Item* where);
188Item *remove_additional_cond(Item* conds);
189bool setup_sum_funcs(Session *session, Item_sum **func_ptr);
190bool init_sum_functions(Item_sum **func, Item_sum **end);
191bool update_sum_func(Item_sum **func);
192void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
193bool change_refs_to_tmp_fields(Session *session,
194 Item **ref_pointer_array,
195 List<Item> &res_selected_fields,
196 List<Item> &res_all_fields,
197 uint32_t elements,
198 List<Item> &all_fields);
199bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed);
200bool check_interleaving_with_nj(JoinTable *next);
201void update_const_equal_items(COND *cond, JoinTable *tab);
202int join_read_const(JoinTable *tab);
203int join_read_key(JoinTable *tab);
204int join_read_always_key(JoinTable *tab);
205int join_read_last_key(JoinTable *tab);
206int join_no_more_records(ReadRecord *info);
207int join_read_next(ReadRecord *info);
208int join_read_next_different(ReadRecord *info);
209int join_init_quick_read_record(JoinTable *tab);
210int init_read_record_seq(JoinTable *tab);
211int test_if_quick_select(JoinTable *tab);
212int join_init_read_record(JoinTable *tab);
213int join_read_first(JoinTable *tab);
214int join_read_next_same(ReadRecord *info);
215int join_read_next_same_diff(ReadRecord *info);
216int join_read_last(JoinTable *tab);
217int join_read_prev_same(ReadRecord *info);
218int join_read_prev(ReadRecord *info);
219int join_read_always_key_or_null(JoinTable *tab);
220int join_read_next_same_or_null(ReadRecord *info);
221
222class KeyPartInfo;
223
224void calc_used_field_length(Session *, JoinTable *join_tab);
225StoredKey *get_store_key(Session *session,
226 optimizer::KeyUse *keyuse,
227 table_map used_tables,
228 KeyPartInfo *key_part,
229 unsigned char *key_buff,
230 uint32_t maybe_null);
231int join_tab_cmp(const void* ptr1, const void* ptr2);
232int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
233void push_index_cond(JoinTable *tab, uint32_t keyno, bool other_tbls_ok);
234void add_not_null_conds(Join *join);
235uint32_t max_part_bit(key_part_map bits);
236COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab);
237bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab);
238int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having);
239int remove_dup_with_hash_index(Session *session,
240 Table *table,
241 uint32_t field_count,
242 Field **first_field,
243 uint32_t key_length,
244 Item *having);
245void update_ref_and_keys(Session *session,
246 DYNAMIC_ARRAY *keyuse,
247 JoinTable *join_tab,
248 uint32_t tables,
249 COND *cond,
250 COND_EQUAL *,
251 table_map normal_tables,
252 Select_Lex *select_lex,
253 std::vector<optimizer::SargableParam> &sargables);
254ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit);
255void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array);
256void add_group_and_distinct_keys(Join *join, JoinTable *join_tab);
257void read_cached_record(JoinTable *tab);
258bool select_query(Session *session, Item ***rref_pointer_array,
259 TableList *tables, uint32_t wild_num, List<Item> &list,
260 COND *conds, uint32_t og_num, Order *order, Order *group,
261 Item *having, uint64_t select_type,
262 select_result *result, Select_Lex_Unit *unit,
263 Select_Lex *select_lex);
264// Create list for using with tempory table
265void init_tmptable_sum_functions(Item_sum **func);
266void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
267bool only_eq_ref_tables(Join *join, Order *order, table_map tables);
268bool create_ref_for_key(Join *join, JoinTable *j,
269 optimizer::KeyUse *org_keyuse,
270 table_map used_tables);
271
272bool cp_buffer_from_ref(Session *session, table_reference_st *ref);
273int safe_index_read(JoinTable *tab);
274COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value);
275int test_if_item_cache_changed(List<Cached_item> &list);
276
277void print_join(Session *session, String *str, List<TableList> *tables);
278
279} /* namespace drizzled */
280
bool select_query(Session *session, Item ***rref_pointer_array, TableList *tables, uint32_t wild_num, List< Item > &fields, COND *conds, uint32_t og_num, Order *order, Order *group, Item *having, uint64_t select_options, select_result *result, Select_Lex_Unit *unit, Select_Lex *select_lex)
int join_tab_cmp_straight(const void *ptr1, const void *ptr2)
int join_read_last_key(JoinTable *tab)
bool change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array, List< Item > &res_selected_fields, List< Item > &res_all_fields, uint32_t elements, List< Item > &all_fields)
Order * create_distinct_group(Session *session, Item **ref_pointer_array, Order *order_list, List< Item > &fields, List< Item > &, bool *all_order_by_fields_used)
bool copy_funcs(Item **func_ptr, const Session *session)
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List< Item > &fields, bool reset_with_sum_func)
bool list_contains_unique_index(Table *table, bool(*find_func)(Field *, void *), void *data)
uint32_t max_part_bit(key_part_map bits)
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
void update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse, JoinTable *join_tab, uint32_t tables, COND *cond, COND_EQUAL *, table_map normal_tables, Select_Lex *select_lex, vector< optimizer::SargableParam > &sargables)
bool setup_copy_fields(Session *session, Tmp_Table_Param *param, Item **ref_pointer_array, List< Item > &res_selected_fields, List< Item > &res_all_fields, uint32_t elements, List< Item > &all_fields)
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
COND * add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
int do_select(Join *join, List< Item > *fields, Table *table)
bool change_to_use_tmp_fields(Session *session, Item **ref_pointer_array, List< Item > &res_selected_fields, List< Item > &res_all_fields, uint32_t elements, List< Item > &all_fields)
void update_const_equal_items(COND *cond, JoinTable *tab)
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
int join_tab_cmp(const void *ptr1, const void *ptr2)
int join_read_const(JoinTable *tab)
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
void calc_used_field_length(Session *, JoinTable *join_tab)
bool find_field_in_item_list(Field *field, void *data)
Item * remove_additional_cond(Item *conds)
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
bool find_field_in_order_list(Field *field, void *data)
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
int remove_dup_with_hash_index(Session *session, Table *table, uint32_t field_count, Field **first_field, uint32_t key_length, Item *having)
void copy_fields(Tmp_Table_Param *param)
int join_read_always_key_or_null(JoinTable *tab)
COND * remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
bool check_interleaving_with_nj(JoinTable *next_tab)
COND * substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
void print_join(Session *session, String *str, List< TableList > *tables)
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
TODO: Rename this file - func.h is stupid.
Field * create_tmp_field_from_field(Session *session, Field *org_field, const char *name, Table *table, Item_field *item, uint32_t convert_blob_length)
Table * create_tmp_table(Session *session, Tmp_Table_Param *param, List< Item > &fields, Order *group, bool distinct, bool save_sum_fields, uint64_t select_options, ha_rows rows_limit, const char *alias)