drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
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
26
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 <algorithm>
34#include <bitset>
35
36#include <boost/noncopyable.hpp>
37
38namespace drizzled {
39
40class Join : public memory::SqlAlloc, boost::noncopyable
41{
47
53
54public:
55 JoinTable *join_tab;
56 JoinTable **best_ref;
59
60 Table **table;
61 Table **all_tables;
68
69 uint32_t tables;
70 uint32_t outer_tables;
71 uint32_t const_tables;
72 uint32_t send_group_parts;
73
74 bool sort_and_group;
75 bool first_record;
76 bool full_join;
77 bool group;
78 bool no_field_update;
79 bool do_send_rows;
102
103 /*
104 simple_xxxxx is set if order_st/GROUP BY doesn't include any references
105 to other tables than the first non-constant table in the Join.
106 It's also set if order_st/GROUP BY is empty.
107 */
108 bool simple_order;
109 bool simple_group;
119 bool need_tmp;
120 bool hidden_group_fields;
121
122 table_map const_table_map;
123 table_map found_const_table_map;
124 table_map outer_join;
125
126 ha_rows send_records;
127 ha_rows found_records;
128 ha_rows examined_rows;
129 ha_rows row_limit;
130 ha_rows select_limit;
140 ha_rows fetch_limit;
141
142 Session *session;
143 List<Item> *fields;
147 Select_Lex_Unit *unit;
149 Select_Lex *select_lex;
151
156 std::bitset<64> cur_embedding_map;
157
162 double best_read;
163 List<Cached_item> group_fields;
164 List<Cached_item> group_fields_cache;
165 Table *tmp_table;
168 Table *exec_tmp_table2;
169 Item_sum **sum_funcs;
170 Item_sum ***sum_funcs_end;
173 Item_sum ***sum_funcs_end2;
174 Item *having;
177 uint64_t select_options;
178 select_result *result;
179 Tmp_Table_Param tmp_table_param;
180 DrizzleLock *lock;
181
184 DYNAMIC_ARRAY keyuse;
185 Item::cond_result cond_value;
186 Item::cond_result having_value;
190 List<Item> tmp_all_fields2;
191 List<Item> tmp_all_fields3;
194 List<Item> tmp_fields_list2;
195 List<Item> tmp_fields_list3;
196 int error;
197
198 Order *order;
200 COND *conds; // ---"---
203 COND_EQUAL *cond_equal;
208 Item **items1;
209 Item **items2;
210 Item **items3;
211 Item **current_ref_pointer_array;
213 const char *zero_result_cause;
214
215 /*
216 storage for caching buffers allocated during query execution.
217 These buffers allocations need to be cached as the thread memory pool is
218 cleared only at the end of the execution of the whole query and not caching
219 allocations that occur in repetition at execution time will result in
220 excessive memory usage.
221 */
222 SortField *sortorder; // make_unireg_sortorder()
223 Table **table_reexec; // make_simple_join()
224 JoinTable *join_tab_reexec; // make_simple_join()
225 /* end of allocation caching storage */
226
228 Join(Session *session_arg,
229 List<Item> &fields_arg,
230 uint64_t select_options_arg,
231 select_result *result_arg);
232
239 void reset(Session *session_arg,
240 List<Item> &fields_arg,
241 uint64_t select_options_arg,
242 select_result *result_arg);
243
244 int prepare(Item ***rref_pointer_array,
246 uint32_t wind_num,
247 COND *conds,
248 uint32_t og_num,
249 Order *order,
250 Order *group,
251 Item *having,
252 Select_Lex *select,
253 Select_Lex_Unit *unit);
254
255 int optimize();
256 int reinit();
257 void exec();
258 int destroy();
259 void restore_tmp();
260 bool alloc_func_list();
263 List<Item> &send_fields,
264 bool before_group_by,
265 bool recompute= false);
266
267 inline void set_items_ref_array(Item **ptr)
268 {
270 current_ref_pointer_array= ptr;
271 }
272 inline void init_items_ref_array()
273 {
276 current_ref_pointer_array= items0;
277 }
278
279 bool rollup_init();
280 bool rollup_make_fields(List<Item> &all_fields,
281 List<Item> &fields,
282 Item_sum ***func);
283 int rollup_send_data(uint32_t idx);
284 int rollup_write_data(uint32_t idx, Table *table);
285 void remove_subq_pushed_predicates(Item **where);
292 void join_free();
294 void cleanup(bool full);
295 void clear();
296 void save_join_tab();
297 void init_save_join_tab();
298 bool send_row_on_empty_set()
299 {
300 return (do_send_rows && tmp_table_param.sum_func_count != 0 &&
301 !group_list);
302 }
303 bool change_result(select_result *result);
304 bool is_top_level_join() const;
305
312 {
313 std::copy(positions, positions + size, best_positions);
314 }
315
316 void cache_const_exprs();
317
324 {
325 return best_positions[index];
326 }
327
334 {
335 return positions[index];
336 }
337
342 void setPosInPartialPlan(uint32_t index, optimizer::Position &in_pos)
343 {
344 positions[index]= in_pos;
345 }
346
354
361 {
362 return positions + index;
363 }
364
365};
366
367enum_nested_loop_state evaluate_join_record(Join *join, JoinTable *join_tab, int error);
369enum_nested_loop_state flush_cached_records(Join *join, JoinTable *join_tab, bool skip_last);
370enum_nested_loop_state end_send(Join *join, JoinTable *join_tab, bool end_of_records);
371enum_nested_loop_state end_write(Join *join, JoinTable *join_tab, bool end_of_records);
372enum_nested_loop_state end_update(Join *join, JoinTable *join_tab, bool end_of_records);
373enum_nested_loop_state end_unique_update(Join *join, JoinTable *join_tab, bool end_of_records);
374
375} /* namespace drizzled */
optimizer::Position * getSpecificPosInPartialPlan(int32_t index)
Definition join.h:360
Item_sum ** sum_funcs2
Definition join.h:172
bool no_order
Definition join.h:114
std::bitset< 64 > cur_embedding_map
Definition join.h:156
optimizer::Position positions[MAX_TABLES+1]
Definition join.h:46
Table * exec_tmp_table1
Definition join.h:167
bool resume_nested_loop
Definition join.h:84
uint32_t tables
Definition join.h:69
Rollup rollup
Definition join.h:183
ha_rows fetch_limit
Definition join.h:140
const char * zero_result_cause
not 0 if exec must return zero result
Definition join.h:213
JoinTable ** map2table
Definition join.h:57
uint32_t ref_pointer_array_size
size of above in bytes
Definition join.h:212
bool union_part
Definition join.h:117
Order * group_list
Definition join.h:199
List< Item > all_fields
Definition join.h:187
Item ** ref_pointer_array
Definition join.h:205
Item * having_history
Definition join.h:176
Item * tmp_having
Definition join.h:175
void setPosInPartialPlan(uint32_t index, optimizer::Position &in_pos)
Definition join.h:342
List< Item > tmp_fields_list1
Definition join.h:193
void copyPartialPlanIntoOptimalPlan(uint32_t size)
Definition join.h:311
optimizer::Position & getPosFromPartialPlan(uint32_t index)
Definition join.h:333
Select_Lex_Unit * unit
Definition join.h:147
Item * conds_history
Definition join.h:201
optimizer::Position & getPosFromOptimalPlan(uint32_t index)
Definition join.h:323
JoinTable * return_tab
Definition join.h:204
List< Item > & fields_list
Definition join.h:144
optimizer::SqlSelect * select
Definition join.h:150
optimizer::Position * getFirstPosInPartialPlan()
Definition join.h:350
optimizer::Position best_positions[MAX_TABLES+1]
Definition join.h:52
bool no_const_tables
Definition join.h:92
uint32_t outer_tables
Definition join.h:70
TableList * tables_list
Definition join.h:202
List< TableList > * join_list
Definition join.h:145
bool optimized
Definition join.h:118
bool select_distinct
Definition join.h:93
double best_read
Definition join.h:162
List< Item > tmp_all_fields1
Definition join.h:189
Join * tmp_join
Definition join.h:182
bool skip_sort_order
Definition join.h:116
Item ** items0
Definition join.h:207
JoinTable * join_tab_save
Definition join.h:58
bool group_optimized_away
Definition join.h:101
Select_Lex * select_lex
Definition join.h:149
Table * sort_by_table
Definition join.h:67
void restore_tmp()
Definition join.cc:1289
enum_nested_loop_state evaluate_null_complemented_join_record(Join *join, JoinTable *join_tab)
Definition join.cc:2734
int rollup_send_data(uint32_t idx)
Definition join.cc:2458
bool alloc_func_list()
Definition join.cc:2132
void reset(Session *session_arg, List< Item > &fields_arg, uint64_t select_options_arg, select_result *result_arg)
Definition join.cc:233
void init_save_join_tab()
Save the original join layout.
Definition join.cc:1349
Join(Session *session_arg, List< Item > &fields_arg, uint64_t select_options_arg, select_result *result_arg)
Definition join.cc:144
int optimize()
Definition join.cc:610
bool change_result(select_result *result)
Definition join.cc:2555
void exec()
Definition join.cc:1376
int destroy()
Definition join.cc:1860
void cleanup(bool full)
Definition join.cc:2040
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:2610
void join_free()
Definition join.cc:1971
void clear()
Definition join.cc:2532
bool make_sum_func_list(List< Item > &all_fields, List< Item > &send_fields, bool before_group_by, bool recompute=false)
Definition join.cc:2184
int rollup_write_data(uint32_t idx, Table *table)
Definition join.cc:2499
bool rollup_init()
Definition join.cc:2222
bool rollup_make_fields(List< Item > &all_fields, List< Item > &fields, Item_sum ***func)
Definition join.cc:2330
enum_nested_loop_state end_unique_update(Join *join, JoinTable *, bool end_of_records)
Definition join.cc:3067
bool setup_subquery_materialization()
Definition join.cc:1908
enum_nested_loop_state end_update(Join *join, JoinTable *, bool end_of_records)
Definition join.cc:3000
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:333
void cache_const_exprs()
Definition join.cc:2569
TODO: Rename this file - func.h is stupid.