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();
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  {
267  current_ref_pointer_array= ptr;
268  }
269  inline void init_items_ref_array()
270  {
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