24
* Defines the Join class
24
* Defines the JOIN class
27
27
#ifndef DRIZZLED_JOIN_H
28
28
#define DRIZZLED_JOIN_H
30
#include <drizzled/optimizer/position.h>
31
#include <drizzled/session.h>
32
#include <drizzled/sql_select.h>
40
class Join :public memory::SqlAlloc
42
Join(const Join &rhs); /**< not implemented */
43
Join& operator=(const Join &rhs); /**< not implemented */
46
* Contains a partial query execution plan which is extended during
47
* cost-based optimization.
49
optimizer::Position positions[MAX_TABLES+1];
52
* Contains the optimal query execution plan after cost-based optimization
55
optimizer::Position best_positions[MAX_TABLES+1];
30
class JOIN :public Sql_alloc
32
JOIN(const JOIN &rhs); /**< not implemented */
33
JOIN& operator=(const JOIN &rhs); /**< not implemented */
58
35
JoinTable *join_tab;
59
36
JoinTable **best_ref;
107
84
simple_xxxxx is set if order_st/GROUP BY doesn't include any references
108
to other tables than the first non-constant table in the Join.
85
to other tables than the first non-constant table in the JOIN.
109
86
It's also set if order_st/GROUP BY is empty.
111
88
bool simple_order;
112
89
bool simple_group;
114
91
Is set only in case if we have a GROUP BY clause
115
and no ORDER BY after constant elimination of 'order'.
92
and no order_st BY after constant elimination of 'order'.
118
/** Is set if we have a GROUP BY and we have ORDER BY on a constant. */
95
/** Is set if we have a GROUP BY and we have order_st BY on a constant. */
119
96
bool skip_sort_order;
120
97
bool union_part; /**< this subselect is part of union */
121
98
bool optimized; /**< flag to avoid double optimization in EXPLAIN */
145
122
Session *session;
146
123
List<Item> *fields;
147
List<Item> &fields_list; /**< hold field list passed to select_query */
124
List<Item> &fields_list; /**< hold field list passed to mysql_select */
148
125
List<TableList> *join_list; /**< list of joined tables in reverse order */
149
126
/** unit structure (with global parameters) for this select */
150
127
Select_Lex_Unit *unit;
151
128
/** select that processed */
152
129
Select_Lex *select_lex;
153
optimizer::SqlSelect *select; /**< created in optimization phase */
130
SQL_SELECT *select; /**< created in optimisation phase */
131
Array<Item_in_subselect> sj_subselects;
133
POSITION positions[MAX_TABLES+1];
134
POSITION best_positions[MAX_TABLES+1];
156
137
Bitmap of nested joins embedding the position at the end of the current
157
138
partial join (valid only during join optimizer run).
159
std::bitset<64> cur_embedding_map;
140
nested_join_map cur_embedding_map;
162
* The cost for the final query execution plan chosen after optimization
163
* has completed. The QEP is stored in the best_positions variable.
165
142
double best_read;
166
143
List<Cached_item> group_fields;
167
144
List<Cached_item> group_fields_cache;
222
199
allocations that occur in repetition at execution time will result in
223
200
excessive memory usage.
225
SortField *sortorder; // make_unireg_sortorder()
202
SORT_FIELD *sortorder; // make_unireg_sortorder()
226
203
Table **table_reexec; // make_simple_join()
227
204
JoinTable *join_tab_reexec; // make_simple_join()
228
205
/* end of allocation caching storage */
230
207
/** Constructors */
231
Join(Session *session_arg,
208
JOIN(Session *session_arg,
232
209
List<Item> &fields_arg,
233
210
uint64_t select_options_arg,
234
select_result *result_arg);
211
select_result *result_arg)
224
sort_and_group(false),
228
no_field_update(false),
230
resume_nested_loop(false),
231
no_const_tables(false),
232
select_distinct(false),
233
group_optimized_away(false),
237
skip_sort_order(false),
241
hidden_group_fields(false),
243
found_const_table_map(0),
250
fetch_limit(HA_POS_ERROR),
251
session(session_arg),
252
fields_list(fields_arg),
257
sj_subselects(session_arg->mem_root, 4),
258
exec_tmp_table1(NULL),
259
exec_tmp_table2(NULL),
264
having_history(NULL),
265
select_options(select_options_arg),
267
lock(session_arg->lock),
269
all_fields(fields_arg),
273
ref_pointer_array(NULL),
278
ref_pointer_array_size(0),
279
zero_result_cause(NULL),
282
join_tab_reexec(NULL)
284
select_distinct= test(select_options & SELECT_DISTINCT);
285
if (&fields_list != &fields_arg) /* only copy if not same*/
286
fields_list= fields_arg;
287
memset(&keyuse, 0, sizeof(keyuse));
288
tmp_table_param.init();
289
tmp_table_param.end_write_records= HA_POS_ERROR;
290
rollup.state= ROLLUP::STATE_NONE;
237
294
* This method is currently only used when a subselect EXPLAIN is performed.
239
296
* was previously in the init() method. See the note about the hack in
240
297
* sql_union.cc...
242
void reset(Session *session_arg,
243
List<Item> &fields_arg,
244
uint64_t select_options_arg,
245
select_result *result_arg);
299
inline void reset(Session *session_arg,
300
List<Item> &fields_arg,
301
uint64_t select_options_arg,
302
select_result *result_arg)
315
sort_and_group= false;
319
no_field_update= false;
321
resume_nested_loop= false;
322
no_const_tables= false;
323
select_distinct= false;
324
group_optimized_away= false;
328
skip_sort_order= false;
332
hidden_group_fields= false;
334
found_const_table_map= 0;
341
fetch_limit= HA_POS_ERROR;
342
session= session_arg;
343
fields_list= fields_arg;
348
exec_tmp_table1= NULL;
349
exec_tmp_table2= NULL;
354
having_history= NULL;
355
select_options= select_options_arg;
357
lock= session_arg->lock;
359
all_fields= fields_arg;
363
ref_pointer_array= NULL;
368
ref_pointer_array_size= 0;
369
zero_result_cause= NULL;
372
join_tab_reexec= NULL;
373
select_distinct= test(select_options & SELECT_DISTINCT);
374
if (&fields_list != &fields_arg) /* only copy if not same*/
375
fields_list= fields_arg;
376
memset(&keyuse, 0, sizeof(keyuse));
377
tmp_table_param.init();
378
tmp_table_param.end_write_records= HA_POS_ERROR;
379
rollup.state= ROLLUP::STATE_NONE;
247
382
int prepare(Item ***rref_pointer_array,
248
383
TableList *tables,
249
384
uint32_t wind_num,
255
390
Select_Lex *select,
256
391
Select_Lex_Unit *unit);
306
440
bool change_result(select_result *result);
307
bool is_top_level_join() const;
310
* Copy the partial query plan into the optimal query plan.
312
* @param[in] size the size of the plan which is to be copied
314
void copyPartialPlanIntoOptimalPlan(uint32_t size)
316
memcpy(best_positions, positions,
317
sizeof(optimizer::Position) * size);
320
void cache_const_exprs();
323
* @param[in] index the index of the position to retrieve
324
* @return a reference to the specified position in the optimal
327
optimizer::Position &getPosFromOptimalPlan(uint32_t index)
329
return best_positions[index];
333
* @param[in] index the index of the position to retrieve
334
* @return a reference to the specified position in the partial
337
optimizer::Position &getPosFromPartialPlan(uint32_t index)
339
return positions[index];
343
* @param[in] index the index of the position to set
344
* @param[in] in_pos the value to set the position to
346
void setPosInPartialPlan(uint32_t index, optimizer::Position &in_pos)
348
positions[index]= in_pos;
352
* @return a pointer to the first position in the partial query plan
354
optimizer::Position *getFirstPosInPartialPlan()
360
* @param[in] index the index of the operator to retrieve from the partial
362
* @return a pointer to the position in the partial query plan
364
optimizer::Position *getSpecificPosInPartialPlan(int32_t index)
366
return positions + index;
441
bool is_top_level_join() const
443
return (unit == &session->lex->unit && (unit->fake_select_lex == 0 ||
444
select_lex == unit->fake_select_lex));
371
enum_nested_loop_state evaluate_join_record(Join *join, JoinTable *join_tab, int error);
372
enum_nested_loop_state evaluate_null_complemented_join_record(Join *join, JoinTable *join_tab);
373
enum_nested_loop_state flush_cached_records(Join *join, JoinTable *join_tab, bool skip_last);
374
enum_nested_loop_state end_send(Join *join, JoinTable *join_tab, bool end_of_records);
375
enum_nested_loop_state end_write(Join *join, JoinTable *join_tab, bool end_of_records);
376
enum_nested_loop_state end_update(Join *join, JoinTable *join_tab, bool end_of_records);
377
enum_nested_loop_state end_unique_update(Join *join, JoinTable *join_tab, bool end_of_records);
379
} /* namespace drizzled */
448
enum_nested_loop_state evaluate_join_record(JOIN *join, JoinTable *join_tab, int error);
449
enum_nested_loop_state evaluate_null_complemented_join_record(JOIN *join, JoinTable *join_tab);
450
enum_nested_loop_state flush_cached_records(JOIN *join, JoinTable *join_tab, bool skip_last);
451
enum_nested_loop_state end_send(JOIN *join, JoinTable *join_tab, bool end_of_records);
452
enum_nested_loop_state end_write(JOIN *join, JoinTable *join_tab, bool end_of_records);
453
enum_nested_loop_state end_update(JOIN *join, JoinTable *join_tab, bool end_of_records);
454
enum_nested_loop_state end_unique_update(JOIN *join, JoinTable *join_tab, bool end_of_records);
381
456
#endif /* DRIZZLED_JOIN_H */