1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008-2009 Sun Microsystems
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.
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.
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
24
* Defines the JOIN class
27
#ifndef DRIZZLED_JOIN_H
28
#define DRIZZLED_JOIN_H
30
class JOIN :public Sql_alloc
32
JOIN(const JOIN &rhs); /**< not implemented */
33
JOIN& operator=(const JOIN &rhs); /**< not implemented */
37
JOIN_TAB **map2table; /**< mapping between table indexes and JOIN_TABs */
38
JOIN_TAB *join_tab_save; /**< saved join_tab for subquery reexecution */
43
The table which has an index that allows to produce the requried ordering.
44
A special value of 0x1 means that the ordering will be produced by
45
passing 1st non-const table to filesort(). NULL means no such table exists.
49
uint32_t tables; /**< Number of tables in the join */
50
uint32_t outer_tables; /**< Number of tables that are not inside semijoin */
51
uint32_t const_tables;
52
uint32_t send_group_parts;
61
true when we want to resume nested loop iterations when
62
fetching data from a cursor
64
bool resume_nested_loop;
66
true <=> optimizer must not mark any table as a constant table.
67
This is needed for subqueries in form "a IN (SELECT .. UNION SELECT ..):
68
when we optimize the select that reads the results of the union from a
69
temporary table, we must not mark the temp. table as constant because
70
the number of rows in it may vary from one subquery execution to another.
73
bool select_distinct; /**< Set if SELECT DISTINCT */
75
If we have the GROUP BY statement in the query,
76
but the group_list was emptied by optimizer, this
78
It happens when fields in the GROUP BY are from
81
bool group_optimized_away;
84
simple_xxxxx is set if order_st/GROUP BY doesn't include any references
85
to other tables than the first non-constant table in the JOIN.
86
It's also set if order_st/GROUP BY is empty.
91
Is set only in case if we have a GROUP BY clause
92
and no order_st BY after constant elimination of 'order'.
95
/** Is set if we have a GROUP BY and we have order_st BY on a constant. */
97
bool union_part; /**< this subselect is part of union */
98
bool optimized; /**< flag to avoid double optimization in EXPLAIN */
100
bool hidden_group_fields;
102
table_map const_table_map;
103
table_map found_const_table_map;
104
table_map outer_join;
106
ha_rows send_records;
107
ha_rows found_records;
108
ha_rows examined_rows;
110
ha_rows select_limit;
112
Used to fetch no more than given amount of rows per one
113
fetch operation of server side cursor.
114
The value is checked in end_send and end_send_group in fashion, similar
116
- fetch_limit= HA_POS_ERROR if there is no cursor.
117
- when we open a cursor, we set fetch_limit to 0,
118
- on each fetch iteration we add num_rows to fetch to fetch_limit
124
List<Item> &fields_list; /**< hold field list passed to mysql_select */
125
List<TableList> *join_list; /**< list of joined tables in reverse order */
126
/** unit structure (with global parameters) for this select */
127
Select_Lex_Unit *unit;
128
/** select that processed */
129
Select_Lex *select_lex;
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];
137
Bitmap of nested joins embedding the position at the end of the current
138
partial join (valid only during join optimizer run).
140
nested_join_map cur_embedding_map;
143
List<Cached_item> group_fields;
144
List<Cached_item> group_fields_cache;
146
/** used to store 2 possible tmp table of SELECT */
147
Table *exec_tmp_table1;
148
Table *exec_tmp_table2;
149
Item_sum **sum_funcs;
150
Item_sum ***sum_funcs_end;
151
/** second copy of sumfuncs (for queries with 2 temporary tables */
152
Item_sum **sum_funcs2;
153
Item_sum ***sum_funcs_end2;
155
Item *tmp_having; /**< To store having when processed temporary table */
156
Item *having_history; /**< Store having for explain */
157
uint64_t select_options;
158
select_result *result;
159
Tmp_Table_Param tmp_table_param;
162
JOIN *tmp_join; /**< copy of this JOIN to be used with temporary tables */
163
ROLLUP rollup; /**< Used with rollup */
164
DYNAMIC_ARRAY keyuse;
165
Item::cond_result cond_value;
166
Item::cond_result having_value;
167
List<Item> all_fields; /**< to store all fields that used in query */
168
/** Above list changed to use temporary table */
169
List<Item> tmp_all_fields1;
170
List<Item> tmp_all_fields2;
171
List<Item> tmp_all_fields3;
172
/** Part, shared with list above, emulate following list */
173
List<Item> tmp_fields_list1;
174
List<Item> tmp_fields_list2;
175
List<Item> tmp_fields_list3;
179
order_st *group_list; /**< hold parameters of mysql_select */
180
COND *conds; // ---"---
181
Item *conds_history; /**< store WHERE for explain */
182
TableList *tables_list; /**< hold 'tables' parameter of mysql_select */
183
COND_EQUAL *cond_equal;
184
JOIN_TAB *return_tab; /**< used only for outer joins */
185
Item **ref_pointer_array; /**< used pointer reference for this select */
186
/** Copy of above to be used with different lists */
191
Item **current_ref_pointer_array;
192
uint32_t ref_pointer_array_size; ///< size of above in bytes
193
const char *zero_result_cause; ///< not 0 if exec must return zero result
195
/* Descriptions of temporary tables used to weed-out semi-join duplicates */
196
SJ_TMP_TABLE *sj_tmp_tables;
198
table_map cur_emb_sj_nests;
201
storage for caching buffers allocated during query execution.
202
These buffers allocations need to be cached as the thread memory pool is
203
cleared only at the end of the execution of the whole query and not caching
204
allocations that occur in repetition at execution time will result in
205
excessive memory usage.
207
SORT_FIELD *sortorder; // make_unireg_sortorder()
208
Table **table_reexec; // make_simple_join()
209
JOIN_TAB *join_tab_reexec; // make_simple_join()
210
/* end of allocation caching storage */
213
JOIN(Session *session_arg,
214
List<Item> &fields_arg,
215
uint64_t select_options_arg,
216
select_result *result_arg)
229
sort_and_group(false),
233
no_field_update(false),
235
resume_nested_loop(false),
236
no_const_tables(false),
237
select_distinct(false),
238
group_optimized_away(false),
242
skip_sort_order(false),
246
hidden_group_fields(false),
247
const_table_map(NULL),
248
found_const_table_map(NULL),
255
fetch_limit(HA_POS_ERROR),
256
session(session_arg),
257
fields_list(fields_arg),
262
sj_subselects(session_arg->mem_root, 4),
263
exec_tmp_table1(NULL),
264
exec_tmp_table2(NULL),
269
having_history(NULL),
270
select_options(select_options_arg),
272
lock(session_arg->lock),
274
all_fields(fields_arg),
278
ref_pointer_array(NULL),
283
ref_pointer_array_size(0),
284
zero_result_cause(NULL),
288
join_tab_reexec(NULL)
290
select_distinct= test(select_options & SELECT_DISTINCT);
291
if (&fields_list != &fields_arg) /* only copy if not same*/
292
fields_list= fields_arg;
293
memset(&keyuse, 0, sizeof(keyuse));
294
tmp_table_param.init();
295
tmp_table_param.end_write_records= HA_POS_ERROR;
296
rollup.state= ROLLUP::STATE_NONE;
300
* This method is currently only used when a subselect EXPLAIN is performed.
301
* I pulled out the init() method and have simply reset the values to what
302
* was previously in the init() method. See the note about the hack in
305
inline void reset(Session *session_arg,
306
List<Item> &fields_arg,
307
uint64_t select_options_arg,
308
select_result *result_arg)
321
sort_and_group= false;
325
no_field_update= false;
327
resume_nested_loop= false;
328
no_const_tables= false;
329
select_distinct= false;
330
group_optimized_away= false;
334
skip_sort_order= false;
338
hidden_group_fields= false;
339
const_table_map= NULL;
340
found_const_table_map= NULL;
347
fetch_limit= HA_POS_ERROR;
348
session= session_arg;
349
fields_list= fields_arg;
354
exec_tmp_table1= NULL;
355
exec_tmp_table2= NULL;
360
having_history= NULL;
361
select_options= select_options_arg;
363
lock= session_arg->lock;
365
all_fields= fields_arg;
369
ref_pointer_array= NULL;
374
ref_pointer_array_size= 0;
375
zero_result_cause= NULL;
379
join_tab_reexec= NULL;
380
select_distinct= test(select_options & SELECT_DISTINCT);
381
if (&fields_list != &fields_arg) /* only copy if not same*/
382
fields_list= fields_arg;
383
memset(&keyuse, 0, sizeof(keyuse));
384
tmp_table_param.init();
385
tmp_table_param.end_write_records= HA_POS_ERROR;
386
rollup.state= ROLLUP::STATE_NONE;
389
int prepare(Item ***rref_pointer_array,
398
Select_Lex_Unit *unit);
404
bool alloc_func_list();
405
bool flatten_subqueries();
406
bool setup_subquery_materialization();
407
bool make_sum_func_list(List<Item> &all_fields,
408
List<Item> &send_fields,
409
bool before_group_by,
410
bool recompute= false);
412
inline void set_items_ref_array(Item **ptr)
414
memcpy(ref_pointer_array, ptr, ref_pointer_array_size);
415
current_ref_pointer_array= ptr;
417
inline void init_items_ref_array()
419
items0= ref_pointer_array + all_fields.elements;
420
memcpy(items0, ref_pointer_array, ref_pointer_array_size);
421
current_ref_pointer_array= items0;
425
bool rollup_make_fields(List<Item> &all_fields,
428
int rollup_send_data(uint32_t idx);
429
int rollup_write_data(uint32_t idx, Table *table);
430
void remove_subq_pushed_predicates(Item **where);
432
Release memory and, if possible, the open tables held by this execution
433
plan (and nested plans). It's used to release some tables before
434
the end of execution in order to increase concurrency and reduce
438
/** Cleanup this JOIN, possibly for reuse */
439
void cleanup(bool full);
441
bool save_join_tab();
442
bool init_save_join_tab();
443
bool send_row_on_empty_set()
445
return (do_send_rows && tmp_table_param.sum_func_count != 0 &&
448
bool change_result(select_result *result);
449
bool is_top_level_join() const
451
return (unit == &session->lex->unit && (unit->fake_select_lex == 0 ||
452
select_lex == unit->fake_select_lex));
456
#endif /* DRIZZLED_JOIN_H */