Drizzled Public API Documentation

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 
36 namespace drizzled {
37 
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 
49 enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records);
50 enum_nested_loop_state sub_select(Join *join,JoinTable *join_tab, bool end_of_records);
51 enum_nested_loop_state end_send_group(Join *join, JoinTable *join_tab, bool end_of_records);
52 enum_nested_loop_state end_write_group(Join *join, JoinTable *join_tab, bool end_of_records);
53 
54 class Rollup
55 {
56 public:
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 
118 private:
119  State state;
120  Item_null_result **null_items;
121  Item ***ref_pointer_arrays;
122  List<Item> *fields;
123 };
124 
125 } /* namespace drizzled */
126 
129 #include <drizzled/join.h>
130 
131 namespace 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 *****************************************************************************/
142 typedef std::pair<Item*, Item_func*> COND_CMP;
143 
144 void TEST_join(Join *join);
145 
146 /* Extern functions in sql_select.cc */
147 bool store_val_in_field(Field *field, Item *val, enum_check_fields check_flag);
148 Table *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);
152 void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param,
153  List<Item> &fields, bool reset_with_sum_func);
154 bool 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);
158 void copy_fields(Tmp_Table_Param *param);
159 bool copy_funcs(Item **func_ptr, const Session *session);
160 Field* 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);
163 bool test_if_ref(Item_field *left_item,Item *right_item);
164 COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value);
165 COND *make_cond_for_table(COND *cond,table_map table, table_map used_table, bool exclude_expensive_cond);
166 COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx);
167 bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data);
168 bool find_field_in_order_list (Field *field, void *data);
169 bool find_field_in_item_list (Field *field, void *data);
170 bool test_if_skip_sort_order(JoinTable *tab,Order *order,ha_rows select_limit, bool no_changes, const key_map *map);
171 Order *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
178 bool 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);
184 int do_select(Join *join, List<Item> *fields, Table *tmp_table);
185 bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
186 int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by);
187 void save_index_subquery_explain_info(JoinTable *join_tab, Item* where);
188 Item *remove_additional_cond(Item* conds);
189 bool setup_sum_funcs(Session *session, Item_sum **func_ptr);
190 bool init_sum_functions(Item_sum **func, Item_sum **end);
191 bool update_sum_func(Item_sum **func);
192 void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
193 bool 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);
199 bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed);
200 bool check_interleaving_with_nj(JoinTable *next);
201 void update_const_equal_items(COND *cond, JoinTable *tab);
202 int join_read_const(JoinTable *tab);
203 int join_read_key(JoinTable *tab);
204 int join_read_always_key(JoinTable *tab);
205 int join_read_last_key(JoinTable *tab);
206 int join_no_more_records(ReadRecord *info);
207 int join_read_next(ReadRecord *info);
208 int join_read_next_different(ReadRecord *info);
209 int join_init_quick_read_record(JoinTable *tab);
210 int init_read_record_seq(JoinTable *tab);
211 int test_if_quick_select(JoinTable *tab);
212 int join_init_read_record(JoinTable *tab);
213 int join_read_first(JoinTable *tab);
214 int join_read_next_same(ReadRecord *info);
215 int join_read_next_same_diff(ReadRecord *info);
216 int join_read_last(JoinTable *tab);
217 int join_read_prev_same(ReadRecord *info);
218 int join_read_prev(ReadRecord *info);
219 int join_read_always_key_or_null(JoinTable *tab);
220 int join_read_next_same_or_null(ReadRecord *info);
221 
222 void calc_used_field_length(Session *, JoinTable *join_tab);
223 StoredKey *get_store_key(Session *session,
224  optimizer::KeyUse *keyuse,
225  table_map used_tables,
226  KeyPartInfo *key_part,
227  unsigned char *key_buff,
228  uint32_t maybe_null);
229 int join_tab_cmp(const void* ptr1, const void* ptr2);
230 int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
231 void push_index_cond(JoinTable *tab, uint32_t keyno, bool other_tbls_ok);
232 void add_not_null_conds(Join *join);
233 uint32_t max_part_bit(key_part_map bits);
234 COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab);
235 bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab);
236 int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having);
237 int remove_dup_with_hash_index(Session *session,
238  Table *table,
239  uint32_t field_count,
240  Field **first_field,
241  uint32_t key_length,
242  Item *having);
243 void update_ref_and_keys(Session *session,
244  DYNAMIC_ARRAY *keyuse,
245  JoinTable *join_tab,
246  uint32_t tables,
247  COND *cond,
248  COND_EQUAL *,
249  table_map normal_tables,
250  Select_Lex *select_lex,
251  std::vector<optimizer::SargableParam> &sargables);
252 ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit);
253 void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array);
254 void add_group_and_distinct_keys(Join *join, JoinTable *join_tab);
255 void read_cached_record(JoinTable *tab);
256 bool select_query(Session *session, Item ***rref_pointer_array,
257  TableList *tables, uint32_t wild_num, List<Item> &list,
258  COND *conds, uint32_t og_num, Order *order, Order *group,
259  Item *having, uint64_t select_type,
260  select_result *result, Select_Lex_Unit *unit,
261  Select_Lex *select_lex);
262 // Create list for using with tempory table
263 void init_tmptable_sum_functions(Item_sum **func);
264 void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
265 bool only_eq_ref_tables(Join *join, Order *order, table_map tables);
266 bool create_ref_for_key(Join *join, JoinTable *j,
267  optimizer::KeyUse *org_keyuse,
268  table_map used_tables);
269 
270 bool cp_buffer_from_ref(Session *session, table_reference_st *ref);
271 int safe_index_read(JoinTable *tab);
272 COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value);
273 int test_if_item_cache_changed(List<Cached_item> &list);
274 
275 void print_join(Session *session, String *str, List<TableList> *tables);
276 
277 } /* namespace drizzled */
278 
int join_tab_cmp_straight(const void *ptr1, const void *ptr2)
Definition: sql_select.cc:866
bool check_interleaving_with_nj(JoinTable *next_tab)
Definition: sql_select.cc:2763
Field * create_tmp_field_from_field(Session *session, Field *org_field, const char *name, Table *table, Item_field *item, uint32_t convert_blob_length)
Definition: table.cc:651
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
Definition: sql_select.cc:3060
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
Definition: sql_select.cc:6578
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)
Definition: table.cc:717
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
Definition: sql_select.cc:6555
void calc_used_field_length(Session *, JoinTable *join_tab)
Definition: sql_select.cc:883
void copy_fields(Tmp_Table_Param *param)
Definition: sql_select.cc:6387
int join_read_always_key_or_null(JoinTable *tab)
Definition: sql_select.cc:4021
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)
Definition: sql_select.cc:6510
uint32_t max_part_bit(key_part_map bits)
Definition: sql_select.cc:515
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
Definition: sql_select.cc:975
bool list_contains_unique_index(Table *table, bool(*find_func)(Field *, void *), void *data)
Definition: sql_select.cc:4720
COND * remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
Definition: sql_select.cc:2851
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List< Item > &fields, bool reset_with_sum_func)
Definition: sql_select.cc:6140
int do_select(Join *join, List< Item > *fields, Table *table)
Definition: sql_select.cc:3206
int join_read_const(JoinTable *tab)
Definition: sql_select.cc:3579
Order * create_distinct_group(Session *session, Item **ref_pointer_array, Order *order_list, List< Item > &fields, List< Item > &, bool *all_order_by_fields_used)
Definition: sql_select.cc:6068
COND * substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
Definition: sql_select.cc:2354
int join_read_last_key(JoinTable *tab)
Definition: sql_select.cc:3737
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
Definition: sql_select.cc:3486
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)
Definition: sql_select.cc:368
int remove_dup_with_hash_index(Session *session, Table *table, uint32_t field_count, Field **first_field, uint32_t key_length, Item *having)
Definition: sql_select.cc:5524
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
Definition: sql_select.cc:1337
int join_tab_cmp(const void *ptr1, const void *ptr2)
Definition: sql_select.cc:843
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)
Definition: sql_select.cc:566
bool find_field_in_item_list(Field *field, void *data)
Definition: sql_select.cc:4794
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
Definition: sql_select.cc:6588
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)
Definition: sql_select.cc:6418
bool find_field_in_order_list(Field *field, void *data)
Definition: sql_select.cc:4764
void print_join(Session *session, String *str, List< TableList > *tables)
Definition: sql_select.cc:6787
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
Definition: sql_select.cc:716
void update_const_equal_items(COND *cond, JoinTable *tab)
Definition: sql_select.cc:2442
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
Definition: sql_select.cc:6706
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)
Definition: sql_select.cc:6245
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
Definition: sql_select.cc:4838
bool copy_funcs(Item **func_ptr, const Session *session)
Definition: sql_select.cc:6629
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
Definition: sql_select.cc:772
Item * remove_additional_cond(Item *conds)
Definition: sql_select.cc:2584