Drizzled Public API Documentation

join.h
Go to the documentation of this file.
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; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
27 #pragma once
28 
29 #include <drizzled/dynamic_array.h>
30 #include <drizzled/optimizer/position.h>
31 #include <drizzled/sql_select.h>
32 #include <drizzled/tmp_table_param.h>
33 #include <bitset>
34 
35 namespace drizzled {
36 
37 class Join : public memory::SqlAlloc, boost::noncopyable
38 {
44 
50 
51 public:
52  JoinTable *join_tab;
53  JoinTable **best_ref;
57  Table **table;
58  Table **all_tables;
65 
66  uint32_t tables;
67  uint32_t outer_tables;
68  uint32_t const_tables;
69  uint32_t send_group_parts;
70 
71  bool sort_and_group;
72  bool first_record;
73  bool full_join;
74  bool group;
75  bool no_field_update;
76  bool do_send_rows;
99 
100  /*
101  simple_xxxxx is set if order_st/GROUP BY doesn't include any references
102  to other tables than the first non-constant table in the Join.
103  It's also set if order_st/GROUP BY is empty.
104  */
105  bool simple_order;
106  bool simple_group;
111  bool no_order;
114  bool union_part;
115  bool optimized;
116  bool need_tmp;
117  bool hidden_group_fields;
118 
119  table_map const_table_map;
120  table_map found_const_table_map;
121  table_map outer_join;
122 
123  ha_rows send_records;
124  ha_rows found_records;
125  ha_rows examined_rows;
126  ha_rows row_limit;
127  ha_rows select_limit;
137  ha_rows fetch_limit;
138 
139  Session *session;
140  List<Item> *fields;
144  Select_Lex_Unit *unit;
146  Select_Lex *select_lex;
153  std::bitset<64> cur_embedding_map;
154 
159  double best_read;
160  List<Cached_item> group_fields;
161  List<Cached_item> group_fields_cache;
162  Table *tmp_table;
165  Table *exec_tmp_table2;
166  Item_sum **sum_funcs;
167  Item_sum ***sum_funcs_end;
170  Item_sum ***sum_funcs_end2;
171  Item *having;
174  uint64_t select_options;
175  select_result *result;
176  Tmp_Table_Param tmp_table_param;
177  DrizzleLock *lock;
178 
181  DYNAMIC_ARRAY keyuse;
182  Item::cond_result cond_value;
183  Item::cond_result having_value;
187  List<Item> tmp_all_fields2;
188  List<Item> tmp_all_fields3;
191  List<Item> tmp_fields_list2;
192  List<Item> tmp_fields_list3;
193  int error;
194 
195  Order *order;
197  COND *conds; // ---"---
200  COND_EQUAL *cond_equal;
205  Item **items1;
206  Item **items2;
207  Item **items3;
208  Item **current_ref_pointer_array;
210  const char *zero_result_cause;
211 
212  /*
213  storage for caching buffers allocated during query execution.
214  These buffers allocations need to be cached as the thread memory pool is
215  cleared only at the end of the execution of the whole query and not caching
216  allocations that occur in repetition at execution time will result in
217  excessive memory usage.
218  */
219  SortField *sortorder; // make_unireg_sortorder()
220  Table **table_reexec; // make_simple_join()
221  JoinTable *join_tab_reexec; // make_simple_join()
222  /* end of allocation caching storage */
223 
225  Join(Session *session_arg,
226  List<Item> &fields_arg,
227  uint64_t select_options_arg,
228  select_result *result_arg);
229 
236  void reset(Session *session_arg,
237  List<Item> &fields_arg,
238  uint64_t select_options_arg,
239  select_result *result_arg);
240 
241  int prepare(Item ***rref_pointer_array,
242  TableList *tables,
243  uint32_t wind_num,
244  COND *conds,
245  uint32_t og_num,
246  Order *order,
247  Order *group,
248  Item *having,
249  Select_Lex *select,
250  Select_Lex_Unit *unit);
251 
252  int optimize();
253  int reinit();
254  void exec();
255  int destroy();
256  void restore_tmp();
257  bool alloc_func_list();
259  bool make_sum_func_list(List<Item> &all_fields,
260  List<Item> &send_fields,
261  bool before_group_by,
262  bool recompute= false);
263 
264  inline void set_items_ref_array(Item **ptr)
265  {
266  memcpy(ref_pointer_array, ptr, ref_pointer_array_size);
267  current_ref_pointer_array= ptr;
268  }
269  inline void init_items_ref_array()
270  {
271  items0= ref_pointer_array + all_fields.size();
272  memcpy(items0, ref_pointer_array, ref_pointer_array_size);
273  current_ref_pointer_array= items0;
274  }
275 
276  bool rollup_init();
277  bool rollup_make_fields(List<Item> &all_fields,
278  List<Item> &fields,
279  Item_sum ***func);
280  int rollup_send_data(uint32_t idx);
281  int rollup_write_data(uint32_t idx, Table *table);
282  void remove_subq_pushed_predicates(Item **where);
289  void join_free();
291  void cleanup(bool full);
292  void clear();
293  void save_join_tab();
294  void init_save_join_tab();
295  bool send_row_on_empty_set()
296  {
297  return (do_send_rows && tmp_table_param.sum_func_count != 0 &&
298  !group_list);
299  }
300  bool change_result(select_result *result);
301  bool is_top_level_join() const;
302 
308  void copyPartialPlanIntoOptimalPlan(uint32_t size)
309  {
310  memcpy(best_positions, positions,
311  sizeof(optimizer::Position) * size);
312  }
313 
314  void cache_const_exprs();
315 
322  {
323  return best_positions[index];
324  }
325 
332  {
333  return positions[index];
334  }
335 
340  void setPosInPartialPlan(uint32_t index, optimizer::Position &in_pos)
341  {
342  positions[index]= in_pos;
343  }
344 
349  {
350  return positions;
351  }
352 
359  {
360  return positions + index;
361  }
362 
363 };
364 
365 enum_nested_loop_state evaluate_join_record(Join *join, JoinTable *join_tab, int error);
366 enum_nested_loop_state evaluate_null_complemented_join_record(Join *join, JoinTable *join_tab);
367 enum_nested_loop_state flush_cached_records(Join *join, JoinTable *join_tab, bool skip_last);
368 enum_nested_loop_state end_send(Join *join, JoinTable *join_tab, bool end_of_records);
369 enum_nested_loop_state end_write(Join *join, JoinTable *join_tab, bool end_of_records);
370 enum_nested_loop_state end_update(Join *join, JoinTable *join_tab, bool end_of_records);
371 enum_nested_loop_state end_unique_update(Join *join, JoinTable *join_tab, bool end_of_records);
372 
373 } /* namespace drizzled */
374 
Item ** items0
Definition: join.h:204
enum_nested_loop_state evaluate_join_record(Join *join, JoinTable *join_tab, int error)
Process one record of the nested loop join.
Definition: join.cc:2606
bool no_order
Definition: join.h:111
bool rollup_init()
Definition: join.cc:2218
bool make_sum_func_list(List< Item > &all_fields, List< Item > &send_fields, bool before_group_by, bool recompute=false)
Definition: join.cc:2180
Select_Lex * select_lex
Definition: join.h:146
Join(Session *session_arg, List< Item > &fields_arg, uint64_t select_options_arg, select_result *result_arg)
Definition: join.cc:143
uint32_t outer_tables
Definition: join.h:67
int rollup_send_data(uint32_t idx)
Definition: join.cc:2454
Join * tmp_join
Definition: join.h:179
TODO: Rename this file - func.h is stupid.
void restore_tmp()
Definition: join.cc:1288
bool optimized
Definition: join.h:115
bool resume_nested_loop
Definition: join.h:81
enum_nested_loop_state end_update(Join *join, JoinTable *, bool end_of_records)
Definition: join.cc:2996
List< Item > tmp_all_fields1
Definition: join.h:186
const char * zero_result_cause
not 0 if exec must return zero result
Definition: join.h:210
bool change_result(select_result *result)
Definition: join.cc:2551
int prepare(Item ***rref_pointer_array, TableList *tables, uint32_t wind_num, COND *conds, uint32_t og_num, Order *order, Order *group, Item *having, Select_Lex *select, Select_Lex_Unit *unit)
Definition: join.cc:332
Item ** ref_pointer_array
Definition: join.h:202
bool rollup_make_fields(List< Item > &all_fields, List< Item > &fields, Item_sum ***func)
Definition: join.cc:2326
JoinTable ** map2table
Definition: join.h:54
Item * conds_history
Definition: join.h:198
Table * sort_by_table
Definition: join.h:64
Rollup rollup
Definition: join.h:180
void cleanup(bool full)
Definition: join.cc:2036
uint32_t ref_pointer_array_size
size of above in bytes
Definition: join.h:209
bool select_distinct
Definition: join.h:90
optimizer::Position positions[MAX_TABLES+1]
Definition: join.h:43
Item * having_history
Definition: join.h:173
void join_free()
Definition: join.cc:1967
optimizer::Position * getFirstPosInPartialPlan()
Definition: join.h:348
int destroy()
Definition: join.cc:1856
optimizer::Position best_positions[MAX_TABLES+1]
Definition: join.h:49
bool setup_subquery_materialization()
Definition: join.cc:1904
optimizer::Position & getPosFromPartialPlan(uint32_t index)
Definition: join.h:331
enum_nested_loop_state evaluate_null_complemented_join_record(Join *join, JoinTable *join_tab)
Definition: join.cc:2730
JoinTable * join_tab_save
Definition: join.h:55
double best_read
Definition: join.h:159
List< TableList > * join_list
Definition: join.h:142
JoinTable * return_tab
Definition: join.h:201
void copyPartialPlanIntoOptimalPlan(uint32_t size)
Definition: join.h:308
Item_sum ** sum_funcs2
Definition: join.h:169
Select_Lex_Unit * unit
Definition: join.h:144
void exec()
Definition: join.cc:1372
TableList * tables_list
Definition: join.h:199
int optimize()
Definition: join.cc:609
enum_nested_loop_state end_unique_update(Join *join, JoinTable *, bool end_of_records)
Definition: join.cc:3063
void reset(Session *session_arg, List< Item > &fields_arg, uint64_t select_options_arg, select_result *result_arg)
Definition: join.cc:232
std::bitset< 64 > cur_embedding_map
Definition: join.h:153
optimizer::Position * getSpecificPosInPartialPlan(int32_t index)
Definition: join.h:358
void cache_const_exprs()
Definition: join.cc:2565
uint32_t tables
Definition: join.h:66
List< Item > & fields_list
Definition: join.h:141
optimizer::SqlSelect * select
Definition: join.h:147
bool group_optimized_away
Definition: join.h:98
List< Item > all_fields
Definition: join.h:184
List< Item > tmp_fields_list1
Definition: join.h:190
void setPosInPartialPlan(uint32_t index, optimizer::Position &in_pos)
Definition: join.h:340
bool skip_sort_order
Definition: join.h:113
Table * exec_tmp_table1
Definition: join.h:164
optimizer::Position & getPosFromOptimalPlan(uint32_t index)
Definition: join.h:321
ha_rows fetch_limit
Definition: join.h:137
Item * tmp_having
Definition: join.h:172
void init_save_join_tab()
Save the original join layout.
Definition: join.cc:1345
bool union_part
Definition: join.h:114
Order * group_list
Definition: join.h:196
bool alloc_func_list()
Definition: join.cc:2128
bool no_const_tables
Definition: join.h:89
void clear()
Definition: join.cc:2528
int rollup_write_data(uint32_t idx, Table *table)
Definition: join.cc:2495