1
by brian
clean slate |
1 |
/* Copyright (C) 2000-2006 MySQL AB |
2 |
||
3 |
This program is free software; you can redistribute it and/or modify |
|
4 |
it under the terms of the GNU General Public License as published by |
|
5 |
the Free Software Foundation; version 2 of the License. |
|
6 |
||
7 |
This program is distributed in the hope that it will be useful, |
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
10 |
GNU General Public License for more details. |
|
11 |
||
12 |
You should have received a copy of the GNU General Public License |
|
13 |
along with this program; if not, write to the Free Software |
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ |
|
15 |
||
16 |
/** |
|
17 |
@file |
|
18 |
||
19 |
@brief |
|
20 |
mysql_select and join optimization |
|
21 |
||
22 |
||
23 |
@defgroup Query_Optimizer Query Optimizer |
|
24 |
@{ |
|
25 |
*/ |
|
26 |
||
27 |
#ifdef USE_PRAGMA_IMPLEMENTATION |
|
28 |
#pragma implementation // gcc: Class implementation |
|
29 |
#endif |
|
30 |
||
31 |
#include "mysql_priv.h" |
|
32 |
#include "sql_select.h" |
|
33 |
||
34 |
#include <m_ctype.h> |
|
35 |
#include <my_bit.h> |
|
36 |
#include <hash.h> |
|
37 |
||
38 |
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref", |
|
39 |
"MAYBE_REF","ALL","range","index", |
|
40 |
"ref_or_null","unique_subquery","index_subquery", |
|
41 |
"index_merge" |
|
42 |
}; |
|
43 |
||
44 |
struct st_sargable_param; |
|
45 |
||
46 |
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array); |
|
47 |
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds, |
|
48 |
DYNAMIC_ARRAY *keyuse); |
|
49 |
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse, |
|
50 |
JOIN_TAB *join_tab, |
|
51 |
uint tables, COND *conds, |
|
52 |
COND_EQUAL *cond_equal, |
|
53 |
table_map table_map, SELECT_LEX *select_lex, |
|
54 |
st_sargable_param **sargables); |
|
55 |
static int sort_keyuse(KEYUSE *a,KEYUSE *b); |
|
56 |
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key); |
|
57 |
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse, |
|
58 |
table_map used_tables); |
|
59 |
static bool choose_plan(JOIN *join,table_map join_tables); |
|
60 |
||
61 |
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd, |
|
62 |
table_map remaining_tables, uint idx, |
|
63 |
double record_count, double read_time); |
|
64 |
static void optimize_straight_join(JOIN *join, table_map join_tables); |
|
65 |
static bool greedy_search(JOIN *join, table_map remaining_tables, |
|
66 |
uint depth, uint prune_level); |
|
67 |
static bool best_extension_by_limited_search(JOIN *join, |
|
68 |
table_map remaining_tables, |
|
69 |
uint idx, double record_count, |
|
70 |
double read_time, uint depth, |
|
71 |
uint prune_level); |
|
72 |
static uint determine_search_depth(JOIN* join); |
|
73 |
static int join_tab_cmp(const void* ptr1, const void* ptr2); |
|
74 |
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2); |
|
75 |
/* |
|
76 |
TODO: 'find_best' is here only temporarily until 'greedy_search' is |
|
77 |
tested and approved. |
|
78 |
*/ |
|
79 |
static bool find_best(JOIN *join,table_map rest_tables,uint index, |
|
80 |
double record_count,double read_time); |
|
81 |
static uint cache_record_length(JOIN *join,uint index); |
|
82 |
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref); |
|
83 |
static bool get_best_combination(JOIN *join); |
|
84 |
static store_key *get_store_key(THD *thd, |
|
85 |
KEYUSE *keyuse, table_map used_tables, |
|
86 |
KEY_PART_INFO *key_part, uchar *key_buff, |
|
87 |
uint maybe_null); |
|
88 |
static bool make_simple_join(JOIN *join,TABLE *tmp_table); |
|
89 |
static void make_outerjoin_info(JOIN *join); |
|
90 |
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item); |
|
151
by Brian Aker
Ulonglong to uint64_t |
91 |
static bool make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after); |
1
by brian
clean slate |
92 |
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables); |
93 |
static void update_depend_map(JOIN *join); |
|
94 |
static void update_depend_map(JOIN *join, ORDER *order); |
|
95 |
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond, |
|
96 |
bool change_list, bool *simple_order); |
|
97 |
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables, |
|
98 |
List<Item> &fields, bool send_row, |
|
151
by Brian Aker
Ulonglong to uint64_t |
99 |
uint64_t select_options, const char *info, |
1
by brian
clean slate |
100 |
Item *having); |
101 |
static COND *build_equal_items(THD *thd, COND *cond, |
|
102 |
COND_EQUAL *inherited, |
|
103 |
List<TABLE_LIST> *join_list, |
|
104 |
COND_EQUAL **cond_equal_ref); |
|
105 |
static COND* substitute_for_best_equal_field(COND *cond, |
|
106 |
COND_EQUAL *cond_equal, |
|
107 |
void *table_join_idx); |
|
108 |
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, |
|
109 |
COND *conds, bool top, bool in_sj); |
|
110 |
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next); |
|
111 |
static void restore_prev_nj_state(JOIN_TAB *last); |
|
112 |
static void reset_nj_counters(List<TABLE_LIST> *join_list); |
|
113 |
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, |
|
114 |
uint first_unused); |
|
115 |
||
116 |
static |
|
117 |
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab); |
|
118 |
static void restore_prev_sj_state(const table_map remaining_tables, |
|
119 |
const JOIN_TAB *tab); |
|
120 |
||
121 |
static COND *optimize_cond(JOIN *join, COND *conds, |
|
122 |
List<TABLE_LIST> *join_list, |
|
123 |
Item::cond_result *cond_value); |
|
124 |
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item); |
|
125 |
static bool open_tmp_table(TABLE *table); |
|
126 |
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, |
|
127 |
MI_COLUMNDEF *start_recinfo, |
|
128 |
MI_COLUMNDEF **recinfo, |
|
151
by Brian Aker
Ulonglong to uint64_t |
129 |
uint64_t options); |
1
by brian
clean slate |
130 |
static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table); |
131 |
||
132 |
static enum_nested_loop_state |
|
133 |
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab, |
|
134 |
int error); |
|
135 |
static enum_nested_loop_state |
|
136 |
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab); |
|
137 |
static enum_nested_loop_state |
|
138 |
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last); |
|
139 |
static enum_nested_loop_state |
|
140 |
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records); |
|
141 |
static enum_nested_loop_state |
|
142 |
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records); |
|
143 |
static enum_nested_loop_state |
|
144 |
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records); |
|
145 |
static enum_nested_loop_state |
|
146 |
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records); |
|
147 |
||
148 |
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos); |
|
149 |
static int join_read_system(JOIN_TAB *tab); |
|
150 |
static int join_read_const(JOIN_TAB *tab); |
|
151 |
static int join_read_key(JOIN_TAB *tab); |
|
152 |
static int join_read_always_key(JOIN_TAB *tab); |
|
153 |
static int join_read_last_key(JOIN_TAB *tab); |
|
154 |
static int join_no_more_records(READ_RECORD *info); |
|
155 |
static int join_read_next(READ_RECORD *info); |
|
156 |
static int join_read_next_different(READ_RECORD *info); |
|
157 |
static int join_init_quick_read_record(JOIN_TAB *tab); |
|
158 |
static int test_if_quick_select(JOIN_TAB *tab); |
|
159 |
static int join_init_read_record(JOIN_TAB *tab); |
|
160 |
static int join_read_first(JOIN_TAB *tab); |
|
161 |
static int join_read_next_same(READ_RECORD *info); |
|
162 |
static int join_read_next_same_diff(READ_RECORD *info); |
|
163 |
static int join_read_last(JOIN_TAB *tab); |
|
164 |
static int join_read_prev_same(READ_RECORD *info); |
|
165 |
static int join_read_prev(READ_RECORD *info); |
|
166 |
int join_read_always_key_or_null(JOIN_TAB *tab); |
|
167 |
int join_read_next_same_or_null(READ_RECORD *info); |
|
168 |
static COND *make_cond_for_table(COND *cond,table_map table, |
|
169 |
table_map used_table, |
|
170 |
bool exclude_expensive_cond); |
|
171 |
static Item* part_of_refkey(TABLE *form,Field *field); |
|
172 |
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order, |
|
173 |
ha_rows select_limit, bool no_changes, |
|
174 |
const key_map *map); |
|
175 |
static bool list_contains_unique_index(TABLE *table, |
|
176 |
bool (*find_func) (Field *, void *), void *data); |
|
177 |
static bool find_field_in_item_list (Field *field, void *data); |
|
178 |
static bool find_field_in_order_list (Field *field, void *data); |
|
179 |
static int create_sort_index(THD *thd, JOIN *join, ORDER *order, |
|
180 |
ha_rows filesort_limit, ha_rows select_limit, |
|
181 |
bool is_order_by); |
|
182 |
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields, |
|
183 |
Item *having); |
|
184 |
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field, |
|
185 |
ulong offset,Item *having); |
|
186 |
static int remove_dup_with_hash_index(THD *thd,TABLE *table, |
|
187 |
uint field_count, Field **first_field, |
|
188 |
||
189 |
ulong key_length,Item *having); |
|
190 |
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count); |
|
191 |
static ulong used_blob_length(CACHE_FIELD **ptr); |
|
192 |
static bool store_record_in_cache(JOIN_CACHE *cache); |
|
193 |
static void reset_cache_read(JOIN_CACHE *cache); |
|
194 |
static void reset_cache_write(JOIN_CACHE *cache); |
|
195 |
static void read_cached_record(JOIN_TAB *tab); |
|
196 |
static bool cmp_buffer_with_ref(JOIN_TAB *tab); |
|
197 |
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array, |
|
198 |
ORDER *order, List<Item> &fields, |
|
199 |
List<Item> &all_fields, |
|
200 |
bool *all_order_by_fields_used); |
|
201 |
static bool test_if_subpart(ORDER *a,ORDER *b); |
|
202 |
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables); |
|
203 |
static void calc_group_buffer(JOIN *join,ORDER *group); |
|
204 |
static bool make_group_fields(JOIN *main_join, JOIN *curr_join); |
|
205 |
static bool alloc_group_fields(JOIN *join,ORDER *group); |
|
206 |
// Create list for using with tempory table |
|
207 |
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array, |
|
208 |
List<Item> &new_list1, |
|
209 |
List<Item> &new_list2, |
|
210 |
uint elements, List<Item> &items); |
|
211 |
// Create list for using with tempory table |
|
212 |
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array, |
|
213 |
List<Item> &new_list1, |
|
214 |
List<Item> &new_list2, |
|
215 |
uint elements, List<Item> &items); |
|
216 |
static void init_tmptable_sum_functions(Item_sum **func); |
|
217 |
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table); |
|
218 |
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end); |
|
219 |
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab); |
|
220 |
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr); |
|
221 |
static bool init_sum_functions(Item_sum **func, Item_sum **end); |
|
222 |
static bool update_sum_func(Item_sum **func); |
|
223 |
void select_describe(JOIN *join, bool need_tmp_table,bool need_order, |
|
224 |
bool distinct, const char *message=NullS); |
|
225 |
static Item *remove_additional_cond(Item* conds); |
|
226 |
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab); |
|
227 |
static bool test_if_ref(Item_field *left_item,Item *right_item); |
|
228 |
static bool replace_where_subcondition(JOIN *join, Item *old_cond, |
|
229 |
Item *new_cond, bool fix_fields); |
|
230 |
||
231 |
/* |
|
232 |
This is used to mark equalities that were made from i-th IN-equality. |
|
233 |
We limit semi-join InsideOut optimization to handling max 64 inequalities, |
|
234 |
The following variable occupies 64 addresses. |
|
235 |
*/ |
|
236 |
const char *subq_sj_cond_name= |
|
237 |
"0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond"; |
|
238 |
||
239 |
static bool bitmap_covers(const table_map x, const table_map y) |
|
240 |
{ |
|
241 |
return !test(y & ~x); |
|
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
This handles SELECT with and without UNION. |
|
246 |
*/ |
|
247 |
||
248 |
bool handle_select(THD *thd, LEX *lex, select_result *result, |
|
249 |
ulong setup_tables_done_option) |
|
250 |
{ |
|
251 |
bool res; |
|
252 |
register SELECT_LEX *select_lex = &lex->select_lex; |
|
253 |
MYSQL_SELECT_START(); |
|
254 |
||
255 |
if (select_lex->master_unit()->is_union() || |
|
256 |
select_lex->master_unit()->fake_select_lex) |
|
257 |
res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option); |
|
258 |
else |
|
259 |
{ |
|
260 |
SELECT_LEX_UNIT *unit= &lex->unit; |
|
261 |
unit->set_limit(unit->global_parameters); |
|
262 |
thd->thd_marker= 0; |
|
263 |
/* |
|
264 |
'options' of mysql_select will be set in JOIN, as far as JOIN for |
|
265 |
every PS/SP execution new, we will not need reset this flag if |
|
266 |
setup_tables_done_option changed for next rexecution |
|
267 |
*/ |
|
268 |
res= mysql_select(thd, &select_lex->ref_pointer_array, |
|
269 |
(TABLE_LIST*) select_lex->table_list.first, |
|
270 |
select_lex->with_wild, select_lex->item_list, |
|
271 |
select_lex->where, |
|
272 |
select_lex->order_list.elements + |
|
273 |
select_lex->group_list.elements, |
|
274 |
(ORDER*) select_lex->order_list.first, |
|
275 |
(ORDER*) select_lex->group_list.first, |
|
276 |
select_lex->having, |
|
277 |
(ORDER*) lex->proc_list.first, |
|
278 |
select_lex->options | thd->options | |
|
279 |
setup_tables_done_option, |
|
280 |
result, unit, select_lex); |
|
281 |
} |
|
282 |
res|= thd->is_error(); |
|
283 |
if (unlikely(res)) |
|
284 |
result->abort(); |
|
285 |
||
286 |
MYSQL_SELECT_END(); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
287 |
return(res); |
1
by brian
clean slate |
288 |
} |
289 |
||
290 |
||
291 |
/* |
|
292 |
Fix fields referenced from inner selects. |
|
293 |
||
294 |
SYNOPSIS |
|
295 |
fix_inner_refs() |
|
296 |
thd Thread handle |
|
297 |
all_fields List of all fields used in select |
|
298 |
select Current select |
|
299 |
ref_pointer_array Array of references to Items used in current select |
|
300 |
||
301 |
DESCRIPTION |
|
302 |
The function serves 3 purposes - adds fields referenced from inner |
|
303 |
selects to the current select list, resolves which class to use |
|
304 |
to access referenced item (Item_ref of Item_direct_ref) and fixes |
|
305 |
references (Item_ref objects) to these fields. |
|
306 |
||
307 |
If a field isn't already in the select list and the ref_pointer_array |
|
308 |
is provided then it is added to the all_fields list and the pointer to |
|
309 |
it is saved in the ref_pointer_array. |
|
310 |
||
311 |
The class to access the outer field is determined by the following rules: |
|
312 |
1. If the outer field isn't used under an aggregate function |
|
313 |
then the Item_ref class should be used. |
|
314 |
2. If the outer field is used under an aggregate function and this |
|
315 |
function is aggregated in the select where the outer field was |
|
316 |
resolved or in some more inner select then the Item_direct_ref |
|
317 |
class should be used. |
|
318 |
The resolution is done here and not at the fix_fields() stage as |
|
319 |
it can be done only after sum functions are fixed and pulled up to |
|
320 |
selects where they are have to be aggregated. |
|
321 |
When the class is chosen it substitutes the original field in the |
|
322 |
Item_outer_ref object. |
|
323 |
||
324 |
After this we proceed with fixing references (Item_outer_ref objects) to |
|
325 |
this field from inner subqueries. |
|
326 |
||
327 |
RETURN |
|
55
by brian
Update for using real bool types. |
328 |
true an error occured |
329 |
false ok |
|
1
by brian
clean slate |
330 |
*/ |
331 |
||
332 |
bool |
|
333 |
fix_inner_refs(THD *thd, List<Item> &all_fields, SELECT_LEX *select, |
|
334 |
Item **ref_pointer_array) |
|
335 |
{ |
|
336 |
Item_outer_ref *ref; |
|
55
by brian
Update for using real bool types. |
337 |
bool res= false; |
338 |
bool direct_ref= false; |
|
1
by brian
clean slate |
339 |
|
340 |
List_iterator<Item_outer_ref> ref_it(select->inner_refs_list); |
|
341 |
while ((ref= ref_it++)) |
|
342 |
{ |
|
343 |
Item *item= ref->outer_ref; |
|
344 |
Item **item_ref= ref->ref; |
|
345 |
Item_ref *new_ref; |
|
346 |
/* |
|
347 |
TODO: this field item already might be present in the select list. |
|
348 |
In this case instead of adding new field item we could use an |
|
349 |
existing one. The change will lead to less operations for copying fields, |
|
350 |
smaller temporary tables and less data passed through filesort. |
|
351 |
*/ |
|
352 |
if (ref_pointer_array && !ref->found_in_select_list) |
|
353 |
{ |
|
354 |
int el= all_fields.elements; |
|
355 |
ref_pointer_array[el]= item; |
|
356 |
/* Add the field item to the select list of the current select. */ |
|
357 |
all_fields.push_front(item); |
|
358 |
/* |
|
359 |
If it's needed reset each Item_ref item that refers this field with |
|
360 |
a new reference taken from ref_pointer_array. |
|
361 |
*/ |
|
362 |
item_ref= ref_pointer_array + el; |
|
363 |
} |
|
364 |
||
365 |
if (ref->in_sum_func) |
|
366 |
{ |
|
367 |
Item_sum *sum_func; |
|
368 |
if (ref->in_sum_func->nest_level > select->nest_level) |
|
55
by brian
Update for using real bool types. |
369 |
direct_ref= true; |
1
by brian
clean slate |
370 |
else |
371 |
{ |
|
372 |
for (sum_func= ref->in_sum_func; sum_func && |
|
373 |
sum_func->aggr_level >= select->nest_level; |
|
374 |
sum_func= sum_func->in_sum_func) |
|
375 |
{ |
|
376 |
if (sum_func->aggr_level == select->nest_level) |
|
377 |
{ |
|
55
by brian
Update for using real bool types. |
378 |
direct_ref= true; |
1
by brian
clean slate |
379 |
break; |
380 |
} |
|
381 |
} |
|
382 |
} |
|
383 |
} |
|
384 |
new_ref= direct_ref ? |
|
385 |
new Item_direct_ref(ref->context, item_ref, ref->table_name, |
|
386 |
ref->field_name, ref->alias_name_used) : |
|
387 |
new Item_ref(ref->context, item_ref, ref->table_name, |
|
388 |
ref->field_name, ref->alias_name_used); |
|
389 |
if (!new_ref) |
|
55
by brian
Update for using real bool types. |
390 |
return true; |
1
by brian
clean slate |
391 |
ref->outer_ref= new_ref; |
392 |
ref->ref= &ref->outer_ref; |
|
393 |
||
394 |
if (!ref->fixed && ref->fix_fields(thd, 0)) |
|
55
by brian
Update for using real bool types. |
395 |
return true; |
1
by brian
clean slate |
396 |
thd->used_tables|= item->used_tables(); |
397 |
} |
|
398 |
return res; |
|
399 |
} |
|
400 |
||
401 |
#define MAGIC_IN_WHERE_TOP_LEVEL 10 |
|
402 |
/** |
|
403 |
Function to setup clauses without sum functions. |
|
404 |
*/ |
|
405 |
inline int setup_without_group(THD *thd, Item **ref_pointer_array, |
|
406 |
TABLE_LIST *tables, |
|
407 |
TABLE_LIST *leaves, |
|
408 |
List<Item> &fields, |
|
409 |
List<Item> &all_fields, |
|
410 |
COND **conds, |
|
411 |
ORDER *order, |
|
412 |
ORDER *group, bool *hidden_group_fields) |
|
413 |
{ |
|
414 |
int res; |
|
415 |
nesting_map save_allow_sum_func=thd->lex->allow_sum_func ; |
|
416 |
||
417 |
thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level); |
|
418 |
res= setup_conds(thd, tables, leaves, conds); |
|
419 |
||
420 |
thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level; |
|
421 |
res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields, |
|
422 |
order); |
|
423 |
thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level); |
|
424 |
res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields, |
|
425 |
group, hidden_group_fields); |
|
426 |
thd->lex->allow_sum_func= save_allow_sum_func; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
427 |
return(res); |
1
by brian
clean slate |
428 |
} |
429 |
||
430 |
/***************************************************************************** |
|
431 |
Check fields, find best join, do the select and output fields. |
|
432 |
mysql_select assumes that all tables are already opened |
|
433 |
*****************************************************************************/ |
|
434 |
||
435 |
/** |
|
436 |
Prepare of whole select (including sub queries in future). |
|
437 |
||
438 |
@todo |
|
439 |
Add check of calculation of GROUP functions and fields: |
|
440 |
SELECT COUNT(*)+table.col1 from table1; |
|
441 |
||
442 |
@retval |
|
443 |
-1 on error |
|
444 |
@retval |
|
445 |
0 on success |
|
446 |
*/ |
|
447 |
int |
|
448 |
JOIN::prepare(Item ***rref_pointer_array, |
|
449 |
TABLE_LIST *tables_init, |
|
450 |
uint wild_num, COND *conds_init, uint og_num, |
|
451 |
ORDER *order_init, ORDER *group_init, |
|
452 |
Item *having_init, |
|
453 |
ORDER *proc_param_init, SELECT_LEX *select_lex_arg, |
|
454 |
SELECT_LEX_UNIT *unit_arg) |
|
455 |
{ |
|
456 |
// to prevent double initialization on EXPLAIN |
|
457 |
if (optimized) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
458 |
return(0); |
1
by brian
clean slate |
459 |
|
460 |
conds= conds_init; |
|
461 |
order= order_init; |
|
462 |
group_list= group_init; |
|
463 |
having= having_init; |
|
464 |
proc_param= proc_param_init; |
|
465 |
tables_list= tables_init; |
|
466 |
select_lex= select_lex_arg; |
|
467 |
select_lex->join= this; |
|
468 |
join_list= &select_lex->top_join_list; |
|
469 |
union_part= unit_arg->is_union(); |
|
470 |
||
471 |
thd->lex->current_select->is_item_list_lookup= 1; |
|
472 |
/* |
|
473 |
If we have already executed SELECT, then it have not sense to prevent |
|
474 |
its table from update (see unique_table()) |
|
475 |
*/ |
|
476 |
if (thd->derived_tables_processing) |
|
55
by brian
Update for using real bool types. |
477 |
select_lex->exclude_from_table_unique_test= true; |
1
by brian
clean slate |
478 |
|
479 |
/* Check that all tables, fields, conds and order are ok */ |
|
480 |
||
481 |
if (!(select_options & OPTION_SETUP_TABLES_DONE) && |
|
482 |
setup_tables_and_check_access(thd, &select_lex->context, join_list, |
|
483 |
tables_list, &select_lex->leaf_tables, |
|
55
by brian
Update for using real bool types. |
484 |
false)) |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
485 |
return(-1); |
1
by brian
clean slate |
486 |
|
487 |
TABLE_LIST *table_ptr; |
|
488 |
for (table_ptr= select_lex->leaf_tables; |
|
489 |
table_ptr; |
|
490 |
table_ptr= table_ptr->next_leaf) |
|
491 |
tables++; |
|
492 |
||
493 |
if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) || |
|
494 |
select_lex->setup_ref_array(thd, og_num) || |
|
495 |
setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ, |
|
496 |
&all_fields, 1) || |
|
497 |
setup_without_group(thd, (*rref_pointer_array), tables_list, |
|
498 |
select_lex->leaf_tables, fields_list, |
|
499 |
all_fields, &conds, order, group_list, |
|
500 |
&hidden_group_fields)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
501 |
return(-1); /* purecov: inspected */ |
1
by brian
clean slate |
502 |
|
503 |
ref_pointer_array= *rref_pointer_array; |
|
504 |
||
505 |
if (having) |
|
506 |
{ |
|
507 |
nesting_map save_allow_sum_func= thd->lex->allow_sum_func; |
|
508 |
thd->where="having clause"; |
|
509 |
thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level; |
|
510 |
select_lex->having_fix_field= 1; |
|
511 |
bool having_fix_rc= (!having->fixed && |
|
512 |
(having->fix_fields(thd, &having) || |
|
513 |
having->check_cols(1))); |
|
514 |
select_lex->having_fix_field= 0; |
|
515 |
if (having_fix_rc || thd->is_error()) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
516 |
return(-1); /* purecov: inspected */ |
1
by brian
clean slate |
517 |
thd->lex->allow_sum_func= save_allow_sum_func; |
518 |
} |
|
519 |
||
520 |
if (!thd->lex->view_prepare_mode) |
|
521 |
{ |
|
522 |
Item_subselect *subselect; |
|
523 |
Item_in_subselect *in_subs= NULL; |
|
524 |
/* |
|
525 |
Are we in a subquery predicate? |
|
526 |
TODO: the block below will be executed for every PS execution without need. |
|
527 |
*/ |
|
528 |
if ((subselect= select_lex->master_unit()->item)) |
|
529 |
{ |
|
530 |
bool do_semijoin= !test(thd->variables.optimizer_switch & |
|
531 |
OPTIMIZER_SWITCH_NO_SEMIJOIN); |
|
532 |
if (subselect->substype() == Item_subselect::IN_SUBS) |
|
533 |
in_subs= (Item_in_subselect*)subselect; |
|
534 |
||
535 |
/* |
|
536 |
Check if we're in subquery that is a candidate for flattening into a |
|
537 |
semi-join (which is done done in flatten_subqueries()). The |
|
538 |
requirements are: |
|
539 |
1. Subquery predicate is an IN/=ANY subq predicate |
|
540 |
2. Subquery is a single SELECT (not a UNION) |
|
541 |
3. Subquery does not have GROUP BY or ORDER BY |
|
542 |
4. Subquery does not use aggregate functions or HAVING |
|
543 |
5. Subquery predicate is at the AND-top-level of ON/WHERE clause |
|
544 |
6. No execution method was already chosen (by a prepared statement). |
|
545 |
||
546 |
(*). We are not in a subquery of a single table UPDATE/DELETE that |
|
547 |
doesn't have a JOIN (TODO: We should handle this at some |
|
548 |
point by switching to multi-table UPDATE/DELETE) |
|
549 |
||
550 |
(**). We're not in a confluent table-less subquery, like |
|
551 |
"SELECT 1". |
|
552 |
*/ |
|
553 |
if (in_subs && // 1 |
|
554 |
!select_lex->master_unit()->first_select()->next_select() && // 2 |
|
555 |
!select_lex->group_list.elements && !order && // 3 |
|
556 |
!having && !select_lex->with_sum_func && // 4 |
|
557 |
thd->thd_marker && // 5 |
|
558 |
select_lex->outer_select()->join && // (*) |
|
559 |
select_lex->master_unit()->first_select()->leaf_tables && // (**) |
|
560 |
do_semijoin && |
|
561 |
in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6 |
|
562 |
{ |
|
563 |
{ |
|
564 |
if (!in_subs->left_expr->fixed && |
|
565 |
in_subs->left_expr->fix_fields(thd, &in_subs->left_expr)) |
|
566 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
567 |
return(-1); |
1
by brian
clean slate |
568 |
} |
569 |
/* |
|
570 |
Check that the right part of the subselect contains no more than one |
|
571 |
column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...) |
|
572 |
*/ |
|
573 |
if (subselect->substype() == Item_subselect::IN_SUBS && |
|
574 |
(select_lex->item_list.elements != |
|
575 |
((Item_in_subselect*)subselect)->left_expr->cols())) |
|
576 |
{ |
|
577 |
my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols()); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
578 |
return(-1); |
1
by brian
clean slate |
579 |
} |
580 |
} |
|
581 |
||
582 |
/* Register the subquery for further processing */ |
|
583 |
select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs); |
|
584 |
in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker; |
|
585 |
} |
|
586 |
else |
|
587 |
{ |
|
588 |
bool do_materialize= !test(thd->variables.optimizer_switch & |
|
589 |
OPTIMIZER_SWITCH_NO_MATERIALIZATION); |
|
590 |
/* |
|
591 |
Check if the subquery predicate can be executed via materialization. |
|
592 |
The required conditions are: |
|
593 |
1. Subquery predicate is an IN/=ANY subq predicate |
|
594 |
2. Subquery is a single SELECT (not a UNION) |
|
595 |
3. Subquery is not a table-less query. In this case there is no |
|
596 |
point in materializing. |
|
597 |
4. Subquery predicate is a top-level predicate |
|
598 |
(this implies it is not negated) |
|
599 |
TODO: this is a limitation that should be lifeted once we |
|
600 |
implement correct NULL semantics (WL#3830) |
|
601 |
5. Subquery is non-correlated |
|
602 |
TODO: |
|
603 |
This is an overly restrictive condition. It can be extended to: |
|
604 |
(Subquery is non-correlated || |
|
605 |
Subquery is correlated to any query outer to IN predicate || |
|
606 |
(Subquery is correlated to the immediate outer query && |
|
607 |
Subquery !contains {GROUP BY, ORDER BY [LIMIT], |
|
608 |
aggregate functions) && subquery predicate is not under "NOT IN")) |
|
609 |
6. No execution method was already chosen (by a prepared statement). |
|
610 |
||
611 |
(*) The subquery must be part of a SELECT statement. The current |
|
612 |
condition also excludes multi-table update statements. |
|
613 |
||
614 |
We have to determine whether we will perform subquery materialization |
|
615 |
before calling the IN=>EXISTS transformation, so that we know whether to |
|
616 |
perform the whole transformation or only that part of it which wraps |
|
617 |
Item_in_subselect in an Item_in_optimizer. |
|
618 |
*/ |
|
619 |
if (do_materialize && |
|
620 |
in_subs && // 1 |
|
621 |
!select_lex->master_unit()->first_select()->next_select() && // 2 |
|
622 |
select_lex->master_unit()->first_select()->leaf_tables && // 3 |
|
623 |
thd->lex->sql_command == SQLCOM_SELECT) // * |
|
624 |
{ |
|
625 |
if (in_subs->is_top_level_item() && // 4 |
|
626 |
!in_subs->is_correlated && // 5 |
|
627 |
in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6 |
|
628 |
in_subs->exec_method= Item_in_subselect::MATERIALIZATION; |
|
629 |
} |
|
630 |
||
631 |
Item_subselect::trans_res trans_res; |
|
632 |
if ((trans_res= subselect->select_transformer(this)) != |
|
633 |
Item_subselect::RES_OK) |
|
634 |
{ |
|
635 |
select_lex->fix_prepare_information(thd, &conds, &having); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
636 |
return((trans_res == Item_subselect::RES_ERROR)); |
1
by brian
clean slate |
637 |
} |
638 |
} |
|
639 |
} |
|
640 |
} |
|
641 |
||
642 |
select_lex->fix_prepare_information(thd, &conds, &having); |
|
643 |
||
644 |
if (order) |
|
645 |
{ |
|
646 |
ORDER *ord; |
|
647 |
for (ord= order; ord; ord= ord->next) |
|
648 |
{ |
|
649 |
Item *item= *ord->item; |
|
650 |
if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM) |
|
651 |
item->split_sum_func(thd, ref_pointer_array, all_fields); |
|
652 |
} |
|
653 |
} |
|
654 |
||
655 |
if (having && having->with_sum_func) |
|
656 |
having->split_sum_func2(thd, ref_pointer_array, all_fields, |
|
55
by brian
Update for using real bool types. |
657 |
&having, true); |
1
by brian
clean slate |
658 |
if (select_lex->inner_sum_func_list) |
659 |
{ |
|
660 |
Item_sum *end=select_lex->inner_sum_func_list; |
|
661 |
Item_sum *item_sum= end; |
|
662 |
do |
|
663 |
{ |
|
664 |
item_sum= item_sum->next; |
|
665 |
item_sum->split_sum_func2(thd, ref_pointer_array, |
|
55
by brian
Update for using real bool types. |
666 |
all_fields, item_sum->ref_by, false); |
1
by brian
clean slate |
667 |
} while (item_sum != end); |
668 |
} |
|
669 |
||
670 |
if (select_lex->inner_refs_list.elements && |
|
671 |
fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
672 |
return(-1); |
1
by brian
clean slate |
673 |
|
674 |
/* |
|
675 |
Check if there are references to un-aggregated columns when computing |
|
676 |
aggregate functions with implicit grouping (there is no GROUP BY). |
|
677 |
||
678 |
MODE_ONLY_FULL_GROUP_BY is enabled here by default |
|
679 |
*/ |
|
680 |
if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED)) |
|
681 |
{ |
|
682 |
my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS, |
|
683 |
ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0)); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
684 |
return(-1); |
1
by brian
clean slate |
685 |
} |
686 |
{ |
|
687 |
/* Caclulate the number of groups */ |
|
688 |
send_group_parts= 0; |
|
689 |
for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next) |
|
690 |
send_group_parts++; |
|
691 |
} |
|
692 |
||
693 |
if (error) |
|
694 |
goto err; /* purecov: inspected */ |
|
695 |
||
696 |
if (result && result->prepare(fields_list, unit_arg)) |
|
697 |
goto err; /* purecov: inspected */ |
|
698 |
||
699 |
/* Init join struct */ |
|
700 |
count_field_types(select_lex, &tmp_table_param, all_fields, 0); |
|
701 |
ref_pointer_array_size= all_fields.elements*sizeof(Item*); |
|
702 |
this->group= group_list != 0; |
|
703 |
unit= unit_arg; |
|
704 |
||
705 |
#ifdef RESTRICTED_GROUP |
|
706 |
if (sum_func_count && !group_list && (func_count || field_count)) |
|
707 |
{ |
|
708 |
my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0)); |
|
709 |
goto err; |
|
710 |
} |
|
711 |
#endif |
|
712 |
if (select_lex->olap == ROLLUP_TYPE && rollup_init()) |
|
713 |
goto err; |
|
714 |
if (alloc_func_list()) |
|
715 |
goto err; |
|
716 |
||
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
717 |
return(0); // All OK |
1
by brian
clean slate |
718 |
|
719 |
err: |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
720 |
return(-1); /* purecov: inspected */ |
1
by brian
clean slate |
721 |
} |
722 |
||
723 |
||
724 |
/* |
|
725 |
Remove the predicates pushed down into the subquery |
|
726 |
||
727 |
SYNOPSIS |
|
728 |
JOIN::remove_subq_pushed_predicates() |
|
729 |
where IN Must be NULL |
|
730 |
OUT The remaining WHERE condition, or NULL |
|
731 |
||
732 |
DESCRIPTION |
|
733 |
Given that this join will be executed using (unique|index)_subquery, |
|
734 |
without "checking NULL", remove the predicates that were pushed down |
|
735 |
into the subquery. |
|
736 |
||
737 |
If the subquery compares scalar values, we can remove the condition that |
|
738 |
was wrapped into trig_cond (it will be checked when needed by the subquery |
|
739 |
engine) |
|
740 |
||
741 |
If the subquery compares row values, we need to keep the wrapped |
|
742 |
equalities in the WHERE clause: when the left (outer) tuple has both NULL |
|
743 |
and non-NULL values, we'll do a full table scan and will rely on the |
|
744 |
equalities corresponding to non-NULL parts of left tuple to filter out |
|
745 |
non-matching records. |
|
746 |
||
747 |
TODO: We can remove the equalities that will be guaranteed to be true by the |
|
748 |
fact that subquery engine will be using index lookup. This must be done only |
|
749 |
for cases where there are no conversion errors of significance, e.g. 257 |
|
750 |
that is searched in a byte. But this requires homogenization of the return |
|
751 |
codes of all Field*::store() methods. |
|
752 |
*/ |
|
753 |
||
754 |
void JOIN::remove_subq_pushed_predicates(Item **where) |
|
755 |
{ |
|
756 |
if (conds->type() == Item::FUNC_ITEM && |
|
757 |
((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC && |
|
758 |
((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM && |
|
759 |
((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM && |
|
760 |
test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1], |
|
761 |
((Item_func *)conds)->arguments()[0])) |
|
762 |
{ |
|
763 |
*where= 0; |
|
764 |
return; |
|
765 |
} |
|
766 |
} |
|
767 |
||
768 |
||
769 |
/* |
|
770 |
Index lookup-based subquery: save some flags for EXPLAIN output |
|
771 |
||
772 |
SYNOPSIS |
|
773 |
save_index_subquery_explain_info() |
|
774 |
join_tab Subquery's join tab (there is only one as index lookup is |
|
775 |
only used for subqueries that are single-table SELECTs) |
|
776 |
where Subquery's WHERE clause |
|
777 |
||
778 |
DESCRIPTION |
|
779 |
For index lookup-based subquery (i.e. one executed with |
|
780 |
subselect_uniquesubquery_engine or subselect_indexsubquery_engine), |
|
781 |
check its EXPLAIN output row should contain |
|
782 |
"Using index" (TAB_INFO_FULL_SCAN_ON_NULL) |
|
783 |
"Using Where" (TAB_INFO_USING_WHERE) |
|
784 |
"Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL) |
|
785 |
and set appropriate flags in join_tab->packed_info. |
|
786 |
*/ |
|
787 |
||
788 |
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where) |
|
789 |
{ |
|
790 |
join_tab->packed_info= TAB_INFO_HAVE_VALUE; |
|
791 |
if (join_tab->table->covering_keys.is_set(join_tab->ref.key)) |
|
792 |
join_tab->packed_info |= TAB_INFO_USING_INDEX; |
|
793 |
if (where) |
|
794 |
join_tab->packed_info |= TAB_INFO_USING_WHERE; |
|
795 |
for (uint i = 0; i < join_tab->ref.key_parts; i++) |
|
796 |
{ |
|
797 |
if (join_tab->ref.cond_guards[i]) |
|
798 |
{ |
|
799 |
join_tab->packed_info |= TAB_INFO_FULL_SCAN_ON_NULL; |
|
800 |
break; |
|
801 |
} |
|
802 |
} |
|
803 |
} |
|
804 |
||
805 |
||
806 |
||
807 |
||
808 |
/* |
|
809 |
Check if the table's rowid is included in the temptable |
|
810 |
||
811 |
SYNOPSIS |
|
812 |
sj_table_is_included() |
|
813 |
join The join |
|
814 |
join_tab The table to be checked |
|
815 |
||
816 |
DESCRIPTION |
|
817 |
SemiJoinDuplicateElimination: check the table's rowid should be included |
|
818 |
in the temptable. This is so if |
|
819 |
||
820 |
1. The table is not embedded within some semi-join nest |
|
821 |
2. The has been pulled out of a semi-join nest, or |
|
822 |
||
823 |
3. The table is functionally dependent on some previous table |
|
824 |
||
825 |
[4. This is also true for constant tables that can't be |
|
826 |
NULL-complemented but this function is not called for such tables] |
|
827 |
||
828 |
RETURN |
|
55
by brian
Update for using real bool types. |
829 |
true - Include table's rowid |
830 |
false - Don't |
|
1
by brian
clean slate |
831 |
*/ |
832 |
||
833 |
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab) |
|
834 |
{ |
|
835 |
if (join_tab->emb_sj_nest) |
|
55
by brian
Update for using real bool types. |
836 |
return false; |
1
by brian
clean slate |
837 |
|
838 |
/* Check if this table is functionally dependent on the tables that |
|
839 |
are within the same outer join nest |
|
840 |
*/ |
|
841 |
TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding; |
|
842 |
if (join_tab->type == JT_EQ_REF) |
|
843 |
{ |
|
844 |
Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS); |
|
845 |
uint idx; |
|
846 |
while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END) |
|
847 |
{ |
|
848 |
JOIN_TAB *ref_tab= join->join_tab + idx; |
|
849 |
if (embedding == ref_tab->table->pos_in_table_list->embedding) |
|
55
by brian
Update for using real bool types. |
850 |
return true; |
1
by brian
clean slate |
851 |
} |
852 |
/* Ok, functionally dependent */ |
|
55
by brian
Update for using real bool types. |
853 |
return false; |
1
by brian
clean slate |
854 |
} |
855 |
/* Not functionally dependent => need to include*/ |
|
55
by brian
Update for using real bool types. |
856 |
return true; |
1
by brian
clean slate |
857 |
} |
858 |
||
859 |
||
860 |
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg, |
|
861 |
SJ_TMP_TABLE *sjtbl); |
|
862 |
||
863 |
||
864 |
/* |
|
865 |
Setup the strategies to eliminate semi-join duplicates. |
|
866 |
||
867 |
SYNOPSIS |
|
868 |
setup_semijoin_dups_elimination() |
|
869 |
join Join to process |
|
870 |
options Join options (needed to see if join buffering will be |
|
871 |
used or not) |
|
872 |
no_jbuf_after Another bit of information re where join buffering will |
|
873 |
be used. |
|
874 |
||
875 |
DESCRIPTION |
|
876 |
Setup the strategies to eliminate semi-join duplicates. ATM there are 3 |
|
877 |
strategies: |
|
878 |
||
879 |
1. DuplicateWeedout (use of temptable to remove duplicates based on rowids |
|
880 |
of row combinations) |
|
881 |
2. FirstMatch (pick only the 1st matching row combination of inner tables) |
|
882 |
3. InsideOut (scanning the sj-inner table in a way that groups duplicates |
|
883 |
together and picking the 1st one) |
|
884 |
||
885 |
The join order has "duplicate-generating ranges", and every range is |
|
886 |
served by one strategy or a combination of FirstMatch with with some |
|
887 |
other strategy. |
|
888 |
||
889 |
"Duplicate-generating range" is defined as a range within the join order |
|
890 |
that contains all of the inner tables of a semi-join. All ranges must be |
|
891 |
disjoint, if tables of several semi-joins are interleaved, then the ranges |
|
892 |
are joined together, which is equivalent to converting |
|
893 |
SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 ) |
|
894 |
to |
|
895 |
SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...) |
|
896 |
. |
|
897 |
||
898 |
Applicability conditions are as follows: |
|
899 |
||
900 |
DuplicateWeedout strategy |
|
901 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
902 |
||
903 |
(ot|nt)* [ it ((it|ot|nt)* (it|ot))] (nt)* |
|
904 |
+------+ +=========================+ +---+ |
|
905 |
(1) (2) (3) |
|
906 |
||
907 |
(1) - Prefix of OuterTables (those that participate in |
|
908 |
IN-equality and/or are correlated with subquery) and outer |
|
909 |
Noncorrelated Tables. |
|
910 |
(2) - The handled range. The range starts with the first sj-inner |
|
911 |
table, and covers all sj-inner and outer tables |
|
912 |
Within the range, Inner, Outer, outer Noncorrelated tables |
|
913 |
may follow in any order. |
|
914 |
(3) - The suffix of outer Noncorrelated tables. |
|
915 |
||
916 |
FirstMatch strategy |
|
917 |
~~~~~~~~~~~~~~~~~~~ |
|
918 |
||
919 |
(ot|nt)* [ it ((it|nt)* it) ] (nt)* |
|
920 |
+------+ +==================+ +---+ |
|
921 |
(1) (2) (3) |
|
922 |
||
923 |
(1) - Prefix of outer and non-correlated tables |
|
924 |
(2) - The handled range, which may contain only inner and |
|
925 |
non-correlated tables. |
|
926 |
(3) - The suffix of outer Noncorrelated tables. |
|
927 |
||
928 |
InsideOut strategy |
|
929 |
~~~~~~~~~~~~~~~~~~ |
|
930 |
||
931 |
(ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ] (ot|nt)* |
|
932 |
+--------+ +===========+ +=============+ +------+ |
|
933 |
(1) (2) (3) (4) |
|
934 |
||
935 |
(1) - Prefix that may contain any outer tables. The prefix must contain |
|
936 |
all the non-trivially correlated outer tables. (non-trivially means |
|
937 |
that the correlation is not just through the IN-equality). |
|
938 |
||
939 |
(2) - Inner table for which the InsideOut scan is performed. |
|
940 |
||
941 |
(3) - The remainder of the duplicate-generating range. It is served by |
|
942 |
application of FirstMatch strategy, with the exception that |
|
943 |
outer IN-correlated tables are considered to be non-correlated. |
|
944 |
||
945 |
(4) - THe suffix of outer and outer non-correlated tables. |
|
946 |
||
947 |
If several strategies are applicable, their relative priorities are: |
|
948 |
1. InsideOut |
|
949 |
2. FirstMatch |
|
950 |
3. DuplicateWeedout |
|
951 |
||
952 |
This function walks over the join order and sets up the strategies by |
|
953 |
setting appropriate members in join_tab structures. |
|
954 |
||
955 |
RETURN |
|
55
by brian
Update for using real bool types. |
956 |
false OK |
957 |
true Out of memory error |
|
1
by brian
clean slate |
958 |
*/ |
959 |
||
960 |
static |
|
151
by Brian Aker
Ulonglong to uint64_t |
961 |
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint no_jbuf_after) |
1
by brian
clean slate |
962 |
{ |
963 |
table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS; |
|
964 |
struct { |
|
965 |
/* |
|
966 |
0 - invalid (EOF marker), |
|
967 |
1 - InsideOut, |
|
968 |
2 - Temptable (maybe confluent), |
|
969 |
3 - Temptable with join buffering |
|
970 |
*/ |
|
971 |
uint strategy; |
|
972 |
uint start_idx; /* Left range bound */ |
|
973 |
uint end_idx; /* Right range bound */ |
|
974 |
/* |
|
975 |
For Temptable strategy: Bitmap of all outer and correlated tables from |
|
976 |
all involved join nests. |
|
977 |
*/ |
|
978 |
table_map outer_tables; |
|
979 |
} dups_ranges [MAX_TABLES]; |
|
980 |
||
981 |
TABLE_LIST *emb_insideout_nest= NULL; |
|
982 |
table_map emb_sj_map= 0; /* A bitmap of sj-nests (that is, their sj-inner |
|
983 |
tables) whose ranges we're in */ |
|
984 |
table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */ |
|
985 |
table_map range_start_map= 0; /* table_map at current range start */ |
|
55
by brian
Update for using real bool types. |
986 |
bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */ |
1
by brian
clean slate |
987 |
int cur_range= 0; |
988 |
uint i; |
|
989 |
||
990 |
/* |
|
991 |
First pass: locate the duplicate-generating ranges and pick the strategies. |
|
992 |
*/ |
|
993 |
for (i=join->const_tables ; i < join->tables ; i++) |
|
994 |
{ |
|
995 |
JOIN_TAB *tab=join->join_tab+i; |
|
996 |
TABLE *table=tab->table; |
|
997 |
cur_map |= table->map; |
|
998 |
||
999 |
if (tab->emb_sj_nest) // Encountered an sj-inner table |
|
1000 |
{ |
|
1001 |
if (!emb_sj_map) |
|
1002 |
{ |
|
1003 |
dups_ranges[cur_range].start_idx= i; |
|
1004 |
range_start_map= cur_map & ~table->map; |
|
1005 |
/* |
|
1006 |
Remember if this is a possible start of range that is covered by |
|
1007 |
the InsideOut strategy (the reason that it is not covered could |
|
1008 |
be that it overlaps with anther semi-join's range. we don't |
|
1009 |
support InsideOut for joined ranges) |
|
1010 |
*/ |
|
1011 |
if (join->best_positions[i].use_insideout_scan) |
|
1012 |
emb_insideout_nest= tab->emb_sj_nest; |
|
1013 |
} |
|
1014 |
||
1015 |
emb_sj_map |= tab->emb_sj_nest->sj_inner_tables; |
|
1016 |
emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on; |
|
1017 |
||
1018 |
if (tab->emb_sj_nest != emb_insideout_nest) |
|
1019 |
{ |
|
1020 |
/* |
|
1021 |
Two different semi-joins interleave. This cannot be handled by |
|
1022 |
InsideOut strategy. |
|
1023 |
*/ |
|
1024 |
emb_insideout_nest= NULL; |
|
1025 |
} |
|
1026 |
} |
|
1027 |
||
1028 |
if (emb_sj_map) /* We're in duplicate-generating range */ |
|
1029 |
{ |
|
1030 |
if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) && |
|
1031 |
tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner && |
|
1032 |
i <= no_jbuf_after && !dealing_with_jbuf) |
|
1033 |
{ |
|
1034 |
/* |
|
1035 |
This table uses join buffering, which makes use of FirstMatch or |
|
1036 |
InsideOut strategies impossible for the current and (we assume) |
|
1037 |
preceding duplicate-producing ranges. |
|
1038 |
That is, for the join order: |
|
1039 |
||
1040 |
x x [ x x] x [x x x] x [x x X* x] x |
|
1041 |
| | | | | \ |
|
1042 |
+-----+ +-----+ | join buffering use |
|
1043 |
r1 r2 we're here |
|
1044 |
||
1045 |
we'll have to remove r1 and r2 and use duplicate-elimination |
|
1046 |
strategy that spans all the tables, starting from the very 1st |
|
1047 |
one. |
|
1048 |
*/ |
|
55
by brian
Update for using real bool types. |
1049 |
dealing_with_jbuf= true; |
1050 |
emb_insideout_nest= false; |
|
1
by brian
clean slate |
1051 |
|
1052 |
/* |
|
1053 |
Absorb all preceding duplicate-eliminating ranges. Their strategies |
|
1054 |
do not matter: |
|
1055 |
*/ |
|
1056 |
for (int prev_range= 0; prev_range < cur_range; prev_range++) |
|
1057 |
{ |
|
1058 |
dups_ranges[cur_range].outer_tables |= |
|
1059 |
dups_ranges[prev_range].outer_tables; |
|
1060 |
} |
|
1061 |
dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */ |
|
1062 |
dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables; |
|
1063 |
cur_range= 0; |
|
1064 |
} |
|
1065 |
||
1066 |
/* |
|
1067 |
Check if we are at the end of duplicate-producing range. We are if |
|
1068 |
||
1069 |
1. It's an InsideOut range (which presumes all correlated tables are |
|
1070 |
in the prefix), and all inner tables are in the join order prefix, |
|
1071 |
or |
|
1072 |
2. It's a DuplicateElimination range (possibly covering several |
|
1073 |
SJ-nests), and all inner, outer, and correlated tables of all |
|
1074 |
sj-nests are in the join order prefix. |
|
1075 |
*/ |
|
55
by brian
Update for using real bool types. |
1076 |
bool end_of_range= false; |
1
by brian
clean slate |
1077 |
if (emb_insideout_nest && |
1078 |
bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables)) |
|
1079 |
{ |
|
1080 |
/* Save that this range is handled with InsideOut: */ |
|
1081 |
dups_ranges[cur_range].strategy= 1; |
|
55
by brian
Update for using real bool types. |
1082 |
end_of_range= true; |
1
by brian
clean slate |
1083 |
} |
1084 |
else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map)) |
|
1085 |
{ |
|
1086 |
/* |
|
1087 |
This is a complete range to be handled with either DuplicateWeedout |
|
1088 |
or FirstMatch |
|
1089 |
*/ |
|
1090 |
dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2; |
|
1091 |
/* |
|
1092 |
This will hold tables from within the range that need to be put |
|
1093 |
into the join buffer before we can use the FirstMatch on its tail. |
|
1094 |
*/ |
|
1095 |
dups_ranges[cur_range].outer_tables= emb_outer_tables & |
|
1096 |
~range_start_map; |
|
55
by brian
Update for using real bool types. |
1097 |
end_of_range= true; |
1
by brian
clean slate |
1098 |
} |
1099 |
||
1100 |
if (end_of_range) |
|
1101 |
{ |
|
1102 |
dups_ranges[cur_range].end_idx= i+1; |
|
1103 |
emb_sj_map= emb_outer_tables= 0; |
|
1104 |
emb_insideout_nest= NULL; |
|
55
by brian
Update for using real bool types. |
1105 |
dealing_with_jbuf= false; |
1
by brian
clean slate |
1106 |
dups_ranges[++cur_range].strategy= 0; |
1107 |
} |
|
1108 |
} |
|
1109 |
} |
|
1110 |
||
1111 |
THD *thd= join->thd; |
|
1112 |
SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables; |
|
1113 |
/* |
|
1114 |
Second pass: setup the chosen strategies |
|
1115 |
*/ |
|
1116 |
for (int j= 0; j < cur_range; j++) |
|
1117 |
{ |
|
1118 |
JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx; |
|
1119 |
JOIN_TAB *jump_to; |
|
1120 |
if (dups_ranges[j].strategy == 1) // InsideOut strategy |
|
1121 |
{ |
|
1122 |
tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1; |
|
1123 |
jump_to= tab++; |
|
1124 |
} |
|
1125 |
else // DuplicateWeedout strategy |
|
1126 |
{ |
|
1127 |
SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES]; |
|
1128 |
table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS; |
|
1129 |
uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes) |
|
1130 |
uint jt_null_bits= 0; // # null bits in tuple bytes |
|
1131 |
SJ_TMP_TABLE::TAB *last_tab= sjtabs; |
|
1132 |
uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID; |
|
1133 |
JOIN_TAB *last_outer_tab= tab - 1; |
|
1134 |
/* |
|
1135 |
Walk through the range and remember |
|
1136 |
- tables that need their rowids to be put into temptable |
|
1137 |
- the last outer table |
|
1138 |
*/ |
|
1139 |
for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++) |
|
1140 |
{ |
|
1141 |
if (sj_table_is_included(join, tab)) |
|
1142 |
{ |
|
1143 |
last_tab->join_tab= tab; |
|
1144 |
last_tab->rowid_offset= jt_rowid_offset; |
|
1145 |
jt_rowid_offset += tab->table->file->ref_length; |
|
1146 |
if (tab->table->maybe_null) |
|
1147 |
{ |
|
1148 |
last_tab->null_byte= jt_null_bits / 8; |
|
1149 |
last_tab->null_bit= jt_null_bits++; |
|
1150 |
} |
|
1151 |
last_tab++; |
|
1152 |
tab->table->prepare_for_position(); |
|
1153 |
tab->rowid_keep_flags= rowid_keep_flags; |
|
1154 |
} |
|
1155 |
cur_map |= tab->table->map; |
|
1156 |
if (!tab->emb_sj_nest && bitmap_covers(cur_map, |
|
1157 |
dups_ranges[j].outer_tables)) |
|
1158 |
last_outer_tab= tab; |
|
1159 |
} |
|
1160 |
||
1161 |
if (jt_rowid_offset) /* Temptable has at least one rowid */ |
|
1162 |
{ |
|
1163 |
SJ_TMP_TABLE *sjtbl; |
|
1164 |
uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB); |
|
1165 |
if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) || |
|
1166 |
!(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1167 |
return(true); |
1
by brian
clean slate |
1168 |
memcpy(sjtbl->tabs, sjtabs, tabs_size); |
1169 |
sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs); |
|
1170 |
sjtbl->rowid_len= jt_rowid_offset; |
|
1171 |
sjtbl->null_bits= jt_null_bits; |
|
1172 |
sjtbl->null_bytes= (jt_null_bits + 7)/8; |
|
1173 |
||
1174 |
*next_sjtbl_ptr= sjtbl; |
|
1175 |
next_sjtbl_ptr= &(sjtbl->next); |
|
1176 |
sjtbl->next= NULL; |
|
1177 |
||
1178 |
sjtbl->tmp_table= |
|
1179 |
create_duplicate_weedout_tmp_table(thd, |
|
1180 |
sjtbl->rowid_len + |
|
1181 |
sjtbl->null_bytes, |
|
1182 |
sjtbl); |
|
1183 |
||
1184 |
join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl; |
|
1185 |
join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl; |
|
1186 |
} |
|
1187 |
tab= last_outer_tab + 1; |
|
1188 |
jump_to= last_outer_tab; |
|
1189 |
} |
|
1190 |
||
1191 |
/* Create the FirstMatch tail */ |
|
1192 |
for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++) |
|
1193 |
{ |
|
1194 |
if (tab->emb_sj_nest) |
|
1195 |
tab->do_firstmatch= jump_to; |
|
1196 |
else |
|
1197 |
jump_to= tab; |
|
1198 |
} |
|
1199 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1200 |
return(false); |
1
by brian
clean slate |
1201 |
} |
1202 |
||
1203 |
||
1204 |
static void cleanup_sj_tmp_tables(JOIN *join) |
|
1205 |
{ |
|
1206 |
for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl; |
|
1207 |
sj_tbl= sj_tbl->next) |
|
1208 |
{ |
|
1209 |
if (sj_tbl->tmp_table) |
|
1210 |
{ |
|
1211 |
free_tmp_table(join->thd, sj_tbl->tmp_table); |
|
1212 |
} |
|
1213 |
} |
|
1214 |
join->sj_tmp_tables= NULL; |
|
1215 |
} |
|
1216 |
||
1217 |
uint make_join_orderinfo(JOIN *join); |
|
1218 |
||
1219 |
/** |
|
1220 |
global select optimisation. |
|
1221 |
||
1222 |
@note |
|
1223 |
error code saved in field 'error' |
|
1224 |
||
1225 |
@retval |
|
1226 |
0 success |
|
1227 |
@retval |
|
1228 |
1 error |
|
1229 |
*/ |
|
1230 |
||
1231 |
int |
|
1232 |
JOIN::optimize() |
|
1233 |
{ |
|
1234 |
// to prevent double initialization on EXPLAIN |
|
1235 |
if (optimized) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1236 |
return(0); |
1
by brian
clean slate |
1237 |
optimized= 1; |
1238 |
||
1239 |
thd_proc_info(thd, "optimizing"); |
|
1240 |
row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR : |
|
1241 |
unit->select_limit_cnt); |
|
1242 |
/* select_limit is used to decide if we are likely to scan the whole table */ |
|
1243 |
select_limit= unit->select_limit_cnt; |
|
1244 |
if (having || (select_options & OPTION_FOUND_ROWS)) |
|
1245 |
select_limit= HA_POS_ERROR; |
|
1246 |
do_send_rows = (unit->select_limit_cnt) ? 1 : 0; |
|
1247 |
// Ignore errors of execution if option IGNORE present |
|
1248 |
if (thd->lex->ignore) |
|
1249 |
thd->lex->current_select->no_error= 1; |
|
1250 |
||
1251 |
#ifdef HAVE_REF_TO_FIELDS // Not done yet |
|
1252 |
/* Add HAVING to WHERE if possible */ |
|
1253 |
if (having && !group_list && !sum_func_count) |
|
1254 |
{ |
|
1255 |
if (!conds) |
|
1256 |
{ |
|
1257 |
conds= having; |
|
1258 |
having= 0; |
|
1259 |
} |
|
1260 |
else if ((conds=new Item_cond_and(conds,having))) |
|
1261 |
{ |
|
1262 |
/* |
|
1263 |
Item_cond_and can't be fixed after creation, so we do not check |
|
1264 |
conds->fixed |
|
1265 |
*/ |
|
1266 |
conds->fix_fields(thd, &conds); |
|
1267 |
conds->change_ref_to_fields(thd, tables_list); |
|
1268 |
conds->top_level_item(); |
|
1269 |
having= 0; |
|
1270 |
} |
|
1271 |
} |
|
1272 |
#endif |
|
1273 |
SELECT_LEX *sel= thd->lex->current_select; |
|
1274 |
if (sel->first_cond_optimization) |
|
1275 |
{ |
|
1276 |
/* |
|
1277 |
The following code will allocate the new items in a permanent |
|
1278 |
MEMROOT for prepared statements and stored procedures. |
|
1279 |
*/ |
|
1280 |
sel->first_cond_optimization= 0; |
|
1281 |
||
1282 |
/* Convert all outer joins to inner joins if possible */ |
|
55
by brian
Update for using real bool types. |
1283 |
conds= simplify_joins(this, join_list, conds, true, false); |
1
by brian
clean slate |
1284 |
build_bitmap_for_nested_joins(join_list, 0); |
1285 |
||
1286 |
sel->prep_where= conds ? conds->copy_andor_structure(thd) : 0; |
|
1287 |
} |
|
1288 |
||
1289 |
conds= optimize_cond(this, conds, join_list, &cond_value); |
|
1290 |
if (thd->is_error()) |
|
1291 |
{ |
|
1292 |
error= 1; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1293 |
return(1); |
1
by brian
clean slate |
1294 |
} |
1295 |
||
1296 |
{ |
|
1297 |
having= optimize_cond(this, having, join_list, &having_value); |
|
1298 |
if (thd->is_error()) |
|
1299 |
{ |
|
1300 |
error= 1; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1301 |
return(1); |
1
by brian
clean slate |
1302 |
} |
1303 |
if (select_lex->where) |
|
1304 |
select_lex->cond_value= cond_value; |
|
1305 |
if (select_lex->having) |
|
1306 |
select_lex->having_value= having_value; |
|
1307 |
||
1308 |
if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || |
|
1309 |
(!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS))) |
|
1310 |
{ /* Impossible cond */ |
|
1311 |
zero_result_cause= having_value == Item::COND_FALSE ? |
|
1312 |
"Impossible HAVING" : "Impossible WHERE"; |
|
1313 |
error= 0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1314 |
return(0); |
1
by brian
clean slate |
1315 |
} |
1316 |
} |
|
1317 |
||
1318 |
/* Optimize count(*), min() and max() */ |
|
1319 |
if (tables_list && tmp_table_param.sum_func_count && ! group_list) |
|
1320 |
{ |
|
1321 |
int res; |
|
1322 |
/* |
|
1323 |
opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match |
|
1324 |
to the WHERE conditions, |
|
1325 |
or 1 if all items were resolved, |
|
1326 |
or 0, or an error number HA_ERR_... |
|
1327 |
*/ |
|
1328 |
if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds))) |
|
1329 |
{ |
|
1330 |
if (res == HA_ERR_KEY_NOT_FOUND) |
|
1331 |
{ |
|
1332 |
zero_result_cause= "No matching min/max row"; |
|
1333 |
error=0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1334 |
return(0); |
1
by brian
clean slate |
1335 |
} |
1336 |
if (res > 1) |
|
1337 |
{ |
|
1338 |
error= res; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1339 |
return(1); |
1
by brian
clean slate |
1340 |
} |
1341 |
if (res < 0) |
|
1342 |
{ |
|
1343 |
zero_result_cause= "No matching min/max row"; |
|
1344 |
error=0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1345 |
return(0); |
1
by brian
clean slate |
1346 |
} |
1347 |
zero_result_cause= "Select tables optimized away"; |
|
1348 |
tables_list= 0; // All tables resolved |
|
1349 |
/* |
|
1350 |
Extract all table-independent conditions and replace the WHERE |
|
1351 |
clause with them. All other conditions were computed by opt_sum_query |
|
1352 |
and the MIN/MAX/COUNT function(s) have been replaced by constants, |
|
1353 |
so there is no need to compute the whole WHERE clause again. |
|
1354 |
Notice that make_cond_for_table() will always succeed to remove all |
|
1355 |
computed conditions, because opt_sum_query() is applicable only to |
|
1356 |
conjunctions. |
|
1357 |
Preserve conditions for EXPLAIN. |
|
1358 |
*/ |
|
1359 |
if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED)) |
|
1360 |
{ |
|
1361 |
COND *table_independent_conds= |
|
1362 |
make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0); |
|
1363 |
conds= table_independent_conds; |
|
1364 |
} |
|
1365 |
} |
|
1366 |
} |
|
1367 |
if (!tables_list) |
|
1368 |
{ |
|
1369 |
error= 0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1370 |
return(0); |
1
by brian
clean slate |
1371 |
} |
1372 |
error= -1; // Error is sent to client |
|
1373 |
sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables); |
|
1374 |
||
1375 |
/* Calculate how to do the join */ |
|
1376 |
thd_proc_info(thd, "statistics"); |
|
1377 |
if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) || |
|
1378 |
thd->is_fatal_error) |
|
1379 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1380 |
return(1); |
1
by brian
clean slate |
1381 |
} |
1382 |
||
1383 |
/* Remove distinct if only const tables */ |
|
1384 |
select_distinct= select_distinct && (const_tables != tables); |
|
1385 |
thd_proc_info(thd, "preparing"); |
|
1386 |
if (result->initialize_tables(this)) |
|
1387 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1388 |
return(1); // error == -1 |
1
by brian
clean slate |
1389 |
} |
1390 |
if (const_table_map != found_const_table_map && |
|
1391 |
!(select_options & SELECT_DESCRIBE) && |
|
1392 |
(!conds || |
|
1393 |
!(conds->used_tables() & RAND_TABLE_BIT) || |
|
1394 |
select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT |
|
1395 |
{ |
|
1396 |
zero_result_cause= "no matching row in const table"; |
|
1397 |
error= 0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1398 |
return(0); |
1
by brian
clean slate |
1399 |
} |
1400 |
if (!(thd->options & OPTION_BIG_SELECTS) && |
|
1401 |
best_read > (double) thd->variables.max_join_size && |
|
1402 |
!(select_options & SELECT_DESCRIBE)) |
|
1403 |
{ /* purecov: inspected */ |
|
1404 |
my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0)); |
|
1405 |
error= -1; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1406 |
return(1); |
1
by brian
clean slate |
1407 |
} |
1408 |
if (const_tables && !thd->locked_tables && |
|
1409 |
!(select_options & SELECT_NO_UNLOCK)) |
|
1410 |
mysql_unlock_some_tables(thd, table, const_tables); |
|
1411 |
if (!conds && outer_join) |
|
1412 |
{ |
|
1413 |
/* Handle the case where we have an OUTER JOIN without a WHERE */ |
|
152
by Brian Aker
longlong replacement |
1414 |
conds=new Item_int((int64_t) 1,1); // Always true |
1
by brian
clean slate |
1415 |
} |
1416 |
select= make_select(*table, const_table_map, |
|
1417 |
const_table_map, conds, 1, &error); |
|
1418 |
if (error) |
|
1419 |
{ /* purecov: inspected */ |
|
1420 |
error= -1; /* purecov: inspected */ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1421 |
return(1); |
1
by brian
clean slate |
1422 |
} |
1423 |
||
1424 |
reset_nj_counters(join_list); |
|
1425 |
make_outerjoin_info(this); |
|
1426 |
||
1427 |
/* |
|
1428 |
Among the equal fields belonging to the same multiple equality |
|
1429 |
choose the one that is to be retrieved first and substitute |
|
1430 |
all references to these in where condition for a reference for |
|
1431 |
the selected field. |
|
1432 |
*/ |
|
1433 |
if (conds) |
|
1434 |
{ |
|
1435 |
conds= substitute_for_best_equal_field(conds, cond_equal, map2table); |
|
1436 |
conds->update_used_tables(); |
|
1437 |
} |
|
1438 |
||
1439 |
/* |
|
1440 |
Permorm the the optimization on fields evaluation mentioned above |
|
1441 |
for all on expressions. |
|
1442 |
*/ |
|
1443 |
for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++) |
|
1444 |
{ |
|
1445 |
if (*tab->on_expr_ref) |
|
1446 |
{ |
|
1447 |
*tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref, |
|
1448 |
tab->cond_equal, |
|
1449 |
map2table); |
|
1450 |
(*tab->on_expr_ref)->update_used_tables(); |
|
1451 |
} |
|
1452 |
} |
|
1453 |
||
1454 |
if (conds &&!outer_join && const_table_map != found_const_table_map && |
|
1455 |
(select_options & SELECT_DESCRIBE) && |
|
1456 |
select_lex->master_unit() == &thd->lex->unit) // upper level SELECT |
|
1457 |
{ |
|
152
by Brian Aker
longlong replacement |
1458 |
conds=new Item_int((int64_t) 0,1); // Always false |
1
by brian
clean slate |
1459 |
} |
1460 |
if (make_join_select(this, select, conds)) |
|
1461 |
{ |
|
1462 |
zero_result_cause= |
|
1463 |
"Impossible WHERE noticed after reading const tables"; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1464 |
return(0); // error == 0 |
1
by brian
clean slate |
1465 |
} |
1466 |
||
1467 |
error= -1; /* if goto err */ |
|
1468 |
||
1469 |
/* Optimize distinct away if possible */ |
|
1470 |
{ |
|
1471 |
ORDER *org_order= order; |
|
1472 |
order=remove_const(this, order,conds,1, &simple_order); |
|
1473 |
if (thd->is_error()) |
|
1474 |
{ |
|
1475 |
error= 1; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1476 |
return(1); |
1
by brian
clean slate |
1477 |
} |
1478 |
||
1479 |
/* |
|
1480 |
If we are using ORDER BY NULL or ORDER BY const_expression, |
|
1481 |
return result in any order (even if we are using a GROUP BY) |
|
1482 |
*/ |
|
1483 |
if (!order && org_order) |
|
1484 |
skip_sort_order= 1; |
|
1485 |
} |
|
1486 |
/* |
|
1487 |
Check if we can optimize away GROUP BY/DISTINCT. |
|
1488 |
We can do that if there are no aggregate functions, the |
|
1489 |
fields in DISTINCT clause (if present) and/or columns in GROUP BY |
|
1490 |
(if present) contain direct references to all key parts of |
|
1491 |
an unique index (in whatever order) and if the key parts of the |
|
1492 |
unique index cannot contain NULLs. |
|
1493 |
Note that the unique keys for DISTINCT and GROUP BY should not |
|
1494 |
be the same (as long as they are unique). |
|
1495 |
||
1496 |
The FROM clause must contain a single non-constant table. |
|
1497 |
*/ |
|
1498 |
if (tables - const_tables == 1 && (group_list || select_distinct) && |
|
1499 |
!tmp_table_param.sum_func_count && |
|
1500 |
(!join_tab[const_tables].select || |
|
1501 |
!join_tab[const_tables].select->quick || |
|
1502 |
join_tab[const_tables].select->quick->get_type() != |
|
1503 |
QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)) |
|
1504 |
{ |
|
1505 |
if (group_list && |
|
1506 |
list_contains_unique_index(join_tab[const_tables].table, |
|
1507 |
find_field_in_order_list, |
|
1508 |
(void *) group_list)) |
|
1509 |
{ |
|
1510 |
/* |
|
1511 |
We have found that grouping can be removed since groups correspond to |
|
1512 |
only one row anyway, but we still have to guarantee correct result |
|
1513 |
order. The line below effectively rewrites the query from GROUP BY |
|
1514 |
<fields> to ORDER BY <fields>. There are two exceptions: |
|
1515 |
- if skip_sort_order is set (see above), then we can simply skip |
|
1516 |
GROUP BY; |
|
1517 |
- we can only rewrite ORDER BY if the ORDER BY fields are 'compatible' |
|
1518 |
with the GROUP BY ones, i.e. either one is a prefix of another. |
|
1519 |
We only check if the ORDER BY is a prefix of GROUP BY. In this case |
|
1520 |
test_if_subpart() copies the ASC/DESC attributes from the original |
|
1521 |
ORDER BY fields. |
|
1522 |
If GROUP BY is a prefix of ORDER BY, then it is safe to leave |
|
1523 |
'order' as is. |
|
1524 |
*/ |
|
1525 |
if (!order || test_if_subpart(group_list, order)) |
|
1526 |
order= skip_sort_order ? 0 : group_list; |
|
1527 |
/* |
|
1528 |
If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be |
|
1529 |
rewritten to IGNORE INDEX FOR ORDER BY(fields). |
|
1530 |
*/ |
|
1531 |
join_tab->table->keys_in_use_for_order_by= |
|
1532 |
join_tab->table->keys_in_use_for_group_by; |
|
1533 |
group_list= 0; |
|
1534 |
group= 0; |
|
1535 |
} |
|
1536 |
if (select_distinct && |
|
1537 |
list_contains_unique_index(join_tab[const_tables].table, |
|
1538 |
find_field_in_item_list, |
|
1539 |
(void *) &fields_list)) |
|
1540 |
{ |
|
1541 |
select_distinct= 0; |
|
1542 |
} |
|
1543 |
} |
|
1544 |
if (group_list || tmp_table_param.sum_func_count) |
|
1545 |
{ |
|
1546 |
if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE) |
|
1547 |
select_distinct=0; |
|
1548 |
} |
|
1549 |
else if (select_distinct && tables - const_tables == 1) |
|
1550 |
{ |
|
1551 |
/* |
|
1552 |
We are only using one table. In this case we change DISTINCT to a |
|
1553 |
GROUP BY query if: |
|
1554 |
- The GROUP BY can be done through indexes (no sort) and the ORDER |
|
1555 |
BY only uses selected fields. |
|
1556 |
(In this case we can later optimize away GROUP BY and ORDER BY) |
|
1557 |
- We are scanning the whole table without LIMIT |
|
1558 |
This can happen if: |
|
1559 |
- We are using CALC_FOUND_ROWS |
|
1560 |
- We are using an ORDER BY that can't be optimized away. |
|
1561 |
||
1562 |
We don't want to use this optimization when we are using LIMIT |
|
1563 |
because in this case we can just create a temporary table that |
|
1564 |
holds LIMIT rows and stop when this table is full. |
|
1565 |
*/ |
|
1566 |
JOIN_TAB *tab= &join_tab[const_tables]; |
|
1567 |
bool all_order_fields_used; |
|
1568 |
if (order) |
|
1569 |
skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1, |
|
1570 |
&tab->table->keys_in_use_for_order_by); |
|
1571 |
if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array, |
|
1572 |
order, fields_list, all_fields, |
|
1573 |
&all_order_fields_used))) |
|
1574 |
{ |
|
1575 |
bool skip_group= (skip_sort_order && |
|
1576 |
test_if_skip_sort_order(tab, group_list, select_limit, 1, |
|
1577 |
&tab->table->keys_in_use_for_group_by) != 0); |
|
1578 |
count_field_types(select_lex, &tmp_table_param, all_fields, 0); |
|
1579 |
if ((skip_group && all_order_fields_used) || |
|
1580 |
select_limit == HA_POS_ERROR || |
|
1581 |
(order && !skip_sort_order)) |
|
1582 |
{ |
|
1583 |
/* Change DISTINCT to GROUP BY */ |
|
1584 |
select_distinct= 0; |
|
1585 |
no_order= !order; |
|
1586 |
if (all_order_fields_used) |
|
1587 |
{ |
|
1588 |
if (order && skip_sort_order) |
|
1589 |
{ |
|
1590 |
/* |
|
1591 |
Force MySQL to read the table in sorted order to get result in |
|
1592 |
ORDER BY order. |
|
1593 |
*/ |
|
1594 |
tmp_table_param.quick_group=0; |
|
1595 |
} |
|
1596 |
order=0; |
|
1597 |
} |
|
1598 |
group=1; // For end_write_group |
|
1599 |
} |
|
1600 |
else |
|
1601 |
group_list= 0; |
|
1602 |
} |
|
1603 |
else if (thd->is_fatal_error) // End of memory |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1604 |
return(1); |
1
by brian
clean slate |
1605 |
} |
1606 |
simple_group= 0; |
|
1607 |
{ |
|
1608 |
ORDER *old_group_list; |
|
1609 |
group_list= remove_const(this, (old_group_list= group_list), conds, |
|
1610 |
rollup.state == ROLLUP::STATE_NONE, |
|
1611 |
&simple_group); |
|
1612 |
if (thd->is_error()) |
|
1613 |
{ |
|
1614 |
error= 1; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1615 |
return(1); |
1
by brian
clean slate |
1616 |
} |
1617 |
if (old_group_list && !group_list) |
|
1618 |
select_distinct= 0; |
|
1619 |
} |
|
1620 |
if (!group_list && group) |
|
1621 |
{ |
|
1622 |
order=0; // The output has only one row |
|
1623 |
simple_order=1; |
|
1624 |
select_distinct= 0; // No need in distinct for 1 row |
|
1625 |
group_optimized_away= 1; |
|
1626 |
} |
|
1627 |
||
1628 |
calc_group_buffer(this, group_list); |
|
1629 |
send_group_parts= tmp_table_param.group_parts; /* Save org parts */ |
|
1630 |
||
1631 |
if (test_if_subpart(group_list, order) || |
|
1632 |
(!group_list && tmp_table_param.sum_func_count)) |
|
1633 |
order=0; |
|
1634 |
||
1635 |
// Can't use sort on head table if using row cache |
|
1636 |
if (full_join) |
|
1637 |
{ |
|
1638 |
if (group_list) |
|
1639 |
simple_group=0; |
|
1640 |
if (order) |
|
1641 |
simple_order=0; |
|
1642 |
} |
|
1643 |
||
1644 |
/* |
|
1645 |
Check if we need to create a temporary table. |
|
1646 |
This has to be done if all tables are not already read (const tables) |
|
1647 |
and one of the following conditions holds: |
|
1648 |
- We are using DISTINCT (simple distinct's are already optimized away) |
|
1649 |
- We are using an ORDER BY or GROUP BY on fields not in the first table |
|
1650 |
- We are using different ORDER BY and GROUP BY orders |
|
1651 |
- The user wants us to buffer the result. |
|
1652 |
*/ |
|
1653 |
need_tmp= (const_tables != tables && |
|
1654 |
((select_distinct || !simple_order || !simple_group) || |
|
1655 |
(group_list && order) || |
|
1656 |
test(select_options & OPTION_BUFFER_RESULT))); |
|
1657 |
||
1658 |
uint no_jbuf_after= make_join_orderinfo(this); |
|
151
by Brian Aker
Ulonglong to uint64_t |
1659 |
uint64_t select_opts_for_readinfo= |
1
by brian
clean slate |
1660 |
(select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0); |
1661 |
||
1662 |
sj_tmp_tables= NULL; |
|
1663 |
if (!select_lex->sj_nests.is_empty()) |
|
1664 |
setup_semijoin_dups_elimination(this, select_opts_for_readinfo, |
|
1665 |
no_jbuf_after); |
|
1666 |
||
1667 |
// No cache for MATCH == 'Don't use join buffering when we use MATCH'. |
|
1668 |
if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1669 |
return(1); |
1
by brian
clean slate |
1670 |
|
1671 |
/* Create all structures needed for materialized subquery execution. */ |
|
1672 |
if (setup_subquery_materialization()) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1673 |
return(1); |
1
by brian
clean slate |
1674 |
|
1675 |
/* |
|
1676 |
is this simple IN subquery? |
|
1677 |
*/ |
|
1678 |
if (!group_list && !order && |
|
1679 |
unit->item && unit->item->substype() == Item_subselect::IN_SUBS && |
|
1680 |
tables == 1 && conds && |
|
1681 |
!unit->is_union()) |
|
1682 |
{ |
|
1683 |
if (!having) |
|
1684 |
{ |
|
1685 |
Item *where= conds; |
|
1686 |
if (join_tab[0].type == JT_EQ_REF && |
|
1687 |
join_tab[0].ref.items[0]->name == in_left_expr_name) |
|
1688 |
{ |
|
1689 |
remove_subq_pushed_predicates(&where); |
|
1690 |
save_index_subquery_explain_info(join_tab, where); |
|
1691 |
join_tab[0].type= JT_UNIQUE_SUBQUERY; |
|
1692 |
error= 0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1693 |
return(unit->item-> |
1
by brian
clean slate |
1694 |
change_engine(new |
1695 |
subselect_uniquesubquery_engine(thd, |
|
1696 |
join_tab, |
|
1697 |
unit->item, |
|
1698 |
where))); |
|
1699 |
} |
|
1700 |
else if (join_tab[0].type == JT_REF && |
|
1701 |
join_tab[0].ref.items[0]->name == in_left_expr_name) |
|
1702 |
{ |
|
1703 |
remove_subq_pushed_predicates(&where); |
|
1704 |
save_index_subquery_explain_info(join_tab, where); |
|
1705 |
join_tab[0].type= JT_INDEX_SUBQUERY; |
|
1706 |
error= 0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1707 |
return(unit->item-> |
1
by brian
clean slate |
1708 |
change_engine(new |
1709 |
subselect_indexsubquery_engine(thd, |
|
1710 |
join_tab, |
|
1711 |
unit->item, |
|
1712 |
where, |
|
1713 |
NULL, |
|
1714 |
0))); |
|
1715 |
} |
|
1716 |
} else if (join_tab[0].type == JT_REF_OR_NULL && |
|
1717 |
join_tab[0].ref.items[0]->name == in_left_expr_name && |
|
1718 |
having->name == in_having_cond) |
|
1719 |
{ |
|
1720 |
join_tab[0].type= JT_INDEX_SUBQUERY; |
|
1721 |
error= 0; |
|
1722 |
conds= remove_additional_cond(conds); |
|
1723 |
save_index_subquery_explain_info(join_tab, conds); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1724 |
return(unit->item-> |
1
by brian
clean slate |
1725 |
change_engine(new subselect_indexsubquery_engine(thd, |
1726 |
join_tab, |
|
1727 |
unit->item, |
|
1728 |
conds, |
|
1729 |
having, |
|
1730 |
1))); |
|
1731 |
} |
|
1732 |
||
1733 |
} |
|
1734 |
/* |
|
1735 |
Need to tell handlers that to play it safe, it should fetch all |
|
1736 |
columns of the primary key of the tables: this is because MySQL may |
|
1737 |
build row pointers for the rows, and for all columns of the primary key |
|
1738 |
the read set has not necessarily been set by the server code. |
|
1739 |
*/ |
|
1740 |
if (need_tmp || select_distinct || group_list || order) |
|
1741 |
{ |
|
1742 |
for (uint i = const_tables; i < tables; i++) |
|
1743 |
join_tab[i].table->prepare_for_position(); |
|
1744 |
} |
|
1745 |
||
1746 |
if (const_tables != tables) |
|
1747 |
{ |
|
1748 |
/* |
|
1749 |
Because filesort always does a full table scan or a quick range scan |
|
1750 |
we must add the removed reference to the select for the table. |
|
1751 |
We only need to do this when we have a simple_order or simple_group |
|
1752 |
as in other cases the join is done before the sort. |
|
1753 |
*/ |
|
1754 |
if ((order || group_list) && |
|
1755 |
(join_tab[const_tables].type != JT_ALL) && |
|
1756 |
(join_tab[const_tables].type != JT_REF_OR_NULL) && |
|
1757 |
((order && simple_order) || (group_list && simple_group))) |
|
1758 |
{ |
|
1759 |
if (add_ref_to_table_cond(thd,&join_tab[const_tables])) { |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1760 |
return(1); |
1
by brian
clean slate |
1761 |
} |
1762 |
} |
|
1763 |
||
1764 |
if (!(select_options & SELECT_BIG_RESULT) && |
|
1765 |
((group_list && |
|
1766 |
(!simple_group || |
|
1767 |
!test_if_skip_sort_order(&join_tab[const_tables], group_list, |
|
1768 |
unit->select_limit_cnt, 0, |
|
1769 |
&join_tab[const_tables].table-> |
|
1770 |
keys_in_use_for_group_by))) || |
|
1771 |
select_distinct) && |
|
1772 |
tmp_table_param.quick_group) |
|
1773 |
{ |
|
1774 |
need_tmp=1; simple_order=simple_group=0; // Force tmp table without sort |
|
1775 |
} |
|
1776 |
if (order) |
|
1777 |
{ |
|
1778 |
/* |
|
1779 |
Force using of tmp table if sorting by a SP or UDF function due to |
|
1780 |
their expensive and probably non-deterministic nature. |
|
1781 |
*/ |
|
1782 |
for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next) |
|
1783 |
{ |
|
1784 |
Item *item= *tmp_order->item; |
|
1785 |
if (item->is_expensive()) |
|
1786 |
{ |
|
1787 |
/* Force tmp table without sort */ |
|
1788 |
need_tmp=1; simple_order=simple_group=0; |
|
1789 |
break; |
|
1790 |
} |
|
1791 |
} |
|
1792 |
} |
|
1793 |
} |
|
1794 |
||
1795 |
tmp_having= having; |
|
1796 |
if (select_options & SELECT_DESCRIBE) |
|
1797 |
{ |
|
1798 |
error= 0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1799 |
return(0); |
1
by brian
clean slate |
1800 |
} |
1801 |
having= 0; |
|
1802 |
||
1803 |
/* |
|
1804 |
The loose index scan access method guarantees that all grouping or |
|
1805 |
duplicate row elimination (for distinct) is already performed |
|
1806 |
during data retrieval, and that all MIN/MAX functions are already |
|
1807 |
computed for each group. Thus all MIN/MAX functions should be |
|
1808 |
treated as regular functions, and there is no need to perform |
|
1809 |
grouping in the main execution loop. |
|
1810 |
Notice that currently loose index scan is applicable only for |
|
1811 |
single table queries, thus it is sufficient to test only the first |
|
1812 |
join_tab element of the plan for its access method. |
|
1813 |
*/ |
|
1814 |
if (join_tab->is_using_loose_index_scan()) |
|
55
by brian
Update for using real bool types. |
1815 |
tmp_table_param.precomputed_group_by= true; |
1
by brian
clean slate |
1816 |
|
1817 |
/* Create a tmp table if distinct or if the sort is too complicated */ |
|
1818 |
if (need_tmp) |
|
1819 |
{ |
|
1820 |
thd_proc_info(thd, "Creating tmp table"); |
|
1821 |
||
1822 |
init_items_ref_array(); |
|
1823 |
||
1824 |
tmp_table_param.hidden_field_count= (all_fields.elements - |
|
1825 |
fields_list.elements); |
|
1826 |
ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list : |
|
1827 |
(ORDER*) 0); |
|
1828 |
/* |
|
1829 |
Pushing LIMIT to the temporary table creation is not applicable |
|
1830 |
when there is ORDER BY or GROUP BY or there is no GROUP BY, but |
|
1831 |
there are aggregate functions, because in all these cases we need |
|
1832 |
all result rows. |
|
1833 |
*/ |
|
1834 |
ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) && |
|
1835 |
!tmp_group && |
|
1836 |
!thd->lex->current_select->with_sum_func) ? |
|
1837 |
select_limit : HA_POS_ERROR; |
|
1838 |
||
1839 |
if (!(exec_tmp_table1= |
|
1840 |
create_tmp_table(thd, &tmp_table_param, all_fields, |
|
1841 |
tmp_group, |
|
1842 |
group_list ? 0 : select_distinct, |
|
1843 |
group_list && simple_group, |
|
1844 |
select_options, |
|
1845 |
tmp_rows_limit, |
|
1846 |
(char *) ""))) |
|
1847 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1848 |
return(1); |
1
by brian
clean slate |
1849 |
} |
1850 |
||
1851 |
/* |
|
1852 |
We don't have to store rows in temp table that doesn't match HAVING if: |
|
1853 |
- we are sorting the table and writing complete group rows to the |
|
1854 |
temp table. |
|
1855 |
- We are using DISTINCT without resolving the distinct as a GROUP BY |
|
1856 |
on all columns. |
|
1857 |
||
1858 |
If having is not handled here, it will be checked before the row |
|
1859 |
is sent to the client. |
|
1860 |
*/ |
|
1861 |
if (tmp_having && |
|
1862 |
(sort_and_group || (exec_tmp_table1->distinct && !group_list))) |
|
1863 |
having= tmp_having; |
|
1864 |
||
1865 |
/* if group or order on first table, sort first */ |
|
1866 |
if (group_list && simple_group) |
|
1867 |
{ |
|
1868 |
thd_proc_info(thd, "Sorting for group"); |
|
1869 |
if (create_sort_index(thd, this, group_list, |
|
55
by brian
Update for using real bool types. |
1870 |
HA_POS_ERROR, HA_POS_ERROR, false) || |
1
by brian
clean slate |
1871 |
alloc_group_fields(this, group_list) || |
1872 |
make_sum_func_list(all_fields, fields_list, 1) || |
|
1873 |
setup_sum_funcs(thd, sum_funcs)) |
|
1874 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1875 |
return(1); |
1
by brian
clean slate |
1876 |
} |
1877 |
group_list=0; |
|
1878 |
} |
|
1879 |
else |
|
1880 |
{ |
|
1881 |
if (make_sum_func_list(all_fields, fields_list, 0) || |
|
1882 |
setup_sum_funcs(thd, sum_funcs)) |
|
1883 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1884 |
return(1); |
1
by brian
clean slate |
1885 |
} |
1886 |
||
1887 |
if (!group_list && ! exec_tmp_table1->distinct && order && simple_order) |
|
1888 |
{ |
|
1889 |
thd_proc_info(thd, "Sorting for order"); |
|
1890 |
if (create_sort_index(thd, this, order, |
|
55
by brian
Update for using real bool types. |
1891 |
HA_POS_ERROR, HA_POS_ERROR, true)) |
1
by brian
clean slate |
1892 |
{ |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1893 |
return(1); |
1
by brian
clean slate |
1894 |
} |
1895 |
order=0; |
|
1896 |
} |
|
1897 |
} |
|
1898 |
||
1899 |
/* |
|
1900 |
Optimize distinct when used on some of the tables |
|
1901 |
SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b |
|
1902 |
In this case we can stop scanning t2 when we have found one t1.a |
|
1903 |
*/ |
|
1904 |
||
1905 |
if (exec_tmp_table1->distinct) |
|
1906 |
{ |
|
1907 |
table_map used_tables= thd->used_tables; |
|
1908 |
JOIN_TAB *last_join_tab= join_tab+tables-1; |
|
1909 |
do |
|
1910 |
{ |
|
1911 |
if (used_tables & last_join_tab->table->map) |
|
1912 |
break; |
|
1913 |
last_join_tab->not_used_in_distinct=1; |
|
1914 |
} while (last_join_tab-- != join_tab); |
|
1915 |
/* Optimize "select distinct b from t1 order by key_part_1 limit #" */ |
|
1916 |
if (order && skip_sort_order) |
|
1917 |
{ |
|
1918 |
/* Should always succeed */ |
|
1919 |
if (test_if_skip_sort_order(&join_tab[const_tables], |
|
1920 |
order, unit->select_limit_cnt, 0, |
|
1921 |
&join_tab[const_tables].table-> |
|
1922 |
keys_in_use_for_order_by)) |
|
1923 |
order=0; |
|
1924 |
} |
|
1925 |
} |
|
1926 |
||
1927 |
/* |
|
1928 |
If this join belongs to an uncacheable subquery save |
|
1929 |
the original join |
|
1930 |
*/ |
|
1931 |
if (select_lex->uncacheable && !is_top_level_join() && |
|
1932 |
init_save_join_tab()) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1933 |
return(-1); /* purecov: inspected */ |
1
by brian
clean slate |
1934 |
} |
1935 |
||
1936 |
error= 0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1937 |
return(0); |
1
by brian
clean slate |
1938 |
} |
1939 |
||
1940 |
||
1941 |
/** |
|
1942 |
Restore values in temporary join. |
|
1943 |
*/ |
|
1944 |
void JOIN::restore_tmp() |
|
1945 |
{ |
|
1946 |
memcpy(tmp_join, this, (size_t) sizeof(JOIN)); |
|
1947 |
} |
|
1948 |
||
1949 |
||
1950 |
int |
|
1951 |
JOIN::reinit() |
|
1952 |
{ |
|
1953 |
unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ? |
|
1954 |
select_lex->offset_limit->val_uint() : |
|
80.1.1
by Brian Aker
LL() cleanup |
1955 |
0ULL); |
1
by brian
clean slate |
1956 |
|
1957 |
first_record= 0; |
|
1958 |
||
1959 |
if (exec_tmp_table1) |
|
1960 |
{ |
|
1961 |
exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE); |
|
1962 |
exec_tmp_table1->file->ha_delete_all_rows(); |
|
1963 |
free_io_cache(exec_tmp_table1); |
|
1964 |
filesort_free_buffers(exec_tmp_table1,0); |
|
1965 |
} |
|
1966 |
if (exec_tmp_table2) |
|
1967 |
{ |
|
1968 |
exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE); |
|
1969 |
exec_tmp_table2->file->ha_delete_all_rows(); |
|
1970 |
free_io_cache(exec_tmp_table2); |
|
1971 |
filesort_free_buffers(exec_tmp_table2,0); |
|
1972 |
} |
|
1973 |
if (items0) |
|
1974 |
set_items_ref_array(items0); |
|
1975 |
||
1976 |
if (join_tab_save) |
|
1977 |
memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables); |
|
1978 |
||
1979 |
if (tmp_join) |
|
1980 |
restore_tmp(); |
|
1981 |
||
1982 |
/* Reset of sum functions */ |
|
1983 |
if (sum_funcs) |
|
1984 |
{ |
|
1985 |
Item_sum *func, **func_ptr= sum_funcs; |
|
1986 |
while ((func= *(func_ptr++))) |
|
1987 |
func->clear(); |
|
1988 |
} |
|
1989 |
||
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
1990 |
return(0); |
1
by brian
clean slate |
1991 |
} |
1992 |
||
1993 |
/** |
|
1994 |
@brief Save the original join layout |
|
1995 |
||
1996 |
@details Saves the original join layout so it can be reused in |
|
1997 |
re-execution and for EXPLAIN. |
|
1998 |
||
1999 |
@return Operation status |
|
2000 |
@retval 0 success. |
|
2001 |
@retval 1 error occurred. |
|
2002 |
*/ |
|
2003 |
||
2004 |
bool |
|
2005 |
JOIN::init_save_join_tab() |
|
2006 |
{ |
|
2007 |
if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN)))) |
|
2008 |
return 1; /* purecov: inspected */ |
|
2009 |
error= 0; // Ensure that tmp_join.error= 0 |
|
2010 |
restore_tmp(); |
|
2011 |
return 0; |
|
2012 |
} |
|
2013 |
||
2014 |
||
2015 |
bool |
|
2016 |
JOIN::save_join_tab() |
|
2017 |
{ |
|
2018 |
if (!join_tab_save && select_lex->master_unit()->uncacheable) |
|
2019 |
{ |
|
2020 |
if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab, |
|
2021 |
sizeof(JOIN_TAB) * tables))) |
|
2022 |
return 1; |
|
2023 |
} |
|
2024 |
return 0; |
|
2025 |
} |
|
2026 |
||
2027 |
||
2028 |
/** |
|
2029 |
Exec select. |
|
2030 |
||
2031 |
@todo |
|
2032 |
Note, that create_sort_index calls test_if_skip_sort_order and may |
|
2033 |
finally replace sorting with index scan if there is a LIMIT clause in |
|
2034 |
the query. It's never shown in EXPLAIN! |
|
2035 |
||
2036 |
@todo |
|
2037 |
When can we have here thd->net.report_error not zero? |
|
2038 |
*/ |
|
2039 |
void |
|
2040 |
JOIN::exec() |
|
2041 |
{ |
|
2042 |
List<Item> *columns_list= &fields_list; |
|
2043 |
int tmp_error; |
|
2044 |
||
2045 |
thd_proc_info(thd, "executing"); |
|
2046 |
error= 0; |
|
2047 |
(void) result->prepare2(); // Currently, this cannot fail. |
|
2048 |
||
2049 |
if (!tables_list && (tables || !select_lex->with_sum_func)) |
|
2050 |
{ // Only test of functions |
|
2051 |
if (select_options & SELECT_DESCRIBE) |
|
55
by brian
Update for using real bool types. |
2052 |
select_describe(this, false, false, false, |
1
by brian
clean slate |
2053 |
(zero_result_cause?zero_result_cause:"No tables used")); |
2054 |
else |
|
2055 |
{ |
|
2056 |
result->send_fields(*columns_list, |
|
2057 |
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF); |
|
2058 |
/* |
|
2059 |
We have to test for 'conds' here as the WHERE may not be constant |
|
2060 |
even if we don't have any tables for prepared statements or if |
|
2061 |
conds uses something like 'rand()'. |
|
2062 |
*/ |
|
2063 |
if (cond_value != Item::COND_FALSE && |
|
2064 |
(!conds || conds->val_int()) && |
|
2065 |
(!having || having->val_int())) |
|
2066 |
{ |
|
2067 |
if (do_send_rows && result->send_data(fields_list)) |
|
2068 |
error= 1; |
|
2069 |
else |
|
2070 |
{ |
|
2071 |
error= (int) result->send_eof(); |
|
2072 |
send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 : |
|
2073 |
thd->sent_row_count); |
|
2074 |
} |
|
2075 |
} |
|
2076 |
else |
|
2077 |
{ |
|
2078 |
error=(int) result->send_eof(); |
|
2079 |
send_records= 0; |
|
2080 |
} |
|
2081 |
} |
|
2082 |
/* Single select (without union) always returns 0 or 1 row */ |
|
2083 |
thd->limit_found_rows= send_records; |
|
2084 |
thd->examined_row_count= 0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2085 |
return; |
1
by brian
clean slate |
2086 |
} |
2087 |
/* |
|
2088 |
Don't reset the found rows count if there're no tables as |
|
2089 |
FOUND_ROWS() may be called. Never reset the examined row count here. |
|
2090 |
It must be accumulated from all join iterations of all join parts. |
|
2091 |
*/ |
|
2092 |
if (tables) |
|
2093 |
thd->limit_found_rows= 0; |
|
2094 |
||
2095 |
if (zero_result_cause) |
|
2096 |
{ |
|
2097 |
(void) return_zero_rows(this, result, select_lex->leaf_tables, |
|
2098 |
*columns_list, |
|
2099 |
send_row_on_empty_set(), |
|
2100 |
select_options, |
|
2101 |
zero_result_cause, |
|
2102 |
having); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2103 |
return; |
1
by brian
clean slate |
2104 |
} |
2105 |
||
2106 |
if ((this->select_lex->options & OPTION_SCHEMA_TABLE) && |
|
2107 |
get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2108 |
return; |
1
by brian
clean slate |
2109 |
|
2110 |
if (select_options & SELECT_DESCRIBE) |
|
2111 |
{ |
|
2112 |
/* |
|
2113 |
Check if we managed to optimize ORDER BY away and don't use temporary |
|
2114 |
table to resolve ORDER BY: in that case, we only may need to do |
|
2115 |
filesort for GROUP BY. |
|
2116 |
*/ |
|
2117 |
if (!order && !no_order && (!skip_sort_order || !need_tmp)) |
|
2118 |
{ |
|
2119 |
/* |
|
2120 |
Reset 'order' to 'group_list' and reinit variables describing |
|
2121 |
'order' |
|
2122 |
*/ |
|
2123 |
order= group_list; |
|
2124 |
simple_order= simple_group; |
|
2125 |
skip_sort_order= 0; |
|
2126 |
} |
|
2127 |
if (order && |
|
2128 |
(order != group_list || !(select_options & SELECT_BIG_RESULT)) && |
|
2129 |
(const_tables == tables || |
|
2130 |
((simple_order || skip_sort_order) && |
|
2131 |
test_if_skip_sort_order(&join_tab[const_tables], order, |
|
2132 |
select_limit, 0, |
|
2133 |
&join_tab[const_tables].table-> |
|
2134 |
keys_in_use_for_query)))) |
|
2135 |
order=0; |
|
2136 |
having= tmp_having; |
|
2137 |
select_describe(this, need_tmp, |
|
2138 |
order != 0 && !skip_sort_order, |
|
2139 |
select_distinct, |
|
2140 |
!tables ? "No tables used" : NullS); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2141 |
return; |
1
by brian
clean slate |
2142 |
} |
2143 |
||
2144 |
JOIN *curr_join= this; |
|
2145 |
List<Item> *curr_all_fields= &all_fields; |
|
2146 |
List<Item> *curr_fields_list= &fields_list; |
|
2147 |
TABLE *curr_tmp_table= 0; |
|
2148 |
/* |
|
2149 |
Initialize examined rows here because the values from all join parts |
|
2150 |
must be accumulated in examined_row_count. Hence every join |
|
2151 |
iteration must count from zero. |
|
2152 |
*/ |
|
2153 |
curr_join->examined_rows= 0; |
|
2154 |
||
2155 |
/* Create a tmp table if distinct or if the sort is too complicated */ |
|
2156 |
if (need_tmp) |
|
2157 |
{ |
|
2158 |
if (tmp_join) |
|
2159 |
{ |
|
2160 |
/* |
|
2161 |
We are in a non cacheable sub query. Get the saved join structure |
|
2162 |
after optimization. |
|
2163 |
(curr_join may have been modified during last exection and we need |
|
2164 |
to reset it) |
|
2165 |
*/ |
|
2166 |
curr_join= tmp_join; |
|
2167 |
} |
|
2168 |
curr_tmp_table= exec_tmp_table1; |
|
2169 |
||
2170 |
/* Copy data to the temporary table */ |
|
2171 |
thd_proc_info(thd, "Copying to tmp table"); |
|
2172 |
if (!curr_join->sort_and_group && |
|
2173 |
curr_join->const_tables != curr_join->tables) |
|
2174 |
curr_join->join_tab[curr_join->const_tables].sorted= 0; |
|
2175 |
if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table))) |
|
2176 |
{ |
|
2177 |
error= tmp_error; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2178 |
return; |
1
by brian
clean slate |
2179 |
} |
2180 |
curr_tmp_table->file->info(HA_STATUS_VARIABLE); |
|
2181 |
||
2182 |
if (curr_join->having) |
|
2183 |
curr_join->having= curr_join->tmp_having= 0; // Allready done |
|
2184 |
||
2185 |
/* Change sum_fields reference to calculated fields in tmp_table */ |
|
2186 |
curr_join->all_fields= *curr_all_fields; |
|
2187 |
if (!items1) |
|
2188 |
{ |
|
2189 |
items1= items0 + all_fields.elements; |
|
2190 |
if (sort_and_group || curr_tmp_table->group) |
|
2191 |
{ |
|
2192 |
if (change_to_use_tmp_fields(thd, items1, |
|
2193 |
tmp_fields_list1, tmp_all_fields1, |
|
2194 |
fields_list.elements, all_fields)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2195 |
return; |
1
by brian
clean slate |
2196 |
} |
2197 |
else |
|
2198 |
{ |
|
2199 |
if (change_refs_to_tmp_fields(thd, items1, |
|
2200 |
tmp_fields_list1, tmp_all_fields1, |
|
2201 |
fields_list.elements, all_fields)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2202 |
return; |
1
by brian
clean slate |
2203 |
} |
2204 |
curr_join->tmp_all_fields1= tmp_all_fields1; |
|
2205 |
curr_join->tmp_fields_list1= tmp_fields_list1; |
|
2206 |
curr_join->items1= items1; |
|
2207 |
} |
|
2208 |
curr_all_fields= &tmp_all_fields1; |
|
2209 |
curr_fields_list= &tmp_fields_list1; |
|
2210 |
curr_join->set_items_ref_array(items1); |
|
2211 |
||
2212 |
if (sort_and_group || curr_tmp_table->group) |
|
2213 |
{ |
|
2214 |
curr_join->tmp_table_param.field_count+= |
|
2215 |
curr_join->tmp_table_param.sum_func_count+ |
|
2216 |
curr_join->tmp_table_param.func_count; |
|
2217 |
curr_join->tmp_table_param.sum_func_count= |
|
2218 |
curr_join->tmp_table_param.func_count= 0; |
|
2219 |
} |
|
2220 |
else |
|
2221 |
{ |
|
2222 |
curr_join->tmp_table_param.field_count+= |
|
2223 |
curr_join->tmp_table_param.func_count; |
|
2224 |
curr_join->tmp_table_param.func_count= 0; |
|
2225 |
} |
|
2226 |
||
2227 |
if (curr_tmp_table->group) |
|
2228 |
{ // Already grouped |
|
2229 |
if (!curr_join->order && !curr_join->no_order && !skip_sort_order) |
|
2230 |
curr_join->order= curr_join->group_list; /* order by group */ |
|
2231 |
curr_join->group_list= 0; |
|
2232 |
} |
|
2233 |
||
2234 |
/* |
|
2235 |
If we have different sort & group then we must sort the data by group |
|
2236 |
and copy it to another tmp table |
|
2237 |
This code is also used if we are using distinct something |
|
2238 |
we haven't been able to store in the temporary table yet |
|
2239 |
like SEC_TO_TIME(SUM(...)). |
|
2240 |
*/ |
|
2241 |
||
2242 |
if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function)) |
|
2243 |
{ /* Must copy to another table */ |
|
2244 |
/* Free first data from old join */ |
|
2245 |
curr_join->join_free(); |
|
2246 |
if (make_simple_join(curr_join, curr_tmp_table)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2247 |
return; |
1
by brian
clean slate |
2248 |
calc_group_buffer(curr_join, group_list); |
2249 |
count_field_types(select_lex, &curr_join->tmp_table_param, |
|
2250 |
curr_join->tmp_all_fields1, |
|
2251 |
curr_join->select_distinct && !curr_join->group_list); |
|
2252 |
curr_join->tmp_table_param.hidden_field_count= |
|
2253 |
(curr_join->tmp_all_fields1.elements- |
|
2254 |
curr_join->tmp_fields_list1.elements); |
|
2255 |
||
2256 |
||
2257 |
if (exec_tmp_table2) |
|
2258 |
curr_tmp_table= exec_tmp_table2; |
|
2259 |
else |
|
2260 |
{ |
|
2261 |
/* group data to new table */ |
|
2262 |
||
2263 |
/* |
|
2264 |
If the access method is loose index scan then all MIN/MAX |
|
2265 |
functions are precomputed, and should be treated as regular |
|
2266 |
functions. See extended comment in JOIN::exec. |
|
2267 |
*/ |
|
2268 |
if (curr_join->join_tab->is_using_loose_index_scan()) |
|
55
by brian
Update for using real bool types. |
2269 |
curr_join->tmp_table_param.precomputed_group_by= true; |
1
by brian
clean slate |
2270 |
|
2271 |
if (!(curr_tmp_table= |
|
2272 |
exec_tmp_table2= create_tmp_table(thd, |
|
2273 |
&curr_join->tmp_table_param, |
|
2274 |
*curr_all_fields, |
|
2275 |
(ORDER*) 0, |
|
2276 |
curr_join->select_distinct && |
|
2277 |
!curr_join->group_list, |
|
2278 |
1, curr_join->select_options, |
|
2279 |
HA_POS_ERROR, |
|
2280 |
(char *) ""))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2281 |
return; |
1
by brian
clean slate |
2282 |
curr_join->exec_tmp_table2= exec_tmp_table2; |
2283 |
} |
|
2284 |
if (curr_join->group_list) |
|
2285 |
{ |
|
2286 |
thd_proc_info(thd, "Creating sort index"); |
|
2287 |
if (curr_join->join_tab == join_tab && save_join_tab()) |
|
2288 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2289 |
return; |
1
by brian
clean slate |
2290 |
} |
2291 |
if (create_sort_index(thd, curr_join, curr_join->group_list, |
|
55
by brian
Update for using real bool types. |
2292 |
HA_POS_ERROR, HA_POS_ERROR, false) || |
1
by brian
clean slate |
2293 |
make_group_fields(this, curr_join)) |
2294 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2295 |
return; |
1
by brian
clean slate |
2296 |
} |
2297 |
sortorder= curr_join->sortorder; |
|
2298 |
} |
|
2299 |
||
2300 |
thd_proc_info(thd, "Copying to group table"); |
|
2301 |
tmp_error= -1; |
|
2302 |
if (curr_join != this) |
|
2303 |
{ |
|
2304 |
if (sum_funcs2) |
|
2305 |
{ |
|
2306 |
curr_join->sum_funcs= sum_funcs2; |
|
2307 |
curr_join->sum_funcs_end= sum_funcs_end2; |
|
2308 |
} |
|
2309 |
else |
|
2310 |
{ |
|
2311 |
curr_join->alloc_func_list(); |
|
2312 |
sum_funcs2= curr_join->sum_funcs; |
|
2313 |
sum_funcs_end2= curr_join->sum_funcs_end; |
|
2314 |
} |
|
2315 |
} |
|
2316 |
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, |
|
55
by brian
Update for using real bool types. |
2317 |
1, true)) |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2318 |
return; |
1
by brian
clean slate |
2319 |
curr_join->group_list= 0; |
2320 |
if (!curr_join->sort_and_group && |
|
2321 |
curr_join->const_tables != curr_join->tables) |
|
2322 |
curr_join->join_tab[curr_join->const_tables].sorted= 0; |
|
2323 |
if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) || |
|
2324 |
(tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table))) |
|
2325 |
{ |
|
2326 |
error= tmp_error; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2327 |
return; |
1
by brian
clean slate |
2328 |
} |
2329 |
end_read_record(&curr_join->join_tab->read_record); |
|
2330 |
curr_join->const_tables= curr_join->tables; // Mark free for cleanup() |
|
2331 |
curr_join->join_tab[0].table= 0; // Table is freed |
|
2332 |
||
2333 |
// No sum funcs anymore |
|
2334 |
if (!items2) |
|
2335 |
{ |
|
2336 |
items2= items1 + all_fields.elements; |
|
2337 |
if (change_to_use_tmp_fields(thd, items2, |
|
2338 |
tmp_fields_list2, tmp_all_fields2, |
|
2339 |
fields_list.elements, tmp_all_fields1)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2340 |
return; |
1
by brian
clean slate |
2341 |
curr_join->tmp_fields_list2= tmp_fields_list2; |
2342 |
curr_join->tmp_all_fields2= tmp_all_fields2; |
|
2343 |
} |
|
2344 |
curr_fields_list= &curr_join->tmp_fields_list2; |
|
2345 |
curr_all_fields= &curr_join->tmp_all_fields2; |
|
2346 |
curr_join->set_items_ref_array(items2); |
|
2347 |
curr_join->tmp_table_param.field_count+= |
|
2348 |
curr_join->tmp_table_param.sum_func_count; |
|
2349 |
curr_join->tmp_table_param.sum_func_count= 0; |
|
2350 |
} |
|
2351 |
if (curr_tmp_table->distinct) |
|
2352 |
curr_join->select_distinct=0; /* Each row is unique */ |
|
2353 |
||
2354 |
curr_join->join_free(); /* Free quick selects */ |
|
2355 |
if (curr_join->select_distinct && ! curr_join->group_list) |
|
2356 |
{ |
|
2357 |
thd_proc_info(thd, "Removing duplicates"); |
|
2358 |
if (curr_join->tmp_having) |
|
2359 |
curr_join->tmp_having->update_used_tables(); |
|
2360 |
if (remove_duplicates(curr_join, curr_tmp_table, |
|
2361 |
*curr_fields_list, curr_join->tmp_having)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2362 |
return; |
1
by brian
clean slate |
2363 |
curr_join->tmp_having=0; |
2364 |
curr_join->select_distinct=0; |
|
2365 |
} |
|
2366 |
curr_tmp_table->reginfo.lock_type= TL_UNLOCK; |
|
2367 |
if (make_simple_join(curr_join, curr_tmp_table)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2368 |
return; |
1
by brian
clean slate |
2369 |
calc_group_buffer(curr_join, curr_join->group_list); |
2370 |
count_field_types(select_lex, &curr_join->tmp_table_param, |
|
2371 |
*curr_all_fields, 0); |
|
2372 |
||
2373 |
} |
|
2374 |
||
2375 |
if (curr_join->group || curr_join->tmp_table_param.sum_func_count) |
|
2376 |
{ |
|
2377 |
if (make_group_fields(this, curr_join)) |
|
2378 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2379 |
return; |
1
by brian
clean slate |
2380 |
} |
2381 |
if (!items3) |
|
2382 |
{ |
|
2383 |
if (!items0) |
|
2384 |
init_items_ref_array(); |
|
2385 |
items3= ref_pointer_array + (all_fields.elements*4); |
|
2386 |
setup_copy_fields(thd, &curr_join->tmp_table_param, |
|
2387 |
items3, tmp_fields_list3, tmp_all_fields3, |
|
2388 |
curr_fields_list->elements, *curr_all_fields); |
|
2389 |
tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs; |
|
2390 |
tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field; |
|
2391 |
tmp_table_param.save_copy_field_end= |
|
2392 |
curr_join->tmp_table_param.copy_field_end; |
|
2393 |
curr_join->tmp_all_fields3= tmp_all_fields3; |
|
2394 |
curr_join->tmp_fields_list3= tmp_fields_list3; |
|
2395 |
} |
|
2396 |
else |
|
2397 |
{ |
|
2398 |
curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs; |
|
2399 |
curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field; |
|
2400 |
curr_join->tmp_table_param.copy_field_end= |
|
2401 |
tmp_table_param.save_copy_field_end; |
|
2402 |
} |
|
2403 |
curr_fields_list= &tmp_fields_list3; |
|
2404 |
curr_all_fields= &tmp_all_fields3; |
|
2405 |
curr_join->set_items_ref_array(items3); |
|
2406 |
||
2407 |
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, |
|
55
by brian
Update for using real bool types. |
2408 |
1, true) || |
1
by brian
clean slate |
2409 |
setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) || |
2410 |
thd->is_fatal_error) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2411 |
return; |
1
by brian
clean slate |
2412 |
} |
2413 |
if (curr_join->group_list || curr_join->order) |
|
2414 |
{ |
|
2415 |
thd_proc_info(thd, "Sorting result"); |
|
2416 |
/* If we have already done the group, add HAVING to sorted table */ |
|
2417 |
if (curr_join->tmp_having && ! curr_join->group_list && |
|
2418 |
! curr_join->sort_and_group) |
|
2419 |
{ |
|
2420 |
// Some tables may have been const |
|
2421 |
curr_join->tmp_having->update_used_tables(); |
|
2422 |
JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables]; |
|
2423 |
table_map used_tables= (curr_join->const_table_map | |
|
2424 |
curr_table->table->map); |
|
2425 |
||
2426 |
Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having, |
|
2427 |
used_tables, |
|
2428 |
used_tables, 0); |
|
2429 |
if (sort_table_cond) |
|
2430 |
{ |
|
2431 |
if (!curr_table->select) |
|
2432 |
if (!(curr_table->select= new SQL_SELECT)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2433 |
return; |
1
by brian
clean slate |
2434 |
if (!curr_table->select->cond) |
2435 |
curr_table->select->cond= sort_table_cond; |
|
2436 |
else // This should never happen |
|
2437 |
{ |
|
2438 |
if (!(curr_table->select->cond= |
|
2439 |
new Item_cond_and(curr_table->select->cond, |
|
2440 |
sort_table_cond))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2441 |
return; |
1
by brian
clean slate |
2442 |
/* |
2443 |
Item_cond_and do not need fix_fields for execution, its parameters |
|
2444 |
are fixed or do not need fix_fields, too |
|
2445 |
*/ |
|
2446 |
curr_table->select->cond->quick_fix_field(); |
|
2447 |
} |
|
2448 |
curr_table->select_cond= curr_table->select->cond; |
|
2449 |
curr_table->select_cond->top_level_item(); |
|
2450 |
curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having, |
|
2451 |
~ (table_map) 0, |
|
2452 |
~used_tables, 0); |
|
2453 |
} |
|
2454 |
} |
|
2455 |
{ |
|
2456 |
if (group) |
|
2457 |
curr_join->select_limit= HA_POS_ERROR; |
|
2458 |
else |
|
2459 |
{ |
|
2460 |
/* |
|
2461 |
We can abort sorting after thd->select_limit rows if we there is no |
|
2462 |
WHERE clause for any tables after the sorted one. |
|
2463 |
*/ |
|
2464 |
JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1]; |
|
2465 |
JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables]; |
|
2466 |
for (; curr_table < end_table ; curr_table++) |
|
2467 |
{ |
|
2468 |
/* |
|
2469 |
table->keyuse is set in the case there was an original WHERE clause |
|
2470 |
on the table that was optimized away. |
|
2471 |
*/ |
|
2472 |
if (curr_table->select_cond || |
|
2473 |
(curr_table->keyuse && !curr_table->first_inner)) |
|
2474 |
{ |
|
2475 |
/* We have to sort all rows */ |
|
2476 |
curr_join->select_limit= HA_POS_ERROR; |
|
2477 |
break; |
|
2478 |
} |
|
2479 |
} |
|
2480 |
} |
|
2481 |
if (curr_join->join_tab == join_tab && save_join_tab()) |
|
2482 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2483 |
return; |
1
by brian
clean slate |
2484 |
} |
2485 |
/* |
|
2486 |
Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser |
|
2487 |
chose FILESORT to be faster than INDEX SCAN or there is no |
|
2488 |
suitable index present. |
|
2489 |
Note, that create_sort_index calls test_if_skip_sort_order and may |
|
2490 |
finally replace sorting with index scan if there is a LIMIT clause in |
|
2491 |
the query. XXX: it's never shown in EXPLAIN! |
|
2492 |
OPTION_FOUND_ROWS supersedes LIMIT and is taken into account. |
|
2493 |
*/ |
|
2494 |
if (create_sort_index(thd, curr_join, |
|
2495 |
curr_join->group_list ? |
|
2496 |
curr_join->group_list : curr_join->order, |
|
2497 |
curr_join->select_limit, |
|
2498 |
(select_options & OPTION_FOUND_ROWS ? |
|
2499 |
HA_POS_ERROR : unit->select_limit_cnt), |
|
55
by brian
Update for using real bool types. |
2500 |
curr_join->group_list ? true : false)) |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2501 |
return; |
1
by brian
clean slate |
2502 |
sortorder= curr_join->sortorder; |
2503 |
if (curr_join->const_tables != curr_join->tables && |
|
2504 |
!curr_join->join_tab[curr_join->const_tables].table->sort.io_cache) |
|
2505 |
{ |
|
2506 |
/* |
|
2507 |
If no IO cache exists for the first table then we are using an |
|
2508 |
INDEX SCAN and no filesort. Thus we should not remove the sorted |
|
2509 |
attribute on the INDEX SCAN. |
|
2510 |
*/ |
|
2511 |
skip_sort_order= 1; |
|
2512 |
} |
|
2513 |
} |
|
2514 |
} |
|
2515 |
/* XXX: When can we have here thd->is_error() not zero? */ |
|
2516 |
if (thd->is_error()) |
|
2517 |
{ |
|
2518 |
error= thd->is_error(); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2519 |
return; |
1
by brian
clean slate |
2520 |
} |
2521 |
curr_join->having= curr_join->tmp_having; |
|
2522 |
curr_join->fields= curr_fields_list; |
|
2523 |
||
2524 |
{ |
|
2525 |
thd_proc_info(thd, "Sending data"); |
|
2526 |
result->send_fields(*curr_fields_list, |
|
2527 |
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF); |
|
2528 |
error= do_select(curr_join, curr_fields_list, NULL); |
|
2529 |
thd->limit_found_rows= curr_join->send_records; |
|
2530 |
} |
|
2531 |
||
2532 |
/* Accumulate the counts from all join iterations of all join parts. */ |
|
2533 |
thd->examined_row_count+= curr_join->examined_rows; |
|
2534 |
||
2535 |
/* |
|
2536 |
With EXPLAIN EXTENDED we have to restore original ref_array |
|
2537 |
for a derived table which is always materialized. |
|
2538 |
Otherwise we would not be able to print the query correctly. |
|
2539 |
*/ |
|
2540 |
if (items0 && |
|
2541 |
(thd->lex->describe & DESCRIBE_EXTENDED) && |
|
2542 |
select_lex->linkage == DERIVED_TABLE_TYPE) |
|
2543 |
set_items_ref_array(items0); |
|
2544 |
||
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2545 |
return; |
1
by brian
clean slate |
2546 |
} |
2547 |
||
2548 |
||
2549 |
/** |
|
2550 |
Clean up join. |
|
2551 |
||
2552 |
@return |
|
2553 |
Return error that hold JOIN. |
|
2554 |
*/ |
|
2555 |
||
2556 |
int |
|
2557 |
JOIN::destroy() |
|
2558 |
{ |
|
2559 |
select_lex->join= 0; |
|
2560 |
||
2561 |
if (tmp_join) |
|
2562 |
{ |
|
2563 |
if (join_tab != tmp_join->join_tab) |
|
2564 |
{ |
|
2565 |
JOIN_TAB *tab, *end; |
|
2566 |
for (tab= join_tab, end= tab+tables ; tab != end ; tab++) |
|
2567 |
tab->cleanup(); |
|
2568 |
} |
|
2569 |
tmp_join->tmp_join= 0; |
|
2570 |
tmp_table_param.copy_field=0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2571 |
return(tmp_join->destroy()); |
1
by brian
clean slate |
2572 |
} |
2573 |
cond_equal= 0; |
|
2574 |
||
2575 |
cleanup(1); |
|
2576 |
if (exec_tmp_table1) |
|
2577 |
free_tmp_table(thd, exec_tmp_table1); |
|
2578 |
if (exec_tmp_table2) |
|
2579 |
free_tmp_table(thd, exec_tmp_table2); |
|
2580 |
delete select; |
|
2581 |
delete_dynamic(&keyuse); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2582 |
return(error); |
1
by brian
clean slate |
2583 |
} |
2584 |
||
2585 |
||
2586 |
||
2587 |
/** |
|
2588 |
An entry point to single-unit select (a select without UNION). |
|
2589 |
||
2590 |
@param thd thread handler |
|
2591 |
@param rref_pointer_array a reference to ref_pointer_array of |
|
2592 |
the top-level select_lex for this query |
|
2593 |
@param tables list of all tables used in this query. |
|
2594 |
The tables have been pre-opened. |
|
2595 |
@param wild_num number of wildcards used in the top level |
|
2596 |
select of this query. |
|
2597 |
For example statement |
|
2598 |
SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2; |
|
2599 |
has 3 wildcards. |
|
2600 |
@param fields list of items in SELECT list of the top-level |
|
2601 |
select |
|
2602 |
e.g. SELECT a, b, c FROM t1 will have Item_field |
|
2603 |
for a, b and c in this list. |
|
2604 |
@param conds top level item of an expression representing |
|
2605 |
WHERE clause of the top level select |
|
2606 |
@param og_num total number of ORDER BY and GROUP BY clauses |
|
2607 |
arguments |
|
2608 |
@param order linked list of ORDER BY agruments |
|
2609 |
@param group linked list of GROUP BY arguments |
|
2610 |
@param having top level item of HAVING expression |
|
2611 |
@param proc_param list of PROCEDUREs |
|
2612 |
@param select_options select options (BIG_RESULT, etc) |
|
2613 |
@param result an instance of result set handling class. |
|
2614 |
This object is responsible for send result |
|
2615 |
set rows to the client or inserting them |
|
2616 |
into a table. |
|
2617 |
@param select_lex the only SELECT_LEX of this query |
|
2618 |
@param unit top-level UNIT of this query |
|
2619 |
UNIT is an artificial object created by the |
|
2620 |
parser for every SELECT clause. |
|
2621 |
e.g. |
|
2622 |
SELECT * FROM t1 WHERE a1 IN (SELECT * FROM t2) |
|
2623 |
has 2 unions. |
|
2624 |
||
2625 |
@retval |
|
55
by brian
Update for using real bool types. |
2626 |
false success |
1
by brian
clean slate |
2627 |
@retval |
55
by brian
Update for using real bool types. |
2628 |
true an error |
1
by brian
clean slate |
2629 |
*/ |
2630 |
||
2631 |
bool |
|
2632 |
mysql_select(THD *thd, Item ***rref_pointer_array, |
|
2633 |
TABLE_LIST *tables, uint wild_num, List<Item> &fields, |
|
2634 |
COND *conds, uint og_num, ORDER *order, ORDER *group, |
|
151
by Brian Aker
Ulonglong to uint64_t |
2635 |
Item *having, ORDER *proc_param, uint64_t select_options, |
1
by brian
clean slate |
2636 |
select_result *result, SELECT_LEX_UNIT *unit, |
2637 |
SELECT_LEX *select_lex) |
|
2638 |
{ |
|
2639 |
bool err; |
|
2640 |
bool free_join= 1; |
|
2641 |
||
55
by brian
Update for using real bool types. |
2642 |
select_lex->context.resolve_in_select_list= true; |
1
by brian
clean slate |
2643 |
JOIN *join; |
2644 |
if (select_lex->join != 0) |
|
2645 |
{ |
|
2646 |
join= select_lex->join; |
|
2647 |
/* |
|
2648 |
is it single SELECT in derived table, called in derived table |
|
2649 |
creation |
|
2650 |
*/ |
|
2651 |
if (select_lex->linkage != DERIVED_TABLE_TYPE || |
|
2652 |
(select_options & SELECT_DESCRIBE)) |
|
2653 |
{ |
|
2654 |
if (select_lex->linkage != GLOBAL_OPTIONS_TYPE) |
|
2655 |
{ |
|
2656 |
//here is EXPLAIN of subselect or derived table |
|
2657 |
if (join->change_result(result)) |
|
2658 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2659 |
return(true); |
1
by brian
clean slate |
2660 |
} |
2661 |
} |
|
2662 |
else |
|
2663 |
{ |
|
2664 |
if ((err= join->prepare(rref_pointer_array, tables, wild_num, |
|
2665 |
conds, og_num, order, group, having, proc_param, |
|
2666 |
select_lex, unit))) |
|
2667 |
{ |
|
2668 |
goto err; |
|
2669 |
} |
|
2670 |
} |
|
2671 |
} |
|
2672 |
free_join= 0; |
|
2673 |
join->select_options= select_options; |
|
2674 |
} |
|
2675 |
else |
|
2676 |
{ |
|
2677 |
if (!(join= new JOIN(thd, fields, select_options, result))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2678 |
return(true); |
1
by brian
clean slate |
2679 |
thd_proc_info(thd, "init"); |
2680 |
thd->used_tables=0; // Updated by setup_fields |
|
2681 |
if ((err= join->prepare(rref_pointer_array, tables, wild_num, |
|
2682 |
conds, og_num, order, group, having, proc_param, |
|
2683 |
select_lex, unit)) == true) |
|
2684 |
{ |
|
2685 |
goto err; |
|
2686 |
} |
|
2687 |
} |
|
2688 |
||
2689 |
/* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */ |
|
2690 |
if (join->flatten_subqueries()) |
|
2691 |
{ |
|
2692 |
err= 1; |
|
2693 |
goto err; |
|
2694 |
} |
|
2695 |
/* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */ |
|
2696 |
||
2697 |
if ((err= join->optimize())) |
|
2698 |
{ |
|
2699 |
goto err; // 1 |
|
2700 |
} |
|
2701 |
||
2702 |
if (thd->lex->describe & DESCRIBE_EXTENDED) |
|
2703 |
{ |
|
2704 |
join->conds_history= join->conds; |
|
2705 |
join->having_history= (join->having?join->having:join->tmp_having); |
|
2706 |
} |
|
2707 |
||
2708 |
if (thd->is_error()) |
|
2709 |
goto err; |
|
2710 |
||
2711 |
join->exec(); |
|
2712 |
||
2713 |
if (thd->lex->describe & DESCRIBE_EXTENDED) |
|
2714 |
{ |
|
2715 |
select_lex->where= join->conds_history; |
|
2716 |
select_lex->having= join->having_history; |
|
2717 |
} |
|
2718 |
||
2719 |
err: |
|
2720 |
if (free_join) |
|
2721 |
{ |
|
2722 |
thd_proc_info(thd, "end"); |
|
2723 |
err|= select_lex->cleanup(); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2724 |
return(err || thd->is_error()); |
1
by brian
clean slate |
2725 |
} |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2726 |
return(join->error); |
1
by brian
clean slate |
2727 |
} |
2728 |
||
2729 |
||
2730 |
int subq_sj_candidate_cmp(Item_in_subselect* const *el1, |
|
2731 |
Item_in_subselect* const *el2) |
|
2732 |
{ |
|
2733 |
return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 : |
|
2734 |
( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1); |
|
2735 |
} |
|
2736 |
||
2737 |
||
2738 |
inline Item * and_items(Item* cond, Item *item) |
|
2739 |
{ |
|
2740 |
return (cond? (new Item_cond_and(cond, item)) : item); |
|
2741 |
} |
|
2742 |
||
2743 |
||
2744 |
static TABLE_LIST *alloc_join_nest(THD *thd) |
|
2745 |
{ |
|
2746 |
TABLE_LIST *tbl; |
|
2747 |
if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+ |
|
2748 |
sizeof(NESTED_JOIN)))) |
|
2749 |
return NULL; |
|
2750 |
tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl + |
|
2751 |
ALIGN_SIZE(sizeof(TABLE_LIST))); |
|
2752 |
return tbl; |
|
2753 |
} |
|
2754 |
||
2755 |
||
2756 |
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist) |
|
2757 |
{ |
|
2758 |
List_iterator<TABLE_LIST> it(*tlist); |
|
2759 |
TABLE_LIST *table; |
|
2760 |
while ((table= it++)) |
|
2761 |
{ |
|
2762 |
if (table->on_expr) |
|
2763 |
table->on_expr->fix_after_pullout(new_parent, &table->on_expr); |
|
2764 |
if (table->nested_join) |
|
2765 |
fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list); |
|
2766 |
} |
|
2767 |
} |
|
2768 |
||
2769 |
||
2770 |
/* |
|
2771 |
Convert a subquery predicate into a TABLE_LIST semi-join nest |
|
2772 |
||
2773 |
SYNOPSIS |
|
2774 |
convert_subq_to_sj() |
|
2775 |
parent_join Parent join, the one that has subq_pred in its WHERE/ON |
|
2776 |
clause |
|
2777 |
subq_pred Subquery predicate to be converted |
|
2778 |
||
2779 |
DESCRIPTION |
|
2780 |
Convert a subquery predicate into a TABLE_LIST semi-join nest. All the |
|
2781 |
prerequisites are already checked, so the conversion is always successfull. |
|
2782 |
||
2783 |
Prepared Statements: the transformation is permanent: |
|
2784 |
- Changes in TABLE_LIST structures are naturally permanent |
|
2785 |
- Item tree changes are performed on statement MEM_ROOT: |
|
2786 |
= we activate statement MEM_ROOT |
|
2787 |
= this function is called before the first fix_prepare_information |
|
2788 |
call. |
|
2789 |
||
2790 |
This is intended because the criteria for subquery-to-sj conversion remain |
|
2791 |
constant for the lifetime of the Prepared Statement. |
|
2792 |
||
2793 |
RETURN |
|
55
by brian
Update for using real bool types. |
2794 |
false OK |
2795 |
true Out of memory error |
|
1
by brian
clean slate |
2796 |
*/ |
2797 |
||
2798 |
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred) |
|
2799 |
{ |
|
2800 |
SELECT_LEX *parent_lex= parent_join->select_lex; |
|
2801 |
TABLE_LIST *emb_tbl_nest= NULL; |
|
2802 |
List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list; |
|
2803 |
THD *thd= parent_join->thd; |
|
2804 |
||
2805 |
/* |
|
2806 |
1. Find out where to put the predicate into. |
|
2807 |
Note: for "t1 LEFT JOIN t2" this will be t2, a leaf. |
|
2808 |
*/ |
|
2809 |
if ((void*)subq_pred->expr_join_nest != (void*)1) |
|
2810 |
{ |
|
2811 |
if (subq_pred->expr_join_nest->nested_join) |
|
2812 |
{ |
|
2813 |
/* |
|
2814 |
We're dealing with |
|
2815 |
||
2816 |
... [LEFT] JOIN ( ... ) ON (subquery AND whatever) ... |
|
2817 |
||
2818 |
The sj-nest will be inserted into the brackets nest. |
|
2819 |
*/ |
|
2820 |
emb_tbl_nest= subq_pred->expr_join_nest; |
|
2821 |
emb_join_list= &emb_tbl_nest->nested_join->join_list; |
|
2822 |
} |
|
2823 |
else if (!subq_pred->expr_join_nest->outer_join) |
|
2824 |
{ |
|
2825 |
/* |
|
2826 |
We're dealing with |
|
2827 |
||
2828 |
... INNER JOIN tblX ON (subquery AND whatever) ... |
|
2829 |
||
2830 |
The sj-nest will be tblX's "sibling", i.e. another child of its |
|
2831 |
parent. This is ok because tblX is joined as an inner join. |
|
2832 |
*/ |
|
2833 |
emb_tbl_nest= subq_pred->expr_join_nest->embedding; |
|
2834 |
if (emb_tbl_nest) |
|
2835 |
emb_join_list= &emb_tbl_nest->nested_join->join_list; |
|
2836 |
} |
|
2837 |
else if (!subq_pred->expr_join_nest->nested_join) |
|
2838 |
{ |
|
2839 |
TABLE_LIST *outer_tbl= subq_pred->expr_join_nest; |
|
2840 |
TABLE_LIST *wrap_nest; |
|
2841 |
/* |
|
2842 |
We're dealing with |
|
2843 |
||
2844 |
... LEFT JOIN tbl ON (on_expr AND subq_pred) ... |
|
2845 |
||
2846 |
we'll need to convert it into: |
|
2847 |
||
2848 |
... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ... |
|
2849 |
| | |
|
2850 |
|<----- wrap_nest ---->| |
|
2851 |
||
2852 |
Q: other subqueries may be pointing to this element. What to do? |
|
2853 |
A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest. |
|
2854 |
But we'll need to fix other pointers. |
|
2855 |
A2: Another way: have TABLE_LIST::next_ptr so the following |
|
2856 |
subqueries know the table has been nested. |
|
2857 |
A3: changes in the TABLE_LIST::outer_join will make everything work |
|
2858 |
automatically. |
|
2859 |
*/ |
|
2860 |
if (!(wrap_nest= alloc_join_nest(parent_join->thd))) |
|
2861 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2862 |
return(true); |
1
by brian
clean slate |
2863 |
} |
2864 |
wrap_nest->embedding= outer_tbl->embedding; |
|
2865 |
wrap_nest->join_list= outer_tbl->join_list; |
|
2866 |
wrap_nest->alias= (char*) "(sj-wrap)"; |
|
2867 |
||
2868 |
wrap_nest->nested_join->join_list.empty(); |
|
2869 |
wrap_nest->nested_join->join_list.push_back(outer_tbl); |
|
2870 |
||
2871 |
outer_tbl->embedding= wrap_nest; |
|
2872 |
outer_tbl->join_list= &wrap_nest->nested_join->join_list; |
|
2873 |
||
2874 |
/* |
|
2875 |
wrap_nest will take place of outer_tbl, so move the outer join flag |
|
2876 |
and on_expr |
|
2877 |
*/ |
|
2878 |
wrap_nest->outer_join= outer_tbl->outer_join; |
|
2879 |
outer_tbl->outer_join= 0; |
|
2880 |
||
2881 |
wrap_nest->on_expr= outer_tbl->on_expr; |
|
2882 |
outer_tbl->on_expr= NULL; |
|
2883 |
||
2884 |
List_iterator<TABLE_LIST> li(*wrap_nest->join_list); |
|
2885 |
TABLE_LIST *tbl; |
|
2886 |
while ((tbl= li++)) |
|
2887 |
{ |
|
2888 |
if (tbl == outer_tbl) |
|
2889 |
{ |
|
2890 |
li.replace(wrap_nest); |
|
2891 |
break; |
|
2892 |
} |
|
2893 |
} |
|
2894 |
/* |
|
2895 |
Ok now wrap_nest 'contains' outer_tbl and we're ready to add the |
|
2896 |
semi-join nest into it |
|
2897 |
*/ |
|
2898 |
emb_join_list= &wrap_nest->nested_join->join_list; |
|
2899 |
emb_tbl_nest= wrap_nest; |
|
2900 |
} |
|
2901 |
} |
|
2902 |
||
2903 |
TABLE_LIST *sj_nest; |
|
2904 |
NESTED_JOIN *nested_join; |
|
2905 |
if (!(sj_nest= alloc_join_nest(parent_join->thd))) |
|
2906 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2907 |
return(true); |
1
by brian
clean slate |
2908 |
} |
2909 |
nested_join= sj_nest->nested_join; |
|
2910 |
||
2911 |
sj_nest->join_list= emb_join_list; |
|
2912 |
sj_nest->embedding= emb_tbl_nest; |
|
2913 |
sj_nest->alias= (char*) "(sj-nest)"; |
|
2914 |
/* Nests do not participate in those 'chains', so: */ |
|
2915 |
/* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/ |
|
2916 |
emb_join_list->push_back(sj_nest); |
|
2917 |
||
2918 |
/* |
|
2919 |
nested_join->used_tables and nested_join->not_null_tables are |
|
2920 |
initialized in simplify_joins(). |
|
2921 |
*/ |
|
2922 |
||
2923 |
/* |
|
2924 |
2. Walk through subquery's top list and set 'embedding' to point to the |
|
2925 |
sj-nest. |
|
2926 |
*/ |
|
2927 |
st_select_lex *subq_lex= subq_pred->unit->first_select(); |
|
2928 |
nested_join->join_list.empty(); |
|
2929 |
List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list); |
|
2930 |
TABLE_LIST *tl, *last_leaf; |
|
2931 |
while ((tl= li++)) |
|
2932 |
{ |
|
2933 |
tl->embedding= sj_nest; |
|
2934 |
tl->join_list= &nested_join->join_list; |
|
2935 |
nested_join->join_list.push_back(tl); |
|
2936 |
} |
|
2937 |
||
2938 |
/* |
|
2939 |
Reconnect the next_leaf chain. |
|
2940 |
TODO: Do we have to put subquery's tables at the end of the chain? |
|
2941 |
Inserting them at the beginning would be a bit faster. |
|
2942 |
NOTE: We actually insert them at the front! That's because the order is |
|
2943 |
reversed in this list. |
|
2944 |
*/ |
|
2945 |
for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {}; |
|
2946 |
tl->next_leaf= subq_lex->leaf_tables; |
|
2947 |
last_leaf= tl; |
|
2948 |
||
2949 |
/* |
|
2950 |
Same as above for next_local chain |
|
2951 |
(a theory: a next_local chain always starts with ::leaf_tables |
|
2952 |
because view's tables are inserted after the view) |
|
2953 |
*/ |
|
2954 |
for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {}; |
|
2955 |
tl->next_local= subq_lex->leaf_tables; |
|
2956 |
||
2957 |
/* A theory: no need to re-connect the next_global chain */ |
|
2958 |
||
2959 |
/* 3. Remove the original subquery predicate from the WHERE/ON */ |
|
2960 |
||
2961 |
// The subqueries were replaced for Item_int(1) earlier |
|
2962 |
subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions |
|
2963 |
/*TODO: also reset the 'with_subselect' there. */ |
|
2964 |
||
2965 |
/* n. Adjust the parent_join->tables counter */ |
|
2966 |
uint table_no= parent_join->tables; |
|
2967 |
/* n. Walk through child's tables and adjust table->map */ |
|
2968 |
for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++) |
|
2969 |
{ |
|
2970 |
tl->table->tablenr= table_no; |
|
2971 |
tl->table->map= ((table_map)1) << table_no; |
|
2972 |
SELECT_LEX *old_sl= tl->select_lex; |
|
2973 |
tl->select_lex= parent_join->select_lex; |
|
2974 |
for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding) |
|
2975 |
emb->select_lex= parent_join->select_lex; |
|
2976 |
} |
|
2977 |
parent_join->tables += subq_lex->join->tables; |
|
2978 |
||
2979 |
/* |
|
2980 |
Put the subquery's WHERE into semi-join's sj_on_expr |
|
2981 |
Add the subquery-induced equalities too. |
|
2982 |
*/ |
|
2983 |
SELECT_LEX *save_lex= thd->lex->current_select; |
|
2984 |
thd->lex->current_select=subq_lex; |
|
2985 |
if (!subq_pred->left_expr->fixed && |
|
2986 |
subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
2987 |
return(true); |
1
by brian
clean slate |
2988 |
thd->lex->current_select=save_lex; |
2989 |
||
2990 |
sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables(); |
|
2991 |
sj_nest->nested_join->sj_depends_on= subq_pred->used_tables() | |
|
2992 |
subq_pred->left_expr->used_tables(); |
|
2993 |
sj_nest->sj_on_expr= subq_lex->where; |
|
2994 |
||
2995 |
/* |
|
2996 |
Create the IN-equalities and inject them into semi-join's ON expression. |
|
2997 |
Additionally, for InsideOut strategy |
|
2998 |
- Record the number of IN-equalities. |
|
2999 |
- Create list of pointers to (oe1, ..., ieN). We'll need the list to |
|
3000 |
see which of the expressions are bound and which are not (for those |
|
3001 |
we'll produce a distinct stream of (ie_i1,...ie_ik). |
|
3002 |
||
3003 |
(TODO: can we just create a list of pointers and hope the expressions |
|
3004 |
will not substitute themselves on fix_fields()? or we need to wrap |
|
3005 |
them into Item_direct_view_refs and store pointers to those. The |
|
3006 |
pointers to Item_direct_view_refs are guaranteed to be stable as |
|
3007 |
Item_direct_view_refs doesn't substitute itself with anything in |
|
3008 |
Item_direct_view_ref::fix_fields. |
|
3009 |
*/ |
|
3010 |
sj_nest->sj_in_exprs= subq_pred->left_expr->cols(); |
|
3011 |
sj_nest->nested_join->sj_outer_expr_list.empty(); |
|
3012 |
||
3013 |
if (subq_pred->left_expr->cols() == 1) |
|
3014 |
{ |
|
3015 |
nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr); |
|
3016 |
||
3017 |
Item *item_eq= new Item_func_eq(subq_pred->left_expr, |
|
3018 |
subq_lex->ref_pointer_array[0]); |
|
3019 |
item_eq->name= (char*)subq_sj_cond_name; |
|
3020 |
sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq); |
|
3021 |
} |
|
3022 |
else |
|
3023 |
{ |
|
3024 |
for (uint i= 0; i < subq_pred->left_expr->cols(); i++) |
|
3025 |
{ |
|
3026 |
nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr-> |
|
3027 |
element_index(i)); |
|
3028 |
Item *item_eq= |
|
3029 |
new Item_func_eq(subq_pred->left_expr->element_index(i), |
|
3030 |
subq_lex->ref_pointer_array[i]); |
|
3031 |
item_eq->name= (char*)subq_sj_cond_name + (i % 64); |
|
3032 |
sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq); |
|
3033 |
} |
|
3034 |
} |
|
3035 |
/* Fix the created equality and AND */ |
|
3036 |
sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr); |
|
3037 |
||
3038 |
/* |
|
3039 |
Walk through sj nest's WHERE and ON expressions and call |
|
3040 |
item->fix_table_changes() for all items. |
|
3041 |
*/ |
|
3042 |
sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr); |
|
3043 |
fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list); |
|
3044 |
||
3045 |
||
3046 |
/* Unlink the child select_lex so it doesn't show up in EXPLAIN: */ |
|
3047 |
subq_lex->master_unit()->exclude_level(); |
|
3048 |
||
3049 |
/* Inject sj_on_expr into the parent's WHERE or ON */ |
|
3050 |
if (emb_tbl_nest) |
|
3051 |
{ |
|
3052 |
emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr, |
|
3053 |
sj_nest->sj_on_expr); |
|
3054 |
emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr); |
|
3055 |
} |
|
3056 |
else |
|
3057 |
{ |
|
3058 |
/* Inject into the WHERE */ |
|
3059 |
parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr); |
|
3060 |
parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds); |
|
3061 |
parent_join->select_lex->where= parent_join->conds; |
|
3062 |
} |
|
3063 |
||
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3064 |
return(false); |
1
by brian
clean slate |
3065 |
} |
3066 |
||
3067 |
||
3068 |
/* |
|
3069 |
Convert candidate subquery predicates to semi-joins |
|
3070 |
||
3071 |
SYNOPSIS |
|
3072 |
JOIN::flatten_subqueries() |
|
3073 |
||
3074 |
DESCRIPTION |
|
3075 |
Convert candidate subquery predicates to semi-joins. |
|
3076 |
||
3077 |
RETURN |
|
55
by brian
Update for using real bool types. |
3078 |
false OK |
3079 |
true Error |
|
1
by brian
clean slate |
3080 |
*/ |
3081 |
||
3082 |
bool JOIN::flatten_subqueries() |
|
3083 |
{ |
|
3084 |
Item_in_subselect **in_subq; |
|
3085 |
Item_in_subselect **in_subq_end; |
|
3086 |
||
3087 |
if (sj_subselects.elements() == 0) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3088 |
return(false); |
1
by brian
clean slate |
3089 |
|
3090 |
/* 1. Fix children subqueries */ |
|
3091 |
for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); |
|
3092 |
in_subq != in_subq_end; in_subq++) |
|
3093 |
{ |
|
3094 |
JOIN *child_join= (*in_subq)->unit->first_select()->join; |
|
3095 |
child_join->outer_tables = child_join->tables; |
|
3096 |
if (child_join->flatten_subqueries()) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3097 |
return(true); |
1
by brian
clean slate |
3098 |
(*in_subq)->sj_convert_priority= |
3099 |
(*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables; |
|
3100 |
} |
|
3101 |
||
3102 |
//dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); |
|
3103 |
/* |
|
3104 |
2. Pick which subqueries to convert: |
|
3105 |
sort the subquery array |
|
3106 |
- prefer correlated subqueries over uncorrelated; |
|
3107 |
- prefer subqueries that have greater number of outer tables; |
|
3108 |
*/ |
|
3109 |
sj_subselects.sort(subq_sj_candidate_cmp); |
|
3110 |
// #tables-in-parent-query + #tables-in-subquery < MAX_TABLES |
|
3111 |
/* Replace all subqueries to be flattened with Item_int(1) */ |
|
3112 |
for (in_subq= sj_subselects.front(); |
|
3113 |
in_subq != in_subq_end && |
|
3114 |
tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES; |
|
3115 |
in_subq++) |
|
3116 |
{ |
|
55
by brian
Update for using real bool types. |
3117 |
if (replace_where_subcondition(this, *in_subq, new Item_int(1), false)) |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3118 |
return(true); |
1
by brian
clean slate |
3119 |
} |
3120 |
||
3121 |
for (in_subq= sj_subselects.front(); |
|
3122 |
in_subq != in_subq_end && |
|
3123 |
tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES; |
|
3124 |
in_subq++) |
|
3125 |
{ |
|
3126 |
if (convert_subq_to_sj(this, *in_subq)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3127 |
return(true); |
1
by brian
clean slate |
3128 |
} |
3129 |
||
3130 |
/* 3. Finalize those we didn't convert */ |
|
3131 |
for (; in_subq!= in_subq_end; in_subq++) |
|
3132 |
{ |
|
3133 |
JOIN *child_join= (*in_subq)->unit->first_select()->join; |
|
3134 |
Item_subselect::trans_res res; |
|
3135 |
(*in_subq)->changed= 0; |
|
3136 |
(*in_subq)->fixed= 0; |
|
3137 |
res= (*in_subq)->select_transformer(child_join); |
|
3138 |
if (res == Item_subselect::RES_ERROR) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3139 |
return(true); |
1
by brian
clean slate |
3140 |
|
3141 |
(*in_subq)->changed= 1; |
|
3142 |
(*in_subq)->fixed= 1; |
|
3143 |
||
3144 |
Item *substitute= (*in_subq)->substitution; |
|
3145 |
bool do_fix_fields= !(*in_subq)->substitution->fixed; |
|
3146 |
if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3147 |
return(true); |
1
by brian
clean slate |
3148 |
|
3149 |
//if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3150 |
// return(true); |
1
by brian
clean slate |
3151 |
} |
3152 |
sj_subselects.clear(); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3153 |
return(false); |
1
by brian
clean slate |
3154 |
} |
3155 |
||
3156 |
||
3157 |
/** |
|
3158 |
Setup for execution all subqueries of a query, for which the optimizer |
|
3159 |
chose hash semi-join. |
|
3160 |
||
3161 |
@details Iterate over all subqueries of the query, and if they are under an |
|
3162 |
IN predicate, and the optimizer chose to compute it via hash semi-join: |
|
3163 |
- try to initialize all data structures needed for the materialized execution |
|
3164 |
of the IN predicate, |
|
3165 |
- if this fails, then perform the IN=>EXISTS transformation which was |
|
3166 |
previously blocked during JOIN::prepare. |
|
3167 |
||
3168 |
This method is part of the "code generation" query processing phase. |
|
3169 |
||
3170 |
This phase must be called after substitute_for_best_equal_field() because |
|
3171 |
that function may replace items with other items from a multiple equality, |
|
3172 |
and we need to reference the correct items in the index access method of the |
|
3173 |
IN predicate. |
|
3174 |
||
3175 |
@return Operation status |
|
55
by brian
Update for using real bool types. |
3176 |
@retval false success. |
3177 |
@retval true error occurred. |
|
1
by brian
clean slate |
3178 |
*/ |
3179 |
||
3180 |
bool JOIN::setup_subquery_materialization() |
|
3181 |
{ |
|
3182 |
for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un; |
|
3183 |
un= un->next_unit()) |
|
3184 |
{ |
|
3185 |
for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select()) |
|
3186 |
{ |
|
3187 |
Item_subselect *subquery_predicate= sl->master_unit()->item; |
|
3188 |
if (subquery_predicate && |
|
3189 |
subquery_predicate->substype() == Item_subselect::IN_SUBS) |
|
3190 |
{ |
|
3191 |
Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate; |
|
3192 |
if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION && |
|
3193 |
in_subs->setup_engine()) |
|
55
by brian
Update for using real bool types. |
3194 |
return true; |
1
by brian
clean slate |
3195 |
} |
3196 |
} |
|
3197 |
} |
|
55
by brian
Update for using real bool types. |
3198 |
return false; |
1
by brian
clean slate |
3199 |
} |
3200 |
||
3201 |
||
3202 |
/* |
|
3203 |
Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate |
|
3204 |
||
3205 |
SYNOPSIS |
|
3206 |
find_eq_ref_candidate() |
|
3207 |
table Table to be checked |
|
3208 |
sj_inner_tables Bitmap of inner tables. eq_ref(inner_table) doesn't |
|
3209 |
count. |
|
3210 |
||
3211 |
DESCRIPTION |
|
3212 |
Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate |
|
3213 |
||
3214 |
TODO |
|
3215 |
Check again if it is feasible to factor common parts with constant table |
|
3216 |
search |
|
3217 |
||
3218 |
RETURN |
|
55
by brian
Update for using real bool types. |
3219 |
true - There exists an eq_ref(outer-tables) candidate |
3220 |
false - Otherwise |
|
1
by brian
clean slate |
3221 |
*/ |
3222 |
||
3223 |
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables) |
|
3224 |
{ |
|
3225 |
KEYUSE *keyuse= table->reginfo.join_tab->keyuse; |
|
3226 |
uint key; |
|
3227 |
||
3228 |
if (keyuse) |
|
3229 |
{ |
|
3230 |
while (1) /* For each key */ |
|
3231 |
{ |
|
3232 |
key= keyuse->key; |
|
3233 |
KEY *keyinfo= table->key_info + key; |
|
3234 |
key_part_map bound_parts= 0; |
|
53.2.14
by Monty Taylor
Removed HA_END_SPACE_KEY and references to it. It was _supposed_ to be gone anyway, but the ifdef around it was broken (MYSQL_VERSION_ID was actually undefined.) |
3235 |
if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME) |
1
by brian
clean slate |
3236 |
{ |
3237 |
do /* For all equalities on all key parts */ |
|
3238 |
{ |
|
3239 |
/* Check if this is "t.keypart = expr(outer_tables) */ |
|
3240 |
if (!(keyuse->used_tables & sj_inner_tables) && |
|
3241 |
!(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)) |
|
3242 |
{ |
|
3243 |
bound_parts |= 1 << keyuse->keypart; |
|
3244 |
} |
|
3245 |
keyuse++; |
|
3246 |
} while (keyuse->key == key && keyuse->table == table); |
|
3247 |
||
3248 |
if (bound_parts == PREV_BITS(uint, keyinfo->key_parts)) |
|
55
by brian
Update for using real bool types. |
3249 |
return true; |
1
by brian
clean slate |
3250 |
if (keyuse->table != table) |
55
by brian
Update for using real bool types. |
3251 |
return false; |
1
by brian
clean slate |
3252 |
} |
3253 |
else |
|
3254 |
{ |
|
3255 |
do |
|
3256 |
{ |
|
3257 |
keyuse++; |
|
3258 |
if (keyuse->table != table) |
|
55
by brian
Update for using real bool types. |
3259 |
return false; |
1
by brian
clean slate |
3260 |
} |
3261 |
while (keyuse->key == key); |
|
3262 |
} |
|
3263 |
} |
|
3264 |
} |
|
55
by brian
Update for using real bool types. |
3265 |
return false; |
1
by brian
clean slate |
3266 |
} |
3267 |
||
3268 |
||
3269 |
/* |
|
3270 |
Pull tables out of semi-join nests, if possible |
|
3271 |
||
3272 |
SYNOPSIS |
|
3273 |
pull_out_semijoin_tables() |
|
3274 |
join The join where to do the semi-join flattening |
|
3275 |
||
3276 |
DESCRIPTION |
|
3277 |
Try to pull tables out of semi-join nests. |
|
3278 |
||
3279 |
PRECONDITIONS |
|
3280 |
When this function is called, the join may have several semi-join nests |
|
3281 |
(possibly within different semi-join nests), but it is guaranteed that |
|
3282 |
one semi-join nest does not contain another. |
|
3283 |
||
3284 |
ACTION |
|
3285 |
A table can be pulled out of the semi-join nest if |
|
3286 |
- It is a constant table |
|
3287 |
- It is accessed |
|
3288 |
||
3289 |
POSTCONDITIONS |
|
3290 |
* Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer |
|
3291 |
tables) |
|
3292 |
* Tables that were not pulled out have JOIN_TAB::emb_sj_nest. |
|
3293 |
* Semi-join nests TABLE_LIST::sj_inner_tables |
|
3294 |
||
3295 |
This operation is (and should be) performed at each PS execution since |
|
3296 |
tables may become/cease to be constant across PS reexecutions. |
|
3297 |
||
3298 |
RETURN |
|
3299 |
0 - OK |
|
3300 |
1 - Out of memory error |
|
3301 |
*/ |
|
3302 |
||
3303 |
int pull_out_semijoin_tables(JOIN *join) |
|
3304 |
{ |
|
3305 |
TABLE_LIST *sj_nest; |
|
3306 |
List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests); |
|
3307 |
||
3308 |
/* Try pulling out of the each of the semi-joins */ |
|
3309 |
while ((sj_nest= sj_list_it++)) |
|
3310 |
{ |
|
3311 |
/* Action #1: Mark the constant tables to be pulled out */ |
|
3312 |
table_map pulled_tables= 0; |
|
3313 |
||
3314 |
List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list); |
|
3315 |
TABLE_LIST *tbl; |
|
3316 |
while ((tbl= child_li++)) |
|
3317 |
{ |
|
3318 |
if (tbl->table) |
|
3319 |
{ |
|
3320 |
tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest; |
|
3321 |
if (tbl->table->map & join->const_table_map) |
|
3322 |
{ |
|
3323 |
pulled_tables |= tbl->table->map; |
|
3324 |
} |
|
3325 |
} |
|
3326 |
} |
|
3327 |
||
3328 |
/* |
|
3329 |
Action #2: Find which tables we can pull out based on |
|
3330 |
update_ref_and_keys() data. Note that pulling one table out can allow |
|
3331 |
us to pull out some other tables too. |
|
3332 |
*/ |
|
3333 |
bool pulled_a_table; |
|
3334 |
do |
|
3335 |
{ |
|
55
by brian
Update for using real bool types. |
3336 |
pulled_a_table= false; |
1
by brian
clean slate |
3337 |
child_li.rewind(); |
3338 |
while ((tbl= child_li++)) |
|
3339 |
{ |
|
3340 |
if (tbl->table && !(pulled_tables & tbl->table->map)) |
|
3341 |
{ |
|
3342 |
if (find_eq_ref_candidate(tbl->table, |
|
3343 |
sj_nest->nested_join->used_tables & |
|
3344 |
~pulled_tables)) |
|
3345 |
{ |
|
55
by brian
Update for using real bool types. |
3346 |
pulled_a_table= true; |
1
by brian
clean slate |
3347 |
pulled_tables |= tbl->table->map; |
3348 |
} |
|
3349 |
} |
|
3350 |
} |
|
3351 |
} while (pulled_a_table); |
|
3352 |
||
3353 |
child_li.rewind(); |
|
3354 |
if ((sj_nest)->nested_join->used_tables == pulled_tables) |
|
3355 |
{ |
|
3356 |
(sj_nest)->sj_inner_tables= 0; |
|
3357 |
while ((tbl= child_li++)) |
|
3358 |
{ |
|
3359 |
if (tbl->table) |
|
3360 |
tbl->table->reginfo.join_tab->emb_sj_nest= NULL; |
|
3361 |
} |
|
3362 |
} |
|
3363 |
else |
|
3364 |
{ |
|
3365 |
/* Record the bitmap of inner tables, mark the inner tables */ |
|
3366 |
table_map inner_tables=(sj_nest)->nested_join->used_tables & |
|
3367 |
~pulled_tables; |
|
3368 |
(sj_nest)->sj_inner_tables= inner_tables; |
|
3369 |
while ((tbl= child_li++)) |
|
3370 |
{ |
|
3371 |
if (tbl->table) |
|
3372 |
{ |
|
3373 |
if (inner_tables & tbl->table->map) |
|
3374 |
tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest); |
|
3375 |
else |
|
3376 |
tbl->table->reginfo.join_tab->emb_sj_nest= NULL; |
|
3377 |
} |
|
3378 |
} |
|
3379 |
} |
|
3380 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3381 |
return(0); |
1
by brian
clean slate |
3382 |
} |
3383 |
||
3384 |
/***************************************************************************** |
|
3385 |
Create JOIN_TABS, make a guess about the table types, |
|
3386 |
Approximate how many records will be used in each table |
|
3387 |
*****************************************************************************/ |
|
3388 |
||
3389 |
||
3390 |
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select, |
|
3391 |
TABLE *table, |
|
3392 |
const key_map *keys,ha_rows limit) |
|
3393 |
{ |
|
3394 |
int error; |
|
3395 |
if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3396 |
return(0); // Fatal error flag is set |
1
by brian
clean slate |
3397 |
if (select) |
3398 |
{ |
|
3399 |
select->head=table; |
|
3400 |
table->reginfo.impossible_range=0; |
|
3401 |
if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0, |
|
55
by brian
Update for using real bool types. |
3402 |
limit, 0, false)) == 1) |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3403 |
return(select->quick->records); |
1
by brian
clean slate |
3404 |
if (error == -1) |
3405 |
{ |
|
3406 |
table->reginfo.impossible_range=1; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3407 |
return(0); |
1
by brian
clean slate |
3408 |
} |
3409 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3410 |
return(HA_POS_ERROR); /* This shouldn't happend */ |
1
by brian
clean slate |
3411 |
} |
3412 |
||
3413 |
/* |
|
3414 |
This structure is used to collect info on potentially sargable |
|
3415 |
predicates in order to check whether they become sargable after |
|
3416 |
reading const tables. |
|
3417 |
We form a bitmap of indexes that can be used for sargable predicates. |
|
3418 |
Only such indexes are involved in range analysis. |
|
3419 |
*/ |
|
3420 |
typedef struct st_sargable_param |
|
3421 |
{ |
|
3422 |
Field *field; /* field against which to check sargability */ |
|
3423 |
Item **arg_value; /* values of potential keys for lookups */ |
|
3424 |
uint num_values; /* number of values in the above array */ |
|
3425 |
} SARGABLE_PARAM; |
|
3426 |
||
3427 |
/** |
|
3428 |
Calculate the best possible join and initialize the join structure. |
|
3429 |
||
3430 |
@retval |
|
3431 |
0 ok |
|
3432 |
@retval |
|
3433 |
1 Fatal error |
|
3434 |
*/ |
|
3435 |
||
3436 |
static bool |
|
3437 |
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds, |
|
3438 |
DYNAMIC_ARRAY *keyuse_array) |
|
3439 |
{ |
|
3440 |
int error; |
|
3441 |
TABLE *table; |
|
3442 |
uint i,table_count,const_count,key; |
|
3443 |
table_map found_const_table_map, all_table_map, found_ref, refs; |
|
3444 |
key_map const_ref, eq_part; |
|
3445 |
TABLE **table_vector; |
|
3446 |
JOIN_TAB *stat,*stat_end,*s,**stat_ref; |
|
3447 |
KEYUSE *keyuse,*start_keyuse; |
|
3448 |
table_map outer_join=0; |
|
3449 |
SARGABLE_PARAM *sargables= 0; |
|
3450 |
JOIN_TAB *stat_vector[MAX_TABLES+1]; |
|
3451 |
||
3452 |
table_count=join->tables; |
|
3453 |
stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count); |
|
3454 |
stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES); |
|
3455 |
table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2)); |
|
3456 |
if (!stat || !stat_ref || !table_vector) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3457 |
return(1); // Eom /* purecov: inspected */ |
1
by brian
clean slate |
3458 |
|
3459 |
join->best_ref=stat_vector; |
|
3460 |
||
3461 |
stat_end=stat+table_count; |
|
3462 |
found_const_table_map= all_table_map=0; |
|
3463 |
const_count=0; |
|
3464 |
||
3465 |
for (s= stat, i= 0; |
|
3466 |
tables; |
|
3467 |
s++, tables= tables->next_leaf, i++) |
|
3468 |
{ |
|
3469 |
TABLE_LIST *embedding= tables->embedding; |
|
3470 |
stat_vector[i]=s; |
|
3471 |
s->keys.init(); |
|
3472 |
s->const_keys.init(); |
|
3473 |
s->checked_keys.init(); |
|
3474 |
s->needed_reg.init(); |
|
3475 |
table_vector[i]=s->table=table=tables->table; |
|
3476 |
table->pos_in_table_list= tables; |
|
3477 |
error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK); |
|
3478 |
if(error) |
|
3479 |
{ |
|
3480 |
table->file->print_error(error, MYF(0)); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3481 |
return(1); |
1
by brian
clean slate |
3482 |
} |
3483 |
table->quick_keys.clear_all(); |
|
3484 |
table->reginfo.join_tab=s; |
|
3485 |
table->reginfo.not_exists_optimize=0; |
|
3486 |
bzero((char*) table->const_key_parts, sizeof(key_part_map)*table->s->keys); |
|
3487 |
all_table_map|= table->map; |
|
3488 |
s->join=join; |
|
3489 |
s->info=0; // For describe |
|
3490 |
||
3491 |
s->dependent= tables->dep_tables; |
|
3492 |
s->key_dependent= 0; |
|
3493 |
if (tables->schema_table) |
|
3494 |
table->file->stats.records= 2; |
|
3495 |
table->quick_condition_rows= table->file->stats.records; |
|
3496 |
||
3497 |
s->on_expr_ref= &tables->on_expr; |
|
3498 |
if (*s->on_expr_ref) |
|
3499 |
{ |
|
3500 |
/* s is the only inner table of an outer join */ |
|
3501 |
if (!table->file->stats.records && !embedding) |
|
3502 |
{ // Empty table |
|
3503 |
s->dependent= 0; // Ignore LEFT JOIN depend. |
|
3504 |
set_position(join,const_count++,s,(KEYUSE*) 0); |
|
3505 |
continue; |
|
3506 |
} |
|
3507 |
outer_join|= table->map; |
|
3508 |
s->embedding_map= 0; |
|
3509 |
for (;embedding; embedding= embedding->embedding) |
|
3510 |
s->embedding_map|= embedding->nested_join->nj_map; |
|
3511 |
continue; |
|
3512 |
} |
|
3513 |
if (embedding && !(embedding->sj_on_expr && ! embedding->embedding)) |
|
3514 |
{ |
|
3515 |
/* s belongs to a nested join, maybe to several embedded joins */ |
|
3516 |
s->embedding_map= 0; |
|
3517 |
do |
|
3518 |
{ |
|
3519 |
NESTED_JOIN *nested_join= embedding->nested_join; |
|
3520 |
s->embedding_map|=nested_join->nj_map; |
|
3521 |
s->dependent|= embedding->dep_tables; |
|
3522 |
embedding= embedding->embedding; |
|
3523 |
outer_join|= nested_join->used_tables; |
|
3524 |
} |
|
3525 |
while (embedding); |
|
3526 |
continue; |
|
3527 |
} |
|
3528 |
if ((table->s->system || table->file->stats.records <= 1) && |
|
3529 |
!s->dependent && |
|
137
by Brian Aker
Removed dead FT bits. Small refactoring in sql_plugin.cc |
3530 |
(table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables) |
1
by brian
clean slate |
3531 |
{ |
3532 |
set_position(join,const_count++,s,(KEYUSE*) 0); |
|
3533 |
} |
|
3534 |
} |
|
3535 |
stat_vector[i]=0; |
|
3536 |
join->outer_join=outer_join; |
|
3537 |
||
3538 |
if (join->outer_join) |
|
3539 |
{ |
|
3540 |
/* |
|
3541 |
Build transitive closure for relation 'to be dependent on'. |
|
3542 |
This will speed up the plan search for many cases with outer joins, |
|
3543 |
as well as allow us to catch illegal cross references/ |
|
3544 |
Warshall's algorithm is used to build the transitive closure. |
|
3545 |
As we use bitmaps to represent the relation the complexity |
|
3546 |
of the algorithm is O((number of tables)^2). |
|
3547 |
*/ |
|
3548 |
for (i= 0, s= stat ; i < table_count ; i++, s++) |
|
3549 |
{ |
|
3550 |
for (uint j= 0 ; j < table_count ; j++) |
|
3551 |
{ |
|
3552 |
table= stat[j].table; |
|
3553 |
if (s->dependent & table->map) |
|
3554 |
s->dependent |= table->reginfo.join_tab->dependent; |
|
3555 |
} |
|
3556 |
if (s->dependent) |
|
3557 |
s->table->maybe_null= 1; |
|
3558 |
} |
|
3559 |
/* Catch illegal cross references for outer joins */ |
|
3560 |
for (i= 0, s= stat ; i < table_count ; i++, s++) |
|
3561 |
{ |
|
3562 |
if (s->dependent & s->table->map) |
|
3563 |
{ |
|
3564 |
join->tables=0; // Don't use join->table |
|
3565 |
my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0)); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3566 |
return(1); |
1
by brian
clean slate |
3567 |
} |
3568 |
s->key_dependent= s->dependent; |
|
3569 |
} |
|
3570 |
} |
|
3571 |
||
3572 |
if (conds || outer_join) |
|
3573 |
if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables, |
|
3574 |
conds, join->cond_equal, |
|
3575 |
~outer_join, join->select_lex, &sargables)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3576 |
return(1); |
1
by brian
clean slate |
3577 |
|
3578 |
/* Read tables with 0 or 1 rows (system tables) */ |
|
3579 |
join->const_table_map= 0; |
|
3580 |
||
3581 |
for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count; |
|
3582 |
p_pos < p_end ; |
|
3583 |
p_pos++) |
|
3584 |
{ |
|
3585 |
int tmp; |
|
3586 |
s= p_pos->table; |
|
3587 |
s->type=JT_SYSTEM; |
|
3588 |
join->const_table_map|=s->table->map; |
|
3589 |
if ((tmp=join_read_const_table(s, p_pos))) |
|
3590 |
{ |
|
3591 |
if (tmp > 0) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3592 |
return(1); // Fatal error |
1
by brian
clean slate |
3593 |
} |
3594 |
else |
|
3595 |
found_const_table_map|= s->table->map; |
|
3596 |
} |
|
3597 |
||
3598 |
/* loop until no more const tables are found */ |
|
3599 |
int ref_changed; |
|
3600 |
do |
|
3601 |
{ |
|
3602 |
more_const_tables_found: |
|
3603 |
ref_changed = 0; |
|
3604 |
found_ref=0; |
|
3605 |
||
3606 |
/* |
|
3607 |
We only have to loop from stat_vector + const_count as |
|
3608 |
set_position() will move all const_tables first in stat_vector |
|
3609 |
*/ |
|
3610 |
||
3611 |
for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++) |
|
3612 |
{ |
|
3613 |
table=s->table; |
|
3614 |
||
3615 |
/* |
|
3616 |
If equi-join condition by a key is null rejecting and after a |
|
3617 |
substitution of a const table the key value happens to be null |
|
3618 |
then we can state that there are no matches for this equi-join. |
|
3619 |
*/ |
|
3620 |
if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map) |
|
3621 |
{ |
|
3622 |
/* |
|
3623 |
When performing an outer join operation if there are no matching rows |
|
3624 |
for the single row of the outer table all the inner tables are to be |
|
3625 |
null complemented and thus considered as constant tables. |
|
3626 |
Here we apply this consideration to the case of outer join operations |
|
3627 |
with a single inner table only because the case with nested tables |
|
3628 |
would require a more thorough analysis. |
|
3629 |
TODO. Apply single row substitution to null complemented inner tables |
|
3630 |
for nested outer join operations. |
|
3631 |
*/ |
|
3632 |
while (keyuse->table == table) |
|
3633 |
{ |
|
3634 |
if (!(keyuse->val->used_tables() & ~join->const_table_map) && |
|
3635 |
keyuse->val->is_null() && keyuse->null_rejecting) |
|
3636 |
{ |
|
3637 |
s->type= JT_CONST; |
|
3638 |
mark_as_null_row(table); |
|
3639 |
found_const_table_map|= table->map; |
|
3640 |
join->const_table_map|= table->map; |
|
3641 |
set_position(join,const_count++,s,(KEYUSE*) 0); |
|
3642 |
goto more_const_tables_found; |
|
3643 |
} |
|
3644 |
keyuse++; |
|
3645 |
} |
|
3646 |
} |
|
3647 |
||
3648 |
if (s->dependent) // If dependent on some table |
|
3649 |
{ |
|
3650 |
// All dep. must be constants |
|
3651 |
if (s->dependent & ~(found_const_table_map)) |
|
3652 |
continue; |
|
3653 |
if (table->file->stats.records <= 1L && |
|
3654 |
(table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && |
|
3655 |
!table->pos_in_table_list->embedding) |
|
3656 |
{ // system table |
|
3657 |
int tmp= 0; |
|
3658 |
s->type=JT_SYSTEM; |
|
3659 |
join->const_table_map|=table->map; |
|
3660 |
set_position(join,const_count++,s,(KEYUSE*) 0); |
|
3661 |
if ((tmp= join_read_const_table(s, join->positions+const_count-1))) |
|
3662 |
{ |
|
3663 |
if (tmp > 0) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3664 |
return(1); // Fatal error |
1
by brian
clean slate |
3665 |
} |
3666 |
else |
|
3667 |
found_const_table_map|= table->map; |
|
3668 |
continue; |
|
3669 |
} |
|
3670 |
} |
|
3671 |
/* check if table can be read by key or table only uses const refs */ |
|
3672 |
if ((keyuse=s->keyuse)) |
|
3673 |
{ |
|
3674 |
s->type= JT_REF; |
|
3675 |
while (keyuse->table == table) |
|
3676 |
{ |
|
3677 |
start_keyuse=keyuse; |
|
3678 |
key=keyuse->key; |
|
3679 |
s->keys.set_bit(key); // QQ: remove this ? |
|
3680 |
||
3681 |
refs=0; |
|
3682 |
const_ref.clear_all(); |
|
3683 |
eq_part.clear_all(); |
|
3684 |
do |
|
3685 |
{ |
|
3686 |
if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize) |
|
3687 |
{ |
|
3688 |
if (!((~found_const_table_map) & keyuse->used_tables)) |
|
3689 |
const_ref.set_bit(keyuse->keypart); |
|
3690 |
else |
|
3691 |
refs|=keyuse->used_tables; |
|
3692 |
eq_part.set_bit(keyuse->keypart); |
|
3693 |
} |
|
3694 |
keyuse++; |
|
3695 |
} while (keyuse->table == table && keyuse->key == key); |
|
3696 |
||
3697 |
if (eq_part.is_prefix(table->key_info[key].key_parts) && |
|
3698 |
!table->pos_in_table_list->embedding) |
|
3699 |
{ |
|
53.2.14
by Monty Taylor
Removed HA_END_SPACE_KEY and references to it. It was _supposed_ to be gone anyway, but the ifdef around it was broken (MYSQL_VERSION_ID was actually undefined.) |
3700 |
if ((table->key_info[key].flags & (HA_NOSAME)) |
1
by brian
clean slate |
3701 |
== HA_NOSAME) |
3702 |
{ |
|
3703 |
if (const_ref == eq_part) |
|
3704 |
{ // Found everything for ref. |
|
3705 |
int tmp; |
|
3706 |
ref_changed = 1; |
|
3707 |
s->type= JT_CONST; |
|
3708 |
join->const_table_map|=table->map; |
|
3709 |
set_position(join,const_count++,s,start_keyuse); |
|
3710 |
if (create_ref_for_key(join, s, start_keyuse, |
|
3711 |
found_const_table_map)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3712 |
return(1); |
1
by brian
clean slate |
3713 |
if ((tmp=join_read_const_table(s, |
3714 |
join->positions+const_count-1))) |
|
3715 |
{ |
|
3716 |
if (tmp > 0) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3717 |
return(1); // Fatal error |
1
by brian
clean slate |
3718 |
} |
3719 |
else |
|
3720 |
found_const_table_map|= table->map; |
|
3721 |
break; |
|
3722 |
} |
|
3723 |
else |
|
3724 |
found_ref|= refs; // Table is const if all refs are const |
|
3725 |
} |
|
3726 |
else if (const_ref == eq_part) |
|
3727 |
s->const_keys.set_bit(key); |
|
3728 |
} |
|
3729 |
} |
|
3730 |
} |
|
3731 |
} |
|
3732 |
} while (join->const_table_map & found_ref && ref_changed); |
|
3733 |
||
3734 |
/* |
|
3735 |
Update info on indexes that can be used for search lookups as |
|
3736 |
reading const tables may has added new sargable predicates. |
|
3737 |
*/ |
|
3738 |
if (const_count && sargables) |
|
3739 |
{ |
|
3740 |
for( ; sargables->field ; sargables++) |
|
3741 |
{ |
|
3742 |
Field *field= sargables->field; |
|
3743 |
JOIN_TAB *join_tab= field->table->reginfo.join_tab; |
|
3744 |
key_map possible_keys= field->key_start; |
|
3745 |
possible_keys.intersect(field->table->keys_in_use_for_query); |
|
3746 |
bool is_const= 1; |
|
3747 |
for (uint j=0; j < sargables->num_values; j++) |
|
3748 |
is_const&= sargables->arg_value[j]->const_item(); |
|
3749 |
if (is_const) |
|
3750 |
join_tab[0].const_keys.merge(possible_keys); |
|
3751 |
} |
|
3752 |
} |
|
3753 |
||
3754 |
if (pull_out_semijoin_tables(join)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3755 |
return(true); |
1
by brian
clean slate |
3756 |
|
3757 |
/* Calc how many (possible) matched records in each table */ |
|
3758 |
||
3759 |
for (s=stat ; s < stat_end ; s++) |
|
3760 |
{ |
|
3761 |
if (s->type == JT_SYSTEM || s->type == JT_CONST) |
|
3762 |
{ |
|
3763 |
/* Only one matching row */ |
|
3764 |
s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0; |
|
3765 |
continue; |
|
3766 |
} |
|
3767 |
/* Approximate found rows and time to read them */ |
|
3768 |
s->found_records=s->records=s->table->file->stats.records; |
|
3769 |
s->read_time=(ha_rows) s->table->file->scan_time(); |
|
3770 |
||
3771 |
/* |
|
3772 |
Set a max range of how many seeks we can expect when using keys |
|
3773 |
This is can't be to high as otherwise we are likely to use |
|
3774 |
table scan. |
|
3775 |
*/ |
|
3776 |
s->worst_seeks= min((double) s->found_records / 10, |
|
3777 |
(double) s->read_time*3); |
|
3778 |
if (s->worst_seeks < 2.0) // Fix for small tables |
|
3779 |
s->worst_seeks=2.0; |
|
3780 |
||
3781 |
/* |
|
3782 |
Add to stat->const_keys those indexes for which all group fields or |
|
3783 |
all select distinct fields participate in one index. |
|
3784 |
*/ |
|
3785 |
add_group_and_distinct_keys(join, s); |
|
3786 |
||
3787 |
if (!s->const_keys.is_clear_all() && |
|
3788 |
!s->table->pos_in_table_list->embedding) |
|
3789 |
{ |
|
3790 |
ha_rows records; |
|
3791 |
SQL_SELECT *select; |
|
3792 |
select= make_select(s->table, found_const_table_map, |
|
3793 |
found_const_table_map, |
|
3794 |
*s->on_expr_ref ? *s->on_expr_ref : conds, |
|
3795 |
1, &error); |
|
3796 |
if (!select) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3797 |
return(1); |
1
by brian
clean slate |
3798 |
records= get_quick_record_count(join->thd, select, s->table, |
3799 |
&s->const_keys, join->row_limit); |
|
3800 |
s->quick=select->quick; |
|
3801 |
s->needed_reg=select->needed_reg; |
|
3802 |
select->quick=0; |
|
3803 |
if (records == 0 && s->table->reginfo.impossible_range) |
|
3804 |
{ |
|
3805 |
/* |
|
3806 |
Impossible WHERE or ON expression |
|
3807 |
In case of ON, we mark that the we match one empty NULL row. |
|
3808 |
In case of WHERE, don't set found_const_table_map to get the |
|
3809 |
caller to abort with a zero row result. |
|
3810 |
*/ |
|
3811 |
join->const_table_map|= s->table->map; |
|
3812 |
set_position(join,const_count++,s,(KEYUSE*) 0); |
|
3813 |
s->type= JT_CONST; |
|
3814 |
if (*s->on_expr_ref) |
|
3815 |
{ |
|
3816 |
/* Generate empty row */ |
|
3817 |
s->info= "Impossible ON condition"; |
|
3818 |
found_const_table_map|= s->table->map; |
|
3819 |
s->type= JT_CONST; |
|
3820 |
mark_as_null_row(s->table); // All fields are NULL |
|
3821 |
} |
|
3822 |
} |
|
3823 |
if (records != HA_POS_ERROR) |
|
3824 |
{ |
|
3825 |
s->found_records=records; |
|
3826 |
s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0); |
|
3827 |
} |
|
3828 |
delete select; |
|
3829 |
} |
|
3830 |
} |
|
3831 |
||
3832 |
join->join_tab=stat; |
|
3833 |
join->map2table=stat_ref; |
|
3834 |
join->table= join->all_tables=table_vector; |
|
3835 |
join->const_tables=const_count; |
|
3836 |
join->found_const_table_map=found_const_table_map; |
|
3837 |
||
3838 |
/* Find an optimal join order of the non-constant tables. */ |
|
3839 |
if (join->const_tables != join->tables) |
|
3840 |
{ |
|
3841 |
optimize_keyuse(join, keyuse_array); |
|
3842 |
if (choose_plan(join, all_table_map & ~join->const_table_map)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3843 |
return(true); |
1
by brian
clean slate |
3844 |
} |
3845 |
else |
|
3846 |
{ |
|
3847 |
memcpy((uchar*) join->best_positions,(uchar*) join->positions, |
|
3848 |
sizeof(POSITION)*join->const_tables); |
|
3849 |
join->best_read=1.0; |
|
3850 |
} |
|
3851 |
/* Generate an execution plan from the found optimal join order. */ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
3852 |
return(join->thd->killed || get_best_combination(join)); |
1
by brian
clean slate |
3853 |
} |
3854 |
||
3855 |
||
3856 |
/***************************************************************************** |
|
3857 |
Check with keys are used and with tables references with tables |
|
3858 |
Updates in stat: |
|
3859 |
keys Bitmap of all used keys |
|
3860 |
const_keys Bitmap of all keys with may be used with quick_select |
|
3861 |
keyuse Pointer to possible keys |
|
3862 |
*****************************************************************************/ |
|
3863 |
||
3864 |
/// Used when finding key fields |
|
3865 |
typedef struct key_field_t { |
|
3866 |
Field *field; |
|
3867 |
Item *val; ///< May be empty if diff constant |
|
3868 |
uint level; |
|
3869 |
uint optimize; // KEY_OPTIMIZE_* |
|
3870 |
bool eq_func; |
|
3871 |
/** |
|
3872 |
If true, the condition this struct represents will not be satisfied |
|
3873 |
when val IS NULL. |
|
3874 |
*/ |
|
3875 |
bool null_rejecting; |
|
3876 |
bool *cond_guard; /* See KEYUSE::cond_guard */ |
|
3877 |
uint sj_pred_no; /* See KEYUSE::sj_pred_no */ |
|
3878 |
} KEY_FIELD; |
|
3879 |
||
3880 |
/** |
|
3881 |
Merge new key definitions to old ones, remove those not used in both. |
|
3882 |
||
3883 |
This is called for OR between different levels. |
|
3884 |
||
3885 |
To be able to do 'ref_or_null' we merge a comparison of a column |
|
3886 |
and 'column IS NULL' to one test. This is useful for sub select queries |
|
3887 |
that are internally transformed to something like:. |
|
3888 |
||
3889 |
@code |
|
3890 |
SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL |
|
3891 |
@endcode |
|
3892 |
||
3893 |
KEY_FIELD::null_rejecting is processed as follows: @n |
|
3894 |
result has null_rejecting=true if it is set for both ORed references. |
|
3895 |
for example: |
|
3896 |
- (t2.key = t1.field OR t2.key = t1.field) -> null_rejecting=true |
|
3897 |
- (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false |
|
3898 |
||
3899 |
@todo |
|
3900 |
The result of this is that we're missing some 'ref' accesses. |
|
3901 |
OptimizerTeam: Fix this |
|
3902 |
*/ |
|
3903 |
||
3904 |
static KEY_FIELD * |
|
3905 |
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end, |
|
3906 |
uint and_level) |
|
3907 |
{ |
|
3908 |
if (start == new_fields) |
|
3909 |
return start; // Impossible or |
|
3910 |
if (new_fields == end) |
|
3911 |
return start; // No new fields, skip all |
|
3912 |
||
3913 |
KEY_FIELD *first_free=new_fields; |
|
3914 |
||
3915 |
/* Mark all found fields in old array */ |
|
3916 |
for (; new_fields != end ; new_fields++) |
|
3917 |
{ |
|
3918 |
for (KEY_FIELD *old=start ; old != first_free ; old++) |
|
3919 |
{ |
|
3920 |
if (old->field == new_fields->field) |
|
3921 |
{ |
|
3922 |
/* |
|
3923 |
NOTE: below const_item() call really works as "!used_tables()", i.e. |
|
55
by brian
Update for using real bool types. |
3924 |
it can return false where it is feasible to make it return true. |
1
by brian
clean slate |
3925 |
|
3926 |
The cause is as follows: Some of the tables are already known to be |
|
3927 |
const tables (the detection code is in make_join_statistics(), |
|
3928 |
above the update_ref_and_keys() call), but we didn't propagate |
|
55
by brian
Update for using real bool types. |
3929 |
information about this: TABLE::const_table is not set to true, and |
1
by brian
clean slate |
3930 |
Item::update_used_tables() hasn't been called for each item. |
3931 |
The result of this is that we're missing some 'ref' accesses. |
|
3932 |
TODO: OptimizerTeam: Fix this |
|
3933 |
*/ |
|
3934 |
if (!new_fields->val->const_item()) |
|
3935 |
{ |
|
3936 |
/* |
|
3937 |
If the value matches, we can use the key reference. |
|
3938 |
If not, we keep it until we have examined all new values |
|
3939 |
*/ |
|
3940 |
if (old->val->eq(new_fields->val, old->field->binary())) |
|
3941 |
{ |
|
3942 |
old->level= and_level; |
|
3943 |
old->optimize= ((old->optimize & new_fields->optimize & |
|
3944 |
KEY_OPTIMIZE_EXISTS) | |
|
3945 |
((old->optimize | new_fields->optimize) & |
|
3946 |
KEY_OPTIMIZE_REF_OR_NULL)); |
|
3947 |
old->null_rejecting= (old->null_rejecting && |
|
3948 |
new_fields->null_rejecting); |
|
3949 |
} |
|
3950 |
} |
|
3951 |
else if (old->eq_func && new_fields->eq_func && |
|
3952 |
old->val->eq_by_collation(new_fields->val, |
|
3953 |
old->field->binary(), |
|
3954 |
old->field->charset())) |
|
3955 |
||
3956 |
{ |
|
3957 |
old->level= and_level; |
|
3958 |
old->optimize= ((old->optimize & new_fields->optimize & |
|
3959 |
KEY_OPTIMIZE_EXISTS) | |
|
3960 |
((old->optimize | new_fields->optimize) & |
|
3961 |
KEY_OPTIMIZE_REF_OR_NULL)); |
|
3962 |
old->null_rejecting= (old->null_rejecting && |
|
3963 |
new_fields->null_rejecting); |
|
3964 |
} |
|
3965 |
else if (old->eq_func && new_fields->eq_func && |
|
3966 |
((old->val->const_item() && old->val->is_null()) || |
|
3967 |
new_fields->val->is_null())) |
|
3968 |
{ |
|
3969 |
/* field = expression OR field IS NULL */ |
|
3970 |
old->level= and_level; |
|
3971 |
old->optimize= KEY_OPTIMIZE_REF_OR_NULL; |
|
3972 |
/* |
|
3973 |
Remember the NOT NULL value unless the value does not depend |
|
3974 |
on other tables. |
|
3975 |
*/ |
|
3976 |
if (!old->val->used_tables() && old->val->is_null()) |
|
3977 |
old->val= new_fields->val; |
|
3978 |
/* The referred expression can be NULL: */ |
|
3979 |
old->null_rejecting= 0; |
|
3980 |
} |
|
3981 |
else |
|
3982 |
{ |
|
3983 |
/* |
|
3984 |
We are comparing two different const. In this case we can't |
|
3985 |
use a key-lookup on this so it's better to remove the value |
|
3986 |
and let the range optimzier handle it |
|
3987 |
*/ |
|
3988 |
if (old == --first_free) // If last item |
|
3989 |
break; |
|
3990 |
*old= *first_free; // Remove old value |
|
3991 |
old--; // Retry this value |
|
3992 |
} |
|
3993 |
} |
|
3994 |
} |
|
3995 |
} |
|
3996 |
/* Remove all not used items */ |
|
3997 |
for (KEY_FIELD *old=start ; old != first_free ;) |
|
3998 |
{ |
|
3999 |
if (old->level != and_level) |
|
4000 |
{ // Not used in all levels |
|
4001 |
if (old == --first_free) |
|
4002 |
break; |
|
4003 |
*old= *first_free; // Remove old value |
|
4004 |
continue; |
|
4005 |
} |
|
4006 |
old++; |
|
4007 |
} |
|
4008 |
return first_free; |
|
4009 |
} |
|
4010 |
||
4011 |
||
4012 |
/** |
|
4013 |
Add a possible key to array of possible keys if it's usable as a key |
|
4014 |
||
4015 |
@param key_fields Pointer to add key, if usable |
|
4016 |
@param and_level And level, to be stored in KEY_FIELD |
|
4017 |
@param cond Condition predicate |
|
4018 |
@param field Field used in comparision |
|
4019 |
@param eq_func True if we used =, <=> or IS NULL |
|
4020 |
@param value Value used for comparison with field |
|
4021 |
@param usable_tables Tables which can be used for key optimization |
|
4022 |
@param sargables IN/OUT Array of found sargable candidates |
|
4023 |
||
4024 |
@note |
|
4025 |
If we are doing a NOT NULL comparison on a NOT NULL field in a outer join |
|
4026 |
table, we store this to be able to do not exists optimization later. |
|
4027 |
||
4028 |
@returns |
|
4029 |
*key_fields is incremented if we stored a key in the array |
|
4030 |
*/ |
|
4031 |
||
4032 |
static void |
|
4033 |
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond, |
|
4034 |
Field *field, bool eq_func, Item **value, uint num_values, |
|
4035 |
table_map usable_tables, SARGABLE_PARAM **sargables) |
|
4036 |
{ |
|
4037 |
uint exists_optimize= 0; |
|
4038 |
if (!(field->flags & PART_KEY_FLAG)) |
|
4039 |
{ |
|
4040 |
// Don't remove column IS NULL on a LEFT JOIN table |
|
4041 |
if (!eq_func || (*value)->type() != Item::NULL_ITEM || |
|
4042 |
!field->table->maybe_null || field->null_ptr) |
|
4043 |
return; // Not a key. Skip it |
|
4044 |
exists_optimize= KEY_OPTIMIZE_EXISTS; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
4045 |
assert(num_values == 1); |
1
by brian
clean slate |
4046 |
} |
4047 |
else |
|
4048 |
{ |
|
4049 |
table_map used_tables=0; |
|
4050 |
bool optimizable=0; |
|
4051 |
for (uint i=0; i<num_values; i++) |
|
4052 |
{ |
|
4053 |
used_tables|=(value[i])->used_tables(); |
|
4054 |
if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT))) |
|
4055 |
optimizable=1; |
|
4056 |
} |
|
4057 |
if (!optimizable) |
|
4058 |
return; |
|
4059 |
if (!(usable_tables & field->table->map)) |
|
4060 |
{ |
|
4061 |
if (!eq_func || (*value)->type() != Item::NULL_ITEM || |
|
4062 |
!field->table->maybe_null || field->null_ptr) |
|
4063 |
return; // Can't use left join optimize |
|
4064 |
exists_optimize= KEY_OPTIMIZE_EXISTS; |
|
4065 |
} |
|
4066 |
else |
|
4067 |
{ |
|
4068 |
JOIN_TAB *stat=field->table->reginfo.join_tab; |
|
4069 |
key_map possible_keys=field->key_start; |
|
4070 |
possible_keys.intersect(field->table->keys_in_use_for_query); |
|
4071 |
stat[0].keys.merge(possible_keys); // Add possible keys |
|
4072 |
||
4073 |
/* |
|
4074 |
Save the following cases: |
|
4075 |
Field op constant |
|
4076 |
Field LIKE constant where constant doesn't start with a wildcard |
|
4077 |
Field = field2 where field2 is in a different table |
|
4078 |
Field op formula |
|
4079 |
Field IS NULL |
|
4080 |
Field IS NOT NULL |
|
4081 |
Field BETWEEN ... |
|
4082 |
Field IN ... |
|
4083 |
*/ |
|
4084 |
stat[0].key_dependent|=used_tables; |
|
4085 |
||
4086 |
bool is_const=1; |
|
4087 |
for (uint i=0; i<num_values; i++) |
|
4088 |
{ |
|
4089 |
if (!(is_const&= value[i]->const_item())) |
|
4090 |
break; |
|
4091 |
} |
|
4092 |
if (is_const) |
|
4093 |
stat[0].const_keys.merge(possible_keys); |
|
4094 |
else if (!eq_func) |
|
4095 |
{ |
|
4096 |
/* |
|
4097 |
Save info to be able check whether this predicate can be |
|
4098 |
considered as sargable for range analisis after reading const tables. |
|
4099 |
We do not save info about equalities as update_const_equal_items |
|
4100 |
will take care of updating info on keys from sargable equalities. |
|
4101 |
*/ |
|
4102 |
(*sargables)--; |
|
4103 |
(*sargables)->field= field; |
|
4104 |
(*sargables)->arg_value= value; |
|
4105 |
(*sargables)->num_values= num_values; |
|
4106 |
} |
|
4107 |
/* |
|
4108 |
We can't always use indexes when comparing a string index to a |
|
4109 |
number. cmp_type() is checked to allow compare of dates to numbers. |
|
4110 |
eq_func is NEVER true when num_values > 1 |
|
4111 |
*/ |
|
4112 |
if (!eq_func) |
|
4113 |
{ |
|
4114 |
/* |
|
4115 |
Additional optimization: if we're processing |
|
4116 |
"t.key BETWEEN c1 AND c1" then proceed as if we were processing |
|
4117 |
"t.key = c1". |
|
4118 |
TODO: This is a very limited fix. A more generic fix is possible. |
|
4119 |
There are 2 options: |
|
4120 |
A) Make equality propagation code be able to handle BETWEEN |
|
4121 |
(including cases like t1.key BETWEEN t2.key AND t3.key) |
|
4122 |
B) Make range optimizer to infer additional "t.key = c" equalities |
|
4123 |
and use them in equality propagation process (see details in |
|
4124 |
OptimizerKBAndTodo) |
|
4125 |
*/ |
|
4126 |
if ((cond->functype() != Item_func::BETWEEN) || |
|
4127 |
((Item_func_between*) cond)->negated || |
|
4128 |
!value[0]->eq(value[1], field->binary())) |
|
4129 |
return; |
|
55
by brian
Update for using real bool types. |
4130 |
eq_func= true; |
1
by brian
clean slate |
4131 |
} |
4132 |
||
4133 |
if (field->result_type() == STRING_RESULT) |
|
4134 |
{ |
|
4135 |
if ((*value)->result_type() != STRING_RESULT) |
|
4136 |
{ |
|
4137 |
if (field->cmp_type() != (*value)->result_type()) |
|
4138 |
return; |
|
4139 |
} |
|
4140 |
else |
|
4141 |
{ |
|
4142 |
/* |
|
4143 |
We can't use indexes if the effective collation |
|
4144 |
of the operation differ from the field collation. |
|
4145 |
*/ |
|
4146 |
if (field->cmp_type() == STRING_RESULT && |
|
4147 |
((Field_str*)field)->charset() != cond->compare_collation()) |
|
4148 |
return; |
|
4149 |
} |
|
4150 |
} |
|
4151 |
} |
|
4152 |
} |
|
4153 |
/* |
|
4154 |
For the moment eq_func is always true. This slot is reserved for future |
|
4155 |
extensions where we want to remembers other things than just eq comparisons |
|
4156 |
*/ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
4157 |
assert(eq_func); |
1
by brian
clean slate |
4158 |
/* Store possible eq field */ |
4159 |
(*key_fields)->field= field; |
|
4160 |
(*key_fields)->eq_func= eq_func; |
|
4161 |
(*key_fields)->val= *value; |
|
4162 |
(*key_fields)->level= and_level; |
|
4163 |
(*key_fields)->optimize= exists_optimize; |
|
4164 |
/* |
|
4165 |
If the condition has form "tbl.keypart = othertbl.field" and |
|
4166 |
othertbl.field can be NULL, there will be no matches if othertbl.field |
|
4167 |
has NULL value. |
|
4168 |
We use null_rejecting in add_not_null_conds() to add |
|
4169 |
'othertbl.field IS NOT NULL' to tab->select_cond. |
|
4170 |
*/ |
|
4171 |
(*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC || |
|
4172 |
cond->functype() == Item_func::MULT_EQUAL_FUNC) && |
|
4173 |
((*value)->type() == Item::FIELD_ITEM) && |
|
4174 |
((Item_field*)*value)->field->maybe_null()); |
|
4175 |
(*key_fields)->cond_guard= NULL; |
|
4176 |
(*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name && |
|
4177 |
cond->name < subq_sj_cond_name + 64)? |
|
4178 |
cond->name - subq_sj_cond_name: UINT_MAX; |
|
4179 |
(*key_fields)++; |
|
4180 |
} |
|
4181 |
||
4182 |
/** |
|
4183 |
Add possible keys to array of possible keys originated from a simple |
|
4184 |
predicate. |
|
4185 |
||
4186 |
@param key_fields Pointer to add key, if usable |
|
4187 |
@param and_level And level, to be stored in KEY_FIELD |
|
4188 |
@param cond Condition predicate |
|
4189 |
@param field Field used in comparision |
|
4190 |
@param eq_func True if we used =, <=> or IS NULL |
|
4191 |
@param value Value used for comparison with field |
|
4192 |
Is NULL for BETWEEN and IN |
|
4193 |
@param usable_tables Tables which can be used for key optimization |
|
4194 |
@param sargables IN/OUT Array of found sargable candidates |
|
4195 |
||
4196 |
@note |
|
4197 |
If field items f1 and f2 belong to the same multiple equality and |
|
4198 |
a key is added for f1, the the same key is added for f2. |
|
4199 |
||
4200 |
@returns |
|
4201 |
*key_fields is incremented if we stored a key in the array |
|
4202 |
*/ |
|
4203 |
||
4204 |
static void |
|
4205 |
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level, |
|
4206 |
Item_func *cond, Item_field *field_item, |
|
4207 |
bool eq_func, Item **val, |
|
4208 |
uint num_values, table_map usable_tables, |
|
4209 |
SARGABLE_PARAM **sargables) |
|
4210 |
{ |
|
4211 |
Field *field= field_item->field; |
|
4212 |
add_key_field(key_fields, and_level, cond, field, |
|
4213 |
eq_func, val, num_values, usable_tables, sargables); |
|
4214 |
Item_equal *item_equal= field_item->item_equal; |
|
4215 |
if (item_equal) |
|
4216 |
{ |
|
4217 |
/* |
|
4218 |
Add to the set of possible key values every substitution of |
|
4219 |
the field for an equal field included into item_equal |
|
4220 |
*/ |
|
4221 |
Item_equal_iterator it(*item_equal); |
|
4222 |
Item_field *item; |
|
4223 |
while ((item= it++)) |
|
4224 |
{ |
|
4225 |
if (!field->eq(item->field)) |
|
4226 |
{ |
|
4227 |
add_key_field(key_fields, and_level, cond, item->field, |
|
4228 |
eq_func, val, num_values, usable_tables, |
|
4229 |
sargables); |
|
4230 |
} |
|
4231 |
} |
|
4232 |
} |
|
4233 |
} |
|
4234 |
||
4235 |
static void |
|
4236 |
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level, |
|
4237 |
COND *cond, table_map usable_tables, |
|
4238 |
SARGABLE_PARAM **sargables) |
|
4239 |
{ |
|
4240 |
if (cond->type() == Item_func::COND_ITEM) |
|
4241 |
{ |
|
4242 |
List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list()); |
|
4243 |
KEY_FIELD *org_key_fields= *key_fields; |
|
4244 |
||
4245 |
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC) |
|
4246 |
{ |
|
4247 |
Item *item; |
|
4248 |
while ((item=li++)) |
|
4249 |
add_key_fields(join, key_fields, and_level, item, usable_tables, |
|
4250 |
sargables); |
|
4251 |
for (; org_key_fields != *key_fields ; org_key_fields++) |
|
4252 |
org_key_fields->level= *and_level; |
|
4253 |
} |
|
4254 |
else |
|
4255 |
{ |
|
4256 |
(*and_level)++; |
|
4257 |
add_key_fields(join, key_fields, and_level, li++, usable_tables, |
|
4258 |
sargables); |
|
4259 |
Item *item; |
|
4260 |
while ((item=li++)) |
|
4261 |
{ |
|
4262 |
KEY_FIELD *start_key_fields= *key_fields; |
|
4263 |
(*and_level)++; |
|
4264 |
add_key_fields(join, key_fields, and_level, item, usable_tables, |
|
4265 |
sargables); |
|
4266 |
*key_fields=merge_key_fields(org_key_fields,start_key_fields, |
|
4267 |
*key_fields,++(*and_level)); |
|
4268 |
} |
|
4269 |
} |
|
4270 |
return; |
|
4271 |
} |
|
4272 |
||
4273 |
/* |
|
4274 |
Subquery optimization: Conditions that are pushed down into subqueries |
|
4275 |
are wrapped into Item_func_trig_cond. We process the wrapped condition |
|
4276 |
but need to set cond_guard for KEYUSE elements generated from it. |
|
4277 |
*/ |
|
4278 |
{ |
|
4279 |
if (cond->type() == Item::FUNC_ITEM && |
|
4280 |
((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC) |
|
4281 |
{ |
|
4282 |
Item *cond_arg= ((Item_func*)cond)->arguments()[0]; |
|
4283 |
if (!join->group_list && !join->order && |
|
4284 |
join->unit->item && |
|
4285 |
join->unit->item->substype() == Item_subselect::IN_SUBS && |
|
4286 |
!join->unit->is_union()) |
|
4287 |
{ |
|
4288 |
KEY_FIELD *save= *key_fields; |
|
4289 |
add_key_fields(join, key_fields, and_level, cond_arg, usable_tables, |
|
4290 |
sargables); |
|
4291 |
// Indicate that this ref access candidate is for subquery lookup: |
|
4292 |
for (; save != *key_fields; save++) |
|
4293 |
save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var(); |
|
4294 |
} |
|
4295 |
return; |
|
4296 |
} |
|
4297 |
} |
|
4298 |
||
4299 |
/* If item is of type 'field op field/constant' add it to key_fields */ |
|
4300 |
if (cond->type() != Item::FUNC_ITEM) |
|
4301 |
return; |
|
4302 |
Item_func *cond_func= (Item_func*) cond; |
|
4303 |
switch (cond_func->select_optimize()) { |
|
4304 |
case Item_func::OPTIMIZE_NONE: |
|
4305 |
break; |
|
4306 |
case Item_func::OPTIMIZE_KEY: |
|
4307 |
{ |
|
4308 |
Item **values; |
|
4309 |
// BETWEEN, IN, NE |
|
4310 |
if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM && |
|
4311 |
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT)) |
|
4312 |
{ |
|
4313 |
values= cond_func->arguments()+1; |
|
4314 |
if (cond_func->functype() == Item_func::NE_FUNC && |
|
4315 |
cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM && |
|
4316 |
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT)) |
|
4317 |
values--; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
4318 |
assert(cond_func->functype() != Item_func::IN_FUNC || |
1
by brian
clean slate |
4319 |
cond_func->argument_count() != 2); |
4320 |
add_key_equal_fields(key_fields, *and_level, cond_func, |
|
4321 |
(Item_field*) (cond_func->key_item()->real_item()), |
|
4322 |
0, values, |
|
4323 |
cond_func->argument_count()-1, |
|
4324 |
usable_tables, sargables); |
|
4325 |
} |
|
4326 |
if (cond_func->functype() == Item_func::BETWEEN) |
|
4327 |
{ |
|
4328 |
values= cond_func->arguments(); |
|
4329 |
for (uint i= 1 ; i < cond_func->argument_count() ; i++) |
|
4330 |
{ |
|
4331 |
Item_field *field_item; |
|
4332 |
if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM |
|
4333 |
&& |
|
4334 |
!(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT)) |
|
4335 |
{ |
|
4336 |
field_item= (Item_field *) (cond_func->arguments()[i]->real_item()); |
|
4337 |
add_key_equal_fields(key_fields, *and_level, cond_func, |
|
4338 |
field_item, 0, values, 1, usable_tables, |
|
4339 |
sargables); |
|
4340 |
} |
|
4341 |
} |
|
4342 |
} |
|
4343 |
break; |
|
4344 |
} |
|
4345 |
case Item_func::OPTIMIZE_OP: |
|
4346 |
{ |
|
4347 |
bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC || |
|
4348 |
cond_func->functype() == Item_func::EQUAL_FUNC); |
|
4349 |
||
4350 |
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM && |
|
4351 |
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT)) |
|
4352 |
{ |
|
4353 |
add_key_equal_fields(key_fields, *and_level, cond_func, |
|
4354 |
(Item_field*) (cond_func->arguments()[0])->real_item(), |
|
4355 |
equal_func, |
|
4356 |
cond_func->arguments()+1, 1, usable_tables, |
|
4357 |
sargables); |
|
4358 |
} |
|
4359 |
if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM && |
|
4360 |
cond_func->functype() != Item_func::LIKE_FUNC && |
|
4361 |
!(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT)) |
|
4362 |
{ |
|
4363 |
add_key_equal_fields(key_fields, *and_level, cond_func, |
|
4364 |
(Item_field*) (cond_func->arguments()[1])->real_item(), |
|
4365 |
equal_func, |
|
4366 |
cond_func->arguments(),1,usable_tables, |
|
4367 |
sargables); |
|
4368 |
} |
|
4369 |
break; |
|
4370 |
} |
|
4371 |
case Item_func::OPTIMIZE_NULL: |
|
4372 |
/* column_name IS [NOT] NULL */ |
|
4373 |
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM && |
|
4374 |
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT)) |
|
4375 |
{ |
|
4376 |
Item *tmp=new Item_null; |
|
4377 |
if (unlikely(!tmp)) // Should never be true |
|
4378 |
return; |
|
4379 |
add_key_equal_fields(key_fields, *and_level, cond_func, |
|
4380 |
(Item_field*) (cond_func->arguments()[0])->real_item(), |
|
4381 |
cond_func->functype() == Item_func::ISNULL_FUNC, |
|
4382 |
&tmp, 1, usable_tables, sargables); |
|
4383 |
} |
|
4384 |
break; |
|
4385 |
case Item_func::OPTIMIZE_EQUAL: |
|
4386 |
Item_equal *item_equal= (Item_equal *) cond; |
|
4387 |
Item *const_item= item_equal->get_const(); |
|
4388 |
Item_equal_iterator it(*item_equal); |
|
4389 |
Item_field *item; |
|
4390 |
if (const_item) |
|
4391 |
{ |
|
4392 |
/* |
|
4393 |
For each field field1 from item_equal consider the equality |
|
4394 |
field1=const_item as a condition allowing an index access of the table |
|
4395 |
with field1 by the keys value of field1. |
|
4396 |
*/ |
|
4397 |
while ((item= it++)) |
|
4398 |
{ |
|
4399 |
add_key_field(key_fields, *and_level, cond_func, item->field, |
|
55
by brian
Update for using real bool types. |
4400 |
true, &const_item, 1, usable_tables, sargables); |
1
by brian
clean slate |
4401 |
} |
4402 |
} |
|
4403 |
else |
|
4404 |
{ |
|
4405 |
/* |
|
4406 |
Consider all pairs of different fields included into item_equal. |
|
4407 |
For each of them (field1, field1) consider the equality |
|
4408 |
field1=field2 as a condition allowing an index access of the table |
|
4409 |
with field1 by the keys value of field2. |
|
4410 |
*/ |
|
4411 |
Item_equal_iterator fi(*item_equal); |
|
4412 |
while ((item= fi++)) |
|
4413 |
{ |
|
4414 |
Field *field= item->field; |
|
4415 |
while ((item= it++)) |
|
4416 |
{ |
|
4417 |
if (!field->eq(item->field)) |
|
4418 |
{ |
|
4419 |
add_key_field(key_fields, *and_level, cond_func, field, |
|
55
by brian
Update for using real bool types. |
4420 |
true, (Item **) &item, 1, usable_tables, |
1
by brian
clean slate |
4421 |
sargables); |
4422 |
} |
|
4423 |
} |
|
4424 |
it.rewind(); |
|
4425 |
} |
|
4426 |
} |
|
4427 |
break; |
|
4428 |
} |
|
4429 |
} |
|
4430 |
||
4431 |
/** |
|
4432 |
Add all keys with uses 'field' for some keypart. |
|
4433 |
||
4434 |
If field->and_level != and_level then only mark key_part as const_part. |
|
4435 |
*/ |
|
4436 |
||
4437 |
static uint |
|
4438 |
max_part_bit(key_part_map bits) |
|
4439 |
{ |
|
4440 |
uint found; |
|
4441 |
for (found=0; bits & 1 ; found++,bits>>=1) ; |
|
4442 |
return found; |
|
4443 |
} |
|
4444 |
||
4445 |
static void |
|
4446 |
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field) |
|
4447 |
{ |
|
4448 |
Field *field=key_field->field; |
|
4449 |
TABLE *form= field->table; |
|
4450 |
KEYUSE keyuse; |
|
4451 |
||
4452 |
if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS)) |
|
4453 |
{ |
|
4454 |
for (uint key=0 ; key < form->s->keys ; key++) |
|
4455 |
{ |
|
4456 |
if (!(form->keys_in_use_for_query.is_set(key))) |
|
4457 |
continue; |
|
4458 |
||
4459 |
uint key_parts= (uint) form->key_info[key].key_parts; |
|
4460 |
for (uint part=0 ; part < key_parts ; part++) |
|
4461 |
{ |
|
4462 |
if (field->eq(form->key_info[key].key_part[part].field)) |
|
4463 |
{ |
|
4464 |
keyuse.table= field->table; |
|
4465 |
keyuse.val = key_field->val; |
|
4466 |
keyuse.key = key; |
|
4467 |
keyuse.keypart=part; |
|
4468 |
keyuse.keypart_map= (key_part_map) 1 << part; |
|
4469 |
keyuse.used_tables=key_field->val->used_tables(); |
|
4470 |
keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL; |
|
4471 |
keyuse.null_rejecting= key_field->null_rejecting; |
|
4472 |
keyuse.cond_guard= key_field->cond_guard; |
|
4473 |
keyuse.sj_pred_no= key_field->sj_pred_no; |
|
4474 |
VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse)); |
|
4475 |
} |
|
4476 |
} |
|
4477 |
} |
|
4478 |
} |
|
4479 |
} |
|
4480 |
||
4481 |
static int |
|
4482 |
sort_keyuse(KEYUSE *a,KEYUSE *b) |
|
4483 |
{ |
|
4484 |
int res; |
|
4485 |
if (a->table->tablenr != b->table->tablenr) |
|
4486 |
return (int) (a->table->tablenr - b->table->tablenr); |
|
4487 |
if (a->key != b->key) |
|
4488 |
return (int) (a->key - b->key); |
|
4489 |
if (a->keypart != b->keypart) |
|
4490 |
return (int) (a->keypart - b->keypart); |
|
4491 |
// Place const values before other ones |
|
4492 |
if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) - |
|
4493 |
test((b->used_tables & ~OUTER_REF_TABLE_BIT)))) |
|
4494 |
return res; |
|
4495 |
/* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */ |
|
4496 |
return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) - |
|
4497 |
(b->optimize & KEY_OPTIMIZE_REF_OR_NULL)); |
|
4498 |
} |
|
4499 |
||
4500 |
||
4501 |
/* |
|
4502 |
Add to KEY_FIELD array all 'ref' access candidates within nested join. |
|
4503 |
||
4504 |
This function populates KEY_FIELD array with entries generated from the |
|
4505 |
ON condition of the given nested join, and does the same for nested joins |
|
4506 |
contained within this nested join. |
|
4507 |
||
4508 |
@param[in] nested_join_table Nested join pseudo-table to process |
|
4509 |
@param[in,out] end End of the key field array |
|
4510 |
@param[in,out] and_level And-level |
|
4511 |
@param[in,out] sargables Array of found sargable candidates |
|
4512 |
||
4513 |
||
4514 |
@note |
|
4515 |
We can add accesses to the tables that are direct children of this nested |
|
4516 |
join (1), and are not inner tables w.r.t their neighbours (2). |
|
4517 |
||
4518 |
Example for #1 (outer brackets pair denotes nested join this function is |
|
4519 |
invoked for): |
|
4520 |
@code |
|
4521 |
... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond |
|
4522 |
@endcode |
|
4523 |
Example for #2: |
|
4524 |
@code |
|
4525 |
... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond |
|
4526 |
@endcode |
|
4527 |
In examples 1-2 for condition cond, we can add 'ref' access candidates to |
|
4528 |
t1 only. |
|
4529 |
Example #3: |
|
4530 |
@code |
|
4531 |
... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond |
|
4532 |
@endcode |
|
4533 |
Here we can add 'ref' access candidates for t1 and t2, but not for t3. |
|
4534 |
*/ |
|
4535 |
||
4536 |
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table, |
|
4537 |
KEY_FIELD **end, uint *and_level, |
|
4538 |
SARGABLE_PARAM **sargables) |
|
4539 |
{ |
|
4540 |
List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list); |
|
4541 |
List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list); |
|
55
by brian
Update for using real bool types. |
4542 |
bool have_another = false; |
1
by brian
clean slate |
4543 |
table_map tables= 0; |
4544 |
TABLE_LIST *table; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
4545 |
assert(nested_join_table->nested_join); |
1
by brian
clean slate |
4546 |
|
55
by brian
Update for using real bool types. |
4547 |
while ((table= li++) || (have_another && (li=li2, have_another=false, |
1
by brian
clean slate |
4548 |
(table= li++)))) |
4549 |
{ |
|
4550 |
if (table->nested_join) |
|
4551 |
{ |
|
4552 |
if (!table->on_expr) |
|
4553 |
{ |
|
4554 |
/* It's a semi-join nest. Walk into it as if it wasn't a nest */ |
|
55
by brian
Update for using real bool types. |
4555 |
have_another= true; |
1
by brian
clean slate |
4556 |
li2= li; |
4557 |
li= List_iterator<TABLE_LIST>(table->nested_join->join_list); |
|
4558 |
} |
|
4559 |
else |
|
4560 |
add_key_fields_for_nj(join, table, end, and_level, sargables); |
|
4561 |
} |
|
4562 |
else |
|
4563 |
if (!table->on_expr) |
|
4564 |
tables |= table->table->map; |
|
4565 |
} |
|
4566 |
if (nested_join_table->on_expr) |
|
4567 |
add_key_fields(join, end, and_level, nested_join_table->on_expr, tables, |
|
4568 |
sargables); |
|
4569 |
} |
|
4570 |
||
4571 |
||
4572 |
/** |
|
4573 |
Update keyuse array with all possible keys we can use to fetch rows. |
|
4574 |
||
4575 |
@param thd |
|
4576 |
@param[out] keyuse Put here ordered array of KEYUSE structures |
|
4577 |
@param join_tab Array in tablenr_order |
|
4578 |
@param tables Number of tables in join |
|
4579 |
@param cond WHERE condition (note that the function analyzes |
|
4580 |
join_tab[i]->on_expr too) |
|
4581 |
@param normal_tables Tables not inner w.r.t some outer join (ones |
|
4582 |
for which we can make ref access based the WHERE |
|
4583 |
clause) |
|
4584 |
@param select_lex current SELECT |
|
4585 |
@param[out] sargables Array of found sargable candidates |
|
4586 |
||
4587 |
@retval |
|
4588 |
0 OK |
|
4589 |
@retval |
|
4590 |
1 Out of memory. |
|
4591 |
*/ |
|
4592 |
||
4593 |
static bool |
|
4594 |
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab, |
|
77.1.45
by Monty Taylor
Warning fixes. |
4595 |
uint tables, COND *cond, |
4596 |
COND_EQUAL *cond_equal __attribute__((__unused__)), |
|
1
by brian
clean slate |
4597 |
table_map normal_tables, SELECT_LEX *select_lex, |
4598 |
SARGABLE_PARAM **sargables) |
|
4599 |
{ |
|
4600 |
uint and_level,i,found_eq_constant; |
|
4601 |
KEY_FIELD *key_fields, *end, *field; |
|
4602 |
uint sz; |
|
4603 |
uint m= max(select_lex->max_equal_elems,1); |
|
4604 |
||
4605 |
/* |
|
4606 |
We use the same piece of memory to store both KEY_FIELD |
|
4607 |
and SARGABLE_PARAM structure. |
|
4608 |
KEY_FIELD values are placed at the beginning this memory |
|
4609 |
while SARGABLE_PARAM values are put at the end. |
|
4610 |
All predicates that are used to fill arrays of KEY_FIELD |
|
4611 |
and SARGABLE_PARAM structures have at most 2 arguments |
|
4612 |
except BETWEEN predicates that have 3 arguments and |
|
4613 |
IN predicates. |
|
4614 |
This any predicate if it's not BETWEEN/IN can be used |
|
4615 |
directly to fill at most 2 array elements, either of KEY_FIELD |
|
4616 |
or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements |
|
4617 |
can be filled as this predicate is considered as |
|
4618 |
saragable with respect to each of its argument. |
|
4619 |
An IN predicate can require at most 1 element as currently |
|
4620 |
it is considered as sargable only for its first argument. |
|
4621 |
Multiple equality can add elements that are filled after |
|
4622 |
substitution of field arguments by equal fields. There |
|
4623 |
can be not more than select_lex->max_equal_elems such |
|
4624 |
substitutions. |
|
4625 |
*/ |
|
4626 |
sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))* |
|
4627 |
(((thd->lex->current_select->cond_count+1)*2 + |
|
4628 |
thd->lex->current_select->between_count)*m+1); |
|
4629 |
if (!(key_fields=(KEY_FIELD*) thd->alloc(sz))) |
|
55
by brian
Update for using real bool types. |
4630 |
return true; /* purecov: inspected */ |
1
by brian
clean slate |
4631 |
and_level= 0; |
4632 |
field= end= key_fields; |
|
4633 |
*sargables= (SARGABLE_PARAM *) key_fields + |
|
4634 |
(sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM); |
|
4635 |
/* set a barrier for the array of SARGABLE_PARAM */ |
|
4636 |
(*sargables)[0].field= 0; |
|
4637 |
||
4638 |
if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64)) |
|
55
by brian
Update for using real bool types. |
4639 |
return true; |
1
by brian
clean slate |
4640 |
if (cond) |
4641 |
{ |
|
4642 |
add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables, |
|
4643 |
sargables); |
|
4644 |
for (; field != end ; field++) |
|
4645 |
{ |
|
4646 |
add_key_part(keyuse,field); |
|
4647 |
/* Mark that we can optimize LEFT JOIN */ |
|
4648 |
if (field->val->type() == Item::NULL_ITEM && |
|
4649 |
!field->field->real_maybe_null()) |
|
4650 |
field->field->table->reginfo.not_exists_optimize=1; |
|
4651 |
} |
|
4652 |
} |
|
4653 |
for (i=0 ; i < tables ; i++) |
|
4654 |
{ |
|
4655 |
/* |
|
4656 |
Block the creation of keys for inner tables of outer joins. |
|
4657 |
Here only the outer joins that can not be converted to |
|
4658 |
inner joins are left and all nests that can be eliminated |
|
4659 |
are flattened. |
|
4660 |
In the future when we introduce conditional accesses |
|
4661 |
for inner tables in outer joins these keys will be taken |
|
4662 |
into account as well. |
|
4663 |
*/ |
|
4664 |
if (*join_tab[i].on_expr_ref) |
|
4665 |
add_key_fields(join_tab->join, &end, &and_level, |
|
4666 |
*join_tab[i].on_expr_ref, |
|
4667 |
join_tab[i].table->map, sargables); |
|
4668 |
} |
|
4669 |
||
4670 |
/* Process ON conditions for the nested joins */ |
|
4671 |
{ |
|
4672 |
List_iterator<TABLE_LIST> li(*join_tab->join->join_list); |
|
4673 |
TABLE_LIST *table; |
|
4674 |
while ((table= li++)) |
|
4675 |
{ |
|
4676 |
if (table->nested_join) |
|
4677 |
add_key_fields_for_nj(join_tab->join, table, &end, &and_level, |
|
4678 |
sargables); |
|
4679 |
} |
|
4680 |
} |
|
4681 |
||
4682 |
/* fill keyuse with found key parts */ |
|
4683 |
for ( ; field != end ; field++) |
|
4684 |
add_key_part(keyuse,field); |
|
4685 |
||
4686 |
/* |
|
4687 |
Sort the array of possible keys and remove the following key parts: |
|
4688 |
- ref if there is a keypart which is a ref and a const. |
|
4689 |
(e.g. if there is a key(a,b) and the clause is a=3 and b=7 and b=t2.d, |
|
4690 |
then we skip the key part corresponding to b=t2.d) |
|
4691 |
- keyparts without previous keyparts |
|
4692 |
(e.g. if there is a key(a,b,c) but only b < 5 (or a=2 and c < 3) is |
|
4693 |
used in the query, we drop the partial key parts from consideration). |
|
4694 |
Special treatment for ft-keys. |
|
4695 |
*/ |
|
4696 |
if (keyuse->elements) |
|
4697 |
{ |
|
4698 |
KEYUSE key_end,*prev,*save_pos,*use; |
|
4699 |
||
4700 |
my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE), |
|
4701 |
(qsort_cmp) sort_keyuse); |
|
4702 |
||
4703 |
bzero((char*) &key_end,sizeof(key_end)); /* Add for easy testing */ |
|
4704 |
VOID(insert_dynamic(keyuse,(uchar*) &key_end)); |
|
4705 |
||
4706 |
use=save_pos=dynamic_element(keyuse,0,KEYUSE*); |
|
4707 |
prev= &key_end; |
|
4708 |
found_eq_constant=0; |
|
4709 |
for (i=0 ; i < keyuse->elements-1 ; i++,use++) |
|
4710 |
{ |
|
4711 |
if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL) |
|
4712 |
use->table->const_key_parts[use->key]|= use->keypart_map; |
|
4713 |
{ |
|
4714 |
if (use->key == prev->key && use->table == prev->table) |
|
4715 |
{ |
|
4716 |
if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant)) |
|
4717 |
continue; /* remove */ |
|
4718 |
} |
|
4719 |
else if (use->keypart != 0) // First found must be 0 |
|
4720 |
continue; |
|
4721 |
} |
|
4722 |
||
4723 |
#ifdef HAVE_purify |
|
4724 |
/* Valgrind complains about overlapped memcpy when save_pos==use. */ |
|
4725 |
if (save_pos != use) |
|
4726 |
#endif |
|
4727 |
*save_pos= *use; |
|
4728 |
prev=use; |
|
4729 |
found_eq_constant= !use->used_tables; |
|
4730 |
/* Save ptr to first use */ |
|
4731 |
if (!use->table->reginfo.join_tab->keyuse) |
|
4732 |
use->table->reginfo.join_tab->keyuse=save_pos; |
|
4733 |
use->table->reginfo.join_tab->checked_keys.set_bit(use->key); |
|
4734 |
save_pos++; |
|
4735 |
} |
|
4736 |
i=(uint) (save_pos-(KEYUSE*) keyuse->buffer); |
|
4737 |
VOID(set_dynamic(keyuse,(uchar*) &key_end,i)); |
|
4738 |
keyuse->elements=i; |
|
4739 |
} |
|
55
by brian
Update for using real bool types. |
4740 |
return false; |
1
by brian
clean slate |
4741 |
} |
4742 |
||
4743 |
/** |
|
4744 |
Update some values in keyuse for faster choose_plan() loop. |
|
4745 |
*/ |
|
4746 |
||
4747 |
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array) |
|
4748 |
{ |
|
4749 |
KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*); |
|
4750 |
||
4751 |
for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++) |
|
4752 |
{ |
|
4753 |
table_map map; |
|
4754 |
/* |
|
4755 |
If we find a ref, assume this table matches a proportional |
|
4756 |
part of this table. |
|
4757 |
For example 100 records matching a table with 5000 records |
|
4758 |
gives 5000/100 = 50 records per key |
|
4759 |
Constant tables are ignored. |
|
4760 |
To avoid bad matches, we don't make ref_table_rows less than 100. |
|
4761 |
*/ |
|
4762 |
keyuse->ref_table_rows= ~(ha_rows) 0; // If no ref |
|
4763 |
if (keyuse->used_tables & |
|
4764 |
(map= (keyuse->used_tables & ~join->const_table_map & |
|
4765 |
~OUTER_REF_TABLE_BIT))) |
|
4766 |
{ |
|
4767 |
uint tablenr; |
|
4768 |
for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ; |
|
4769 |
if (map == 1) // Only one table |
|
4770 |
{ |
|
4771 |
TABLE *tmp_table=join->all_tables[tablenr]; |
|
4772 |
keyuse->ref_table_rows= max(tmp_table->file->stats.records, 100); |
|
4773 |
} |
|
4774 |
} |
|
4775 |
/* |
|
4776 |
Outer reference (external field) is constant for single executing |
|
4777 |
of subquery |
|
4778 |
*/ |
|
4779 |
if (keyuse->used_tables == OUTER_REF_TABLE_BIT) |
|
4780 |
keyuse->ref_table_rows= 1; |
|
4781 |
} |
|
4782 |
} |
|
4783 |
||
4784 |
||
4785 |
/** |
|
4786 |
Discover the indexes that can be used for GROUP BY or DISTINCT queries. |
|
4787 |
||
4788 |
If the query has a GROUP BY clause, find all indexes that contain all |
|
4789 |
GROUP BY fields, and add those indexes to join->const_keys. |
|
4790 |
||
4791 |
If the query has a DISTINCT clause, find all indexes that contain all |
|
4792 |
SELECT fields, and add those indexes to join->const_keys. |
|
4793 |
This allows later on such queries to be processed by a |
|
4794 |
QUICK_GROUP_MIN_MAX_SELECT. |
|
4795 |
||
4796 |
@param join |
|
4797 |
@param join_tab |
|
4798 |
||
4799 |
@return |
|
4800 |
None |
|
4801 |
*/ |
|
4802 |
||
4803 |
static void |
|
4804 |
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab) |
|
4805 |
{ |
|
4806 |
List<Item_field> indexed_fields; |
|
4807 |
List_iterator<Item_field> indexed_fields_it(indexed_fields); |
|
4808 |
ORDER *cur_group; |
|
4809 |
Item_field *cur_item; |
|
4810 |
key_map possible_keys(0); |
|
4811 |
||
4812 |
if (join->group_list) |
|
4813 |
{ /* Collect all query fields referenced in the GROUP clause. */ |
|
4814 |
for (cur_group= join->group_list; cur_group; cur_group= cur_group->next) |
|
4815 |
(*cur_group->item)->walk(&Item::collect_item_field_processor, 0, |
|
4816 |
(uchar*) &indexed_fields); |
|
4817 |
} |
|
4818 |
else if (join->select_distinct) |
|
4819 |
{ /* Collect all query fields referenced in the SELECT clause. */ |
|
4820 |
List<Item> &select_items= join->fields_list; |
|
4821 |
List_iterator<Item> select_items_it(select_items); |
|
4822 |
Item *item; |
|
4823 |
while ((item= select_items_it++)) |
|
4824 |
item->walk(&Item::collect_item_field_processor, 0, |
|
4825 |
(uchar*) &indexed_fields); |
|
4826 |
} |
|
4827 |
else |
|
4828 |
return; |
|
4829 |
||
4830 |
if (indexed_fields.elements == 0) |
|
4831 |
return; |
|
4832 |
||
4833 |
/* Intersect the keys of all group fields. */ |
|
4834 |
cur_item= indexed_fields_it++; |
|
4835 |
possible_keys.merge(cur_item->field->part_of_key); |
|
4836 |
while ((cur_item= indexed_fields_it++)) |
|
4837 |
{ |
|
4838 |
possible_keys.intersect(cur_item->field->part_of_key); |
|
4839 |
} |
|
4840 |
||
4841 |
if (!possible_keys.is_clear_all()) |
|
4842 |
join_tab->const_keys.merge(possible_keys); |
|
4843 |
} |
|
4844 |
||
4845 |
||
4846 |
/***************************************************************************** |
|
4847 |
Go through all combinations of not marked tables and find the one |
|
4848 |
which uses least records |
|
4849 |
*****************************************************************************/ |
|
4850 |
||
4851 |
/** Save const tables first as used tables. */ |
|
4852 |
||
4853 |
static void |
|
4854 |
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key) |
|
4855 |
{ |
|
4856 |
join->positions[idx].table= table; |
|
4857 |
join->positions[idx].key=key; |
|
4858 |
join->positions[idx].records_read=1.0; /* This is a const table */ |
|
4859 |
join->positions[idx].ref_depend_map= 0; |
|
4860 |
||
4861 |
/* Move the const table as down as possible in best_ref */ |
|
4862 |
JOIN_TAB **pos=join->best_ref+idx+1; |
|
4863 |
JOIN_TAB *next=join->best_ref[idx]; |
|
4864 |
for (;next != table ; pos++) |
|
4865 |
{ |
|
4866 |
JOIN_TAB *tmp=pos[0]; |
|
4867 |
pos[0]=next; |
|
4868 |
next=tmp; |
|
4869 |
} |
|
4870 |
join->best_ref[idx]=table; |
|
4871 |
} |
|
4872 |
||
4873 |
||
4874 |
/* |
|
4875 |
Given a semi-join nest, find out which of the IN-equalities are bound |
|
4876 |
||
4877 |
SYNOPSIS |
|
4878 |
get_bound_sj_equalities() |
|
4879 |
sj_nest Semi-join nest |
|
4880 |
remaining_tables Tables that are not yet bound |
|
4881 |
||
4882 |
DESCRIPTION |
|
4883 |
Given a semi-join nest, find out which of the IN-equalities have their |
|
4884 |
left part expression bound (i.e. the said expression doesn't refer to |
|
4885 |
any of remaining_tables and can be evaluated). |
|
4886 |
||
4887 |
RETURN |
|
4888 |
Bitmap of bound IN-equalities. |
|
4889 |
*/ |
|
4890 |
||
151
by Brian Aker
Ulonglong to uint64_t |
4891 |
uint64_t get_bound_sj_equalities(TABLE_LIST *sj_nest, |
1
by brian
clean slate |
4892 |
table_map remaining_tables) |
4893 |
{ |
|
4894 |
List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list); |
|
4895 |
Item *item; |
|
4896 |
uint i= 0; |
|
151
by Brian Aker
Ulonglong to uint64_t |
4897 |
uint64_t res= 0; |
1
by brian
clean slate |
4898 |
while ((item= li++)) |
4899 |
{ |
|
4900 |
/* |
|
4901 |
Q: should this take into account equality propagation and how? |
|
4902 |
A: If e->outer_side is an Item_field, walk over the equality |
|
4903 |
class and see if there is an element that is bound? |
|
4904 |
(this is an optional feature) |
|
4905 |
*/ |
|
4906 |
if (!(item->used_tables() & remaining_tables)) |
|
4907 |
{ |
|
4908 |
res |= 1ULL < i; |
|
4909 |
} |
|
4910 |
} |
|
4911 |
return res; |
|
4912 |
} |
|
4913 |
||
4914 |
||
4915 |
/** |
|
4916 |
Find the best access path for an extension of a partial execution |
|
4917 |
plan and add this path to the plan. |
|
4918 |
||
4919 |
The function finds the best access path to table 's' from the passed |
|
4920 |
partial plan where an access path is the general term for any means to |
|
4921 |
access the data in 's'. An access path may use either an index or a scan, |
|
4922 |
whichever is cheaper. The input partial plan is passed via the array |
|
4923 |
'join->positions' of length 'idx'. The chosen access method for 's' and its |
|
4924 |
cost are stored in 'join->positions[idx]'. |
|
4925 |
||
4926 |
@param join pointer to the structure providing all context info |
|
4927 |
for the query |
|
4928 |
@param s the table to be joined by the function |
|
4929 |
@param thd thread for the connection that submitted the query |
|
4930 |
@param remaining_tables set of tables not included into the partial plan yet |
|
4931 |
@param idx the length of the partial plan |
|
4932 |
@param record_count estimate for the number of records returned by the |
|
4933 |
partial plan |
|
4934 |
@param read_time the cost of the partial plan |
|
4935 |
||
4936 |
@return |
|
4937 |
None |
|
4938 |
*/ |
|
4939 |
||
4940 |
static void |
|
4941 |
best_access_path(JOIN *join, |
|
4942 |
JOIN_TAB *s, |
|
4943 |
THD *thd, |
|
4944 |
table_map remaining_tables, |
|
4945 |
uint idx, |
|
4946 |
double record_count, |
|
77.1.45
by Monty Taylor
Warning fixes. |
4947 |
double read_time __attribute__((__unused__))) |
1
by brian
clean slate |
4948 |
{ |
4949 |
KEYUSE *best_key= 0; |
|
4950 |
uint best_max_key_part= 0; |
|
148
by Brian Aker
my_bool cleanup |
4951 |
bool found_constraint= 0; |
1
by brian
clean slate |
4952 |
double best= DBL_MAX; |
4953 |
double best_time= DBL_MAX; |
|
4954 |
double records= DBL_MAX; |
|
4955 |
table_map best_ref_depends_map= 0; |
|
4956 |
double tmp; |
|
4957 |
ha_rows rec; |
|
4958 |
uint best_is_sj_inside_out= 0; |
|
4959 |
||
4960 |
if (s->keyuse) |
|
4961 |
{ /* Use key if possible */ |
|
4962 |
TABLE *table= s->table; |
|
4963 |
KEYUSE *keyuse,*start_key=0; |
|
4964 |
double best_records= DBL_MAX; |
|
4965 |
uint max_key_part=0; |
|
151
by Brian Aker
Ulonglong to uint64_t |
4966 |
uint64_t bound_sj_equalities= 0; |
55
by brian
Update for using real bool types. |
4967 |
bool try_sj_inside_out= false; |
1
by brian
clean slate |
4968 |
/* |
4969 |
Discover the bound equalites. We need to do this, if |
|
4970 |
1. The next table is an SJ-inner table, and |
|
4971 |
2. It is the first table from that semijoin, and |
|
4972 |
3. We're not within a semi-join range (i.e. all semi-joins either have |
|
4973 |
all or none of their tables in join_table_map), except |
|
4974 |
s->emb_sj_nest (which we've just entered). |
|
4975 |
3. All correlation references from this sj-nest are bound |
|
4976 |
*/ |
|
4977 |
if (s->emb_sj_nest && // (1) |
|
4978 |
s->emb_sj_nest->sj_in_exprs < 64 && |
|
4979 |
((remaining_tables & s->emb_sj_nest->sj_inner_tables) == // (2) |
|
4980 |
s->emb_sj_nest->sj_inner_tables) && // (2) |
|
4981 |
join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables && // (3) |
|
4982 |
!(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4) |
|
4983 |
{ |
|
4984 |
/* This table is an InsideOut scan candidate */ |
|
4985 |
bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest, |
|
4986 |
remaining_tables); |
|
55
by brian
Update for using real bool types. |
4987 |
try_sj_inside_out= true; |
1
by brian
clean slate |
4988 |
} |
4989 |
||
4990 |
/* Test how we can use keys */ |
|
4991 |
rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE; // Assumed records/key |
|
4992 |
for (keyuse=s->keyuse ; keyuse->table == table ;) |
|
4993 |
{ |
|
4994 |
key_part_map found_part= 0; |
|
4995 |
table_map found_ref= 0; |
|
4996 |
uint key= keyuse->key; |
|
4997 |
KEY *keyinfo= table->key_info+key; |
|
4998 |
/* Bitmap of keyparts where the ref access is over 'keypart=const': */ |
|
4999 |
key_part_map const_part= 0; |
|
5000 |
/* The or-null keypart in ref-or-null access: */ |
|
5001 |
key_part_map ref_or_null_part= 0; |
|
5002 |
||
5003 |
/* Calculate how many key segments of the current key we can use */ |
|
5004 |
start_key= keyuse; |
|
151
by Brian Aker
Ulonglong to uint64_t |
5005 |
uint64_t handled_sj_equalities=0; |
1
by brian
clean slate |
5006 |
key_part_map sj_insideout_map= 0; |
5007 |
||
5008 |
do /* For each keypart */ |
|
5009 |
{ |
|
5010 |
uint keypart= keyuse->keypart; |
|
5011 |
table_map best_part_found_ref= 0; |
|
5012 |
double best_prev_record_reads= DBL_MAX; |
|
5013 |
||
5014 |
do /* For each way to access the keypart */ |
|
5015 |
{ |
|
5016 |
||
5017 |
/* |
|
5018 |
if 1. expression doesn't refer to forward tables |
|
5019 |
2. we won't get two ref-or-null's |
|
5020 |
*/ |
|
5021 |
if (!(remaining_tables & keyuse->used_tables) && |
|
5022 |
!(ref_or_null_part && (keyuse->optimize & |
|
5023 |
KEY_OPTIMIZE_REF_OR_NULL))) |
|
5024 |
{ |
|
5025 |
found_part|= keyuse->keypart_map; |
|
5026 |
if (!(keyuse->used_tables & ~join->const_table_map)) |
|
5027 |
const_part|= keyuse->keypart_map; |
|
5028 |
||
5029 |
double tmp2= prev_record_reads(join, idx, (found_ref | |
|
5030 |
keyuse->used_tables)); |
|
5031 |
if (tmp2 < best_prev_record_reads) |
|
5032 |
{ |
|
5033 |
best_part_found_ref= keyuse->used_tables & ~join->const_table_map; |
|
5034 |
best_prev_record_reads= tmp2; |
|
5035 |
} |
|
5036 |
if (rec > keyuse->ref_table_rows) |
|
5037 |
rec= keyuse->ref_table_rows; |
|
5038 |
/* |
|
5039 |
If there is one 'key_column IS NULL' expression, we can |
|
5040 |
use this ref_or_null optimisation of this field |
|
5041 |
*/ |
|
5042 |
if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) |
|
5043 |
ref_or_null_part |= keyuse->keypart_map; |
|
5044 |
} |
|
5045 |
||
5046 |
if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX) |
|
5047 |
{ |
|
5048 |
if (!(remaining_tables & keyuse->used_tables)) |
|
5049 |
bound_sj_equalities |= 1ULL << keyuse->sj_pred_no; |
|
5050 |
else |
|
5051 |
{ |
|
5052 |
handled_sj_equalities |= 1ULL << keyuse->sj_pred_no; |
|
5053 |
sj_insideout_map |= ((key_part_map)1) << keyuse->keypart; |
|
5054 |
} |
|
5055 |
} |
|
5056 |
||
5057 |
keyuse++; |
|
5058 |
} while (keyuse->table == table && keyuse->key == key && |
|
5059 |
keyuse->keypart == keypart); |
|
5060 |
found_ref|= best_part_found_ref; |
|
5061 |
} while (keyuse->table == table && keyuse->key == key); |
|
5062 |
||
5063 |
/* |
|
5064 |
Assume that that each key matches a proportional part of table. |
|
5065 |
*/ |
|
5066 |
if (!found_part && !handled_sj_equalities) |
|
5067 |
continue; // Nothing usable found |
|
5068 |
||
5069 |
if (rec < MATCHING_ROWS_IN_OTHER_TABLE) |
|
5070 |
rec= MATCHING_ROWS_IN_OTHER_TABLE; // Fix for small tables |
|
5071 |
||
55
by brian
Update for using real bool types. |
5072 |
bool sj_inside_out_scan= false; |
1
by brian
clean slate |
5073 |
{ |
5074 |
found_constraint= 1; |
|
5075 |
/* |
|
5076 |
Check if InsideOut scan is applicable: |
|
5077 |
1. All IN-equalities are either "bound" or "handled" |
|
5078 |
2. Index keyparts are |
|
5079 |
... |
|
5080 |
*/ |
|
5081 |
if (try_sj_inside_out && |
|
5082 |
table->covering_keys.is_set(key) && |
|
5083 |
(handled_sj_equalities | bound_sj_equalities) == // (1) |
|
151
by Brian Aker
Ulonglong to uint64_t |
5084 |
PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1) |
1
by brian
clean slate |
5085 |
{ |
5086 |
uint n_fixed_parts= max_part_bit(found_part); |
|
5087 |
if (n_fixed_parts != keyinfo->key_parts && |
|
5088 |
(PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) == |
|
5089 |
PREV_BITS(uint, keyinfo->key_parts)) |
|
5090 |
{ |
|
5091 |
/* |
|
5092 |
Not all parts are fixed. Produce bitmap of remaining bits and |
|
5093 |
check if all of them are covered. |
|
5094 |
*/ |
|
55
by brian
Update for using real bool types. |
5095 |
sj_inside_out_scan= true; |
1
by brian
clean slate |
5096 |
if (!n_fixed_parts) |
5097 |
{ |
|
5098 |
/* |
|
5099 |
It's a confluent ref scan. |
|
5100 |
||
5101 |
That is, all found KEYUSE elements refer to IN-equalities, |
|
5102 |
and there is really no ref access because there is no |
|
5103 |
t.keypart0 = {bound expression} |
|
5104 |
||
5105 |
Calculate the cost of complete loose index scan. |
|
5106 |
*/ |
|
5107 |
records= (double)s->table->file->stats.records; |
|
5108 |
||
5109 |
/* The cost is entire index scan cost (divided by 2) */ |
|
5110 |
best_time= s->table->file->index_only_read_time(key, records); |
|
5111 |
||
5112 |
/* Now figure how many different keys we will get */ |
|
5113 |
ulong rpc; |
|
5114 |
if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1])) |
|
5115 |
records= records / rpc; |
|
5116 |
start_key= NULL; |
|
5117 |
} |
|
5118 |
} |
|
5119 |
} |
|
5120 |
||
5121 |
/* |
|
5122 |
Check if we found full key |
|
5123 |
*/ |
|
5124 |
if (found_part == PREV_BITS(uint,keyinfo->key_parts) && |
|
5125 |
!ref_or_null_part) |
|
5126 |
{ /* use eq key */ |
|
5127 |
max_key_part= (uint) ~0; |
|
5128 |
if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME) |
|
5129 |
{ |
|
5130 |
tmp = prev_record_reads(join, idx, found_ref); |
|
5131 |
records=1.0; |
|
5132 |
} |
|
5133 |
else |
|
5134 |
{ |
|
5135 |
if (!found_ref) |
|
5136 |
{ /* We found a const key */ |
|
5137 |
/* |
|
5138 |
ReuseRangeEstimateForRef-1: |
|
5139 |
We get here if we've found a ref(const) (c_i are constants): |
|
5140 |
"(keypart1=c1) AND ... AND (keypartN=cN)" [ref_const_cond] |
|
5141 |
||
5142 |
If range optimizer was able to construct a "range" |
|
5143 |
access on this index, then its condition "quick_cond" was |
|
5144 |
eqivalent to ref_const_cond (*), and we can re-use E(#rows) |
|
5145 |
from the range optimizer. |
|
5146 |
||
5147 |
Proof of (*): By properties of range and ref optimizers |
|
5148 |
quick_cond will be equal or tighther than ref_const_cond. |
|
5149 |
ref_const_cond already covers "smallest" possible interval - |
|
5150 |
a singlepoint interval over all keyparts. Therefore, |
|
5151 |
quick_cond is equivalent to ref_const_cond (if it was an |
|
5152 |
empty interval we wouldn't have got here). |
|
5153 |
*/ |
|
5154 |
if (table->quick_keys.is_set(key)) |
|
5155 |
records= (double) table->quick_rows[key]; |
|
5156 |
else |
|
5157 |
{ |
|
5158 |
/* quick_range couldn't use key! */ |
|
5159 |
records= (double) s->records/rec; |
|
5160 |
} |
|
5161 |
} |
|
5162 |
else |
|
5163 |
{ |
|
5164 |
if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1])) |
|
5165 |
{ /* Prefer longer keys */ |
|
5166 |
records= |
|
5167 |
((double) s->records / (double) rec * |
|
5168 |
(1.0 + |
|
5169 |
((double) (table->s->max_key_length-keyinfo->key_length) / |
|
5170 |
(double) table->s->max_key_length))); |
|
5171 |
if (records < 2.0) |
|
5172 |
records=2.0; /* Can't be as good as a unique */ |
|
5173 |
} |
|
5174 |
/* |
|
5175 |
ReuseRangeEstimateForRef-2: We get here if we could not reuse |
|
5176 |
E(#rows) from range optimizer. Make another try: |
|
5177 |
||
5178 |
If range optimizer produced E(#rows) for a prefix of the ref |
|
5179 |
access we're considering, and that E(#rows) is lower then our |
|
5180 |
current estimate, make an adjustment. The criteria of when we |
|
5181 |
can make an adjustment is a special case of the criteria used |
|
5182 |
in ReuseRangeEstimateForRef-3. |
|
5183 |
*/ |
|
5184 |
if (table->quick_keys.is_set(key) && |
|
5185 |
const_part & (1 << table->quick_key_parts[key]) && |
|
5186 |
table->quick_n_ranges[key] == 1 && |
|
5187 |
records > (double) table->quick_rows[key]) |
|
5188 |
{ |
|
5189 |
records= (double) table->quick_rows[key]; |
|
5190 |
} |
|
5191 |
} |
|
5192 |
/* Limit the number of matched rows */ |
|
5193 |
tmp= records; |
|
5194 |
set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key); |
|
5195 |
if (table->covering_keys.is_set(key)) |
|
5196 |
{ |
|
5197 |
/* we can use only index tree */ |
|
5198 |
tmp= record_count * table->file->index_only_read_time(key, tmp); |
|
5199 |
} |
|
5200 |
else |
|
5201 |
tmp= record_count*min(tmp,s->worst_seeks); |
|
5202 |
} |
|
5203 |
} |
|
5204 |
else |
|
5205 |
{ |
|
5206 |
/* |
|
5207 |
Use as much key-parts as possible and a uniq key is better |
|
5208 |
than a not unique key |
|
5209 |
Set tmp to (previous record count) * (records / combination) |
|
5210 |
*/ |
|
5211 |
if ((found_part & 1) && |
|
5212 |
(!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) || |
|
5213 |
found_part == PREV_BITS(uint,keyinfo->key_parts))) |
|
5214 |
{ |
|
5215 |
max_key_part= max_part_bit(found_part); |
|
5216 |
/* |
|
5217 |
ReuseRangeEstimateForRef-3: |
|
5218 |
We're now considering a ref[or_null] access via |
|
5219 |
(t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR |
|
5220 |
(same-as-above but with one cond replaced |
|
5221 |
with "t.keypart_i IS NULL")] (**) |
|
5222 |
||
5223 |
Try re-using E(#rows) from "range" optimizer: |
|
5224 |
We can do so if "range" optimizer used the same intervals as |
|
5225 |
in (**). The intervals used by range optimizer may be not |
|
5226 |
available at this point (as "range" access might have choosen to |
|
5227 |
create quick select over another index), so we can't compare |
|
5228 |
them to (**). We'll make indirect judgements instead. |
|
5229 |
The sufficient conditions for re-use are: |
|
55
by brian
Update for using real bool types. |
5230 |
(C1) All e_i in (**) are constants, i.e. found_ref==false. (if |
1
by brian
clean slate |
5231 |
this is not satisfied we have no way to know which ranges |
5232 |
will be actually scanned by 'ref' until we execute the |
|
5233 |
join) |
|
5234 |
(C2) max #key parts in 'range' access == K == max_key_part (this |
|
5235 |
is apparently a necessary requirement) |
|
5236 |
||
5237 |
We also have a property that "range optimizer produces equal or |
|
5238 |
tighter set of scan intervals than ref(const) optimizer". Each |
|
5239 |
of the intervals in (**) are "tightest possible" intervals when |
|
5240 |
one limits itself to using keyparts 1..K (which we do in #2). |
|
5241 |
From here it follows that range access used either one, or |
|
5242 |
both of the (I1) and (I2) intervals: |
|
5243 |
||
5244 |
(t.keypart1=c1 AND ... AND t.keypartK=eK) (I1) |
|
5245 |
(same-as-above but with one cond replaced |
|
5246 |
with "t.keypart_i IS NULL") (I2) |
|
5247 |
||
5248 |
The remaining part is to exclude the situation where range |
|
5249 |
optimizer used one interval while we're considering |
|
5250 |
ref-or-null and looking for estimate for two intervals. This |
|
5251 |
is done by last limitation: |
|
5252 |
||
5253 |
(C3) "range optimizer used (have ref_or_null?2:1) intervals" |
|
5254 |
*/ |
|
5255 |
if (table->quick_keys.is_set(key) && !found_ref && //(C1) |
|
5256 |
table->quick_key_parts[key] == max_key_part && //(C2) |
|
5257 |
table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3) |
|
5258 |
{ |
|
5259 |
tmp= records= (double) table->quick_rows[key]; |
|
5260 |
} |
|
5261 |
else |
|
5262 |
{ |
|
5263 |
/* Check if we have statistic about the distribution */ |
|
5264 |
if ((records= keyinfo->rec_per_key[max_key_part-1])) |
|
5265 |
{ |
|
5266 |
/* |
|
5267 |
Fix for the case where the index statistics is too |
|
5268 |
optimistic: If |
|
5269 |
(1) We're considering ref(const) and there is quick select |
|
5270 |
on the same index, |
|
5271 |
(2) and that quick select uses more keyparts (i.e. it will |
|
5272 |
scan equal/smaller interval then this ref(const)) |
|
5273 |
(3) and E(#rows) for quick select is higher then our |
|
5274 |
estimate, |
|
5275 |
Then |
|
5276 |
We'll use E(#rows) from quick select. |
|
5277 |
||
5278 |
Q: Why do we choose to use 'ref'? Won't quick select be |
|
5279 |
cheaper in some cases ? |
|
5280 |
TODO: figure this out and adjust the plan choice if needed. |
|
5281 |
*/ |
|
5282 |
if (!found_ref && table->quick_keys.is_set(key) && // (1) |
|
5283 |
table->quick_key_parts[key] > max_key_part && // (2) |
|
5284 |
records < (double)table->quick_rows[key]) // (3) |
|
5285 |
records= (double)table->quick_rows[key]; |
|
5286 |
||
5287 |
tmp= records; |
|
5288 |
} |
|
5289 |
else |
|
5290 |
{ |
|
5291 |
/* |
|
5292 |
Assume that the first key part matches 1% of the file |
|
5293 |
and that the whole key matches 10 (duplicates) or 1 |
|
5294 |
(unique) records. |
|
5295 |
Assume also that more key matches proportionally more |
|
5296 |
records |
|
5297 |
This gives the formula: |
|
5298 |
records = (x * (b-a) + a*c-b)/(c-1) |
|
5299 |
||
5300 |
b = records matched by whole key |
|
5301 |
a = records matched by first key part (1% of all records?) |
|
5302 |
c = number of key parts in key |
|
5303 |
x = used key parts (1 <= x <= c) |
|
5304 |
*/ |
|
5305 |
double rec_per_key; |
|
5306 |
if (!(rec_per_key=(double) |
|
5307 |
keyinfo->rec_per_key[keyinfo->key_parts-1])) |
|
5308 |
rec_per_key=(double) s->records/rec+1; |
|
5309 |
||
5310 |
if (!s->records) |
|
5311 |
tmp = 0; |
|
5312 |
else if (rec_per_key/(double) s->records >= 0.01) |
|
5313 |
tmp = rec_per_key; |
|
5314 |
else |
|
5315 |
{ |
|
5316 |
double a=s->records*0.01; |
|
5317 |
if (keyinfo->key_parts > 1) |
|
5318 |
tmp= (max_key_part * (rec_per_key - a) + |
|
5319 |
a*keyinfo->key_parts - rec_per_key)/ |
|
5320 |
(keyinfo->key_parts-1); |
|
5321 |
else |
|
5322 |
tmp= a; |
|
5323 |
set_if_bigger(tmp,1.0); |
|
5324 |
} |
|
5325 |
records = (ulong) tmp; |
|
5326 |
} |
|
5327 |
||
5328 |
if (ref_or_null_part) |
|
5329 |
{ |
|
5330 |
/* We need to do two key searches to find key */ |
|
5331 |
tmp *= 2.0; |
|
5332 |
records *= 2.0; |
|
5333 |
} |
|
5334 |
||
5335 |
/* |
|
5336 |
ReuseRangeEstimateForRef-4: We get here if we could not reuse |
|
5337 |
E(#rows) from range optimizer. Make another try: |
|
5338 |
||
5339 |
If range optimizer produced E(#rows) for a prefix of the ref |
|
5340 |
access we're considering, and that E(#rows) is lower then our |
|
5341 |
current estimate, make the adjustment. |
|
5342 |
||
5343 |
The decision whether we can re-use the estimate from the range |
|
5344 |
optimizer is the same as in ReuseRangeEstimateForRef-3, |
|
5345 |
applied to first table->quick_key_parts[key] key parts. |
|
5346 |
*/ |
|
5347 |
if (table->quick_keys.is_set(key) && |
|
5348 |
table->quick_key_parts[key] <= max_key_part && |
|
5349 |
const_part & (1 << table->quick_key_parts[key]) && |
|
5350 |
table->quick_n_ranges[key] == 1 + test(ref_or_null_part & |
|
5351 |
const_part) && |
|
5352 |
records > (double) table->quick_rows[key]) |
|
5353 |
{ |
|
5354 |
tmp= records= (double) table->quick_rows[key]; |
|
5355 |
} |
|
5356 |
} |
|
5357 |
||
5358 |
/* Limit the number of matched rows */ |
|
5359 |
set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key); |
|
5360 |
if (table->covering_keys.is_set(key)) |
|
5361 |
{ |
|
5362 |
/* we can use only index tree */ |
|
5363 |
tmp= record_count * table->file->index_only_read_time(key, tmp); |
|
5364 |
} |
|
5365 |
else |
|
5366 |
tmp= record_count * min(tmp,s->worst_seeks); |
|
5367 |
} |
|
5368 |
else |
|
5369 |
tmp= best_time; // Do nothing |
|
5370 |
} |
|
5371 |
||
5372 |
if (sj_inside_out_scan && !start_key) |
|
5373 |
{ |
|
5374 |
tmp= tmp/2; |
|
5375 |
if (records) |
|
5376 |
records= records/2; |
|
5377 |
} |
|
5378 |
||
5379 |
} |
|
5380 |
if (tmp < best_time - records/(double) TIME_FOR_COMPARE) |
|
5381 |
{ |
|
5382 |
best_time= tmp + records/(double) TIME_FOR_COMPARE; |
|
5383 |
best= tmp; |
|
5384 |
best_records= records; |
|
5385 |
best_key= start_key; |
|
5386 |
best_max_key_part= max_key_part; |
|
5387 |
best_ref_depends_map= found_ref; |
|
5388 |
best_is_sj_inside_out= sj_inside_out_scan; |
|
5389 |
} |
|
5390 |
} |
|
5391 |
records= best_records; |
|
5392 |
} |
|
5393 |
||
5394 |
/* |
|
5395 |
Don't test table scan if it can't be better. |
|
5396 |
Prefer key lookup if we would use the same key for scanning. |
|
5397 |
||
5398 |
Don't do a table scan on InnoDB tables, if we can read the used |
|
5399 |
parts of the row from any of the used index. |
|
5400 |
This is because table scans uses index and we would not win |
|
5401 |
anything by using a table scan. |
|
5402 |
||
5403 |
A word for word translation of the below if-statement in sergefp's |
|
5404 |
understanding: we check if we should use table scan if: |
|
5405 |
(1) The found 'ref' access produces more records than a table scan |
|
5406 |
(or index scan, or quick select), or 'ref' is more expensive than |
|
5407 |
any of them. |
|
5408 |
(2) This doesn't hold: the best way to perform table scan is to to perform |
|
5409 |
'range' access using index IDX, and the best way to perform 'ref' |
|
5410 |
access is to use the same index IDX, with the same or more key parts. |
|
5411 |
(note: it is not clear how this rule is/should be extended to |
|
5412 |
index_merge quick selects) |
|
5413 |
(3) See above note about InnoDB. |
|
5414 |
(4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access |
|
5415 |
path, but there is no quick select) |
|
5416 |
If the condition in the above brackets holds, then the only possible |
|
5417 |
"table scan" access method is ALL/index (there is no quick select). |
|
5418 |
Since we have a 'ref' access path, and FORCE INDEX instructs us to |
|
5419 |
choose it over ALL/index, there is no need to consider a full table |
|
5420 |
scan. |
|
5421 |
*/ |
|
5422 |
if ((records >= s->found_records || best > s->read_time) && // (1) |
|
5423 |
!(s->quick && best_key && s->quick->index == best_key->key && // (2) |
|
5424 |
best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2) |
|
5425 |
!((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) && // (3) |
|
5426 |
! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3) |
|
5427 |
!(s->table->force_index && best_key && !s->quick)) // (4) |
|
5428 |
{ // Check full join |
|
5429 |
ha_rows rnd_records= s->found_records; |
|
5430 |
/* |
|
5431 |
If there is a filtering condition on the table (i.e. ref analyzer found |
|
5432 |
at least one "table.keyXpartY= exprZ", where exprZ refers only to tables |
|
5433 |
preceding this table in the join order we're now considering), then |
|
5434 |
assume that 25% of the rows will be filtered out by this condition. |
|
5435 |
||
5436 |
This heuristic is supposed to force tables used in exprZ to be before |
|
5437 |
this table in join order. |
|
5438 |
*/ |
|
5439 |
if (found_constraint) |
|
5440 |
rnd_records-= rnd_records/4; |
|
5441 |
||
5442 |
/* |
|
5443 |
If applicable, get a more accurate estimate. Don't use the two |
|
5444 |
heuristics at once. |
|
5445 |
*/ |
|
5446 |
if (s->table->quick_condition_rows != s->found_records) |
|
5447 |
rnd_records= s->table->quick_condition_rows; |
|
5448 |
||
5449 |
/* |
|
5450 |
Range optimizer never proposes a RANGE if it isn't better |
|
5451 |
than FULL: so if RANGE is present, it's always preferred to FULL. |
|
5452 |
Here we estimate its cost. |
|
5453 |
*/ |
|
5454 |
if (s->quick) |
|
5455 |
{ |
|
5456 |
/* |
|
5457 |
For each record we: |
|
5458 |
- read record range through 'quick' |
|
5459 |
- skip rows which does not satisfy WHERE constraints |
|
5460 |
TODO: |
|
5461 |
We take into account possible use of join cache for ALL/index |
|
5462 |
access (see first else-branch below), but we don't take it into |
|
5463 |
account here for range/index_merge access. Find out why this is so. |
|
5464 |
*/ |
|
5465 |
tmp= record_count * |
|
5466 |
(s->quick->read_time + |
|
5467 |
(s->found_records - rnd_records)/(double) TIME_FOR_COMPARE); |
|
5468 |
} |
|
5469 |
else |
|
5470 |
{ |
|
5471 |
/* Estimate cost of reading table. */ |
|
5472 |
tmp= s->table->file->scan_time(); |
|
5473 |
if (s->table->map & join->outer_join) // Can't use join cache |
|
5474 |
{ |
|
5475 |
/* |
|
5476 |
For each record we have to: |
|
5477 |
- read the whole table record |
|
5478 |
- skip rows which does not satisfy join condition |
|
5479 |
*/ |
|
5480 |
tmp= record_count * |
|
5481 |
(tmp + |
|
5482 |
(s->records - rnd_records)/(double) TIME_FOR_COMPARE); |
|
5483 |
} |
|
5484 |
else |
|
5485 |
{ |
|
5486 |
/* We read the table as many times as join buffer becomes full. */ |
|
5487 |
tmp*= (1.0 + floor((double) cache_record_length(join,idx) * |
|
5488 |
record_count / |
|
5489 |
(double) thd->variables.join_buff_size)); |
|
5490 |
/* |
|
5491 |
We don't make full cartesian product between rows in the scanned |
|
5492 |
table and existing records because we skip all rows from the |
|
5493 |
scanned table, which does not satisfy join condition when |
|
5494 |
we read the table (see flush_cached_records for details). Here we |
|
5495 |
take into account cost to read and skip these records. |
|
5496 |
*/ |
|
5497 |
tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE; |
|
5498 |
} |
|
5499 |
} |
|
5500 |
||
5501 |
/* |
|
5502 |
We estimate the cost of evaluating WHERE clause for found records |
|
5503 |
as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus |
|
5504 |
tmp give us total cost of using TABLE SCAN |
|
5505 |
*/ |
|
5506 |
if (best == DBL_MAX || |
|
5507 |
(tmp + record_count/(double) TIME_FOR_COMPARE*rnd_records < |
|
5508 |
best + record_count/(double) TIME_FOR_COMPARE*records)) |
|
5509 |
{ |
|
5510 |
/* |
|
5511 |
If the table has a range (s->quick is set) make_join_select() |
|
5512 |
will ensure that this will be used |
|
5513 |
*/ |
|
5514 |
best= tmp; |
|
5515 |
records= rows2double(rnd_records); |
|
5516 |
best_key= 0; |
|
5517 |
/* range/index_merge/ALL/index access method are "independent", so: */ |
|
5518 |
best_ref_depends_map= 0; |
|
55
by brian
Update for using real bool types. |
5519 |
best_is_sj_inside_out= false; |
1
by brian
clean slate |
5520 |
} |
5521 |
} |
|
5522 |
||
5523 |
/* Update the cost information for the current partial plan */ |
|
5524 |
join->positions[idx].records_read= records; |
|
5525 |
join->positions[idx].read_time= best; |
|
5526 |
join->positions[idx].key= best_key; |
|
5527 |
join->positions[idx].table= s; |
|
5528 |
join->positions[idx].ref_depend_map= best_ref_depends_map; |
|
5529 |
join->positions[idx].use_insideout_scan= best_is_sj_inside_out; |
|
5530 |
||
5531 |
if (!best_key && |
|
5532 |
idx == join->const_tables && |
|
5533 |
s->table == join->sort_by_table && |
|
5534 |
join->unit->select_limit_cnt >= records) |
|
5535 |
join->sort_by_table= (TABLE*) 1; // Must use temporary table |
|
5536 |
||
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
5537 |
return; |
1
by brian
clean slate |
5538 |
} |
5539 |
||
5540 |
||
5541 |
/** |
|
5542 |
Selects and invokes a search strategy for an optimal query plan. |
|
5543 |
||
5544 |
The function checks user-configurable parameters that control the search |
|
5545 |
strategy for an optimal plan, selects the search method and then invokes |
|
5546 |
it. Each specific optimization procedure stores the final optimal plan in |
|
5547 |
the array 'join->best_positions', and the cost of the plan in |
|
5548 |
'join->best_read'. |
|
5549 |
||
5550 |
@param join pointer to the structure providing all context info for |
|
5551 |
the query |
|
5552 |
@param join_tables set of the tables in the query |
|
5553 |
||
5554 |
@todo |
|
5555 |
'MAX_TABLES+2' denotes the old implementation of find_best before |
|
5556 |
the greedy version. Will be removed when greedy_search is approved. |
|
5557 |
||
5558 |
@retval |
|
55
by brian
Update for using real bool types. |
5559 |
false ok |
1
by brian
clean slate |
5560 |
@retval |
55
by brian
Update for using real bool types. |
5561 |
true Fatal error |
1
by brian
clean slate |
5562 |
*/ |
5563 |
||
5564 |
static bool |
|
5565 |
choose_plan(JOIN *join, table_map join_tables) |
|
5566 |
{ |
|
5567 |
uint search_depth= join->thd->variables.optimizer_search_depth; |
|
5568 |
uint prune_level= join->thd->variables.optimizer_prune_level; |
|
5569 |
bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN); |
|
5570 |
||
5571 |
join->cur_embedding_map= 0; |
|
5572 |
reset_nj_counters(join->join_list); |
|
5573 |
/* |
|
5574 |
if (SELECT_STRAIGHT_JOIN option is set) |
|
5575 |
reorder tables so dependent tables come after tables they depend |
|
5576 |
on, otherwise keep tables in the order they were specified in the query |
|
5577 |
else |
|
5578 |
Apply heuristic: pre-sort all access plans with respect to the number of |
|
5579 |
records accessed. |
|
5580 |
*/ |
|
5581 |
my_qsort(join->best_ref + join->const_tables, |
|
5582 |
join->tables - join->const_tables, sizeof(JOIN_TAB*), |
|
5583 |
straight_join ? join_tab_cmp_straight : join_tab_cmp); |
|
5584 |
join->cur_emb_sj_nests= 0; |
|
5585 |
if (straight_join) |
|
5586 |
{ |
|
5587 |
optimize_straight_join(join, join_tables); |
|
5588 |
} |
|
5589 |
else |
|
5590 |
{ |
|
5591 |
if (search_depth == MAX_TABLES+2) |
|
5592 |
{ /* |
|
5593 |
TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before |
|
5594 |
the greedy version. Will be removed when greedy_search is approved. |
|
5595 |
*/ |
|
5596 |
join->best_read= DBL_MAX; |
|
5597 |
if (find_best(join, join_tables, join->const_tables, 1.0, 0.0)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
5598 |
return(true); |
1
by brian
clean slate |
5599 |
} |
5600 |
else |
|
5601 |
{ |
|
5602 |
if (search_depth == 0) |
|
5603 |
/* Automatically determine a reasonable value for 'search_depth' */ |
|
5604 |
search_depth= determine_search_depth(join); |
|
5605 |
if (greedy_search(join, join_tables, search_depth, prune_level)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
5606 |
return(true); |
1
by brian
clean slate |
5607 |
} |
5608 |
} |
|
5609 |
||
5610 |
/* |
|
5611 |
Store the cost of this query into a user variable |
|
5612 |
Don't update last_query_cost for statements that are not "flat joins" : |
|
5613 |
i.e. they have subqueries, unions or call stored procedures. |
|
5614 |
TODO: calculate a correct cost for a query with subqueries and UNIONs. |
|
5615 |
*/ |
|
5616 |
if (join->thd->lex->is_single_level_stmt()) |
|
5617 |
join->thd->status_var.last_query_cost= join->best_read; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
5618 |
return(false); |
1
by brian
clean slate |
5619 |
} |
5620 |
||
5621 |
||
5622 |
/** |
|
5623 |
Compare two JOIN_TAB objects based on the number of accessed records. |
|
5624 |
||
5625 |
@param ptr1 pointer to first JOIN_TAB object |
|
5626 |
@param ptr2 pointer to second JOIN_TAB object |
|
5627 |
||
5628 |
NOTES |
|
5629 |
The order relation implemented by join_tab_cmp() is not transitive, |
|
5630 |
i.e. it is possible to choose such a, b and c that (a < b) && (b < c) |
|
5631 |
but (c < a). This implies that result of a sort using the relation |
|
5632 |
implemented by join_tab_cmp() depends on the order in which |
|
5633 |
elements are compared, i.e. the result is implementation-specific. |
|
5634 |
Example: |
|
5635 |
a: dependent = 0x0 table->map = 0x1 found_records = 3 ptr = 0x907e6b0 |
|
5636 |
b: dependent = 0x0 table->map = 0x2 found_records = 3 ptr = 0x907e838 |
|
5637 |
c: dependent = 0x6 table->map = 0x10 found_records = 2 ptr = 0x907ecd0 |
|
5638 |
||
5639 |
@retval |
|
5640 |
1 if first is bigger |
|
5641 |
@retval |
|
5642 |
-1 if second is bigger |
|
5643 |
@retval |
|
5644 |
0 if equal |
|
5645 |
*/ |
|
5646 |
||
5647 |
static int |
|
5648 |
join_tab_cmp(const void* ptr1, const void* ptr2) |
|
5649 |
{ |
|
5650 |
JOIN_TAB *jt1= *(JOIN_TAB**) ptr1; |
|
5651 |
JOIN_TAB *jt2= *(JOIN_TAB**) ptr2; |
|
5652 |
||
5653 |
if (jt1->dependent & jt2->table->map) |
|
5654 |
return 1; |
|
5655 |
if (jt2->dependent & jt1->table->map) |
|
5656 |
return -1; |
|
5657 |
if (jt1->found_records > jt2->found_records) |
|
5658 |
return 1; |
|
5659 |
if (jt1->found_records < jt2->found_records) |
|
5660 |
return -1; |
|
5661 |
return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0); |
|
5662 |
} |
|
5663 |
||
5664 |
||
5665 |
/** |
|
5666 |
Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN. |
|
5667 |
*/ |
|
5668 |
||
5669 |
static int |
|
5670 |
join_tab_cmp_straight(const void* ptr1, const void* ptr2) |
|
5671 |
{ |
|
5672 |
JOIN_TAB *jt1= *(JOIN_TAB**) ptr1; |
|
5673 |
JOIN_TAB *jt2= *(JOIN_TAB**) ptr2; |
|
5674 |
||
5675 |
if (jt1->dependent & jt2->table->map) |
|
5676 |
return 1; |
|
5677 |
if (jt2->dependent & jt1->table->map) |
|
5678 |
return -1; |
|
5679 |
return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0); |
|
5680 |
} |
|
5681 |
||
5682 |
/** |
|
5683 |
Heuristic procedure to automatically guess a reasonable degree of |
|
5684 |
exhaustiveness for the greedy search procedure. |
|
5685 |
||
5686 |
The procedure estimates the optimization time and selects a search depth |
|
5687 |
big enough to result in a near-optimal QEP, that doesn't take too long to |
|
5688 |
find. If the number of tables in the query exceeds some constant, then |
|
5689 |
search_depth is set to this constant. |
|
5690 |
||
5691 |
@param join pointer to the structure providing all context info for |
|
5692 |
the query |
|
5693 |
||
5694 |
@note |
|
5695 |
This is an extremely simplistic implementation that serves as a stub for a |
|
5696 |
more advanced analysis of the join. Ideally the search depth should be |
|
5697 |
determined by learning from previous query optimizations, because it will |
|
5698 |
depend on the CPU power (and other factors). |
|
5699 |
||
5700 |
@todo |
|
5701 |
this value should be determined dynamically, based on statistics: |
|
5702 |
uint max_tables_for_exhaustive_opt= 7; |
|
5703 |
||
5704 |
@todo |
|
5705 |
this value could be determined by some mapping of the form: |
|
5706 |
depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE] |
|
5707 |
||
5708 |
@return |
|
5709 |
A positive integer that specifies the search depth (and thus the |
|
5710 |
exhaustiveness) of the depth-first search algorithm used by |
|
5711 |
'greedy_search'. |
|
5712 |
*/ |
|
5713 |
||
5714 |
static uint |
|
5715 |
determine_search_depth(JOIN *join) |
|
5716 |
{ |
|
5717 |
uint table_count= join->tables - join->const_tables; |
|
5718 |
uint search_depth; |
|
5719 |
/* TODO: this value should be determined dynamically, based on statistics: */ |
|
5720 |
uint max_tables_for_exhaustive_opt= 7; |
|
5721 |
||
5722 |
if (table_count <= max_tables_for_exhaustive_opt) |
|
5723 |
search_depth= table_count+1; // use exhaustive for small number of tables |
|
5724 |
else |
|
5725 |
/* |
|
5726 |
TODO: this value could be determined by some mapping of the form: |
|
5727 |
depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE] |
|
5728 |
*/ |
|
5729 |
search_depth= max_tables_for_exhaustive_opt; // use greedy search |
|
5730 |
||
5731 |
return search_depth; |
|
5732 |
} |
|
5733 |
||
5734 |
||
5735 |
/** |
|
5736 |
Select the best ways to access the tables in a query without reordering them. |
|
5737 |
||
5738 |
Find the best access paths for each query table and compute their costs |
|
5739 |
according to their order in the array 'join->best_ref' (thus without |
|
5740 |
reordering the join tables). The function calls sequentially |
|
5741 |
'best_access_path' for each table in the query to select the best table |
|
5742 |
access method. The final optimal plan is stored in the array |
|
5743 |
'join->best_positions', and the corresponding cost in 'join->best_read'. |
|
5744 |
||
5745 |
@param join pointer to the structure providing all context info for |
|
5746 |
the query |
|
5747 |
@param join_tables set of the tables in the query |
|
5748 |
||
5749 |
@note |
|
5750 |
This function can be applied to: |
|
5751 |
- queries with STRAIGHT_JOIN |
|
5752 |
- internally to compute the cost of an arbitrary QEP |
|
5753 |
@par |
|
5754 |
Thus 'optimize_straight_join' can be used at any stage of the query |
|
5755 |
optimization process to finalize a QEP as it is. |
|
5756 |
*/ |
|
5757 |
||
5758 |
static void |
|
5759 |
optimize_straight_join(JOIN *join, table_map join_tables) |
|
5760 |
{ |
|
5761 |
JOIN_TAB *s; |
|
5762 |
uint idx= join->const_tables; |
|
5763 |
double record_count= 1.0; |
|
5764 |
double read_time= 0.0; |
|
5765 |
||
5766 |
for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++) |
|
5767 |
{ |
|
5768 |
/* Find the best access method from 's' to the current partial plan */ |
|
5769 |
advance_sj_state(join_tables, s); |
|
5770 |
best_access_path(join, s, join->thd, join_tables, idx, |
|
5771 |
record_count, read_time); |
|
5772 |
/* compute the cost of the new plan extended with 's' */ |
|
5773 |
record_count*= join->positions[idx].records_read; |
|
5774 |
read_time+= join->positions[idx].read_time; |
|
5775 |
join_tables&= ~(s->table->map); |
|
5776 |
++idx; |
|
5777 |
} |
|
5778 |
||
5779 |
read_time+= record_count / (double) TIME_FOR_COMPARE; |
|
5780 |
if (join->sort_by_table && |
|
5781 |
join->sort_by_table != join->positions[join->const_tables].table->table) |
|
5782 |
read_time+= record_count; // We have to make a temp table |
|
5783 |
memcpy((uchar*) join->best_positions, (uchar*) join->positions, |
|
5784 |
sizeof(POSITION)*idx); |
|
5785 |
join->best_read= read_time; |
|
5786 |
} |
|
5787 |
||
5788 |
||
5789 |
/** |
|
5790 |
Find a good, possibly optimal, query execution plan (QEP) by a greedy search. |
|
5791 |
||
5792 |
The search procedure uses a hybrid greedy/exhaustive search with controlled |
|
5793 |
exhaustiveness. The search is performed in N = card(remaining_tables) |
|
5794 |
steps. Each step evaluates how promising is each of the unoptimized tables, |
|
5795 |
selects the most promising table, and extends the current partial QEP with |
|
5796 |
that table. Currenly the most 'promising' table is the one with least |
|
5797 |
expensive extension.\ |
|
5798 |
||
5799 |
There are two extreme cases: |
|
5800 |
-# When (card(remaining_tables) < search_depth), the estimate finds the |
|
5801 |
best complete continuation of the partial QEP. This continuation can be |
|
5802 |
used directly as a result of the search. |
|
5803 |
-# When (search_depth == 1) the 'best_extension_by_limited_search' |
|
5804 |
consideres the extension of the current QEP with each of the remaining |
|
5805 |
unoptimized tables. |
|
5806 |
||
5807 |
All other cases are in-between these two extremes. Thus the parameter |
|
5808 |
'search_depth' controlls the exhaustiveness of the search. The higher the |
|
5809 |
value, the longer the optimizaton time and possibly the better the |
|
5810 |
resulting plan. The lower the value, the fewer alternative plans are |
|
5811 |
estimated, but the more likely to get a bad QEP. |
|
5812 |
||
5813 |
All intermediate and final results of the procedure are stored in 'join': |
|
5814 |
- join->positions : modified for every partial QEP that is explored |
|
5815 |
- join->best_positions: modified for the current best complete QEP |
|
5816 |
- join->best_read : modified for the current best complete QEP |
|
5817 |
- join->best_ref : might be partially reordered |
|
5818 |
||
5819 |
The final optimal plan is stored in 'join->best_positions', and its |
|
5820 |
corresponding cost in 'join->best_read'. |
|
5821 |
||
5822 |
@note |
|
5823 |
The following pseudocode describes the algorithm of 'greedy_search': |
|
5824 |
||
5825 |
@code |
|
5826 |
procedure greedy_search |
|
5827 |
input: remaining_tables |
|
5828 |
output: pplan; |
|
5829 |
{ |
|
5830 |
pplan = <>; |
|
5831 |
do { |
|
5832 |
(t, a) = best_extension(pplan, remaining_tables); |
|
5833 |
pplan = concat(pplan, (t, a)); |
|
5834 |
remaining_tables = remaining_tables - t; |
|
5835 |
} while (remaining_tables != {}) |
|
5836 |
return pplan; |
|
5837 |
} |
|
5838 |
||
5839 |
@endcode |
|
5840 |
where 'best_extension' is a placeholder for a procedure that selects the |
|
5841 |
most "promising" of all tables in 'remaining_tables'. |
|
5842 |
Currently this estimate is performed by calling |
|
5843 |
'best_extension_by_limited_search' to evaluate all extensions of the |
|
5844 |
current QEP of size 'search_depth', thus the complexity of 'greedy_search' |
|
5845 |
mainly depends on that of 'best_extension_by_limited_search'. |
|
5846 |
||
5847 |
@par |
|
5848 |
If 'best_extension()' == 'best_extension_by_limited_search()', then the |
|
5849 |
worst-case complexity of this algorithm is <= |
|
5850 |
O(N*N^search_depth/search_depth). When serch_depth >= N, then the |
|
5851 |
complexity of greedy_search is O(N!). |
|
5852 |
||
5853 |
@par |
|
5854 |
In the future, 'greedy_search' might be extended to support other |
|
5855 |
implementations of 'best_extension', e.g. some simpler quadratic procedure. |
|
5856 |
||
5857 |
@param join pointer to the structure providing all context info |
|
5858 |
for the query |
|
5859 |
@param remaining_tables set of tables not included into the partial plan yet |
|
5860 |
@param search_depth controlls the exhaustiveness of the search |
|
5861 |
@param prune_level the pruning heuristics that should be applied during |
|
5862 |
search |
|
5863 |
||
5864 |
@retval |
|
55
by brian
Update for using real bool types. |
5865 |
false ok |
1
by brian
clean slate |
5866 |
@retval |
55
by brian
Update for using real bool types. |
5867 |
true Fatal error |
1
by brian
clean slate |
5868 |
*/ |
5869 |
||
5870 |
static bool |
|
5871 |
greedy_search(JOIN *join, |
|
5872 |
table_map remaining_tables, |
|
5873 |
uint search_depth, |
|
5874 |
uint prune_level) |
|
5875 |
{ |
|
5876 |
double record_count= 1.0; |
|
5877 |
double read_time= 0.0; |
|
5878 |
uint idx= join->const_tables; // index into 'join->best_ref' |
|
5879 |
uint best_idx; |
|
5880 |
uint size_remain; // cardinality of remaining_tables |
|
5881 |
POSITION best_pos; |
|
5882 |
JOIN_TAB *best_table; // the next plan node to be added to the curr QEP |
|
5883 |
||
5884 |
/* number of tables that remain to be optimized */ |
|
5885 |
size_remain= my_count_bits(remaining_tables); |
|
5886 |
||
5887 |
do { |
|
5888 |
/* Find the extension of the current QEP with the lowest cost */ |
|
5889 |
join->best_read= DBL_MAX; |
|
5890 |
if (best_extension_by_limited_search(join, remaining_tables, idx, record_count, |
|
5891 |
read_time, search_depth, prune_level)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
5892 |
return(true); |
1
by brian
clean slate |
5893 |
|
5894 |
if (size_remain <= search_depth) |
|
5895 |
{ |
|
5896 |
/* |
|
5897 |
'join->best_positions' contains a complete optimal extension of the |
|
5898 |
current partial QEP. |
|
5899 |
*/ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
5900 |
return(false); |
1
by brian
clean slate |
5901 |
} |
5902 |
||
5903 |
/* select the first table in the optimal extension as most promising */ |
|
5904 |
best_pos= join->best_positions[idx]; |
|
5905 |
best_table= best_pos.table; |
|
5906 |
/* |
|
5907 |
Each subsequent loop of 'best_extension_by_limited_search' uses |
|
5908 |
'join->positions' for cost estimates, therefore we have to update its |
|
5909 |
value. |
|
5910 |
*/ |
|
5911 |
join->positions[idx]= best_pos; |
|
5912 |
||
5913 |
/* find the position of 'best_table' in 'join->best_ref' */ |
|
5914 |
best_idx= idx; |
|
5915 |
JOIN_TAB *pos= join->best_ref[best_idx]; |
|
5916 |
while (pos && best_table != pos) |
|
5917 |
pos= join->best_ref[++best_idx]; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
5918 |
assert((pos != NULL)); // should always find 'best_table' |
1
by brian
clean slate |
5919 |
/* move 'best_table' at the first free position in the array of joins */ |
5920 |
swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]); |
|
5921 |
||
5922 |
/* compute the cost of the new plan extended with 'best_table' */ |
|
5923 |
record_count*= join->positions[idx].records_read; |
|
5924 |
read_time+= join->positions[idx].read_time; |
|
5925 |
||
5926 |
remaining_tables&= ~(best_table->table->map); |
|
5927 |
--size_remain; |
|
5928 |
++idx; |
|
55
by brian
Update for using real bool types. |
5929 |
} while (true); |
1
by brian
clean slate |
5930 |
} |
5931 |
||
5932 |
||
5933 |
/** |
|
5934 |
Find a good, possibly optimal, query execution plan (QEP) by a possibly |
|
5935 |
exhaustive search. |
|
5936 |
||
5937 |
The procedure searches for the optimal ordering of the query tables in set |
|
5938 |
'remaining_tables' of size N, and the corresponding optimal access paths to |
|
5939 |
each table. The choice of a table order and an access path for each table |
|
5940 |
constitutes a query execution plan (QEP) that fully specifies how to |
|
5941 |
execute the query. |
|
5942 |
||
5943 |
The maximal size of the found plan is controlled by the parameter |
|
5944 |
'search_depth'. When search_depth == N, the resulting plan is complete and |
|
5945 |
can be used directly as a QEP. If search_depth < N, the found plan consists |
|
5946 |
of only some of the query tables. Such "partial" optimal plans are useful |
|
5947 |
only as input to query optimization procedures, and cannot be used directly |
|
5948 |
to execute a query. |
|
5949 |
||
5950 |
The algorithm begins with an empty partial plan stored in 'join->positions' |
|
5951 |
and a set of N tables - 'remaining_tables'. Each step of the algorithm |
|
5952 |
evaluates the cost of the partial plan extended by all access plans for |
|
5953 |
each of the relations in 'remaining_tables', expands the current partial |
|
5954 |
plan with the access plan that results in lowest cost of the expanded |
|
5955 |
partial plan, and removes the corresponding relation from |
|
5956 |
'remaining_tables'. The algorithm continues until it either constructs a |
|
5957 |
complete optimal plan, or constructs an optimal plartial plan with size = |
|
5958 |
search_depth. |
|
5959 |
||
5960 |
The final optimal plan is stored in 'join->best_positions'. The |
|
5961 |
corresponding cost of the optimal plan is in 'join->best_read'. |
|
5962 |
||
5963 |
@note |
|
5964 |
The procedure uses a recursive depth-first search where the depth of the |
|
5965 |
recursion (and thus the exhaustiveness of the search) is controlled by the |
|
5966 |
parameter 'search_depth'. |
|
5967 |
||
5968 |
@note |
|
5969 |
The pseudocode below describes the algorithm of |
|
5970 |
'best_extension_by_limited_search'. The worst-case complexity of this |
|
5971 |
algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then |
|
5972 |
the complexity of greedy_search is O(N!). |
|
5973 |
||
5974 |
@code |
|
5975 |
procedure best_extension_by_limited_search( |
|
5976 |
pplan in, // in, partial plan of tables-joined-so-far |
|
5977 |
pplan_cost, // in, cost of pplan |
|
5978 |
remaining_tables, // in, set of tables not referenced in pplan |
|
5979 |
best_plan_so_far, // in/out, best plan found so far |
|
5980 |
best_plan_so_far_cost,// in/out, cost of best_plan_so_far |
|
5981 |
search_depth) // in, maximum size of the plans being considered |
|
5982 |
{ |
|
5983 |
for each table T from remaining_tables |
|
5984 |
{ |
|
5985 |
// Calculate the cost of using table T as above |
|
5986 |
cost = complex-series-of-calculations; |
|
5987 |
||
5988 |
// Add the cost to the cost so far. |
|
5989 |
pplan_cost+= cost; |
|
5990 |
||
5991 |
if (pplan_cost >= best_plan_so_far_cost) |
|
5992 |
// pplan_cost already too great, stop search |
|
5993 |
continue; |
|
5994 |
||
5995 |
pplan= expand pplan by best_access_method; |
|
5996 |
remaining_tables= remaining_tables - table T; |
|
5997 |
if (remaining_tables is not an empty set |
|
5998 |
and |
|
5999 |
search_depth > 1) |
|
6000 |
{ |
|
6001 |
best_extension_by_limited_search(pplan, pplan_cost, |
|
6002 |
remaining_tables, |
|
6003 |
best_plan_so_far, |
|
6004 |
best_plan_so_far_cost, |
|
6005 |
search_depth - 1); |
|
6006 |
} |
|
6007 |
else |
|
6008 |
{ |
|
6009 |
best_plan_so_far_cost= pplan_cost; |
|
6010 |
best_plan_so_far= pplan; |
|
6011 |
} |
|
6012 |
} |
|
6013 |
} |
|
6014 |
@endcode |
|
6015 |
||
6016 |
@note |
|
6017 |
When 'best_extension_by_limited_search' is called for the first time, |
|
6018 |
'join->best_read' must be set to the largest possible value (e.g. DBL_MAX). |
|
6019 |
The actual implementation provides a way to optionally use pruning |
|
6020 |
heuristic (controlled by the parameter 'prune_level') to reduce the search |
|
6021 |
space by skipping some partial plans. |
|
6022 |
||
6023 |
@note |
|
6024 |
The parameter 'search_depth' provides control over the recursion |
|
6025 |
depth, and thus the size of the resulting optimal plan. |
|
6026 |
||
6027 |
@param join pointer to the structure providing all context info |
|
6028 |
for the query |
|
6029 |
@param remaining_tables set of tables not included into the partial plan yet |
|
6030 |
@param idx length of the partial QEP in 'join->positions'; |
|
6031 |
since a depth-first search is used, also corresponds |
|
6032 |
to the current depth of the search tree; |
|
6033 |
also an index in the array 'join->best_ref'; |
|
6034 |
@param record_count estimate for the number of records returned by the |
|
6035 |
best partial plan |
|
6036 |
@param read_time the cost of the best partial plan |
|
6037 |
@param search_depth maximum depth of the recursion and thus size of the |
|
6038 |
found optimal plan |
|
6039 |
(0 < search_depth <= join->tables+1). |
|
6040 |
@param prune_level pruning heuristics that should be applied during |
|
6041 |
optimization |
|
6042 |
(values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS) |
|
6043 |
||
6044 |
@retval |
|
55
by brian
Update for using real bool types. |
6045 |
false ok |
1
by brian
clean slate |
6046 |
@retval |
55
by brian
Update for using real bool types. |
6047 |
true Fatal error |
1
by brian
clean slate |
6048 |
*/ |
6049 |
||
6050 |
static bool |
|
6051 |
best_extension_by_limited_search(JOIN *join, |
|
6052 |
table_map remaining_tables, |
|
6053 |
uint idx, |
|
6054 |
double record_count, |
|
6055 |
double read_time, |
|
6056 |
uint search_depth, |
|
6057 |
uint prune_level) |
|
6058 |
{ |
|
6059 |
THD *thd= join->thd; |
|
6060 |
if (thd->killed) // Abort |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6061 |
return(true); |
1
by brian
clean slate |
6062 |
|
6063 |
/* |
|
6064 |
'join' is a partial plan with lower cost than the best plan so far, |
|
6065 |
so continue expanding it further with the tables in 'remaining_tables'. |
|
6066 |
*/ |
|
6067 |
JOIN_TAB *s; |
|
6068 |
double best_record_count= DBL_MAX; |
|
6069 |
double best_read_time= DBL_MAX; |
|
6070 |
||
6071 |
for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++) |
|
6072 |
{ |
|
6073 |
table_map real_table_bit= s->table->map; |
|
6074 |
if ((remaining_tables & real_table_bit) && |
|
6075 |
!(remaining_tables & s->dependent) && |
|
6076 |
(!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s))) |
|
6077 |
{ |
|
6078 |
double current_record_count, current_read_time; |
|
6079 |
advance_sj_state(remaining_tables, s); |
|
6080 |
||
6081 |
/* |
|
6082 |
psergey-insideout-todo: |
|
6083 |
when best_access_path() detects it could do an InsideOut scan or |
|
6084 |
some other scan, have it return an insideout scan and a flag that |
|
6085 |
requests to "fork" this loop iteration. (Q: how does that behave |
|
6086 |
when the depth is insufficient??) |
|
6087 |
*/ |
|
6088 |
/* Find the best access method from 's' to the current partial plan */ |
|
6089 |
best_access_path(join, s, thd, remaining_tables, idx, |
|
6090 |
record_count, read_time); |
|
6091 |
/* Compute the cost of extending the plan with 's' */ |
|
6092 |
current_record_count= record_count * join->positions[idx].records_read; |
|
6093 |
current_read_time= read_time + join->positions[idx].read_time; |
|
6094 |
||
6095 |
/* Expand only partial plans with lower cost than the best QEP so far */ |
|
6096 |
if ((current_read_time + |
|
6097 |
current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read) |
|
6098 |
{ |
|
6099 |
restore_prev_nj_state(s); |
|
6100 |
restore_prev_sj_state(remaining_tables, s); |
|
6101 |
continue; |
|
6102 |
} |
|
6103 |
||
6104 |
/* |
|
6105 |
Prune some less promising partial plans. This heuristic may miss |
|
6106 |
the optimal QEPs, thus it results in a non-exhaustive search. |
|
6107 |
*/ |
|
6108 |
if (prune_level == 1) |
|
6109 |
{ |
|
6110 |
if (best_record_count > current_record_count || |
|
6111 |
best_read_time > current_read_time || |
|
6112 |
(idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP |
|
6113 |
{ |
|
6114 |
if (best_record_count >= current_record_count && |
|
6115 |
best_read_time >= current_read_time && |
|
6116 |
/* TODO: What is the reasoning behind this condition? */ |
|
6117 |
(!(s->key_dependent & remaining_tables) || |
|
6118 |
join->positions[idx].records_read < 2.0)) |
|
6119 |
{ |
|
6120 |
best_record_count= current_record_count; |
|
6121 |
best_read_time= current_read_time; |
|
6122 |
} |
|
6123 |
} |
|
6124 |
else |
|
6125 |
{ |
|
6126 |
restore_prev_nj_state(s); |
|
6127 |
restore_prev_sj_state(remaining_tables, s); |
|
6128 |
continue; |
|
6129 |
} |
|
6130 |
} |
|
6131 |
||
6132 |
if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) ) |
|
6133 |
{ /* Recursively expand the current partial plan */ |
|
6134 |
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos); |
|
6135 |
if (best_extension_by_limited_search(join, |
|
6136 |
remaining_tables & ~real_table_bit, |
|
6137 |
idx + 1, |
|
6138 |
current_record_count, |
|
6139 |
current_read_time, |
|
6140 |
search_depth - 1, |
|
6141 |
prune_level)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6142 |
return(true); |
1
by brian
clean slate |
6143 |
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos); |
6144 |
} |
|
6145 |
else |
|
6146 |
{ /* |
|
6147 |
'join' is either the best partial QEP with 'search_depth' relations, |
|
6148 |
or the best complete QEP so far, whichever is smaller. |
|
6149 |
*/ |
|
6150 |
current_read_time+= current_record_count / (double) TIME_FOR_COMPARE; |
|
6151 |
if (join->sort_by_table && |
|
6152 |
join->sort_by_table != |
|
6153 |
join->positions[join->const_tables].table->table) |
|
6154 |
/* We have to make a temp table */ |
|
6155 |
current_read_time+= current_record_count; |
|
6156 |
if ((search_depth == 1) || (current_read_time < join->best_read)) |
|
6157 |
{ |
|
6158 |
memcpy((uchar*) join->best_positions, (uchar*) join->positions, |
|
6159 |
sizeof(POSITION) * (idx + 1)); |
|
6160 |
join->best_read= current_read_time - 0.001; |
|
6161 |
} |
|
6162 |
} |
|
6163 |
restore_prev_nj_state(s); |
|
6164 |
restore_prev_sj_state(remaining_tables, s); |
|
6165 |
} |
|
6166 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6167 |
return(false); |
1
by brian
clean slate |
6168 |
} |
6169 |
||
6170 |
||
6171 |
/** |
|
6172 |
@todo |
|
6173 |
- TODO: this function is here only temporarily until 'greedy_search' is |
|
6174 |
tested and accepted. |
|
6175 |
||
6176 |
RETURN VALUES |
|
55
by brian
Update for using real bool types. |
6177 |
false ok |
6178 |
true Fatal error |
|
1
by brian
clean slate |
6179 |
*/ |
6180 |
static bool |
|
6181 |
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count, |
|
6182 |
double read_time) |
|
6183 |
{ |
|
6184 |
THD *thd= join->thd; |
|
6185 |
if (thd->killed) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6186 |
return(true); |
1
by brian
clean slate |
6187 |
if (!rest_tables) |
6188 |
{ |
|
6189 |
read_time+=record_count/(double) TIME_FOR_COMPARE; |
|
6190 |
if (join->sort_by_table && |
|
6191 |
join->sort_by_table != |
|
6192 |
join->positions[join->const_tables].table->table) |
|
6193 |
read_time+=record_count; // We have to make a temp table |
|
6194 |
if (read_time < join->best_read) |
|
6195 |
{ |
|
6196 |
memcpy((uchar*) join->best_positions,(uchar*) join->positions, |
|
6197 |
sizeof(POSITION)*idx); |
|
6198 |
join->best_read= read_time - 0.001; |
|
6199 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6200 |
return(false); |
1
by brian
clean slate |
6201 |
} |
6202 |
if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6203 |
return(false); /* Found better before */ |
1
by brian
clean slate |
6204 |
|
6205 |
JOIN_TAB *s; |
|
6206 |
double best_record_count=DBL_MAX,best_read_time=DBL_MAX; |
|
6207 |
for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++) |
|
6208 |
{ |
|
6209 |
table_map real_table_bit=s->table->map; |
|
6210 |
if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) && |
|
6211 |
(!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s))) |
|
6212 |
{ |
|
6213 |
double records, best; |
|
6214 |
advance_sj_state(rest_tables, s); |
|
6215 |
best_access_path(join, s, thd, rest_tables, idx, record_count, |
|
6216 |
read_time); |
|
6217 |
records= join->positions[idx].records_read; |
|
6218 |
best= join->positions[idx].read_time; |
|
6219 |
/* |
|
6220 |
Go to the next level only if there hasn't been a better key on |
|
6221 |
this level! This will cut down the search for a lot simple cases! |
|
6222 |
*/ |
|
6223 |
double current_record_count=record_count*records; |
|
6224 |
double current_read_time=read_time+best; |
|
6225 |
if (best_record_count > current_record_count || |
|
6226 |
best_read_time > current_read_time || |
|
6227 |
(idx == join->const_tables && s->table == join->sort_by_table)) |
|
6228 |
{ |
|
6229 |
if (best_record_count >= current_record_count && |
|
6230 |
best_read_time >= current_read_time && |
|
6231 |
(!(s->key_dependent & rest_tables) || records < 2.0)) |
|
6232 |
{ |
|
6233 |
best_record_count=current_record_count; |
|
6234 |
best_read_time=current_read_time; |
|
6235 |
} |
|
6236 |
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos); |
|
6237 |
if (find_best(join,rest_tables & ~real_table_bit,idx+1, |
|
6238 |
current_record_count,current_read_time)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6239 |
return(true); |
1
by brian
clean slate |
6240 |
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos); |
6241 |
} |
|
6242 |
restore_prev_nj_state(s); |
|
6243 |
restore_prev_sj_state(rest_tables, s); |
|
6244 |
if (join->select_options & SELECT_STRAIGHT_JOIN) |
|
6245 |
break; // Don't test all combinations |
|
6246 |
} |
|
6247 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6248 |
return(false); |
1
by brian
clean slate |
6249 |
} |
6250 |
||
6251 |
||
6252 |
/** |
|
6253 |
Find how much space the prevous read not const tables takes in cache. |
|
6254 |
*/ |
|
6255 |
||
77.1.45
by Monty Taylor
Warning fixes. |
6256 |
static void calc_used_field_length(THD *thd __attribute__((__unused__)), |
6257 |
JOIN_TAB *join_tab) |
|
1
by brian
clean slate |
6258 |
{ |
6259 |
uint null_fields,blobs,fields,rec_length; |
|
6260 |
Field **f_ptr,*field; |
|
6261 |
MY_BITMAP *read_set= join_tab->table->read_set;; |
|
6262 |
||
6263 |
null_fields= blobs= fields= rec_length=0; |
|
6264 |
for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++) |
|
6265 |
{ |
|
6266 |
if (bitmap_is_set(read_set, field->field_index)) |
|
6267 |
{ |
|
6268 |
uint flags=field->flags; |
|
6269 |
fields++; |
|
6270 |
rec_length+=field->pack_length(); |
|
6271 |
if (flags & BLOB_FLAG) |
|
6272 |
blobs++; |
|
6273 |
if (!(flags & NOT_NULL_FLAG)) |
|
6274 |
null_fields++; |
|
6275 |
} |
|
6276 |
} |
|
6277 |
if (null_fields) |
|
6278 |
rec_length+=(join_tab->table->s->null_fields+7)/8; |
|
6279 |
if (join_tab->table->maybe_null) |
|
148
by Brian Aker
my_bool cleanup |
6280 |
rec_length+=sizeof(bool); |
1
by brian
clean slate |
6281 |
if (blobs) |
6282 |
{ |
|
6283 |
uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length- |
|
6284 |
(join_tab->table->s->reclength- rec_length)); |
|
6285 |
rec_length+=(uint) max(4,blob_length); |
|
6286 |
} |
|
6287 |
join_tab->used_fields=fields; |
|
6288 |
join_tab->used_fieldlength=rec_length; |
|
6289 |
join_tab->used_blobs=blobs; |
|
6290 |
} |
|
6291 |
||
6292 |
||
6293 |
static uint |
|
6294 |
cache_record_length(JOIN *join,uint idx) |
|
6295 |
{ |
|
6296 |
uint length=0; |
|
6297 |
JOIN_TAB **pos,**end; |
|
6298 |
THD *thd=join->thd; |
|
6299 |
||
6300 |
for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ; |
|
6301 |
pos != end ; |
|
6302 |
pos++) |
|
6303 |
{ |
|
6304 |
JOIN_TAB *join_tab= *pos; |
|
6305 |
if (!join_tab->used_fieldlength) /* Not calced yet */ |
|
6306 |
calc_used_field_length(thd, join_tab); |
|
6307 |
length+=join_tab->used_fieldlength; |
|
6308 |
} |
|
6309 |
return length; |
|
6310 |
} |
|
6311 |
||
6312 |
||
6313 |
/* |
|
6314 |
Get the number of different row combinations for subset of partial join |
|
6315 |
||
6316 |
SYNOPSIS |
|
6317 |
prev_record_reads() |
|
6318 |
join The join structure |
|
6319 |
idx Number of tables in the partial join order (i.e. the |
|
6320 |
partial join order is in join->positions[0..idx-1]) |
|
6321 |
found_ref Bitmap of tables for which we need to find # of distinct |
|
6322 |
row combinations. |
|
6323 |
||
6324 |
DESCRIPTION |
|
6325 |
Given a partial join order (in join->positions[0..idx-1]) and a subset of |
|
6326 |
tables within that join order (specified in found_ref), find out how many |
|
6327 |
distinct row combinations of subset tables will be in the result of the |
|
6328 |
partial join order. |
|
6329 |
||
6330 |
This is used as follows: Suppose we have a table accessed with a ref-based |
|
6331 |
method. The ref access depends on current rows of tables in found_ref. |
|
6332 |
We want to count # of different ref accesses. We assume two ref accesses |
|
6333 |
will be different if at least one of access parameters is different. |
|
6334 |
Example: consider a query |
|
6335 |
||
6336 |
SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field |
|
6337 |
||
6338 |
and a join order: |
|
6339 |
t1, ref access on t1.key=c1 |
|
6340 |
t2, ref access on t2.key=c2 |
|
6341 |
t3, ref access on t3.key=t1.field |
|
6342 |
||
6343 |
For t1: n_ref_scans = 1, n_distinct_ref_scans = 1 |
|
6344 |
For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1 |
|
6345 |
For t3: n_ref_scans = records_read(t1)*records_read(t2) |
|
6346 |
n_distinct_ref_scans = #records_read(t1) |
|
6347 |
||
6348 |
The reason for having this function (at least the latest version of it) |
|
6349 |
is that we need to account for buffering in join execution. |
|
6350 |
||
6351 |
An edge-case example: if we have a non-first table in join accessed via |
|
6352 |
ref(const) or ref(param) where there is a small number of different |
|
6353 |
values of param, then the access will likely hit the disk cache and will |
|
6354 |
not require any disk seeks. |
|
6355 |
||
6356 |
The proper solution would be to assume an LRU disk cache of some size, |
|
6357 |
calculate probability of cache hits, etc. For now we just count |
|
6358 |
identical ref accesses as one. |
|
6359 |
||
6360 |
RETURN |
|
6361 |
Expected number of row combinations |
|
6362 |
*/ |
|
6363 |
||
6364 |
static double |
|
6365 |
prev_record_reads(JOIN *join, uint idx, table_map found_ref) |
|
6366 |
{ |
|
6367 |
double found=1.0; |
|
6368 |
POSITION *pos_end= join->positions - 1; |
|
6369 |
for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--) |
|
6370 |
{ |
|
6371 |
if (pos->table->table->map & found_ref) |
|
6372 |
{ |
|
6373 |
found_ref|= pos->ref_depend_map; |
|
6374 |
/* |
|
6375 |
For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table |
|
6376 |
with no matching row we will get position[t2].records_read==0. |
|
6377 |
Actually the size of output is one null-complemented row, therefore |
|
6378 |
we will use value of 1 whenever we get records_read==0. |
|
6379 |
||
6380 |
Note |
|
6381 |
- the above case can't occur if inner part of outer join has more |
|
6382 |
than one table: table with no matches will not be marked as const. |
|
6383 |
||
6384 |
- Ideally we should add 1 to records_read for every possible null- |
|
6385 |
complemented row. We're not doing it because: 1. it will require |
|
6386 |
non-trivial code and add overhead. 2. The value of records_read |
|
6387 |
is an inprecise estimate and adding 1 (or, in the worst case, |
|
6388 |
#max_nested_outer_joins=64-1) will not make it any more precise. |
|
6389 |
*/ |
|
6390 |
if (pos->records_read > DBL_EPSILON) |
|
6391 |
found*= pos->records_read; |
|
6392 |
} |
|
6393 |
} |
|
6394 |
return found; |
|
6395 |
} |
|
6396 |
||
6397 |
||
6398 |
/** |
|
6399 |
Set up join struct according to best position. |
|
6400 |
*/ |
|
6401 |
||
6402 |
static bool |
|
6403 |
get_best_combination(JOIN *join) |
|
6404 |
{ |
|
6405 |
uint i,tablenr; |
|
6406 |
table_map used_tables; |
|
6407 |
JOIN_TAB *join_tab,*j; |
|
6408 |
KEYUSE *keyuse; |
|
6409 |
uint table_count; |
|
6410 |
THD *thd=join->thd; |
|
6411 |
||
6412 |
table_count=join->tables; |
|
6413 |
if (!(join->join_tab=join_tab= |
|
6414 |
(JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6415 |
return(true); |
1
by brian
clean slate |
6416 |
|
6417 |
join->full_join=0; |
|
6418 |
||
6419 |
used_tables= OUTER_REF_TABLE_BIT; // Outer row is already read |
|
6420 |
for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++) |
|
6421 |
{ |
|
6422 |
TABLE *form; |
|
6423 |
*j= *join->best_positions[tablenr].table; |
|
6424 |
form=join->table[tablenr]=j->table; |
|
6425 |
used_tables|= form->map; |
|
6426 |
form->reginfo.join_tab=j; |
|
6427 |
if (!*j->on_expr_ref) |
|
6428 |
form->reginfo.not_exists_optimize=0; // Only with LEFT JOIN |
|
6429 |
if (j->type == JT_CONST) |
|
6430 |
continue; // Handled in make_join_stat.. |
|
6431 |
||
6432 |
j->ref.key = -1; |
|
6433 |
j->ref.key_parts=0; |
|
6434 |
||
6435 |
if (j->type == JT_SYSTEM) |
|
6436 |
continue; |
|
6437 |
if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key)) |
|
6438 |
{ |
|
6439 |
j->type=JT_ALL; |
|
6440 |
if (tablenr != join->const_tables) |
|
6441 |
join->full_join=1; |
|
6442 |
} |
|
6443 |
else if (create_ref_for_key(join, j, keyuse, used_tables)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6444 |
return(true); // Something went wrong |
1
by brian
clean slate |
6445 |
} |
6446 |
||
6447 |
for (i=0 ; i < table_count ; i++) |
|
6448 |
join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i; |
|
6449 |
update_depend_map(join); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6450 |
return(0); |
1
by brian
clean slate |
6451 |
} |
6452 |
||
6453 |
||
6454 |
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse, |
|
6455 |
table_map used_tables) |
|
6456 |
{ |
|
6457 |
KEYUSE *keyuse=org_keyuse; |
|
6458 |
THD *thd= join->thd; |
|
6459 |
uint keyparts,length,key; |
|
6460 |
TABLE *table; |
|
6461 |
KEY *keyinfo; |
|
6462 |
||
6463 |
/* Use best key from find_best */ |
|
6464 |
table=j->table; |
|
6465 |
key=keyuse->key; |
|
6466 |
keyinfo=table->key_info+key; |
|
6467 |
||
6468 |
{ |
|
6469 |
keyparts=length=0; |
|
6470 |
uint found_part_ref_or_null= 0; |
|
6471 |
/* |
|
6472 |
Calculate length for the used key |
|
6473 |
Stop if there is a missing key part or when we find second key_part |
|
6474 |
with KEY_OPTIMIZE_REF_OR_NULL |
|
6475 |
*/ |
|
6476 |
do |
|
6477 |
{ |
|
6478 |
if (!(~used_tables & keyuse->used_tables)) |
|
6479 |
{ |
|
6480 |
if (keyparts == keyuse->keypart && |
|
6481 |
!(found_part_ref_or_null & keyuse->optimize)) |
|
6482 |
{ |
|
6483 |
keyparts++; |
|
6484 |
length+= keyinfo->key_part[keyuse->keypart].store_length; |
|
6485 |
found_part_ref_or_null|= keyuse->optimize; |
|
6486 |
} |
|
6487 |
} |
|
6488 |
keyuse++; |
|
6489 |
} while (keyuse->table == table && keyuse->key == key); |
|
6490 |
} |
|
6491 |
||
6492 |
/* set up fieldref */ |
|
6493 |
keyinfo=table->key_info+key; |
|
6494 |
j->ref.key_parts=keyparts; |
|
6495 |
j->ref.key_length=length; |
|
6496 |
j->ref.key=(int) key; |
|
6497 |
if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) || |
|
6498 |
!(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) * |
|
6499 |
(keyparts+1)))) || |
|
6500 |
!(j->ref.items= (Item**) thd->alloc(sizeof(Item*)*keyparts)) || |
|
6501 |
!(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts))) |
|
6502 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6503 |
return(true); |
1
by brian
clean slate |
6504 |
} |
6505 |
j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length); |
|
6506 |
j->ref.key_err=1; |
|
6507 |
j->ref.null_rejecting= 0; |
|
55
by brian
Update for using real bool types. |
6508 |
j->ref.disable_cache= false; |
1
by brian
clean slate |
6509 |
keyuse=org_keyuse; |
6510 |
||
6511 |
store_key **ref_key= j->ref.key_copy; |
|
6512 |
uchar *key_buff=j->ref.key_buff, *null_ref_key= 0; |
|
55
by brian
Update for using real bool types. |
6513 |
bool keyuse_uses_no_tables= true; |
1
by brian
clean slate |
6514 |
{ |
6515 |
uint i; |
|
6516 |
for (i=0 ; i < keyparts ; keyuse++,i++) |
|
6517 |
{ |
|
6518 |
while (keyuse->keypart != i || |
|
6519 |
((~used_tables) & keyuse->used_tables)) |
|
6520 |
keyuse++; /* Skip other parts */ |
|
6521 |
||
6522 |
uint maybe_null= test(keyinfo->key_part[i].null_bit); |
|
6523 |
j->ref.items[i]=keyuse->val; // Save for cond removal |
|
6524 |
j->ref.cond_guards[i]= keyuse->cond_guard; |
|
6525 |
if (keyuse->null_rejecting) |
|
6526 |
j->ref.null_rejecting |= 1 << i; |
|
6527 |
keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables; |
|
6528 |
if (!keyuse->used_tables && |
|
6529 |
!(join->select_options & SELECT_DESCRIBE)) |
|
6530 |
{ // Compare against constant |
|
6531 |
store_key_item tmp(thd, keyinfo->key_part[i].field, |
|
6532 |
key_buff + maybe_null, |
|
6533 |
maybe_null ? key_buff : 0, |
|
6534 |
keyinfo->key_part[i].length, keyuse->val); |
|
6535 |
if (thd->is_fatal_error) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6536 |
return(true); |
1
by brian
clean slate |
6537 |
tmp.copy(); |
6538 |
} |
|
6539 |
else |
|
6540 |
*ref_key++= get_store_key(thd, |
|
6541 |
keyuse,join->const_table_map, |
|
6542 |
&keyinfo->key_part[i], |
|
6543 |
key_buff, maybe_null); |
|
6544 |
/* |
|
6545 |
Remember if we are going to use REF_OR_NULL |
|
6546 |
But only if field _really_ can be null i.e. we force JT_REF |
|
6547 |
instead of JT_REF_OR_NULL in case if field can't be null |
|
6548 |
*/ |
|
6549 |
if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null) |
|
6550 |
null_ref_key= key_buff; |
|
6551 |
key_buff+=keyinfo->key_part[i].store_length; |
|
6552 |
} |
|
6553 |
} |
|
6554 |
*ref_key=0; // end_marker |
|
6555 |
if (j->type == JT_CONST) |
|
6556 |
j->table->const_table= 1; |
|
53.2.14
by Monty Taylor
Removed HA_END_SPACE_KEY and references to it. It was _supposed_ to be gone anyway, but the ifdef around it was broken (MYSQL_VERSION_ID was actually undefined.) |
6557 |
else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) || |
6558 |
keyparts != keyinfo->key_parts || null_ref_key) |
|
1
by brian
clean slate |
6559 |
{ |
6560 |
/* Must read with repeat */ |
|
6561 |
j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF; |
|
6562 |
j->ref.null_ref_key= null_ref_key; |
|
6563 |
} |
|
6564 |
else if (keyuse_uses_no_tables) |
|
6565 |
{ |
|
6566 |
/* |
|
6567 |
This happen if we are using a constant expression in the ON part |
|
6568 |
of an LEFT JOIN. |
|
6569 |
SELECT * FROM a LEFT JOIN b ON b.key=30 |
|
6570 |
Here we should not mark the table as a 'const' as a field may |
|
6571 |
have a 'normal' value or a NULL value. |
|
6572 |
*/ |
|
6573 |
j->type=JT_CONST; |
|
6574 |
} |
|
6575 |
else |
|
6576 |
j->type=JT_EQ_REF; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6577 |
return(0); |
1
by brian
clean slate |
6578 |
} |
6579 |
||
6580 |
||
6581 |
||
6582 |
static store_key * |
|
6583 |
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables, |
|
6584 |
KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null) |
|
6585 |
{ |
|
6586 |
if (!((~used_tables) & keyuse->used_tables)) // if const item |
|
6587 |
{ |
|
6588 |
return new store_key_const_item(thd, |
|
6589 |
key_part->field, |
|
6590 |
key_buff + maybe_null, |
|
6591 |
maybe_null ? key_buff : 0, |
|
6592 |
key_part->length, |
|
6593 |
keyuse->val); |
|
6594 |
} |
|
6595 |
else if (keyuse->val->type() == Item::FIELD_ITEM || |
|
6596 |
(keyuse->val->type() == Item::REF_ITEM && |
|
6597 |
((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF && |
|
6598 |
(*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() == |
|
6599 |
Item_ref::DIRECT_REF && |
|
6600 |
keyuse->val->real_item()->type() == Item::FIELD_ITEM)) |
|
6601 |
return new store_key_field(thd, |
|
6602 |
key_part->field, |
|
6603 |
key_buff + maybe_null, |
|
6604 |
maybe_null ? key_buff : 0, |
|
6605 |
key_part->length, |
|
6606 |
((Item_field*) keyuse->val->real_item())->field, |
|
6607 |
keyuse->val->full_name()); |
|
6608 |
return new store_key_item(thd, |
|
6609 |
key_part->field, |
|
6610 |
key_buff + maybe_null, |
|
6611 |
maybe_null ? key_buff : 0, |
|
6612 |
key_part->length, |
|
6613 |
keyuse->val); |
|
6614 |
} |
|
6615 |
||
6616 |
/** |
|
6617 |
This function is only called for const items on fields which are keys. |
|
6618 |
||
6619 |
@return |
|
6620 |
returns 1 if there was some conversion made when the field was stored. |
|
6621 |
*/ |
|
6622 |
||
6623 |
bool |
|
6624 |
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag) |
|
6625 |
{ |
|
6626 |
bool error; |
|
6627 |
TABLE *table= field->table; |
|
6628 |
THD *thd= table->in_use; |
|
6629 |
ha_rows cuted_fields=thd->cuted_fields; |
|
6630 |
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, |
|
6631 |
table->write_set); |
|
6632 |
||
6633 |
/* |
|
6634 |
we should restore old value of count_cuted_fields because |
|
6635 |
store_val_in_field can be called from mysql_insert |
|
6636 |
with select_insert, which make count_cuted_fields= 1 |
|
6637 |
*/ |
|
6638 |
enum_check_fields old_count_cuted_fields= thd->count_cuted_fields; |
|
6639 |
thd->count_cuted_fields= check_flag; |
|
6640 |
error= item->save_in_field(field, 1); |
|
6641 |
thd->count_cuted_fields= old_count_cuted_fields; |
|
6642 |
dbug_tmp_restore_column_map(table->write_set, old_map); |
|
6643 |
return error || cuted_fields != thd->cuted_fields; |
|
6644 |
} |
|
6645 |
||
6646 |
||
6647 |
static bool |
|
6648 |
make_simple_join(JOIN *join,TABLE *tmp_table) |
|
6649 |
{ |
|
6650 |
TABLE **tableptr; |
|
6651 |
JOIN_TAB *join_tab; |
|
6652 |
||
6653 |
/* |
|
6654 |
Reuse TABLE * and JOIN_TAB if already allocated by a previous call |
|
6655 |
to this function through JOIN::exec (may happen for sub-queries). |
|
6656 |
*/ |
|
6657 |
if (!join->table_reexec) |
|
6658 |
{ |
|
6659 |
if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*)))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6660 |
return(true); /* purecov: inspected */ |
1
by brian
clean slate |
6661 |
if (join->tmp_join) |
6662 |
join->tmp_join->table_reexec= join->table_reexec; |
|
6663 |
} |
|
6664 |
if (!join->join_tab_reexec) |
|
6665 |
{ |
|
6666 |
if (!(join->join_tab_reexec= |
|
6667 |
(JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB)))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6668 |
return(true); /* purecov: inspected */ |
1
by brian
clean slate |
6669 |
if (join->tmp_join) |
6670 |
join->tmp_join->join_tab_reexec= join->join_tab_reexec; |
|
6671 |
} |
|
6672 |
tableptr= join->table_reexec; |
|
6673 |
join_tab= join->join_tab_reexec; |
|
6674 |
||
6675 |
join->join_tab=join_tab; |
|
6676 |
join->table=tableptr; tableptr[0]=tmp_table; |
|
6677 |
join->tables=1; |
|
6678 |
join->const_tables=0; |
|
6679 |
join->const_table_map=0; |
|
6680 |
join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count= |
|
6681 |
join->tmp_table_param.func_count=0; |
|
6682 |
join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0; |
|
6683 |
join->first_record=join->sort_and_group=0; |
|
6684 |
join->send_records=(ha_rows) 0; |
|
6685 |
join->group=0; |
|
6686 |
join->row_limit=join->unit->select_limit_cnt; |
|
6687 |
join->do_send_rows = (join->row_limit) ? 1 : 0; |
|
6688 |
||
6689 |
join_tab->cache.buff=0; /* No caching */ |
|
6690 |
join_tab->table=tmp_table; |
|
6691 |
join_tab->select=0; |
|
6692 |
join_tab->select_cond=0; |
|
6693 |
join_tab->quick=0; |
|
6694 |
join_tab->type= JT_ALL; /* Map through all records */ |
|
6695 |
join_tab->keys.init(); |
|
6696 |
join_tab->keys.set_all(); /* test everything in quick */ |
|
6697 |
join_tab->info=0; |
|
6698 |
join_tab->on_expr_ref=0; |
|
6699 |
join_tab->last_inner= 0; |
|
6700 |
join_tab->first_unmatched= 0; |
|
6701 |
join_tab->ref.key = -1; |
|
6702 |
join_tab->not_used_in_distinct=0; |
|
6703 |
join_tab->read_first_record= join_init_read_record; |
|
6704 |
join_tab->join=join; |
|
6705 |
join_tab->ref.key_parts= 0; |
|
6706 |
join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL; |
|
6707 |
join_tab->do_firstmatch= NULL; |
|
6708 |
bzero((char*) &join_tab->read_record,sizeof(join_tab->read_record)); |
|
6709 |
tmp_table->status=0; |
|
6710 |
tmp_table->null_row=0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6711 |
return(false); |
1
by brian
clean slate |
6712 |
} |
6713 |
||
6714 |
||
6715 |
inline void add_cond_and_fix(Item **e1, Item *e2) |
|
6716 |
{ |
|
6717 |
if (*e1) |
|
6718 |
{ |
|
6719 |
Item *res; |
|
6720 |
if ((res= new Item_cond_and(*e1, e2))) |
|
6721 |
{ |
|
6722 |
*e1= res; |
|
6723 |
res->quick_fix_field(); |
|
6724 |
} |
|
6725 |
} |
|
6726 |
else |
|
6727 |
*e1= e2; |
|
6728 |
} |
|
6729 |
||
6730 |
||
6731 |
/** |
|
6732 |
Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions |
|
6733 |
we've inferred from ref/eq_ref access performed. |
|
6734 |
||
6735 |
This function is a part of "Early NULL-values filtering for ref access" |
|
6736 |
optimization. |
|
6737 |
||
6738 |
Example of this optimization: |
|
6739 |
For query SELECT * FROM t1,t2 WHERE t2.key=t1.field @n |
|
6740 |
and plan " any-access(t1), ref(t2.key=t1.field) " @n |
|
6741 |
add "t1.field IS NOT NULL" to t1's table condition. @n |
|
6742 |
||
6743 |
Description of the optimization: |
|
6744 |
||
6745 |
We look through equalities choosen to perform ref/eq_ref access, |
|
6746 |
pick equalities that have form "tbl.part_of_key = othertbl.field" |
|
6747 |
(where othertbl is a non-const table and othertbl.field may be NULL) |
|
6748 |
and add them to conditions on correspoding tables (othertbl in this |
|
6749 |
example). |
|
6750 |
||
6751 |
Exception from that is the case when referred_tab->join != join. |
|
6752 |
I.e. don't add NOT NULL constraints from any embedded subquery. |
|
6753 |
Consider this query: |
|
6754 |
@code |
|
6755 |
SELECT A.f2 FROM t1 LEFT JOIN t2 A ON A.f2 = f1 |
|
6756 |
WHERE A.f3=(SELECT MIN(f3) FROM t2 C WHERE A.f4 = C.f4) OR A.f3 IS NULL; |
|
6757 |
@endocde |
|
6758 |
Here condition A.f3 IS NOT NULL is going to be added to the WHERE |
|
6759 |
condition of the embedding query. |
|
6760 |
Another example: |
|
6761 |
SELECT * FROM t10, t11 WHERE (t10.a < 10 OR t10.a IS NULL) |
|
6762 |
AND t11.b <=> t10.b AND (t11.a = (SELECT MAX(a) FROM t12 |
|
6763 |
WHERE t12.b = t10.a )); |
|
6764 |
Here condition t10.a IS NOT NULL is going to be added. |
|
6765 |
In both cases addition of NOT NULL condition will erroneously reject |
|
6766 |
some rows of the result set. |
|
6767 |
referred_tab->join != join constraint would disallow such additions. |
|
6768 |
||
6769 |
This optimization doesn't affect the choices that ref, range, or join |
|
6770 |
optimizer make. This was intentional because this was added after 4.1 |
|
6771 |
was GA. |
|
6772 |
||
6773 |
Implementation overview |
|
6774 |
1. update_ref_and_keys() accumulates info about null-rejecting |
|
6775 |
predicates in in KEY_FIELD::null_rejecting |
|
6776 |
1.1 add_key_part saves these to KEYUSE. |
|
6777 |
2. create_ref_for_key copies them to TABLE_REF. |
|
6778 |
3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of |
|
6779 |
appropiate JOIN_TAB members. |
|
6780 |
*/ |
|
6781 |
||
6782 |
static void add_not_null_conds(JOIN *join) |
|
6783 |
{ |
|
6784 |
for (uint i=join->const_tables ; i < join->tables ; i++) |
|
6785 |
{ |
|
6786 |
JOIN_TAB *tab=join->join_tab+i; |
|
6787 |
if ((tab->type == JT_REF || tab->type == JT_EQ_REF || |
|
6788 |
tab->type == JT_REF_OR_NULL) && |
|
6789 |
!tab->table->maybe_null) |
|
6790 |
{ |
|
6791 |
for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++) |
|
6792 |
{ |
|
6793 |
if (tab->ref.null_rejecting & (1 << keypart)) |
|
6794 |
{ |
|
6795 |
Item *item= tab->ref.items[keypart]; |
|
6796 |
Item *notnull; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6797 |
assert(item->type() == Item::FIELD_ITEM); |
1
by brian
clean slate |
6798 |
Item_field *not_null_item= (Item_field*)item; |
6799 |
JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab; |
|
6800 |
/* |
|
6801 |
For UPDATE queries such as: |
|
6802 |
UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1); |
|
6803 |
not_null_item is the t1.f1, but it's referred_tab is 0. |
|
6804 |
*/ |
|
6805 |
if (!referred_tab || referred_tab->join != join) |
|
6806 |
continue; |
|
6807 |
if (!(notnull= new Item_func_isnotnull(not_null_item))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6808 |
return; |
1
by brian
clean slate |
6809 |
/* |
6810 |
We need to do full fix_fields() call here in order to have correct |
|
6811 |
notnull->const_item(). This is needed e.g. by test_quick_select |
|
6812 |
when it is called from make_join_select after this function is |
|
6813 |
called. |
|
6814 |
*/ |
|
6815 |
if (notnull->fix_fields(join->thd, ¬null)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6816 |
return; |
1
by brian
clean slate |
6817 |
add_cond_and_fix(&referred_tab->select_cond, notnull); |
6818 |
} |
|
6819 |
} |
|
6820 |
} |
|
6821 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6822 |
return; |
1
by brian
clean slate |
6823 |
} |
6824 |
||
6825 |
/** |
|
6826 |
Build a predicate guarded by match variables for embedding outer joins. |
|
6827 |
The function recursively adds guards for predicate cond |
|
6828 |
assending from tab to the first inner table next embedding |
|
6829 |
nested outer join and so on until it reaches root_tab |
|
6830 |
(root_tab can be 0). |
|
6831 |
||
6832 |
@param tab the first inner table for most nested outer join |
|
6833 |
@param cond the predicate to be guarded (must be set) |
|
6834 |
@param root_tab the first inner table to stop |
|
6835 |
||
6836 |
@return |
|
6837 |
- pointer to the guarded predicate, if success |
|
6838 |
- 0, otherwise |
|
6839 |
*/ |
|
6840 |
||
6841 |
static COND* |
|
6842 |
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab) |
|
6843 |
{ |
|
6844 |
COND *tmp; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6845 |
assert(cond != 0); |
1
by brian
clean slate |
6846 |
if (tab == root_tab) |
6847 |
return cond; |
|
6848 |
if ((tmp= add_found_match_trig_cond(tab->first_upper, cond, root_tab))) |
|
6849 |
tmp= new Item_func_trig_cond(tmp, &tab->found); |
|
6850 |
if (tmp) |
|
6851 |
{ |
|
6852 |
tmp->quick_fix_field(); |
|
6853 |
tmp->update_used_tables(); |
|
6854 |
} |
|
6855 |
return tmp; |
|
6856 |
} |
|
6857 |
||
6858 |
||
6859 |
/** |
|
6860 |
Fill in outer join related info for the execution plan structure. |
|
6861 |
||
6862 |
For each outer join operation left after simplification of the |
|
6863 |
original query the function set up the following pointers in the linear |
|
6864 |
structure join->join_tab representing the selected execution plan. |
|
6865 |
The first inner table t0 for the operation is set to refer to the last |
|
6866 |
inner table tk through the field t0->last_inner. |
|
6867 |
Any inner table ti for the operation are set to refer to the first |
|
6868 |
inner table ti->first_inner. |
|
6869 |
The first inner table t0 for the operation is set to refer to the |
|
6870 |
first inner table of the embedding outer join operation, if there is any, |
|
6871 |
through the field t0->first_upper. |
|
6872 |
The on expression for the outer join operation is attached to the |
|
6873 |
corresponding first inner table through the field t0->on_expr_ref. |
|
6874 |
Here ti are structures of the JOIN_TAB type. |
|
6875 |
||
6876 |
EXAMPLE. For the query: |
|
6877 |
@code |
|
6878 |
SELECT * FROM t1 |
|
6879 |
LEFT JOIN |
|
6880 |
(t2, t3 LEFT JOIN t4 ON t3.a=t4.a) |
|
6881 |
ON (t1.a=t2.a AND t1.b=t3.b) |
|
6882 |
WHERE t1.c > 5, |
|
6883 |
@endcode |
|
6884 |
||
6885 |
given the execution plan with the table order t1,t2,t3,t4 |
|
6886 |
is selected, the following references will be set; |
|
6887 |
t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2] |
|
6888 |
t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2], |
|
6889 |
on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to |
|
6890 |
*t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref. |
|
6891 |
||
6892 |
@param join reference to the info fully describing the query |
|
6893 |
||
6894 |
@note |
|
6895 |
The function assumes that the simplification procedure has been |
|
6896 |
already applied to the join query (see simplify_joins). |
|
6897 |
This function can be called only after the execution plan |
|
6898 |
has been chosen. |
|
6899 |
*/ |
|
6900 |
||
6901 |
static void |
|
6902 |
make_outerjoin_info(JOIN *join) |
|
6903 |
{ |
|
6904 |
for (uint i=join->const_tables ; i < join->tables ; i++) |
|
6905 |
{ |
|
6906 |
JOIN_TAB *tab=join->join_tab+i; |
|
6907 |
TABLE *table=tab->table; |
|
6908 |
TABLE_LIST *tbl= table->pos_in_table_list; |
|
6909 |
TABLE_LIST *embedding= tbl->embedding; |
|
6910 |
||
6911 |
if (tbl->outer_join) |
|
6912 |
{ |
|
6913 |
/* |
|
6914 |
Table tab is the only one inner table for outer join. |
|
6915 |
(Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a |
|
6916 |
is in the query above.) |
|
6917 |
*/ |
|
6918 |
tab->last_inner= tab->first_inner= tab; |
|
6919 |
tab->on_expr_ref= &tbl->on_expr; |
|
6920 |
tab->cond_equal= tbl->cond_equal; |
|
6921 |
if (embedding) |
|
6922 |
tab->first_upper= embedding->nested_join->first_nested; |
|
6923 |
} |
|
6924 |
for ( ; embedding ; embedding= embedding->embedding) |
|
6925 |
{ |
|
6926 |
/* Ignore sj-nests: */ |
|
6927 |
if (!embedding->on_expr) |
|
6928 |
continue; |
|
6929 |
NESTED_JOIN *nested_join= embedding->nested_join; |
|
6930 |
if (!nested_join->counter_) |
|
6931 |
{ |
|
6932 |
/* |
|
6933 |
Table tab is the first inner table for nested_join. |
|
6934 |
Save reference to it in the nested join structure. |
|
6935 |
*/ |
|
6936 |
nested_join->first_nested= tab; |
|
6937 |
tab->on_expr_ref= &embedding->on_expr; |
|
6938 |
tab->cond_equal= tbl->cond_equal; |
|
6939 |
if (embedding->embedding) |
|
6940 |
tab->first_upper= embedding->embedding->nested_join->first_nested; |
|
6941 |
} |
|
6942 |
if (!tab->first_inner) |
|
6943 |
tab->first_inner= nested_join->first_nested; |
|
6944 |
if (++nested_join->counter_ < nested_join->join_list.elements) |
|
6945 |
break; |
|
6946 |
/* Table tab is the last inner table for nested join. */ |
|
6947 |
nested_join->first_nested->last_inner= tab; |
|
6948 |
} |
|
6949 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6950 |
return; |
1
by brian
clean slate |
6951 |
} |
6952 |
||
6953 |
||
6954 |
static bool |
|
6955 |
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) |
|
6956 |
{ |
|
6957 |
THD *thd= join->thd; |
|
6958 |
if (select) |
|
6959 |
{ |
|
6960 |
add_not_null_conds(join); |
|
6961 |
table_map used_tables; |
|
6962 |
if (cond) /* Because of QUICK_GROUP_MIN_MAX_SELECT */ |
|
6963 |
{ /* there may be a select without a cond. */ |
|
6964 |
if (join->tables > 1) |
|
6965 |
cond->update_used_tables(); // Tablenr may have changed |
|
6966 |
if (join->const_tables == join->tables && |
|
6967 |
thd->lex->current_select->master_unit() == |
|
6968 |
&thd->lex->unit) // not upper level SELECT |
|
6969 |
join->const_table_map|=RAND_TABLE_BIT; |
|
6970 |
{ // Check const tables |
|
6971 |
COND *const_cond= |
|
6972 |
make_cond_for_table(cond, |
|
6973 |
join->const_table_map, |
|
6974 |
(table_map) 0, 1); |
|
6975 |
for (JOIN_TAB *tab= join->join_tab+join->const_tables; |
|
6976 |
tab < join->join_tab+join->tables ; tab++) |
|
6977 |
{ |
|
6978 |
if (*tab->on_expr_ref) |
|
6979 |
{ |
|
6980 |
JOIN_TAB *cond_tab= tab->first_inner; |
|
6981 |
COND *tmp= make_cond_for_table(*tab->on_expr_ref, |
|
6982 |
join->const_table_map, |
|
6983 |
( table_map) 0, 0); |
|
6984 |
if (!tmp) |
|
6985 |
continue; |
|
6986 |
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl); |
|
6987 |
if (!tmp) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6988 |
return(1); |
1
by brian
clean slate |
6989 |
tmp->quick_fix_field(); |
6990 |
cond_tab->select_cond= !cond_tab->select_cond ? tmp : |
|
6991 |
new Item_cond_and(cond_tab->select_cond, |
|
6992 |
tmp); |
|
6993 |
if (!cond_tab->select_cond) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
6994 |
return(1); |
1
by brian
clean slate |
6995 |
cond_tab->select_cond->quick_fix_field(); |
6996 |
} |
|
6997 |
} |
|
6998 |
if (const_cond && !const_cond->val_int()) |
|
6999 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7000 |
return(1); // Impossible const condition |
1
by brian
clean slate |
7001 |
} |
7002 |
} |
|
7003 |
} |
|
7004 |
used_tables=((select->const_tables=join->const_table_map) | |
|
7005 |
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT); |
|
7006 |
for (uint i=join->const_tables ; i < join->tables ; i++) |
|
7007 |
{ |
|
7008 |
JOIN_TAB *tab=join->join_tab+i; |
|
7009 |
/* |
|
7010 |
first_inner is the X in queries like: |
|
7011 |
SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X |
|
7012 |
*/ |
|
7013 |
JOIN_TAB *first_inner_tab= tab->first_inner; |
|
7014 |
table_map current_map= tab->table->map; |
|
7015 |
bool use_quick_range=0; |
|
7016 |
COND *tmp; |
|
7017 |
||
7018 |
/* |
|
7019 |
Following force including random expression in last table condition. |
|
7020 |
It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5 |
|
7021 |
*/ |
|
7022 |
if (i == join->tables-1) |
|
7023 |
current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT; |
|
7024 |
used_tables|=current_map; |
|
7025 |
||
7026 |
if (tab->type == JT_REF && tab->quick && |
|
7027 |
(uint) tab->ref.key == tab->quick->index && |
|
7028 |
tab->ref.key_length < tab->quick->max_used_key_length) |
|
7029 |
{ |
|
7030 |
/* Range uses longer key; Use this instead of ref on key */ |
|
7031 |
tab->type=JT_ALL; |
|
7032 |
use_quick_range=1; |
|
7033 |
tab->use_quick=1; |
|
7034 |
tab->ref.key= -1; |
|
7035 |
tab->ref.key_parts=0; // Don't use ref key. |
|
7036 |
join->best_positions[i].records_read= rows2double(tab->quick->records); |
|
7037 |
/* |
|
7038 |
We will use join cache here : prevent sorting of the first |
|
7039 |
table only and sort at the end. |
|
7040 |
*/ |
|
7041 |
if (i != join->const_tables && join->tables > join->const_tables + 1) |
|
7042 |
join->full_join= 1; |
|
7043 |
} |
|
7044 |
||
7045 |
tmp= NULL; |
|
7046 |
if (cond) |
|
7047 |
tmp= make_cond_for_table(cond,used_tables,current_map, 0); |
|
7048 |
if (cond && !tmp && tab->quick) |
|
7049 |
{ // Outer join |
|
7050 |
if (tab->type != JT_ALL) |
|
7051 |
{ |
|
7052 |
/* |
|
7053 |
Don't use the quick method |
|
7054 |
We come here in the case where we have 'key=constant' and |
|
7055 |
the test is removed by make_cond_for_table() |
|
7056 |
*/ |
|
7057 |
delete tab->quick; |
|
7058 |
tab->quick= 0; |
|
7059 |
} |
|
7060 |
else |
|
7061 |
{ |
|
7062 |
/* |
|
7063 |
Hack to handle the case where we only refer to a table |
|
7064 |
in the ON part of an OUTER JOIN. In this case we want the code |
|
7065 |
below to check if we should use 'quick' instead. |
|
7066 |
*/ |
|
152
by Brian Aker
longlong replacement |
7067 |
tmp= new Item_int((int64_t) 1,1); // Always true |
1
by brian
clean slate |
7068 |
} |
7069 |
||
7070 |
} |
|
7071 |
if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL || |
|
7072 |
tab->type == JT_EQ_REF) |
|
7073 |
{ |
|
7074 |
SQL_SELECT *sel= tab->select= ((SQL_SELECT*) |
|
7075 |
thd->memdup((uchar*) select, |
|
7076 |
sizeof(*select))); |
|
7077 |
if (!sel) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7078 |
return(1); // End of memory |
1
by brian
clean slate |
7079 |
/* |
7080 |
If tab is an inner table of an outer join operation, |
|
7081 |
add a match guard to the pushed down predicate. |
|
7082 |
The guard will turn the predicate on only after |
|
7083 |
the first match for outer tables is encountered. |
|
7084 |
*/ |
|
7085 |
if (cond && tmp) |
|
7086 |
{ |
|
7087 |
/* |
|
7088 |
Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without |
|
7089 |
a cond, so neutralize the hack above. |
|
7090 |
*/ |
|
7091 |
if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7092 |
return(1); |
1
by brian
clean slate |
7093 |
tab->select_cond=sel->cond=tmp; |
7094 |
/* Push condition to storage engine if this is enabled |
|
7095 |
and the condition is not guarded */ |
|
7096 |
tab->table->file->pushed_cond= NULL; |
|
7097 |
if (thd->variables.engine_condition_pushdown) |
|
7098 |
{ |
|
7099 |
COND *push_cond= |
|
7100 |
make_cond_for_table(tmp, current_map, current_map, 0); |
|
7101 |
if (push_cond) |
|
7102 |
{ |
|
7103 |
/* Push condition to handler */ |
|
7104 |
if (!tab->table->file->cond_push(push_cond)) |
|
7105 |
tab->table->file->pushed_cond= push_cond; |
|
7106 |
} |
|
7107 |
} |
|
7108 |
} |
|
7109 |
else |
|
7110 |
tab->select_cond= sel->cond= NULL; |
|
7111 |
||
7112 |
sel->head=tab->table; |
|
7113 |
if (tab->quick) |
|
7114 |
{ |
|
7115 |
/* Use quick key read if it's a constant and it's not used |
|
7116 |
with key reading */ |
|
7117 |
if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF |
|
7118 |
&& (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index)) |
|
7119 |
{ |
|
7120 |
sel->quick=tab->quick; // Use value from get_quick_... |
|
7121 |
sel->quick_keys.clear_all(); |
|
7122 |
sel->needed_reg.clear_all(); |
|
7123 |
} |
|
7124 |
else |
|
7125 |
{ |
|
7126 |
delete tab->quick; |
|
7127 |
} |
|
7128 |
tab->quick=0; |
|
7129 |
} |
|
7130 |
uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1; |
|
7131 |
if (i == join->const_tables && ref_key) |
|
7132 |
{ |
|
7133 |
if (!tab->const_keys.is_clear_all() && |
|
7134 |
tab->table->reginfo.impossible_range) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7135 |
return(1); |
1
by brian
clean slate |
7136 |
} |
7137 |
else if (tab->type == JT_ALL && ! use_quick_range) |
|
7138 |
{ |
|
7139 |
if (!tab->const_keys.is_clear_all() && |
|
7140 |
tab->table->reginfo.impossible_range) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7141 |
return(1); // Impossible range |
1
by brian
clean slate |
7142 |
/* |
7143 |
We plan to scan all rows. |
|
7144 |
Check again if we should use an index. |
|
7145 |
We could have used an column from a previous table in |
|
7146 |
the index if we are using limit and this is the first table |
|
7147 |
*/ |
|
7148 |
||
7149 |
if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) || |
|
7150 |
(!tab->const_keys.is_clear_all() && (i == join->const_tables) && (join->unit->select_limit_cnt < join->best_positions[i].records_read) && ((join->select_options & OPTION_FOUND_ROWS) == false))) |
|
7151 |
{ |
|
7152 |
/* Join with outer join condition */ |
|
7153 |
COND *orig_cond=sel->cond; |
|
7154 |
sel->cond= and_conds(sel->cond, *tab->on_expr_ref); |
|
7155 |
||
7156 |
/* |
|
7157 |
We can't call sel->cond->fix_fields, |
|
7158 |
as it will break tab->on_expr if it's AND condition |
|
7159 |
(fix_fields currently removes extra AND/OR levels). |
|
7160 |
Yet attributes of the just built condition are not needed. |
|
7161 |
Thus we call sel->cond->quick_fix_field for safety. |
|
7162 |
*/ |
|
7163 |
if (sel->cond && !sel->cond->fixed) |
|
7164 |
sel->cond->quick_fix_field(); |
|
7165 |
||
7166 |
if (sel->test_quick_select(thd, tab->keys, |
|
7167 |
used_tables & ~ current_map, |
|
7168 |
(join->select_options & |
|
7169 |
OPTION_FOUND_ROWS ? |
|
7170 |
HA_POS_ERROR : |
|
7171 |
join->unit->select_limit_cnt), 0, |
|
55
by brian
Update for using real bool types. |
7172 |
false) < 0) |
1
by brian
clean slate |
7173 |
{ |
7174 |
/* |
|
7175 |
Before reporting "Impossible WHERE" for the whole query |
|
7176 |
we have to check isn't it only "impossible ON" instead |
|
7177 |
*/ |
|
7178 |
sel->cond=orig_cond; |
|
7179 |
if (!*tab->on_expr_ref || |
|
7180 |
sel->test_quick_select(thd, tab->keys, |
|
7181 |
used_tables & ~ current_map, |
|
7182 |
(join->select_options & |
|
7183 |
OPTION_FOUND_ROWS ? |
|
7184 |
HA_POS_ERROR : |
|
7185 |
join->unit->select_limit_cnt),0, |
|
55
by brian
Update for using real bool types. |
7186 |
false) < 0) |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7187 |
return(1); // Impossible WHERE |
1
by brian
clean slate |
7188 |
} |
7189 |
else |
|
7190 |
sel->cond=orig_cond; |
|
7191 |
||
7192 |
/* Fix for EXPLAIN */ |
|
7193 |
if (sel->quick) |
|
7194 |
join->best_positions[i].records_read= (double)sel->quick->records; |
|
7195 |
} |
|
7196 |
else |
|
7197 |
{ |
|
7198 |
sel->needed_reg=tab->needed_reg; |
|
7199 |
sel->quick_keys.clear_all(); |
|
7200 |
} |
|
7201 |
if (!sel->quick_keys.is_subset(tab->checked_keys) || |
|
7202 |
!sel->needed_reg.is_subset(tab->checked_keys)) |
|
7203 |
{ |
|
7204 |
tab->keys=sel->quick_keys; |
|
7205 |
tab->keys.merge(sel->needed_reg); |
|
7206 |
tab->use_quick= (!sel->needed_reg.is_clear_all() && |
|
7207 |
(select->quick_keys.is_clear_all() || |
|
7208 |
(select->quick && |
|
7209 |
(select->quick->records >= 100L)))) ? |
|
7210 |
2 : 1; |
|
7211 |
sel->read_tables= used_tables & ~current_map; |
|
7212 |
} |
|
7213 |
if (i != join->const_tables && tab->use_quick != 2) |
|
7214 |
{ /* Read with cache */ |
|
7215 |
if (cond && |
|
7216 |
(tmp=make_cond_for_table(cond, |
|
7217 |
join->const_table_map | |
|
7218 |
current_map, |
|
7219 |
current_map, 0))) |
|
7220 |
{ |
|
7221 |
tab->cache.select=(SQL_SELECT*) |
|
7222 |
thd->memdup((uchar*) sel, sizeof(SQL_SELECT)); |
|
7223 |
tab->cache.select->cond=tmp; |
|
7224 |
tab->cache.select->read_tables=join->const_table_map; |
|
7225 |
} |
|
7226 |
} |
|
7227 |
} |
|
7228 |
} |
|
7229 |
||
7230 |
/* |
|
7231 |
Push down conditions from all on expressions. |
|
7232 |
Each of these conditions are guarded by a variable |
|
7233 |
that turns if off just before null complemented row for |
|
7234 |
outer joins is formed. Thus, the condition from an |
|
7235 |
'on expression' are guaranteed not to be checked for |
|
7236 |
the null complemented row. |
|
7237 |
*/ |
|
7238 |
||
7239 |
/* First push down constant conditions from on expressions */ |
|
7240 |
for (JOIN_TAB *join_tab= join->join_tab+join->const_tables; |
|
7241 |
join_tab < join->join_tab+join->tables ; join_tab++) |
|
7242 |
{ |
|
7243 |
if (*join_tab->on_expr_ref) |
|
7244 |
{ |
|
7245 |
JOIN_TAB *cond_tab= join_tab->first_inner; |
|
7246 |
COND *tmp= make_cond_for_table(*join_tab->on_expr_ref, |
|
7247 |
join->const_table_map, |
|
7248 |
(table_map) 0, 0); |
|
7249 |
if (!tmp) |
|
7250 |
continue; |
|
7251 |
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl); |
|
7252 |
if (!tmp) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7253 |
return(1); |
1
by brian
clean slate |
7254 |
tmp->quick_fix_field(); |
7255 |
cond_tab->select_cond= !cond_tab->select_cond ? tmp : |
|
7256 |
new Item_cond_and(cond_tab->select_cond,tmp); |
|
7257 |
if (!cond_tab->select_cond) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7258 |
return(1); |
1
by brian
clean slate |
7259 |
cond_tab->select_cond->quick_fix_field(); |
7260 |
} |
|
7261 |
} |
|
7262 |
||
7263 |
/* Push down non-constant conditions from on expressions */ |
|
7264 |
JOIN_TAB *last_tab= tab; |
|
7265 |
while (first_inner_tab && first_inner_tab->last_inner == last_tab) |
|
7266 |
{ |
|
7267 |
/* |
|
7268 |
Table tab is the last inner table of an outer join. |
|
7269 |
An on expression is always attached to it. |
|
7270 |
*/ |
|
7271 |
COND *on_expr= *first_inner_tab->on_expr_ref; |
|
7272 |
||
7273 |
table_map used_tables2= (join->const_table_map | |
|
7274 |
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT); |
|
7275 |
for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++) |
|
7276 |
{ |
|
7277 |
current_map= tab->table->map; |
|
7278 |
used_tables2|= current_map; |
|
7279 |
COND *tmp_cond= make_cond_for_table(on_expr, used_tables2, |
|
7280 |
current_map, 0); |
|
7281 |
if (tmp_cond) |
|
7282 |
{ |
|
7283 |
JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab; |
|
7284 |
/* |
|
7285 |
First add the guards for match variables of |
|
7286 |
all embedding outer join operations. |
|
7287 |
*/ |
|
7288 |
if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner, |
|
7289 |
tmp_cond, |
|
7290 |
first_inner_tab))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7291 |
return(1); |
1
by brian
clean slate |
7292 |
/* |
7293 |
Now add the guard turning the predicate off for |
|
7294 |
the null complemented row. |
|
7295 |
*/ |
|
7296 |
tmp_cond= new Item_func_trig_cond(tmp_cond, |
|
7297 |
&first_inner_tab-> |
|
7298 |
not_null_compl); |
|
7299 |
if (tmp_cond) |
|
7300 |
tmp_cond->quick_fix_field(); |
|
7301 |
/* Add the predicate to other pushed down predicates */ |
|
7302 |
cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond : |
|
7303 |
new Item_cond_and(cond_tab->select_cond, |
|
7304 |
tmp_cond); |
|
7305 |
if (!cond_tab->select_cond) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7306 |
return(1); |
1
by brian
clean slate |
7307 |
cond_tab->select_cond->quick_fix_field(); |
7308 |
} |
|
7309 |
} |
|
7310 |
first_inner_tab= first_inner_tab->first_upper; |
|
7311 |
} |
|
7312 |
} |
|
7313 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7314 |
return(0); |
1
by brian
clean slate |
7315 |
} |
7316 |
||
7317 |
||
7318 |
/* |
|
7319 |
Check if given expression uses only table fields covered by the given index |
|
7320 |
||
7321 |
SYNOPSIS |
|
7322 |
uses_index_fields_only() |
|
7323 |
item Expression to check |
|
7324 |
tbl The table having the index |
|
7325 |
keyno The index number |
|
55
by brian
Update for using real bool types. |
7326 |
other_tbls_ok true <=> Fields of other non-const tables are allowed |
1
by brian
clean slate |
7327 |
|
7328 |
DESCRIPTION |
|
7329 |
Check if given expression only uses fields covered by index #keyno in the |
|
7330 |
table tbl. The expression can use any fields in any other tables. |
|
7331 |
||
7332 |
The expression is guaranteed not to be AND or OR - those constructs are |
|
7333 |
handled outside of this function. |
|
7334 |
||
7335 |
RETURN |
|
55
by brian
Update for using real bool types. |
7336 |
true Yes |
7337 |
false No |
|
1
by brian
clean slate |
7338 |
*/ |
7339 |
||
7340 |
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno, |
|
7341 |
bool other_tbls_ok) |
|
7342 |
{ |
|
7343 |
if (item->const_item()) |
|
55
by brian
Update for using real bool types. |
7344 |
return true; |
1
by brian
clean slate |
7345 |
|
7346 |
/* |
|
7347 |
Don't push down the triggered conditions. Nested outer joins execution |
|
7348 |
code may need to evaluate a condition several times (both triggered and |
|
7349 |
untriggered), and there is no way to put thi |
|
7350 |
TODO: Consider cloning the triggered condition and using the copies for: |
|
7351 |
1. push the first copy down, to have most restrictive index condition |
|
7352 |
possible |
|
7353 |
2. Put the second copy into tab->select_cond. |
|
7354 |
*/ |
|
7355 |
if (item->type() == Item::FUNC_ITEM && |
|
7356 |
((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC) |
|
55
by brian
Update for using real bool types. |
7357 |
return false; |
1
by brian
clean slate |
7358 |
|
7359 |
if (!(item->used_tables() & tbl->map)) |
|
7360 |
return other_tbls_ok; |
|
7361 |
||
7362 |
Item::Type item_type= item->type(); |
|
7363 |
switch (item_type) { |
|
7364 |
case Item::FUNC_ITEM: |
|
7365 |
{ |
|
7366 |
/* This is a function, apply condition recursively to arguments */ |
|
7367 |
Item_func *item_func= (Item_func*)item; |
|
7368 |
Item **child; |
|
7369 |
Item **item_end= (item_func->arguments()) + item_func->argument_count(); |
|
7370 |
for (child= item_func->arguments(); child != item_end; child++) |
|
7371 |
{ |
|
7372 |
if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok)) |
|
55
by brian
Update for using real bool types. |
7373 |
return false; |
1
by brian
clean slate |
7374 |
} |
55
by brian
Update for using real bool types. |
7375 |
return true; |
1
by brian
clean slate |
7376 |
} |
7377 |
case Item::COND_ITEM: |
|
7378 |
{ |
|
7379 |
/* This is a function, apply condition recursively to arguments */ |
|
7380 |
List_iterator<Item> li(*((Item_cond*)item)->argument_list()); |
|
7381 |
Item *item; |
|
7382 |
while ((item=li++)) |
|
7383 |
{ |
|
7384 |
if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok)) |
|
55
by brian
Update for using real bool types. |
7385 |
return false; |
1
by brian
clean slate |
7386 |
} |
55
by brian
Update for using real bool types. |
7387 |
return true; |
1
by brian
clean slate |
7388 |
} |
7389 |
case Item::FIELD_ITEM: |
|
7390 |
{ |
|
7391 |
Item_field *item_field= (Item_field*)item; |
|
7392 |
if (item_field->field->table != tbl) |
|
55
by brian
Update for using real bool types. |
7393 |
return true; |
1
by brian
clean slate |
7394 |
return item_field->field->part_of_key.is_set(keyno); |
7395 |
} |
|
7396 |
case Item::REF_ITEM: |
|
7397 |
return uses_index_fields_only(item->real_item(), tbl, keyno, |
|
7398 |
other_tbls_ok); |
|
7399 |
default: |
|
55
by brian
Update for using real bool types. |
7400 |
return false; /* Play it safe, don't push unknown non-const items */ |
1
by brian
clean slate |
7401 |
} |
7402 |
} |
|
7403 |
||
7404 |
||
7405 |
#define ICP_COND_USES_INDEX_ONLY 10 |
|
7406 |
||
7407 |
/* |
|
7408 |
Get a part of the condition that can be checked using only index fields |
|
7409 |
||
7410 |
SYNOPSIS |
|
7411 |
make_cond_for_index() |
|
7412 |
cond The source condition |
|
7413 |
table The table that is partially available |
|
7414 |
keyno The index in the above table. Only fields covered by the index |
|
7415 |
are available |
|
55
by brian
Update for using real bool types. |
7416 |
other_tbls_ok true <=> Fields of other non-const tables are allowed |
1
by brian
clean slate |
7417 |
|
7418 |
DESCRIPTION |
|
7419 |
Get a part of the condition that can be checked when for the given table |
|
7420 |
we have values only of fields covered by some index. The condition may |
|
7421 |
refer to other tables, it is assumed that we have values of all of their |
|
7422 |
fields. |
|
7423 |
||
7424 |
Example: |
|
7425 |
make_cond_for_index( |
|
7426 |
"cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)", |
|
7427 |
t2, keyno(t2.key1)) |
|
7428 |
will return |
|
7429 |
"cond(t1.field) AND cond(t2.key2)" |
|
7430 |
||
7431 |
RETURN |
|
7432 |
Index condition, or NULL if no condition could be inferred. |
|
7433 |
*/ |
|
7434 |
||
7435 |
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno, |
|
7436 |
bool other_tbls_ok) |
|
7437 |
{ |
|
7438 |
if (!cond) |
|
7439 |
return NULL; |
|
7440 |
if (cond->type() == Item::COND_ITEM) |
|
7441 |
{ |
|
7442 |
uint n_marked= 0; |
|
7443 |
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC) |
|
7444 |
{ |
|
7445 |
Item_cond_and *new_cond=new Item_cond_and; |
|
7446 |
if (!new_cond) |
|
7447 |
return (COND*) 0; |
|
7448 |
List_iterator<Item> li(*((Item_cond*) cond)->argument_list()); |
|
7449 |
Item *item; |
|
7450 |
while ((item=li++)) |
|
7451 |
{ |
|
7452 |
Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok); |
|
7453 |
if (fix) |
|
7454 |
new_cond->argument_list()->push_back(fix); |
|
7455 |
n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY); |
|
7456 |
} |
|
7457 |
if (n_marked ==((Item_cond*)cond)->argument_list()->elements) |
|
7458 |
cond->marker= ICP_COND_USES_INDEX_ONLY; |
|
7459 |
switch (new_cond->argument_list()->elements) { |
|
7460 |
case 0: |
|
7461 |
return (COND*) 0; |
|
7462 |
case 1: |
|
7463 |
return new_cond->argument_list()->head(); |
|
7464 |
default: |
|
7465 |
new_cond->quick_fix_field(); |
|
7466 |
return new_cond; |
|
7467 |
} |
|
7468 |
} |
|
7469 |
else /* It's OR */ |
|
7470 |
{ |
|
7471 |
Item_cond_or *new_cond=new Item_cond_or; |
|
7472 |
if (!new_cond) |
|
7473 |
return (COND*) 0; |
|
7474 |
List_iterator<Item> li(*((Item_cond*) cond)->argument_list()); |
|
7475 |
Item *item; |
|
7476 |
while ((item=li++)) |
|
7477 |
{ |
|
7478 |
Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok); |
|
7479 |
if (!fix) |
|
7480 |
return (COND*) 0; |
|
7481 |
new_cond->argument_list()->push_back(fix); |
|
7482 |
n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY); |
|
7483 |
} |
|
7484 |
if (n_marked ==((Item_cond*)cond)->argument_list()->elements) |
|
7485 |
cond->marker= ICP_COND_USES_INDEX_ONLY; |
|
7486 |
new_cond->quick_fix_field(); |
|
7487 |
new_cond->top_level_item(); |
|
7488 |
return new_cond; |
|
7489 |
} |
|
7490 |
} |
|
7491 |
||
7492 |
if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok)) |
|
7493 |
return (COND*) 0; |
|
7494 |
cond->marker= ICP_COND_USES_INDEX_ONLY; |
|
7495 |
return cond; |
|
7496 |
} |
|
7497 |
||
7498 |
||
7499 |
Item *make_cond_remainder(Item *cond, bool exclude_index) |
|
7500 |
{ |
|
7501 |
if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY) |
|
7502 |
return 0; /* Already checked */ |
|
7503 |
||
7504 |
if (cond->type() == Item::COND_ITEM) |
|
7505 |
{ |
|
7506 |
table_map tbl_map= 0; |
|
7507 |
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC) |
|
7508 |
{ |
|
7509 |
/* Create new top level AND item */ |
|
7510 |
Item_cond_and *new_cond=new Item_cond_and; |
|
7511 |
if (!new_cond) |
|
7512 |
return (COND*) 0; |
|
7513 |
List_iterator<Item> li(*((Item_cond*) cond)->argument_list()); |
|
7514 |
Item *item; |
|
7515 |
while ((item=li++)) |
|
7516 |
{ |
|
7517 |
Item *fix= make_cond_remainder(item, exclude_index); |
|
7518 |
if (fix) |
|
7519 |
{ |
|
7520 |
new_cond->argument_list()->push_back(fix); |
|
7521 |
tbl_map |= fix->used_tables(); |
|
7522 |
} |
|
7523 |
} |
|
7524 |
switch (new_cond->argument_list()->elements) { |
|
7525 |
case 0: |
|
7526 |
return (COND*) 0; |
|
7527 |
case 1: |
|
7528 |
return new_cond->argument_list()->head(); |
|
7529 |
default: |
|
7530 |
new_cond->quick_fix_field(); |
|
7531 |
((Item_cond*)new_cond)->used_tables_cache= tbl_map; |
|
7532 |
return new_cond; |
|
7533 |
} |
|
7534 |
} |
|
7535 |
else /* It's OR */ |
|
7536 |
{ |
|
7537 |
Item_cond_or *new_cond=new Item_cond_or; |
|
7538 |
if (!new_cond) |
|
7539 |
return (COND*) 0; |
|
7540 |
List_iterator<Item> li(*((Item_cond*) cond)->argument_list()); |
|
7541 |
Item *item; |
|
7542 |
while ((item=li++)) |
|
7543 |
{ |
|
55
by brian
Update for using real bool types. |
7544 |
Item *fix= make_cond_remainder(item, false); |
1
by brian
clean slate |
7545 |
if (!fix) |
7546 |
return (COND*) 0; |
|
7547 |
new_cond->argument_list()->push_back(fix); |
|
7548 |
tbl_map |= fix->used_tables(); |
|
7549 |
} |
|
7550 |
new_cond->quick_fix_field(); |
|
7551 |
((Item_cond*)new_cond)->used_tables_cache= tbl_map; |
|
7552 |
new_cond->top_level_item(); |
|
7553 |
return new_cond; |
|
7554 |
} |
|
7555 |
} |
|
7556 |
return cond; |
|
7557 |
} |
|
7558 |
||
7559 |
||
7560 |
/* |
|
7561 |
Try to extract and push the index condition |
|
7562 |
||
7563 |
SYNOPSIS |
|
7564 |
push_index_cond() |
|
7565 |
tab A join tab that has tab->table->file and its condition |
|
7566 |
in tab->select_cond |
|
7567 |
keyno Index for which extract and push the condition |
|
55
by brian
Update for using real bool types. |
7568 |
other_tbls_ok true <=> Fields of other non-const tables are allowed |
1
by brian
clean slate |
7569 |
|
7570 |
DESCRIPTION |
|
7571 |
Try to extract and push the index condition down to table handler |
|
7572 |
*/ |
|
7573 |
||
7574 |
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok) |
|
7575 |
{ |
|
7576 |
Item *idx_cond; |
|
7577 |
if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN && |
|
7578 |
tab->join->thd->variables.engine_condition_pushdown) |
|
7579 |
{ |
|
7580 |
idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno, |
|
7581 |
other_tbls_ok); |
|
7582 |
||
7583 |
if (idx_cond) |
|
7584 |
{ |
|
7585 |
tab->pre_idx_push_select_cond= tab->select_cond; |
|
7586 |
Item *idx_remainder_cond= |
|
7587 |
tab->table->file->idx_cond_push(keyno, idx_cond); |
|
7588 |
||
7589 |
/* |
|
7590 |
Disable eq_ref's "lookup cache" if we've pushed down an index |
|
7591 |
condition. |
|
7592 |
TODO: This check happens to work on current ICP implementations, but |
|
7593 |
there may exist a compliant implementation that will not work |
|
7594 |
correctly with it. Sort this out when we stabilize the condition |
|
7595 |
pushdown APIs. |
|
7596 |
*/ |
|
7597 |
if (idx_remainder_cond != idx_cond) |
|
55
by brian
Update for using real bool types. |
7598 |
tab->ref.disable_cache= true; |
1
by brian
clean slate |
7599 |
|
55
by brian
Update for using real bool types. |
7600 |
Item *row_cond= make_cond_remainder(tab->select_cond, true); |
1
by brian
clean slate |
7601 |
|
7602 |
if (row_cond) |
|
7603 |
{ |
|
7604 |
if (!idx_remainder_cond) |
|
7605 |
tab->select_cond= row_cond; |
|
7606 |
else |
|
7607 |
{ |
|
7608 |
tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond); |
|
7609 |
tab->select_cond->quick_fix_field(); |
|
7610 |
((Item_cond_and*)tab->select_cond)->used_tables_cache= |
|
7611 |
row_cond->used_tables() | idx_remainder_cond->used_tables(); |
|
7612 |
} |
|
7613 |
} |
|
7614 |
else |
|
7615 |
tab->select_cond= idx_remainder_cond; |
|
7616 |
if (tab->select) |
|
7617 |
{ |
|
7618 |
tab->select->cond= tab->select_cond; |
|
7619 |
} |
|
7620 |
} |
|
7621 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7622 |
return; |
1
by brian
clean slate |
7623 |
} |
7624 |
||
7625 |
||
7626 |
||
7627 |
/* |
|
7628 |
Determine if the set is already ordered for ORDER BY, so it can |
|
7629 |
disable join cache because it will change the ordering of the results. |
|
7630 |
Code handles sort table that is at any location (not only first after |
|
7631 |
the const tables) despite the fact that it's currently prohibited. |
|
7632 |
We must disable join cache if the first non-const table alone is |
|
7633 |
ordered. If there is a temp table the ordering is done as a last |
|
7634 |
operation and doesn't prevent join cache usage. |
|
7635 |
*/ |
|
7636 |
uint make_join_orderinfo(JOIN *join) |
|
7637 |
{ |
|
7638 |
uint i; |
|
7639 |
if (join->need_tmp) |
|
7640 |
return join->tables; |
|
7641 |
||
7642 |
for (i=join->const_tables ; i < join->tables ; i++) |
|
7643 |
{ |
|
7644 |
JOIN_TAB *tab=join->join_tab+i; |
|
7645 |
TABLE *table=tab->table; |
|
7646 |
if ((table == join->sort_by_table && |
|
7647 |
(!join->order || join->skip_sort_order)) || |
|
7648 |
(join->sort_by_table == (TABLE *) 1 && i != join->const_tables)) |
|
7649 |
{ |
|
7650 |
break; |
|
7651 |
} |
|
7652 |
} |
|
7653 |
return i; |
|
7654 |
} |
|
7655 |
||
7656 |
||
7657 |
/* |
|
7658 |
Plan refinement stage: do various set ups for the executioner |
|
7659 |
||
7660 |
SYNOPSIS |
|
7661 |
make_join_readinfo() |
|
7662 |
join Join being processed |
|
7663 |
options Join's options (checking for SELECT_DESCRIBE, |
|
7664 |
SELECT_NO_JOIN_CACHE) |
|
7665 |
no_jbuf_after Don't use join buffering after table with this number. |
|
7666 |
||
7667 |
DESCRIPTION |
|
7668 |
Plan refinement stage: do various set ups for the executioner |
|
7669 |
- set up use of join buffering |
|
7670 |
- push index conditions |
|
7671 |
- increment counters |
|
7672 |
- etc |
|
7673 |
||
7674 |
RETURN |
|
55
by brian
Update for using real bool types. |
7675 |
false - OK |
7676 |
true - Out of memory |
|
1
by brian
clean slate |
7677 |
*/ |
7678 |
||
7679 |
static bool |
|
151
by Brian Aker
Ulonglong to uint64_t |
7680 |
make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after) |
1
by brian
clean slate |
7681 |
{ |
7682 |
uint i; |
|
7683 |
bool statistics= test(!(join->select_options & SELECT_DESCRIBE)); |
|
7684 |
bool sorted= 1; |
|
7685 |
||
7686 |
for (i=join->const_tables ; i < join->tables ; i++) |
|
7687 |
{ |
|
7688 |
JOIN_TAB *tab=join->join_tab+i; |
|
7689 |
TABLE *table=tab->table; |
|
7690 |
bool using_join_cache; |
|
7691 |
tab->read_record.table= table; |
|
7692 |
tab->read_record.file=table->file; |
|
7693 |
tab->next_select=sub_select; /* normal select */ |
|
7694 |
/* |
|
7695 |
TODO: don't always instruct first table's ref/range access method to |
|
7696 |
produce sorted output. |
|
7697 |
*/ |
|
7698 |
tab->sorted= sorted; |
|
7699 |
sorted= 0; // only first must be sorted |
|
7700 |
if (tab->insideout_match_tab) |
|
7701 |
{ |
|
7702 |
if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info |
|
7703 |
[tab->index]. |
|
7704 |
key_length))) |
|
55
by brian
Update for using real bool types. |
7705 |
return true; |
1
by brian
clean slate |
7706 |
} |
7707 |
switch (tab->type) { |
|
7708 |
case JT_SYSTEM: // Only happens with left join |
|
7709 |
table->status=STATUS_NO_RECORD; |
|
7710 |
tab->read_first_record= join_read_system; |
|
7711 |
tab->read_record.read_record= join_no_more_records; |
|
7712 |
break; |
|
7713 |
case JT_CONST: // Only happens with left join |
|
7714 |
table->status=STATUS_NO_RECORD; |
|
7715 |
tab->read_first_record= join_read_const; |
|
7716 |
tab->read_record.read_record= join_no_more_records; |
|
7717 |
if (table->covering_keys.is_set(tab->ref.key) && |
|
7718 |
!table->no_keyread) |
|
7719 |
{ |
|
7720 |
table->key_read=1; |
|
7721 |
table->file->extra(HA_EXTRA_KEYREAD); |
|
7722 |
} |
|
7723 |
break; |
|
7724 |
case JT_EQ_REF: |
|
7725 |
table->status=STATUS_NO_RECORD; |
|
7726 |
if (tab->select) |
|
7727 |
{ |
|
7728 |
delete tab->select->quick; |
|
7729 |
tab->select->quick=0; |
|
7730 |
} |
|
7731 |
delete tab->quick; |
|
7732 |
tab->quick=0; |
|
7733 |
tab->read_first_record= join_read_key; |
|
7734 |
tab->read_record.read_record= join_no_more_records; |
|
7735 |
if (table->covering_keys.is_set(tab->ref.key) && |
|
7736 |
!table->no_keyread) |
|
7737 |
{ |
|
7738 |
table->key_read=1; |
|
7739 |
table->file->extra(HA_EXTRA_KEYREAD); |
|
7740 |
} |
|
7741 |
else |
|
55
by brian
Update for using real bool types. |
7742 |
push_index_cond(tab, tab->ref.key, true); |
1
by brian
clean slate |
7743 |
break; |
7744 |
case JT_REF_OR_NULL: |
|
7745 |
case JT_REF: |
|
7746 |
table->status=STATUS_NO_RECORD; |
|
7747 |
if (tab->select) |
|
7748 |
{ |
|
7749 |
delete tab->select->quick; |
|
7750 |
tab->select->quick=0; |
|
7751 |
} |
|
7752 |
delete tab->quick; |
|
7753 |
tab->quick=0; |
|
7754 |
if (table->covering_keys.is_set(tab->ref.key) && |
|
7755 |
!table->no_keyread) |
|
7756 |
{ |
|
7757 |
table->key_read=1; |
|
7758 |
table->file->extra(HA_EXTRA_KEYREAD); |
|
7759 |
} |
|
7760 |
else |
|
55
by brian
Update for using real bool types. |
7761 |
push_index_cond(tab, tab->ref.key, true); |
1
by brian
clean slate |
7762 |
if (tab->type == JT_REF) |
7763 |
{ |
|
7764 |
tab->read_first_record= join_read_always_key; |
|
7765 |
tab->read_record.read_record= tab->insideout_match_tab? |
|
7766 |
join_read_next_same_diff : join_read_next_same; |
|
7767 |
} |
|
7768 |
else |
|
7769 |
{ |
|
7770 |
tab->read_first_record= join_read_always_key_or_null; |
|
7771 |
tab->read_record.read_record= join_read_next_same_or_null; |
|
7772 |
} |
|
7773 |
break; |
|
7774 |
case JT_ALL: |
|
7775 |
/* |
|
7776 |
If previous table use cache |
|
7777 |
If the incoming data set is already sorted don't use cache. |
|
7778 |
*/ |
|
7779 |
table->status=STATUS_NO_RECORD; |
|
55
by brian
Update for using real bool types. |
7780 |
using_join_cache= false; |
1
by brian
clean slate |
7781 |
if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) && |
7782 |
tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after && |
|
7783 |
!tab->insideout_match_tab) |
|
7784 |
{ |
|
7785 |
if ((options & SELECT_DESCRIBE) || |
|
7786 |
!join_init_cache(join->thd,join->join_tab+join->const_tables, |
|
7787 |
i-join->const_tables)) |
|
7788 |
{ |
|
55
by brian
Update for using real bool types. |
7789 |
using_join_cache= true; |
1
by brian
clean slate |
7790 |
tab[-1].next_select=sub_select_cache; /* Patch previous */ |
7791 |
} |
|
7792 |
} |
|
7793 |
/* These init changes read_record */ |
|
7794 |
if (tab->use_quick == 2) |
|
7795 |
{ |
|
7796 |
join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED; |
|
7797 |
tab->read_first_record= join_init_quick_read_record; |
|
7798 |
if (statistics) |
|
7799 |
status_var_increment(join->thd->status_var.select_range_check_count); |
|
7800 |
} |
|
7801 |
else |
|
7802 |
{ |
|
7803 |
tab->read_first_record= join_init_read_record; |
|
7804 |
if (i == join->const_tables) |
|
7805 |
{ |
|
7806 |
if (tab->select && tab->select->quick) |
|
7807 |
{ |
|
7808 |
if (statistics) |
|
7809 |
status_var_increment(join->thd->status_var.select_range_count); |
|
7810 |
} |
|
7811 |
else |
|
7812 |
{ |
|
7813 |
join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED; |
|
7814 |
if (statistics) |
|
7815 |
status_var_increment(join->thd->status_var.select_scan_count); |
|
7816 |
} |
|
7817 |
} |
|
7818 |
else |
|
7819 |
{ |
|
7820 |
if (tab->select && tab->select->quick) |
|
7821 |
{ |
|
7822 |
if (statistics) |
|
7823 |
status_var_increment(join->thd->status_var.select_full_range_join_count); |
|
7824 |
} |
|
7825 |
else |
|
7826 |
{ |
|
7827 |
join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED; |
|
7828 |
if (statistics) |
|
7829 |
status_var_increment(join->thd->status_var.select_full_join_count); |
|
7830 |
} |
|
7831 |
} |
|
7832 |
if (!table->no_keyread) |
|
7833 |
{ |
|
7834 |
if (tab->select && tab->select->quick && |
|
7835 |
tab->select->quick->index != MAX_KEY && //not index_merge |
|
7836 |
table->covering_keys.is_set(tab->select->quick->index)) |
|
7837 |
{ |
|
7838 |
table->key_read=1; |
|
7839 |
table->file->extra(HA_EXTRA_KEYREAD); |
|
7840 |
} |
|
7841 |
else if (!table->covering_keys.is_clear_all() && |
|
7842 |
!(tab->select && tab->select->quick)) |
|
7843 |
{ // Only read index tree |
|
7844 |
if (!tab->insideout_match_tab) |
|
7845 |
{ |
|
7846 |
/* |
|
7847 |
See bug #26447: "Using the clustered index for a table scan |
|
7848 |
is always faster than using a secondary index". |
|
7849 |
*/ |
|
7850 |
if (table->s->primary_key != MAX_KEY && |
|
7851 |
table->file->primary_key_is_clustered()) |
|
7852 |
tab->index= table->s->primary_key; |
|
7853 |
else |
|
7854 |
tab->index=find_shortest_key(table, & table->covering_keys); |
|
7855 |
} |
|
7856 |
tab->read_first_record= join_read_first; |
|
7857 |
tab->type=JT_NEXT; // Read with index_first / index_next |
|
7858 |
} |
|
7859 |
} |
|
7860 |
if (tab->select && tab->select->quick && |
|
7861 |
tab->select->quick->index != MAX_KEY && ! tab->table->key_read) |
|
7862 |
push_index_cond(tab, tab->select->quick->index, !using_join_cache); |
|
7863 |
} |
|
7864 |
break; |
|
7865 |
default: |
|
7866 |
break; /* purecov: deadcode */ |
|
7867 |
case JT_UNKNOWN: |
|
7868 |
case JT_MAYBE_REF: |
|
7869 |
abort(); /* purecov: deadcode */ |
|
7870 |
} |
|
7871 |
} |
|
7872 |
join->join_tab[join->tables-1].next_select=0; /* Set by do_select */ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
7873 |
return(false); |
1
by brian
clean slate |
7874 |
} |
7875 |
||
7876 |
||
7877 |
/** |
|
7878 |
Give error if we some tables are done with a full join. |
|
7879 |
||
7880 |
This is used by multi_table_update and multi_table_delete when running |
|
7881 |
in safe mode. |
|
7882 |
||
7883 |
@param join Join condition |
|
7884 |
||
7885 |
@retval |
|
7886 |
0 ok |
|
7887 |
@retval |
|
7888 |
1 Error (full join used) |
|
7889 |
*/ |
|
7890 |
||
7891 |
bool error_if_full_join(JOIN *join) |
|
7892 |
{ |
|
7893 |
for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables; |
|
7894 |
tab < end; |
|
7895 |
tab++) |
|
7896 |
{ |
|
7897 |
if (tab->type == JT_ALL && (!tab->select || !tab->select->quick)) |
|
7898 |
{ |
|
7899 |
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE, |
|
7900 |
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0)); |
|
7901 |
return(1); |
|
7902 |
} |
|
7903 |
} |
|
7904 |
return(0); |
|
7905 |
} |
|
7906 |
||
7907 |
||
7908 |
/** |
|
7909 |
cleanup JOIN_TAB. |
|
7910 |
*/ |
|
7911 |
||
7912 |
void JOIN_TAB::cleanup() |
|
7913 |
{ |
|
7914 |
delete select; |
|
7915 |
select= 0; |
|
7916 |
delete quick; |
|
7917 |
quick= 0; |
|
7918 |
x_free(cache.buff); |
|
7919 |
cache.buff= 0; |
|
7920 |
limit= 0; |
|
7921 |
if (table) |
|
7922 |
{ |
|
7923 |
if (table->key_read) |
|
7924 |
{ |
|
7925 |
table->key_read= 0; |
|
7926 |
table->file->extra(HA_EXTRA_NO_KEYREAD); |
|
7927 |
} |
|
7928 |
table->file->ha_index_or_rnd_end(); |
|
7929 |
/* |
|
7930 |
We need to reset this for next select |
|
7931 |
(Tested in part_of_refkey) |
|
7932 |
*/ |
|
7933 |
table->reginfo.join_tab= 0; |
|
7934 |
} |
|
7935 |
end_read_record(&read_record); |
|
7936 |
} |
|
7937 |
||
7938 |
||
7939 |
/** |
|
7940 |
Partially cleanup JOIN after it has executed: close index or rnd read |
|
7941 |
(table cursors), free quick selects. |
|
7942 |
||
7943 |
This function is called in the end of execution of a JOIN, before the used |
|
7944 |
tables are unlocked and closed. |
|
7945 |
||
7946 |
For a join that is resolved using a temporary table, the first sweep is |
|
7947 |
performed against actual tables and an intermediate result is inserted |
|
7948 |
into the temprorary table. |
|
7949 |
The last sweep is performed against the temporary table. Therefore, |
|
7950 |
the base tables and associated buffers used to fill the temporary table |
|
7951 |
are no longer needed, and this function is called to free them. |
|
7952 |
||
7953 |
For a join that is performed without a temporary table, this function |
|
7954 |
is called after all rows are sent, but before EOF packet is sent. |
|
7955 |
||
7956 |
For a simple SELECT with no subqueries this function performs a full |
|
7957 |
cleanup of the JOIN and calls mysql_unlock_read_tables to free used base |
|
7958 |
tables. |
|
7959 |
||
7960 |
If a JOIN is executed for a subquery or if it has a subquery, we can't |
|
7961 |
do the full cleanup and need to do a partial cleanup only. |
|
7962 |
- If a JOIN is not the top level join, we must not unlock the tables |
|
7963 |
because the outer select may not have been evaluated yet, and we |
|
7964 |
can't unlock only selected tables of a query. |
|
7965 |
- Additionally, if this JOIN corresponds to a correlated subquery, we |
|
7966 |
should not free quick selects and join buffers because they will be |
|
7967 |
needed for the next execution of the correlated subquery. |
|
7968 |
- However, if this is a JOIN for a [sub]select, which is not |
|
7969 |
a correlated subquery itself, but has subqueries, we can free it |
|
7970 |
fully and also free JOINs of all its subqueries. The exception |
|
7971 |
is a subquery in SELECT list, e.g: @n |
|
7972 |
SELECT a, (select max(b) from t1) group by c @n |
|
7973 |
This subquery will not be evaluated at first sweep and its value will |
|
7974 |
not be inserted into the temporary table. Instead, it's evaluated |
|
7975 |
when selecting from the temporary table. Therefore, it can't be freed |
|
7976 |
here even though it's not correlated. |
|
7977 |
||
7978 |
@todo |
|
7979 |
Unlock tables even if the join isn't top level select in the tree |
|
7980 |
*/ |
|
7981 |
||
7982 |
void JOIN::join_free() |
|
7983 |
{ |
|
7984 |
SELECT_LEX_UNIT *tmp_unit; |
|
7985 |
SELECT_LEX *sl; |
|
7986 |
/* |
|
7987 |
Optimization: if not EXPLAIN and we are done with the JOIN, |
|
7988 |
free all tables. |
|
7989 |
*/ |
|
7990 |
bool full= (!select_lex->uncacheable && !thd->lex->describe); |
|
7991 |
bool can_unlock= full; |
|
7992 |
||
7993 |
cleanup(full); |
|
7994 |
||
7995 |
for (tmp_unit= select_lex->first_inner_unit(); |
|
7996 |
tmp_unit; |
|
7997 |
tmp_unit= tmp_unit->next_unit()) |
|
7998 |
for (sl= tmp_unit->first_select(); sl; sl= sl->next_select()) |
|
7999 |
{ |
|
8000 |
Item_subselect *subselect= sl->master_unit()->item; |
|
8001 |
bool full_local= full && (!subselect || subselect->is_evaluated()); |
|
8002 |
/* |
|
8003 |
If this join is evaluated, we can fully clean it up and clean up all |
|
8004 |
its underlying joins even if they are correlated -- they will not be |
|
8005 |
used any more anyway. |
|
8006 |
If this join is not yet evaluated, we still must clean it up to |
|
8007 |
close its table cursors -- it may never get evaluated, as in case of |
|
55
by brian
Update for using real bool types. |
8008 |
... HAVING false OR a IN (SELECT ...)) |
1
by brian
clean slate |
8009 |
but all table cursors must be closed before the unlock. |
8010 |
*/ |
|
8011 |
sl->cleanup_all_joins(full_local); |
|
8012 |
/* Can't unlock if at least one JOIN is still needed */ |
|
8013 |
can_unlock= can_unlock && full_local; |
|
8014 |
} |
|
8015 |
||
8016 |
/* |
|
8017 |
We are not using tables anymore |
|
8018 |
Unlock all tables. We may be in an INSERT .... SELECT statement. |
|
8019 |
*/ |
|
8020 |
if (can_unlock && lock && thd->lock && |
|
8021 |
!(select_options & SELECT_NO_UNLOCK) && |
|
8022 |
!select_lex->subquery_in_having && |
|
8023 |
(select_lex == (thd->lex->unit.fake_select_lex ? |
|
8024 |
thd->lex->unit.fake_select_lex : &thd->lex->select_lex))) |
|
8025 |
{ |
|
8026 |
/* |
|
8027 |
TODO: unlock tables even if the join isn't top level select in the |
|
8028 |
tree. |
|
8029 |
*/ |
|
8030 |
mysql_unlock_read_tables(thd, lock); // Don't free join->lock |
|
8031 |
lock= 0; |
|
8032 |
} |
|
8033 |
||
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
8034 |
return; |
1
by brian
clean slate |
8035 |
} |
8036 |
||
8037 |
||
8038 |
/** |
|
8039 |
Free resources of given join. |
|
8040 |
||
8041 |
@param fill true if we should free all resources, call with full==1 |
|
8042 |
should be last, before it this function can be called with |
|
8043 |
full==0 |
|
8044 |
||
8045 |
@note |
|
8046 |
With subquery this function definitely will be called several times, |
|
8047 |
but even for simple query it can be called several times. |
|
8048 |
*/ |
|
8049 |
||
8050 |
void JOIN::cleanup(bool full) |
|
8051 |
{ |
|
8052 |
if (table) |
|
8053 |
{ |
|
8054 |
JOIN_TAB *tab,*end; |
|
8055 |
/* |
|
8056 |
Only a sorted table may be cached. This sorted table is always the |
|
8057 |
first non const table in join->table |
|
8058 |
*/ |
|
8059 |
if (tables > const_tables) // Test for not-const tables |
|
8060 |
{ |
|
8061 |
free_io_cache(table[const_tables]); |
|
8062 |
filesort_free_buffers(table[const_tables],full); |
|
8063 |
} |
|
8064 |
||
8065 |
if (full) |
|
8066 |
{ |
|
8067 |
for (tab= join_tab, end= tab+tables; tab != end; tab++) |
|
8068 |
tab->cleanup(); |
|
8069 |
table= 0; |
|
8070 |
} |
|
8071 |
else |
|
8072 |
{ |
|
8073 |
for (tab= join_tab, end= tab+tables; tab != end; tab++) |
|
8074 |
{ |
|
8075 |
if (tab->table) |
|
8076 |
tab->table->file->ha_index_or_rnd_end(); |
|
8077 |
} |
|
8078 |
} |
|
8079 |
cleanup_sj_tmp_tables(this);// |
|
8080 |
} |
|
8081 |
/* |
|
8082 |
We are not using tables anymore |
|
8083 |
Unlock all tables. We may be in an INSERT .... SELECT statement. |
|
8084 |
*/ |
|
8085 |
if (full) |
|
8086 |
{ |
|
8087 |
if (tmp_join) |
|
8088 |
tmp_table_param.copy_field= 0; |
|
8089 |
group_fields.delete_elements(); |
|
8090 |
/* |
|
8091 |
We can't call delete_elements() on copy_funcs as this will cause |
|
8092 |
problems in free_elements() as some of the elements are then deleted. |
|
8093 |
*/ |
|
8094 |
tmp_table_param.copy_funcs.empty(); |
|
8095 |
/* |
|
8096 |
If we have tmp_join and 'this' JOIN is not tmp_join and |
|
8097 |
tmp_table_param.copy_field's of them are equal then we have to remove |
|
8098 |
pointer to tmp_table_param.copy_field from tmp_join, because it qill |
|
8099 |
be removed in tmp_table_param.cleanup(). |
|
8100 |
*/ |
|
8101 |
if (tmp_join && |
|
8102 |
tmp_join != this && |
|
8103 |
tmp_join->tmp_table_param.copy_field == |
|
8104 |
tmp_table_param.copy_field) |
|
8105 |
{ |
|
8106 |
tmp_join->tmp_table_param.copy_field= |
|
8107 |
tmp_join->tmp_table_param.save_copy_field= 0; |
|
8108 |
} |
|
8109 |
tmp_table_param.cleanup(); |
|
8110 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
8111 |
return; |
1
by brian
clean slate |
8112 |
} |
8113 |
||
8114 |
||
8115 |
/** |
|
8116 |
Remove the following expressions from ORDER BY and GROUP BY: |
|
8117 |
Constant expressions @n |
|
8118 |
Expression that only uses tables that are of type EQ_REF and the reference |
|
8119 |
is in the ORDER list or if all refereed tables are of the above type. |
|
8120 |
||
8121 |
In the following, the X field can be removed: |
|
8122 |
@code |
|
8123 |
SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X |
|
8124 |
SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X |
|
8125 |
@endcode |
|
8126 |
||
8127 |
These can't be optimized: |
|
8128 |
@code |
|
8129 |
SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a |
|
8130 |
SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c |
|
8131 |
SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a |
|
8132 |
@endcode |
|
8133 |
*/ |
|
8134 |
||
8135 |
static bool |
|
8136 |
eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab) |
|
8137 |
{ |
|
8138 |
if (tab->cached_eq_ref_table) // If cached |
|
8139 |
return tab->eq_ref_table; |
|
8140 |
tab->cached_eq_ref_table=1; |
|
8141 |
/* We can skip const tables only if not an outer table */ |
|
8142 |
if (tab->type == JT_CONST && !tab->first_inner) |
|
8143 |
return (tab->eq_ref_table=1); /* purecov: inspected */ |
|
8144 |
if (tab->type != JT_EQ_REF || tab->table->maybe_null) |
|
8145 |
return (tab->eq_ref_table=0); // We must use this |
|
8146 |
Item **ref_item=tab->ref.items; |
|
8147 |
Item **end=ref_item+tab->ref.key_parts; |
|
8148 |
uint found=0; |
|
8149 |
table_map map=tab->table->map; |
|
8150 |
||
8151 |
for (; ref_item != end ; ref_item++) |
|
8152 |
{ |
|
8153 |
if (! (*ref_item)->const_item()) |
|
8154 |
{ // Not a const ref |
|
8155 |
ORDER *order; |
|
8156 |
for (order=start_order ; order ; order=order->next) |
|
8157 |
{ |
|
8158 |
if ((*ref_item)->eq(order->item[0],0)) |
|
8159 |
break; |
|
8160 |
} |
|
8161 |
if (order) |
|
8162 |
{ |
|
8163 |
found++; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
8164 |
assert(!(order->used & map)); |
1
by brian
clean slate |
8165 |
order->used|=map; |
8166 |
continue; // Used in ORDER BY |
|
8167 |
} |
|
8168 |
if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables())) |
|
8169 |
return (tab->eq_ref_table=0); |
|
8170 |
} |
|
8171 |
} |
|
8172 |
/* Check that there was no reference to table before sort order */ |
|
8173 |
for (; found && start_order ; start_order=start_order->next) |
|
8174 |
{ |
|
8175 |
if (start_order->used & map) |
|
8176 |
{ |
|
8177 |
found--; |
|
8178 |
continue; |
|
8179 |
} |
|
8180 |
if (start_order->depend_map & map) |
|
8181 |
return (tab->eq_ref_table=0); |
|
8182 |
} |
|
8183 |
return tab->eq_ref_table=1; |
|
8184 |
} |
|
8185 |
||
8186 |
||
8187 |
static bool |
|
8188 |
only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables) |
|
8189 |
{ |
|
8190 |
if (specialflag & SPECIAL_SAFE_MODE) |
|
8191 |
return 0; // skip this optimize /* purecov: inspected */ |
|
8192 |
for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1) |
|
8193 |
{ |
|
8194 |
if (tables & 1 && !eq_ref_table(join, order, *tab)) |
|
8195 |
return 0; |
|
8196 |
} |
|
8197 |
return 1; |
|
8198 |
} |
|
8199 |
||
8200 |
||
8201 |
/** Update the dependency map for the tables. */ |
|
8202 |
||
8203 |
static void update_depend_map(JOIN *join) |
|
8204 |
{ |
|
8205 |
JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables; |
|
8206 |
||
8207 |
for (; join_tab != end ; join_tab++) |
|
8208 |
{ |
|
8209 |
TABLE_REF *ref= &join_tab->ref; |
|
8210 |
table_map depend_map=0; |
|
8211 |
Item **item=ref->items; |
|
8212 |
uint i; |
|
8213 |
for (i=0 ; i < ref->key_parts ; i++,item++) |
|
8214 |
depend_map|=(*item)->used_tables(); |
|
8215 |
ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT; |
|
8216 |
depend_map&= ~OUTER_REF_TABLE_BIT; |
|
8217 |
for (JOIN_TAB **tab=join->map2table; |
|
8218 |
depend_map ; |
|
8219 |
tab++,depend_map>>=1 ) |
|
8220 |
{ |
|
8221 |
if (depend_map & 1) |
|
8222 |
ref->depend_map|=(*tab)->ref.depend_map; |
|
8223 |
} |
|
8224 |
} |
|
8225 |
} |
|
8226 |
||
8227 |
||
8228 |
/** Update the dependency map for the sort order. */ |
|
8229 |
||
8230 |
static void update_depend_map(JOIN *join, ORDER *order) |
|
8231 |
{ |
|
8232 |
for (; order ; order=order->next) |
|
8233 |
{ |
|
8234 |
table_map depend_map; |
|
8235 |
order->item[0]->update_used_tables(); |
|
8236 |
order->depend_map=depend_map=order->item[0]->used_tables(); |
|
8237 |
// Not item_sum(), RAND() and no reference to table outside of sub select |
|
8238 |
if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT)) |
|
8239 |
&& !order->item[0]->with_sum_func) |
|
8240 |
{ |
|
8241 |
for (JOIN_TAB **tab=join->map2table; |
|
8242 |
depend_map ; |
|
8243 |
tab++, depend_map>>=1) |
|
8244 |
{ |
|
8245 |
if (depend_map & 1) |
|
8246 |
order->depend_map|=(*tab)->ref.depend_map; |
|
8247 |
} |
|
8248 |
} |
|
8249 |
} |
|
8250 |
} |
|
8251 |
||
8252 |
||
8253 |
/** |
|
8254 |
Remove all constants and check if ORDER only contains simple |
|
8255 |
expressions. |
|
8256 |
||
8257 |
simple_order is set to 1 if sort_order only uses fields from head table |
|
8258 |
and the head table is not a LEFT JOIN table. |
|
8259 |
||
8260 |
@param join Join handler |
|
8261 |
@param first_order List of SORT or GROUP order |
|
8262 |
@param cond WHERE statement |
|
8263 |
@param change_list Set to 1 if we should remove things from list. |
|
8264 |
If this is not set, then only simple_order is |
|
8265 |
calculated. |
|
8266 |
@param simple_order Set to 1 if we are only using simple expressions |
|
8267 |
||
8268 |
@return |
|
8269 |
Returns new sort order |
|
8270 |
*/ |
|
8271 |
||
8272 |
static ORDER * |
|
8273 |
remove_const(JOIN *join,ORDER *first_order, COND *cond, |
|
8274 |
bool change_list, bool *simple_order) |
|
8275 |
{ |
|
8276 |
if (join->tables == join->const_tables) |
|
8277 |
return change_list ? 0 : first_order; // No need to sort |
|
8278 |
||
8279 |
ORDER *order,**prev_ptr; |
|
8280 |
table_map first_table= join->join_tab[join->const_tables].table->map; |
|
8281 |
table_map not_const_tables= ~join->const_table_map; |
|
8282 |
table_map ref; |
|
8283 |
||
8284 |
prev_ptr= &first_order; |
|
8285 |
*simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1; |
|
8286 |
||
8287 |
/* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */ |
|
8288 |
||
8289 |
update_depend_map(join, first_order); |
|
8290 |
for (order=first_order; order ; order=order->next) |
|
8291 |
{ |
|
8292 |
table_map order_tables=order->item[0]->used_tables(); |
|
8293 |
if (order->item[0]->with_sum_func) |
|
8294 |
*simple_order=0; // Must do a temp table to sort |
|
8295 |
else if (!(order_tables & not_const_tables)) |
|
8296 |
{ |
|
8297 |
if (order->item[0]->with_subselect) |
|
8298 |
order->item[0]->val_str(&order->item[0]->str_value); |
|
8299 |
continue; // skip const item |
|
8300 |
} |
|
8301 |
else |
|
8302 |
{ |
|
8303 |
if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)) |
|
8304 |
*simple_order=0; |
|
8305 |
else |
|
8306 |
{ |
|
8307 |
Item *comp_item=0; |
|
8308 |
if (cond && const_expression_in_where(cond,order->item[0], &comp_item)) |
|
8309 |
{ |
|
8310 |
continue; |
|
8311 |
} |
|
8312 |
if ((ref=order_tables & (not_const_tables ^ first_table))) |
|
8313 |
{ |
|
8314 |
if (!(order_tables & first_table) && |
|
8315 |
only_eq_ref_tables(join,first_order, ref)) |
|
8316 |
{ |
|
8317 |
continue; |
|
8318 |
} |
|
8319 |
*simple_order=0; // Must do a temp table to sort |
|
8320 |
} |
|
8321 |
} |
|
8322 |
} |
|
8323 |
if (change_list) |
|
8324 |
*prev_ptr= order; // use this entry |
|
8325 |
prev_ptr= &order->next; |
|
8326 |
} |
|
8327 |
if (change_list) |
|
8328 |
*prev_ptr=0; |
|
8329 |
if (prev_ptr == &first_order) // Nothing to sort/group |
|
8330 |
*simple_order=1; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
8331 |
return(first_order); |
1
by brian
clean slate |
8332 |
} |
8333 |
||
8334 |
||
8335 |
static int |
|
8336 |
return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables, |
|
151
by Brian Aker
Ulonglong to uint64_t |
8337 |
List<Item> &fields, bool send_row, uint64_t select_options, |
1
by brian
clean slate |
8338 |
const char *info, Item *having) |
8339 |
{ |
|
8340 |
if (select_options & SELECT_DESCRIBE) |
|
8341 |
{ |
|
55
by brian
Update for using real bool types. |
8342 |
select_describe(join, false, false, false, info); |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
8343 |
return(0); |
1
by brian
clean slate |
8344 |
} |
8345 |
||
8346 |
join->join_free(); |
|
8347 |
||
8348 |
if (send_row) |
|
8349 |
{ |
|
8350 |
for (TABLE_LIST *table= tables; table; table= table->next_leaf) |
|
8351 |
mark_as_null_row(table->table); // All fields are NULL |
|
8352 |
if (having && having->val_int() == 0) |
|
8353 |
send_row=0; |
|
8354 |
} |
|
8355 |
if (!(result->send_fields(fields, |
|
8356 |
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))) |
|
8357 |
{ |
|
8358 |
if (send_row) |
|
8359 |
{ |
|
8360 |
List_iterator_fast<Item> it(fields); |
|
8361 |
Item *item; |
|
8362 |
while ((item= it++)) |
|
8363 |
item->no_rows_in_result(); |
|
8364 |
result->send_data(fields); |
|
8365 |
} |
|
8366 |
result->send_eof(); // Should be safe |
|
8367 |
} |
|
8368 |
/* Update results for FOUND_ROWS */ |
|
8369 |
join->thd->limit_found_rows= join->thd->examined_row_count= 0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
8370 |
return(0); |
1
by brian
clean slate |
8371 |
} |
8372 |
||
8373 |
/* |
|
8374 |
used only in JOIN::clear |
|
8375 |
*/ |
|
8376 |
static void clear_tables(JOIN *join) |
|
8377 |
{ |
|
8378 |
/* |
|
8379 |
must clear only the non-const tables, as const tables |
|
8380 |
are not re-calculated. |
|
8381 |
*/ |
|
8382 |
for (uint i=join->const_tables ; i < join->tables ; i++) |
|
8383 |
mark_as_null_row(join->table[i]); // All fields are NULL |
|
8384 |
} |
|
8385 |
||
8386 |
/***************************************************************************** |
|
8387 |
Make som simple condition optimization: |
|
8388 |
If there is a test 'field = const' change all refs to 'field' to 'const' |
|
8389 |
Remove all dummy tests 'item = item', 'const op const'. |
|
8390 |
Remove all 'item is NULL', when item can never be null! |
|
8391 |
item->marker should be 0 for all items on entry |
|
55
by brian
Update for using real bool types. |
8392 |
Return in cond_value false if condition is impossible (1 = 2) |
1
by brian
clean slate |
8393 |
*****************************************************************************/ |
8394 |
||
8395 |
class COND_CMP :public ilink { |
|
8396 |
public: |
|
8397 |
static void *operator new(size_t size) |
|
8398 |
{ |
|
8399 |
return (void*) sql_alloc((uint) size); |
|
8400 |
} |
|
8401 |
static void operator delete(void *ptr __attribute__((unused)), |
|
8402 |
size_t size __attribute__((unused))) |
|
8403 |
{ TRASH(ptr, size); } |
|
8404 |
||
8405 |
Item *and_level; |
|
8406 |
Item_func *cmp_func; |
|
8407 |
COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {} |
|
8408 |
}; |
|
8409 |
||
8410 |
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION |
|
8411 |
template class I_List<COND_CMP>; |
|
8412 |
template class I_List_iterator<COND_CMP>; |
|
8413 |
#endif |
|
8414 |
||
8415 |
||
8416 |
/** |
|
8417 |
Find the multiple equality predicate containing a field. |
|
8418 |
||
8419 |
The function retrieves the multiple equalities accessed through |
|
8420 |
the con_equal structure from current level and up looking for |
|
8421 |
an equality containing field. It stops retrieval as soon as the equality |
|
55
by brian
Update for using real bool types. |
8422 |
is found and set up inherited_fl to true if it's found on upper levels. |
1
by brian
clean slate |
8423 |
|
8424 |
@param cond_equal multiple equalities to search in |
|
8425 |
@param field field to look for |
|
55
by brian
Update for using real bool types. |
8426 |
@param[out] inherited_fl set up to true if multiple equality is found |
1
by brian
clean slate |
8427 |
on upper levels (not on current level of |
8428 |
cond_equal) |
|
8429 |
||
8430 |
@return |
|
8431 |
- Item_equal for the found multiple equality predicate if a success; |
|
8432 |
- NULL otherwise. |
|
8433 |
*/ |
|
8434 |
||
8435 |
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, |
|
8436 |
bool *inherited_fl) |
|
8437 |
{ |
|
8438 |
Item_equal *item= 0; |
|
55
by brian
Update for using real bool types. |
8439 |
bool in_upper_level= false; |
1
by brian
clean slate |
8440 |
while (cond_equal) |
8441 |
{ |
|
8442 |
List_iterator_fast<Item_equal> li(cond_equal->current_level); |
|
8443 |
while ((item= li++)) |
|
8444 |
{ |
|
8445 |
if (item->contains(field)) |
|
8446 |
goto finish; |
|
8447 |
} |
|
55
by brian
Update for using real bool types. |
8448 |
in_upper_level= true; |
1
by brian
clean slate |
8449 |
cond_equal= cond_equal->upper_levels; |
8450 |
} |
|
55
by brian
Update for using real bool types. |
8451 |
in_upper_level= false; |
1
by brian
clean slate |
8452 |
finish: |
8453 |
*inherited_fl= in_upper_level; |
|
8454 |
return item; |
|
8455 |
} |
|
8456 |
||
8457 |
||
8458 |
/** |
|
8459 |
Check whether an equality can be used to build multiple equalities. |
|
8460 |
||
8461 |
This function first checks whether the equality (left_item=right_item) |
|
8462 |
is a simple equality i.e. the one that equates a field with another field |
|
8463 |
or a constant (field=field_item or field=const_item). |
|
8464 |
If this is the case the function looks for a multiple equality |
|
8465 |
in the lists referenced directly or indirectly by cond_equal inferring |
|
8466 |
the given simple equality. If it doesn't find any, it builds a multiple |
|
8467 |
equality that covers the predicate, i.e. the predicate can be inferred |
|
8468 |
from this multiple equality. |
|
8469 |
The built multiple equality could be obtained in such a way: |
|
8470 |
create a binary multiple equality equivalent to the predicate, then |
|
8471 |
merge it, if possible, with one of old multiple equalities. |
|
8472 |
This guarantees that the set of multiple equalities covering equality |
|
8473 |
predicates will be minimal. |
|
8474 |
||
8475 |
EXAMPLE: |
|
8476 |
For the where condition |
|
8477 |
@code |
|
8478 |
WHERE a=b AND b=c AND |
|
8479 |
(b=2 OR f=e) |
|
8480 |
@endcode |
|
8481 |
the check_equality will be called for the following equality |
|
8482 |
predicates a=b, b=c, b=2 and f=e. |
|
8483 |
- For a=b it will be called with *cond_equal=(0,[]) and will transform |
|
8484 |
*cond_equal into (0,[Item_equal(a,b)]). |
|
8485 |
- For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)]) |
|
8486 |
and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]). |
|
8487 |
- For b=2 it will be called with *cond_equal=(ptr(CE),[]) |
|
8488 |
and will transform *cond_equal into (ptr(CE),[Item_equal(2,a,b,c)]). |
|
8489 |
- For f=e it will be called with *cond_equal=(ptr(CE), []) |
|
8490 |
and will transform *cond_equal into (ptr(CE),[Item_equal(f,e)]). |
|
8491 |
||
8492 |
@note |
|
8493 |
Now only fields that have the same type definitions (verified by |
|
8494 |
the Field::eq_def method) are placed to the same multiple equalities. |
|
8495 |
Because of this some equality predicates are not eliminated and |
|
8496 |
can be used in the constant propagation procedure. |
|
8497 |
We could weeken the equlity test as soon as at least one of the |
|
8498 |
equal fields is to be equal to a constant. It would require a |
|
8499 |
more complicated implementation: we would have to store, in |
|
8500 |
general case, its own constant for each fields from the multiple |
|
8501 |
equality. But at the same time it would allow us to get rid |
|
8502 |
of constant propagation completely: it would be done by the call |
|
8503 |
to build_equal_items_for_cond. |
|
8504 |
||
8505 |
||
8506 |
The implementation does not follow exactly the above rules to |
|
8507 |
build a new multiple equality for the equality predicate. |
|
8508 |
If it processes the equality of the form field1=field2, it |
|
8509 |
looks for multiple equalities me1 containig field1 and me2 containing |
|
8510 |
field2. If only one of them is found the fuction expands it with |
|
8511 |
the lacking field. If multiple equalities for both fields are |
|
8512 |
found they are merged. If both searches fail a new multiple equality |
|
8513 |
containing just field1 and field2 is added to the existing |
|
8514 |
multiple equalities. |
|
8515 |
If the function processes the predicate of the form field1=const, |
|
8516 |
it looks for a multiple equality containing field1. If found, the |
|
8517 |
function checks the constant of the multiple equality. If the value |
|
8518 |
is unknown, it is setup to const. Otherwise the value is compared with |
|
8519 |
const and the evaluation of the equality predicate is performed. |
|
8520 |
When expanding/merging equality predicates from the upper levels |
|
8521 |
the function first copies them for the current level. It looks |
|
8522 |
acceptable, as this happens rarely. The implementation without |
|
8523 |
copying would be much more complicated. |
|
8524 |
||
8525 |
@param left_item left term of the quality to be checked |
|
8526 |
@param right_item right term of the equality to be checked |
|
8527 |
@param item equality item if the equality originates from a condition |
|
8528 |
predicate, 0 if the equality is the result of row |
|
8529 |
elimination |
|
8530 |
@param cond_equal multiple equalities that must hold together with the |
|
8531 |
equality |
|
8532 |
||
8533 |
@retval |
|
55
by brian
Update for using real bool types. |
8534 |
true if the predicate is a simple equality predicate to be used |
1
by brian
clean slate |
8535 |
for building multiple equalities |
8536 |
@retval |
|
55
by brian
Update for using real bool types. |
8537 |
false otherwise |
1
by brian
clean slate |
8538 |
*/ |
8539 |
||
8540 |
static bool check_simple_equality(Item *left_item, Item *right_item, |
|
8541 |
Item *item, COND_EQUAL *cond_equal) |
|
8542 |
{ |
|
8543 |
if (left_item->type() == Item::REF_ITEM && |
|
8544 |
((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF) |
|
8545 |
{ |
|
8546 |
if (((Item_ref*)left_item)->depended_from) |
|
55
by brian
Update for using real bool types. |
8547 |
return false; |
1
by brian
clean slate |
8548 |
left_item= left_item->real_item(); |
8549 |
} |
|
8550 |
if (right_item->type() == Item::REF_ITEM && |
|
8551 |
((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF) |
|
8552 |
{ |
|
8553 |
if (((Item_ref*)right_item)->depended_from) |
|
55
by brian
Update for using real bool types. |
8554 |
return false; |
1
by brian
clean slate |
8555 |
right_item= right_item->real_item(); |
8556 |
} |
|
8557 |
if (left_item->type() == Item::FIELD_ITEM && |
|
8558 |
right_item->type() == Item::FIELD_ITEM && |
|
8559 |
!((Item_field*)left_item)->depended_from && |
|
8560 |
!((Item_field*)right_item)->depended_from) |
|
8561 |
{ |
|
8562 |
/* The predicate the form field1=field2 is processed */ |
|
8563 |
||
8564 |
Field *left_field= ((Item_field*) left_item)->field; |
|
8565 |
Field *right_field= ((Item_field*) right_item)->field; |
|
8566 |
||
8567 |
if (!left_field->eq_def(right_field)) |
|
55
by brian
Update for using real bool types. |
8568 |
return false; |
1
by brian
clean slate |
8569 |
|
8570 |
/* Search for multiple equalities containing field1 and/or field2 */ |
|
8571 |
bool left_copyfl, right_copyfl; |
|
8572 |
Item_equal *left_item_equal= |
|
8573 |
find_item_equal(cond_equal, left_field, &left_copyfl); |
|
8574 |
Item_equal *right_item_equal= |
|
8575 |
find_item_equal(cond_equal, right_field, &right_copyfl); |
|
8576 |
||
55
by brian
Update for using real bool types. |
8577 |
/* As (NULL=NULL) != true we can't just remove the predicate f=f */ |
1
by brian
clean slate |
8578 |
if (left_field->eq(right_field)) /* f = f */ |
8579 |
return (!(left_field->maybe_null() && !left_item_equal)); |
|
8580 |
||
8581 |
if (left_item_equal && left_item_equal == right_item_equal) |
|
8582 |
{ |
|
8583 |
/* |
|
8584 |
The equality predicate is inference of one of the existing |
|
8585 |
multiple equalities, i.e the condition is already covered |
|
8586 |
by upper level equalities |
|
8587 |
*/ |
|
55
by brian
Update for using real bool types. |
8588 |
return true; |
1
by brian
clean slate |
8589 |
} |
8590 |
||
8591 |
bool copy_item_name= test(item && item->name >= subq_sj_cond_name && |
|
8592 |
item->name < subq_sj_cond_name + 64); |
|
8593 |
/* Copy the found multiple equalities at the current level if needed */ |
|
8594 |
if (left_copyfl) |
|
8595 |
{ |
|
8596 |
/* left_item_equal of an upper level contains left_item */ |
|
8597 |
left_item_equal= new Item_equal(left_item_equal); |
|
8598 |
cond_equal->current_level.push_back(left_item_equal); |
|
8599 |
if (copy_item_name) |
|
8600 |
left_item_equal->name = item->name; |
|
8601 |
} |
|
8602 |
if (right_copyfl) |
|
8603 |
{ |
|
8604 |
/* right_item_equal of an upper level contains right_item */ |
|
8605 |
right_item_equal= new Item_equal(right_item_equal); |
|
8606 |
cond_equal->current_level.push_back(right_item_equal); |
|
8607 |
if (copy_item_name) |
|
8608 |
right_item_equal->name = item->name; |
|
8609 |
} |
|
8610 |
||
8611 |
if (left_item_equal) |
|
8612 |
{ |
|
8613 |
/* left item was found in the current or one of the upper levels */ |
|
8614 |
if (! right_item_equal) |
|
8615 |
left_item_equal->add((Item_field *) right_item); |
|
8616 |
else |
|
8617 |
{ |
|
8618 |
/* Merge two multiple equalities forming a new one */ |
|
8619 |
left_item_equal->merge(right_item_equal); |
|
8620 |
/* Remove the merged multiple equality from the list */ |
|
8621 |
List_iterator<Item_equal> li(cond_equal->current_level); |
|
8622 |
while ((li++) != right_item_equal) {}; |
|
8623 |
li.remove(); |
|
8624 |
} |
|
8625 |
} |
|
8626 |
else |
|
8627 |
{ |
|
8628 |
/* left item was not found neither the current nor in upper levels */ |
|
8629 |
if (right_item_equal) |
|
8630 |
{ |
|
8631 |
right_item_equal->add((Item_field *) left_item); |
|
8632 |
if (copy_item_name) |
|
8633 |
right_item_equal->name = item->name; |
|
8634 |
} |
|
8635 |
else |
|
8636 |
{ |
|
8637 |
/* None of the fields was found in multiple equalities */ |
|
8638 |
Item_equal *item_equal= new Item_equal((Item_field *) left_item, |
|
8639 |
(Item_field *) right_item); |
|
8640 |
cond_equal->current_level.push_back(item_equal); |
|
8641 |
if (copy_item_name) |
|
8642 |
item_equal->name = item->name; |
|
8643 |
} |
|
8644 |
} |
|
55
by brian
Update for using real bool types. |
8645 |
return true; |
1
by brian
clean slate |
8646 |
} |
8647 |
||
8648 |
{ |
|
8649 |
/* The predicate of the form field=const/const=field is processed */ |
|
8650 |
Item *const_item= 0; |
|
8651 |
Item_field *field_item= 0; |
|
8652 |
if (left_item->type() == Item::FIELD_ITEM && |
|
8653 |
!((Item_field*)left_item)->depended_from && |
|
8654 |
right_item->const_item()) |
|
8655 |
{ |
|
8656 |
field_item= (Item_field*) left_item; |
|
8657 |
const_item= right_item; |
|
8658 |
} |
|
8659 |
else if (right_item->type() == Item::FIELD_ITEM && |
|
8660 |
!((Item_field*)right_item)->depended_from && |
|
8661 |
left_item->const_item()) |
|
8662 |
{ |
|
8663 |
field_item= (Item_field*) right_item; |
|
8664 |
const_item= left_item; |
|
8665 |
} |
|
8666 |
||
8667 |
if (const_item && |
|
8668 |
field_item->result_type() == const_item->result_type()) |
|
8669 |
{ |
|
8670 |
bool copyfl; |
|
8671 |
||
8672 |
if (field_item->result_type() == STRING_RESULT) |
|
8673 |
{ |
|
8674 |
CHARSET_INFO *cs= ((Field_str*) field_item->field)->charset(); |
|
8675 |
if (!item) |
|
8676 |
{ |
|
8677 |
Item_func_eq *eq_item; |
|
8678 |
if ((eq_item= new Item_func_eq(left_item, right_item))) |
|
55
by brian
Update for using real bool types. |
8679 |
return false; |
1
by brian
clean slate |
8680 |
eq_item->set_cmp_func(); |
8681 |
eq_item->quick_fix_field(); |
|
8682 |
item= eq_item; |
|
8683 |
} |
|
8684 |
if ((cs != ((Item_func *) item)->compare_collation()) || |
|
8685 |
!cs->coll->propagate(cs, 0, 0)) |
|
55
by brian
Update for using real bool types. |
8686 |
return false; |
1
by brian
clean slate |
8687 |
} |
8688 |
||
8689 |
Item_equal *item_equal = find_item_equal(cond_equal, |
|
8690 |
field_item->field, ©fl); |
|
8691 |
if (copyfl) |
|
8692 |
{ |
|
8693 |
item_equal= new Item_equal(item_equal); |
|
8694 |
cond_equal->current_level.push_back(item_equal); |
|
8695 |
} |
|
8696 |
if (item_equal) |
|
8697 |
{ |
|
8698 |
/* |
|
8699 |
The flag cond_false will be set to 1 after this, if item_equal |
|
8700 |
already contains a constant and its value is not equal to |
|
8701 |
the value of const_item. |
|
8702 |
*/ |
|
8703 |
item_equal->add(const_item); |
|
8704 |
} |
|
8705 |
else |
|
8706 |
{ |
|
8707 |
item_equal= new Item_equal(const_item, field_item); |
|
8708 |
cond_equal->current_level.push_back(item_equal); |
|
8709 |
} |
|
55
by brian
Update for using real bool types. |
8710 |
return true; |
1
by brian
clean slate |
8711 |
} |
8712 |
} |
|
55
by brian
Update for using real bool types. |
8713 |
return false; |
1
by brian
clean slate |
8714 |
} |
8715 |
||
8716 |
||
8717 |
/** |
|
8718 |
Convert row equalities into a conjunction of regular equalities. |
|
8719 |
||
8720 |
The function converts a row equality of the form (E1,...,En)=(E'1,...,E'n) |
|
8721 |
into a list of equalities E1=E'1,...,En=E'n. For each of these equalities |
|
8722 |
Ei=E'i the function checks whether it is a simple equality or a row |
|
8723 |
equality. If it is a simple equality it is used to expand multiple |
|
8724 |
equalities of cond_equal. If it is a row equality it converted to a |
|
8725 |
sequence of equalities between row elements. If Ei=E'i is neither a |
|
8726 |
simple equality nor a row equality the item for this predicate is added |
|
8727 |
to eq_list. |
|
8728 |
||
8729 |
@param thd thread handle |
|
8730 |
@param left_row left term of the row equality to be processed |
|
8731 |
@param right_row right term of the row equality to be processed |
|
8732 |
@param cond_equal multiple equalities that must hold together with the |
|
8733 |
predicate |
|
8734 |
@param eq_list results of conversions of row equalities that are not |
|
8735 |
simple enough to form multiple equalities |
|
8736 |
||
8737 |
@retval |
|
55
by brian
Update for using real bool types. |
8738 |
true if conversion has succeeded (no fatal error) |
1
by brian
clean slate |
8739 |
@retval |
55
by brian
Update for using real bool types. |
8740 |
false otherwise |
1
by brian
clean slate |
8741 |
*/ |
8742 |
||
8743 |
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row, |
|
8744 |
COND_EQUAL *cond_equal, List<Item>* eq_list) |
|
8745 |
{ |
|
8746 |
uint n= left_row->cols(); |
|
8747 |
for (uint i= 0 ; i < n; i++) |
|
8748 |
{ |
|
8749 |
bool is_converted; |
|
8750 |
Item *left_item= left_row->element_index(i); |
|
8751 |
Item *right_item= right_row->element_index(i); |
|
8752 |
if (left_item->type() == Item::ROW_ITEM && |
|
8753 |
right_item->type() == Item::ROW_ITEM) |
|
8754 |
{ |
|
8755 |
is_converted= check_row_equality(thd, |
|
8756 |
(Item_row *) left_item, |
|
8757 |
(Item_row *) right_item, |
|
8758 |
cond_equal, eq_list); |
|
8759 |
if (!is_converted) |
|
8760 |
thd->lex->current_select->cond_count++; |
|
8761 |
} |
|
8762 |
else |
|
8763 |
{ |
|
8764 |
is_converted= check_simple_equality(left_item, right_item, 0, cond_equal); |
|
8765 |
thd->lex->current_select->cond_count++; |
|
8766 |
} |
|
8767 |
||
8768 |
if (!is_converted) |
|
8769 |
{ |
|
8770 |
Item_func_eq *eq_item; |
|
8771 |
if (!(eq_item= new Item_func_eq(left_item, right_item))) |
|
55
by brian
Update for using real bool types. |
8772 |
return false; |
1
by brian
clean slate |
8773 |
eq_item->set_cmp_func(); |
8774 |
eq_item->quick_fix_field(); |
|
8775 |
eq_list->push_back(eq_item); |
|
8776 |
} |
|
8777 |
} |
|
55
by brian
Update for using real bool types. |
8778 |
return true; |
1
by brian
clean slate |
8779 |
} |
8780 |
||
8781 |
||
8782 |
/** |
|
8783 |
Eliminate row equalities and form multiple equalities predicates. |
|
8784 |
||
8785 |
This function checks whether the item is a simple equality |
|
8786 |
i.e. the one that equates a field with another field or a constant |
|
8787 |
(field=field_item or field=constant_item), or, a row equality. |
|
8788 |
For a simple equality the function looks for a multiple equality |
|
8789 |
in the lists referenced directly or indirectly by cond_equal inferring |
|
8790 |
the given simple equality. If it doesn't find any, it builds/expands |
|
8791 |
multiple equality that covers the predicate. |
|
8792 |
Row equalities are eliminated substituted for conjunctive regular |
|
8793 |
equalities which are treated in the same way as original equality |
|
8794 |
predicates. |
|
8795 |
||
8796 |
@param thd thread handle |
|
8797 |
@param item predicate to process |
|
8798 |
@param cond_equal multiple equalities that must hold together with the |
|
8799 |
predicate |
|
8800 |
@param eq_list results of conversions of row equalities that are not |
|
8801 |
simple enough to form multiple equalities |
|
8802 |
||
8803 |
@retval |
|
55
by brian
Update for using real bool types. |
8804 |
true if re-writing rules have been applied |
1
by brian
clean slate |
8805 |
@retval |
55
by brian
Update for using real bool types. |
8806 |
false otherwise, i.e. |
1
by brian
clean slate |
8807 |
if the predicate is not an equality, |
8808 |
or, if the equality is neither a simple one nor a row equality, |
|
8809 |
or, if the procedure fails by a fatal error. |
|
8810 |
*/ |
|
8811 |
||
8812 |
static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal, |
|
8813 |
List<Item> *eq_list) |
|
8814 |
{ |
|
8815 |
if (item->type() == Item::FUNC_ITEM && |
|
8816 |
((Item_func*) item)->functype() == Item_func::EQ_FUNC) |
|
8817 |
{ |
|
8818 |
Item *left_item= ((Item_func*) item)->arguments()[0]; |
|
8819 |
Item *right_item= ((Item_func*) item)->arguments()[1]; |
|
8820 |
||
8821 |
if (left_item->type() == Item::ROW_ITEM && |
|
8822 |
right_item->type() == Item::ROW_ITEM) |
|
8823 |
{ |
|
8824 |
thd->lex->current_select->cond_count--; |
|
8825 |
return check_row_equality(thd, |
|
8826 |
(Item_row *) left_item, |
|
8827 |
(Item_row *) right_item, |
|
8828 |
cond_equal, eq_list); |
|
8829 |
} |
|
8830 |
else |
|
8831 |
return check_simple_equality(left_item, right_item, item, cond_equal); |
|
8832 |
} |
|
55
by brian
Update for using real bool types. |
8833 |
return false; |
1
by brian
clean slate |
8834 |
} |
8835 |
||
8836 |
||
8837 |
/** |
|
8838 |
Replace all equality predicates in a condition by multiple equality items. |
|
8839 |
||
8840 |
At each 'and' level the function detects items for equality predicates |
|
8841 |
and replaced them by a set of multiple equality items of class Item_equal, |
|
8842 |
taking into account inherited equalities from upper levels. |
|
8843 |
If an equality predicate is used not in a conjunction it's just |
|
8844 |
replaced by a multiple equality predicate. |
|
8845 |
For each 'and' level the function set a pointer to the inherited |
|
8846 |
multiple equalities in the cond_equal field of the associated |
|
8847 |
object of the type Item_cond_and. |
|
8848 |
The function also traverses the cond tree and and for each field reference |
|
8849 |
sets a pointer to the multiple equality item containing the field, if there |
|
8850 |
is any. If this multiple equality equates fields to a constant the |
|
8851 |
function replaces the field reference by the constant in the cases |
|
8852 |
when the field is not of a string type or when the field reference is |
|
8853 |
just an argument of a comparison predicate. |
|
8854 |
The function also determines the maximum number of members in |
|
8855 |
equality lists of each Item_cond_and object assigning it to |
|
8856 |
thd->lex->current_select->max_equal_elems. |
|
8857 |
||
8858 |
@note |
|
8859 |
Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of |
|
8860 |
f1=f2, .., fn-1=fn. It substitutes any inference from these |
|
8861 |
equality predicates that is equivalent to the conjunction. |
|
8862 |
Thus, =(a1,a2,a3) can substitute for ((a1=a3) AND (a2=a3) AND (a2=a1)) as |
|
8863 |
it is equivalent to ((a1=a2) AND (a2=a3)). |
|
8864 |
The function always makes a substitution of all equality predicates occured |
|
8865 |
in a conjuction for a minimal set of multiple equality predicates. |
|
8866 |
This set can be considered as a canonical representation of the |
|
8867 |
sub-conjunction of the equality predicates. |
|
8868 |
E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by |
|
8869 |
(=(t1.a,t2.b,t3.c) AND t2.b>5), not by |
|
8870 |
(=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5); |
|
8871 |
while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by |
|
8872 |
(=(t1.a,t2.b) AND =(t3.c=t4.d) AND t2.b>5), |
|
8873 |
but if additionally =(t4.d,t2.b) is inherited, it |
|
8874 |
will be replaced by (=(t1.a,t2.b,t3.c,t4.d) AND t2.b>5) |
|
8875 |
||
8876 |
The function performs the substitution in a recursive descent by |
|
8877 |
the condtion tree, passing to the next AND level a chain of multiple |
|
8878 |
equality predicates which have been built at the upper levels. |
|
8879 |
The Item_equal items built at the level are attached to other |
|
8880 |
non-equality conjucts as a sublist. The pointer to the inherited |
|
8881 |
multiple equalities is saved in the and condition object (Item_cond_and). |
|
8882 |
This chain allows us for any field reference occurence easyly to find a |
|
8883 |
multiple equality that must be held for this occurence. |
|
8884 |
For each AND level we do the following: |
|
8885 |
- scan it for all equality predicate (=) items |
|
8886 |
- join them into disjoint Item_equal() groups |
|
8887 |
- process the included OR conditions recursively to do the same for |
|
8888 |
lower AND levels. |
|
8889 |
||
8890 |
We need to do things in this order as lower AND levels need to know about |
|
8891 |
all possible Item_equal objects in upper levels. |
|
8892 |
||
8893 |
@param thd thread handle |
|
8894 |
@param cond condition(expression) where to make replacement |
|
8895 |
@param inherited path to all inherited multiple equality items |
|
8896 |
||
8897 |
@return |
|
8898 |
pointer to the transformed condition |
|
8899 |
*/ |
|
8900 |
||
8901 |
static COND *build_equal_items_for_cond(THD *thd, COND *cond, |
|
8902 |
COND_EQUAL *inherited) |
|
8903 |
{ |
|
8904 |
Item_equal *item_equal; |
|
8905 |
COND_EQUAL cond_equal; |
|
8906 |
cond_equal.upper_levels= inherited; |
|
8907 |
||
8908 |
if (cond->type() == Item::COND_ITEM) |
|
8909 |
{ |
|
8910 |
List<Item> eq_list; |
|
8911 |
bool and_level= ((Item_cond*) cond)->functype() == |
|
8912 |
Item_func::COND_AND_FUNC; |
|
8913 |
List<Item> *args= ((Item_cond*) cond)->argument_list(); |
|
8914 |
||
8915 |
List_iterator<Item> li(*args); |
|
8916 |
Item *item; |
|
8917 |
||
8918 |
if (and_level) |
|
8919 |
{ |
|
8920 |
/* |
|
8921 |
Retrieve all conjucts of this level detecting the equality |
|
8922 |
that are subject to substitution by multiple equality items and |
|
8923 |
removing each such predicate from the conjunction after having |
|
8924 |
found/created a multiple equality whose inference the predicate is. |
|
8925 |
*/ |
|
8926 |
while ((item= li++)) |
|
8927 |
{ |
|
8928 |
/* |
|
8929 |
PS/SP note: we can safely remove a node from AND-OR |
|
8930 |
structure here because it's restored before each |
|
8931 |
re-execution of any prepared statement/stored procedure. |
|
8932 |
*/ |
|
8933 |
if (check_equality(thd, item, &cond_equal, &eq_list)) |
|
8934 |
li.remove(); |
|
8935 |
} |
|
8936 |
||
8937 |
List_iterator_fast<Item_equal> it(cond_equal.current_level); |
|
8938 |
while ((item_equal= it++)) |
|
8939 |
{ |
|
8940 |
item_equal->fix_length_and_dec(); |
|
8941 |
item_equal->update_used_tables(); |
|
8942 |
set_if_bigger(thd->lex->current_select->max_equal_elems, |
|
8943 |
item_equal->members()); |
|
8944 |
} |
|
8945 |
||
8946 |
((Item_cond_and*)cond)->cond_equal= cond_equal; |
|
8947 |
inherited= &(((Item_cond_and*)cond)->cond_equal); |
|
8948 |
} |
|
8949 |
/* |
|
8950 |
Make replacement of equality predicates for lower levels |
|
8951 |
of the condition expression. |
|
8952 |
*/ |
|
8953 |
li.rewind(); |
|
8954 |
while ((item= li++)) |
|
8955 |
{ |
|
8956 |
Item *new_item; |
|
8957 |
if ((new_item= build_equal_items_for_cond(thd, item, inherited)) != item) |
|
8958 |
{ |
|
8959 |
/* This replacement happens only for standalone equalities */ |
|
8960 |
/* |
|
8961 |
This is ok with PS/SP as the replacement is done for |
|
8962 |
arguments of an AND/OR item, which are restored for each |
|
8963 |
execution of PS/SP. |
|
8964 |
*/ |
|
8965 |
li.replace(new_item); |
|
8966 |
} |
|
8967 |
} |
|
8968 |
if (and_level) |
|
8969 |
{ |
|
8970 |
args->concat(&eq_list); |
|
8971 |
args->concat((List<Item> *)&cond_equal.current_level); |
|
8972 |
} |
|
8973 |
} |
|
8974 |
else if (cond->type() == Item::FUNC_ITEM) |
|
8975 |
{ |
|
8976 |
List<Item> eq_list; |
|
8977 |
/* |
|
8978 |
If an equality predicate forms the whole and level, |
|
8979 |
we call it standalone equality and it's processed here. |
|
8980 |
E.g. in the following where condition |
|
8981 |
WHERE a=5 AND (b=5 or a=c) |
|
8982 |
(b=5) and (a=c) are standalone equalities. |
|
8983 |
In general we can't leave alone standalone eqalities: |
|
8984 |
for WHERE a=b AND c=d AND (b=c OR d=5) |
|
8985 |
b=c is replaced by =(a,b,c,d). |
|
8986 |
*/ |
|
8987 |
if (check_equality(thd, cond, &cond_equal, &eq_list)) |
|
8988 |
{ |
|
8989 |
int n= cond_equal.current_level.elements + eq_list.elements; |
|
8990 |
if (n == 0) |
|
152
by Brian Aker
longlong replacement |
8991 |
return new Item_int((int64_t) 1,1); |
1
by brian
clean slate |
8992 |
else if (n == 1) |
8993 |
{ |
|
8994 |
if ((item_equal= cond_equal.current_level.pop())) |
|
8995 |
{ |
|
8996 |
item_equal->fix_length_and_dec(); |
|
8997 |
item_equal->update_used_tables(); |
|
8998 |
} |
|
8999 |
else |
|
9000 |
item_equal= (Item_equal *) eq_list.pop(); |
|
9001 |
set_if_bigger(thd->lex->current_select->max_equal_elems, |
|
9002 |
item_equal->members()); |
|
9003 |
return item_equal; |
|
9004 |
} |
|
9005 |
else |
|
9006 |
{ |
|
9007 |
/* |
|
9008 |
Here a new AND level must be created. It can happen only |
|
9009 |
when a row equality is processed as a standalone predicate. |
|
9010 |
*/ |
|
9011 |
Item_cond_and *and_cond= new Item_cond_and(eq_list); |
|
9012 |
and_cond->quick_fix_field(); |
|
9013 |
List<Item> *args= and_cond->argument_list(); |
|
9014 |
List_iterator_fast<Item_equal> it(cond_equal.current_level); |
|
9015 |
while ((item_equal= it++)) |
|
9016 |
{ |
|
9017 |
item_equal->fix_length_and_dec(); |
|
9018 |
item_equal->update_used_tables(); |
|
9019 |
set_if_bigger(thd->lex->current_select->max_equal_elems, |
|
9020 |
item_equal->members()); |
|
9021 |
} |
|
9022 |
and_cond->cond_equal= cond_equal; |
|
9023 |
args->concat((List<Item> *)&cond_equal.current_level); |
|
9024 |
||
9025 |
return and_cond; |
|
9026 |
} |
|
9027 |
} |
|
9028 |
/* |
|
9029 |
For each field reference in cond, not from equal item predicates, |
|
9030 |
set a pointer to the multiple equality it belongs to (if there is any) |
|
9031 |
as soon the field is not of a string type or the field reference is |
|
9032 |
an argument of a comparison predicate. |
|
9033 |
*/ |
|
9034 |
uchar *is_subst_valid= (uchar *) 1; |
|
9035 |
cond= cond->compile(&Item::subst_argument_checker, |
|
9036 |
&is_subst_valid, |
|
9037 |
&Item::equal_fields_propagator, |
|
9038 |
(uchar *) inherited); |
|
9039 |
cond->update_used_tables(); |
|
9040 |
} |
|
9041 |
return cond; |
|
9042 |
} |
|
9043 |
||
9044 |
||
9045 |
/** |
|
9046 |
Build multiple equalities for a condition and all on expressions that |
|
9047 |
inherit these multiple equalities. |
|
9048 |
||
9049 |
The function first applies the build_equal_items_for_cond function |
|
9050 |
to build all multiple equalities for condition cond utilizing equalities |
|
9051 |
referred through the parameter inherited. The extended set of |
|
9052 |
equalities is returned in the structure referred by the cond_equal_ref |
|
9053 |
parameter. After this the function calls itself recursively for |
|
9054 |
all on expressions whose direct references can be found in join_list |
|
9055 |
and who inherit directly the multiple equalities just having built. |
|
9056 |
||
9057 |
@note |
|
9058 |
The on expression used in an outer join operation inherits all equalities |
|
9059 |
from the on expression of the embedding join, if there is any, or |
|
9060 |
otherwise - from the where condition. |
|
9061 |
This fact is not obvious, but presumably can be proved. |
|
9062 |
Consider the following query: |
|
9063 |
@code |
|
9064 |
SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t1.a=t3.a AND t2.a=t4.a |
|
9065 |
WHERE t1.a=t2.a; |
|
9066 |
@endcode |
|
9067 |
If the on expression in the query inherits =(t1.a,t2.a), then we |
|
9068 |
can build the multiple equality =(t1.a,t2.a,t3.a,t4.a) that infers |
|
9069 |
the equality t3.a=t4.a. Although the on expression |
|
9070 |
t1.a=t3.a AND t2.a=t4.a AND t3.a=t4.a is not equivalent to the one |
|
9071 |
in the query the latter can be replaced by the former: the new query |
|
9072 |
will return the same result set as the original one. |
|
9073 |
||
9074 |
Interesting that multiple equality =(t1.a,t2.a,t3.a,t4.a) allows us |
|
9075 |
to use t1.a=t3.a AND t3.a=t4.a under the on condition: |
|
9076 |
@code |
|
9077 |
SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t1.a=t3.a AND t3.a=t4.a |
|
9078 |
WHERE t1.a=t2.a |
|
9079 |
@endcode |
|
9080 |
This query equivalent to: |
|
9081 |
@code |
|
9082 |
SELECT * FROM (t1 LEFT JOIN (t3,t4) ON t1.a=t3.a AND t3.a=t4.a),t2 |
|
9083 |
WHERE t1.a=t2.a |
|
9084 |
@endcode |
|
9085 |
Similarly the original query can be rewritten to the query: |
|
9086 |
@code |
|
9087 |
SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a |
|
9088 |
WHERE t1.a=t2.a |
|
9089 |
@endcode |
|
9090 |
that is equivalent to: |
|
9091 |
@code |
|
9092 |
SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1 |
|
9093 |
WHERE t1.a=t2.a |
|
9094 |
@endcode |
|
9095 |
Thus, applying equalities from the where condition we basically |
|
9096 |
can get more freedom in performing join operations. |
|
9097 |
Althogh we don't use this property now, it probably makes sense to use |
|
9098 |
it in the future. |
|
9099 |
@param thd Thread handler |
|
9100 |
@param cond condition to build the multiple equalities for |
|
9101 |
@param inherited path to all inherited multiple equality items |
|
9102 |
@param join_list list of join tables to which the condition |
|
9103 |
refers to |
|
9104 |
@param[out] cond_equal_ref pointer to the structure to place built |
|
9105 |
equalities in |
|
9106 |
||
9107 |
@return |
|
9108 |
pointer to the transformed condition containing multiple equalities |
|
9109 |
*/ |
|
9110 |
||
9111 |
static COND *build_equal_items(THD *thd, COND *cond, |
|
9112 |
COND_EQUAL *inherited, |
|
9113 |
List<TABLE_LIST> *join_list, |
|
9114 |
COND_EQUAL **cond_equal_ref) |
|
9115 |
{ |
|
9116 |
COND_EQUAL *cond_equal= 0; |
|
9117 |
||
9118 |
if (cond) |
|
9119 |
{ |
|
9120 |
cond= build_equal_items_for_cond(thd, cond, inherited); |
|
9121 |
cond->update_used_tables(); |
|
9122 |
if (cond->type() == Item::COND_ITEM && |
|
9123 |
((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC) |
|
9124 |
cond_equal= &((Item_cond_and*) cond)->cond_equal; |
|
9125 |
else if (cond->type() == Item::FUNC_ITEM && |
|
9126 |
((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC) |
|
9127 |
{ |
|
9128 |
cond_equal= new COND_EQUAL; |
|
9129 |
cond_equal->current_level.push_back((Item_equal *) cond); |
|
9130 |
} |
|
9131 |
} |
|
9132 |
if (cond_equal) |
|
9133 |
{ |
|
9134 |
cond_equal->upper_levels= inherited; |
|
9135 |
inherited= cond_equal; |
|
9136 |
} |
|
9137 |
*cond_equal_ref= cond_equal; |
|
9138 |
||
9139 |
if (join_list) |
|
9140 |
{ |
|
9141 |
TABLE_LIST *table; |
|
9142 |
List_iterator<TABLE_LIST> li(*join_list); |
|
9143 |
||
9144 |
while ((table= li++)) |
|
9145 |
{ |
|
9146 |
if (table->on_expr) |
|
9147 |
{ |
|
9148 |
List<TABLE_LIST> *nested_join_list= table->nested_join ? |
|
9149 |
&table->nested_join->join_list : NULL; |
|
9150 |
/* |
|
9151 |
We can modify table->on_expr because its old value will |
|
9152 |
be restored before re-execution of PS/SP. |
|
9153 |
*/ |
|
9154 |
table->on_expr= build_equal_items(thd, table->on_expr, inherited, |
|
9155 |
nested_join_list, |
|
9156 |
&table->cond_equal); |
|
9157 |
} |
|
9158 |
} |
|
9159 |
} |
|
9160 |
||
9161 |
return cond; |
|
9162 |
} |
|
9163 |
||
9164 |
||
9165 |
/** |
|
9166 |
Compare field items by table order in the execution plan. |
|
9167 |
||
9168 |
field1 considered as better than field2 if the table containing |
|
9169 |
field1 is accessed earlier than the table containing field2. |
|
9170 |
The function finds out what of two fields is better according |
|
9171 |
this criteria. |
|
9172 |
||
9173 |
@param field1 first field item to compare |
|
9174 |
@param field2 second field item to compare |
|
9175 |
@param table_join_idx index to tables determining table order |
|
9176 |
||
9177 |
@retval |
|
9178 |
1 if field1 is better than field2 |
|
9179 |
@retval |
|
9180 |
-1 if field2 is better than field1 |
|
9181 |
@retval |
|
9182 |
0 otherwise |
|
9183 |
*/ |
|
9184 |
||
9185 |
static int compare_fields_by_table_order(Item_field *field1, |
|
9186 |
Item_field *field2, |
|
9187 |
void *table_join_idx) |
|
9188 |
{ |
|
9189 |
int cmp= 0; |
|
9190 |
bool outer_ref= 0; |
|
9191 |
if (field2->used_tables() & OUTER_REF_TABLE_BIT) |
|
9192 |
{ |
|
9193 |
outer_ref= 1; |
|
9194 |
cmp= -1; |
|
9195 |
} |
|
9196 |
if (field2->used_tables() & OUTER_REF_TABLE_BIT) |
|
9197 |
{ |
|
9198 |
outer_ref= 1; |
|
9199 |
cmp++; |
|
9200 |
} |
|
9201 |
if (outer_ref) |
|
9202 |
return cmp; |
|
9203 |
JOIN_TAB **idx= (JOIN_TAB **) table_join_idx; |
|
9204 |
cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr]; |
|
9205 |
return cmp < 0 ? -1 : (cmp ? 1 : 0); |
|
9206 |
} |
|
9207 |
||
9208 |
||
9209 |
/** |
|
9210 |
Generate minimal set of simple equalities equivalent to a multiple equality. |
|
9211 |
||
9212 |
The function retrieves the fields of the multiple equality item |
|
9213 |
item_equal and for each field f: |
|
9214 |
- if item_equal contains const it generates the equality f=const_item; |
|
9215 |
- otherwise, if f is not the first field, generates the equality |
|
9216 |
f=item_equal->get_first(). |
|
9217 |
All generated equality are added to the cond conjunction. |
|
9218 |
||
9219 |
@param cond condition to add the generated equality to |
|
9220 |
@param upper_levels structure to access multiple equality of upper levels |
|
9221 |
@param item_equal multiple equality to generate simple equality from |
|
9222 |
||
9223 |
@note |
|
9224 |
Before generating an equality function checks that it has not |
|
9225 |
been generated for multiple equalities of the upper levels. |
|
9226 |
E.g. for the following where condition |
|
9227 |
WHERE a=5 AND ((a=b AND b=c) OR c>4) |
|
9228 |
the upper level AND condition will contain =(5,a), |
|
9229 |
while the lower level AND condition will contain =(5,a,b,c). |
|
9230 |
When splitting =(5,a,b,c) into a separate equality predicates |
|
9231 |
we should omit 5=a, as we have it already in the upper level. |
|
9232 |
The following where condition gives us a more complicated case: |
|
9233 |
WHERE t1.a=t2.b AND t3.c=t4.d AND (t2.b=t3.c OR t4.e>5 ...) AND ... |
|
9234 |
Given the tables are accessed in the order t1->t2->t3->t4 for |
|
9235 |
the selected query execution plan the lower level multiple |
|
9236 |
equality =(t1.a,t2.b,t3.c,t4.d) formally should be converted to |
|
9237 |
t1.a=t2.b AND t1.a=t3.c AND t1.a=t4.d. But t1.a=t2.a will be |
|
9238 |
generated for the upper level. Also t3.c=t4.d will be generated there. |
|
9239 |
So only t1.a=t3.c should be left in the lower level. |
|
9240 |
If cond is equal to 0, then not more then one equality is generated |
|
9241 |
and a pointer to it is returned as the result of the function. |
|
9242 |
||
9243 |
@return |
|
9244 |
- The condition with generated simple equalities or |
|
9245 |
a pointer to the simple generated equality, if success. |
|
9246 |
- 0, otherwise. |
|
9247 |
*/ |
|
9248 |
||
9249 |
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, |
|
9250 |
Item_equal *item_equal) |
|
9251 |
{ |
|
9252 |
List<Item> eq_list; |
|
9253 |
Item_func_eq *eq_item= 0; |
|
9254 |
if (((Item *) item_equal)->const_item() && !item_equal->val_int()) |
|
152
by Brian Aker
longlong replacement |
9255 |
return new Item_int((int64_t) 0,1); |
1
by brian
clean slate |
9256 |
Item *item_const= item_equal->get_const(); |
9257 |
Item_equal_iterator it(*item_equal); |
|
9258 |
Item *head; |
|
9259 |
if (item_const) |
|
9260 |
head= item_const; |
|
9261 |
else |
|
9262 |
{ |
|
9263 |
head= item_equal->get_first(); |
|
9264 |
it++; |
|
9265 |
} |
|
9266 |
Item_field *item_field; |
|
9267 |
while ((item_field= it++)) |
|
9268 |
{ |
|
9269 |
Item_equal *upper= item_field->find_item_equal(upper_levels); |
|
9270 |
Item_field *item= item_field; |
|
9271 |
if (upper) |
|
9272 |
{ |
|
9273 |
if (item_const && upper->get_const()) |
|
9274 |
item= 0; |
|
9275 |
else |
|
9276 |
{ |
|
9277 |
Item_equal_iterator li(*item_equal); |
|
9278 |
while ((item= li++) != item_field) |
|
9279 |
{ |
|
9280 |
if (item->find_item_equal(upper_levels) == upper) |
|
9281 |
break; |
|
9282 |
} |
|
9283 |
} |
|
9284 |
} |
|
9285 |
if (item == item_field) |
|
9286 |
{ |
|
9287 |
if (eq_item) |
|
9288 |
eq_list.push_back(eq_item); |
|
9289 |
eq_item= new Item_func_eq(item_field, head); |
|
9290 |
if (!eq_item) |
|
9291 |
return 0; |
|
9292 |
eq_item->set_cmp_func(); |
|
9293 |
eq_item->quick_fix_field(); |
|
9294 |
} |
|
9295 |
} |
|
9296 |
||
9297 |
if (!cond && !eq_list.head()) |
|
9298 |
{ |
|
9299 |
if (!eq_item) |
|
152
by Brian Aker
longlong replacement |
9300 |
return new Item_int((int64_t) 1,1); |
1
by brian
clean slate |
9301 |
return eq_item; |
9302 |
} |
|
9303 |
||
9304 |
if (eq_item) |
|
9305 |
eq_list.push_back(eq_item); |
|
9306 |
if (!cond) |
|
9307 |
cond= new Item_cond_and(eq_list); |
|
9308 |
else |
|
9309 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
9310 |
assert(cond->type() == Item::COND_ITEM); |
1
by brian
clean slate |
9311 |
((Item_cond *) cond)->add_at_head(&eq_list); |
9312 |
} |
|
9313 |
||
9314 |
cond->quick_fix_field(); |
|
9315 |
cond->update_used_tables(); |
|
9316 |
||
9317 |
return cond; |
|
9318 |
} |
|
9319 |
||
9320 |
||
9321 |
/** |
|
9322 |
Substitute every field reference in a condition by the best equal field |
|
9323 |
and eliminate all multiple equality predicates. |
|
9324 |
||
9325 |
The function retrieves the cond condition and for each encountered |
|
9326 |
multiple equality predicate it sorts the field references in it |
|
9327 |
according to the order of tables specified by the table_join_idx |
|
9328 |
parameter. Then it eliminates the multiple equality predicate it |
|
9329 |
replacing it by the conjunction of simple equality predicates |
|
9330 |
equating every field from the multiple equality to the first |
|
9331 |
field in it, or to the constant, if there is any. |
|
9332 |
After this the function retrieves all other conjuncted |
|
9333 |
predicates substitute every field reference by the field reference |
|
9334 |
to the first equal field or equal constant if there are any. |
|
9335 |
@param cond condition to process |
|
9336 |
@param cond_equal multiple equalities to take into consideration |
|
9337 |
@param table_join_idx index to tables determining field preference |
|
9338 |
||
9339 |
@note |
|
9340 |
At the first glance full sort of fields in multiple equality |
|
9341 |
seems to be an overkill. Yet it's not the case due to possible |
|
9342 |
new fields in multiple equality item of lower levels. We want |
|
9343 |
the order in them to comply with the order of upper levels. |
|
9344 |
||
9345 |
@return |
|
9346 |
The transformed condition |
|
9347 |
*/ |
|
9348 |
||
9349 |
static COND* substitute_for_best_equal_field(COND *cond, |
|
9350 |
COND_EQUAL *cond_equal, |
|
9351 |
void *table_join_idx) |
|
9352 |
{ |
|
9353 |
Item_equal *item_equal; |
|
9354 |
||
9355 |
if (cond->type() == Item::COND_ITEM) |
|
9356 |
{ |
|
9357 |
List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); |
|
9358 |
||
9359 |
bool and_level= ((Item_cond*) cond)->functype() == |
|
9360 |
Item_func::COND_AND_FUNC; |
|
9361 |
if (and_level) |
|
9362 |
{ |
|
9363 |
cond_equal= &((Item_cond_and *) cond)->cond_equal; |
|
9364 |
cond_list->disjoin((List<Item> *) &cond_equal->current_level); |
|
9365 |
||
9366 |
List_iterator_fast<Item_equal> it(cond_equal->current_level); |
|
9367 |
while ((item_equal= it++)) |
|
9368 |
{ |
|
9369 |
item_equal->sort(&compare_fields_by_table_order, table_join_idx); |
|
9370 |
} |
|
9371 |
} |
|
9372 |
||
9373 |
List_iterator<Item> li(*cond_list); |
|
9374 |
Item *item; |
|
9375 |
while ((item= li++)) |
|
9376 |
{ |
|
9377 |
Item *new_item =substitute_for_best_equal_field(item, cond_equal, |
|
9378 |
table_join_idx); |
|
9379 |
/* |
|
9380 |
This works OK with PS/SP re-execution as changes are made to |
|
9381 |
the arguments of AND/OR items only |
|
9382 |
*/ |
|
9383 |
if (new_item != item) |
|
9384 |
li.replace(new_item); |
|
9385 |
} |
|
9386 |
||
9387 |
if (and_level) |
|
9388 |
{ |
|
9389 |
List_iterator_fast<Item_equal> it(cond_equal->current_level); |
|
9390 |
while ((item_equal= it++)) |
|
9391 |
{ |
|
9392 |
cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal); |
|
9393 |
// This occurs when eliminate_item_equal() founds that cond is |
|
9394 |
// always false and substitutes it with Item_int 0. |
|
9395 |
// Due to this, value of item_equal will be 0, so just return it. |
|
9396 |
if (cond->type() != Item::COND_ITEM) |
|
9397 |
break; |
|
9398 |
} |
|
9399 |
} |
|
9400 |
if (cond->type() == Item::COND_ITEM && |
|
9401 |
!((Item_cond*)cond)->argument_list()->elements) |
|
9402 |
cond= new Item_int((int32)cond->val_bool()); |
|
9403 |
||
9404 |
} |
|
9405 |
else if (cond->type() == Item::FUNC_ITEM && |
|
9406 |
((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC) |
|
9407 |
{ |
|
9408 |
item_equal= (Item_equal *) cond; |
|
9409 |
item_equal->sort(&compare_fields_by_table_order, table_join_idx); |
|
9410 |
if (cond_equal && cond_equal->current_level.head() == item_equal) |
|
9411 |
cond_equal= 0; |
|
9412 |
return eliminate_item_equal(0, cond_equal, item_equal); |
|
9413 |
} |
|
9414 |
else |
|
9415 |
cond->transform(&Item::replace_equal_field, 0); |
|
9416 |
return cond; |
|
9417 |
} |
|
9418 |
||
9419 |
||
9420 |
/** |
|
9421 |
Check appearance of new constant items in multiple equalities |
|
9422 |
of a condition after reading a constant table. |
|
9423 |
||
9424 |
The function retrieves the cond condition and for each encountered |
|
9425 |
multiple equality checks whether new constants have appeared after |
|
9426 |
reading the constant (single row) table tab. If so it adjusts |
|
9427 |
the multiple equality appropriately. |
|
9428 |
||
9429 |
@param cond condition whose multiple equalities are to be checked |
|
9430 |
@param table constant table that has been read |
|
9431 |
*/ |
|
9432 |
||
9433 |
static void update_const_equal_items(COND *cond, JOIN_TAB *tab) |
|
9434 |
{ |
|
9435 |
if (!(cond->used_tables() & tab->table->map)) |
|
9436 |
return; |
|
9437 |
||
9438 |
if (cond->type() == Item::COND_ITEM) |
|
9439 |
{ |
|
9440 |
List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); |
|
9441 |
List_iterator_fast<Item> li(*cond_list); |
|
9442 |
Item *item; |
|
9443 |
while ((item= li++)) |
|
9444 |
update_const_equal_items(item, tab); |
|
9445 |
} |
|
9446 |
else if (cond->type() == Item::FUNC_ITEM && |
|
9447 |
((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC) |
|
9448 |
{ |
|
9449 |
Item_equal *item_equal= (Item_equal *) cond; |
|
9450 |
bool contained_const= item_equal->get_const() != NULL; |
|
9451 |
item_equal->update_const(); |
|
9452 |
if (!contained_const && item_equal->get_const()) |
|
9453 |
{ |
|
9454 |
/* Update keys for range analysis */ |
|
9455 |
Item_equal_iterator it(*item_equal); |
|
9456 |
Item_field *item_field; |
|
9457 |
while ((item_field= it++)) |
|
9458 |
{ |
|
9459 |
Field *field= item_field->field; |
|
9460 |
JOIN_TAB *stat= field->table->reginfo.join_tab; |
|
9461 |
key_map possible_keys= field->key_start; |
|
9462 |
possible_keys.intersect(field->table->keys_in_use_for_query); |
|
9463 |
stat[0].const_keys.merge(possible_keys); |
|
9464 |
||
9465 |
/* |
|
9466 |
For each field in the multiple equality (for which we know that it |
|
9467 |
is a constant) we have to find its corresponding key part, and set |
|
9468 |
that key part in const_key_parts. |
|
9469 |
*/ |
|
9470 |
if (!possible_keys.is_clear_all()) |
|
9471 |
{ |
|
9472 |
TABLE *tab= field->table; |
|
9473 |
KEYUSE *use; |
|
9474 |
for (use= stat->keyuse; use && use->table == tab; use++) |
|
9475 |
if (possible_keys.is_set(use->key) && |
|
9476 |
tab->key_info[use->key].key_part[use->keypart].field == |
|
9477 |
field) |
|
9478 |
tab->const_key_parts[use->key]|= use->keypart_map; |
|
9479 |
} |
|
9480 |
} |
|
9481 |
} |
|
9482 |
} |
|
9483 |
} |
|
9484 |
||
9485 |
||
9486 |
/* |
|
9487 |
change field = field to field = const for each found field = const in the |
|
9488 |
and_level |
|
9489 |
*/ |
|
9490 |
||
9491 |
static void |
|
9492 |
change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list, |
|
9493 |
Item *and_father, Item *cond, |
|
9494 |
Item *field, Item *value) |
|
9495 |
{ |
|
9496 |
if (cond->type() == Item::COND_ITEM) |
|
9497 |
{ |
|
9498 |
bool and_level= ((Item_cond*) cond)->functype() == |
|
9499 |
Item_func::COND_AND_FUNC; |
|
9500 |
List_iterator<Item> li(*((Item_cond*) cond)->argument_list()); |
|
9501 |
Item *item; |
|
9502 |
while ((item=li++)) |
|
9503 |
change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item, |
|
9504 |
field, value); |
|
9505 |
return; |
|
9506 |
} |
|
9507 |
if (cond->eq_cmp_result() == Item::COND_OK) |
|
9508 |
return; // Not a boolean function |
|
9509 |
||
9510 |
Item_bool_func2 *func= (Item_bool_func2*) cond; |
|
9511 |
Item **args= func->arguments(); |
|
9512 |
Item *left_item= args[0]; |
|
9513 |
Item *right_item= args[1]; |
|
9514 |
Item_func::Functype functype= func->functype(); |
|
9515 |
||
9516 |
if (right_item->eq(field,0) && left_item != value && |
|
9517 |
right_item->cmp_context == field->cmp_context && |
|
9518 |
(left_item->result_type() != STRING_RESULT || |
|
9519 |
value->result_type() != STRING_RESULT || |
|
9520 |
left_item->collation.collation == value->collation.collation)) |
|
9521 |
{ |
|
9522 |
Item *tmp=value->clone_item(); |
|
9523 |
tmp->collation.set(right_item->collation); |
|
9524 |
||
9525 |
if (tmp) |
|
9526 |
{ |
|
9527 |
thd->change_item_tree(args + 1, tmp); |
|
9528 |
func->update_used_tables(); |
|
9529 |
if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) |
|
9530 |
&& and_father != cond && !left_item->const_item()) |
|
9531 |
{ |
|
9532 |
cond->marker=1; |
|
9533 |
COND_CMP *tmp2; |
|
9534 |
if ((tmp2=new COND_CMP(and_father,func))) |
|
9535 |
save_list->push_back(tmp2); |
|
9536 |
} |
|
9537 |
func->set_cmp_func(); |
|
9538 |
} |
|
9539 |
} |
|
9540 |
else if (left_item->eq(field,0) && right_item != value && |
|
9541 |
left_item->cmp_context == field->cmp_context && |
|
9542 |
(right_item->result_type() != STRING_RESULT || |
|
9543 |
value->result_type() != STRING_RESULT || |
|
9544 |
right_item->collation.collation == value->collation.collation)) |
|
9545 |
{ |
|
9546 |
Item *tmp= value->clone_item(); |
|
9547 |
tmp->collation.set(left_item->collation); |
|
9548 |
||
9549 |
if (tmp) |
|
9550 |
{ |
|
9551 |
thd->change_item_tree(args, tmp); |
|
9552 |
value= tmp; |
|
9553 |
func->update_used_tables(); |
|
9554 |
if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) |
|
9555 |
&& and_father != cond && !right_item->const_item()) |
|
9556 |
{ |
|
9557 |
args[0]= args[1]; // For easy check |
|
9558 |
thd->change_item_tree(args + 1, value); |
|
9559 |
cond->marker=1; |
|
9560 |
COND_CMP *tmp2; |
|
9561 |
if ((tmp2=new COND_CMP(and_father,func))) |
|
9562 |
save_list->push_back(tmp2); |
|
9563 |
} |
|
9564 |
func->set_cmp_func(); |
|
9565 |
} |
|
9566 |
} |
|
9567 |
} |
|
9568 |
||
9569 |
/** |
|
9570 |
Remove additional condition inserted by IN/ALL/ANY transformation. |
|
9571 |
||
9572 |
@param conds condition for processing |
|
9573 |
||
9574 |
@return |
|
9575 |
new conditions |
|
9576 |
*/ |
|
9577 |
||
9578 |
static Item *remove_additional_cond(Item* conds) |
|
9579 |
{ |
|
9580 |
if (conds->name == in_additional_cond) |
|
9581 |
return 0; |
|
9582 |
if (conds->type() == Item::COND_ITEM) |
|
9583 |
{ |
|
9584 |
Item_cond *cnd= (Item_cond*) conds; |
|
9585 |
List_iterator<Item> li(*(cnd->argument_list())); |
|
9586 |
Item *item; |
|
9587 |
while ((item= li++)) |
|
9588 |
{ |
|
9589 |
if (item->name == in_additional_cond) |
|
9590 |
{ |
|
9591 |
li.remove(); |
|
9592 |
if (cnd->argument_list()->elements == 1) |
|
9593 |
return cnd->argument_list()->head(); |
|
9594 |
return conds; |
|
9595 |
} |
|
9596 |
} |
|
9597 |
} |
|
9598 |
return conds; |
|
9599 |
} |
|
9600 |
||
9601 |
static void |
|
9602 |
propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list, |
|
9603 |
COND *and_father, COND *cond) |
|
9604 |
{ |
|
9605 |
if (cond->type() == Item::COND_ITEM) |
|
9606 |
{ |
|
9607 |
bool and_level= ((Item_cond*) cond)->functype() == |
|
9608 |
Item_func::COND_AND_FUNC; |
|
9609 |
List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list()); |
|
9610 |
Item *item; |
|
9611 |
I_List<COND_CMP> save; |
|
9612 |
while ((item=li++)) |
|
9613 |
{ |
|
9614 |
propagate_cond_constants(thd, &save,and_level ? cond : item, item); |
|
9615 |
} |
|
9616 |
if (and_level) |
|
9617 |
{ // Handle other found items |
|
9618 |
I_List_iterator<COND_CMP> cond_itr(save); |
|
9619 |
COND_CMP *cond_cmp; |
|
9620 |
while ((cond_cmp=cond_itr++)) |
|
9621 |
{ |
|
9622 |
Item **args= cond_cmp->cmp_func->arguments(); |
|
9623 |
if (!args[0]->const_item()) |
|
9624 |
change_cond_ref_to_const(thd, &save,cond_cmp->and_level, |
|
9625 |
cond_cmp->and_level, args[0], args[1]); |
|
9626 |
} |
|
9627 |
} |
|
9628 |
} |
|
9629 |
else if (and_father != cond && !cond->marker) // In a AND group |
|
9630 |
{ |
|
9631 |
if (cond->type() == Item::FUNC_ITEM && |
|
9632 |
(((Item_func*) cond)->functype() == Item_func::EQ_FUNC || |
|
9633 |
((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)) |
|
9634 |
{ |
|
9635 |
Item_func_eq *func=(Item_func_eq*) cond; |
|
9636 |
Item **args= func->arguments(); |
|
9637 |
bool left_const= args[0]->const_item(); |
|
9638 |
bool right_const= args[1]->const_item(); |
|
9639 |
if (!(left_const && right_const) && |
|
9640 |
args[0]->result_type() == args[1]->result_type()) |
|
9641 |
{ |
|
9642 |
if (right_const) |
|
9643 |
{ |
|
9644 |
resolve_const_item(thd, &args[1], args[0]); |
|
9645 |
func->update_used_tables(); |
|
9646 |
change_cond_ref_to_const(thd, save_list, and_father, and_father, |
|
9647 |
args[0], args[1]); |
|
9648 |
} |
|
9649 |
else if (left_const) |
|
9650 |
{ |
|
9651 |
resolve_const_item(thd, &args[0], args[1]); |
|
9652 |
func->update_used_tables(); |
|
9653 |
change_cond_ref_to_const(thd, save_list, and_father, and_father, |
|
9654 |
args[1], args[0]); |
|
9655 |
} |
|
9656 |
} |
|
9657 |
} |
|
9658 |
} |
|
9659 |
} |
|
9660 |
||
9661 |
||
9662 |
/** |
|
9663 |
Simplify joins replacing outer joins by inner joins whenever it's |
|
9664 |
possible. |
|
9665 |
||
9666 |
The function, during a retrieval of join_list, eliminates those |
|
9667 |
outer joins that can be converted into inner join, possibly nested. |
|
9668 |
It also moves the on expressions for the converted outer joins |
|
9669 |
and from inner joins to conds. |
|
9670 |
The function also calculates some attributes for nested joins: |
|
9671 |
- used_tables |
|
9672 |
- not_null_tables |
|
9673 |
- dep_tables. |
|
9674 |
- on_expr_dep_tables |
|
9675 |
The first two attributes are used to test whether an outer join can |
|
9676 |
be substituted for an inner join. The third attribute represents the |
|
9677 |
relation 'to be dependent on' for tables. If table t2 is dependent |
|
9678 |
on table t1, then in any evaluated execution plan table access to |
|
9679 |
table t2 must precede access to table t2. This relation is used also |
|
9680 |
to check whether the query contains invalid cross-references. |
|
9681 |
The forth attribute is an auxiliary one and is used to calculate |
|
9682 |
dep_tables. |
|
9683 |
As the attribute dep_tables qualifies possibles orders of tables in the |
|
9684 |
execution plan, the dependencies required by the straight join |
|
9685 |
modifiers are reflected in this attribute as well. |
|
9686 |
The function also removes all braces that can be removed from the join |
|
9687 |
expression without changing its meaning. |
|
9688 |
||
9689 |
@note |
|
9690 |
An outer join can be replaced by an inner join if the where condition |
|
9691 |
or the on expression for an embedding nested join contains a conjunctive |
|
9692 |
predicate rejecting null values for some attribute of the inner tables. |
|
9693 |
||
9694 |
E.g. in the query: |
|
9695 |
@code |
|
9696 |
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5 |
|
9697 |
@endcode |
|
9698 |
the predicate t2.b < 5 rejects nulls. |
|
9699 |
The query is converted first to: |
|
9700 |
@code |
|
9701 |
SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5 |
|
9702 |
@endcode |
|
9703 |
then to the equivalent form: |
|
9704 |
@code |
|
9705 |
SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a |
|
9706 |
@endcode |
|
9707 |
||
9708 |
||
9709 |
Similarly the following query: |
|
9710 |
@code |
|
9711 |
SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b |
|
9712 |
WHERE t2.c < 5 |
|
9713 |
@endcode |
|
9714 |
is converted to: |
|
9715 |
@code |
|
9716 |
SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b |
|
9717 |
||
9718 |
@endcode |
|
9719 |
||
9720 |
One conversion might trigger another: |
|
9721 |
@code |
|
9722 |
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a |
|
9723 |
LEFT JOIN t3 ON t3.b=t2.b |
|
9724 |
WHERE t3 IS NOT NULL => |
|
9725 |
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3 |
|
9726 |
WHERE t3 IS NOT NULL AND t3.b=t2.b => |
|
9727 |
SELECT * FROM t1, t2, t3 |
|
9728 |
WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a |
|
9729 |
@endcode |
|
9730 |
||
9731 |
The function removes all unnecessary braces from the expression |
|
9732 |
produced by the conversions. |
|
9733 |
E.g. |
|
9734 |
@code |
|
9735 |
SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b |
|
9736 |
@endcode |
|
9737 |
finally is converted to: |
|
9738 |
@code |
|
9739 |
SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b |
|
9740 |
||
9741 |
@endcode |
|
9742 |
||
9743 |
||
9744 |
It also will remove braces from the following queries: |
|
9745 |
@code |
|
9746 |
SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b |
|
9747 |
SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b. |
|
9748 |
@endcode |
|
9749 |
||
9750 |
The benefit of this simplification procedure is that it might return |
|
9751 |
a query for which the optimizer can evaluate execution plan with more |
|
9752 |
join orders. With a left join operation the optimizer does not |
|
9753 |
consider any plan where one of the inner tables is before some of outer |
|
9754 |
tables. |
|
9755 |
||
9756 |
IMPLEMENTATION |
|
9757 |
The function is implemented by a recursive procedure. On the recursive |
|
9758 |
ascent all attributes are calculated, all outer joins that can be |
|
9759 |
converted are replaced and then all unnecessary braces are removed. |
|
9760 |
As join list contains join tables in the reverse order sequential |
|
9761 |
elimination of outer joins does not require extra recursive calls. |
|
9762 |
||
9763 |
SEMI-JOIN NOTES |
|
9764 |
Remove all semi-joins that have are within another semi-join (i.e. have |
|
9765 |
an "ancestor" semi-join nest) |
|
9766 |
||
9767 |
EXAMPLES |
|
9768 |
Here is an example of a join query with invalid cross references: |
|
9769 |
@code |
|
9770 |
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b |
|
9771 |
@endcode |
|
9772 |
||
9773 |
@param join reference to the query info |
|
9774 |
@param join_list list representation of the join to be converted |
|
9775 |
@param conds conditions to add on expressions for converted joins |
|
9776 |
@param top true <=> conds is the where condition |
|
9777 |
||
9778 |
@return |
|
9779 |
- The new condition, if success |
|
9780 |
- 0, otherwise |
|
9781 |
*/ |
|
9782 |
||
9783 |
static COND * |
|
9784 |
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top, |
|
9785 |
bool in_sj) |
|
9786 |
{ |
|
9787 |
TABLE_LIST *table; |
|
9788 |
NESTED_JOIN *nested_join; |
|
9789 |
TABLE_LIST *prev_table= 0; |
|
9790 |
List_iterator<TABLE_LIST> li(*join_list); |
|
9791 |
||
9792 |
/* |
|
9793 |
Try to simplify join operations from join_list. |
|
9794 |
The most outer join operation is checked for conversion first. |
|
9795 |
*/ |
|
9796 |
while ((table= li++)) |
|
9797 |
{ |
|
9798 |
table_map used_tables; |
|
9799 |
table_map not_null_tables= (table_map) 0; |
|
9800 |
||
9801 |
if ((nested_join= table->nested_join)) |
|
9802 |
{ |
|
9803 |
/* |
|
9804 |
If the element of join_list is a nested join apply |
|
9805 |
the procedure to its nested join list first. |
|
9806 |
*/ |
|
9807 |
if (table->on_expr) |
|
9808 |
{ |
|
9809 |
Item *expr= table->on_expr; |
|
9810 |
/* |
|
9811 |
If an on expression E is attached to the table, |
|
9812 |
check all null rejected predicates in this expression. |
|
9813 |
If such a predicate over an attribute belonging to |
|
9814 |
an inner table of an embedded outer join is found, |
|
9815 |
the outer join is converted to an inner join and |
|
9816 |
the corresponding on expression is added to E. |
|
9817 |
*/ |
|
9818 |
expr= simplify_joins(join, &nested_join->join_list, |
|
55
by brian
Update for using real bool types. |
9819 |
expr, false, in_sj || table->sj_on_expr); |
1
by brian
clean slate |
9820 |
|
9821 |
if (!table->prep_on_expr || expr != table->on_expr) |
|
9822 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
9823 |
assert(expr); |
1
by brian
clean slate |
9824 |
|
9825 |
table->on_expr= expr; |
|
9826 |
table->prep_on_expr= expr->copy_andor_structure(join->thd); |
|
9827 |
} |
|
9828 |
} |
|
9829 |
nested_join->used_tables= (table_map) 0; |
|
9830 |
nested_join->not_null_tables=(table_map) 0; |
|
9831 |
conds= simplify_joins(join, &nested_join->join_list, conds, top, |
|
9832 |
in_sj || table->sj_on_expr); |
|
9833 |
used_tables= nested_join->used_tables; |
|
9834 |
not_null_tables= nested_join->not_null_tables; |
|
9835 |
} |
|
9836 |
else |
|
9837 |
{ |
|
9838 |
if (!table->prep_on_expr) |
|
9839 |
table->prep_on_expr= table->on_expr; |
|
9840 |
used_tables= table->table->map; |
|
9841 |
if (conds) |
|
9842 |
not_null_tables= conds->not_null_tables(); |
|
9843 |
} |
|
9844 |
||
9845 |
if (table->embedding) |
|
9846 |
{ |
|
9847 |
table->embedding->nested_join->used_tables|= used_tables; |
|
9848 |
table->embedding->nested_join->not_null_tables|= not_null_tables; |
|
9849 |
} |
|
9850 |
||
9851 |
if (!table->outer_join || (used_tables & not_null_tables)) |
|
9852 |
{ |
|
9853 |
/* |
|
9854 |
For some of the inner tables there are conjunctive predicates |
|
9855 |
that reject nulls => the outer join can be replaced by an inner join. |
|
9856 |
*/ |
|
9857 |
table->outer_join= 0; |
|
9858 |
if (table->on_expr) |
|
9859 |
{ |
|
9860 |
/* Add ON expression to the WHERE or upper-level ON condition. */ |
|
9861 |
if (conds) |
|
9862 |
{ |
|
9863 |
conds= and_conds(conds, table->on_expr); |
|
9864 |
conds->top_level_item(); |
|
9865 |
/* conds is always a new item as both cond and on_expr existed */ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
9866 |
assert(!conds->fixed); |
1
by brian
clean slate |
9867 |
conds->fix_fields(join->thd, &conds); |
9868 |
} |
|
9869 |
else |
|
9870 |
conds= table->on_expr; |
|
9871 |
table->prep_on_expr= table->on_expr= 0; |
|
9872 |
} |
|
9873 |
} |
|
9874 |
||
9875 |
if (!top) |
|
9876 |
continue; |
|
9877 |
||
9878 |
/* |
|
9879 |
Only inner tables of non-convertible outer joins |
|
9880 |
remain with on_expr. |
|
9881 |
*/ |
|
9882 |
if (table->on_expr) |
|
9883 |
{ |
|
9884 |
table->dep_tables|= table->on_expr->used_tables(); |
|
9885 |
if (table->embedding) |
|
9886 |
{ |
|
9887 |
table->dep_tables&= ~table->embedding->nested_join->used_tables; |
|
9888 |
/* |
|
9889 |
Embedding table depends on tables used |
|
9890 |
in embedded on expressions. |
|
9891 |
*/ |
|
9892 |
table->embedding->on_expr_dep_tables|= table->on_expr->used_tables(); |
|
9893 |
} |
|
9894 |
else |
|
9895 |
table->dep_tables&= ~table->table->map; |
|
9896 |
} |
|
9897 |
||
9898 |
if (prev_table) |
|
9899 |
{ |
|
9900 |
/* The order of tables is reverse: prev_table follows table */ |
|
9901 |
if (prev_table->straight) |
|
9902 |
prev_table->dep_tables|= used_tables; |
|
9903 |
if (prev_table->on_expr) |
|
9904 |
{ |
|
9905 |
prev_table->dep_tables|= table->on_expr_dep_tables; |
|
9906 |
table_map prev_used_tables= prev_table->nested_join ? |
|
9907 |
prev_table->nested_join->used_tables : |
|
9908 |
prev_table->table->map; |
|
9909 |
/* |
|
9910 |
If on expression contains only references to inner tables |
|
9911 |
we still make the inner tables dependent on the outer tables. |
|
9912 |
It would be enough to set dependency only on one outer table |
|
9913 |
for them. Yet this is really a rare case. |
|
9914 |
*/ |
|
9915 |
if (!(prev_table->on_expr->used_tables() & ~prev_used_tables)) |
|
9916 |
prev_table->dep_tables|= used_tables; |
|
9917 |
} |
|
9918 |
} |
|
9919 |
prev_table= table; |
|
9920 |
} |
|
9921 |
||
9922 |
/* |
|
9923 |
Flatten nested joins that can be flattened. |
|
9924 |
no ON expression and not a semi-join => can be flattened. |
|
9925 |
*/ |
|
9926 |
li.rewind(); |
|
9927 |
while ((table= li++)) |
|
9928 |
{ |
|
9929 |
nested_join= table->nested_join; |
|
9930 |
if (table->sj_on_expr && !in_sj) |
|
9931 |
{ |
|
9932 |
/* |
|
9933 |
If this is a semi-join that is not contained within another semi-join, |
|
9934 |
leave it intact (otherwise it is flattened) |
|
9935 |
*/ |
|
9936 |
join->select_lex->sj_nests.push_back(table); |
|
9937 |
} |
|
9938 |
else if (nested_join && !table->on_expr) |
|
9939 |
{ |
|
9940 |
TABLE_LIST *tbl; |
|
9941 |
List_iterator<TABLE_LIST> it(nested_join->join_list); |
|
9942 |
while ((tbl= it++)) |
|
9943 |
{ |
|
9944 |
tbl->embedding= table->embedding; |
|
9945 |
tbl->join_list= table->join_list; |
|
9946 |
} |
|
9947 |
li.replace(nested_join->join_list); |
|
9948 |
} |
|
9949 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
9950 |
return(conds); |
1
by brian
clean slate |
9951 |
} |
9952 |
||
9953 |
||
9954 |
/** |
|
9955 |
Assign each nested join structure a bit in nested_join_map. |
|
9956 |
||
9957 |
Assign each nested join structure (except "confluent" ones - those that |
|
9958 |
embed only one element) a bit in nested_join_map. |
|
9959 |
||
9960 |
@param join Join being processed |
|
9961 |
@param join_list List of tables |
|
9962 |
@param first_unused Number of first unused bit in nested_join_map before the |
|
9963 |
call |
|
9964 |
||
9965 |
@note |
|
9966 |
This function is called after simplify_joins(), when there are no |
|
9967 |
redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so |
|
9968 |
we will not run out of bits in nested_join_map. |
|
9969 |
||
9970 |
@return |
|
9971 |
First unused bit in nested_join_map after the call. |
|
9972 |
*/ |
|
9973 |
||
9974 |
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, |
|
9975 |
uint first_unused) |
|
9976 |
{ |
|
9977 |
List_iterator<TABLE_LIST> li(*join_list); |
|
9978 |
TABLE_LIST *table; |
|
9979 |
while ((table= li++)) |
|
9980 |
{ |
|
9981 |
NESTED_JOIN *nested_join; |
|
9982 |
if ((nested_join= table->nested_join)) |
|
9983 |
{ |
|
9984 |
/* |
|
9985 |
It is guaranteed by simplify_joins() function that a nested join |
|
9986 |
that has only one child is either |
|
9987 |
- a single-table view (the child is the underlying table), or |
|
9988 |
- a single-table semi-join nest |
|
9989 |
||
9990 |
We don't assign bits to such sj-nests because |
|
9991 |
1. it is redundant (a "sequence" of one table cannot be interleaved |
|
9992 |
with anything) |
|
9993 |
2. we could run out bits in nested_join_map otherwise. |
|
9994 |
*/ |
|
9995 |
if (nested_join->join_list.elements != 1) |
|
9996 |
{ |
|
9997 |
/* Don't assign bits to sj-nests */ |
|
9998 |
if (table->on_expr) |
|
9999 |
nested_join->nj_map= (nested_join_map) 1 << first_unused++; |
|
10000 |
first_unused= build_bitmap_for_nested_joins(&nested_join->join_list, |
|
10001 |
first_unused); |
|
10002 |
} |
|
10003 |
} |
|
10004 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
10005 |
return(first_unused); |
1
by brian
clean slate |
10006 |
} |
10007 |
||
10008 |
||
10009 |
/** |
|
10010 |
Set NESTED_JOIN::counter=0 in all nested joins in passed list. |
|
10011 |
||
10012 |
Recursively set NESTED_JOIN::counter=0 for all nested joins contained in |
|
10013 |
the passed join_list. |
|
10014 |
||
10015 |
@param join_list List of nested joins to process. It may also contain base |
|
10016 |
tables which will be ignored. |
|
10017 |
*/ |
|
10018 |
||
10019 |
static void reset_nj_counters(List<TABLE_LIST> *join_list) |
|
10020 |
{ |
|
10021 |
List_iterator<TABLE_LIST> li(*join_list); |
|
10022 |
TABLE_LIST *table; |
|
10023 |
while ((table= li++)) |
|
10024 |
{ |
|
10025 |
NESTED_JOIN *nested_join; |
|
10026 |
if ((nested_join= table->nested_join)) |
|
10027 |
{ |
|
10028 |
nested_join->counter_= 0; |
|
10029 |
reset_nj_counters(&nested_join->join_list); |
|
10030 |
} |
|
10031 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
10032 |
return; |
1
by brian
clean slate |
10033 |
} |
10034 |
||
10035 |
||
10036 |
/** |
|
10037 |
Check interleaving with an inner tables of an outer join for |
|
10038 |
extension table. |
|
10039 |
||
10040 |
Check if table next_tab can be added to current partial join order, and |
|
10041 |
if yes, record that it has been added. |
|
10042 |
||
10043 |
The function assumes that both current partial join order and its |
|
10044 |
extension with next_tab are valid wrt table dependencies. |
|
10045 |
||
10046 |
@verbatim |
|
10047 |
IMPLEMENTATION |
|
10048 |
LIMITATIONS ON JOIN ORDER |
|
10049 |
The nested [outer] joins executioner algorithm imposes these limitations |
|
10050 |
on join order: |
|
10051 |
1. "Outer tables first" - any "outer" table must be before any |
|
10052 |
corresponding "inner" table. |
|
10053 |
2. "No interleaving" - tables inside a nested join must form a continuous |
|
10054 |
sequence in join order (i.e. the sequence must not be interrupted by |
|
10055 |
tables that are outside of this nested join). |
|
10056 |
||
10057 |
#1 is checked elsewhere, this function checks #2 provided that #1 has |
|
10058 |
been already checked. |
|
10059 |
||
10060 |
WHY NEED NON-INTERLEAVING |
|
10061 |
Consider an example: |
|
10062 |
||
10063 |
select * from t0 join t1 left join (t2 join t3) on cond1 |
|
10064 |
||
10065 |
The join order "t1 t2 t0 t3" is invalid: |
|
10066 |
||
10067 |
table t0 is outside of the nested join, so WHERE condition for t0 is |
|
10068 |
attached directly to t0 (without triggers, and it may be used to access |
|
10069 |
t0). Applying WHERE(t0) to (t2,t0,t3) record is invalid as we may miss |
|
10070 |
combinations of (t1, t2, t3) that satisfy condition cond1, and produce a |
|
10071 |
null-complemented (t1, t2.NULLs, t3.NULLs) row, which should not have |
|
10072 |
been produced. |
|
10073 |
||
10074 |
If table t0 is not between t2 and t3, the problem doesn't exist: |
|
10075 |
If t0 is located after (t2,t3), WHERE(t0) is applied after nested join |
|
10076 |
processing has finished. |
|
10077 |
If t0 is located before (t2,t3), predicates like WHERE_cond(t0, t2) are |
|
10078 |
wrapped into condition triggers, which takes care of correct nested |
|
10079 |
join processing. |
|
10080 |
||
10081 |
HOW IT IS IMPLEMENTED |
|
10082 |
The limitations on join order can be rephrased as follows: for valid |
|
10083 |
join order one must be able to: |
|
10084 |
1. write down the used tables in the join order on one line. |
|
10085 |
2. for each nested join, put one '(' and one ')' on the said line |
|
10086 |
3. write "LEFT JOIN" and "ON (...)" where appropriate |
|
10087 |
4. get a query equivalent to the query we're trying to execute. |
|
10088 |
||
10089 |
Calls to check_interleaving_with_nj() are equivalent to writing the |
|
10090 |
above described line from left to right. |
|
10091 |
A single check_interleaving_with_nj(A,B) call is equivalent to writing |
|
10092 |
table B and appropriate brackets on condition that table A and |
|
10093 |
appropriate brackets is the last what was written. Graphically the |
|
10094 |
transition is as follows: |
|
10095 |
||
10096 |
+---- current position |
|
10097 |
| |
|
10098 |
... last_tab ))) | ( next_tab ) )..) | ... |
|
10099 |
X Y Z | |
|
10100 |
+- need to move to this |
|
10101 |
position. |
|
10102 |
||
10103 |
Notes about the position: |
|
10104 |
The caller guarantees that there is no more then one X-bracket by |
|
10105 |
checking "!(remaining_tables & s->dependent)" before calling this |
|
10106 |
function. X-bracket may have a pair in Y-bracket. |
|
10107 |
||
10108 |
When "writing" we store/update this auxilary info about the current |
|
10109 |
position: |
|
10110 |
1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested |
|
10111 |
joins) we've opened but didn't close. |
|
10112 |
2. {each NESTED_JOIN structure not simplified away}->counter - number |
|
10113 |
of this nested join's children that have already been added to to |
|
10114 |
the partial join order. |
|
10115 |
@endverbatim |
|
10116 |
||
10117 |
@param join Join being processed |
|
10118 |
@param last_tab Last table in current partial join order (this function is |
|
10119 |
not called for empty partial join orders) |
|
10120 |
@param next_tab Table we're going to extend the current partial join with |
|
10121 |
||
10122 |
@retval |
|
55
by brian
Update for using real bool types. |
10123 |
false Join order extended, nested joins info about current join |
1
by brian
clean slate |
10124 |
order (see NOTE section) updated. |
10125 |
@retval |
|
55
by brian
Update for using real bool types. |
10126 |
true Requested join order extension not allowed. |
1
by brian
clean slate |
10127 |
*/ |
10128 |
||
10129 |
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab) |
|
10130 |
{ |
|
10131 |
TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding; |
|
10132 |
JOIN *join= last_tab->join; |
|
10133 |
||
10134 |
if (join->cur_embedding_map & ~next_tab->embedding_map) |
|
10135 |
{ |
|
10136 |
/* |
|
10137 |
next_tab is outside of the "pair of brackets" we're currently in. |
|
10138 |
Cannot add it. |
|
10139 |
*/ |
|
55
by brian
Update for using real bool types. |
10140 |
return true; |
1
by brian
clean slate |
10141 |
} |
10142 |
||
10143 |
/* |
|
10144 |
Do update counters for "pairs of brackets" that we've left (marked as |
|
10145 |
X,Y,Z in the above picture) |
|
10146 |
*/ |
|
10147 |
for (;next_emb; next_emb= next_emb->embedding) |
|
10148 |
{ |
|
10149 |
next_emb->nested_join->counter_++; |
|
10150 |
if (next_emb->nested_join->counter_ == 1) |
|
10151 |
{ |
|
10152 |
/* |
|
10153 |
next_emb is the first table inside a nested join we've "entered". In |
|
10154 |
the picture above, we're looking at the 'X' bracket. Don't exit yet as |
|
10155 |
X bracket might have Y pair bracket. |
|
10156 |
*/ |
|
10157 |
join->cur_embedding_map |= next_emb->nested_join->nj_map; |
|
10158 |
} |
|
10159 |
||
10160 |
if (next_emb->nested_join->join_list.elements != |
|
10161 |
next_emb->nested_join->counter_) |
|
10162 |
break; |
|
10163 |
||
10164 |
/* |
|
10165 |
We're currently at Y or Z-bracket as depicted in the above picture. |
|
10166 |
Mark that we've left it and continue walking up the brackets hierarchy. |
|
10167 |
*/ |
|
10168 |
join->cur_embedding_map &= ~next_emb->nested_join->nj_map; |
|
10169 |
} |
|
55
by brian
Update for using real bool types. |
10170 |
return false; |
1
by brian
clean slate |
10171 |
} |
10172 |
||
10173 |
||
10174 |
/** |
|
10175 |
Nested joins perspective: Remove the last table from the join order. |
|
10176 |
||
10177 |
Remove the last table from the partial join order and update the nested |
|
10178 |
joins counters and join->cur_embedding_map. It is ok to call this |
|
10179 |
function for the first table in join order (for which |
|
10180 |
check_interleaving_with_nj has not been called) |
|
10181 |
||
10182 |
@param last join table to remove, it is assumed to be the last in current |
|
10183 |
partial join order. |
|
10184 |
*/ |
|
10185 |
||
10186 |
static void restore_prev_nj_state(JOIN_TAB *last) |
|
10187 |
{ |
|
10188 |
TABLE_LIST *last_emb= last->table->pos_in_table_list->embedding; |
|
10189 |
JOIN *join= last->join; |
|
10190 |
while (last_emb) |
|
10191 |
{ |
|
10192 |
if (last_emb->on_expr) |
|
10193 |
{ |
|
10194 |
if (!(--last_emb->nested_join->counter_)) |
|
10195 |
join->cur_embedding_map&= ~last_emb->nested_join->nj_map; |
|
10196 |
else if (last_emb->nested_join->join_list.elements-1 == |
|
10197 |
last_emb->nested_join->counter_) |
|
10198 |
join->cur_embedding_map|= last_emb->nested_join->nj_map; |
|
10199 |
else |
|
10200 |
break; |
|
10201 |
} |
|
10202 |
last_emb= last_emb->embedding; |
|
10203 |
} |
|
10204 |
} |
|
10205 |
||
10206 |
||
10207 |
||
10208 |
static |
|
10209 |
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab) |
|
10210 |
{ |
|
10211 |
TABLE_LIST *emb_sj_nest; |
|
10212 |
if ((emb_sj_nest= tab->emb_sj_nest)) |
|
10213 |
{ |
|
10214 |
tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables; |
|
10215 |
/* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */ |
|
10216 |
if (!(remaining_tables & emb_sj_nest->sj_inner_tables)) |
|
10217 |
tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables; |
|
10218 |
} |
|
10219 |
} |
|
10220 |
||
10221 |
||
10222 |
/* |
|
10223 |
we assume remaining_tables doesnt contain @tab. |
|
10224 |
*/ |
|
10225 |
||
10226 |
static void restore_prev_sj_state(const table_map remaining_tables, |
|
10227 |
const JOIN_TAB *tab) |
|
10228 |
{ |
|
10229 |
TABLE_LIST *emb_sj_nest; |
|
10230 |
if ((emb_sj_nest= tab->emb_sj_nest)) |
|
10231 |
{ |
|
10232 |
/* If we're removing the last SJ-inner table, remove the sj-nest */ |
|
10233 |
if ((remaining_tables & emb_sj_nest->sj_inner_tables) == |
|
10234 |
(emb_sj_nest->sj_inner_tables & ~tab->table->map)) |
|
10235 |
{ |
|
10236 |
tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables; |
|
10237 |
} |
|
10238 |
} |
|
10239 |
} |
|
10240 |
||
10241 |
||
10242 |
static COND * |
|
10243 |
optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list, |
|
10244 |
Item::cond_result *cond_value) |
|
10245 |
{ |
|
10246 |
THD *thd= join->thd; |
|
10247 |
||
10248 |
if (!conds) |
|
10249 |
*cond_value= Item::COND_TRUE; |
|
10250 |
else |
|
10251 |
{ |
|
10252 |
/* |
|
10253 |
Build all multiple equality predicates and eliminate equality |
|
10254 |
predicates that can be inferred from these multiple equalities. |
|
10255 |
For each reference of a field included into a multiple equality |
|
10256 |
that occurs in a function set a pointer to the multiple equality |
|
10257 |
predicate. Substitute a constant instead of this field if the |
|
10258 |
multiple equality contains a constant. |
|
10259 |
*/ |
|
10260 |
conds= build_equal_items(join->thd, conds, NULL, join_list, |
|
10261 |
&join->cond_equal); |
|
10262 |
||
10263 |
/* change field = field to field = const for each found field = const */ |
|
10264 |
propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds); |
|
10265 |
/* |
|
10266 |
Remove all instances of item == item |
|
10267 |
Remove all and-levels where CONST item != CONST item |
|
10268 |
*/ |
|
10269 |
conds= remove_eq_conds(thd, conds, cond_value) ; |
|
10270 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
10271 |
return(conds); |
1
by brian
clean slate |
10272 |
} |
10273 |
||
10274 |
||
10275 |
/** |
|
10276 |
Remove const and eq items. |
|
10277 |
||
10278 |
@return |
|
10279 |
Return new item, or NULL if no condition @n |
|
10280 |
cond_value is set to according: |
|
10281 |
- COND_OK : query is possible (field = constant) |
|
10282 |
- COND_TRUE : always true ( 1 = 1 ) |
|
10283 |
- COND_FALSE : always false ( 1 = 2 ) |
|
10284 |
*/ |
|
10285 |
||
10286 |
COND * |
|
10287 |
remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value) |
|
10288 |
{ |
|
10289 |
if (cond->type() == Item::COND_ITEM) |
|
10290 |
{ |
|
10291 |
bool and_level= ((Item_cond*) cond)->functype() |
|
10292 |
== Item_func::COND_AND_FUNC; |
|
10293 |
List_iterator<Item> li(*((Item_cond*) cond)->argument_list()); |
|
10294 |
Item::cond_result tmp_cond_value; |
|
10295 |
bool should_fix_fields=0; |
|
10296 |
||
10297 |
*cond_value=Item::COND_UNDEF; |
|
10298 |
Item *item; |
|
10299 |
while ((item=li++)) |
|
10300 |
{ |
|
10301 |
Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value); |
|
10302 |
if (!new_item) |
|
10303 |
li.remove(); |
|
10304 |
else if (item != new_item) |
|
10305 |
{ |
|
10306 |
VOID(li.replace(new_item)); |
|
10307 |
should_fix_fields=1; |
|
10308 |
} |
|
10309 |
if (*cond_value == Item::COND_UNDEF) |
|
10310 |
*cond_value=tmp_cond_value; |
|
10311 |
switch (tmp_cond_value) { |
|
55
by brian
Update for using real bool types. |
10312 |
case Item::COND_OK: // Not true or false |
1
by brian
clean slate |
10313 |
if (and_level || *cond_value == Item::COND_FALSE) |
10314 |
*cond_value=tmp_cond_value; |
|
10315 |
break; |
|
10316 |
case Item::COND_FALSE: |
|
10317 |
if (and_level) |
|
10318 |
{ |
|
10319 |
*cond_value=tmp_cond_value; |
|
10320 |
return (COND*) 0; // Always false |
|
10321 |
} |
|
10322 |
break; |
|
10323 |
case Item::COND_TRUE: |
|
10324 |
if (!and_level) |
|
10325 |
{ |
|
10326 |
*cond_value= tmp_cond_value; |
|
10327 |
return (COND*) 0; // Always true |
|
10328 |
} |
|
10329 |
break; |
|
10330 |
case Item::COND_UNDEF: // Impossible |
|
10331 |
break; /* purecov: deadcode */ |
|
10332 |
} |
|
10333 |
} |
|
10334 |
if (should_fix_fields) |
|
10335 |
cond->update_used_tables(); |
|
10336 |
||
10337 |
if (!((Item_cond*) cond)->argument_list()->elements || |
|
10338 |
*cond_value != Item::COND_OK) |
|
10339 |
return (COND*) 0; |
|
10340 |
if (((Item_cond*) cond)->argument_list()->elements == 1) |
|
10341 |
{ // Remove list |
|
10342 |
item= ((Item_cond*) cond)->argument_list()->head(); |
|
10343 |
((Item_cond*) cond)->argument_list()->empty(); |
|
10344 |
return item; |
|
10345 |
} |
|
10346 |
} |
|
10347 |
else if (cond->type() == Item::FUNC_ITEM && |
|
10348 |
((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC) |
|
10349 |
{ |
|
10350 |
/* |
|
10351 |
Handles this special case for some ODBC applications: |
|
10352 |
The are requesting the row that was just updated with a auto_increment |
|
10353 |
value with this construct: |
|
10354 |
||
10355 |
SELECT * from table_name where auto_increment_column IS NULL |
|
10356 |
This will be changed to: |
|
10357 |
SELECT * from table_name where auto_increment_column = LAST_INSERT_ID |
|
10358 |
*/ |
|
10359 |
||
10360 |
Item_func_isnull *func=(Item_func_isnull*) cond; |
|
10361 |
Item **args= func->arguments(); |
|
10362 |
if (args[0]->type() == Item::FIELD_ITEM) |
|
10363 |
{ |
|
10364 |
Field *field=((Item_field*) args[0])->field; |
|
10365 |
if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null && |
|
10366 |
(thd->options & OPTION_AUTO_IS_NULL) && |
|
10367 |
(thd->first_successful_insert_id_in_prev_stmt > 0 && |
|
10368 |
thd->substitute_null_with_insert_id)) |
|
10369 |
{ |
|
10370 |
COND *new_cond; |
|
10371 |
if ((new_cond= new Item_func_eq(args[0], |
|
10372 |
new Item_int("last_insert_id()", |
|
10373 |
thd->read_first_successful_insert_id_in_prev_stmt(), |
|
10374 |
MY_INT64_NUM_DECIMAL_DIGITS)))) |
|
10375 |
{ |
|
10376 |
cond=new_cond; |
|
10377 |
/* |
|
10378 |
Item_func_eq can't be fixed after creation so we do not check |
|
10379 |
cond->fixed, also it do not need tables so we use 0 as second |
|
10380 |
argument. |
|
10381 |
*/ |
|
10382 |
cond->fix_fields(thd, &cond); |
|
10383 |
} |
|
10384 |
/* |
|
10385 |
IS NULL should be mapped to LAST_INSERT_ID only for first row, so |
|
10386 |
clear for next row |
|
10387 |
*/ |
|
55
by brian
Update for using real bool types. |
10388 |
thd->substitute_null_with_insert_id= false; |
1
by brian
clean slate |
10389 |
} |
10390 |
/* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */ |
|
97
by Brian Aker
DATE cleanup. |
10391 |
else if (((field->type() == MYSQL_TYPE_NEWDATE) || |
1
by brian
clean slate |
10392 |
(field->type() == MYSQL_TYPE_DATETIME)) && |
10393 |
(field->flags & NOT_NULL_FLAG) && |
|
10394 |
!field->table->maybe_null) |
|
10395 |
{ |
|
10396 |
COND *new_cond; |
|
10397 |
if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2)))) |
|
10398 |
{ |
|
10399 |
cond=new_cond; |
|
10400 |
/* |
|
10401 |
Item_func_eq can't be fixed after creation so we do not check |
|
10402 |
cond->fixed, also it do not need tables so we use 0 as second |
|
10403 |
argument. |
|
10404 |
*/ |
|
10405 |
cond->fix_fields(thd, &cond); |
|
10406 |
} |
|
10407 |
} |
|
10408 |
} |
|
10409 |
if (cond->const_item()) |
|
10410 |
{ |
|
10411 |
*cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE; |
|
10412 |
return (COND*) 0; |
|
10413 |
} |
|
10414 |
} |
|
10415 |
else if (cond->const_item() && !cond->is_expensive()) |
|
10416 |
/* |
|
10417 |
TODO: |
|
10418 |
Excluding all expensive functions is too restritive we should exclude only |
|
10419 |
materialized IN because it is created later than this phase, and cannot be |
|
10420 |
evaluated at this point. |
|
10421 |
The condition should be something as (need to fix member access): |
|
10422 |
!(cond->type() == Item::FUNC_ITEM && |
|
10423 |
((Item_func*)cond)->func_name() == "<in_optimizer>" && |
|
10424 |
((Item_in_optimizer*)cond)->is_expensive())) |
|
10425 |
*/ |
|
10426 |
{ |
|
10427 |
*cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE; |
|
10428 |
return (COND*) 0; |
|
10429 |
} |
|
10430 |
else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK) |
|
10431 |
{ // boolan compare function |
|
10432 |
Item *left_item= ((Item_func*) cond)->arguments()[0]; |
|
10433 |
Item *right_item= ((Item_func*) cond)->arguments()[1]; |
|
10434 |
if (left_item->eq(right_item,1)) |
|
10435 |
{ |
|
10436 |
if (!left_item->maybe_null || |
|
10437 |
((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC) |
|
10438 |
return (COND*) 0; // Compare of identical items |
|
10439 |
} |
|
10440 |
} |
|
10441 |
*cond_value=Item::COND_OK; |
|
10442 |
return cond; // Point at next and level |
|
10443 |
} |
|
10444 |
||
10445 |
/* |
|
10446 |
Check if equality can be used in removing components of GROUP BY/DISTINCT |
|
10447 |
||
10448 |
SYNOPSIS |
|
10449 |
test_if_equality_guarantees_uniqueness() |
|
10450 |
l the left comparison argument (a field if any) |
|
10451 |
r the right comparison argument (a const of any) |
|
10452 |
||
10453 |
DESCRIPTION |
|
10454 |
Checks if an equality predicate can be used to take away |
|
10455 |
DISTINCT/GROUP BY because it is known to be true for exactly one |
|
10456 |
distinct value (e.g. <expr> == <const>). |
|
10457 |
Arguments must be of the same type because e.g. |
|
10458 |
<string_field> = <int_const> may match more than 1 distinct value from |
|
10459 |
the column. |
|
10460 |
We must take into consideration and the optimization done for various |
|
10461 |
string constants when compared to dates etc (see Item_int_with_ref) as |
|
10462 |
well as the collation of the arguments. |
|
10463 |
||
10464 |
RETURN VALUE |
|
55
by brian
Update for using real bool types. |
10465 |
true can be used |
10466 |
false cannot be used |
|
1
by brian
clean slate |
10467 |
*/ |
10468 |
static bool |
|
10469 |
test_if_equality_guarantees_uniqueness(Item *l, Item *r) |
|
10470 |
{ |
|
10471 |
return r->const_item() && |
|
10472 |
/* elements must be compared as dates */ |
|
10473 |
(Arg_comparator::can_compare_as_dates(l, r, 0) || |
|
10474 |
/* or of the same result type */ |
|
10475 |
(r->result_type() == l->result_type() && |
|
10476 |
/* and must have the same collation if compared as strings */ |
|
10477 |
(l->result_type() != STRING_RESULT || |
|
10478 |
l->collation.collation == r->collation.collation))); |
|
10479 |
} |
|
10480 |
||
10481 |
/** |
|
55
by brian
Update for using real bool types. |
10482 |
Return true if the item is a const value in all the WHERE clause. |
1
by brian
clean slate |
10483 |
*/ |
10484 |
||
10485 |
static bool |
|
10486 |
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item) |
|
10487 |
{ |
|
10488 |
if (cond->type() == Item::COND_ITEM) |
|
10489 |
{ |
|
10490 |
bool and_level= (((Item_cond*) cond)->functype() |
|
10491 |
== Item_func::COND_AND_FUNC); |
|
10492 |
List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list()); |
|
10493 |
Item *item; |
|
10494 |
while ((item=li++)) |
|
10495 |
{ |
|
10496 |
bool res=const_expression_in_where(item, comp_item, const_item); |
|
10497 |
if (res) // Is a const value |
|
10498 |
{ |
|
10499 |
if (and_level) |
|
10500 |
return 1; |
|
10501 |
} |
|
10502 |
else if (!and_level) |
|
10503 |
return 0; |
|
10504 |
} |
|
10505 |
return and_level ? 0 : 1; |
|
10506 |
} |
|
10507 |
else if (cond->eq_cmp_result() != Item::COND_OK) |
|
10508 |
{ // boolan compare function |
|
10509 |
Item_func* func= (Item_func*) cond; |
|
10510 |
if (func->functype() != Item_func::EQUAL_FUNC && |
|
10511 |
func->functype() != Item_func::EQ_FUNC) |
|
10512 |
return 0; |
|
10513 |
Item *left_item= ((Item_func*) cond)->arguments()[0]; |
|
10514 |
Item *right_item= ((Item_func*) cond)->arguments()[1]; |
|
10515 |
if (left_item->eq(comp_item,1)) |
|
10516 |
{ |
|
10517 |
if (test_if_equality_guarantees_uniqueness (left_item, right_item)) |
|
10518 |
{ |
|
10519 |
if (*const_item) |
|
10520 |
return right_item->eq(*const_item, 1); |
|
10521 |
*const_item=right_item; |
|
10522 |
return 1; |
|
10523 |
} |
|
10524 |
} |
|
10525 |
else if (right_item->eq(comp_item,1)) |
|
10526 |
{ |
|
10527 |
if (test_if_equality_guarantees_uniqueness (right_item, left_item)) |
|
10528 |
{ |
|
10529 |
if (*const_item) |
|
10530 |
return left_item->eq(*const_item, 1); |
|
10531 |
*const_item=left_item; |
|
10532 |
return 1; |
|
10533 |
} |
|
10534 |
} |
|
10535 |
} |
|
10536 |
return 0; |
|
10537 |
} |
|
10538 |
||
10539 |
/**************************************************************************** |
|
10540 |
Create internal temporary table |
|
10541 |
****************************************************************************/ |
|
10542 |
||
10543 |
/** |
|
10544 |
Create field for temporary table from given field. |
|
10545 |
||
10546 |
@param thd Thread handler |
|
10547 |
@param org_field field from which new field will be created |
|
10548 |
@param name New field name |
|
10549 |
@param table Temporary table |
|
10550 |
@param item !=NULL if item->result_field should point to new field. |
|
10551 |
This is relevant for how fill_record() is going to work: |
|
10552 |
If item != NULL then fill_record() will update |
|
10553 |
the record in the original table. |
|
10554 |
If item == NULL then fill_record() will update |
|
10555 |
the temporary table |
|
10556 |
@param convert_blob_length If >0 create a varstring(convert_blob_length) |
|
10557 |
field instead of blob. |
|
10558 |
||
10559 |
@retval |
|
10560 |
NULL on error |
|
10561 |
@retval |
|
10562 |
new_created field |
|
10563 |
*/ |
|
10564 |
||
10565 |
Field *create_tmp_field_from_field(THD *thd, Field *org_field, |
|
10566 |
const char *name, TABLE *table, |
|
10567 |
Item_field *item, uint convert_blob_length) |
|
10568 |
{ |
|
10569 |
Field *new_field; |
|
10570 |
||
10571 |
/* |
|
10572 |
Make sure that the blob fits into a Field_varstring which has |
|
10573 |
2-byte lenght. |
|
10574 |
*/ |
|
10575 |
if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE && |
|
10576 |
(org_field->flags & BLOB_FLAG)) |
|
10577 |
new_field= new Field_varstring(convert_blob_length, |
|
10578 |
org_field->maybe_null(), |
|
10579 |
org_field->field_name, table->s, |
|
10580 |
org_field->charset()); |
|
10581 |
else |
|
10582 |
new_field= org_field->new_field(thd->mem_root, table, |
|
10583 |
table == org_field->table); |
|
10584 |
if (new_field) |
|
10585 |
{ |
|
10586 |
new_field->init(table); |
|
10587 |
new_field->orig_table= org_field->orig_table; |
|
10588 |
if (item) |
|
10589 |
item->result_field= new_field; |
|
10590 |
else |
|
10591 |
new_field->field_name= name; |
|
10592 |
new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG); |
|
10593 |
if (org_field->maybe_null() || (item && item->maybe_null)) |
|
10594 |
new_field->flags&= ~NOT_NULL_FLAG; // Because of outer join |
|
10595 |
if (org_field->type() == MYSQL_TYPE_VAR_STRING || |
|
10596 |
org_field->type() == MYSQL_TYPE_VARCHAR) |
|
10597 |
table->s->db_create_options|= HA_OPTION_PACK_RECORD; |
|
10598 |
else if (org_field->type() == FIELD_TYPE_DOUBLE) |
|
55
by brian
Update for using real bool types. |
10599 |
((Field_double *) new_field)->not_fixed= true; |
1
by brian
clean slate |
10600 |
} |
10601 |
return new_field; |
|
10602 |
} |
|
10603 |
||
10604 |
/** |
|
10605 |
Create field for temporary table using type of given item. |
|
10606 |
||
10607 |
@param thd Thread handler |
|
10608 |
@param item Item to create a field for |
|
10609 |
@param table Temporary table |
|
10610 |
@param copy_func If set and item is a function, store copy of |
|
10611 |
item in this array |
|
10612 |
@param modify_item 1 if item->result_field should point to new |
|
10613 |
item. This is relevent for how fill_record() |
|
10614 |
is going to work: |
|
10615 |
If modify_item is 1 then fill_record() will |
|
10616 |
update the record in the original table. |
|
10617 |
If modify_item is 0 then fill_record() will |
|
10618 |
update the temporary table |
|
10619 |
@param convert_blob_length If >0 create a varstring(convert_blob_length) |
|
10620 |
field instead of blob. |
|
10621 |
||
10622 |
@retval |
|
10623 |
0 on error |
|
10624 |
@retval |
|
10625 |
new_created field |
|
10626 |
*/ |
|
10627 |
||
77.1.45
by Monty Taylor
Warning fixes. |
10628 |
static Field *create_tmp_field_from_item(THD *thd __attribute__((__unused__)), |
10629 |
Item *item, TABLE *table, |
|
1
by brian
clean slate |
10630 |
Item ***copy_func, bool modify_item, |
10631 |
uint convert_blob_length) |
|
10632 |
{ |
|
10633 |
bool maybe_null= item->maybe_null; |
|
10634 |
Field *new_field; |
|
10635 |
||
10636 |
switch (item->result_type()) { |
|
10637 |
case REAL_RESULT: |
|
10638 |
new_field= new Field_double(item->max_length, maybe_null, |
|
55
by brian
Update for using real bool types. |
10639 |
item->name, item->decimals, true); |
1
by brian
clean slate |
10640 |
break; |
10641 |
case INT_RESULT: |
|
10642 |
/* |
|
10643 |
Select an integer type with the minimal fit precision. |
|
10644 |
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign. |
|
10645 |
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into |
|
152
by Brian Aker
longlong replacement |
10646 |
Field_long : make them Field_int64_t. |
1
by brian
clean slate |
10647 |
*/ |
10648 |
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1)) |
|
152
by Brian Aker
longlong replacement |
10649 |
new_field=new Field_int64_t(item->max_length, maybe_null, |
1
by brian
clean slate |
10650 |
item->name, item->unsigned_flag); |
10651 |
else |
|
10652 |
new_field=new Field_long(item->max_length, maybe_null, |
|
10653 |
item->name, item->unsigned_flag); |
|
10654 |
break; |
|
10655 |
case STRING_RESULT: |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
10656 |
assert(item->collation.collation); |
1
by brian
clean slate |
10657 |
|
10658 |
enum enum_field_types type; |
|
10659 |
/* |
|
10660 |
DATE/TIME fields have STRING_RESULT result type. |
|
10661 |
To preserve type they needed to be handled separately. |
|
10662 |
*/ |
|
10663 |
if ((type= item->field_type()) == MYSQL_TYPE_DATETIME || |
|
97
by Brian Aker
DATE cleanup. |
10664 |
type == MYSQL_TYPE_TIME || type == MYSQL_TYPE_NEWDATE || |
1
by brian
clean slate |
10665 |
type == MYSQL_TYPE_TIMESTAMP) |
10666 |
new_field= item->tmp_table_field_from_field_type(table, 1); |
|
10667 |
/* |
|
10668 |
Make sure that the blob fits into a Field_varstring which has |
|
10669 |
2-byte lenght. |
|
10670 |
*/ |
|
10671 |
else if (item->max_length/item->collation.collation->mbmaxlen > 255 && |
|
10672 |
convert_blob_length <= Field_varstring::MAX_SIZE && |
|
10673 |
convert_blob_length) |
|
10674 |
new_field= new Field_varstring(convert_blob_length, maybe_null, |
|
10675 |
item->name, table->s, |
|
10676 |
item->collation.collation); |
|
10677 |
else |
|
10678 |
new_field= item->make_string_field(table); |
|
10679 |
new_field->set_derivation(item->collation.derivation); |
|
10680 |
break; |
|
10681 |
case DECIMAL_RESULT: |
|
10682 |
{ |
|
10683 |
uint8 dec= item->decimals; |
|
10684 |
uint8 intg= ((Item_decimal *) item)->decimal_precision() - dec; |
|
10685 |
uint32 len= item->max_length; |
|
10686 |
||
10687 |
/* |
|
10688 |
Trying to put too many digits overall in a DECIMAL(prec,dec) |
|
10689 |
will always throw a warning. We must limit dec to |
|
10690 |
DECIMAL_MAX_SCALE however to prevent an assert() later. |
|
10691 |
*/ |
|
10692 |
||
10693 |
if (dec > 0) |
|
10694 |
{ |
|
10695 |
signed int overflow; |
|
10696 |
||
10697 |
dec= min(dec, DECIMAL_MAX_SCALE); |
|
10698 |
||
10699 |
/* |
|
10700 |
If the value still overflows the field with the corrected dec, |
|
10701 |
we'll throw out decimals rather than integers. This is still |
|
10702 |
bad and of course throws a truncation warning. |
|
10703 |
+1: for decimal point |
|
10704 |
*/ |
|
10705 |
||
10706 |
overflow= my_decimal_precision_to_length(intg + dec, dec, |
|
10707 |
item->unsigned_flag) - len; |
|
10708 |
||
10709 |
if (overflow > 0) |
|
10710 |
dec= max(0, dec - overflow); // too long, discard fract |
|
10711 |
else |
|
10712 |
len -= item->decimals - dec; // corrected value fits |
|
10713 |
} |
|
10714 |
||
10715 |
new_field= new Field_new_decimal(len, maybe_null, item->name, |
|
10716 |
dec, item->unsigned_flag); |
|
10717 |
break; |
|
10718 |
} |
|
10719 |
case ROW_RESULT: |
|
10720 |
default: |
|
10721 |
// This case should never be choosen |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
10722 |
assert(0); |
1
by brian
clean slate |
10723 |
new_field= 0; |
10724 |
break; |
|
10725 |
} |
|
10726 |
if (new_field) |
|
10727 |
new_field->init(table); |
|
10728 |
||
10729 |
if (copy_func && item->is_result_field()) |
|
10730 |
*((*copy_func)++) = item; // Save for copy_funcs |
|
10731 |
if (modify_item) |
|
10732 |
item->set_result_field(new_field); |
|
10733 |
if (item->type() == Item::NULL_ITEM) |
|
55
by brian
Update for using real bool types. |
10734 |
new_field->is_created_from_null_item= true; |
1
by brian
clean slate |
10735 |
return new_field; |
10736 |
} |
|
10737 |
||
10738 |
||
10739 |
/** |
|
10740 |
Create field for information schema table. |
|
10741 |
||
10742 |
@param thd Thread handler |
|
10743 |
@param table Temporary table |
|
10744 |
@param item Item to create a field for |
|
10745 |
||
10746 |
@retval |
|
10747 |
0 on error |
|
10748 |
@retval |
|
10749 |
new_created field |
|
10750 |
*/ |
|
10751 |
||
77.1.45
by Monty Taylor
Warning fixes. |
10752 |
Field *create_tmp_field_for_schema(THD *thd __attribute__((__unused__)), |
10753 |
Item *item, TABLE *table) |
|
1
by brian
clean slate |
10754 |
{ |
10755 |
if (item->field_type() == MYSQL_TYPE_VARCHAR) |
|
10756 |
{ |
|
10757 |
Field *field; |
|
10758 |
if (item->max_length > MAX_FIELD_VARCHARLENGTH) |
|
10759 |
field= new Field_blob(item->max_length, item->maybe_null, |
|
10760 |
item->name, item->collation.collation); |
|
10761 |
else |
|
10762 |
field= new Field_varstring(item->max_length, item->maybe_null, |
|
10763 |
item->name, |
|
10764 |
table->s, item->collation.collation); |
|
10765 |
if (field) |
|
10766 |
field->init(table); |
|
10767 |
return field; |
|
10768 |
} |
|
10769 |
return item->tmp_table_field_from_field_type(table, 0); |
|
10770 |
} |
|
10771 |
||
10772 |
||
10773 |
/** |
|
10774 |
Create field for temporary table. |
|
10775 |
||
10776 |
@param thd Thread handler |
|
10777 |
@param table Temporary table |
|
10778 |
@param item Item to create a field for |
|
10779 |
@param type Type of item (normally item->type) |
|
10780 |
@param copy_func If set and item is a function, store copy of item |
|
10781 |
in this array |
|
10782 |
@param from_field if field will be created using other field as example, |
|
10783 |
pointer example field will be written here |
|
10784 |
@param default_field If field has a default value field, store it here |
|
10785 |
@param group 1 if we are going to do a relative group by on result |
|
10786 |
@param modify_item 1 if item->result_field should point to new item. |
|
10787 |
This is relevent for how fill_record() is going to |
|
10788 |
work: |
|
10789 |
If modify_item is 1 then fill_record() will update |
|
10790 |
the record in the original table. |
|
10791 |
If modify_item is 0 then fill_record() will update |
|
10792 |
the temporary table |
|
10793 |
@param convert_blob_length If >0 create a varstring(convert_blob_length) |
|
10794 |
field instead of blob. |
|
10795 |
||
10796 |
@retval |
|
10797 |
0 on error |
|
10798 |
@retval |
|
10799 |
new_created field |
|
10800 |
*/ |
|
10801 |
||
10802 |
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type, |
|
10803 |
Item ***copy_func, Field **from_field, |
|
10804 |
Field **default_field, |
|
10805 |
bool group, bool modify_item, |
|
77.1.45
by Monty Taylor
Warning fixes. |
10806 |
bool table_cant_handle_bit_fields __attribute__((__unused__)), |
1
by brian
clean slate |
10807 |
bool make_copy_field, |
10808 |
uint convert_blob_length) |
|
10809 |
{ |
|
10810 |
Field *result; |
|
10811 |
Item::Type orig_type= type; |
|
10812 |
Item *orig_item= 0; |
|
10813 |
||
10814 |
if (type != Item::FIELD_ITEM && |
|
10815 |
item->real_item()->type() == Item::FIELD_ITEM) |
|
10816 |
{ |
|
10817 |
orig_item= item; |
|
10818 |
item= item->real_item(); |
|
10819 |
type= Item::FIELD_ITEM; |
|
10820 |
} |
|
10821 |
||
10822 |
switch (type) { |
|
10823 |
case Item::SUM_FUNC_ITEM: |
|
10824 |
{ |
|
10825 |
Item_sum *item_sum=(Item_sum*) item; |
|
10826 |
result= item_sum->create_tmp_field(group, table, convert_blob_length); |
|
10827 |
if (!result) |
|
10828 |
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR)); |
|
10829 |
return result; |
|
10830 |
} |
|
10831 |
case Item::FIELD_ITEM: |
|
10832 |
case Item::DEFAULT_VALUE_ITEM: |
|
10833 |
{ |
|
10834 |
Item_field *field= (Item_field*) item; |
|
10835 |
bool orig_modify= modify_item; |
|
10836 |
if (orig_type == Item::REF_ITEM) |
|
10837 |
modify_item= 0; |
|
10838 |
/* |
|
10839 |
If item have to be able to store NULLs but underlaid field can't do it, |
|
10840 |
create_tmp_field_from_field() can't be used for tmp field creation. |
|
10841 |
*/ |
|
10842 |
if (field->maybe_null && !field->field->maybe_null()) |
|
10843 |
{ |
|
10844 |
result= create_tmp_field_from_item(thd, item, table, NULL, |
|
10845 |
modify_item, convert_blob_length); |
|
10846 |
*from_field= field->field; |
|
10847 |
if (result && modify_item) |
|
10848 |
field->result_field= result; |
|
10849 |
} |
|
10850 |
else |
|
10851 |
result= create_tmp_field_from_field(thd, (*from_field= field->field), |
|
10852 |
orig_item ? orig_item->name : |
|
10853 |
item->name, |
|
10854 |
table, |
|
10855 |
modify_item ? field : |
|
10856 |
NULL, |
|
10857 |
convert_blob_length); |
|
10858 |
if (orig_type == Item::REF_ITEM && orig_modify) |
|
10859 |
((Item_ref*)orig_item)->set_result_field(result); |
|
10860 |
if (field->field->eq_def(result)) |
|
10861 |
*default_field= field->field; |
|
10862 |
return result; |
|
10863 |
} |
|
10864 |
/* Fall through */ |
|
10865 |
case Item::FUNC_ITEM: |
|
10866 |
/* Fall through */ |
|
10867 |
case Item::COND_ITEM: |
|
10868 |
case Item::FIELD_AVG_ITEM: |
|
10869 |
case Item::FIELD_STD_ITEM: |
|
10870 |
case Item::SUBSELECT_ITEM: |
|
10871 |
/* The following can only happen with 'CREATE TABLE ... SELECT' */ |
|
10872 |
case Item::PROC_ITEM: |
|
10873 |
case Item::INT_ITEM: |
|
10874 |
case Item::REAL_ITEM: |
|
10875 |
case Item::DECIMAL_ITEM: |
|
10876 |
case Item::STRING_ITEM: |
|
10877 |
case Item::REF_ITEM: |
|
10878 |
case Item::NULL_ITEM: |
|
10879 |
case Item::VARBIN_ITEM: |
|
10880 |
if (make_copy_field) |
|
10881 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
10882 |
assert(((Item_result_field*)item)->result_field); |
1
by brian
clean slate |
10883 |
*from_field= ((Item_result_field*)item)->result_field; |
10884 |
} |
|
10885 |
return create_tmp_field_from_item(thd, item, table, |
|
10886 |
(make_copy_field ? 0 : copy_func), |
|
10887 |
modify_item, convert_blob_length); |
|
10888 |
case Item::TYPE_HOLDER: |
|
10889 |
result= ((Item_type_holder *)item)->make_field_by_type(table); |
|
10890 |
result->set_derivation(item->collation.derivation); |
|
10891 |
return result; |
|
10892 |
default: // Dosen't have to be stored |
|
10893 |
return 0; |
|
10894 |
} |
|
10895 |
} |
|
10896 |
||
10897 |
/* |
|
10898 |
Set up column usage bitmaps for a temporary table |
|
10899 |
||
10900 |
IMPLEMENTATION |
|
10901 |
For temporary tables, we need one bitmap with all columns set and |
|
10902 |
a tmp_set bitmap to be used by things like filesort. |
|
10903 |
*/ |
|
10904 |
||
10905 |
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps) |
|
10906 |
{ |
|
10907 |
uint field_count= table->s->fields; |
|
10908 |
bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count, |
|
55
by brian
Update for using real bool types. |
10909 |
false); |
1
by brian
clean slate |
10910 |
bitmap_init(&table->tmp_set, |
10911 |
(my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)), |
|
55
by brian
Update for using real bool types. |
10912 |
field_count, false); |
1
by brian
clean slate |
10913 |
/* write_set and all_set are copies of read_set */ |
10914 |
table->def_write_set= table->def_read_set; |
|
10915 |
table->s->all_set= table->def_read_set; |
|
10916 |
bitmap_set_all(&table->s->all_set); |
|
10917 |
table->default_column_bitmaps(); |
|
10918 |
} |
|
10919 |
||
10920 |
||
10921 |
/** |
|
10922 |
Create a temp table according to a field list. |
|
10923 |
||
10924 |
Given field pointers are changed to point at tmp_table for |
|
10925 |
send_fields. The table object is self contained: it's |
|
10926 |
allocated in its own memory root, as well as Field objects |
|
10927 |
created for table columns. |
|
10928 |
This function will replace Item_sum items in 'fields' list with |
|
10929 |
corresponding Item_field items, pointing at the fields in the |
|
55
by brian
Update for using real bool types. |
10930 |
temporary table, unless this was prohibited by true |
1
by brian
clean slate |
10931 |
value of argument save_sum_fields. The Item_field objects |
10932 |
are created in THD memory root. |
|
10933 |
||
10934 |
@param thd thread handle |
|
10935 |
@param param a description used as input to create the table |
|
10936 |
@param fields list of items that will be used to define |
|
10937 |
column types of the table (also see NOTES) |
|
10938 |
@param group TODO document |
|
10939 |
@param distinct should table rows be distinct |
|
10940 |
@param save_sum_fields see NOTES |
|
10941 |
@param select_options |
|
10942 |
@param rows_limit |
|
10943 |
@param table_alias possible name of the temporary table that can |
|
10944 |
be used for name resolving; can be "". |
|
10945 |
*/ |
|
10946 |
||
10947 |
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128 |
|
10948 |
#define AVG_STRING_LENGTH_TO_PACK_ROWS 64 |
|
10949 |
#define RATIO_TO_PACK_ROWS 2 |
|
10950 |
#define MIN_STRING_LENGTH_TO_PACK_ROWS 10 |
|
10951 |
||
10952 |
TABLE * |
|
10953 |
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields, |
|
10954 |
ORDER *group, bool distinct, bool save_sum_fields, |
|
151
by Brian Aker
Ulonglong to uint64_t |
10955 |
uint64_t select_options, ha_rows rows_limit, |
1
by brian
clean slate |
10956 |
char *table_alias) |
10957 |
{ |
|
10958 |
MEM_ROOT *mem_root_save, own_root; |
|
10959 |
TABLE *table; |
|
10960 |
TABLE_SHARE *share; |
|
10961 |
uint i,field_count,null_count,null_pack_length; |
|
10962 |
uint copy_func_count= param->func_count; |
|
10963 |
uint hidden_null_count, hidden_null_pack_length, hidden_field_count; |
|
10964 |
uint blob_count,group_null_items, string_count; |
|
10965 |
uint temp_pool_slot=MY_BIT_NONE; |
|
10966 |
uint fieldnr= 0; |
|
10967 |
ulong reclength, string_total_length; |
|
10968 |
bool using_unique_constraint= 0; |
|
10969 |
bool use_packed_rows= 0; |
|
10970 |
bool not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS); |
|
10971 |
char *tmpname,path[FN_REFLEN]; |
|
10972 |
uchar *pos, *group_buff, *bitmaps; |
|
10973 |
uchar *null_flags; |
|
10974 |
Field **reg_field, **from_field, **default_field; |
|
10975 |
uint *blob_field; |
|
10976 |
Copy_field *copy=0; |
|
10977 |
KEY *keyinfo; |
|
10978 |
KEY_PART_INFO *key_part_info; |
|
10979 |
Item **copy_func; |
|
10980 |
MI_COLUMNDEF *recinfo; |
|
10981 |
uint total_uneven_bit_length= 0; |
|
10982 |
bool force_copy_fields= param->force_copy_fields; |
|
10983 |
||
10984 |
status_var_increment(thd->status_var.created_tmp_tables); |
|
10985 |
||
10986 |
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES)) |
|
10987 |
temp_pool_slot = bitmap_lock_set_next(&temp_pool); |
|
10988 |
||
10989 |
if (temp_pool_slot != MY_BIT_NONE) // we got a slot |
|
10990 |
sprintf(path, "%s_%lx_%i", tmp_file_prefix, |
|
10991 |
current_pid, temp_pool_slot); |
|
10992 |
else |
|
10993 |
{ |
|
10994 |
/* if we run out of slots or we are not using tempool */ |
|
10995 |
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid, |
|
10996 |
thd->thread_id, thd->tmp_table++); |
|
10997 |
} |
|
10998 |
||
10999 |
/* |
|
11000 |
No need to change table name to lower case as we are only creating |
|
11001 |
MyISAM or HEAP tables here |
|
11002 |
*/ |
|
11003 |
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME); |
|
11004 |
||
11005 |
||
11006 |
if (group) |
|
11007 |
{ |
|
11008 |
if (!param->quick_group) |
|
11009 |
group=0; // Can't use group key |
|
11010 |
else for (ORDER *tmp=group ; tmp ; tmp=tmp->next) |
|
11011 |
{ |
|
11012 |
/* |
|
11013 |
marker == 4 means two things: |
|
11014 |
- store NULLs in the key, and |
|
11015 |
- convert BIT fields to 64-bit long, needed because MEMORY tables |
|
11016 |
can't index BIT fields. |
|
11017 |
*/ |
|
11018 |
(*tmp->item)->marker= 4; |
|
11019 |
if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB) |
|
11020 |
using_unique_constraint=1; |
|
11021 |
} |
|
11022 |
if (param->group_length >= MAX_BLOB_WIDTH) |
|
11023 |
using_unique_constraint=1; |
|
11024 |
if (group) |
|
11025 |
distinct=0; // Can't use distinct |
|
11026 |
} |
|
11027 |
||
11028 |
field_count=param->field_count+param->func_count+param->sum_func_count; |
|
11029 |
hidden_field_count=param->hidden_field_count; |
|
11030 |
||
11031 |
/* |
|
11032 |
When loose index scan is employed as access method, it already |
|
11033 |
computes all groups and the result of all aggregate functions. We |
|
11034 |
make space for the items of the aggregate function in the list of |
|
11035 |
functions TMP_TABLE_PARAM::items_to_copy, so that the values of |
|
11036 |
these items are stored in the temporary table. |
|
11037 |
*/ |
|
11038 |
if (param->precomputed_group_by) |
|
11039 |
copy_func_count+= param->sum_func_count; |
|
11040 |
||
11041 |
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0); |
|
11042 |
||
11043 |
if (!multi_alloc_root(&own_root, |
|
11044 |
&table, sizeof(*table), |
|
11045 |
&share, sizeof(*share), |
|
11046 |
®_field, sizeof(Field*) * (field_count+1), |
|
11047 |
&default_field, sizeof(Field*) * (field_count), |
|
11048 |
&blob_field, sizeof(uint)*(field_count+1), |
|
11049 |
&from_field, sizeof(Field*)*field_count, |
|
11050 |
©_func, sizeof(*copy_func)*(copy_func_count+1), |
|
11051 |
¶m->keyinfo, sizeof(*param->keyinfo), |
|
11052 |
&key_part_info, |
|
11053 |
sizeof(*key_part_info)*(param->group_parts+1), |
|
11054 |
¶m->start_recinfo, |
|
11055 |
sizeof(*param->recinfo)*(field_count*2+4), |
|
11056 |
&tmpname, (uint) strlen(path)+1, |
|
11057 |
&group_buff, (group && ! using_unique_constraint ? |
|
11058 |
param->group_length : 0), |
|
11059 |
&bitmaps, bitmap_buffer_size(field_count)*2, |
|
11060 |
NullS)) |
|
11061 |
{ |
|
11062 |
if (temp_pool_slot != MY_BIT_NONE) |
|
11063 |
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
11064 |
return(NULL); /* purecov: inspected */ |
1
by brian
clean slate |
11065 |
} |
11066 |
/* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */ |
|
11067 |
if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count])) |
|
11068 |
{ |
|
11069 |
if (temp_pool_slot != MY_BIT_NONE) |
|
11070 |
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot); |
|
11071 |
free_root(&own_root, MYF(0)); /* purecov: inspected */ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
11072 |
return(NULL); /* purecov: inspected */ |
1
by brian
clean slate |
11073 |
} |
11074 |
param->items_to_copy= copy_func; |
|
11075 |
strmov(tmpname,path); |
|
11076 |
/* make table according to fields */ |
|
11077 |
||
11078 |
bzero((char*) table,sizeof(*table)); |
|
11079 |
bzero((char*) reg_field,sizeof(Field*)*(field_count+1)); |
|
11080 |
bzero((char*) default_field, sizeof(Field*) * (field_count)); |
|
11081 |
bzero((char*) from_field,sizeof(Field*)*field_count); |
|
11082 |
||
11083 |
table->mem_root= own_root; |
|
11084 |
mem_root_save= thd->mem_root; |
|
11085 |
thd->mem_root= &table->mem_root; |
|
11086 |
||
11087 |
table->field=reg_field; |
|
11088 |
table->alias= table_alias; |
|
11089 |
table->reginfo.lock_type=TL_WRITE; /* Will be updated */ |
|
11090 |
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE; |
|
11091 |
table->map=1; |
|
11092 |
table->temp_pool_slot = temp_pool_slot; |
|
11093 |
table->copy_blobs= 1; |
|
11094 |
table->in_use= thd; |
|
11095 |
table->quick_keys.init(); |
|
11096 |
table->covering_keys.init(); |
|
11097 |
table->keys_in_use_for_query.init(); |
|
11098 |
||
11099 |
table->s= share; |
|
11100 |
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname); |
|
11101 |
share->blob_field= blob_field; |
|
11102 |
share->blob_ptr_size= portable_sizeof_char_ptr; |
|
11103 |
share->db_low_byte_first=1; // True for HEAP and MyISAM |
|
11104 |
share->table_charset= param->table_charset; |
|
11105 |
share->primary_key= MAX_KEY; // Indicate no primary key |
|
11106 |
share->keys_for_keyread.init(); |
|
11107 |
share->keys_in_use.init(); |
|
11108 |
||
11109 |
/* Calculate which type of fields we will store in the temporary table */ |
|
11110 |
||
11111 |
reclength= string_total_length= 0; |
|
11112 |
blob_count= string_count= null_count= hidden_null_count= group_null_items= 0; |
|
11113 |
param->using_indirect_summary_function=0; |
|
11114 |
||
11115 |
List_iterator_fast<Item> li(fields); |
|
11116 |
Item *item; |
|
11117 |
Field **tmp_from_field=from_field; |
|
11118 |
while ((item=li++)) |
|
11119 |
{ |
|
11120 |
Item::Type type=item->type(); |
|
11121 |
if (not_all_columns) |
|
11122 |
{ |
|
11123 |
if (item->with_sum_func && type != Item::SUM_FUNC_ITEM) |
|
11124 |
{ |
|
11125 |
if (item->used_tables() & OUTER_REF_TABLE_BIT) |
|
11126 |
item->update_used_tables(); |
|
11127 |
if (type == Item::SUBSELECT_ITEM || |
|
11128 |
(item->used_tables() & ~OUTER_REF_TABLE_BIT)) |
|
11129 |
{ |
|
11130 |
/* |
|
11131 |
Mark that the we have ignored an item that refers to a summary |
|
11132 |
function. We need to know this if someone is going to use |
|
11133 |
DISTINCT on the result. |
|
11134 |
*/ |
|
11135 |
param->using_indirect_summary_function=1; |
|
11136 |
continue; |
|
11137 |
} |
|
11138 |
} |
|
11139 |
if (item->const_item() && (int) hidden_field_count <= 0) |
|
11140 |
continue; // We don't have to store this |
|
11141 |
} |
|
11142 |
if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields) |
|
11143 |
{ /* Can't calc group yet */ |
|
11144 |
((Item_sum*) item)->result_field=0; |
|
11145 |
for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++) |
|
11146 |
{ |
|
11147 |
Item **argp= ((Item_sum*) item)->args + i; |
|
11148 |
Item *arg= *argp; |
|
11149 |
if (!arg->const_item()) |
|
11150 |
{ |
|
11151 |
Field *new_field= |
|
11152 |
create_tmp_field(thd, table, arg, arg->type(), ©_func, |
|
11153 |
tmp_from_field, &default_field[fieldnr], |
|
11154 |
group != 0,not_all_columns, |
|
11155 |
distinct, 0, |
|
11156 |
param->convert_blob_length); |
|
11157 |
if (!new_field) |
|
11158 |
goto err; // Should be OOM |
|
11159 |
tmp_from_field++; |
|
11160 |
reclength+=new_field->pack_length(); |
|
11161 |
if (new_field->flags & BLOB_FLAG) |
|
11162 |
{ |
|
11163 |
*blob_field++= fieldnr; |
|
11164 |
blob_count++; |
|
11165 |
} |
|
11166 |
*(reg_field++)= new_field; |
|
11167 |
if (new_field->real_type() == MYSQL_TYPE_STRING || |
|
11168 |
new_field->real_type() == MYSQL_TYPE_VARCHAR) |
|
11169 |
{ |
|
11170 |
string_count++; |
|
11171 |
string_total_length+= new_field->pack_length(); |
|
11172 |
} |
|
11173 |
thd->mem_root= mem_root_save; |
|
11174 |
thd->change_item_tree(argp, new Item_field(new_field)); |
|
11175 |
thd->mem_root= &table->mem_root; |
|
11176 |
if (!(new_field->flags & NOT_NULL_FLAG)) |
|
11177 |
{ |
|
11178 |
null_count++; |
|
11179 |
/* |
|
11180 |
new_field->maybe_null() is still false, it will be |
|
11181 |
changed below. But we have to setup Item_field correctly |
|
11182 |
*/ |
|
11183 |
(*argp)->maybe_null=1; |
|
11184 |
} |
|
11185 |
new_field->field_index= fieldnr++; |
|
11186 |
} |
|
11187 |
} |
|
11188 |
} |
|
11189 |
else |
|
11190 |
{ |
|
11191 |
/* |
|
11192 |
The last parameter to create_tmp_field() is a bit tricky: |
|
11193 |
||
11194 |
We need to set it to 0 in union, to get fill_record() to modify the |
|
11195 |
temporary table. |
|
11196 |
We need to set it to 1 on multi-table-update and in select to |
|
11197 |
write rows to the temporary table. |
|
11198 |
We here distinguish between UNION and multi-table-updates by the fact |
|
11199 |
that in the later case group is set to the row pointer. |
|
11200 |
*/ |
|
11201 |
Field *new_field= (param->schema_table) ? |
|
11202 |
create_tmp_field_for_schema(thd, item, table) : |
|
11203 |
create_tmp_field(thd, table, item, type, ©_func, |
|
11204 |
tmp_from_field, &default_field[fieldnr], |
|
11205 |
group != 0, |
|
11206 |
!force_copy_fields && |
|
11207 |
(not_all_columns || group !=0), |
|
11208 |
/* |
|
11209 |
If item->marker == 4 then we force create_tmp_field |
|
11210 |
to create a 64-bit longs for BIT fields because HEAP |
|
11211 |
tables can't index BIT fields directly. We do the same |
|
11212 |
for distinct, as we want the distinct index to be |
|
11213 |
usable in this case too. |
|
11214 |
*/ |
|
11215 |
item->marker == 4 || param->bit_fields_as_long, |
|
11216 |
force_copy_fields, |
|
11217 |
param->convert_blob_length); |
|
11218 |
||
11219 |
if (!new_field) |
|
11220 |
{ |
|
11221 |
if (thd->is_fatal_error) |
|
11222 |
goto err; // Got OOM |
|
11223 |
continue; // Some kindf of const item |
|
11224 |
} |
|
11225 |
if (type == Item::SUM_FUNC_ITEM) |
|
11226 |
((Item_sum *) item)->result_field= new_field; |
|
11227 |
tmp_from_field++; |
|
11228 |
reclength+=new_field->pack_length(); |
|
11229 |
if (!(new_field->flags & NOT_NULL_FLAG)) |
|
11230 |
null_count++; |
|
11231 |
if (new_field->flags & BLOB_FLAG) |
|
11232 |
{ |
|
11233 |
*blob_field++= fieldnr; |
|
11234 |
blob_count++; |
|
11235 |
} |
|
11236 |
if (item->marker == 4 && item->maybe_null) |
|
11237 |
{ |
|
11238 |
group_null_items++; |
|
11239 |
new_field->flags|= GROUP_FLAG; |
|
11240 |
} |
|
11241 |
new_field->field_index= fieldnr++; |
|
11242 |
*(reg_field++)= new_field; |
|
11243 |
} |
|
11244 |
if (!--hidden_field_count) |
|
11245 |
{ |
|
11246 |
/* |
|
11247 |
This was the last hidden field; Remember how many hidden fields could |
|
11248 |
have null |
|
11249 |
*/ |
|
11250 |
hidden_null_count=null_count; |
|
11251 |
/* |
|
11252 |
We need to update hidden_field_count as we may have stored group |
|
11253 |
functions with constant arguments |
|
11254 |
*/ |
|
11255 |
param->hidden_field_count= fieldnr; |
|
11256 |
null_count= 0; |
|
11257 |
} |
|
11258 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
11259 |
assert(fieldnr == (uint) (reg_field - table->field)); |
11260 |
assert(field_count >= (uint) (reg_field - table->field)); |
|
1
by brian
clean slate |
11261 |
field_count= fieldnr; |
11262 |
*reg_field= 0; |
|
11263 |
*blob_field= 0; // End marker |
|
11264 |
share->fields= field_count; |
|
11265 |
||
11266 |
/* If result table is small; use a heap */ |
|
11267 |
/* future: storage engine selection can be made dynamic? */ |
|
11268 |
if (blob_count || using_unique_constraint || |
|
11269 |
(select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) == |
|
11270 |
OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM)) |
|
11271 |
{ |
|
11272 |
share->db_plugin= ha_lock_engine(0, myisam_hton); |
|
11273 |
table->file= get_new_handler(share, &table->mem_root, |
|
11274 |
share->db_type()); |
|
11275 |
if (group && |
|
11276 |
(param->group_parts > table->file->max_key_parts() || |
|
11277 |
param->group_length > table->file->max_key_length())) |
|
11278 |
using_unique_constraint=1; |
|
11279 |
} |
|
11280 |
else |
|
11281 |
{ |
|
11282 |
share->db_plugin= ha_lock_engine(0, heap_hton); |
|
11283 |
table->file= get_new_handler(share, &table->mem_root, |
|
11284 |
share->db_type()); |
|
11285 |
} |
|
11286 |
if (!table->file) |
|
11287 |
goto err; |
|
11288 |
||
11289 |
||
11290 |
if (!using_unique_constraint) |
|
11291 |
reclength+= group_null_items; // null flag is stored separately |
|
11292 |
||
11293 |
share->blob_fields= blob_count; |
|
11294 |
if (blob_count == 0) |
|
11295 |
{ |
|
11296 |
/* We need to ensure that first byte is not 0 for the delete link */ |
|
11297 |
if (param->hidden_field_count) |
|
11298 |
hidden_null_count++; |
|
11299 |
else |
|
11300 |
null_count++; |
|
11301 |
} |
|
11302 |
hidden_null_pack_length=(hidden_null_count+7)/8; |
|
11303 |
null_pack_length= (hidden_null_pack_length + |
|
11304 |
(null_count + total_uneven_bit_length + 7) / 8); |
|
11305 |
reclength+=null_pack_length; |
|
11306 |
if (!reclength) |
|
11307 |
reclength=1; // Dummy select |
|
11308 |
/* Use packed rows if there is blobs or a lot of space to gain */ |
|
11309 |
if (blob_count || ((string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS) && (reclength / string_total_length <= RATIO_TO_PACK_ROWS || (string_total_length / string_count) >= AVG_STRING_LENGTH_TO_PACK_ROWS))) |
|
11310 |
use_packed_rows= 1; |
|
11311 |
||
11312 |
share->reclength= reclength; |
|
11313 |
{ |
|
11314 |
uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1); |
|
11315 |
share->rec_buff_length= alloc_length; |
|
11316 |
if (!(table->record[0]= (uchar*) |
|
11317 |
alloc_root(&table->mem_root, alloc_length*3))) |
|
11318 |
goto err; |
|
11319 |
table->record[1]= table->record[0]+alloc_length; |
|
11320 |
share->default_values= table->record[1]+alloc_length; |
|
11321 |
} |
|
11322 |
copy_func[0]=0; // End marker |
|
11323 |
param->func_count= copy_func - param->items_to_copy; |
|
11324 |
||
11325 |
setup_tmp_table_column_bitmaps(table, bitmaps); |
|
11326 |
||
11327 |
recinfo=param->start_recinfo; |
|
11328 |
null_flags=(uchar*) table->record[0]; |
|
11329 |
pos=table->record[0]+ null_pack_length; |
|
11330 |
if (null_pack_length) |
|
11331 |
{ |
|
11332 |
bzero((uchar*) recinfo,sizeof(*recinfo)); |
|
11333 |
recinfo->type=FIELD_NORMAL; |
|
11334 |
recinfo->length=null_pack_length; |
|
11335 |
recinfo++; |
|
11336 |
bfill(null_flags,null_pack_length,255); // Set null fields |
|
11337 |
||
11338 |
table->null_flags= (uchar*) table->record[0]; |
|
11339 |
share->null_fields= null_count+ hidden_null_count; |
|
11340 |
share->null_bytes= null_pack_length; |
|
11341 |
} |
|
11342 |
null_count= (blob_count == 0) ? 1 : 0; |
|
11343 |
hidden_field_count=param->hidden_field_count; |
|
11344 |
for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++) |
|
11345 |
{ |
|
11346 |
Field *field= *reg_field; |
|
11347 |
uint length; |
|
11348 |
bzero((uchar*) recinfo,sizeof(*recinfo)); |
|
11349 |
||
11350 |
if (!(field->flags & NOT_NULL_FLAG)) |
|
11351 |
{ |
|
11352 |
if (field->flags & GROUP_FLAG && !using_unique_constraint) |
|
11353 |
{ |
|
11354 |
/* |
|
11355 |
We have to reserve one byte here for NULL bits, |
|
11356 |
as this is updated by 'end_update()' |
|
11357 |
*/ |
|
11358 |
*pos++=0; // Null is stored here |
|
11359 |
recinfo->length=1; |
|
11360 |
recinfo->type=FIELD_NORMAL; |
|
11361 |
recinfo++; |
|
11362 |
bzero((uchar*) recinfo,sizeof(*recinfo)); |
|
11363 |
} |
|
11364 |
else |
|
11365 |
{ |
|
11366 |
recinfo->null_bit= 1 << (null_count & 7); |
|
11367 |
recinfo->null_pos= null_count/8; |
|
11368 |
} |
|
11369 |
field->move_field(pos,null_flags+null_count/8, |
|
11370 |
1 << (null_count & 7)); |
|
11371 |
null_count++; |
|
11372 |
} |
|
11373 |
else |
|
11374 |
field->move_field(pos,(uchar*) 0,0); |
|
11375 |
field->reset(); |
|
11376 |
||
11377 |
/* |
|
11378 |
Test if there is a default field value. The test for ->ptr is to skip |
|
11379 |
'offset' fields generated by initalize_tables |
|
11380 |
*/ |
|
11381 |
if (default_field[i] && default_field[i]->ptr) |
|
11382 |
{ |
|
11383 |
/* |
|
11384 |
default_field[i] is set only in the cases when 'field' can |
|
11385 |
inherit the default value that is defined for the field referred |
|
11386 |
by the Item_field object from which 'field' has been created. |
|
11387 |
*/ |
|
11388 |
my_ptrdiff_t diff; |
|
11389 |
Field *orig_field= default_field[i]; |
|
11390 |
/* Get the value from default_values */ |
|
11391 |
diff= (my_ptrdiff_t) (orig_field->table->s->default_values- |
|
11392 |
orig_field->table->record[0]); |
|
11393 |
orig_field->move_field_offset(diff); // Points now at default_values |
|
11394 |
if (orig_field->is_real_null()) |
|
11395 |
field->set_null(); |
|
11396 |
else |
|
11397 |
{ |
|
11398 |
field->set_notnull(); |
|
11399 |
memcpy(field->ptr, orig_field->ptr, field->pack_length()); |
|
11400 |
} |
|
11401 |
orig_field->move_field_offset(-diff); // Back to record[0] |
|
11402 |
} |
|
11403 |
||
11404 |
if (from_field[i]) |
|
11405 |
{ /* Not a table Item */ |
|
11406 |
copy->set(field,from_field[i],save_sum_fields); |
|
11407 |
copy++; |
|
11408 |
} |
|
11409 |
length=field->pack_length(); |
|
11410 |
pos+= length; |
|
11411 |
||
11412 |
/* Make entry for create table */ |
|
11413 |
recinfo->length=length; |
|
11414 |
if (field->flags & BLOB_FLAG) |
|
11415 |
recinfo->type= (int) FIELD_BLOB; |
|
11416 |
else if (use_packed_rows && |
|
11417 |
field->real_type() == MYSQL_TYPE_STRING && |
|
11418 |
length >= MIN_STRING_LENGTH_TO_PACK_ROWS) |
|
11419 |
recinfo->type=FIELD_SKIP_ENDSPACE; |
|
11420 |
else |
|
11421 |
recinfo->type=FIELD_NORMAL; |
|
11422 |
if (!--hidden_field_count) |
|
11423 |
null_count=(null_count+7) & ~7; // move to next byte |
|
11424 |
||
11425 |
// fix table name in field entry |
|
11426 |
field->table_name= &table->alias; |
|
11427 |
} |
|
11428 |
||
11429 |
param->copy_field_end=copy; |
|
11430 |
param->recinfo=recinfo; |
|
11431 |
store_record(table,s->default_values); // Make empty default record |
|
11432 |
||
151
by Brian Aker
Ulonglong to uint64_t |
11433 |
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit |
1
by brian
clean slate |
11434 |
share->max_rows= ~(ha_rows) 0; |
11435 |
else |
|
11436 |
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ? |
|
11437 |
min(thd->variables.tmp_table_size, |
|
11438 |
thd->variables.max_heap_table_size) : |
|
11439 |
thd->variables.tmp_table_size) / |
|
11440 |
share->reclength); |
|
11441 |
set_if_bigger(share->max_rows,1); // For dummy start options |
|
11442 |
/* |
|
11443 |
Push the LIMIT clause to the temporary table creation, so that we |
|
11444 |
materialize only up to 'rows_limit' records instead of all result records. |
|
11445 |
*/ |
|
11446 |
set_if_smaller(share->max_rows, rows_limit); |
|
11447 |
param->end_write_records= rows_limit; |
|
11448 |
||
11449 |
keyinfo= param->keyinfo; |
|
11450 |
||
11451 |
if (group) |
|
11452 |
{ |
|
11453 |
table->group=group; /* Table is grouped by key */ |
|
11454 |
param->group_buff=group_buff; |
|
11455 |
share->keys=1; |
|
11456 |
share->uniques= test(using_unique_constraint); |
|
11457 |
table->key_info=keyinfo; |
|
11458 |
keyinfo->key_part=key_part_info; |
|
11459 |
keyinfo->flags=HA_NOSAME; |
|
11460 |
keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts; |
|
11461 |
keyinfo->key_length=0; |
|
11462 |
keyinfo->rec_per_key=0; |
|
11463 |
keyinfo->algorithm= HA_KEY_ALG_UNDEF; |
|
11464 |
keyinfo->name= (char*) "group_key"; |
|
11465 |
ORDER *cur_group= group; |
|
11466 |
for (; cur_group ; cur_group= cur_group->next, key_part_info++) |
|
11467 |
{ |
|
11468 |
Field *field=(*cur_group->item)->get_tmp_table_field(); |
|
11469 |
bool maybe_null=(*cur_group->item)->maybe_null; |
|
11470 |
key_part_info->null_bit=0; |
|
11471 |
key_part_info->field= field; |
|
11472 |
key_part_info->offset= field->offset(table->record[0]); |
|
11473 |
key_part_info->length= (uint16) field->key_length(); |
|
11474 |
key_part_info->type= (uint8) field->key_type(); |
|
11475 |
key_part_info->key_type = |
|
11476 |
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT || |
|
11477 |
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 || |
|
11478 |
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ? |
|
11479 |
0 : FIELDFLAG_BINARY; |
|
11480 |
if (!using_unique_constraint) |
|
11481 |
{ |
|
11482 |
cur_group->buff=(char*) group_buff; |
|
11483 |
if (!(cur_group->field= field->new_key_field(thd->mem_root,table, |
|
11484 |
group_buff + |
|
11485 |
test(maybe_null), |
|
11486 |
field->null_ptr, |
|
11487 |
field->null_bit))) |
|
11488 |
goto err; /* purecov: inspected */ |
|
11489 |
if (maybe_null) |
|
11490 |
{ |
|
11491 |
/* |
|
11492 |
To be able to group on NULL, we reserved place in group_buff |
|
11493 |
for the NULL flag just before the column. (see above). |
|
11494 |
The field data is after this flag. |
|
11495 |
The NULL flag is updated in 'end_update()' and 'end_write()' |
|
11496 |
*/ |
|
11497 |
keyinfo->flags|= HA_NULL_ARE_EQUAL; // def. that NULL == NULL |
|
11498 |
key_part_info->null_bit=field->null_bit; |
|
11499 |
key_part_info->null_offset= (uint) (field->null_ptr - |
|
11500 |
(uchar*) table->record[0]); |
|
11501 |
cur_group->buff++; // Pointer to field data |
|
11502 |
group_buff++; // Skipp null flag |
|
11503 |
} |
|
11504 |
/* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */ |
|
11505 |
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; |
|
11506 |
group_buff+= cur_group->field->pack_length(); |
|
11507 |
} |
|
11508 |
keyinfo->key_length+= key_part_info->length; |
|
11509 |
} |
|
11510 |
} |
|
11511 |
||
11512 |
if (distinct && field_count != param->hidden_field_count) |
|
11513 |
{ |
|
11514 |
/* |
|
11515 |
Create an unique key or an unique constraint over all columns |
|
11516 |
that should be in the result. In the temporary table, there are |
|
11517 |
'param->hidden_field_count' extra columns, whose null bits are stored |
|
11518 |
in the first 'hidden_null_pack_length' bytes of the row. |
|
11519 |
*/ |
|
11520 |
if (blob_count) |
|
11521 |
{ |
|
11522 |
/* |
|
11523 |
Special mode for index creation in MyISAM used to support unique |
|
11524 |
indexes on blobs with arbitrary length. Such indexes cannot be |
|
11525 |
used for lookups. |
|
11526 |
*/ |
|
11527 |
share->uniques= 1; |
|
11528 |
} |
|
11529 |
null_pack_length-=hidden_null_pack_length; |
|
11530 |
keyinfo->key_parts= ((field_count-param->hidden_field_count)+ |
|
11531 |
(share->uniques ? test(null_pack_length) : 0)); |
|
11532 |
table->distinct= 1; |
|
11533 |
share->keys= 1; |
|
11534 |
if (!(key_part_info= (KEY_PART_INFO*) |
|
11535 |
alloc_root(&table->mem_root, |
|
11536 |
keyinfo->key_parts * sizeof(KEY_PART_INFO)))) |
|
11537 |
goto err; |
|
11538 |
bzero((void*) key_part_info, keyinfo->key_parts * sizeof(KEY_PART_INFO)); |
|
11539 |
table->key_info=keyinfo; |
|
11540 |
keyinfo->key_part=key_part_info; |
|
11541 |
keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL; |
|
11542 |
keyinfo->key_length=(uint16) reclength; |
|
11543 |
keyinfo->name= (char*) "distinct_key"; |
|
11544 |
keyinfo->algorithm= HA_KEY_ALG_UNDEF; |
|
11545 |
keyinfo->rec_per_key=0; |
|
11546 |
||
11547 |
/* |
|
11548 |
Create an extra field to hold NULL bits so that unique indexes on |
|
11549 |
blobs can distinguish NULL from 0. This extra field is not needed |
|
11550 |
when we do not use UNIQUE indexes for blobs. |
|
11551 |
*/ |
|
11552 |
if (null_pack_length && share->uniques) |
|
11553 |
{ |
|
11554 |
key_part_info->null_bit=0; |
|
11555 |
key_part_info->offset=hidden_null_pack_length; |
|
11556 |
key_part_info->length=null_pack_length; |
|
11557 |
key_part_info->field= new Field_string(table->record[0], |
|
11558 |
(uint32) key_part_info->length, |
|
11559 |
(uchar*) 0, |
|
11560 |
(uint) 0, |
|
11561 |
Field::NONE, |
|
11562 |
NullS, &my_charset_bin); |
|
11563 |
if (!key_part_info->field) |
|
11564 |
goto err; |
|
11565 |
key_part_info->field->init(table); |
|
11566 |
key_part_info->key_type=FIELDFLAG_BINARY; |
|
11567 |
key_part_info->type= HA_KEYTYPE_BINARY; |
|
11568 |
key_part_info++; |
|
11569 |
} |
|
11570 |
/* Create a distinct key over the columns we are going to return */ |
|
11571 |
for (i=param->hidden_field_count, reg_field=table->field + i ; |
|
11572 |
i < field_count; |
|
11573 |
i++, reg_field++, key_part_info++) |
|
11574 |
{ |
|
11575 |
key_part_info->null_bit=0; |
|
11576 |
key_part_info->field= *reg_field; |
|
11577 |
key_part_info->offset= (*reg_field)->offset(table->record[0]); |
|
11578 |
key_part_info->length= (uint16) (*reg_field)->pack_length(); |
|
11579 |
/* TODO: |
|
11580 |
The below method of computing the key format length of the |
|
11581 |
key part is a copy/paste from opt_range.cc, and table.cc. |
|
11582 |
This should be factored out, e.g. as a method of Field. |
|
11583 |
In addition it is not clear if any of the Field::*_length |
|
11584 |
methods is supposed to compute the same length. If so, it |
|
11585 |
might be reused. |
|
11586 |
*/ |
|
11587 |
key_part_info->store_length= key_part_info->length; |
|
11588 |
||
11589 |
if ((*reg_field)->real_maybe_null()) |
|
11590 |
key_part_info->store_length+= HA_KEY_NULL_LENGTH; |
|
11591 |
if ((*reg_field)->type() == MYSQL_TYPE_BLOB || |
|
11592 |
(*reg_field)->real_type() == MYSQL_TYPE_VARCHAR) |
|
11593 |
key_part_info->store_length+= HA_KEY_BLOB_LENGTH; |
|
11594 |
||
11595 |
key_part_info->type= (uint8) (*reg_field)->key_type(); |
|
11596 |
key_part_info->key_type = |
|
11597 |
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT || |
|
11598 |
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 || |
|
11599 |
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ? |
|
11600 |
0 : FIELDFLAG_BINARY; |
|
11601 |
} |
|
11602 |
} |
|
11603 |
||
11604 |
if (thd->is_fatal_error) // If end of memory |
|
11605 |
goto err; /* purecov: inspected */ |
|
11606 |
share->db_record_offset= 1; |
|
11607 |
if (share->db_type() == myisam_hton) |
|
11608 |
{ |
|
11609 |
if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo, |
|
11610 |
¶m->recinfo, select_options)) |
|
11611 |
goto err; |
|
11612 |
} |
|
11613 |
if (open_tmp_table(table)) |
|
11614 |
goto err; |
|
11615 |
||
11616 |
thd->mem_root= mem_root_save; |
|
11617 |
||
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
11618 |
return(table); |
1
by brian
clean slate |
11619 |
|
11620 |
err: |
|
11621 |
thd->mem_root= mem_root_save; |
|
11622 |
free_tmp_table(thd,table); /* purecov: inspected */ |
|
11623 |
if (temp_pool_slot != MY_BIT_NONE) |
|
11624 |
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
11625 |
return(NULL); /* purecov: inspected */ |
1
by brian
clean slate |
11626 |
} |
11627 |
||
11628 |
||
11629 |
||
11630 |
||
11631 |
/* |
|
11632 |
Create a temporary table to weed out duplicate rowid combinations |
|
11633 |
||
11634 |
SYNOPSIS |
|
11635 |
||
11636 |
create_duplicate_weedout_tmp_table() |
|
11637 |
thd |
|
11638 |
uniq_tuple_length_arg |
|
11639 |
SJ_TMP_TABLE |
|
11640 |
||
11641 |
DESCRIPTION |
|
11642 |
Create a temporary table to weed out duplicate rowid combinations. The |
|
11643 |
table has a single column that is a concatenation of all rowids in the |
|
11644 |
combination. |
|
11645 |
||
11646 |
Depending on the needed length, there are two cases: |
|
11647 |
||
11648 |
1. When the length of the column < max_key_length: |
|
11649 |
||
11650 |
CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col)); |
|
11651 |
||
11652 |
2. Otherwise (not a valid SQL syntax but internally supported): |
|
11653 |
||
11654 |
CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col)); |
|
11655 |
||
11656 |
The code in this function was produced by extraction of relevant parts |
|
11657 |
from create_tmp_table(). |
|
11658 |
||
11659 |
RETURN |
|
11660 |
created table |
|
11661 |
NULL on error |
|
11662 |
*/ |
|
11663 |
||
11664 |
TABLE *create_duplicate_weedout_tmp_table(THD *thd, |
|
11665 |
uint uniq_tuple_length_arg, |
|
11666 |
SJ_TMP_TABLE *sjtbl) |
|
11667 |
{ |
|
11668 |
MEM_ROOT *mem_root_save, own_root; |
|
11669 |
TABLE *table; |
|
11670 |
TABLE_SHARE *share; |
|
11671 |
uint temp_pool_slot=MY_BIT_NONE; |
|
11672 |
char *tmpname,path[FN_REFLEN]; |
|
11673 |
Field **reg_field; |
|
11674 |
KEY_PART_INFO *key_part_info; |
|
11675 |
KEY *keyinfo; |
|
11676 |
uchar *group_buff; |
|
11677 |
uchar *bitmaps; |
|
11678 |
uint *blob_field; |
|
11679 |
MI_COLUMNDEF *recinfo, *start_recinfo; |
|
55
by brian
Update for using real bool types. |
11680 |
bool using_unique_constraint=false; |
11681 |
bool use_packed_rows= false; |
|
1
by brian
clean slate |
11682 |
Field *field, *key_field; |
11683 |
uint blob_count, null_pack_length, null_count; |
|
11684 |
uchar *null_flags; |
|
11685 |
uchar *pos; |
|
11686 |
||
11687 |
/* |
|
11688 |
STEP 1: Get temporary table name |
|
11689 |
*/ |
|
11690 |
statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status); |
|
11691 |
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES)) |
|
11692 |
temp_pool_slot = bitmap_lock_set_next(&temp_pool); |
|
11693 |
||
11694 |
if (temp_pool_slot != MY_BIT_NONE) // we got a slot |
|
11695 |
sprintf(path, "%s_%lx_%i", tmp_file_prefix, |
|
11696 |
current_pid, temp_pool_slot); |
|
11697 |
else |
|
11698 |
{ |
|
11699 |
/* if we run out of slots or we are not using tempool */ |
|
11700 |
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid, |
|
11701 |
thd->thread_id, thd->tmp_table++); |
|
11702 |
} |
|
11703 |
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME); |
|
11704 |
||
11705 |
/* STEP 2: Figure if we'll be using a key or blob+constraint */ |
|
11706 |
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB) |
|
55
by brian
Update for using real bool types. |
11707 |
using_unique_constraint= true; |
1
by brian
clean slate |
11708 |
|
11709 |
/* STEP 3: Allocate memory for temptable description */ |
|
11710 |
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0); |
|
11711 |
if (!multi_alloc_root(&own_root, |
|
11712 |
&table, sizeof(*table), |
|
11713 |
&share, sizeof(*share), |
|
11714 |
®_field, sizeof(Field*) * (1+1), |
|
11715 |
&blob_field, sizeof(uint)*2, |
|
11716 |
&keyinfo, sizeof(*keyinfo), |
|
11717 |
&key_part_info, sizeof(*key_part_info) * 2, |
|
11718 |
&start_recinfo, |
|
11719 |
sizeof(*recinfo)*(1*2+4), |
|
11720 |
&tmpname, (uint) strlen(path)+1, |
|
11721 |
&group_buff, (!using_unique_constraint ? |
|
11722 |
uniq_tuple_length_arg : 0), |
|
11723 |
&bitmaps, bitmap_buffer_size(1)*2, |
|
11724 |
NullS)) |
|
11725 |
{ |
|
11726 |
if (temp_pool_slot != MY_BIT_NONE) |
|
11727 |
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
11728 |
return(NULL); |
1
by brian
clean slate |
11729 |
} |
11730 |
strmov(tmpname,path); |
|
11731 |
||
11732 |
||
11733 |
/* STEP 4: Create TABLE description */ |
|
11734 |
bzero((char*) table,sizeof(*table)); |
|
11735 |
bzero((char*) reg_field,sizeof(Field*)*2); |
|
11736 |
||
11737 |
table->mem_root= own_root; |
|
11738 |
mem_root_save= thd->mem_root; |
|
11739 |
thd->mem_root= &table->mem_root; |
|
11740 |
||
11741 |
table->field=reg_field; |
|
11742 |
table->alias= "weedout-tmp"; |
|
11743 |
table->reginfo.lock_type=TL_WRITE; /* Will be updated */ |
|
11744 |
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE; |
|
11745 |
table->map=1; |
|
11746 |
table->temp_pool_slot = temp_pool_slot; |
|
11747 |
table->copy_blobs= 1; |
|
11748 |
table->in_use= thd; |
|
11749 |
table->quick_keys.init(); |
|
11750 |
table->covering_keys.init(); |
|
11751 |
table->keys_in_use_for_query.init(); |
|
11752 |
||
11753 |
table->s= share; |
|
11754 |
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname); |
|
11755 |
share->blob_field= blob_field; |
|
11756 |
share->blob_ptr_size= portable_sizeof_char_ptr; |
|
11757 |
share->db_low_byte_first=1; // True for HEAP and MyISAM |
|
11758 |
share->table_charset= NULL; |
|
11759 |
share->primary_key= MAX_KEY; // Indicate no primary key |
|
11760 |
share->keys_for_keyread.init(); |
|
11761 |
share->keys_in_use.init(); |
|
11762 |
||
11763 |
blob_count= 0; |
|
11764 |
||
11765 |
/* Create the field */ |
|
11766 |
{ |
|
11767 |
/* |
|
11768 |
For the sake of uniformity, always use Field_varstring (altough we could |
|
11769 |
use Field_string for shorter keys) |
|
11770 |
*/ |
|
55
by brian
Update for using real bool types. |
11771 |
field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share, |
1
by brian
clean slate |
11772 |
&my_charset_bin); |
11773 |
if (!field) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
11774 |
return(0); |
1
by brian
clean slate |
11775 |
field->table= table; |
11776 |
field->key_start.init(0); |
|
11777 |
field->part_of_key.init(0); |
|
11778 |
field->part_of_sortkey.init(0); |
|
11779 |
field->unireg_check= Field::NONE; |
|
11780 |
field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG); |
|
11781 |
field->reset_fields(); |
|
11782 |
field->init(table); |
|
11783 |
field->orig_table= NULL; |
|
11784 |
||
11785 |
field->field_index= 0; |
|
11786 |
||
11787 |
*(reg_field++)= field; |
|
11788 |
*blob_field= 0; |
|
11789 |
*reg_field= 0; |
|
11790 |
||
11791 |
share->fields= 1; |
|
11792 |
share->blob_fields= 0; |
|
11793 |
} |
|
11794 |
||
11795 |
uint reclength= field->pack_length(); |
|
11796 |
if (using_unique_constraint) |
|
11797 |
{ |
|
11798 |
share->db_plugin= ha_lock_engine(0, myisam_hton); |
|
11799 |
table->file= get_new_handler(share, &table->mem_root, |
|
11800 |
share->db_type()); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
11801 |
assert(uniq_tuple_length_arg <= table->file->max_key_length()); |
1
by brian
clean slate |
11802 |
} |
11803 |
else |
|
11804 |
{ |
|
11805 |
share->db_plugin= ha_lock_engine(0, heap_hton); |
|
11806 |
table->file= get_new_handler(share, &table->mem_root, |
|
11807 |
share->db_type()); |
|
11808 |
} |
|
11809 |
if (!table->file) |
|
11810 |
goto err; |
|
11811 |
||
11812 |
null_count=1; |
|
11813 |
||
11814 |
null_pack_length= 1; |
|
11815 |
reclength += null_pack_length; |
|
11816 |
||
11817 |
share->reclength= reclength; |
|
11818 |
{ |
|
11819 |
uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1); |
|
11820 |
share->rec_buff_length= alloc_length; |
|
11821 |
if (!(table->record[0]= (uchar*) |
|
11822 |
alloc_root(&table->mem_root, alloc_length*3))) |
|
11823 |
goto err; |
|
11824 |
table->record[1]= table->record[0]+alloc_length; |
|
11825 |
share->default_values= table->record[1]+alloc_length; |
|
11826 |
} |
|
11827 |
setup_tmp_table_column_bitmaps(table, bitmaps); |
|
11828 |
||
11829 |
recinfo= start_recinfo; |
|
11830 |
null_flags=(uchar*) table->record[0]; |
|
11831 |
pos=table->record[0]+ null_pack_length; |
|
11832 |
if (null_pack_length) |
|
11833 |
{ |
|
11834 |
bzero((uchar*) recinfo,sizeof(*recinfo)); |
|
11835 |
recinfo->type=FIELD_NORMAL; |
|
11836 |
recinfo->length=null_pack_length; |
|
11837 |
recinfo++; |
|
11838 |
bfill(null_flags,null_pack_length,255); // Set null fields |
|
11839 |
||
11840 |
table->null_flags= (uchar*) table->record[0]; |
|
11841 |
share->null_fields= null_count; |
|
11842 |
share->null_bytes= null_pack_length; |
|
11843 |
} |
|
11844 |
null_count=1; |
|
11845 |
||
11846 |
{ |
|
11847 |
//Field *field= *reg_field; |
|
11848 |
uint length; |
|
11849 |
bzero((uchar*) recinfo,sizeof(*recinfo)); |
|
11850 |
field->move_field(pos,(uchar*) 0,0); |
|
11851 |
||
11852 |
field->reset(); |
|
11853 |
/* |
|
11854 |
Test if there is a default field value. The test for ->ptr is to skip |
|
11855 |
'offset' fields generated by initalize_tables |
|
11856 |
*/ |
|
11857 |
// Initialize the table field: |
|
11858 |
bzero(field->ptr, field->pack_length()); |
|
11859 |
||
11860 |
length=field->pack_length(); |
|
11861 |
pos+= length; |
|
11862 |
||
11863 |
/* Make entry for create table */ |
|
11864 |
recinfo->length=length; |
|
11865 |
if (field->flags & BLOB_FLAG) |
|
11866 |
recinfo->type= (int) FIELD_BLOB; |
|
11867 |
else if (use_packed_rows && |
|
11868 |
field->real_type() == MYSQL_TYPE_STRING && |
|
11869 |
length >= MIN_STRING_LENGTH_TO_PACK_ROWS) |
|
11870 |
recinfo->type=FIELD_SKIP_ENDSPACE; |
|
11871 |
else |
|
11872 |
recinfo->type=FIELD_NORMAL; |
|
11873 |
||
11874 |
field->table_name= &table->alias; |
|
11875 |
} |
|
11876 |
||
11877 |
//param->recinfo=recinfo; |
|
11878 |
//store_record(table,s->default_values); // Make empty default record |
|
11879 |
||
151
by Brian Aker
Ulonglong to uint64_t |
11880 |
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit |
1
by brian
clean slate |
11881 |
share->max_rows= ~(ha_rows) 0; |
11882 |
else |
|
11883 |
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ? |
|
11884 |
min(thd->variables.tmp_table_size, |
|
11885 |
thd->variables.max_heap_table_size) : |
|
11886 |
thd->variables.tmp_table_size) / |
|
11887 |
share->reclength); |
|
11888 |
set_if_bigger(share->max_rows,1); // For dummy start options |
|
11889 |
||
11890 |
||
11891 |
//// keyinfo= param->keyinfo; |
|
55
by brian
Update for using real bool types. |
11892 |
if (true) |
1
by brian
clean slate |
11893 |
{ |
11894 |
share->keys=1; |
|
11895 |
share->uniques= test(using_unique_constraint); |
|
11896 |
table->key_info=keyinfo; |
|
11897 |
keyinfo->key_part=key_part_info; |
|
11898 |
keyinfo->flags=HA_NOSAME; |
|
11899 |
keyinfo->usable_key_parts= keyinfo->key_parts= 1; |
|
11900 |
keyinfo->key_length=0; |
|
11901 |
keyinfo->rec_per_key=0; |
|
11902 |
keyinfo->algorithm= HA_KEY_ALG_UNDEF; |
|
11903 |
keyinfo->name= (char*) "weedout_key"; |
|
11904 |
{ |
|
11905 |
key_part_info->null_bit=0; |
|
11906 |
key_part_info->field= field; |
|
11907 |
key_part_info->offset= field->offset(table->record[0]); |
|
11908 |
key_part_info->length= (uint16) field->key_length(); |
|
11909 |
key_part_info->type= (uint8) field->key_type(); |
|
11910 |
key_part_info->key_type = FIELDFLAG_BINARY; |
|
11911 |
if (!using_unique_constraint) |
|
11912 |
{ |
|
11913 |
if (!(key_field= field->new_key_field(thd->mem_root, table, |
|
11914 |
group_buff, |
|
11915 |
field->null_ptr, |
|
11916 |
field->null_bit))) |
|
11917 |
goto err; |
|
11918 |
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this? |
|
11919 |
} |
|
11920 |
keyinfo->key_length+= key_part_info->length; |
|
11921 |
} |
|
11922 |
} |
|
11923 |
||
11924 |
if (thd->is_fatal_error) // If end of memory |
|
11925 |
goto err; |
|
11926 |
share->db_record_offset= 1; |
|
11927 |
if (share->db_type() == myisam_hton) |
|
11928 |
{ |
|
11929 |
recinfo++; |
|
11930 |
if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0)) |
|
11931 |
goto err; |
|
11932 |
} |
|
11933 |
sjtbl->start_recinfo= start_recinfo; |
|
11934 |
sjtbl->recinfo= recinfo; |
|
11935 |
if (open_tmp_table(table)) |
|
11936 |
goto err; |
|
11937 |
||
11938 |
thd->mem_root= mem_root_save; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
11939 |
return(table); |
1
by brian
clean slate |
11940 |
|
11941 |
err: |
|
11942 |
thd->mem_root= mem_root_save; |
|
11943 |
free_tmp_table(thd,table); /* purecov: inspected */ |
|
11944 |
if (temp_pool_slot != MY_BIT_NONE) |
|
11945 |
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
11946 |
return(NULL); /* purecov: inspected */ |
1
by brian
clean slate |
11947 |
} |
11948 |
||
11949 |
/****************************************************************************/ |
|
11950 |
||
11951 |
/** |
|
11952 |
Create a reduced TABLE object with properly set up Field list from a |
|
11953 |
list of field definitions. |
|
11954 |
||
11955 |
The created table doesn't have a table handler associated with |
|
11956 |
it, has no keys, no group/distinct, no copy_funcs array. |
|
11957 |
The sole purpose of this TABLE object is to use the power of Field |
|
11958 |
class to read/write data to/from table->record[0]. Then one can store |
|
11959 |
the record in any container (RB tree, hash, etc). |
|
11960 |
The table is created in THD mem_root, so are the table's fields. |
|
11961 |
Consequently, if you don't BLOB fields, you don't need to free it. |
|
11962 |
||
11963 |
@param thd connection handle |
|
11964 |
@param field_list list of column definitions |
|
11965 |
||
11966 |
@return |
|
11967 |
0 if out of memory, TABLE object in case of success |
|
11968 |
*/ |
|
11969 |
||
11970 |
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list) |
|
11971 |
{ |
|
11972 |
uint field_count= field_list.elements; |
|
11973 |
uint blob_count= 0; |
|
11974 |
Field **field; |
|
11975 |
Create_field *cdef; /* column definition */ |
|
11976 |
uint record_length= 0; |
|
11977 |
uint null_count= 0; /* number of columns which may be null */ |
|
11978 |
uint null_pack_length; /* NULL representation array length */ |
|
11979 |
uint *blob_field; |
|
11980 |
uchar *bitmaps; |
|
11981 |
TABLE *table; |
|
11982 |
TABLE_SHARE *share; |
|
11983 |
||
11984 |
if (!multi_alloc_root(thd->mem_root, |
|
11985 |
&table, sizeof(*table), |
|
11986 |
&share, sizeof(*share), |
|
11987 |
&field, (field_count + 1) * sizeof(Field*), |
|
11988 |
&blob_field, (field_count+1) *sizeof(uint), |
|
11989 |
&bitmaps, bitmap_buffer_size(field_count)*2, |
|
11990 |
NullS)) |
|
11991 |
return 0; |
|
11992 |
||
11993 |
bzero(table, sizeof(*table)); |
|
11994 |
bzero(share, sizeof(*share)); |
|
11995 |
table->field= field; |
|
11996 |
table->s= share; |
|
11997 |
share->blob_field= blob_field; |
|
11998 |
share->fields= field_count; |
|
11999 |
share->blob_ptr_size= portable_sizeof_char_ptr; |
|
12000 |
setup_tmp_table_column_bitmaps(table, bitmaps); |
|
12001 |
||
12002 |
/* Create all fields and calculate the total length of record */ |
|
12003 |
List_iterator_fast<Create_field> it(field_list); |
|
12004 |
while ((cdef= it++)) |
|
12005 |
{ |
|
12006 |
*field= make_field(share, 0, cdef->length, |
|
12007 |
(uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0), |
|
12008 |
f_maybe_null(cdef->pack_flag) ? 1 : 0, |
|
12009 |
cdef->pack_flag, cdef->sql_type, cdef->charset, |
|
12010 |
cdef->unireg_check, |
|
12011 |
cdef->interval, cdef->field_name); |
|
12012 |
if (!*field) |
|
12013 |
goto error; |
|
12014 |
(*field)->init(table); |
|
12015 |
record_length+= (*field)->pack_length(); |
|
12016 |
if (! ((*field)->flags & NOT_NULL_FLAG)) |
|
12017 |
null_count++; |
|
12018 |
||
12019 |
if ((*field)->flags & BLOB_FLAG) |
|
12020 |
share->blob_field[blob_count++]= (uint) (field - table->field); |
|
12021 |
||
12022 |
field++; |
|
12023 |
} |
|
12024 |
*field= NULL; /* mark the end of the list */ |
|
12025 |
share->blob_field[blob_count]= 0; /* mark the end of the list */ |
|
12026 |
share->blob_fields= blob_count; |
|
12027 |
||
12028 |
null_pack_length= (null_count + 7)/8; |
|
12029 |
share->reclength= record_length + null_pack_length; |
|
12030 |
share->rec_buff_length= ALIGN_SIZE(share->reclength + 1); |
|
12031 |
table->record[0]= (uchar*) thd->alloc(share->rec_buff_length); |
|
12032 |
if (!table->record[0]) |
|
12033 |
goto error; |
|
12034 |
||
12035 |
if (null_pack_length) |
|
12036 |
{ |
|
12037 |
table->null_flags= (uchar*) table->record[0]; |
|
12038 |
share->null_fields= null_count; |
|
12039 |
share->null_bytes= null_pack_length; |
|
12040 |
} |
|
12041 |
||
12042 |
table->in_use= thd; /* field->reset() may access table->in_use */ |
|
12043 |
{ |
|
12044 |
/* Set up field pointers */ |
|
12045 |
uchar *null_pos= table->record[0]; |
|
12046 |
uchar *field_pos= null_pos + share->null_bytes; |
|
12047 |
uint null_bit= 1; |
|
12048 |
||
12049 |
for (field= table->field; *field; ++field) |
|
12050 |
{ |
|
12051 |
Field *cur_field= *field; |
|
12052 |
if ((cur_field->flags & NOT_NULL_FLAG)) |
|
12053 |
cur_field->move_field(field_pos); |
|
12054 |
else |
|
12055 |
{ |
|
12056 |
cur_field->move_field(field_pos, (uchar*) null_pos, null_bit); |
|
12057 |
null_bit<<= 1; |
|
12058 |
if (null_bit == (1 << 8)) |
|
12059 |
{ |
|
12060 |
++null_pos; |
|
12061 |
null_bit= 1; |
|
12062 |
} |
|
12063 |
} |
|
12064 |
cur_field->reset(); |
|
12065 |
||
12066 |
field_pos+= cur_field->pack_length(); |
|
12067 |
} |
|
12068 |
} |
|
12069 |
return table; |
|
12070 |
error: |
|
12071 |
for (field= table->field; *field; ++field) |
|
12072 |
delete *field; /* just invokes field destructor */ |
|
12073 |
return 0; |
|
12074 |
} |
|
12075 |
||
12076 |
||
12077 |
static bool open_tmp_table(TABLE *table) |
|
12078 |
{ |
|
12079 |
int error; |
|
12080 |
if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR, |
|
12081 |
HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE))) |
|
12082 |
{ |
|
12083 |
table->file->print_error(error,MYF(0)); /* purecov: inspected */ |
|
12084 |
table->db_stat=0; |
|
12085 |
return(1); |
|
12086 |
} |
|
12087 |
(void) table->file->extra(HA_EXTRA_QUICK); /* Faster */ |
|
12088 |
return(0); |
|
12089 |
} |
|
12090 |
||
12091 |
||
12092 |
/* |
|
12093 |
Create MyISAM temporary table |
|
12094 |
||
12095 |
SYNOPSIS |
|
12096 |
create_myisam_tmp_table() |
|
12097 |
table Table object that descrimes the table to be created |
|
12098 |
keyinfo Description of the index (there is always one index) |
|
12099 |
start_recinfo MyISAM's column descriptions |
|
12100 |
recinfo INOUT End of MyISAM's column descriptions |
|
12101 |
options Option bits |
|
12102 |
||
12103 |
DESCRIPTION |
|
12104 |
Create a MyISAM temporary table according to passed description. The is |
|
12105 |
assumed to have one unique index or constraint. |
|
12106 |
||
12107 |
The passed array or MI_COLUMNDEF structures must have this form: |
|
12108 |
||
12109 |
1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte |
|
12110 |
when there are many nullable columns) |
|
12111 |
2. Table columns |
|
12112 |
3. One free MI_COLUMNDEF element (*recinfo points here) |
|
12113 |
||
12114 |
This function may use the free element to create hash column for unique |
|
12115 |
constraint. |
|
12116 |
||
12117 |
RETURN |
|
55
by brian
Update for using real bool types. |
12118 |
false - OK |
12119 |
true - Error |
|
1
by brian
clean slate |
12120 |
*/ |
12121 |
||
12122 |
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, |
|
12123 |
MI_COLUMNDEF *start_recinfo, |
|
12124 |
MI_COLUMNDEF **recinfo, |
|
151
by Brian Aker
Ulonglong to uint64_t |
12125 |
uint64_t options) |
1
by brian
clean slate |
12126 |
{ |
12127 |
int error; |
|
12128 |
MI_KEYDEF keydef; |
|
12129 |
MI_UNIQUEDEF uniquedef; |
|
12130 |
TABLE_SHARE *share= table->s; |
|
12131 |
||
12132 |
if (share->keys) |
|
12133 |
{ // Get keys for ni_create |
|
12134 |
bool using_unique_constraint=0; |
|
12135 |
HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root, |
|
12136 |
sizeof(*seg) * keyinfo->key_parts); |
|
12137 |
if (!seg) |
|
12138 |
goto err; |
|
12139 |
||
12140 |
bzero(seg, sizeof(*seg) * keyinfo->key_parts); |
|
12141 |
if (keyinfo->key_length >= table->file->max_key_length() || |
|
12142 |
keyinfo->key_parts > table->file->max_key_parts() || |
|
12143 |
share->uniques) |
|
12144 |
{ |
|
12145 |
/* Can't create a key; Make a unique constraint instead of a key */ |
|
12146 |
share->keys= 0; |
|
12147 |
share->uniques= 1; |
|
12148 |
using_unique_constraint=1; |
|
12149 |
bzero((char*) &uniquedef,sizeof(uniquedef)); |
|
12150 |
uniquedef.keysegs=keyinfo->key_parts; |
|
12151 |
uniquedef.seg=seg; |
|
12152 |
uniquedef.null_are_equal=1; |
|
12153 |
||
12154 |
/* Create extra column for hash value */ |
|
12155 |
bzero((uchar*) *recinfo,sizeof(**recinfo)); |
|
12156 |
(*recinfo)->type= FIELD_CHECK; |
|
12157 |
(*recinfo)->length=MI_UNIQUE_HASH_LENGTH; |
|
12158 |
(*recinfo)++; |
|
12159 |
share->reclength+=MI_UNIQUE_HASH_LENGTH; |
|
12160 |
} |
|
12161 |
else |
|
12162 |
{ |
|
12163 |
/* Create an unique key */ |
|
12164 |
bzero((char*) &keydef,sizeof(keydef)); |
|
12165 |
keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY; |
|
12166 |
keydef.keysegs= keyinfo->key_parts; |
|
12167 |
keydef.seg= seg; |
|
12168 |
} |
|
12169 |
for (uint i=0; i < keyinfo->key_parts ; i++,seg++) |
|
12170 |
{ |
|
12171 |
Field *field=keyinfo->key_part[i].field; |
|
12172 |
seg->flag= 0; |
|
12173 |
seg->language= field->charset()->number; |
|
12174 |
seg->length= keyinfo->key_part[i].length; |
|
12175 |
seg->start= keyinfo->key_part[i].offset; |
|
12176 |
if (field->flags & BLOB_FLAG) |
|
12177 |
{ |
|
12178 |
seg->type= |
|
12179 |
((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ? |
|
12180 |
HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2); |
|
12181 |
seg->bit_start= (uint8)(field->pack_length() - share->blob_ptr_size); |
|
12182 |
seg->flag= HA_BLOB_PART; |
|
12183 |
seg->length=0; // Whole blob in unique constraint |
|
12184 |
} |
|
12185 |
else |
|
12186 |
{ |
|
12187 |
seg->type= keyinfo->key_part[i].type; |
|
12188 |
/* Tell handler if it can do suffic space compression */ |
|
12189 |
if (field->real_type() == MYSQL_TYPE_STRING && |
|
12190 |
keyinfo->key_part[i].length > 4) |
|
12191 |
seg->flag|= HA_SPACE_PACK; |
|
12192 |
} |
|
12193 |
if (!(field->flags & NOT_NULL_FLAG)) |
|
12194 |
{ |
|
12195 |
seg->null_bit= field->null_bit; |
|
12196 |
seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]); |
|
12197 |
/* |
|
12198 |
We are using a GROUP BY on something that contains NULL |
|
12199 |
In this case we have to tell MyISAM that two NULL should |
|
12200 |
on INSERT be regarded at the same value |
|
12201 |
*/ |
|
12202 |
if (!using_unique_constraint) |
|
12203 |
keydef.flag|= HA_NULL_ARE_EQUAL; |
|
12204 |
} |
|
12205 |
} |
|
12206 |
} |
|
12207 |
MI_CREATE_INFO create_info; |
|
12208 |
bzero((char*) &create_info,sizeof(create_info)); |
|
12209 |
||
12210 |
if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) == |
|
12211 |
OPTION_BIG_TABLES) |
|
151
by Brian Aker
Ulonglong to uint64_t |
12212 |
create_info.data_file_length= ~(uint64_t) 0; |
1
by brian
clean slate |
12213 |
|
12214 |
if ((error=mi_create(share->table_name.str, share->keys, &keydef, |
|
12215 |
(uint) (*recinfo-start_recinfo), |
|
12216 |
start_recinfo, |
|
12217 |
share->uniques, &uniquedef, |
|
12218 |
&create_info, |
|
12219 |
HA_CREATE_TMP_TABLE))) |
|
12220 |
{ |
|
12221 |
table->file->print_error(error,MYF(0)); /* purecov: inspected */ |
|
12222 |
table->db_stat=0; |
|
12223 |
goto err; |
|
12224 |
} |
|
12225 |
status_var_increment(table->in_use->status_var.created_tmp_disk_tables); |
|
12226 |
share->db_record_offset= 1; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
12227 |
return(0); |
1
by brian
clean slate |
12228 |
err: |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
12229 |
return(1); |
1
by brian
clean slate |
12230 |
} |
12231 |
||
12232 |
||
12233 |
void |
|
12234 |
free_tmp_table(THD *thd, TABLE *entry) |
|
12235 |
{ |
|
12236 |
MEM_ROOT own_root= entry->mem_root; |
|
12237 |
const char *save_proc_info; |
|
12238 |
||
12239 |
save_proc_info=thd->proc_info; |
|
12240 |
thd_proc_info(thd, "removing tmp table"); |
|
12241 |
||
12242 |
if (entry->file) |
|
12243 |
{ |
|
12244 |
if (entry->db_stat) |
|
12245 |
entry->file->ha_drop_table(entry->s->table_name.str); |
|
12246 |
else |
|
12247 |
entry->file->ha_delete_table(entry->s->table_name.str); |
|
12248 |
delete entry->file; |
|
12249 |
} |
|
12250 |
||
12251 |
/* free blobs */ |
|
12252 |
for (Field **ptr=entry->field ; *ptr ; ptr++) |
|
12253 |
(*ptr)->free(); |
|
12254 |
free_io_cache(entry); |
|
12255 |
||
12256 |
if (entry->temp_pool_slot != MY_BIT_NONE) |
|
12257 |
bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot); |
|
12258 |
||
12259 |
plugin_unlock(0, entry->s->db_plugin); |
|
12260 |
||
12261 |
free_root(&own_root, MYF(0)); /* the table is allocated in its own root */ |
|
12262 |
thd_proc_info(thd, save_proc_info); |
|
12263 |
||
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
12264 |
return; |
1
by brian
clean slate |
12265 |
} |
12266 |
||
12267 |
/** |
|
12268 |
If a HEAP table gets full, create a MyISAM table and copy all rows |
|
12269 |
to this. |
|
12270 |
*/ |
|
12271 |
||
12272 |
bool create_myisam_from_heap(THD *thd, TABLE *table, |
|
12273 |
MI_COLUMNDEF *start_recinfo, |
|
12274 |
MI_COLUMNDEF **recinfo, |
|
12275 |
int error, bool ignore_last_dupp_key_error) |
|
12276 |
{ |
|
12277 |
TABLE new_table; |
|
12278 |
TABLE_SHARE share; |
|
12279 |
const char *save_proc_info; |
|
12280 |
int write_err; |
|
12281 |
||
12282 |
if (table->s->db_type() != heap_hton || |
|
12283 |
error != HA_ERR_RECORD_FILE_FULL) |
|
12284 |
{ |
|
12285 |
table->file->print_error(error,MYF(0)); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
12286 |
return(1); |
1
by brian
clean slate |
12287 |
} |
12288 |
new_table= *table; |
|
12289 |
share= *table->s; |
|
12290 |
new_table.s= &share; |
|
12291 |
new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton); |
|
12292 |
if (!(new_table.file= get_new_handler(&share, &new_table.mem_root, |
|
12293 |
new_table.s->db_type()))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
12294 |
return(1); // End of memory |
1
by brian
clean slate |
12295 |
|
12296 |
save_proc_info=thd->proc_info; |
|
12297 |
thd_proc_info(thd, "converting HEAP to MyISAM"); |
|
12298 |
||
12299 |
if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo, |
|
12300 |
recinfo, thd->lex->select_lex.options | |
|
12301 |
thd->options)) |
|
12302 |
goto err2; |
|
12303 |
if (open_tmp_table(&new_table)) |
|
12304 |
goto err1; |
|
12305 |
if (table->file->indexes_are_disabled()) |
|
12306 |
new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL); |
|
12307 |
table->file->ha_index_or_rnd_end(); |
|
12308 |
table->file->ha_rnd_init(1); |
|
12309 |
if (table->no_rows) |
|
12310 |
{ |
|
12311 |
new_table.file->extra(HA_EXTRA_NO_ROWS); |
|
12312 |
new_table.no_rows=1; |
|
12313 |
} |
|
12314 |
||
12315 |
#ifdef TO_BE_DONE_LATER_IN_4_1 |
|
12316 |
/* |
|
12317 |
To use start_bulk_insert() (which is new in 4.1) we need to find |
|
12318 |
all places where a corresponding end_bulk_insert() should be put. |
|
12319 |
*/ |
|
12320 |
table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */ |
|
12321 |
new_table.file->ha_start_bulk_insert(table->file->stats.records); |
|
12322 |
#else |
|
12323 |
/* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */ |
|
12324 |
new_table.file->extra(HA_EXTRA_WRITE_CACHE); |
|
12325 |
#endif |
|
12326 |
||
12327 |
/* |
|
12328 |
copy all old rows from heap table to MyISAM table |
|
12329 |
This is the only code that uses record[1] to read/write but this |
|
12330 |
is safe as this is a temporary MyISAM table without timestamp/autoincrement. |
|
12331 |
*/ |
|
12332 |
while (!table->file->rnd_next(new_table.record[1])) |
|
12333 |
{ |
|
12334 |
write_err= new_table.file->ha_write_row(new_table.record[1]); |
|
12335 |
if (write_err) |
|
12336 |
goto err; |
|
12337 |
} |
|
12338 |
/* copy row that filled HEAP table */ |
|
12339 |
if ((write_err=new_table.file->ha_write_row(table->record[0]))) |
|
12340 |
{ |
|
12341 |
if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) || |
|
12342 |
!ignore_last_dupp_key_error) |
|
12343 |
goto err; |
|
12344 |
} |
|
12345 |
||
12346 |
/* remove heap table and change to use myisam table */ |
|
12347 |
(void) table->file->ha_rnd_end(); |
|
12348 |
(void) table->file->close(); // This deletes the table ! |
|
12349 |
delete table->file; |
|
12350 |
table->file=0; |
|
12351 |
plugin_unlock(0, table->s->db_plugin); |
|
12352 |
share.db_plugin= my_plugin_lock(0, &share.db_plugin); |
|
12353 |
new_table.s= table->s; // Keep old share |
|
12354 |
*table= new_table; |
|
12355 |
*table->s= share; |
|
12356 |
||
12357 |
table->file->change_table_ptr(table, table->s); |
|
12358 |
table->use_all_columns(); |
|
12359 |
if (save_proc_info) |
|
12360 |
{ |
|
12361 |
const char *new_proc_info= |
|
12362 |
(!strcmp(save_proc_info,"Copying to tmp table") ? |
|
12363 |
"Copying to tmp table on disk" : save_proc_info); |
|
12364 |
thd_proc_info(thd, new_proc_info); |
|
12365 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
12366 |
return(0); |
1
by brian
clean slate |
12367 |
|
12368 |
err: |
|
12369 |
table->file->print_error(write_err, MYF(0)); |
|
12370 |
(void) table->file->ha_rnd_end(); |
|
12371 |
(void) new_table.file->close(); |
|
12372 |
err1: |
|
12373 |
new_table.file->ha_delete_table(new_table.s->table_name.str); |
|
12374 |
err2: |
|
12375 |
delete new_table.file; |
|
12376 |
thd_proc_info(thd, save_proc_info); |
|
12377 |
table->mem_root= new_table.mem_root; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
12378 |
return(1); |
1
by brian
clean slate |
12379 |
} |
12380 |
||
12381 |
||
12382 |
/** |
|
12383 |
@details |
|
12384 |
Rows produced by a join sweep may end up in a temporary table or be sent |
|
12385 |
to a client. Setup the function of the nested loop join algorithm which |
|
12386 |
handles final fully constructed and matched records. |
|
12387 |
||
12388 |
@param join join to setup the function for. |
|
12389 |
||
12390 |
@return |
|
12391 |
end_select function to use. This function can't fail. |
|
12392 |
*/ |
|
12393 |
||
12394 |
Next_select_func setup_end_select_func(JOIN *join) |
|
12395 |
{ |
|
12396 |
TABLE *table= join->tmp_table; |
|
12397 |
TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param; |
|
12398 |
Next_select_func end_select; |
|
12399 |
||
12400 |
/* Set up select_end */ |
|
12401 |
if (table) |
|
12402 |
{ |
|
12403 |
if (table->group && tmp_tbl->sum_func_count && |
|
12404 |
!tmp_tbl->precomputed_group_by) |
|
12405 |
{ |
|
12406 |
if (table->s->keys) |
|
12407 |
{ |
|
12408 |
end_select=end_update; |
|
12409 |
} |
|
12410 |
else |
|
12411 |
{ |
|
12412 |
end_select=end_unique_update; |
|
12413 |
} |
|
12414 |
} |
|
12415 |
else if (join->sort_and_group && !tmp_tbl->precomputed_group_by) |
|
12416 |
{ |
|
12417 |
end_select=end_write_group; |
|
12418 |
} |
|
12419 |
else |
|
12420 |
{ |
|
12421 |
end_select=end_write; |
|
12422 |
if (tmp_tbl->precomputed_group_by) |
|
12423 |
{ |
|
12424 |
/* |
|
12425 |
A preceding call to create_tmp_table in the case when loose |
|
12426 |
index scan is used guarantees that |
|
12427 |
TMP_TABLE_PARAM::items_to_copy has enough space for the group |
|
12428 |
by functions. It is OK here to use memcpy since we copy |
|
12429 |
Item_sum pointers into an array of Item pointers. |
|
12430 |
*/ |
|
12431 |
memcpy(tmp_tbl->items_to_copy + tmp_tbl->func_count, |
|
12432 |
join->sum_funcs, |
|
12433 |
sizeof(Item*)*tmp_tbl->sum_func_count); |
|
12434 |
tmp_tbl->items_to_copy[tmp_tbl->func_count+tmp_tbl->sum_func_count]= 0; |
|
12435 |
} |
|
12436 |
} |
|
12437 |
} |
|
12438 |
else |
|
12439 |
{ |
|
12440 |
if ((join->sort_and_group) && |
|
12441 |
!tmp_tbl->precomputed_group_by) |
|
12442 |
end_select= end_send_group; |
|
12443 |
else |
|
12444 |
end_select= end_send; |
|
12445 |
} |
|
12446 |
return end_select; |
|
12447 |
} |
|
12448 |
||
12449 |
||
12450 |
/** |
|
12451 |
Make a join of all tables and write it on socket or to table. |
|
12452 |
||
12453 |
@retval |
|
12454 |
0 if ok |
|
12455 |
@retval |
|
12456 |
1 if error is sent |
|
12457 |
@retval |
|
12458 |
-1 if error should be sent |
|
12459 |
*/ |
|
12460 |
||
12461 |
static int |
|
12462 |
do_select(JOIN *join,List<Item> *fields,TABLE *table) |
|
12463 |
{ |
|
12464 |
int rc= 0; |
|
12465 |
enum_nested_loop_state error= NESTED_LOOP_OK; |
|
12466 |
JOIN_TAB *join_tab= NULL; |
|
12467 |
||
12468 |
join->tmp_table= table; /* Save for easy recursion */ |
|
12469 |
join->fields= fields; |
|
12470 |
||
12471 |
if (table) |
|
12472 |
{ |
|
12473 |
VOID(table->file->extra(HA_EXTRA_WRITE_CACHE)); |
|
12474 |
empty_record(table); |
|
12475 |
if (table->group && join->tmp_table_param.sum_func_count && |
|
12476 |
table->s->keys && !table->file->inited) |
|
12477 |
table->file->ha_index_init(0, 0); |
|
12478 |
} |
|
12479 |
/* Set up select_end */ |
|
12480 |
Next_select_func end_select= setup_end_select_func(join); |
|
12481 |
if (join->tables) |
|
12482 |
{ |
|
12483 |
join->join_tab[join->tables-1].next_select= end_select; |
|
12484 |
||
12485 |
join_tab=join->join_tab+join->const_tables; |
|
12486 |
} |
|
12487 |
join->send_records=0; |
|
12488 |
if (join->tables == join->const_tables) |
|
12489 |
{ |
|
12490 |
/* |
|
12491 |
HAVING will be checked after processing aggregate functions, |
|
12492 |
But WHERE should checkd here (we alredy have read tables) |
|
12493 |
*/ |
|
12494 |
if (!join->conds || join->conds->val_int()) |
|
12495 |
{ |
|
12496 |
error= (*end_select)(join, 0, 0); |
|
12497 |
if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT) |
|
12498 |
error= (*end_select)(join, 0, 1); |
|
12499 |
||
12500 |
/* |
|
12501 |
If we don't go through evaluate_join_record(), do the counting |
|
12502 |
here. join->send_records is increased on success in end_send(), |
|
12503 |
so we don't touch it here. |
|
12504 |
*/ |
|
12505 |
join->examined_rows++; |
|
12506 |
join->thd->row_count++; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
12507 |
assert(join->examined_rows <= 1); |
1
by brian
clean slate |
12508 |
} |
12509 |
else if (join->send_row_on_empty_set()) |
|
12510 |
{ |
|
12511 |
List<Item> *columns_list= fields; |
|
12512 |
rc= join->result->send_data(*columns_list); |
|
12513 |
} |
|
12514 |
} |
|
12515 |
else |
|
12516 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
12517 |
assert(join->tables); |
1
by brian
clean slate |
12518 |
error= sub_select(join,join_tab,0); |
12519 |
if (error == NESTED_LOOP_OK || error == NESTED_LOOP_NO_MORE_ROWS) |
|
12520 |
error= sub_select(join,join_tab,1); |
|
12521 |
if (error == NESTED_LOOP_QUERY_LIMIT) |
|
12522 |
error= NESTED_LOOP_OK; /* select_limit used */ |
|
12523 |
} |
|
12524 |
if (error == NESTED_LOOP_NO_MORE_ROWS) |
|
12525 |
error= NESTED_LOOP_OK; |
|
12526 |
||
12527 |
if (error == NESTED_LOOP_OK) |
|
12528 |
{ |
|
12529 |
/* |
|
12530 |
Sic: this branch works even if rc != 0, e.g. when |
|
12531 |
send_data above returns an error. |
|
12532 |
*/ |
|
12533 |
if (!table) // If sending data to client |
|
12534 |
{ |
|
12535 |
/* |
|
12536 |
The following will unlock all cursors if the command wasn't an |
|
12537 |
update command |
|
12538 |
*/ |
|
12539 |
join->join_free(); // Unlock all cursors |
|
12540 |
if (join->result->send_eof()) |
|
12541 |
rc= 1; // Don't send error |
|
12542 |
} |
|
12543 |
} |
|
12544 |
else |
|
12545 |
rc= -1; |
|
12546 |
if (table) |
|
12547 |
{ |
|
12548 |
int tmp, new_errno= 0; |
|
12549 |
if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE))) |
|
12550 |
{ |
|
12551 |
new_errno= tmp; |
|
12552 |
} |
|
12553 |
if ((tmp=table->file->ha_index_or_rnd_end())) |
|
12554 |
{ |
|
12555 |
new_errno= tmp; |
|
12556 |
} |
|
12557 |
if (new_errno) |
|
12558 |
table->file->print_error(new_errno,MYF(0)); |
|
12559 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
12560 |
return(join->thd->is_error() ? -1 : rc); |
1
by brian
clean slate |
12561 |
} |
12562 |
||
12563 |
||
12564 |
enum_nested_loop_state |
|
12565 |
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records) |
|
12566 |
{ |
|
12567 |
enum_nested_loop_state rc; |
|
12568 |
||
12569 |
if (end_of_records) |
|
12570 |
{ |
|
55
by brian
Update for using real bool types. |
12571 |
rc= flush_cached_records(join,join_tab,false); |
1
by brian
clean slate |
12572 |
if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS) |
12573 |
rc= sub_select(join,join_tab,end_of_records); |
|
12574 |
return rc; |
|
12575 |
} |
|
12576 |
if (join->thd->killed) // If aborted by user |
|
12577 |
{ |
|
12578 |
join->thd->send_kill_message(); |
|
12579 |
return NESTED_LOOP_KILLED; /* purecov: inspected */ |
|
12580 |
} |
|
12581 |
if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0) |
|
12582 |
{ |
|
12583 |
if (!store_record_in_cache(&join_tab->cache)) |
|
12584 |
return NESTED_LOOP_OK; // There is more room in cache |
|
55
by brian
Update for using real bool types. |
12585 |
return flush_cached_records(join,join_tab,false); |
1
by brian
clean slate |
12586 |
} |
55
by brian
Update for using real bool types. |
12587 |
rc= flush_cached_records(join, join_tab, true); |
1
by brian
clean slate |
12588 |
if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS) |
12589 |
rc= sub_select(join, join_tab, end_of_records); |
|
12590 |
return rc; |
|
12591 |
} |
|
12592 |
||
12593 |
/** |
|
12594 |
Retrieve records ends with a given beginning from the result of a join. |
|
12595 |
||
12596 |
For a given partial join record consisting of records from the tables |
|
12597 |
preceding the table join_tab in the execution plan, the function |
|
12598 |
retrieves all matching full records from the result set and |
|
12599 |
send them to the result set stream. |
|
12600 |
||
12601 |
@note |
|
12602 |
The function effectively implements the final (n-k) nested loops |
|
12603 |
of nested loops join algorithm, where k is the ordinal number of |
|
12604 |
the join_tab table and n is the total number of tables in the join query. |
|
12605 |
It performs nested loops joins with all conjunctive predicates from |
|
12606 |
the where condition pushed as low to the tables as possible. |
|
12607 |
E.g. for the query |
|
12608 |
@code |
|
12609 |
SELECT * FROM t1,t2,t3 |
|
12610 |
WHERE t1.a=t2.a AND t2.b=t3.b AND t1.a BETWEEN 5 AND 9 |
|
12611 |
@endcode |
|
12612 |
the predicate (t1.a BETWEEN 5 AND 9) will be pushed to table t1, |
|
12613 |
given the selected plan prescribes to nest retrievals of the |
|
12614 |
joined tables in the following order: t1,t2,t3. |
|
12615 |
A pushed down predicate are attached to the table which it pushed to, |
|
12616 |
at the field join_tab->select_cond. |
|
12617 |
When executing a nested loop of level k the function runs through |
|
12618 |
the rows of 'join_tab' and for each row checks the pushed condition |
|
12619 |
attached to the table. |
|
12620 |
If it is false the function moves to the next row of the |
|
12621 |
table. If the condition is true the function recursively executes (n-k-1) |
|
12622 |
remaining embedded nested loops. |
|
12623 |
The situation becomes more complicated if outer joins are involved in |
|
12624 |
the execution plan. In this case the pushed down predicates can be |
|
12625 |
checked only at certain conditions. |
|
12626 |
Suppose for the query |
|
12627 |
@code |
|
12628 |
SELECT * FROM t1 LEFT JOIN (t2,t3) ON t3.a=t1.a |
|
12629 |
WHERE t1>2 AND (t2.b>5 OR t2.b IS NULL) |
|
12630 |
@endcode |
|
12631 |
the optimizer has chosen a plan with the table order t1,t2,t3. |
|
12632 |
The predicate P1=t1>2 will be pushed down to the table t1, while the |
|
12633 |
predicate P2=(t2.b>5 OR t2.b IS NULL) will be attached to the table |
|
12634 |
t2. But the second predicate can not be unconditionally tested right |
|
12635 |
after a row from t2 has been read. This can be done only after the |
|
12636 |
first row with t3.a=t1.a has been encountered. |
|
12637 |
Thus, the second predicate P2 is supplied with a guarded value that are |
|
12638 |
stored in the field 'found' of the first inner table for the outer join |
|
12639 |
(table t2). When the first row with t3.a=t1.a for the current row |
|
12640 |
of table t1 appears, the value becomes true. For now on the predicate |
|
12641 |
is evaluated immediately after the row of table t2 has been read. |
|
12642 |
When the first row with t3.a=t1.a has been encountered all |
|
12643 |
conditions attached to the inner tables t2,t3 must be evaluated. |
|
12644 |
Only when all of them are true the row is sent to the output stream. |
|
12645 |
If not, the function returns to the lowest nest level that has a false |
|
12646 |
attached condition. |
|
12647 |
The predicates from on expressions are also pushed down. If in the |
|
12648 |
the above example the on expression were (t3.a=t1.a AND t2.a=t1.a), |
|
12649 |
then t1.a=t2.a would be pushed down to table t2, and without any |
|
12650 |
guard. |
|
12651 |
If after the run through all rows of table t2, the first inner table |
|
12652 |
for the outer join operation, it turns out that no matches are |
|
12653 |
found for the current row of t1, then current row from table t1 |
|
12654 |
is complemented by nulls for t2 and t3. Then the pushed down predicates |
|
12655 |
are checked for the composed row almost in the same way as it had |
|
12656 |
been done for the first row with a match. The only difference is |
|
12657 |
the predicates from on expressions are not checked. |
|
12658 |
||
12659 |
@par |
|
12660 |
@b IMPLEMENTATION |
|
12661 |
@par |
|
12662 |
The function forms output rows for a current partial join of k |
|
12663 |
tables tables recursively. |
|
12664 |
For each partial join record ending with a certain row from |
|
12665 |
join_tab it calls sub_select that builds all possible matching |
|
12666 |
tails from the result set. |
|
12667 |
To be able check predicates conditionally items of the class |
|
12668 |
Item_func_trig_cond are employed. |
|
12669 |
An object of this class is constructed from an item of class COND |
|
12670 |
and a pointer to a guarding boolean variable. |
|
12671 |
When the value of the guard variable is true the value of the object |
|
12672 |
is the same as the value of the predicate, otherwise it's just returns |
|
12673 |
true. |
|
12674 |
To carry out a return to a nested loop level of join table t the pointer |
|
12675 |
to t is remembered in the field 'return_tab' of the join structure. |
|
12676 |
Consider the following query: |
|
12677 |
@code |
|
12678 |
SELECT * FROM t1, |
|
12679 |
LEFT JOIN |
|
12680 |
(t2, t3 LEFT JOIN (t4,t5) ON t5.a=t3.a) |
|
12681 |
ON t4.a=t2.a |
|
12682 |
WHERE (t2.b=5 OR t2.b IS NULL) AND (t4.b=2 OR t4.b IS NULL) |
|
12683 |
@endcode |
|
12684 |
Suppose the chosen execution plan dictates the order t1,t2,t3,t4,t5 |
|
12685 |
and suppose for a given joined rows from tables t1,t2,t3 there are |
|
12686 |
no rows in the result set yet. |
|
12687 |
When first row from t5 that satisfies the on condition |
|
12688 |
t5.a=t3.a is found, the pushed down predicate t4.b=2 OR t4.b IS NULL |
|
12689 |
becomes 'activated', as well the predicate t4.a=t2.a. But |
|
12690 |
the predicate (t2.b=5 OR t2.b IS NULL) can not be checked until |
|
12691 |
t4.a=t2.a becomes true. |
|
12692 |
In order not to re-evaluate the predicates that were already evaluated |
|
12693 |
as attached pushed down predicates, a pointer to the the first |
|
12694 |
most inner unmatched table is maintained in join_tab->first_unmatched. |
|
12695 |
Thus, when the first row from t5 with t5.a=t3.a is found |
|
12696 |
this pointer for t5 is changed from t4 to t2. |
|
12697 |
||
12698 |
@par |
|
12699 |
@b STRUCTURE @b NOTES |
|
12700 |
@par |
|
12701 |
join_tab->first_unmatched points always backwards to the first inner |
|
12702 |
table of the embedding nested join, if any. |
|
12703 |
||
12704 |
@param join pointer to the structure providing all context info for |
|
12705 |
the query |
|
12706 |
@param join_tab the first next table of the execution plan to be retrieved |
|
12707 |
@param end_records true when we need to perform final steps of retrival |
|
12708 |
||
12709 |
@return |
|
12710 |
return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS. |
|
12711 |
*/ |
|
12712 |
int do_sj_reset(SJ_TMP_TABLE *sj_tbl); |
|
12713 |
||
12714 |
enum_nested_loop_state |
|
12715 |
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records) |
|
12716 |
{ |
|
12717 |
join_tab->table->null_row=0; |
|
12718 |
if (end_of_records) |
|
12719 |
return (*join_tab->next_select)(join,join_tab+1,end_of_records); |
|
12720 |
||
12721 |
int error; |
|
12722 |
enum_nested_loop_state rc; |
|
12723 |
READ_RECORD *info= &join_tab->read_record; |
|
12724 |
||
12725 |
if (join_tab->flush_weedout_table) |
|
12726 |
{ |
|
12727 |
do_sj_reset(join_tab->flush_weedout_table); |
|
12728 |
} |
|
12729 |
||
12730 |
if (join->resume_nested_loop) |
|
12731 |
{ |
|
12732 |
/* If not the last table, plunge down the nested loop */ |
|
12733 |
if (join_tab < join->join_tab + join->tables - 1) |
|
12734 |
rc= (*join_tab->next_select)(join, join_tab + 1, 0); |
|
12735 |
else |
|
12736 |
{ |
|
55
by brian
Update for using real bool types. |
12737 |
join->resume_nested_loop= false; |
1
by brian
clean slate |
12738 |
rc= NESTED_LOOP_OK; |
12739 |
} |
|
12740 |
} |
|
12741 |
else |
|
12742 |
{ |
|
12743 |
join->return_tab= join_tab; |
|
12744 |
||
12745 |
if (join_tab->last_inner) |
|
12746 |
{ |
|
12747 |
/* join_tab is the first inner table for an outer join operation. */ |
|
12748 |
||
12749 |
/* Set initial state of guard variables for this table.*/ |
|
12750 |
join_tab->found=0; |
|
12751 |
join_tab->not_null_compl= 1; |
|
12752 |
||
12753 |
/* Set first_unmatched for the last inner table of this group */ |
|
12754 |
join_tab->last_inner->first_unmatched= join_tab; |
|
12755 |
} |
|
12756 |
join->thd->row_count= 0; |
|
12757 |
||
12758 |
error= (*join_tab->read_first_record)(join_tab); |
|
12759 |
rc= evaluate_join_record(join, join_tab, error); |
|
12760 |
} |
|
12761 |
||
12762 |
/* |
|
12763 |
Note: psergey has added the 2nd part of the following condition; the |
|
12764 |
change should probably be made in 5.1, too. |
|
12765 |
*/ |
|
12766 |
while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab) |
|
12767 |
{ |
|
12768 |
error= info->read_record(info); |
|
12769 |
rc= evaluate_join_record(join, join_tab, error); |
|
12770 |
} |
|
12771 |
||
12772 |
if (rc == NESTED_LOOP_NO_MORE_ROWS && |
|
12773 |
join_tab->last_inner && !join_tab->found) |
|
12774 |
rc= evaluate_null_complemented_join_record(join, join_tab); |
|
12775 |
||
12776 |
if (rc == NESTED_LOOP_NO_MORE_ROWS) |
|
12777 |
rc= NESTED_LOOP_OK; |
|
12778 |
return rc; |
|
12779 |
} |
|
12780 |
||
12781 |
||
12782 |
||
12783 |
||
12784 |
/* |
|
12785 |
SemiJoinDuplicateElimination: Weed out duplicate row combinations |
|
12786 |
||
12787 |
SYNPOSIS |
|
12788 |
do_sj_dups_weedout() |
|
12789 |
||
12790 |
RETURN |
|
12791 |
-1 Error |
|
12792 |
1 The row combination is a duplicate (discard it) |
|
12793 |
0 The row combination is not a duplicate (continue) |
|
12794 |
*/ |
|
12795 |
||
12796 |
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl) |
|
12797 |
{ |
|
12798 |
int error; |
|
12799 |
SJ_TMP_TABLE::TAB *tab= sjtbl->tabs; |
|
12800 |
SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end; |
|
12801 |
uchar *ptr= sjtbl->tmp_table->record[0] + 1; |
|
12802 |
uchar *nulls_ptr= ptr; |
|
12803 |
||
12804 |
/* Put the the rowids tuple into table->record[0]: */ |
|
12805 |
||
12806 |
// 1. Store the length |
|
12807 |
if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1) |
|
12808 |
{ |
|
12809 |
*ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes); |
|
12810 |
ptr++; |
|
12811 |
} |
|
12812 |
else |
|
12813 |
{ |
|
12814 |
int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes); |
|
12815 |
ptr += 2; |
|
12816 |
} |
|
12817 |
||
12818 |
// 2. Zero the null bytes |
|
12819 |
if (sjtbl->null_bytes) |
|
12820 |
{ |
|
12821 |
bzero(ptr, sjtbl->null_bytes); |
|
12822 |
ptr += sjtbl->null_bytes; |
|
12823 |
} |
|
12824 |
||
12825 |
// 3. Put the rowids |
|
12826 |
for (uint i=0; tab != tab_end; tab++, i++) |
|
12827 |
{ |
|
12828 |
handler *h= tab->join_tab->table->file; |
|
12829 |
if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row) |
|
12830 |
{ |
|
12831 |
/* It's a NULL-complemented row */ |
|
12832 |
*(nulls_ptr + tab->null_byte) |= tab->null_bit; |
|
12833 |
bzero(ptr + tab->rowid_offset, h->ref_length); |
|
12834 |
} |
|
12835 |
else |
|
12836 |
{ |
|
12837 |
/* Copy the rowid value */ |
|
12838 |
if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION) |
|
12839 |
h->position(tab->join_tab->table->record[0]); |
|
12840 |
memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length); |
|
12841 |
} |
|
12842 |
} |
|
12843 |
||
12844 |
error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]); |
|
12845 |
if (error) |
|
12846 |
{ |
|
12847 |
/* create_myisam_from_heap will generate error if needed */ |
|
12848 |
if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) && |
|
12849 |
create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo, |
|
12850 |
&sjtbl->recinfo, error, 1)) |
|
12851 |
return -1; |
|
12852 |
//return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1; |
|
12853 |
return 1; |
|
12854 |
} |
|
12855 |
return 0; |
|
12856 |
} |
|
12857 |
||
12858 |
||
12859 |
/* |
|
12860 |
SemiJoinDuplicateElimination: Reset the temporary table |
|
12861 |
*/ |
|
12862 |
||
12863 |
int do_sj_reset(SJ_TMP_TABLE *sj_tbl) |
|
12864 |
{ |
|
12865 |
if (sj_tbl->tmp_table) |
|
12866 |
return sj_tbl->tmp_table->file->ha_delete_all_rows(); |
|
12867 |
return 0; |
|
12868 |
} |
|
12869 |
||
12870 |
/* |
|
12871 |
Process one record of the nested loop join. |
|
12872 |
||
12873 |
This function will evaluate parts of WHERE/ON clauses that are |
|
12874 |
applicable to the partial record on hand and in case of success |
|
12875 |
submit this record to the next level of the nested loop. |
|
12876 |
*/ |
|
12877 |
||
12878 |
static enum_nested_loop_state |
|
12879 |
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab, |
|
12880 |
int error) |
|
12881 |
{ |
|
12882 |
bool not_used_in_distinct=join_tab->not_used_in_distinct; |
|
12883 |
ha_rows found_records=join->found_records; |
|
12884 |
COND *select_cond= join_tab->select_cond; |
|
12885 |
||
12886 |
if (error > 0 || (join->thd->is_error())) // Fatal error |
|
12887 |
return NESTED_LOOP_ERROR; |
|
12888 |
if (error < 0) |
|
12889 |
return NESTED_LOOP_NO_MORE_ROWS; |
|
12890 |
if (join->thd->killed) // Aborted by user |
|
12891 |
{ |
|
12892 |
join->thd->send_kill_message(); |
|
12893 |
return NESTED_LOOP_KILLED; /* purecov: inspected */ |
|
12894 |
} |
|
12895 |
if (!select_cond || select_cond->val_int()) |
|
12896 |
{ |
|
12897 |
/* |
|
12898 |
There is no select condition or the attached pushed down |
|
12899 |
condition is true => a match is found. |
|
12900 |
*/ |
|
12901 |
bool found= 1; |
|
12902 |
while (join_tab->first_unmatched && found) |
|
12903 |
{ |
|
12904 |
/* |
|
12905 |
The while condition is always false if join_tab is not |
|
12906 |
the last inner join table of an outer join operation. |
|
12907 |
*/ |
|
12908 |
JOIN_TAB *first_unmatched= join_tab->first_unmatched; |
|
12909 |
/* |
|
12910 |
Mark that a match for current outer table is found. |
|
12911 |
This activates push down conditional predicates attached |
|
12912 |
to the all inner tables of the outer join. |
|
12913 |
*/ |
|
12914 |
first_unmatched->found= 1; |
|
12915 |
for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++) |
|
12916 |
{ |
|
12917 |
if (tab->table->reginfo.not_exists_optimize) |
|
12918 |
return NESTED_LOOP_NO_MORE_ROWS; |
|
12919 |
/* Check all predicates that has just been activated. */ |
|
12920 |
/* |
|
12921 |
Actually all predicates non-guarded by first_unmatched->found |
|
12922 |
will be re-evaluated again. It could be fixed, but, probably, |
|
12923 |
it's not worth doing now. |
|
12924 |
*/ |
|
12925 |
if (tab->select_cond && !tab->select_cond->val_int()) |
|
12926 |
{ |
|
12927 |
/* The condition attached to table tab is false */ |
|
12928 |
if (tab == join_tab) |
|
12929 |
found= 0; |
|
12930 |
else |
|
12931 |
{ |
|
12932 |
/* |
|
12933 |
Set a return point if rejected predicate is attached |
|
12934 |
not to the last table of the current nest level. |
|
12935 |
*/ |
|
12936 |
join->return_tab= tab; |
|
12937 |
return NESTED_LOOP_OK; |
|
12938 |
} |
|
12939 |
} |
|
12940 |
} |
|
12941 |
/* |
|
12942 |
Check whether join_tab is not the last inner table |
|
12943 |
for another embedding outer join. |
|
12944 |
*/ |
|
12945 |
if ((first_unmatched= first_unmatched->first_upper) && |
|
12946 |
first_unmatched->last_inner != join_tab) |
|
12947 |
first_unmatched= 0; |
|
12948 |
join_tab->first_unmatched= first_unmatched; |
|
12949 |
} |
|
12950 |
||
12951 |
JOIN_TAB *return_tab= join->return_tab; |
|
55
by brian
Update for using real bool types. |
12952 |
join_tab->found_match= true; |
1
by brian
clean slate |
12953 |
if (join_tab->check_weed_out_table) |
12954 |
{ |
|
12955 |
int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table); |
|
12956 |
if (res == -1) |
|
12957 |
return NESTED_LOOP_ERROR; |
|
12958 |
if (res == 1) |
|
12959 |
return NESTED_LOOP_OK; |
|
12960 |
} |
|
12961 |
else if (join_tab->do_firstmatch) |
|
12962 |
{ |
|
12963 |
/* |
|
12964 |
We should return to the join_tab->do_firstmatch after we have |
|
12965 |
enumerated all the suffixes for current prefix row combination |
|
12966 |
*/ |
|
12967 |
return_tab= join_tab->do_firstmatch; |
|
12968 |
} |
|
12969 |
||
12970 |
/* |
|
12971 |
It was not just a return to lower loop level when one |
|
12972 |
of the newly activated predicates is evaluated as false |
|
12973 |
(See above join->return_tab= tab). |
|
12974 |
*/ |
|
12975 |
join->examined_rows++; |
|
12976 |
join->thd->row_count++; |
|
12977 |
||
12978 |
if (found) |
|
12979 |
{ |
|
12980 |
enum enum_nested_loop_state rc; |
|
12981 |
/* A match from join_tab is found for the current partial join. */ |
|
12982 |
rc= (*join_tab->next_select)(join, join_tab+1, 0); |
|
12983 |
if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS) |
|
12984 |
return rc; |
|
12985 |
if (return_tab < join->return_tab) |
|
12986 |
join->return_tab= return_tab; |
|
12987 |
||
12988 |
if (join->return_tab < join_tab) |
|
12989 |
return NESTED_LOOP_OK; |
|
12990 |
/* |
|
12991 |
Test if this was a SELECT DISTINCT query on a table that |
|
12992 |
was not in the field list; In this case we can abort if |
|
12993 |
we found a row, as no new rows can be added to the result. |
|
12994 |
*/ |
|
12995 |
if (not_used_in_distinct && found_records != join->found_records) |
|
12996 |
return NESTED_LOOP_NO_MORE_ROWS; |
|
12997 |
} |
|
12998 |
else |
|
12999 |
join_tab->read_record.file->unlock_row(); |
|
13000 |
} |
|
13001 |
else |
|
13002 |
{ |
|
13003 |
/* |
|
13004 |
The condition pushed down to the table join_tab rejects all rows |
|
13005 |
with the beginning coinciding with the current partial join. |
|
13006 |
*/ |
|
13007 |
join->examined_rows++; |
|
13008 |
join->thd->row_count++; |
|
13009 |
join_tab->read_record.file->unlock_row(); |
|
13010 |
} |
|
13011 |
return NESTED_LOOP_OK; |
|
13012 |
} |
|
13013 |
||
13014 |
||
13015 |
/** |
|
13016 |
||
13017 |
@details |
|
13018 |
Construct a NULL complimented partial join record and feed it to the next |
|
13019 |
level of the nested loop. This function is used in case we have |
|
13020 |
an OUTER join and no matching record was found. |
|
13021 |
*/ |
|
13022 |
||
13023 |
static enum_nested_loop_state |
|
13024 |
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab) |
|
13025 |
{ |
|
13026 |
/* |
|
13027 |
The table join_tab is the first inner table of a outer join operation |
|
13028 |
and no matches has been found for the current outer row. |
|
13029 |
*/ |
|
13030 |
JOIN_TAB *last_inner_tab= join_tab->last_inner; |
|
13031 |
/* Cache variables for faster loop */ |
|
13032 |
COND *select_cond; |
|
13033 |
for ( ; join_tab <= last_inner_tab ; join_tab++) |
|
13034 |
{ |
|
13035 |
/* Change the the values of guard predicate variables. */ |
|
13036 |
join_tab->found= 1; |
|
13037 |
join_tab->not_null_compl= 0; |
|
13038 |
/* The outer row is complemented by nulls for each inner tables */ |
|
13039 |
restore_record(join_tab->table,s->default_values); // Make empty record |
|
13040 |
mark_as_null_row(join_tab->table); // For group by without error |
|
13041 |
select_cond= join_tab->select_cond; |
|
13042 |
/* Check all attached conditions for inner table rows. */ |
|
13043 |
if (select_cond && !select_cond->val_int()) |
|
13044 |
return NESTED_LOOP_OK; |
|
13045 |
} |
|
13046 |
join_tab--; |
|
13047 |
/* |
|
13048 |
The row complemented by nulls might be the first row |
|
13049 |
of embedding outer joins. |
|
13050 |
If so, perform the same actions as in the code |
|
13051 |
for the first regular outer join row above. |
|
13052 |
*/ |
|
13053 |
for ( ; ; ) |
|
13054 |
{ |
|
13055 |
JOIN_TAB *first_unmatched= join_tab->first_unmatched; |
|
13056 |
if ((first_unmatched= first_unmatched->first_upper) && |
|
13057 |
first_unmatched->last_inner != join_tab) |
|
13058 |
first_unmatched= 0; |
|
13059 |
join_tab->first_unmatched= first_unmatched; |
|
13060 |
if (!first_unmatched) |
|
13061 |
break; |
|
13062 |
first_unmatched->found= 1; |
|
13063 |
for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++) |
|
13064 |
{ |
|
13065 |
if (tab->select_cond && !tab->select_cond->val_int()) |
|
13066 |
{ |
|
13067 |
join->return_tab= tab; |
|
13068 |
return NESTED_LOOP_OK; |
|
13069 |
} |
|
13070 |
} |
|
13071 |
} |
|
13072 |
/* |
|
13073 |
The row complemented by nulls satisfies all conditions |
|
13074 |
attached to inner tables. |
|
13075 |
Send the row complemented by nulls to be joined with the |
|
13076 |
remaining tables. |
|
13077 |
*/ |
|
13078 |
return (*join_tab->next_select)(join, join_tab+1, 0); |
|
13079 |
} |
|
13080 |
||
13081 |
||
13082 |
static enum_nested_loop_state |
|
13083 |
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last) |
|
13084 |
{ |
|
13085 |
enum_nested_loop_state rc= NESTED_LOOP_OK; |
|
13086 |
int error; |
|
13087 |
READ_RECORD *info; |
|
13088 |
||
13089 |
join_tab->table->null_row= 0; |
|
13090 |
if (!join_tab->cache.records) |
|
13091 |
return NESTED_LOOP_OK; /* Nothing to do */ |
|
13092 |
if (skip_last) |
|
13093 |
(void) store_record_in_cache(&join_tab->cache); // Must save this for later |
|
13094 |
if (join_tab->use_quick == 2) |
|
13095 |
{ |
|
13096 |
if (join_tab->select->quick) |
|
13097 |
{ /* Used quick select last. reset it */ |
|
13098 |
delete join_tab->select->quick; |
|
13099 |
join_tab->select->quick=0; |
|
13100 |
} |
|
13101 |
} |
|
13102 |
/* read through all records */ |
|
13103 |
if ((error=join_init_read_record(join_tab))) |
|
13104 |
{ |
|
13105 |
reset_cache_write(&join_tab->cache); |
|
13106 |
return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR; |
|
13107 |
} |
|
13108 |
||
13109 |
for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++) |
|
13110 |
{ |
|
13111 |
tmp->status=tmp->table->status; |
|
13112 |
tmp->table->status=0; |
|
13113 |
} |
|
13114 |
||
13115 |
info= &join_tab->read_record; |
|
13116 |
do |
|
13117 |
{ |
|
13118 |
if (join->thd->killed) |
|
13119 |
{ |
|
13120 |
join->thd->send_kill_message(); |
|
13121 |
return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */ |
|
13122 |
} |
|
13123 |
SQL_SELECT *select=join_tab->select; |
|
13124 |
if (rc == NESTED_LOOP_OK && |
|
13125 |
(!join_tab->cache.select || !join_tab->cache.select->skip_record())) |
|
13126 |
{ |
|
13127 |
uint i; |
|
13128 |
reset_cache_read(&join_tab->cache); |
|
13129 |
for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;) |
|
13130 |
{ |
|
13131 |
read_cached_record(join_tab); |
|
13132 |
if (!select || !select->skip_record()) |
|
13133 |
{ |
|
13134 |
int res= 0; |
|
13135 |
if (!join_tab->check_weed_out_table || |
|
13136 |
!(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table))) |
|
13137 |
{ |
|
13138 |
rc= (join_tab->next_select)(join,join_tab+1,0); |
|
13139 |
if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS) |
|
13140 |
{ |
|
13141 |
reset_cache_write(&join_tab->cache); |
|
13142 |
return rc; |
|
13143 |
} |
|
13144 |
} |
|
13145 |
if (res == -1) |
|
13146 |
return NESTED_LOOP_ERROR; |
|
13147 |
} |
|
13148 |
} |
|
13149 |
} |
|
13150 |
} while (!(error=info->read_record(info))); |
|
13151 |
||
13152 |
if (skip_last) |
|
13153 |
read_cached_record(join_tab); // Restore current record |
|
13154 |
reset_cache_write(&join_tab->cache); |
|
13155 |
if (error > 0) // Fatal error |
|
13156 |
return NESTED_LOOP_ERROR; /* purecov: inspected */ |
|
13157 |
for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++) |
|
13158 |
tmp2->table->status=tmp2->status; |
|
13159 |
return NESTED_LOOP_OK; |
|
13160 |
} |
|
13161 |
||
13162 |
||
13163 |
/***************************************************************************** |
|
13164 |
The different ways to read a record |
|
13165 |
Returns -1 if row was not found, 0 if row was found and 1 on errors |
|
13166 |
*****************************************************************************/ |
|
13167 |
||
13168 |
/** Help function when we get some an error from the table handler. */ |
|
13169 |
||
13170 |
int report_error(TABLE *table, int error) |
|
13171 |
{ |
|
13172 |
if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND) |
|
13173 |
{ |
|
13174 |
table->status= STATUS_GARBAGE; |
|
13175 |
return -1; // key not found; ok |
|
13176 |
} |
|
13177 |
/* |
|
13178 |
Locking reads can legally return also these errors, do not |
|
13179 |
print them to the .err log |
|
13180 |
*/ |
|
13181 |
if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT) |
|
13182 |
sql_print_error("Got error %d when reading table '%s'", |
|
13183 |
error, table->s->path.str); |
|
13184 |
table->file->print_error(error,MYF(0)); |
|
13185 |
return 1; |
|
13186 |
} |
|
13187 |
||
13188 |
||
13189 |
int safe_index_read(JOIN_TAB *tab) |
|
13190 |
{ |
|
13191 |
int error; |
|
13192 |
TABLE *table= tab->table; |
|
13193 |
if ((error=table->file->index_read_map(table->record[0], |
|
13194 |
tab->ref.key_buff, |
|
13195 |
make_prev_keypart_map(tab->ref.key_parts), |
|
13196 |
HA_READ_KEY_EXACT))) |
|
13197 |
return report_error(table, error); |
|
13198 |
return 0; |
|
13199 |
} |
|
13200 |
||
13201 |
||
13202 |
static int |
|
13203 |
join_read_const_table(JOIN_TAB *tab, POSITION *pos) |
|
13204 |
{ |
|
13205 |
int error; |
|
13206 |
TABLE *table=tab->table; |
|
13207 |
table->const_table=1; |
|
13208 |
table->null_row=0; |
|
13209 |
table->status=STATUS_NO_RECORD; |
|
13210 |
||
13211 |
if (tab->type == JT_SYSTEM) |
|
13212 |
{ |
|
13213 |
if ((error=join_read_system(tab))) |
|
13214 |
{ // Info for DESCRIBE |
|
13215 |
tab->info="const row not found"; |
|
13216 |
/* Mark for EXPLAIN that the row was not found */ |
|
13217 |
pos->records_read=0.0; |
|
13218 |
pos->ref_depend_map= 0; |
|
13219 |
if (!table->maybe_null || error > 0) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13220 |
return(error); |
1
by brian
clean slate |
13221 |
} |
13222 |
} |
|
13223 |
else |
|
13224 |
{ |
|
13225 |
if (!table->key_read && table->covering_keys.is_set(tab->ref.key) && |
|
13226 |
!table->no_keyread && |
|
13227 |
(int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY) |
|
13228 |
{ |
|
13229 |
table->key_read=1; |
|
13230 |
table->file->extra(HA_EXTRA_KEYREAD); |
|
13231 |
tab->index= tab->ref.key; |
|
13232 |
} |
|
13233 |
error=join_read_const(tab); |
|
13234 |
if (table->key_read) |
|
13235 |
{ |
|
13236 |
table->key_read=0; |
|
13237 |
table->file->extra(HA_EXTRA_NO_KEYREAD); |
|
13238 |
} |
|
13239 |
if (error) |
|
13240 |
{ |
|
13241 |
tab->info="unique row not found"; |
|
13242 |
/* Mark for EXPLAIN that the row was not found */ |
|
13243 |
pos->records_read=0.0; |
|
13244 |
pos->ref_depend_map= 0; |
|
13245 |
if (!table->maybe_null || error > 0) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13246 |
return(error); |
1
by brian
clean slate |
13247 |
} |
13248 |
} |
|
13249 |
if (*tab->on_expr_ref && !table->null_row) |
|
13250 |
{ |
|
13251 |
if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0))) |
|
13252 |
mark_as_null_row(table); |
|
13253 |
} |
|
13254 |
if (!table->null_row) |
|
13255 |
table->maybe_null=0; |
|
13256 |
||
13257 |
/* Check appearance of new constant items in Item_equal objects */ |
|
13258 |
JOIN *join= tab->join; |
|
13259 |
if (join->conds) |
|
13260 |
update_const_equal_items(join->conds, tab); |
|
13261 |
TABLE_LIST *tbl; |
|
13262 |
for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf) |
|
13263 |
{ |
|
13264 |
TABLE_LIST *embedded; |
|
13265 |
TABLE_LIST *embedding= tbl; |
|
13266 |
do |
|
13267 |
{ |
|
13268 |
embedded= embedding; |
|
13269 |
if (embedded->on_expr) |
|
13270 |
update_const_equal_items(embedded->on_expr, tab); |
|
13271 |
embedding= embedded->embedding; |
|
13272 |
} |
|
13273 |
while (embedding && |
|
13274 |
embedding->nested_join->join_list.head() == embedded); |
|
13275 |
} |
|
13276 |
||
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13277 |
return(0); |
1
by brian
clean slate |
13278 |
} |
13279 |
||
13280 |
||
13281 |
static int |
|
13282 |
join_read_system(JOIN_TAB *tab) |
|
13283 |
{ |
|
13284 |
TABLE *table= tab->table; |
|
13285 |
int error; |
|
13286 |
if (table->status & STATUS_GARBAGE) // If first read |
|
13287 |
{ |
|
13288 |
if ((error=table->file->read_first_row(table->record[0], |
|
13289 |
table->s->primary_key))) |
|
13290 |
{ |
|
13291 |
if (error != HA_ERR_END_OF_FILE) |
|
13292 |
return report_error(table, error); |
|
13293 |
mark_as_null_row(tab->table); |
|
13294 |
empty_record(table); // Make empty record |
|
13295 |
return -1; |
|
13296 |
} |
|
13297 |
store_record(table,record[1]); |
|
13298 |
} |
|
13299 |
else if (!table->status) // Only happens with left join |
|
13300 |
restore_record(table,record[1]); // restore old record |
|
13301 |
table->null_row=0; |
|
13302 |
return table->status ? -1 : 0; |
|
13303 |
} |
|
13304 |
||
13305 |
||
13306 |
/** |
|
13307 |
Read a (constant) table when there is at most one matching row. |
|
13308 |
||
13309 |
@param tab Table to read |
|
13310 |
||
13311 |
@retval |
|
13312 |
0 Row was found |
|
13313 |
@retval |
|
13314 |
-1 Row was not found |
|
13315 |
@retval |
|
13316 |
1 Got an error (other than row not found) during read |
|
13317 |
*/ |
|
13318 |
||
13319 |
static int |
|
13320 |
join_read_const(JOIN_TAB *tab) |
|
13321 |
{ |
|
13322 |
int error; |
|
13323 |
TABLE *table= tab->table; |
|
13324 |
if (table->status & STATUS_GARBAGE) // If first read |
|
13325 |
{ |
|
13326 |
table->status= 0; |
|
13327 |
if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref)) |
|
13328 |
error=HA_ERR_KEY_NOT_FOUND; |
|
13329 |
else |
|
13330 |
{ |
|
13331 |
error=table->file->index_read_idx_map(table->record[0],tab->ref.key, |
|
13332 |
(uchar*) tab->ref.key_buff, |
|
13333 |
make_prev_keypart_map(tab->ref.key_parts), |
|
13334 |
HA_READ_KEY_EXACT); |
|
13335 |
} |
|
13336 |
if (error) |
|
13337 |
{ |
|
13338 |
table->status= STATUS_NOT_FOUND; |
|
13339 |
mark_as_null_row(tab->table); |
|
13340 |
empty_record(table); |
|
13341 |
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE) |
|
13342 |
return report_error(table, error); |
|
13343 |
return -1; |
|
13344 |
} |
|
13345 |
store_record(table,record[1]); |
|
13346 |
} |
|
13347 |
else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join |
|
13348 |
{ |
|
13349 |
table->status=0; |
|
13350 |
restore_record(table,record[1]); // restore old record |
|
13351 |
} |
|
13352 |
table->null_row=0; |
|
13353 |
return table->status ? -1 : 0; |
|
13354 |
} |
|
13355 |
||
13356 |
||
13357 |
/* |
|
13358 |
eq_ref access method implementation: "read_first" function |
|
13359 |
||
13360 |
SYNOPSIS |
|
13361 |
join_read_key() |
|
13362 |
tab JOIN_TAB of the accessed table |
|
13363 |
||
13364 |
DESCRIPTION |
|
13365 |
This is "read_fist" function for the "ref" access method. The difference |
|
13366 |
from "ref" is that it has a one-element "cache" (see cmp_buffer_with_ref) |
|
13367 |
||
13368 |
RETURN |
|
13369 |
0 - Ok |
|
13370 |
-1 - Row not found |
|
13371 |
1 - Error |
|
13372 |
*/ |
|
13373 |
||
13374 |
static int |
|
13375 |
join_read_key(JOIN_TAB *tab) |
|
13376 |
{ |
|
13377 |
int error; |
|
13378 |
TABLE *table= tab->table; |
|
13379 |
||
13380 |
if (!table->file->inited) |
|
13381 |
{ |
|
13382 |
table->file->ha_index_init(tab->ref.key, tab->sorted); |
|
13383 |
} |
|
13384 |
||
13385 |
/* TODO: Why don't we do "Late NULLs Filtering" here? */ |
|
13386 |
if (cmp_buffer_with_ref(tab) || |
|
13387 |
(table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW))) |
|
13388 |
{ |
|
13389 |
if (tab->ref.key_err) |
|
13390 |
{ |
|
13391 |
table->status=STATUS_NOT_FOUND; |
|
13392 |
return -1; |
|
13393 |
} |
|
13394 |
error=table->file->index_read_map(table->record[0], |
|
13395 |
tab->ref.key_buff, |
|
13396 |
make_prev_keypart_map(tab->ref.key_parts), |
|
13397 |
HA_READ_KEY_EXACT); |
|
13398 |
if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE) |
|
13399 |
return report_error(table, error); |
|
13400 |
} |
|
13401 |
table->null_row=0; |
|
13402 |
return table->status ? -1 : 0; |
|
13403 |
} |
|
13404 |
||
13405 |
||
13406 |
/* |
|
13407 |
ref access method implementation: "read_first" function |
|
13408 |
||
13409 |
SYNOPSIS |
|
13410 |
join_read_always_key() |
|
13411 |
tab JOIN_TAB of the accessed table |
|
13412 |
||
13413 |
DESCRIPTION |
|
13414 |
This is "read_fist" function for the "ref" access method. |
|
13415 |
||
13416 |
The functon must leave the index initialized when it returns. |
|
13417 |
ref_or_null access implementation depends on that. |
|
13418 |
||
13419 |
RETURN |
|
13420 |
0 - Ok |
|
13421 |
-1 - Row not found |
|
13422 |
1 - Error |
|
13423 |
*/ |
|
13424 |
||
13425 |
static int |
|
13426 |
join_read_always_key(JOIN_TAB *tab) |
|
13427 |
{ |
|
13428 |
int error; |
|
13429 |
TABLE *table= tab->table; |
|
13430 |
||
13431 |
/* Initialize the index first */ |
|
13432 |
if (!table->file->inited) |
|
13433 |
table->file->ha_index_init(tab->ref.key, tab->sorted); |
|
13434 |
||
13435 |
/* Perform "Late NULLs Filtering" (see internals manual for explanations) */ |
|
13436 |
for (uint i= 0 ; i < tab->ref.key_parts ; i++) |
|
13437 |
{ |
|
13438 |
if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null()) |
|
13439 |
return -1; |
|
13440 |
} |
|
13441 |
||
13442 |
if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref)) |
|
13443 |
return -1; |
|
13444 |
if ((error=table->file->index_read_map(table->record[0], |
|
13445 |
tab->ref.key_buff, |
|
13446 |
make_prev_keypart_map(tab->ref.key_parts), |
|
13447 |
HA_READ_KEY_EXACT))) |
|
13448 |
{ |
|
13449 |
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE) |
|
13450 |
return report_error(table, error); |
|
13451 |
return -1; /* purecov: inspected */ |
|
13452 |
} |
|
13453 |
return 0; |
|
13454 |
} |
|
13455 |
||
13456 |
||
13457 |
/** |
|
13458 |
This function is used when optimizing away ORDER BY in |
|
13459 |
SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC. |
|
13460 |
*/ |
|
13461 |
||
13462 |
static int |
|
13463 |
join_read_last_key(JOIN_TAB *tab) |
|
13464 |
{ |
|
13465 |
int error; |
|
13466 |
TABLE *table= tab->table; |
|
13467 |
||
13468 |
if (!table->file->inited) |
|
13469 |
table->file->ha_index_init(tab->ref.key, tab->sorted); |
|
13470 |
if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref)) |
|
13471 |
return -1; |
|
13472 |
if ((error=table->file->index_read_last_map(table->record[0], |
|
13473 |
tab->ref.key_buff, |
|
13474 |
make_prev_keypart_map(tab->ref.key_parts)))) |
|
13475 |
{ |
|
13476 |
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE) |
|
13477 |
return report_error(table, error); |
|
13478 |
return -1; /* purecov: inspected */ |
|
13479 |
} |
|
13480 |
return 0; |
|
13481 |
} |
|
13482 |
||
13483 |
||
13484 |
/* ARGSUSED */ |
|
13485 |
static int |
|
13486 |
join_no_more_records(READ_RECORD *info __attribute__((unused))) |
|
13487 |
{ |
|
13488 |
return -1; |
|
13489 |
} |
|
13490 |
||
13491 |
static int |
|
13492 |
join_read_next_same_diff(READ_RECORD *info) |
|
13493 |
{ |
|
13494 |
TABLE *table= info->table; |
|
13495 |
JOIN_TAB *tab=table->reginfo.join_tab; |
|
13496 |
if (tab->insideout_match_tab->found_match) |
|
13497 |
{ |
|
13498 |
KEY *key= tab->table->key_info + tab->index; |
|
13499 |
do |
|
13500 |
{ |
|
13501 |
int error; |
|
13502 |
/* Save index tuple from record to the buffer */ |
|
13503 |
key_copy(tab->insideout_buf, info->record, key, 0); |
|
13504 |
||
13505 |
if ((error=table->file->index_next_same(table->record[0], |
|
13506 |
tab->ref.key_buff, |
|
13507 |
tab->ref.key_length))) |
|
13508 |
{ |
|
13509 |
if (error != HA_ERR_END_OF_FILE) |
|
13510 |
return report_error(table, error); |
|
13511 |
table->status= STATUS_GARBAGE; |
|
13512 |
return -1; |
|
13513 |
} |
|
13514 |
} while (!key_cmp(tab->table->key_info[tab->index].key_part, |
|
13515 |
tab->insideout_buf, key->key_length)); |
|
13516 |
tab->insideout_match_tab->found_match= 0; |
|
13517 |
return 0; |
|
13518 |
} |
|
13519 |
else |
|
13520 |
return join_read_next_same(info); |
|
13521 |
} |
|
13522 |
||
13523 |
static int |
|
13524 |
join_read_next_same(READ_RECORD *info) |
|
13525 |
{ |
|
13526 |
int error; |
|
13527 |
TABLE *table= info->table; |
|
13528 |
JOIN_TAB *tab=table->reginfo.join_tab; |
|
13529 |
||
13530 |
if ((error=table->file->index_next_same(table->record[0], |
|
13531 |
tab->ref.key_buff, |
|
13532 |
tab->ref.key_length))) |
|
13533 |
{ |
|
13534 |
if (error != HA_ERR_END_OF_FILE) |
|
13535 |
return report_error(table, error); |
|
13536 |
table->status= STATUS_GARBAGE; |
|
13537 |
return -1; |
|
13538 |
} |
|
13539 |
return 0; |
|
13540 |
} |
|
13541 |
||
13542 |
||
13543 |
static int |
|
13544 |
join_read_prev_same(READ_RECORD *info) |
|
13545 |
{ |
|
13546 |
int error; |
|
13547 |
TABLE *table= info->table; |
|
13548 |
JOIN_TAB *tab=table->reginfo.join_tab; |
|
13549 |
||
13550 |
if ((error=table->file->index_prev(table->record[0]))) |
|
13551 |
return report_error(table, error); |
|
13552 |
if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key, |
|
13553 |
tab->ref.key_length)) |
|
13554 |
{ |
|
13555 |
table->status=STATUS_NOT_FOUND; |
|
13556 |
error= -1; |
|
13557 |
} |
|
13558 |
return error; |
|
13559 |
} |
|
13560 |
||
13561 |
||
13562 |
static int |
|
13563 |
join_init_quick_read_record(JOIN_TAB *tab) |
|
13564 |
{ |
|
13565 |
if (test_if_quick_select(tab) == -1) |
|
13566 |
return -1; /* No possible records */ |
|
13567 |
return join_init_read_record(tab); |
|
13568 |
} |
|
13569 |
||
13570 |
||
13571 |
int rr_sequential(READ_RECORD *info); |
|
13572 |
int init_read_record_seq(JOIN_TAB *tab) |
|
13573 |
{ |
|
13574 |
tab->read_record.read_record= rr_sequential; |
|
13575 |
if (tab->read_record.file->ha_rnd_init(1)) |
|
13576 |
return 1; |
|
13577 |
return (*tab->read_record.read_record)(&tab->read_record); |
|
13578 |
} |
|
13579 |
||
13580 |
static int |
|
13581 |
test_if_quick_select(JOIN_TAB *tab) |
|
13582 |
{ |
|
13583 |
delete tab->select->quick; |
|
13584 |
tab->select->quick=0; |
|
13585 |
return tab->select->test_quick_select(tab->join->thd, tab->keys, |
|
13586 |
(table_map) 0, HA_POS_ERROR, 0, |
|
55
by brian
Update for using real bool types. |
13587 |
false); |
1
by brian
clean slate |
13588 |
} |
13589 |
||
13590 |
||
13591 |
static int |
|
13592 |
join_init_read_record(JOIN_TAB *tab) |
|
13593 |
{ |
|
13594 |
if (tab->select && tab->select->quick && tab->select->quick->reset()) |
|
13595 |
return 1; |
|
13596 |
init_read_record(&tab->read_record, tab->join->thd, tab->table, |
|
13597 |
tab->select,1,1); |
|
13598 |
return (*tab->read_record.read_record)(&tab->read_record); |
|
13599 |
} |
|
13600 |
||
13601 |
||
13602 |
static int |
|
13603 |
join_read_first(JOIN_TAB *tab) |
|
13604 |
{ |
|
13605 |
int error; |
|
13606 |
TABLE *table=tab->table; |
|
13607 |
if (!table->key_read && table->covering_keys.is_set(tab->index) && |
|
13608 |
!table->no_keyread) |
|
13609 |
{ |
|
13610 |
table->key_read=1; |
|
13611 |
table->file->extra(HA_EXTRA_KEYREAD); |
|
13612 |
} |
|
13613 |
tab->table->status=0; |
|
13614 |
tab->read_record.table=table; |
|
13615 |
tab->read_record.file=table->file; |
|
13616 |
tab->read_record.index=tab->index; |
|
13617 |
tab->read_record.record=table->record[0]; |
|
13618 |
if (tab->insideout_match_tab) |
|
13619 |
{ |
|
13620 |
tab->read_record.do_insideout_scan= tab; |
|
13621 |
tab->read_record.read_record=join_read_next_different; |
|
13622 |
tab->insideout_match_tab->found_match= 0; |
|
13623 |
} |
|
13624 |
else |
|
13625 |
{ |
|
13626 |
tab->read_record.read_record=join_read_next; |
|
13627 |
tab->read_record.do_insideout_scan= 0; |
|
13628 |
} |
|
13629 |
||
13630 |
if (!table->file->inited) |
|
13631 |
table->file->ha_index_init(tab->index, tab->sorted); |
|
13632 |
if ((error=tab->table->file->index_first(tab->table->record[0]))) |
|
13633 |
{ |
|
13634 |
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE) |
|
13635 |
report_error(table, error); |
|
13636 |
return -1; |
|
13637 |
} |
|
13638 |
return 0; |
|
13639 |
} |
|
13640 |
||
13641 |
||
13642 |
static int |
|
13643 |
join_read_next_different(READ_RECORD *info) |
|
13644 |
{ |
|
13645 |
JOIN_TAB *tab= info->do_insideout_scan; |
|
13646 |
if (tab->insideout_match_tab->found_match) |
|
13647 |
{ |
|
13648 |
KEY *key= tab->table->key_info + tab->index; |
|
13649 |
do |
|
13650 |
{ |
|
13651 |
int error; |
|
13652 |
/* Save index tuple from record to the buffer */ |
|
13653 |
key_copy(tab->insideout_buf, info->record, key, 0); |
|
13654 |
||
13655 |
if ((error=info->file->index_next(info->record))) |
|
13656 |
return report_error(info->table, error); |
|
13657 |
||
13658 |
} while (!key_cmp(tab->table->key_info[tab->index].key_part, |
|
13659 |
tab->insideout_buf, key->key_length)); |
|
13660 |
tab->insideout_match_tab->found_match= 0; |
|
13661 |
return 0; |
|
13662 |
} |
|
13663 |
else |
|
13664 |
return join_read_next(info); |
|
13665 |
} |
|
13666 |
||
13667 |
||
13668 |
static int |
|
13669 |
join_read_next(READ_RECORD *info) |
|
13670 |
{ |
|
13671 |
int error; |
|
13672 |
if ((error=info->file->index_next(info->record))) |
|
13673 |
return report_error(info->table, error); |
|
13674 |
return 0; |
|
13675 |
} |
|
13676 |
||
13677 |
||
13678 |
static int |
|
13679 |
join_read_last(JOIN_TAB *tab) |
|
13680 |
{ |
|
13681 |
TABLE *table=tab->table; |
|
13682 |
int error; |
|
13683 |
if (!table->key_read && table->covering_keys.is_set(tab->index) && |
|
13684 |
!table->no_keyread) |
|
13685 |
{ |
|
13686 |
table->key_read=1; |
|
13687 |
table->file->extra(HA_EXTRA_KEYREAD); |
|
13688 |
} |
|
13689 |
tab->table->status=0; |
|
13690 |
tab->read_record.read_record=join_read_prev; |
|
13691 |
tab->read_record.table=table; |
|
13692 |
tab->read_record.file=table->file; |
|
13693 |
tab->read_record.index=tab->index; |
|
13694 |
tab->read_record.record=table->record[0]; |
|
13695 |
if (!table->file->inited) |
|
13696 |
table->file->ha_index_init(tab->index, 1); |
|
13697 |
if ((error= tab->table->file->index_last(tab->table->record[0]))) |
|
13698 |
return report_error(table, error); |
|
13699 |
return 0; |
|
13700 |
} |
|
13701 |
||
13702 |
||
13703 |
static int |
|
13704 |
join_read_prev(READ_RECORD *info) |
|
13705 |
{ |
|
13706 |
int error; |
|
13707 |
if ((error= info->file->index_prev(info->record))) |
|
13708 |
return report_error(info->table, error); |
|
13709 |
return 0; |
|
13710 |
} |
|
13711 |
||
13712 |
/** |
|
13713 |
Reading of key with key reference and one part that may be NULL. |
|
13714 |
*/ |
|
13715 |
||
13716 |
int |
|
13717 |
join_read_always_key_or_null(JOIN_TAB *tab) |
|
13718 |
{ |
|
13719 |
int res; |
|
13720 |
||
13721 |
/* First read according to key which is NOT NULL */ |
|
13722 |
*tab->ref.null_ref_key= 0; // Clear null byte |
|
13723 |
if ((res= join_read_always_key(tab)) >= 0) |
|
13724 |
return res; |
|
13725 |
||
13726 |
/* Then read key with null value */ |
|
13727 |
*tab->ref.null_ref_key= 1; // Set null byte |
|
13728 |
return safe_index_read(tab); |
|
13729 |
} |
|
13730 |
||
13731 |
||
13732 |
int |
|
13733 |
join_read_next_same_or_null(READ_RECORD *info) |
|
13734 |
{ |
|
13735 |
int error; |
|
13736 |
if ((error= join_read_next_same(info)) >= 0) |
|
13737 |
return error; |
|
13738 |
JOIN_TAB *tab= info->table->reginfo.join_tab; |
|
13739 |
||
13740 |
/* Test if we have already done a read after null key */ |
|
13741 |
if (*tab->ref.null_ref_key) |
|
13742 |
return -1; // All keys read |
|
13743 |
*tab->ref.null_ref_key= 1; // Set null byte |
|
13744 |
return safe_index_read(tab); // then read null keys |
|
13745 |
} |
|
13746 |
||
13747 |
||
13748 |
/***************************************************************************** |
|
13749 |
DESCRIPTION |
|
13750 |
Functions that end one nested loop iteration. Different functions |
|
13751 |
are used to support GROUP BY clause and to redirect records |
|
13752 |
to a table (e.g. in case of SELECT into a temporary table) or to the |
|
13753 |
network client. |
|
13754 |
||
13755 |
RETURN VALUES |
|
13756 |
NESTED_LOOP_OK - the record has been successfully handled |
|
13757 |
NESTED_LOOP_ERROR - a fatal error (like table corruption) |
|
13758 |
was detected |
|
13759 |
NESTED_LOOP_KILLED - thread shutdown was requested while processing |
|
13760 |
the record |
|
13761 |
NESTED_LOOP_QUERY_LIMIT - the record has been successfully handled; |
|
13762 |
additionally, the nested loop produced the |
|
13763 |
number of rows specified in the LIMIT clause |
|
13764 |
for the query |
|
13765 |
NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled; |
|
13766 |
additionally, there is a cursor and the nested |
|
13767 |
loop algorithm produced the number of rows |
|
13768 |
that is specified for current cursor fetch |
|
13769 |
operation. |
|
13770 |
All return values except NESTED_LOOP_OK abort the nested loop. |
|
13771 |
*****************************************************************************/ |
|
13772 |
||
13773 |
/* ARGSUSED */ |
|
13774 |
static enum_nested_loop_state |
|
13775 |
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), |
|
13776 |
bool end_of_records) |
|
13777 |
{ |
|
13778 |
if (!end_of_records) |
|
13779 |
{ |
|
13780 |
int error; |
|
13781 |
if (join->having && join->having->val_int() == 0) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13782 |
return(NESTED_LOOP_OK); // Didn't match having |
1
by brian
clean slate |
13783 |
error=0; |
13784 |
if (join->do_send_rows) |
|
13785 |
error=join->result->send_data(*join->fields); |
|
13786 |
if (error) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13787 |
return(NESTED_LOOP_ERROR); /* purecov: inspected */ |
1
by brian
clean slate |
13788 |
if (++join->send_records >= join->unit->select_limit_cnt && |
13789 |
join->do_send_rows) |
|
13790 |
{ |
|
13791 |
if (join->select_options & OPTION_FOUND_ROWS) |
|
13792 |
{ |
|
13793 |
JOIN_TAB *jt=join->join_tab; |
|
13794 |
if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group |
|
13795 |
&& !join->send_group_parts && !join->having && !jt->select_cond && |
|
13796 |
!(jt->select && jt->select->quick) && |
|
13797 |
(jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && |
|
13798 |
(jt->ref.key < 0)) |
|
13799 |
{ |
|
13800 |
/* Join over all rows in table; Return number of found rows */ |
|
13801 |
TABLE *table=jt->table; |
|
13802 |
||
13803 |
join->select_options ^= OPTION_FOUND_ROWS; |
|
13804 |
if (table->sort.record_pointers || |
|
13805 |
(table->sort.io_cache && my_b_inited(table->sort.io_cache))) |
|
13806 |
{ |
|
13807 |
/* Using filesort */ |
|
13808 |
join->send_records= table->sort.found_records; |
|
13809 |
} |
|
13810 |
else |
|
13811 |
{ |
|
13812 |
table->file->info(HA_STATUS_VARIABLE); |
|
13813 |
join->send_records= table->file->stats.records; |
|
13814 |
} |
|
13815 |
} |
|
13816 |
else |
|
13817 |
{ |
|
13818 |
join->do_send_rows= 0; |
|
13819 |
if (join->unit->fake_select_lex) |
|
13820 |
join->unit->fake_select_lex->select_limit= 0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13821 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
13822 |
} |
13823 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13824 |
return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely |
1
by brian
clean slate |
13825 |
} |
13826 |
else if (join->send_records >= join->fetch_limit) |
|
13827 |
{ |
|
13828 |
/* |
|
13829 |
There is a server side cursor and all rows for |
|
13830 |
this fetch request are sent. |
|
13831 |
*/ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13832 |
return(NESTED_LOOP_CURSOR_LIMIT); |
1
by brian
clean slate |
13833 |
} |
13834 |
} |
|
13835 |
||
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13836 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
13837 |
} |
13838 |
||
13839 |
||
13840 |
/* ARGSUSED */ |
|
13841 |
enum_nested_loop_state |
|
13842 |
end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), |
|
13843 |
bool end_of_records) |
|
13844 |
{ |
|
13845 |
int idx= -1; |
|
13846 |
enum_nested_loop_state ok_code= NESTED_LOOP_OK; |
|
13847 |
||
13848 |
if (!join->first_record || end_of_records || |
|
13849 |
(idx=test_if_item_cache_changed(join->group_fields)) >= 0) |
|
13850 |
{ |
|
13851 |
if (join->first_record || |
|
13852 |
(end_of_records && !join->group && !join->group_optimized_away)) |
|
13853 |
{ |
|
13854 |
if (idx < (int) join->send_group_parts) |
|
13855 |
{ |
|
13856 |
int error=0; |
|
13857 |
{ |
|
13858 |
if (!join->first_record) |
|
13859 |
{ |
|
13860 |
List_iterator_fast<Item> it(*join->fields); |
|
13861 |
Item *item; |
|
13862 |
/* No matching rows for group function */ |
|
13863 |
join->clear(); |
|
13864 |
||
13865 |
while ((item= it++)) |
|
13866 |
item->no_rows_in_result(); |
|
13867 |
} |
|
13868 |
if (join->having && join->having->val_int() == 0) |
|
13869 |
error= -1; // Didn't satisfy having |
|
13870 |
else |
|
13871 |
{ |
|
13872 |
if (join->do_send_rows) |
|
13873 |
error=join->result->send_data(*join->fields) ? 1 : 0; |
|
13874 |
join->send_records++; |
|
13875 |
} |
|
13876 |
if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0) |
|
13877 |
{ |
|
13878 |
if (join->rollup_send_data((uint) (idx+1))) |
|
13879 |
error= 1; |
|
13880 |
} |
|
13881 |
} |
|
13882 |
if (error > 0) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13883 |
return(NESTED_LOOP_ERROR); /* purecov: inspected */ |
1
by brian
clean slate |
13884 |
if (end_of_records) |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13885 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
13886 |
if (join->send_records >= join->unit->select_limit_cnt && |
13887 |
join->do_send_rows) |
|
13888 |
{ |
|
13889 |
if (!(join->select_options & OPTION_FOUND_ROWS)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13890 |
return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely |
1
by brian
clean slate |
13891 |
join->do_send_rows=0; |
13892 |
join->unit->select_limit_cnt = HA_POS_ERROR; |
|
13893 |
} |
|
13894 |
else if (join->send_records >= join->fetch_limit) |
|
13895 |
{ |
|
13896 |
/* |
|
13897 |
There is a server side cursor and all rows |
|
13898 |
for this fetch request are sent. |
|
13899 |
*/ |
|
13900 |
/* |
|
13901 |
Preventing code duplication. When finished with the group reset |
|
13902 |
the group functions and copy_fields. We fall through. bug #11904 |
|
13903 |
*/ |
|
13904 |
ok_code= NESTED_LOOP_CURSOR_LIMIT; |
|
13905 |
} |
|
13906 |
} |
|
13907 |
} |
|
13908 |
else |
|
13909 |
{ |
|
13910 |
if (end_of_records) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13911 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
13912 |
join->first_record=1; |
13913 |
VOID(test_if_item_cache_changed(join->group_fields)); |
|
13914 |
} |
|
13915 |
if (idx < (int) join->send_group_parts) |
|
13916 |
{ |
|
13917 |
/* |
|
13918 |
This branch is executed also for cursors which have finished their |
|
13919 |
fetch limit - the reason for ok_code. |
|
13920 |
*/ |
|
13921 |
copy_fields(&join->tmp_table_param); |
|
13922 |
if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1])) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13923 |
return(NESTED_LOOP_ERROR); |
13924 |
return(ok_code); |
|
1
by brian
clean slate |
13925 |
} |
13926 |
} |
|
13927 |
if (update_sum_func(join->sum_funcs)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13928 |
return(NESTED_LOOP_ERROR); |
13929 |
return(NESTED_LOOP_OK); |
|
1
by brian
clean slate |
13930 |
} |
13931 |
||
13932 |
||
13933 |
/* ARGSUSED */ |
|
13934 |
enum_nested_loop_state |
|
13935 |
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), |
|
13936 |
bool end_of_records) |
|
13937 |
{ |
|
13938 |
TABLE *table=join->tmp_table; |
|
13939 |
||
13940 |
if (join->thd->killed) // Aborted by user |
|
13941 |
{ |
|
13942 |
join->thd->send_kill_message(); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13943 |
return(NESTED_LOOP_KILLED); /* purecov: inspected */ |
1
by brian
clean slate |
13944 |
} |
13945 |
if (!end_of_records) |
|
13946 |
{ |
|
13947 |
copy_fields(&join->tmp_table_param); |
|
13948 |
copy_funcs(join->tmp_table_param.items_to_copy); |
|
13949 |
#ifdef TO_BE_DELETED |
|
13950 |
if (!table->uniques) // If not unique handling |
|
13951 |
{ |
|
13952 |
/* Copy null values from group to row */ |
|
13953 |
ORDER *group; |
|
13954 |
for (group=table->group ; group ; group=group->next) |
|
13955 |
{ |
|
13956 |
Item *item= *group->item; |
|
13957 |
if (item->maybe_null) |
|
13958 |
{ |
|
13959 |
Field *field=item->get_tmp_table_field(); |
|
13960 |
field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0); |
|
13961 |
} |
|
13962 |
} |
|
13963 |
} |
|
13964 |
#endif |
|
13965 |
if (!join->having || join->having->val_int()) |
|
13966 |
{ |
|
13967 |
int error; |
|
13968 |
join->found_records++; |
|
13969 |
if ((error=table->file->ha_write_row(table->record[0]))) |
|
13970 |
{ |
|
13971 |
if (!table->file->is_fatal_error(error, HA_CHECK_DUP)) |
|
13972 |
goto end; |
|
13973 |
if (create_myisam_from_heap(join->thd, table, |
|
13974 |
join->tmp_table_param.start_recinfo, |
|
13975 |
&join->tmp_table_param.recinfo, |
|
13976 |
error, 1)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13977 |
return(NESTED_LOOP_ERROR); // Not a table_is_full error |
1
by brian
clean slate |
13978 |
table->s->uniques=0; // To ensure rows are the same |
13979 |
} |
|
13980 |
if (++join->send_records >= join->tmp_table_param.end_write_records && |
|
13981 |
join->do_send_rows) |
|
13982 |
{ |
|
13983 |
if (!(join->select_options & OPTION_FOUND_ROWS)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13984 |
return(NESTED_LOOP_QUERY_LIMIT); |
1
by brian
clean slate |
13985 |
join->do_send_rows=0; |
13986 |
join->unit->select_limit_cnt = HA_POS_ERROR; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13987 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
13988 |
} |
13989 |
} |
|
13990 |
} |
|
13991 |
end: |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
13992 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
13993 |
} |
13994 |
||
13995 |
/* ARGSUSED */ |
|
13996 |
/** Group by searching after group record and updating it if possible. */ |
|
13997 |
||
13998 |
static enum_nested_loop_state |
|
13999 |
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), |
|
14000 |
bool end_of_records) |
|
14001 |
{ |
|
14002 |
TABLE *table=join->tmp_table; |
|
14003 |
ORDER *group; |
|
14004 |
int error; |
|
14005 |
||
14006 |
if (end_of_records) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14007 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
14008 |
if (join->thd->killed) // Aborted by user |
14009 |
{ |
|
14010 |
join->thd->send_kill_message(); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14011 |
return(NESTED_LOOP_KILLED); /* purecov: inspected */ |
1
by brian
clean slate |
14012 |
} |
14013 |
||
14014 |
join->found_records++; |
|
14015 |
copy_fields(&join->tmp_table_param); // Groups are copied twice. |
|
14016 |
/* Make a key of group index */ |
|
14017 |
for (group=table->group ; group ; group=group->next) |
|
14018 |
{ |
|
14019 |
Item *item= *group->item; |
|
14020 |
item->save_org_in_field(group->field); |
|
14021 |
/* Store in the used key if the field was 0 */ |
|
14022 |
if (item->maybe_null) |
|
14023 |
group->buff[-1]= (char) group->field->is_null(); |
|
14024 |
} |
|
14025 |
if (!table->file->index_read_map(table->record[1], |
|
14026 |
join->tmp_table_param.group_buff, |
|
14027 |
HA_WHOLE_KEY, |
|
14028 |
HA_READ_KEY_EXACT)) |
|
14029 |
{ /* Update old record */ |
|
14030 |
restore_record(table,record[1]); |
|
14031 |
update_tmptable_sum_func(join->sum_funcs,table); |
|
14032 |
if ((error=table->file->ha_update_row(table->record[1], |
|
14033 |
table->record[0]))) |
|
14034 |
{ |
|
14035 |
table->file->print_error(error,MYF(0)); /* purecov: inspected */ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14036 |
return(NESTED_LOOP_ERROR); /* purecov: inspected */ |
1
by brian
clean slate |
14037 |
} |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14038 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
14039 |
} |
14040 |
||
14041 |
/* |
|
14042 |
Copy null bits from group key to table |
|
14043 |
We can't copy all data as the key may have different format |
|
14044 |
as the row data (for example as with VARCHAR keys) |
|
14045 |
*/ |
|
14046 |
KEY_PART_INFO *key_part; |
|
14047 |
for (group=table->group,key_part=table->key_info[0].key_part; |
|
14048 |
group ; |
|
14049 |
group=group->next,key_part++) |
|
14050 |
{ |
|
14051 |
if (key_part->null_bit) |
|
14052 |
memcpy(table->record[0]+key_part->offset, group->buff, 1); |
|
14053 |
} |
|
14054 |
init_tmptable_sum_functions(join->sum_funcs); |
|
14055 |
copy_funcs(join->tmp_table_param.items_to_copy); |
|
14056 |
if ((error=table->file->ha_write_row(table->record[0]))) |
|
14057 |
{ |
|
14058 |
if (create_myisam_from_heap(join->thd, table, |
|
14059 |
join->tmp_table_param.start_recinfo, |
|
14060 |
&join->tmp_table_param.recinfo, |
|
14061 |
error, 0)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14062 |
return(NESTED_LOOP_ERROR); // Not a table_is_full error |
1
by brian
clean slate |
14063 |
/* Change method to update rows */ |
14064 |
table->file->ha_index_init(0, 0); |
|
14065 |
join->join_tab[join->tables-1].next_select=end_unique_update; |
|
14066 |
} |
|
14067 |
join->send_records++; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14068 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
14069 |
} |
14070 |
||
14071 |
||
14072 |
/** Like end_update, but this is done with unique constraints instead of keys. */ |
|
14073 |
||
14074 |
static enum_nested_loop_state |
|
14075 |
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), |
|
14076 |
bool end_of_records) |
|
14077 |
{ |
|
14078 |
TABLE *table=join->tmp_table; |
|
14079 |
int error; |
|
14080 |
||
14081 |
if (end_of_records) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14082 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
14083 |
if (join->thd->killed) // Aborted by user |
14084 |
{ |
|
14085 |
join->thd->send_kill_message(); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14086 |
return(NESTED_LOOP_KILLED); /* purecov: inspected */ |
1
by brian
clean slate |
14087 |
} |
14088 |
||
14089 |
init_tmptable_sum_functions(join->sum_funcs); |
|
14090 |
copy_fields(&join->tmp_table_param); // Groups are copied twice. |
|
14091 |
copy_funcs(join->tmp_table_param.items_to_copy); |
|
14092 |
||
14093 |
if (!(error=table->file->ha_write_row(table->record[0]))) |
|
14094 |
join->send_records++; // New group |
|
14095 |
else |
|
14096 |
{ |
|
14097 |
if ((int) table->file->get_dup_key(error) < 0) |
|
14098 |
{ |
|
14099 |
table->file->print_error(error,MYF(0)); /* purecov: inspected */ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14100 |
return(NESTED_LOOP_ERROR); /* purecov: inspected */ |
1
by brian
clean slate |
14101 |
} |
14102 |
if (table->file->rnd_pos(table->record[1],table->file->dup_ref)) |
|
14103 |
{ |
|
14104 |
table->file->print_error(error,MYF(0)); /* purecov: inspected */ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14105 |
return(NESTED_LOOP_ERROR); /* purecov: inspected */ |
1
by brian
clean slate |
14106 |
} |
14107 |
restore_record(table,record[1]); |
|
14108 |
update_tmptable_sum_func(join->sum_funcs,table); |
|
14109 |
if ((error=table->file->ha_update_row(table->record[1], |
|
14110 |
table->record[0]))) |
|
14111 |
{ |
|
14112 |
table->file->print_error(error,MYF(0)); /* purecov: inspected */ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14113 |
return(NESTED_LOOP_ERROR); /* purecov: inspected */ |
1
by brian
clean slate |
14114 |
} |
14115 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14116 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
14117 |
} |
14118 |
||
14119 |
||
14120 |
/* ARGSUSED */ |
|
14121 |
enum_nested_loop_state |
|
14122 |
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), |
|
14123 |
bool end_of_records) |
|
14124 |
{ |
|
14125 |
TABLE *table=join->tmp_table; |
|
14126 |
int idx= -1; |
|
14127 |
||
14128 |
if (join->thd->killed) |
|
14129 |
{ // Aborted by user |
|
14130 |
join->thd->send_kill_message(); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14131 |
return(NESTED_LOOP_KILLED); /* purecov: inspected */ |
1
by brian
clean slate |
14132 |
} |
14133 |
if (!join->first_record || end_of_records || |
|
14134 |
(idx=test_if_item_cache_changed(join->group_fields)) >= 0) |
|
14135 |
{ |
|
14136 |
if (join->first_record || (end_of_records && !join->group)) |
|
14137 |
{ |
|
14138 |
int send_group_parts= join->send_group_parts; |
|
14139 |
if (idx < send_group_parts) |
|
14140 |
{ |
|
14141 |
if (!join->first_record) |
|
14142 |
{ |
|
14143 |
/* No matching rows for group function */ |
|
14144 |
join->clear(); |
|
14145 |
} |
|
14146 |
copy_sum_funcs(join->sum_funcs, |
|
14147 |
join->sum_funcs_end[send_group_parts]); |
|
14148 |
if (!join->having || join->having->val_int()) |
|
14149 |
{ |
|
14150 |
int error= table->file->ha_write_row(table->record[0]); |
|
14151 |
if (error && create_myisam_from_heap(join->thd, table, |
|
14152 |
join->tmp_table_param.start_recinfo, |
|
14153 |
&join->tmp_table_param.recinfo, |
|
14154 |
error, 0)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14155 |
return(NESTED_LOOP_ERROR); |
1
by brian
clean slate |
14156 |
} |
14157 |
if (join->rollup.state != ROLLUP::STATE_NONE) |
|
14158 |
{ |
|
14159 |
if (join->rollup_write_data((uint) (idx+1), table)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14160 |
return(NESTED_LOOP_ERROR); |
1
by brian
clean slate |
14161 |
} |
14162 |
if (end_of_records) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14163 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
14164 |
} |
14165 |
} |
|
14166 |
else |
|
14167 |
{ |
|
14168 |
if (end_of_records) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14169 |
return(NESTED_LOOP_OK); |
1
by brian
clean slate |
14170 |
join->first_record=1; |
14171 |
VOID(test_if_item_cache_changed(join->group_fields)); |
|
14172 |
} |
|
14173 |
if (idx < (int) join->send_group_parts) |
|
14174 |
{ |
|
14175 |
copy_fields(&join->tmp_table_param); |
|
14176 |
copy_funcs(join->tmp_table_param.items_to_copy); |
|
14177 |
if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1])) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14178 |
return(NESTED_LOOP_ERROR); |
14179 |
return(NESTED_LOOP_OK); |
|
1
by brian
clean slate |
14180 |
} |
14181 |
} |
|
14182 |
if (update_sum_func(join->sum_funcs)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14183 |
return(NESTED_LOOP_ERROR); |
14184 |
return(NESTED_LOOP_OK); |
|
1
by brian
clean slate |
14185 |
} |
14186 |
||
14187 |
||
14188 |
/***************************************************************************** |
|
14189 |
Remove calculation with tables that aren't yet read. Remove also tests |
|
14190 |
against fields that are read through key where the table is not a |
|
14191 |
outer join table. |
|
14192 |
We can't remove tests that are made against columns which are stored |
|
14193 |
in sorted order. |
|
14194 |
*****************************************************************************/ |
|
14195 |
||
14196 |
/** |
|
14197 |
@return |
|
14198 |
1 if right_item is used removable reference key on left_item |
|
14199 |
*/ |
|
14200 |
||
14201 |
static bool test_if_ref(Item_field *left_item,Item *right_item) |
|
14202 |
{ |
|
14203 |
Field *field=left_item->field; |
|
14204 |
// No need to change const test. We also have to keep tests on LEFT JOIN |
|
14205 |
if (!field->table->const_table && !field->table->maybe_null) |
|
14206 |
{ |
|
14207 |
Item *ref_item=part_of_refkey(field->table,field); |
|
14208 |
if (ref_item && ref_item->eq(right_item,1)) |
|
14209 |
{ |
|
14210 |
right_item= right_item->real_item(); |
|
14211 |
if (right_item->type() == Item::FIELD_ITEM) |
|
14212 |
return (field->eq_def(((Item_field *) right_item)->field)); |
|
14213 |
/* remove equalities injected by IN->EXISTS transformation */ |
|
14214 |
else if (right_item->type() == Item::CACHE_ITEM) |
|
14215 |
return ((Item_cache *)right_item)->eq_def (field); |
|
14216 |
if (right_item->const_item() && !(right_item->is_null())) |
|
14217 |
{ |
|
14218 |
/* |
|
14219 |
We can remove binary fields and numerical fields except float, |
|
14220 |
as float comparison isn't 100 % secure |
|
14221 |
We have to keep normal strings to be able to check for end spaces |
|
14222 |
||
14223 |
sergefp: the above seems to be too restrictive. Counterexample: |
|
14224 |
create table t100 (v varchar(10), key(v)) default charset=latin1; |
|
14225 |
insert into t100 values ('a'),('a '); |
|
14226 |
explain select * from t100 where v='a'; |
|
14227 |
The EXPLAIN shows 'using Where'. Running the query returns both |
|
14228 |
rows, so it seems there are no problems with endspace in the most |
|
14229 |
frequent case? |
|
14230 |
*/ |
|
14231 |
if (field->binary() && |
|
14232 |
field->real_type() != MYSQL_TYPE_STRING && |
|
14233 |
field->real_type() != MYSQL_TYPE_VARCHAR && |
|
166
by Brian Aker
Removal of FLOAT type |
14234 |
field->decimals() == 0) |
1
by brian
clean slate |
14235 |
{ |
14236 |
return !store_val_in_field(field, right_item, CHECK_FIELD_WARN); |
|
14237 |
} |
|
14238 |
} |
|
14239 |
} |
|
14240 |
} |
|
14241 |
return 0; // keep test |
|
14242 |
} |
|
14243 |
||
14244 |
/** |
|
14245 |
@brief Replaces an expression destructively inside the expression tree of |
|
14246 |
the WHERE clase. |
|
14247 |
||
14248 |
@note Because of current requirements for semijoin flattening, we do not |
|
14249 |
need to recurse here, hence this function will only examine the top-level |
|
14250 |
AND conditions. (see JOIN::prepare, comment above the line |
|
14251 |
'if (do_materialize)' |
|
14252 |
||
14253 |
@param join The top-level query. |
|
14254 |
@param old_cond The expression to be replaced. |
|
14255 |
@param new_cond The expression to be substituted. |
|
14256 |
@param do_fix_fields If true, Item::fix_fields(THD*, Item**) is called for |
|
14257 |
the new expression. |
|
14258 |
@return <code>true</code> if there was an error, <code>false</code> if |
|
14259 |
successful. |
|
14260 |
*/ |
|
14261 |
static bool replace_where_subcondition(JOIN *join, Item *old_cond, |
|
14262 |
Item *new_cond, bool do_fix_fields) |
|
14263 |
{ |
|
14264 |
if (join->conds == old_cond) { |
|
14265 |
join->conds= new_cond; |
|
14266 |
if (do_fix_fields) |
|
14267 |
new_cond->fix_fields(join->thd, &join->conds); |
|
55
by brian
Update for using real bool types. |
14268 |
return false; |
1
by brian
clean slate |
14269 |
} |
14270 |
||
14271 |
if (join->conds->type() == Item::COND_ITEM) { |
|
14272 |
List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list()); |
|
14273 |
Item *item; |
|
14274 |
while ((item= li++)) |
|
14275 |
if (item == old_cond) |
|
14276 |
{ |
|
14277 |
li.replace(new_cond); |
|
14278 |
if (do_fix_fields) |
|
14279 |
new_cond->fix_fields(join->thd, li.ref()); |
|
55
by brian
Update for using real bool types. |
14280 |
return false; |
1
by brian
clean slate |
14281 |
} |
14282 |
} |
|
14283 |
||
55
by brian
Update for using real bool types. |
14284 |
return true; |
1
by brian
clean slate |
14285 |
} |
14286 |
||
14287 |
/* |
|
14288 |
Extract a condition that can be checked after reading given table |
|
14289 |
||
14290 |
SYNOPSIS |
|
14291 |
make_cond_for_table() |
|
14292 |
cond Condition to analyze |
|
14293 |
tables Tables for which "current field values" are available |
|
14294 |
used_table Table that we're extracting the condition for (may |
|
14295 |
also include PSEUDO_TABLE_BITS |
|
14296 |
||
14297 |
DESCRIPTION |
|
14298 |
Extract the condition that can be checked after reading the table |
|
14299 |
specified in 'used_table', given that current-field values for tables |
|
14300 |
specified in 'tables' bitmap are available. |
|
14301 |
||
14302 |
The function assumes that |
|
14303 |
- Constant parts of the condition has already been checked. |
|
14304 |
- Condition that could be checked for tables in 'tables' has already |
|
14305 |
been checked. |
|
14306 |
||
14307 |
The function takes into account that some parts of the condition are |
|
14308 |
guaranteed to be true by employed 'ref' access methods (the code that |
|
14309 |
does this is located at the end, search down for "EQ_FUNC"). |
|
14310 |
||
14311 |
||
14312 |
SEE ALSO |
|
14313 |
make_cond_for_info_schema uses similar algorithm |
|
14314 |
||
14315 |
RETURN |
|
14316 |
Extracted condition |
|
14317 |
*/ |
|
14318 |
||
14319 |
static COND * |
|
14320 |
make_cond_for_table(COND *cond, table_map tables, table_map used_table, |
|
14321 |
bool exclude_expensive_cond) |
|
14322 |
{ |
|
14323 |
if (used_table && !(cond->used_tables() & used_table) && |
|
14324 |
/* |
|
14325 |
Exclude constant conditions not checked at optimization time if |
|
14326 |
the table we are pushing conditions to is the first one. |
|
14327 |
As a result, such conditions are not considered as already checked |
|
14328 |
and will be checked at execution time, attached to the first table. |
|
14329 |
*/ |
|
14330 |
!((used_table & 1) && cond->is_expensive())) |
|
14331 |
return (COND*) 0; // Already checked |
|
14332 |
if (cond->type() == Item::COND_ITEM) |
|
14333 |
{ |
|
14334 |
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC) |
|
14335 |
{ |
|
14336 |
/* Create new top level AND item */ |
|
14337 |
Item_cond_and *new_cond=new Item_cond_and; |
|
14338 |
if (!new_cond) |
|
14339 |
return (COND*) 0; // OOM /* purecov: inspected */ |
|
14340 |
List_iterator<Item> li(*((Item_cond*) cond)->argument_list()); |
|
14341 |
Item *item; |
|
14342 |
while ((item=li++)) |
|
14343 |
{ |
|
14344 |
Item *fix=make_cond_for_table(item,tables,used_table, |
|
14345 |
exclude_expensive_cond); |
|
14346 |
if (fix) |
|
14347 |
new_cond->argument_list()->push_back(fix); |
|
14348 |
} |
|
14349 |
switch (new_cond->argument_list()->elements) { |
|
14350 |
case 0: |
|
14351 |
return (COND*) 0; // Always true |
|
14352 |
case 1: |
|
14353 |
return new_cond->argument_list()->head(); |
|
14354 |
default: |
|
14355 |
/* |
|
14356 |
Item_cond_and do not need fix_fields for execution, its parameters |
|
14357 |
are fixed or do not need fix_fields, too |
|
14358 |
*/ |
|
14359 |
new_cond->quick_fix_field(); |
|
14360 |
new_cond->used_tables_cache= |
|
14361 |
((Item_cond_and*) cond)->used_tables_cache & |
|
14362 |
tables; |
|
14363 |
return new_cond; |
|
14364 |
} |
|
14365 |
} |
|
14366 |
else |
|
14367 |
{ // Or list |
|
14368 |
Item_cond_or *new_cond=new Item_cond_or; |
|
14369 |
if (!new_cond) |
|
14370 |
return (COND*) 0; // OOM /* purecov: inspected */ |
|
14371 |
List_iterator<Item> li(*((Item_cond*) cond)->argument_list()); |
|
14372 |
Item *item; |
|
14373 |
while ((item=li++)) |
|
14374 |
{ |
|
14375 |
Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond); |
|
14376 |
if (!fix) |
|
14377 |
return (COND*) 0; // Always true |
|
14378 |
new_cond->argument_list()->push_back(fix); |
|
14379 |
} |
|
14380 |
/* |
|
14381 |
Item_cond_and do not need fix_fields for execution, its parameters |
|
14382 |
are fixed or do not need fix_fields, too |
|
14383 |
*/ |
|
14384 |
new_cond->quick_fix_field(); |
|
14385 |
new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache; |
|
14386 |
new_cond->top_level_item(); |
|
14387 |
return new_cond; |
|
14388 |
} |
|
14389 |
} |
|
14390 |
||
14391 |
/* |
|
14392 |
Because the following test takes a while and it can be done |
|
14393 |
table_count times, we mark each item that we have examined with the result |
|
14394 |
of the test |
|
14395 |
*/ |
|
14396 |
||
14397 |
if (cond->marker == 3 || (cond->used_tables() & ~tables) || |
|
14398 |
/* |
|
14399 |
When extracting constant conditions, treat expensive conditions as |
|
14400 |
non-constant, so that they are not evaluated at optimization time. |
|
14401 |
*/ |
|
14402 |
(!used_table && exclude_expensive_cond && cond->is_expensive())) |
|
14403 |
return (COND*) 0; // Can't check this yet |
|
14404 |
if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK) |
|
14405 |
return cond; // Not boolean op |
|
14406 |
||
14407 |
/* |
|
14408 |
Remove equalities that are guaranteed to be true by use of 'ref' access |
|
14409 |
method |
|
14410 |
*/ |
|
14411 |
if (((Item_func*) cond)->functype() == Item_func::EQ_FUNC) |
|
14412 |
{ |
|
14413 |
Item *left_item= ((Item_func*) cond)->arguments()[0]; |
|
14414 |
Item *right_item= ((Item_func*) cond)->arguments()[1]; |
|
14415 |
if (left_item->type() == Item::FIELD_ITEM && |
|
14416 |
test_if_ref((Item_field*) left_item,right_item)) |
|
14417 |
{ |
|
14418 |
cond->marker=3; // Checked when read |
|
14419 |
return (COND*) 0; |
|
14420 |
} |
|
14421 |
if (right_item->type() == Item::FIELD_ITEM && |
|
14422 |
test_if_ref((Item_field*) right_item,left_item)) |
|
14423 |
{ |
|
14424 |
cond->marker=3; // Checked when read |
|
14425 |
return (COND*) 0; |
|
14426 |
} |
|
14427 |
} |
|
14428 |
cond->marker=2; |
|
14429 |
return cond; |
|
14430 |
} |
|
14431 |
||
14432 |
||
14433 |
static Item * |
|
14434 |
part_of_refkey(TABLE *table,Field *field) |
|
14435 |
{ |
|
14436 |
if (!table->reginfo.join_tab) |
|
14437 |
return (Item*) 0; // field from outer non-select (UPDATE,...) |
|
14438 |
||
14439 |
uint ref_parts=table->reginfo.join_tab->ref.key_parts; |
|
14440 |
if (ref_parts) |
|
14441 |
{ |
|
14442 |
KEY_PART_INFO *key_part= |
|
14443 |
table->key_info[table->reginfo.join_tab->ref.key].key_part; |
|
14444 |
uint part; |
|
14445 |
||
14446 |
for (part=0 ; part < ref_parts ; part++) |
|
14447 |
{ |
|
14448 |
if (table->reginfo.join_tab->ref.cond_guards[part]) |
|
14449 |
return 0; |
|
14450 |
} |
|
14451 |
||
14452 |
for (part=0 ; part < ref_parts ; part++,key_part++) |
|
14453 |
if (field->eq(key_part->field) && |
|
14454 |
!(key_part->key_part_flag & HA_PART_KEY_SEG)) |
|
14455 |
return table->reginfo.join_tab->ref.items[part]; |
|
14456 |
} |
|
14457 |
return (Item*) 0; |
|
14458 |
} |
|
14459 |
||
14460 |
||
14461 |
/** |
|
14462 |
Test if one can use the key to resolve ORDER BY. |
|
14463 |
||
14464 |
@param order Sort order |
|
14465 |
@param table Table to sort |
|
14466 |
@param idx Index to check |
|
14467 |
@param used_key_parts Return value for used key parts. |
|
14468 |
||
14469 |
||
14470 |
@note |
|
14471 |
used_key_parts is set to correct key parts used if return value != 0 |
|
14472 |
(On other cases, used_key_part may be changed) |
|
14473 |
||
14474 |
@retval |
|
14475 |
1 key is ok. |
|
14476 |
@retval |
|
14477 |
0 Key can't be used |
|
14478 |
@retval |
|
14479 |
-1 Reverse key can be used |
|
14480 |
*/ |
|
14481 |
||
14482 |
static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx, |
|
14483 |
uint *used_key_parts) |
|
14484 |
{ |
|
14485 |
KEY_PART_INFO *key_part,*key_part_end; |
|
14486 |
key_part=table->key_info[idx].key_part; |
|
14487 |
key_part_end=key_part+table->key_info[idx].key_parts; |
|
14488 |
key_part_map const_key_parts=table->const_key_parts[idx]; |
|
14489 |
int reverse=0; |
|
148
by Brian Aker
my_bool cleanup |
14490 |
bool on_primary_key= false; |
1
by brian
clean slate |
14491 |
|
14492 |
for (; order ; order=order->next, const_key_parts>>=1) |
|
14493 |
{ |
|
14494 |
Field *field=((Item_field*) (*order->item)->real_item())->field; |
|
14495 |
int flag; |
|
14496 |
||
14497 |
/* |
|
14498 |
Skip key parts that are constants in the WHERE clause. |
|
14499 |
These are already skipped in the ORDER BY by const_expression_in_where() |
|
14500 |
*/ |
|
14501 |
for (; const_key_parts & 1 ; const_key_parts>>= 1) |
|
14502 |
key_part++; |
|
14503 |
||
14504 |
if (key_part == key_part_end) |
|
14505 |
{ |
|
14506 |
/* |
|
14507 |
We are at the end of the key. Check if the engine has the primary |
|
14508 |
key as a suffix to the secondary keys. If it has continue to check |
|
14509 |
the primary key as a suffix. |
|
14510 |
*/ |
|
14511 |
if (!on_primary_key && |
|
14512 |
(table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) && |
|
14513 |
table->s->primary_key != MAX_KEY) |
|
14514 |
{ |
|
55
by brian
Update for using real bool types. |
14515 |
on_primary_key= true; |
1
by brian
clean slate |
14516 |
key_part= table->key_info[table->s->primary_key].key_part; |
14517 |
key_part_end=key_part+table->key_info[table->s->primary_key].key_parts; |
|
14518 |
const_key_parts=table->const_key_parts[table->s->primary_key]; |
|
14519 |
||
14520 |
for (; const_key_parts & 1 ; const_key_parts>>= 1) |
|
14521 |
key_part++; |
|
14522 |
/* |
|
14523 |
The primary and secondary key parts were all const (i.e. there's |
|
14524 |
one row). The sorting doesn't matter. |
|
14525 |
*/ |
|
14526 |
if (key_part == key_part_end && reverse == 0) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14527 |
return(1); |
1
by brian
clean slate |
14528 |
} |
14529 |
else |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14530 |
return(0); |
1
by brian
clean slate |
14531 |
} |
14532 |
||
14533 |
if (key_part->field != field) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14534 |
return(0); |
1
by brian
clean slate |
14535 |
|
14536 |
/* set flag to 1 if we can use read-next on key, else to -1 */ |
|
14537 |
flag= ((order->asc == !(key_part->key_part_flag & HA_REVERSE_SORT)) ? |
|
14538 |
1 : -1); |
|
14539 |
if (reverse && flag != reverse) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14540 |
return(0); |
1
by brian
clean slate |
14541 |
reverse=flag; // Remember if reverse |
14542 |
key_part++; |
|
14543 |
} |
|
14544 |
*used_key_parts= on_primary_key ? table->key_info[idx].key_parts : |
|
14545 |
(uint) (key_part - table->key_info[idx].key_part); |
|
14546 |
if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) & |
|
14547 |
HA_READ_PREV)) |
|
14548 |
reverse= 0; // Index can't be used |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14549 |
return(reverse); |
1
by brian
clean slate |
14550 |
} |
14551 |
||
14552 |
||
14553 |
uint find_shortest_key(TABLE *table, const key_map *usable_keys) |
|
14554 |
{ |
|
14555 |
uint min_length= (uint) ~0; |
|
14556 |
uint best= MAX_KEY; |
|
14557 |
if (!usable_keys->is_clear_all()) |
|
14558 |
{ |
|
14559 |
for (uint nr=0; nr < table->s->keys ; nr++) |
|
14560 |
{ |
|
14561 |
if (usable_keys->is_set(nr)) |
|
14562 |
{ |
|
14563 |
if (table->key_info[nr].key_length < min_length) |
|
14564 |
{ |
|
14565 |
min_length=table->key_info[nr].key_length; |
|
14566 |
best=nr; |
|
14567 |
} |
|
14568 |
} |
|
14569 |
} |
|
14570 |
} |
|
14571 |
return best; |
|
14572 |
} |
|
14573 |
||
14574 |
/** |
|
14575 |
Test if a second key is the subkey of the first one. |
|
14576 |
||
14577 |
@param key_part First key parts |
|
14578 |
@param ref_key_part Second key parts |
|
14579 |
@param ref_key_part_end Last+1 part of the second key |
|
14580 |
||
14581 |
@note |
|
14582 |
Second key MUST be shorter than the first one. |
|
14583 |
||
14584 |
@retval |
|
14585 |
1 is a subkey |
|
14586 |
@retval |
|
14587 |
0 no sub key |
|
14588 |
*/ |
|
14589 |
||
14590 |
inline bool |
|
14591 |
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part, |
|
14592 |
KEY_PART_INFO *ref_key_part_end) |
|
14593 |
{ |
|
14594 |
for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++) |
|
14595 |
if (!key_part->field->eq(ref_key_part->field)) |
|
14596 |
return 0; |
|
14597 |
return 1; |
|
14598 |
} |
|
14599 |
||
14600 |
/** |
|
14601 |
Test if we can use one of the 'usable_keys' instead of 'ref' key |
|
14602 |
for sorting. |
|
14603 |
||
14604 |
@param ref Number of key, used for WHERE clause |
|
14605 |
@param usable_keys Keys for testing |
|
14606 |
||
14607 |
@return |
|
14608 |
- MAX_KEY If we can't use other key |
|
14609 |
- the number of found key Otherwise |
|
14610 |
*/ |
|
14611 |
||
14612 |
static uint |
|
14613 |
test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts, |
|
14614 |
const key_map *usable_keys) |
|
14615 |
{ |
|
14616 |
uint nr; |
|
14617 |
uint min_length= (uint) ~0; |
|
14618 |
uint best= MAX_KEY; |
|
14619 |
uint not_used; |
|
14620 |
KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part; |
|
14621 |
KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts; |
|
14622 |
||
14623 |
for (nr= 0 ; nr < table->s->keys ; nr++) |
|
14624 |
{ |
|
14625 |
if (usable_keys->is_set(nr) && |
|
14626 |
table->key_info[nr].key_length < min_length && |
|
14627 |
table->key_info[nr].key_parts >= ref_key_parts && |
|
14628 |
is_subkey(table->key_info[nr].key_part, ref_key_part, |
|
14629 |
ref_key_part_end) && |
|
14630 |
test_if_order_by_key(order, table, nr, ¬_used)) |
|
14631 |
{ |
|
14632 |
min_length= table->key_info[nr].key_length; |
|
14633 |
best= nr; |
|
14634 |
} |
|
14635 |
} |
|
14636 |
return best; |
|
14637 |
} |
|
14638 |
||
14639 |
||
14640 |
/** |
|
14641 |
Check if GROUP BY/DISTINCT can be optimized away because the set is |
|
14642 |
already known to be distinct. |
|
14643 |
||
14644 |
Used in removing the GROUP BY/DISTINCT of the following types of |
|
14645 |
statements: |
|
14646 |
@code |
|
14647 |
SELECT [DISTINCT] <unique_key_cols>... FROM <single_table_ref> |
|
14648 |
[GROUP BY <unique_key_cols>,...] |
|
14649 |
@endcode |
|
14650 |
||
14651 |
If (a,b,c is distinct) |
|
14652 |
then <any combination of a,b,c>,{whatever} is also distinct |
|
14653 |
||
14654 |
This function checks if all the key parts of any of the unique keys |
|
14655 |
of the table are referenced by a list : either the select list |
|
14656 |
through find_field_in_item_list or GROUP BY list through |
|
14657 |
find_field_in_order_list. |
|
14658 |
If the above holds and the key parts cannot contain NULLs then we |
|
14659 |
can safely remove the GROUP BY/DISTINCT, |
|
14660 |
as no result set can be more distinct than an unique key. |
|
14661 |
||
14662 |
@param table The table to operate on. |
|
14663 |
@param find_func function to iterate over the list and search |
|
14664 |
for a field |
|
14665 |
||
14666 |
@retval |
|
14667 |
1 found |
|
14668 |
@retval |
|
14669 |
0 not found. |
|
14670 |
*/ |
|
14671 |
||
14672 |
static bool |
|
14673 |
list_contains_unique_index(TABLE *table, |
|
14674 |
bool (*find_func) (Field *, void *), void *data) |
|
14675 |
{ |
|
14676 |
for (uint keynr= 0; keynr < table->s->keys; keynr++) |
|
14677 |
{ |
|
14678 |
if (keynr == table->s->primary_key || |
|
14679 |
(table->key_info[keynr].flags & HA_NOSAME)) |
|
14680 |
{ |
|
14681 |
KEY *keyinfo= table->key_info + keynr; |
|
14682 |
KEY_PART_INFO *key_part, *key_part_end; |
|
14683 |
||
14684 |
for (key_part=keyinfo->key_part, |
|
14685 |
key_part_end=key_part+ keyinfo->key_parts; |
|
14686 |
key_part < key_part_end; |
|
14687 |
key_part++) |
|
14688 |
{ |
|
14689 |
if (key_part->field->maybe_null() || |
|
14690 |
!find_func(key_part->field, data)) |
|
14691 |
break; |
|
14692 |
} |
|
14693 |
if (key_part == key_part_end) |
|
14694 |
return 1; |
|
14695 |
} |
|
14696 |
} |
|
14697 |
return 0; |
|
14698 |
} |
|
14699 |
||
14700 |
||
14701 |
/** |
|
14702 |
Helper function for list_contains_unique_index. |
|
14703 |
Find a field reference in a list of ORDER structures. |
|
14704 |
Finds a direct reference of the Field in the list. |
|
14705 |
||
14706 |
@param field The field to search for. |
|
14707 |
@param data ORDER *.The list to search in |
|
14708 |
||
14709 |
@retval |
|
14710 |
1 found |
|
14711 |
@retval |
|
14712 |
0 not found. |
|
14713 |
*/ |
|
14714 |
||
14715 |
static bool |
|
14716 |
find_field_in_order_list (Field *field, void *data) |
|
14717 |
{ |
|
14718 |
ORDER *group= (ORDER *) data; |
|
14719 |
bool part_found= 0; |
|
14720 |
for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next) |
|
14721 |
{ |
|
14722 |
Item *item= (*tmp_group->item)->real_item(); |
|
14723 |
if (item->type() == Item::FIELD_ITEM && |
|
14724 |
((Item_field*) item)->field->eq(field)) |
|
14725 |
{ |
|
14726 |
part_found= 1; |
|
14727 |
break; |
|
14728 |
} |
|
14729 |
} |
|
14730 |
return part_found; |
|
14731 |
} |
|
14732 |
||
14733 |
||
14734 |
/** |
|
14735 |
Helper function for list_contains_unique_index. |
|
14736 |
Find a field reference in a dynamic list of Items. |
|
14737 |
Finds a direct reference of the Field in the list. |
|
14738 |
||
14739 |
@param[in] field The field to search for. |
|
14740 |
@param[in] data List<Item> *.The list to search in |
|
14741 |
||
14742 |
@retval |
|
14743 |
1 found |
|
14744 |
@retval |
|
14745 |
0 not found. |
|
14746 |
*/ |
|
14747 |
||
14748 |
static bool |
|
14749 |
find_field_in_item_list (Field *field, void *data) |
|
14750 |
{ |
|
14751 |
List<Item> *fields= (List<Item> *) data; |
|
14752 |
bool part_found= 0; |
|
14753 |
List_iterator<Item> li(*fields); |
|
14754 |
Item *item; |
|
14755 |
||
14756 |
while ((item= li++)) |
|
14757 |
{ |
|
14758 |
if (item->type() == Item::FIELD_ITEM && |
|
14759 |
((Item_field*) item)->field->eq(field)) |
|
14760 |
{ |
|
14761 |
part_found= 1; |
|
14762 |
break; |
|
14763 |
} |
|
14764 |
} |
|
14765 |
return part_found; |
|
14766 |
} |
|
14767 |
||
14768 |
||
14769 |
/** |
|
14770 |
Test if we can skip the ORDER BY by using an index. |
|
14771 |
||
14772 |
SYNOPSIS |
|
14773 |
test_if_skip_sort_order() |
|
14774 |
tab |
|
14775 |
order |
|
14776 |
select_limit |
|
14777 |
no_changes |
|
14778 |
map |
|
14779 |
||
14780 |
If we can use an index, the JOIN_TAB / tab->select struct |
|
14781 |
is changed to use the index. |
|
14782 |
||
14783 |
The index must cover all fields in <order>, or it will not be considered. |
|
14784 |
||
14785 |
@todo |
|
14786 |
- sergeyp: Results of all index merge selects actually are ordered |
|
14787 |
by clustered PK values. |
|
14788 |
||
14789 |
@retval |
|
14790 |
0 We have to use filesort to do the sorting |
|
14791 |
@retval |
|
14792 |
1 We can use an index. |
|
14793 |
*/ |
|
14794 |
||
14795 |
static bool |
|
14796 |
test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, |
|
14797 |
bool no_changes, const key_map *map) |
|
14798 |
{ |
|
14799 |
int ref_key; |
|
14800 |
uint ref_key_parts; |
|
14801 |
int order_direction; |
|
14802 |
uint used_key_parts; |
|
14803 |
TABLE *table=tab->table; |
|
14804 |
SQL_SELECT *select=tab->select; |
|
14805 |
key_map usable_keys; |
|
14806 |
QUICK_SELECT_I *save_quick= 0; |
|
14807 |
||
14808 |
/* |
|
14809 |
Keys disabled by ALTER TABLE ... DISABLE KEYS should have already |
|
14810 |
been taken into account. |
|
14811 |
*/ |
|
14812 |
usable_keys= *map; |
|
14813 |
||
14814 |
for (ORDER *tmp_order=order; tmp_order ; tmp_order=tmp_order->next) |
|
14815 |
{ |
|
14816 |
Item *item= (*tmp_order->item)->real_item(); |
|
14817 |
if (item->type() != Item::FIELD_ITEM) |
|
14818 |
{ |
|
14819 |
usable_keys.clear_all(); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14820 |
return(0); |
1
by brian
clean slate |
14821 |
} |
14822 |
usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey); |
|
14823 |
if (usable_keys.is_clear_all()) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14824 |
return(0); // No usable keys |
1
by brian
clean slate |
14825 |
} |
14826 |
||
14827 |
ref_key= -1; |
|
14828 |
/* Test if constant range in WHERE */ |
|
14829 |
if (tab->ref.key >= 0 && tab->ref.key_parts) |
|
14830 |
{ |
|
14831 |
ref_key= tab->ref.key; |
|
14832 |
ref_key_parts= tab->ref.key_parts; |
|
14833 |
if (tab->type == JT_REF_OR_NULL) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14834 |
return(0); |
1
by brian
clean slate |
14835 |
} |
14836 |
else if (select && select->quick) // Range found by opt_range |
|
14837 |
{ |
|
14838 |
int quick_type= select->quick->get_type(); |
|
14839 |
save_quick= select->quick; |
|
14840 |
/* |
|
14841 |
assume results are not ordered when index merge is used |
|
14842 |
TODO: sergeyp: Results of all index merge selects actually are ordered |
|
14843 |
by clustered PK values. |
|
14844 |
*/ |
|
14845 |
||
14846 |
if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || |
|
14847 |
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || |
|
14848 |
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14849 |
return(0); |
1
by brian
clean slate |
14850 |
ref_key= select->quick->index; |
14851 |
ref_key_parts= select->quick->used_key_parts; |
|
14852 |
} |
|
14853 |
||
14854 |
if (ref_key >= 0) |
|
14855 |
{ |
|
14856 |
/* |
|
14857 |
We come here when there is a REF key. |
|
14858 |
*/ |
|
14859 |
if (!usable_keys.is_set(ref_key)) |
|
14860 |
{ |
|
14861 |
/* |
|
14862 |
We come here when ref_key is not among usable_keys |
|
14863 |
*/ |
|
14864 |
uint new_ref_key; |
|
14865 |
/* |
|
14866 |
If using index only read, only consider other possible index only |
|
14867 |
keys |
|
14868 |
*/ |
|
14869 |
if (table->covering_keys.is_set(ref_key)) |
|
14870 |
usable_keys.intersect(table->covering_keys); |
|
14871 |
if (tab->pre_idx_push_select_cond) |
|
14872 |
tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond; |
|
14873 |
if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts, |
|
14874 |
&usable_keys)) < MAX_KEY) |
|
14875 |
{ |
|
14876 |
/* Found key that can be used to retrieve data in sorted order */ |
|
14877 |
if (tab->ref.key >= 0) |
|
14878 |
{ |
|
14879 |
/* |
|
14880 |
We'll use ref access method on key new_ref_key. In general case |
|
14881 |
the index search tuple for new_ref_key will be different (e.g. |
|
14882 |
when one index is defined as (part1, part2, ...) and another as |
|
14883 |
(part1, part2(N), ...) and the WHERE clause contains |
|
14884 |
"part1 = const1 AND part2=const2". |
|
14885 |
So we build tab->ref from scratch here. |
|
14886 |
*/ |
|
14887 |
KEYUSE *keyuse= tab->keyuse; |
|
14888 |
while (keyuse->key != new_ref_key && keyuse->table == tab->table) |
|
14889 |
keyuse++; |
|
14890 |
||
14891 |
if (create_ref_for_key(tab->join, tab, keyuse, |
|
14892 |
tab->join->const_table_map)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14893 |
return(0); |
1
by brian
clean slate |
14894 |
} |
14895 |
else |
|
14896 |
{ |
|
14897 |
/* |
|
14898 |
The range optimizer constructed QUICK_RANGE for ref_key, and |
|
14899 |
we want to use instead new_ref_key as the index. We can't |
|
14900 |
just change the index of the quick select, because this may |
|
14901 |
result in an incosistent QUICK_SELECT object. Below we |
|
14902 |
create a new QUICK_SELECT from scratch so that all its |
|
14903 |
parameres are set correctly by the range optimizer. |
|
14904 |
*/ |
|
14905 |
key_map new_ref_key_map; |
|
14906 |
new_ref_key_map.clear_all(); // Force the creation of quick select |
|
14907 |
new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key. |
|
14908 |
||
14909 |
if (select->test_quick_select(tab->join->thd, new_ref_key_map, 0, |
|
14910 |
(tab->join->select_options & |
|
14911 |
OPTION_FOUND_ROWS) ? |
|
14912 |
HA_POS_ERROR : |
|
14913 |
tab->join->unit->select_limit_cnt,0, |
|
55
by brian
Update for using real bool types. |
14914 |
true) <= |
1
by brian
clean slate |
14915 |
0) |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14916 |
return(0); |
1
by brian
clean slate |
14917 |
} |
14918 |
ref_key= new_ref_key; |
|
14919 |
} |
|
14920 |
} |
|
14921 |
/* Check if we get the rows in requested sorted order by using the key */ |
|
14922 |
if (usable_keys.is_set(ref_key) && |
|
14923 |
(order_direction= test_if_order_by_key(order,table,ref_key, |
|
14924 |
&used_key_parts))) |
|
14925 |
goto check_reverse_order; |
|
14926 |
} |
|
14927 |
{ |
|
14928 |
/* |
|
14929 |
Check whether there is an index compatible with the given order |
|
14930 |
usage of which is cheaper than usage of the ref_key index (ref_key>=0) |
|
14931 |
or a table scan. |
|
14932 |
It may be the case if ORDER/GROUP BY is used with LIMIT. |
|
14933 |
*/ |
|
14934 |
uint nr; |
|
14935 |
key_map keys; |
|
14936 |
uint best_key_parts= 0; |
|
14937 |
int best_key_direction= 0; |
|
14938 |
ha_rows best_records= 0; |
|
14939 |
double read_time; |
|
14940 |
int best_key= -1; |
|
55
by brian
Update for using real bool types. |
14941 |
bool is_best_covering= false; |
1
by brian
clean slate |
14942 |
double fanout= 1; |
14943 |
JOIN *join= tab->join; |
|
14944 |
uint tablenr= tab - join->join_tab; |
|
14945 |
ha_rows table_records= table->file->stats.records; |
|
14946 |
bool group= join->group && order == join->group_list; |
|
14947 |
||
14948 |
/* |
|
14949 |
If not used with LIMIT, only use keys if the whole query can be |
|
14950 |
resolved with a key; This is because filesort() is usually faster than |
|
14951 |
retrieving all rows through an index. |
|
14952 |
*/ |
|
14953 |
if (select_limit >= table_records) |
|
14954 |
{ |
|
14955 |
/* |
|
14956 |
filesort() and join cache are usually faster than reading in |
|
14957 |
index order and not using join cache |
|
14958 |
*/ |
|
14959 |
if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
14960 |
return(0); |
1
by brian
clean slate |
14961 |
keys= *table->file->keys_to_use_for_scanning(); |
14962 |
keys.merge(table->covering_keys); |
|
14963 |
||
14964 |
/* |
|
14965 |
We are adding here also the index specified in FORCE INDEX clause, |
|
14966 |
if any. |
|
14967 |
This is to allow users to use index in ORDER BY. |
|
14968 |
*/ |
|
14969 |
if (table->force_index) |
|
14970 |
keys.merge(group ? table->keys_in_use_for_group_by : |
|
14971 |
table->keys_in_use_for_order_by); |
|
14972 |
keys.intersect(usable_keys); |
|
14973 |
} |
|
14974 |
else |
|
14975 |
keys= usable_keys; |
|
14976 |
||
14977 |
read_time= join->best_positions[tablenr].read_time; |
|
14978 |
for (uint i= tablenr+1; i < join->tables; i++) |
|
14979 |
fanout*= join->best_positions[i].records_read; // fanout is always >= 1 |
|
14980 |
||
14981 |
for (nr=0; nr < table->s->keys ; nr++) |
|
14982 |
{ |
|
14983 |
int direction; |
|
14984 |
if (keys.is_set(nr) && |
|
14985 |
(direction= test_if_order_by_key(order, table, nr, &used_key_parts))) |
|
14986 |
{ |
|
14987 |
bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered()); |
|
14988 |
||
14989 |
/* |
|
14990 |
Don't use an index scan with ORDER BY without limit. |
|
14991 |
For GROUP BY without limit always use index scan |
|
14992 |
if there is a suitable index. |
|
14993 |
Why we hold to this asymmetry hardly can be explained |
|
14994 |
rationally. It's easy to demonstrate that using |
|
14995 |
temporary table + filesort could be cheaper for grouping |
|
14996 |
queries too. |
|
14997 |
*/ |
|
14998 |
if (is_covering || |
|
14999 |
select_limit != HA_POS_ERROR || |
|
15000 |
(ref_key < 0 && (group || table->force_index))) |
|
15001 |
{ |
|
15002 |
double rec_per_key; |
|
15003 |
double index_scan_time; |
|
15004 |
KEY *keyinfo= tab->table->key_info+nr; |
|
15005 |
if (select_limit == HA_POS_ERROR) |
|
15006 |
select_limit= table_records; |
|
15007 |
if (group) |
|
15008 |
{ |
|
15009 |
rec_per_key= keyinfo->rec_per_key[used_key_parts-1]; |
|
15010 |
set_if_bigger(rec_per_key, 1); |
|
15011 |
/* |
|
15012 |
With a grouping query each group containing on average |
|
15013 |
rec_per_key records produces only one row that will |
|
15014 |
be included into the result set. |
|
15015 |
*/ |
|
15016 |
if (select_limit > table_records/rec_per_key) |
|
15017 |
select_limit= table_records; |
|
15018 |
else |
|
15019 |
select_limit= (ha_rows) (select_limit*rec_per_key); |
|
15020 |
} |
|
15021 |
/* |
|
15022 |
If tab=tk is not the last joined table tn then to get first |
|
15023 |
L records from the result set we can expect to retrieve |
|
15024 |
only L/fanout(tk,tn) where fanout(tk,tn) says how many |
|
15025 |
rows in the record set on average will match each row tk. |
|
15026 |
Usually our estimates for fanouts are too pessimistic. |
|
15027 |
So the estimate for L/fanout(tk,tn) will be too optimistic |
|
15028 |
and as result we'll choose an index scan when using ref/range |
|
15029 |
access + filesort will be cheaper. |
|
15030 |
*/ |
|
15031 |
select_limit= (ha_rows) (select_limit < fanout ? |
|
15032 |
1 : select_limit/fanout); |
|
15033 |
/* |
|
15034 |
We assume that each of the tested indexes is not correlated |
|
15035 |
with ref_key. Thus, to select first N records we have to scan |
|
15036 |
N/selectivity(ref_key) index entries. |
|
15037 |
selectivity(ref_key) = #scanned_records/#table_records = |
|
15038 |
table->quick_condition_rows/table_records. |
|
15039 |
In any case we can't select more than #table_records. |
|
15040 |
N/(table->quick_condition_rows/table_records) > table_records |
|
15041 |
<=> N > table->quick_condition_rows. |
|
15042 |
*/ |
|
15043 |
if (select_limit > table->quick_condition_rows) |
|
15044 |
select_limit= table_records; |
|
15045 |
else |
|
15046 |
select_limit= (ha_rows) (select_limit * |
|
15047 |
(double) table_records / |
|
15048 |
table->quick_condition_rows); |
|
15049 |
rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1]; |
|
15050 |
set_if_bigger(rec_per_key, 1); |
|
15051 |
/* |
|
15052 |
Here we take into account the fact that rows are |
|
15053 |
accessed in sequences rec_per_key records in each. |
|
15054 |
Rows in such a sequence are supposed to be ordered |
|
15055 |
by rowid/primary key. When reading the data |
|
15056 |
in a sequence we'll touch not more pages than the |
|
15057 |
table file contains. |
|
15058 |
TODO. Use the formula for a disk sweep sequential access |
|
15059 |
to calculate the cost of accessing data rows for one |
|
15060 |
index entry. |
|
15061 |
*/ |
|
15062 |
index_scan_time= select_limit/rec_per_key * |
|
15063 |
min(rec_per_key, table->file->scan_time()); |
|
15064 |
if (is_covering || (ref_key < 0 && (group || table->force_index)) || |
|
15065 |
index_scan_time < read_time) |
|
15066 |
{ |
|
15067 |
ha_rows quick_records= table_records; |
|
15068 |
if (is_best_covering && !is_covering) |
|
15069 |
continue; |
|
15070 |
if (table->quick_keys.is_set(nr)) |
|
15071 |
quick_records= table->quick_rows[nr]; |
|
15072 |
if (best_key < 0 || |
|
15073 |
(select_limit <= min(quick_records,best_records) ? |
|
15074 |
keyinfo->key_parts < best_key_parts : |
|
15075 |
quick_records < best_records)) |
|
15076 |
{ |
|
15077 |
best_key= nr; |
|
15078 |
best_key_parts= keyinfo->key_parts; |
|
15079 |
best_records= quick_records; |
|
15080 |
is_best_covering= is_covering; |
|
15081 |
best_key_direction= direction; |
|
15082 |
} |
|
15083 |
} |
|
15084 |
} |
|
15085 |
} |
|
15086 |
} |
|
15087 |
if (best_key >= 0) |
|
15088 |
{ |
|
55
by brian
Update for using real bool types. |
15089 |
bool quick_created= false; |
1
by brian
clean slate |
15090 |
if (table->quick_keys.is_set(best_key) && best_key != ref_key) |
15091 |
{ |
|
15092 |
key_map map; |
|
15093 |
map.clear_all(); // Force the creation of quick select |
|
15094 |
map.set_bit(best_key); // only best_key. |
|
15095 |
quick_created= |
|
15096 |
select->test_quick_select(join->thd, map, 0, |
|
15097 |
join->select_options & OPTION_FOUND_ROWS ? |
|
15098 |
HA_POS_ERROR : |
|
15099 |
join->unit->select_limit_cnt, |
|
55
by brian
Update for using real bool types. |
15100 |
true, false) > 0; |
1
by brian
clean slate |
15101 |
} |
15102 |
if (!no_changes) |
|
15103 |
{ |
|
15104 |
if (!quick_created) |
|
15105 |
{ |
|
15106 |
tab->index= best_key; |
|
15107 |
tab->read_first_record= best_key_direction > 0 ? |
|
15108 |
join_read_first:join_read_last; |
|
15109 |
tab->type=JT_NEXT; // Read with index_first(), index_next() |
|
15110 |
if (select && select->quick) |
|
15111 |
{ |
|
15112 |
delete select->quick; |
|
15113 |
select->quick= 0; |
|
15114 |
} |
|
15115 |
if (table->covering_keys.is_set(best_key)) |
|
15116 |
{ |
|
15117 |
table->key_read=1; |
|
15118 |
table->file->extra(HA_EXTRA_KEYREAD); |
|
15119 |
} |
|
15120 |
table->file->ha_index_or_rnd_end(); |
|
15121 |
if (join->select_options & SELECT_DESCRIBE) |
|
15122 |
{ |
|
15123 |
tab->ref.key= -1; |
|
15124 |
tab->ref.key_parts= 0; |
|
15125 |
if (select_limit < table_records) |
|
15126 |
tab->limit= select_limit; |
|
15127 |
} |
|
15128 |
} |
|
15129 |
else if (tab->type != JT_ALL) |
|
15130 |
{ |
|
15131 |
/* |
|
15132 |
We're about to use a quick access to the table. |
|
15133 |
We need to change the access method so as the quick access |
|
15134 |
method is actually used. |
|
15135 |
*/ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15136 |
assert(tab->select->quick); |
1
by brian
clean slate |
15137 |
tab->type=JT_ALL; |
15138 |
tab->use_quick=1; |
|
15139 |
tab->ref.key= -1; |
|
15140 |
tab->ref.key_parts=0; // Don't use ref key. |
|
15141 |
tab->read_first_record= join_init_read_record; |
|
15142 |
/* |
|
15143 |
TODO: update the number of records in join->best_positions[tablenr] |
|
15144 |
*/ |
|
15145 |
} |
|
15146 |
} |
|
15147 |
used_key_parts= best_key_parts; |
|
15148 |
order_direction= best_key_direction; |
|
15149 |
} |
|
15150 |
else |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15151 |
return(0); |
1
by brian
clean slate |
15152 |
} |
15153 |
||
15154 |
check_reverse_order: |
|
15155 |
if (order_direction == -1) // If ORDER BY ... DESC |
|
15156 |
{ |
|
15157 |
if (select && select->quick) |
|
15158 |
{ |
|
15159 |
/* |
|
15160 |
Don't reverse the sort order, if it's already done. |
|
15161 |
(In some cases test_if_order_by_key() can be called multiple times |
|
15162 |
*/ |
|
15163 |
if (!select->quick->reverse_sorted()) |
|
15164 |
{ |
|
15165 |
QUICK_SELECT_DESC *tmp; |
|
55
by brian
Update for using real bool types. |
15166 |
bool error= false; |
1
by brian
clean slate |
15167 |
int quick_type= select->quick->get_type(); |
15168 |
if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || |
|
15169 |
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT || |
|
15170 |
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || |
|
15171 |
quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX) |
|
15172 |
{ |
|
15173 |
tab->limit= 0; |
|
15174 |
select->quick= save_quick; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15175 |
return(0); // Use filesort |
1
by brian
clean slate |
15176 |
} |
15177 |
||
15178 |
/* ORDER BY range_key DESC */ |
|
15179 |
tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick), |
|
15180 |
used_key_parts, &error); |
|
15181 |
if (!tmp || error) |
|
15182 |
{ |
|
15183 |
delete tmp; |
|
15184 |
select->quick= save_quick; |
|
15185 |
tab->limit= 0; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15186 |
return(0); // Reverse sort not supported |
1
by brian
clean slate |
15187 |
} |
15188 |
select->quick=tmp; |
|
15189 |
} |
|
15190 |
} |
|
15191 |
else if (tab->type != JT_NEXT && |
|
15192 |
tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts) |
|
15193 |
{ |
|
15194 |
/* |
|
15195 |
SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC |
|
15196 |
||
15197 |
Use a traversal function that starts by reading the last row |
|
15198 |
with key part (A) and then traverse the index backwards. |
|
15199 |
*/ |
|
15200 |
tab->read_first_record= join_read_last_key; |
|
15201 |
tab->read_record.read_record= join_read_prev_same; |
|
15202 |
} |
|
15203 |
} |
|
15204 |
else if (select && select->quick) |
|
15205 |
select->quick->sorted= 1; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15206 |
return(1); |
1
by brian
clean slate |
15207 |
} |
15208 |
||
15209 |
||
15210 |
/* |
|
15211 |
If not selecting by given key, create an index how records should be read |
|
15212 |
||
15213 |
SYNOPSIS |
|
15214 |
create_sort_index() |
|
15215 |
thd Thread handler |
|
15216 |
tab Table to sort (in join structure) |
|
15217 |
order How table should be sorted |
|
15218 |
filesort_limit Max number of rows that needs to be sorted |
|
15219 |
select_limit Max number of rows in final output |
|
15220 |
Used to decide if we should use index or not |
|
15221 |
is_order_by true if we are sorting on ORDER BY, false if GROUP BY |
|
15222 |
Used to decide if we should use index or not |
|
15223 |
||
15224 |
||
15225 |
IMPLEMENTATION |
|
15226 |
- If there is an index that can be used, 'tab' is modified to use |
|
15227 |
this index. |
|
15228 |
- If no index, create with filesort() an index file that can be used to |
|
15229 |
retrieve rows in order (should be done with 'read_record'). |
|
15230 |
The sorted data is stored in tab->table and will be freed when calling |
|
15231 |
free_io_cache(tab->table). |
|
15232 |
||
15233 |
RETURN VALUES |
|
15234 |
0 ok |
|
15235 |
-1 Some fatal error |
|
15236 |
1 No records |
|
15237 |
*/ |
|
15238 |
||
15239 |
static int |
|
15240 |
create_sort_index(THD *thd, JOIN *join, ORDER *order, |
|
15241 |
ha_rows filesort_limit, ha_rows select_limit, |
|
15242 |
bool is_order_by) |
|
15243 |
{ |
|
15244 |
uint length= 0; |
|
15245 |
ha_rows examined_rows; |
|
15246 |
TABLE *table; |
|
15247 |
SQL_SELECT *select; |
|
15248 |
JOIN_TAB *tab; |
|
15249 |
||
15250 |
if (join->tables == join->const_tables) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15251 |
return(0); // One row, no need to sort |
1
by brian
clean slate |
15252 |
tab= join->join_tab + join->const_tables; |
15253 |
table= tab->table; |
|
15254 |
select= tab->select; |
|
15255 |
||
15256 |
/* |
|
15257 |
When there is SQL_BIG_RESULT do not sort using index for GROUP BY, |
|
15258 |
and thus force sorting on disk unless a group min-max optimization |
|
15259 |
is going to be used as it is applied now only for one table queries |
|
15260 |
with covering indexes. |
|
15261 |
*/ |
|
15262 |
if ((order != join->group_list || |
|
15263 |
!(join->select_options & SELECT_BIG_RESULT) || |
|
15264 |
(select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) && |
|
15265 |
test_if_skip_sort_order(tab,order,select_limit,0, |
|
15266 |
is_order_by ? &table->keys_in_use_for_order_by : |
|
15267 |
&table->keys_in_use_for_group_by)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15268 |
return(0); |
1
by brian
clean slate |
15269 |
for (ORDER *ord= join->order; ord; ord= ord->next) |
15270 |
length++; |
|
15271 |
if (!(join->sortorder= |
|
15272 |
make_unireg_sortorder(order, &length, join->sortorder))) |
|
15273 |
goto err; /* purecov: inspected */ |
|
15274 |
||
15275 |
table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE), |
|
15276 |
MYF(MY_WME | MY_ZEROFILL)); |
|
15277 |
table->status=0; // May be wrong if quick_select |
|
15278 |
||
15279 |
// If table has a range, move it to select |
|
15280 |
if (select && !select->quick && tab->ref.key >= 0) |
|
15281 |
{ |
|
15282 |
if (tab->quick) |
|
15283 |
{ |
|
15284 |
select->quick=tab->quick; |
|
15285 |
tab->quick=0; |
|
15286 |
/* |
|
15287 |
We can only use 'Only index' if quick key is same as ref_key |
|
15288 |
and in index_merge 'Only index' cannot be used |
|
15289 |
*/ |
|
15290 |
if (table->key_read && ((uint) tab->ref.key != select->quick->index)) |
|
15291 |
{ |
|
15292 |
table->key_read=0; |
|
15293 |
table->file->extra(HA_EXTRA_NO_KEYREAD); |
|
15294 |
} |
|
15295 |
} |
|
15296 |
else |
|
15297 |
{ |
|
15298 |
/* |
|
15299 |
We have a ref on a const; Change this to a range that filesort |
|
15300 |
can use. |
|
15301 |
For impossible ranges (like when doing a lookup on NULL on a NOT NULL |
|
15302 |
field, quick will contain an empty record set. |
|
15303 |
*/ |
|
15304 |
if (!(select->quick= (get_quick_select_for_ref(thd, table, &tab->ref, |
|
15305 |
tab->found_records)))) |
|
15306 |
goto err; |
|
15307 |
} |
|
15308 |
} |
|
15309 |
||
15310 |
/* Fill schema tables with data before filesort if it's necessary */ |
|
15311 |
if ((join->select_lex->options & OPTION_SCHEMA_TABLE) && |
|
15312 |
get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX)) |
|
15313 |
goto err; |
|
15314 |
||
15315 |
if (table->s->tmp_table) |
|
15316 |
table->file->info(HA_STATUS_VARIABLE); // Get record count |
|
15317 |
table->sort.found_records=filesort(thd, table,join->sortorder, length, |
|
15318 |
select, filesort_limit, 0, |
|
15319 |
&examined_rows); |
|
15320 |
tab->records= table->sort.found_records; // For SQL_CALC_ROWS |
|
15321 |
if (select) |
|
15322 |
{ |
|
15323 |
select->cleanup(); // filesort did select |
|
15324 |
tab->select= 0; |
|
15325 |
} |
|
15326 |
tab->select_cond=0; |
|
15327 |
tab->last_inner= 0; |
|
15328 |
tab->first_unmatched= 0; |
|
15329 |
tab->type=JT_ALL; // Read with normal read_record |
|
15330 |
tab->read_first_record= join_init_read_record; |
|
15331 |
tab->join->examined_rows+=examined_rows; |
|
15332 |
if (table->key_read) // Restore if we used indexes |
|
15333 |
{ |
|
15334 |
table->key_read=0; |
|
15335 |
table->file->extra(HA_EXTRA_NO_KEYREAD); |
|
15336 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15337 |
return(table->sort.found_records == HA_POS_ERROR); |
1
by brian
clean slate |
15338 |
err: |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15339 |
return(-1); |
1
by brian
clean slate |
15340 |
} |
15341 |
||
15342 |
/***************************************************************************** |
|
15343 |
Remove duplicates from tmp table |
|
15344 |
This should be recoded to add a unique index to the table and remove |
|
15345 |
duplicates |
|
15346 |
Table is a locked single thread table |
|
15347 |
fields is the number of fields to check (from the end) |
|
15348 |
*****************************************************************************/ |
|
15349 |
||
15350 |
static bool compare_record(TABLE *table, Field **ptr) |
|
15351 |
{ |
|
15352 |
for (; *ptr ; ptr++) |
|
15353 |
{ |
|
15354 |
if ((*ptr)->cmp_offset(table->s->rec_buff_length)) |
|
15355 |
return 1; |
|
15356 |
} |
|
15357 |
return 0; |
|
15358 |
} |
|
15359 |
||
15360 |
static bool copy_blobs(Field **ptr) |
|
15361 |
{ |
|
15362 |
for (; *ptr ; ptr++) |
|
15363 |
{ |
|
15364 |
if ((*ptr)->flags & BLOB_FLAG) |
|
15365 |
if (((Field_blob *) (*ptr))->copy()) |
|
15366 |
return 1; // Error |
|
15367 |
} |
|
15368 |
return 0; |
|
15369 |
} |
|
15370 |
||
15371 |
static void free_blobs(Field **ptr) |
|
15372 |
{ |
|
15373 |
for (; *ptr ; ptr++) |
|
15374 |
{ |
|
15375 |
if ((*ptr)->flags & BLOB_FLAG) |
|
15376 |
((Field_blob *) (*ptr))->free(); |
|
15377 |
} |
|
15378 |
} |
|
15379 |
||
15380 |
||
15381 |
static int |
|
15382 |
remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having) |
|
15383 |
{ |
|
15384 |
int error; |
|
15385 |
ulong reclength,offset; |
|
15386 |
uint field_count; |
|
15387 |
THD *thd= join->thd; |
|
15388 |
||
15389 |
entry->reginfo.lock_type=TL_WRITE; |
|
15390 |
||
15391 |
/* Calculate how many saved fields there is in list */ |
|
15392 |
field_count=0; |
|
15393 |
List_iterator<Item> it(fields); |
|
15394 |
Item *item; |
|
15395 |
while ((item=it++)) |
|
15396 |
{ |
|
15397 |
if (item->get_tmp_table_field() && ! item->const_item()) |
|
15398 |
field_count++; |
|
15399 |
} |
|
15400 |
||
15401 |
if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) |
|
15402 |
{ // only const items with no OPTION_FOUND_ROWS |
|
15403 |
join->unit->select_limit_cnt= 1; // Only send first row |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15404 |
return(0); |
1
by brian
clean slate |
15405 |
} |
15406 |
Field **first_field=entry->field+entry->s->fields - field_count; |
|
15407 |
offset= (field_count ? |
|
15408 |
entry->field[entry->s->fields - field_count]-> |
|
15409 |
offset(entry->record[0]) : 0); |
|
15410 |
reclength=entry->s->reclength-offset; |
|
15411 |
||
15412 |
free_io_cache(entry); // Safety |
|
15413 |
entry->file->info(HA_STATUS_VARIABLE); |
|
15414 |
if (entry->s->db_type() == heap_hton || |
|
15415 |
(!entry->s->blob_fields && |
|
15416 |
((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records < |
|
15417 |
thd->variables.sortbuff_size))) |
|
15418 |
error=remove_dup_with_hash_index(join->thd, entry, |
|
15419 |
field_count, first_field, |
|
15420 |
reclength, having); |
|
15421 |
else |
|
15422 |
error=remove_dup_with_compare(join->thd, entry, first_field, offset, |
|
15423 |
having); |
|
15424 |
||
15425 |
free_blobs(first_field); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15426 |
return(error); |
1
by brian
clean slate |
15427 |
} |
15428 |
||
15429 |
||
15430 |
static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field, |
|
15431 |
ulong offset, Item *having) |
|
15432 |
{ |
|
15433 |
handler *file=table->file; |
|
15434 |
char *org_record,*new_record; |
|
15435 |
uchar *record; |
|
15436 |
int error; |
|
15437 |
ulong reclength= table->s->reclength-offset; |
|
15438 |
||
15439 |
org_record=(char*) (record=table->record[0])+offset; |
|
15440 |
new_record=(char*) table->record[1]+offset; |
|
15441 |
||
15442 |
file->ha_rnd_init(1); |
|
15443 |
error=file->rnd_next(record); |
|
15444 |
for (;;) |
|
15445 |
{ |
|
15446 |
if (thd->killed) |
|
15447 |
{ |
|
15448 |
thd->send_kill_message(); |
|
15449 |
error=0; |
|
15450 |
goto err; |
|
15451 |
} |
|
15452 |
if (error) |
|
15453 |
{ |
|
15454 |
if (error == HA_ERR_RECORD_DELETED) |
|
15455 |
continue; |
|
15456 |
if (error == HA_ERR_END_OF_FILE) |
|
15457 |
break; |
|
15458 |
goto err; |
|
15459 |
} |
|
15460 |
if (having && !having->val_int()) |
|
15461 |
{ |
|
15462 |
if ((error=file->ha_delete_row(record))) |
|
15463 |
goto err; |
|
15464 |
error=file->rnd_next(record); |
|
15465 |
continue; |
|
15466 |
} |
|
15467 |
if (copy_blobs(first_field)) |
|
15468 |
{ |
|
15469 |
my_message(ER_OUTOFMEMORY, ER(ER_OUTOFMEMORY), MYF(0)); |
|
15470 |
error=0; |
|
15471 |
goto err; |
|
15472 |
} |
|
15473 |
memcpy(new_record,org_record,reclength); |
|
15474 |
||
15475 |
/* Read through rest of file and mark duplicated rows deleted */ |
|
15476 |
bool found=0; |
|
15477 |
for (;;) |
|
15478 |
{ |
|
15479 |
if ((error=file->rnd_next(record))) |
|
15480 |
{ |
|
15481 |
if (error == HA_ERR_RECORD_DELETED) |
|
15482 |
continue; |
|
15483 |
if (error == HA_ERR_END_OF_FILE) |
|
15484 |
break; |
|
15485 |
goto err; |
|
15486 |
} |
|
15487 |
if (compare_record(table, first_field) == 0) |
|
15488 |
{ |
|
15489 |
if ((error=file->ha_delete_row(record))) |
|
15490 |
goto err; |
|
15491 |
} |
|
15492 |
else if (!found) |
|
15493 |
{ |
|
15494 |
found=1; |
|
15495 |
file->position(record); // Remember position |
|
15496 |
} |
|
15497 |
} |
|
15498 |
if (!found) |
|
15499 |
break; // End of file |
|
15500 |
/* Restart search on next row */ |
|
15501 |
error=file->restart_rnd_next(record,file->ref); |
|
15502 |
} |
|
15503 |
||
15504 |
file->extra(HA_EXTRA_NO_CACHE); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15505 |
return(0); |
1
by brian
clean slate |
15506 |
err: |
15507 |
file->extra(HA_EXTRA_NO_CACHE); |
|
15508 |
if (error) |
|
15509 |
file->print_error(error,MYF(0)); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15510 |
return(1); |
1
by brian
clean slate |
15511 |
} |
15512 |
||
15513 |
||
15514 |
/** |
|
15515 |
Generate a hash index for each row to quickly find duplicate rows. |
|
15516 |
||
15517 |
@note |
|
15518 |
Note that this will not work on tables with blobs! |
|
15519 |
*/ |
|
15520 |
||
15521 |
static int remove_dup_with_hash_index(THD *thd, TABLE *table, |
|
15522 |
uint field_count, |
|
15523 |
Field **first_field, |
|
15524 |
ulong key_length, |
|
15525 |
Item *having) |
|
15526 |
{ |
|
15527 |
uchar *key_buffer, *key_pos, *record=table->record[0]; |
|
15528 |
int error; |
|
15529 |
handler *file= table->file; |
|
15530 |
ulong extra_length= ALIGN_SIZE(key_length)-key_length; |
|
15531 |
uint *field_lengths,*field_length; |
|
15532 |
HASH hash; |
|
15533 |
||
15534 |
if (!my_multi_malloc(MYF(MY_WME), |
|
15535 |
&key_buffer, |
|
15536 |
(uint) ((key_length + extra_length) * |
|
15537 |
(long) file->stats.records), |
|
15538 |
&field_lengths, |
|
15539 |
(uint) (field_count*sizeof(*field_lengths)), |
|
15540 |
NullS)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15541 |
return(1); |
1
by brian
clean slate |
15542 |
|
15543 |
{ |
|
15544 |
Field **ptr; |
|
15545 |
ulong total_length= 0; |
|
15546 |
for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++) |
|
15547 |
{ |
|
15548 |
uint length= (*ptr)->sort_length(); |
|
15549 |
(*field_length++)= length; |
|
15550 |
total_length+= length; |
|
15551 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15552 |
assert(total_length <= key_length); |
1
by brian
clean slate |
15553 |
key_length= total_length; |
15554 |
extra_length= ALIGN_SIZE(key_length)-key_length; |
|
15555 |
} |
|
15556 |
||
15557 |
if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, |
|
15558 |
key_length, (hash_get_key) 0, 0, 0)) |
|
15559 |
{ |
|
15560 |
my_free((char*) key_buffer,MYF(0)); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15561 |
return(1); |
1
by brian
clean slate |
15562 |
} |
15563 |
||
15564 |
file->ha_rnd_init(1); |
|
15565 |
key_pos=key_buffer; |
|
15566 |
for (;;) |
|
15567 |
{ |
|
15568 |
uchar *org_key_pos; |
|
15569 |
if (thd->killed) |
|
15570 |
{ |
|
15571 |
thd->send_kill_message(); |
|
15572 |
error=0; |
|
15573 |
goto err; |
|
15574 |
} |
|
15575 |
if ((error=file->rnd_next(record))) |
|
15576 |
{ |
|
15577 |
if (error == HA_ERR_RECORD_DELETED) |
|
15578 |
continue; |
|
15579 |
if (error == HA_ERR_END_OF_FILE) |
|
15580 |
break; |
|
15581 |
goto err; |
|
15582 |
} |
|
15583 |
if (having && !having->val_int()) |
|
15584 |
{ |
|
15585 |
if ((error=file->ha_delete_row(record))) |
|
15586 |
goto err; |
|
15587 |
continue; |
|
15588 |
} |
|
15589 |
||
15590 |
/* copy fields to key buffer */ |
|
15591 |
org_key_pos= key_pos; |
|
15592 |
field_length=field_lengths; |
|
15593 |
for (Field **ptr= first_field ; *ptr ; ptr++) |
|
15594 |
{ |
|
15595 |
(*ptr)->sort_string(key_pos,*field_length); |
|
15596 |
key_pos+= *field_length++; |
|
15597 |
} |
|
15598 |
/* Check if it exists before */ |
|
15599 |
if (hash_search(&hash, org_key_pos, key_length)) |
|
15600 |
{ |
|
15601 |
/* Duplicated found ; Remove the row */ |
|
15602 |
if ((error=file->ha_delete_row(record))) |
|
15603 |
goto err; |
|
15604 |
} |
|
15605 |
else |
|
15606 |
(void) my_hash_insert(&hash, org_key_pos); |
|
15607 |
key_pos+=extra_length; |
|
15608 |
} |
|
15609 |
my_free((char*) key_buffer,MYF(0)); |
|
15610 |
hash_free(&hash); |
|
15611 |
file->extra(HA_EXTRA_NO_CACHE); |
|
15612 |
(void) file->ha_rnd_end(); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15613 |
return(0); |
1
by brian
clean slate |
15614 |
|
15615 |
err: |
|
15616 |
my_free((char*) key_buffer,MYF(0)); |
|
15617 |
hash_free(&hash); |
|
15618 |
file->extra(HA_EXTRA_NO_CACHE); |
|
15619 |
(void) file->ha_rnd_end(); |
|
15620 |
if (error) |
|
15621 |
file->print_error(error,MYF(0)); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15622 |
return(1); |
1
by brian
clean slate |
15623 |
} |
15624 |
||
15625 |
||
15626 |
SORT_FIELD *make_unireg_sortorder(ORDER *order, uint *length, |
|
15627 |
SORT_FIELD *sortorder) |
|
15628 |
{ |
|
15629 |
uint count; |
|
15630 |
SORT_FIELD *sort,*pos; |
|
15631 |
||
15632 |
count=0; |
|
15633 |
for (ORDER *tmp = order; tmp; tmp=tmp->next) |
|
15634 |
count++; |
|
15635 |
if (!sortorder) |
|
15636 |
sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) * |
|
15637 |
(max(count, *length) + 1)); |
|
15638 |
pos= sort= sortorder; |
|
15639 |
||
15640 |
if (!pos) |
|
15641 |
return 0; |
|
15642 |
||
15643 |
for (;order;order=order->next,pos++) |
|
15644 |
{ |
|
15645 |
Item *item= order->item[0]->real_item(); |
|
15646 |
pos->field= 0; pos->item= 0; |
|
15647 |
if (item->type() == Item::FIELD_ITEM) |
|
15648 |
pos->field= ((Item_field*) item)->field; |
|
15649 |
else if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item()) |
|
15650 |
pos->field= ((Item_sum*) item)->get_tmp_table_field(); |
|
15651 |
else if (item->type() == Item::COPY_STR_ITEM) |
|
15652 |
{ // Blob patch |
|
15653 |
pos->item= ((Item_copy_string*) item)->item; |
|
15654 |
} |
|
15655 |
else |
|
15656 |
pos->item= *order->item; |
|
15657 |
pos->reverse=! order->asc; |
|
15658 |
} |
|
15659 |
*length=count; |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15660 |
return(sort); |
1
by brian
clean slate |
15661 |
} |
15662 |
||
15663 |
||
15664 |
/***************************************************************************** |
|
15665 |
Fill join cache with packed records |
|
15666 |
Records are stored in tab->cache.buffer and last record in |
|
15667 |
last record is stored with pointers to blobs to support very big |
|
15668 |
records |
|
15669 |
******************************************************************************/ |
|
15670 |
||
15671 |
static int |
|
15672 |
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count) |
|
15673 |
{ |
|
15674 |
register unsigned int i; |
|
15675 |
unsigned int length, blobs; |
|
15676 |
size_t size; |
|
15677 |
CACHE_FIELD *copy,**blob_ptr; |
|
15678 |
JOIN_CACHE *cache; |
|
15679 |
JOIN_TAB *join_tab; |
|
15680 |
||
15681 |
cache= &tables[table_count].cache; |
|
15682 |
cache->fields=blobs=0; |
|
15683 |
||
15684 |
join_tab=tables; |
|
15685 |
for (i=0 ; i < table_count ; i++,join_tab++) |
|
15686 |
{ |
|
15687 |
if (!join_tab->used_fieldlength) /* Not calced yet */ |
|
15688 |
calc_used_field_length(thd, join_tab); |
|
15689 |
cache->fields+=join_tab->used_fields; |
|
15690 |
blobs+=join_tab->used_blobs; |
|
15691 |
||
15692 |
/* SemiJoinDuplicateElimination: reserve space for rowid */ |
|
15693 |
if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID) |
|
15694 |
{ |
|
15695 |
cache->fields++; |
|
15696 |
join_tab->used_fieldlength += join_tab->table->file->ref_length; |
|
15697 |
} |
|
15698 |
} |
|
15699 |
if (!(cache->field=(CACHE_FIELD*) |
|
15700 |
sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)* |
|
15701 |
||
15702 |
sizeof(CACHE_FIELD*)))) |
|
15703 |
{ |
|
15704 |
my_free((uchar*) cache->buff,MYF(0)); /* purecov: inspected */ |
|
15705 |
cache->buff=0; /* purecov: inspected */ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15706 |
return(1); /* purecov: inspected */ |
1
by brian
clean slate |
15707 |
} |
15708 |
copy=cache->field; |
|
15709 |
blob_ptr=cache->blob_ptr=(CACHE_FIELD**) |
|
15710 |
(cache->field+cache->fields+table_count*2); |
|
15711 |
||
15712 |
length=0; |
|
15713 |
for (i=0 ; i < table_count ; i++) |
|
15714 |
{ |
|
15715 |
uint null_fields=0,used_fields; |
|
15716 |
Field **f_ptr,*field; |
|
15717 |
MY_BITMAP *read_set= tables[i].table->read_set; |
|
15718 |
for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ; |
|
15719 |
used_fields ; |
|
15720 |
f_ptr++) |
|
15721 |
{ |
|
15722 |
field= *f_ptr; |
|
15723 |
if (bitmap_is_set(read_set, field->field_index)) |
|
15724 |
{ |
|
15725 |
used_fields--; |
|
15726 |
length+=field->fill_cache_field(copy); |
|
15727 |
if (copy->blob_field) |
|
15728 |
(*blob_ptr++)=copy; |
|
15729 |
if (field->maybe_null()) |
|
15730 |
null_fields++; |
|
15731 |
copy->get_rowid= NULL; |
|
15732 |
copy++; |
|
15733 |
} |
|
15734 |
} |
|
15735 |
/* Copy null bits from table */ |
|
15736 |
if (null_fields && tables[i].table->s->null_fields) |
|
15737 |
{ /* must copy null bits */ |
|
15738 |
copy->str= tables[i].table->null_flags; |
|
15739 |
copy->length= tables[i].table->s->null_bytes; |
|
15740 |
copy->strip=0; |
|
15741 |
copy->blob_field=0; |
|
15742 |
copy->get_rowid= NULL; |
|
15743 |
length+=copy->length; |
|
15744 |
copy++; |
|
15745 |
cache->fields++; |
|
15746 |
} |
|
15747 |
/* If outer join table, copy null_row flag */ |
|
15748 |
if (tables[i].table->maybe_null) |
|
15749 |
{ |
|
15750 |
copy->str= (uchar*) &tables[i].table->null_row; |
|
15751 |
copy->length=sizeof(tables[i].table->null_row); |
|
15752 |
copy->strip=0; |
|
15753 |
copy->blob_field=0; |
|
15754 |
copy->get_rowid= NULL; |
|
15755 |
length+=copy->length; |
|
15756 |
copy++; |
|
15757 |
cache->fields++; |
|
15758 |
} |
|
15759 |
/* SemiJoinDuplicateElimination: Allocate space for rowid if needed */ |
|
15760 |
if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID) |
|
15761 |
{ |
|
15762 |
copy->str= tables[i].table->file->ref; |
|
15763 |
copy->length= tables[i].table->file->ref_length; |
|
15764 |
copy->strip=0; |
|
15765 |
copy->blob_field=0; |
|
15766 |
copy->get_rowid= NULL; |
|
15767 |
if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION) |
|
15768 |
{ |
|
15769 |
/* We will need to call h->position(): */ |
|
15770 |
copy->get_rowid= tables[i].table; |
|
15771 |
/* And those after us won't have to: */ |
|
15772 |
tables[i].rowid_keep_flags &= ~((int)JOIN_TAB::CALL_POSITION); |
|
15773 |
} |
|
15774 |
copy++; |
|
15775 |
} |
|
15776 |
} |
|
15777 |
||
15778 |
cache->length=length+blobs*sizeof(char*); |
|
15779 |
cache->blobs=blobs; |
|
15780 |
*blob_ptr=0; /* End sequentel */ |
|
15781 |
size=max(thd->variables.join_buff_size, cache->length); |
|
15782 |
if (!(cache->buff=(uchar*) my_malloc(size,MYF(0)))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15783 |
return(1); /* Don't use cache */ /* purecov: inspected */ |
1
by brian
clean slate |
15784 |
cache->end=cache->buff+size; |
15785 |
reset_cache_write(cache); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
15786 |
return(0); |
1
by brian
clean slate |
15787 |
} |
15788 |
||
15789 |
||
15790 |
static ulong |
|
15791 |
used_blob_length(CACHE_FIELD **ptr) |
|
15792 |
{ |
|
15793 |
uint length,blob_length; |
|
15794 |
for (length=0 ; *ptr ; ptr++) |
|
15795 |
{ |
|
15796 |
(*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length(); |
|
15797 |
length+=blob_length; |
|
15798 |
(*ptr)->blob_field->get_ptr(&(*ptr)->str); |
|
15799 |
} |
|
15800 |
return length; |
|
15801 |
} |
|
15802 |
||
15803 |
||
15804 |
static bool |
|
15805 |
store_record_in_cache(JOIN_CACHE *cache) |
|
15806 |
{ |
|
15807 |
uint length; |
|
15808 |
uchar *pos; |
|
15809 |
CACHE_FIELD *copy,*end_field; |
|
15810 |
bool last_record; |
|
15811 |
||
15812 |
pos=cache->pos; |
|
15813 |
end_field=cache->field+cache->fields; |
|
15814 |
||
15815 |
length=cache->length; |
|
15816 |
if (cache->blobs) |
|
15817 |
length+=used_blob_length(cache->blob_ptr); |
|
15818 |
if ((last_record= (length + cache->length > (size_t) (cache->end - pos)))) |
|
15819 |
cache->ptr_record=cache->records; |
|
15820 |
/* |
|
15821 |
There is room in cache. Put record there |
|
15822 |
*/ |
|
15823 |
cache->records++; |
|
15824 |
for (copy=cache->field ; copy < end_field; copy++) |
|
15825 |
{ |
|
15826 |
if (copy->blob_field) |
|
15827 |
{ |
|
15828 |
if (last_record) |
|
15829 |
{ |
|
15830 |
copy->blob_field->get_image(pos, copy->length+sizeof(char*), |
|
15831 |
copy->blob_field->charset()); |
|
15832 |
pos+=copy->length+sizeof(char*); |
|
15833 |
} |
|
15834 |
else |
|
15835 |
{ |
|
15836 |
copy->blob_field->get_image(pos, copy->length, // blob length |
|
15837 |
copy->blob_field->charset()); |
|
15838 |
memcpy(pos+copy->length,copy->str,copy->blob_length); // Blob data |
|
15839 |
pos+=copy->length+copy->blob_length; |
|
15840 |
} |
|
15841 |
} |
|
15842 |
else |
|
15843 |
{ |
|
15844 |
// SemiJoinDuplicateElimination: Get the rowid into table->ref: |
|
15845 |
if (copy->get_rowid) |
|
15846 |
copy->get_rowid->file->position(copy->get_rowid->record[0]); |
|
15847 |
||
15848 |
if (copy->strip) |
|
15849 |
{ |
|
15850 |
uchar *str,*end; |
|
15851 |
for (str=copy->str,end= str+copy->length; |
|
15852 |
end > str && end[-1] == ' ' ; |
|
15853 |
end--) ; |
|
15854 |
length=(uint) (end-str); |
|
15855 |
memcpy(pos+2, str, length); |
|
15856 |
int2store(pos, length); |
|
15857 |
pos+= length+2; |
|
15858 |
} |
|
15859 |
else |
|
15860 |
{ |
|
15861 |
memcpy(pos,copy->str,copy->length); |
|
15862 |
pos+=copy->length; |
|
15863 |
} |
|
15864 |
} |
|
15865 |
} |
|
15866 |
cache->pos=pos; |
|
15867 |
return last_record || (size_t) (cache->end - pos) < cache->length; |
|
15868 |
} |
|
15869 |
||
15870 |
||
15871 |
static void |
|
15872 |
reset_cache_read(JOIN_CACHE *cache) |
|
15873 |
{ |
|
15874 |
cache->record_nr=0; |
|
15875 |
cache->pos=cache->buff; |
|
15876 |
} |
|
15877 |
||
15878 |
||
15879 |
static void reset_cache_write(JOIN_CACHE *cache) |
|
15880 |
{ |
|
15881 |
reset_cache_read(cache); |
|
15882 |
cache->records= 0; |
|
15883 |
cache->ptr_record= (uint) ~0; |
|
15884 |
} |
|
15885 |
||
15886 |
||
15887 |
static void |
|
15888 |
read_cached_record(JOIN_TAB *tab) |
|
15889 |
{ |
|
15890 |
uchar *pos; |
|
15891 |
uint length; |
|
15892 |
bool last_record; |
|
15893 |
CACHE_FIELD *copy,*end_field; |
|
15894 |
||
15895 |
last_record=tab->cache.record_nr++ == tab->cache.ptr_record; |
|
15896 |
pos=tab->cache.pos; |
|
15897 |
for (copy=tab->cache.field,end_field=copy+tab->cache.fields ; |
|
15898 |
copy < end_field; |
|
15899 |
copy++) |
|
15900 |
{ |
|
15901 |
if (copy->blob_field) |
|
15902 |
{ |
|
15903 |
if (last_record) |
|
15904 |
{ |
|
15905 |
copy->blob_field->set_image(pos, copy->length+sizeof(char*), |
|
15906 |
copy->blob_field->charset()); |
|
15907 |
pos+=copy->length+sizeof(char*); |
|
15908 |
} |
|
15909 |
else |
|
15910 |
{ |
|
15911 |
copy->blob_field->set_ptr(pos, pos+copy->length); |
|
15912 |
pos+=copy->length+copy->blob_field->get_length(); |
|
15913 |
} |
|
15914 |
} |
|
15915 |
else |
|
15916 |
{ |
|
15917 |
if (copy->strip) |
|
15918 |
{ |
|
15919 |
length= uint2korr(pos); |
|
15920 |
memcpy(copy->str, pos+2, length); |
|
15921 |
memset(copy->str+length, ' ', copy->length-length); |
|
15922 |
pos+= 2 + length; |
|
15923 |
} |
|
15924 |
else |
|
15925 |
{ |
|
15926 |
memcpy(copy->str,pos,copy->length); |
|
15927 |
pos+=copy->length; |
|
15928 |
} |
|
15929 |
} |
|
15930 |
} |
|
15931 |
tab->cache.pos=pos; |
|
15932 |
return; |
|
15933 |
} |
|
15934 |
||
15935 |
||
15936 |
/* |
|
15937 |
eq_ref: Create the lookup key and check if it is the same as saved key |
|
15938 |
||
15939 |
SYNOPSIS |
|
15940 |
cmp_buffer_with_ref() |
|
15941 |
tab Join tab of the accessed table |
|
15942 |
||
15943 |
DESCRIPTION |
|
15944 |
Used by eq_ref access method: create the index lookup key and check if |
|
15945 |
we've used this key at previous lookup (If yes, we don't need to repeat |
|
15946 |
the lookup - the record has been already fetched) |
|
15947 |
||
15948 |
RETURN |
|
55
by brian
Update for using real bool types. |
15949 |
true No cached record for the key, or failed to create the key (due to |
1
by brian
clean slate |
15950 |
out-of-domain error) |
55
by brian
Update for using real bool types. |
15951 |
false The created key is the same as the previous one (and the record |
1
by brian
clean slate |
15952 |
is already in table->record) |
15953 |
*/ |
|
15954 |
||
15955 |
static bool |
|
15956 |
cmp_buffer_with_ref(JOIN_TAB *tab) |
|
15957 |
{ |
|
15958 |
bool no_prev_key; |
|
15959 |
if (!tab->ref.disable_cache) |
|
15960 |
{ |
|
15961 |
if (!(no_prev_key= tab->ref.key_err)) |
|
15962 |
{ |
|
15963 |
/* Previous access found a row. Copy its key */ |
|
15964 |
memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length); |
|
15965 |
} |
|
15966 |
} |
|
15967 |
else |
|
55
by brian
Update for using real bool types. |
15968 |
no_prev_key= true; |
1
by brian
clean slate |
15969 |
if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, tab->table, |
15970 |
&tab->ref)) || |
|
15971 |
no_prev_key) |
|
15972 |
return 1; |
|
15973 |
return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length) |
|
15974 |
!= 0; |
|
15975 |
} |
|
15976 |
||
15977 |
||
15978 |
bool |
|
15979 |
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref) |
|
15980 |
{ |
|
15981 |
enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields; |
|
15982 |
thd->count_cuted_fields= CHECK_FIELD_IGNORE; |
|
15983 |
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set); |
|
15984 |
bool result= 0; |
|
15985 |
||
15986 |
for (store_key **copy=ref->key_copy ; *copy ; copy++) |
|
15987 |
{ |
|
15988 |
if ((*copy)->copy() & 1) |
|
15989 |
{ |
|
15990 |
result= 1; |
|
15991 |
break; |
|
15992 |
} |
|
15993 |
} |
|
15994 |
thd->count_cuted_fields= save_count_cuted_fields; |
|
15995 |
dbug_tmp_restore_column_map(table->write_set, old_map); |
|
15996 |
return result; |
|
15997 |
} |
|
15998 |
||
15999 |
||
16000 |
/***************************************************************************** |
|
16001 |
Group and order functions |
|
16002 |
*****************************************************************************/ |
|
16003 |
||
16004 |
/** |
|
16005 |
Resolve an ORDER BY or GROUP BY column reference. |
|
16006 |
||
16007 |
Given a column reference (represented by 'order') from a GROUP BY or ORDER |
|
16008 |
BY clause, find the actual column it represents. If the column being |
|
16009 |
resolved is from the GROUP BY clause, the procedure searches the SELECT |
|
16010 |
list 'fields' and the columns in the FROM list 'tables'. If 'order' is from |
|
16011 |
the ORDER BY clause, only the SELECT list is being searched. |
|
16012 |
||
16013 |
If 'order' is resolved to an Item, then order->item is set to the found |
|
16014 |
Item. If there is no item for the found column (that is, it was resolved |
|
16015 |
into a table field), order->item is 'fixed' and is added to all_fields and |
|
16016 |
ref_pointer_array. |
|
16017 |
||
16018 |
ref_pointer_array and all_fields are updated. |
|
16019 |
||
16020 |
@param[in] thd Pointer to current thread structure |
|
16021 |
@param[in,out] ref_pointer_array All select, group and order by fields |
|
16022 |
@param[in] tables List of tables to search in (usually |
|
16023 |
FROM clause) |
|
16024 |
@param[in] order Column reference to be resolved |
|
16025 |
@param[in] fields List of fields to search in (usually |
|
16026 |
SELECT list) |
|
16027 |
@param[in,out] all_fields All select, group and order by fields |
|
16028 |
@param[in] is_group_field True if order is a GROUP field, false if |
|
16029 |
ORDER by field |
|
16030 |
||
16031 |
@retval |
|
55
by brian
Update for using real bool types. |
16032 |
false if OK |
1
by brian
clean slate |
16033 |
@retval |
55
by brian
Update for using real bool types. |
16034 |
true if error occurred |
1
by brian
clean slate |
16035 |
*/ |
16036 |
||
16037 |
static bool |
|
16038 |
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables, |
|
16039 |
ORDER *order, List<Item> &fields, List<Item> &all_fields, |
|
16040 |
bool is_group_field) |
|
16041 |
{ |
|
16042 |
Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */ |
|
16043 |
Item::Type order_item_type; |
|
16044 |
Item **select_item; /* The corresponding item from the SELECT clause. */ |
|
16045 |
Field *from_field; /* The corresponding field from the FROM clause. */ |
|
16046 |
uint counter; |
|
16047 |
enum_resolution_type resolution; |
|
16048 |
||
16049 |
/* |
|
16050 |
Local SP variables may be int but are expressions, not positions. |
|
16051 |
(And they can't be used before fix_fields is called for them). |
|
16052 |
*/ |
|
16053 |
if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item()) |
|
16054 |
{ /* Order by position */ |
|
16055 |
uint count= (uint) order_item->val_int(); |
|
16056 |
if (!count || count > fields.elements) |
|
16057 |
{ |
|
16058 |
my_error(ER_BAD_FIELD_ERROR, MYF(0), |
|
16059 |
order_item->full_name(), thd->where); |
|
55
by brian
Update for using real bool types. |
16060 |
return true; |
1
by brian
clean slate |
16061 |
} |
16062 |
order->item= ref_pointer_array + count - 1; |
|
16063 |
order->in_field_list= 1; |
|
16064 |
order->counter= count; |
|
16065 |
order->counter_used= 1; |
|
55
by brian
Update for using real bool types. |
16066 |
return false; |
1
by brian
clean slate |
16067 |
} |
16068 |
/* Lookup the current GROUP/ORDER field in the SELECT clause. */ |
|
16069 |
select_item= find_item_in_list(order_item, fields, &counter, |
|
16070 |
REPORT_EXCEPT_NOT_FOUND, &resolution); |
|
16071 |
if (!select_item) |
|
55
by brian
Update for using real bool types. |
16072 |
return true; /* The item is not unique, or some other error occured. */ |
1
by brian
clean slate |
16073 |
|
16074 |
||
16075 |
/* Check whether the resolved field is not ambiguos. */ |
|
16076 |
if (select_item != not_found_item) |
|
16077 |
{ |
|
16078 |
Item *view_ref= NULL; |
|
16079 |
/* |
|
16080 |
If we have found field not by its alias in select list but by its |
|
16081 |
original field name, we should additionaly check if we have conflict |
|
16082 |
for this name (in case if we would perform lookup in all tables). |
|
16083 |
*/ |
|
16084 |
if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed && |
|
16085 |
order_item->fix_fields(thd, order->item)) |
|
55
by brian
Update for using real bool types. |
16086 |
return true; |
1
by brian
clean slate |
16087 |
|
16088 |
/* Lookup the current GROUP field in the FROM clause. */ |
|
16089 |
order_item_type= order_item->type(); |
|
16090 |
from_field= (Field*) not_found_field; |
|
16091 |
if ((is_group_field && order_item_type == Item::FIELD_ITEM) || |
|
16092 |
order_item_type == Item::REF_ITEM) |
|
16093 |
{ |
|
16094 |
from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables, |
|
55
by brian
Update for using real bool types. |
16095 |
NULL, &view_ref, IGNORE_ERRORS, true, |
16096 |
false); |
|
1
by brian
clean slate |
16097 |
if (!from_field) |
16098 |
from_field= (Field*) not_found_field; |
|
16099 |
} |
|
16100 |
||
16101 |
if (from_field == not_found_field || |
|
16102 |
(from_field != view_ref_found ? |
|
16103 |
/* it is field of base table => check that fields are same */ |
|
16104 |
((*select_item)->type() == Item::FIELD_ITEM && |
|
16105 |
((Item_field*) (*select_item))->field->eq(from_field)) : |
|
16106 |
/* |
|
16107 |
in is field of view table => check that references on translation |
|
16108 |
table are same |
|
16109 |
*/ |
|
16110 |
((*select_item)->type() == Item::REF_ITEM && |
|
16111 |
view_ref->type() == Item::REF_ITEM && |
|
16112 |
((Item_ref *) (*select_item))->ref == |
|
16113 |
((Item_ref *) view_ref)->ref))) |
|
16114 |
{ |
|
16115 |
/* |
|
16116 |
If there is no such field in the FROM clause, or it is the same field |
|
16117 |
as the one found in the SELECT clause, then use the Item created for |
|
16118 |
the SELECT field. As a result if there was a derived field that |
|
16119 |
'shadowed' a table field with the same name, the table field will be |
|
16120 |
chosen over the derived field. |
|
16121 |
*/ |
|
16122 |
order->item= ref_pointer_array + counter; |
|
16123 |
order->in_field_list=1; |
|
55
by brian
Update for using real bool types. |
16124 |
return false; |
1
by brian
clean slate |
16125 |
} |
16126 |
else |
|
16127 |
{ |
|
16128 |
/* |
|
16129 |
There is a field with the same name in the FROM clause. This |
|
16130 |
is the field that will be chosen. In this case we issue a |
|
16131 |
warning so the user knows that the field from the FROM clause |
|
16132 |
overshadows the column reference from the SELECT list. |
|
16133 |
*/ |
|
16134 |
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR, |
|
16135 |
ER(ER_NON_UNIQ_ERROR), |
|
16136 |
((Item_ident*) order_item)->field_name, |
|
16137 |
current_thd->where); |
|
16138 |
} |
|
16139 |
} |
|
16140 |
||
16141 |
order->in_field_list=0; |
|
16142 |
/* |
|
16143 |
The call to order_item->fix_fields() means that here we resolve |
|
16144 |
'order_item' to a column from a table in the list 'tables', or to |
|
16145 |
a column in some outer query. Exactly because of the second case |
|
16146 |
we come to this point even if (select_item == not_found_item), |
|
16147 |
inspite of that fix_fields() calls find_item_in_list() one more |
|
16148 |
time. |
|
16149 |
||
16150 |
We check order_item->fixed because Item_func_group_concat can put |
|
16151 |
arguments for which fix_fields already was called. |
|
16152 |
*/ |
|
16153 |
if (!order_item->fixed && |
|
16154 |
(order_item->fix_fields(thd, order->item) || |
|
16155 |
(order_item= *order->item)->check_cols(1) || |
|
16156 |
thd->is_fatal_error)) |
|
55
by brian
Update for using real bool types. |
16157 |
return true; /* Wrong field. */ |
1
by brian
clean slate |
16158 |
|
16159 |
uint el= all_fields.elements; |
|
16160 |
all_fields.push_front(order_item); /* Add new field to field list. */ |
|
16161 |
ref_pointer_array[el]= order_item; |
|
16162 |
order->item= ref_pointer_array + el; |
|
55
by brian
Update for using real bool types. |
16163 |
return false; |
1
by brian
clean slate |
16164 |
} |
16165 |
||
16166 |
||
16167 |
/** |
|
16168 |
Change order to point at item in select list. |
|
16169 |
||
16170 |
If item isn't a number and doesn't exits in the select list, add it the |
|
16171 |
the field list. |
|
16172 |
*/ |
|
16173 |
||
16174 |
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables, |
|
16175 |
List<Item> &fields, List<Item> &all_fields, ORDER *order) |
|
16176 |
{ |
|
16177 |
thd->where="order clause"; |
|
16178 |
for (; order; order=order->next) |
|
16179 |
{ |
|
16180 |
if (find_order_in_list(thd, ref_pointer_array, tables, order, fields, |
|
55
by brian
Update for using real bool types. |
16181 |
all_fields, false)) |
1
by brian
clean slate |
16182 |
return 1; |
16183 |
} |
|
16184 |
return 0; |
|
16185 |
} |
|
16186 |
||
16187 |
||
16188 |
/** |
|
16189 |
Intitialize the GROUP BY list. |
|
16190 |
||
16191 |
@param thd Thread handler |
|
16192 |
@param ref_pointer_array We store references to all fields that was |
|
16193 |
not in 'fields' here. |
|
16194 |
@param fields All fields in the select part. Any item in |
|
16195 |
'order' that is part of these list is replaced |
|
16196 |
by a pointer to this fields. |
|
16197 |
@param all_fields Total list of all unique fields used by the |
|
16198 |
select. All items in 'order' that was not part |
|
16199 |
of fields will be added first to this list. |
|
16200 |
@param order The fields we should do GROUP BY on. |
|
16201 |
@param hidden_group_fields Pointer to flag that is set to 1 if we added |
|
16202 |
any fields to all_fields. |
|
16203 |
||
16204 |
@todo |
|
16205 |
change ER_WRONG_FIELD_WITH_GROUP to more detailed |
|
16206 |
ER_NON_GROUPING_FIELD_USED |
|
16207 |
||
16208 |
@retval |
|
16209 |
0 ok |
|
16210 |
@retval |
|
16211 |
1 error (probably out of memory) |
|
16212 |
*/ |
|
16213 |
||
16214 |
int |
|
16215 |
setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables, |
|
16216 |
List<Item> &fields, List<Item> &all_fields, ORDER *order, |
|
16217 |
bool *hidden_group_fields) |
|
16218 |
{ |
|
16219 |
*hidden_group_fields=0; |
|
16220 |
ORDER *ord; |
|
16221 |
||
16222 |
if (!order) |
|
16223 |
return 0; /* Everything is ok */ |
|
16224 |
||
16225 |
uint org_fields=all_fields.elements; |
|
16226 |
||
16227 |
thd->where="group statement"; |
|
16228 |
for (ord= order; ord; ord= ord->next) |
|
16229 |
{ |
|
16230 |
if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields, |
|
55
by brian
Update for using real bool types. |
16231 |
all_fields, true)) |
1
by brian
clean slate |
16232 |
return 1; |
16233 |
(*ord->item)->marker= UNDEF_POS; /* Mark found */ |
|
16234 |
if ((*ord->item)->with_sum_func) |
|
16235 |
{ |
|
16236 |
my_error(ER_WRONG_GROUP_FIELD, MYF(0), (*ord->item)->full_name()); |
|
16237 |
return 1; |
|
16238 |
} |
|
16239 |
} |
|
16240 |
/* MODE_ONLY_FULL_GROUP_BY */ |
|
16241 |
{ |
|
16242 |
/* |
|
16243 |
Don't allow one to use fields that is not used in GROUP BY |
|
16244 |
For each select a list of field references that aren't under an |
|
16245 |
aggregate function is created. Each field in this list keeps the |
|
16246 |
position of the select list expression which it belongs to. |
|
16247 |
||
16248 |
First we check an expression from the select list against the GROUP BY |
|
16249 |
list. If it's found there then it's ok. It's also ok if this expression |
|
16250 |
is a constant or an aggregate function. Otherwise we scan the list |
|
16251 |
of non-aggregated fields and if we'll find at least one field reference |
|
16252 |
that belongs to this expression and doesn't occur in the GROUP BY list |
|
16253 |
we throw an error. If there are no fields in the created list for a |
|
16254 |
select list expression this means that all fields in it are used under |
|
16255 |
aggregate functions. |
|
16256 |
*/ |
|
16257 |
Item *item; |
|
16258 |
Item_field *field; |
|
16259 |
int cur_pos_in_select_list= 0; |
|
16260 |
List_iterator<Item> li(fields); |
|
16261 |
List_iterator<Item_field> naf_it(thd->lex->current_select->non_agg_fields); |
|
16262 |
||
16263 |
field= naf_it++; |
|
16264 |
while (field && (item=li++)) |
|
16265 |
{ |
|
16266 |
if (item->type() != Item::SUM_FUNC_ITEM && item->marker >= 0 && |
|
16267 |
!item->const_item() && |
|
16268 |
!(item->real_item()->type() == Item::FIELD_ITEM && |
|
16269 |
item->used_tables() & OUTER_REF_TABLE_BIT)) |
|
16270 |
{ |
|
16271 |
while (field) |
|
16272 |
{ |
|
16273 |
/* Skip fields from previous expressions. */ |
|
16274 |
if (field->marker < cur_pos_in_select_list) |
|
16275 |
goto next_field; |
|
16276 |
/* Found a field from the next expression. */ |
|
16277 |
if (field->marker > cur_pos_in_select_list) |
|
16278 |
break; |
|
16279 |
/* |
|
16280 |
Check whether the field occur in the GROUP BY list. |
|
16281 |
Throw the error later if the field isn't found. |
|
16282 |
*/ |
|
16283 |
for (ord= order; ord; ord= ord->next) |
|
16284 |
if ((*ord->item)->eq((Item*)field, 0)) |
|
16285 |
goto next_field; |
|
16286 |
/* |
|
16287 |
TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed |
|
16288 |
ER_NON_GROUPING_FIELD_USED |
|
16289 |
*/ |
|
16290 |
my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name()); |
|
16291 |
return 1; |
|
16292 |
next_field: |
|
16293 |
field= naf_it++; |
|
16294 |
} |
|
16295 |
} |
|
16296 |
cur_pos_in_select_list++; |
|
16297 |
} |
|
16298 |
} |
|
16299 |
if (org_fields != all_fields.elements) |
|
16300 |
*hidden_group_fields=1; // group fields is not used |
|
16301 |
return 0; |
|
16302 |
} |
|
16303 |
||
16304 |
/** |
|
16305 |
Create a group by that consist of all non const fields. |
|
16306 |
||
16307 |
Try to use the fields in the order given by 'order' to allow one to |
|
16308 |
optimize away 'order by'. |
|
16309 |
*/ |
|
16310 |
||
16311 |
static ORDER * |
|
16312 |
create_distinct_group(THD *thd, Item **ref_pointer_array, |
|
16313 |
ORDER *order_list, List<Item> &fields, |
|
77.1.45
by Monty Taylor
Warning fixes. |
16314 |
List<Item> &all_fields __attribute__((__unused__)), |
16315 |
bool *all_order_by_fields_used) |
|
1
by brian
clean slate |
16316 |
{ |
16317 |
List_iterator<Item> li(fields); |
|
77.1.45
by Monty Taylor
Warning fixes. |
16318 |
Item *item; |
1
by brian
clean slate |
16319 |
ORDER *order,*group,**prev; |
16320 |
||
16321 |
*all_order_by_fields_used= 1; |
|
16322 |
while ((item=li++)) |
|
16323 |
item->marker=0; /* Marker that field is not used */ |
|
16324 |
||
16325 |
prev= &group; group=0; |
|
16326 |
for (order=order_list ; order; order=order->next) |
|
16327 |
{ |
|
16328 |
if (order->in_field_list) |
|
16329 |
{ |
|
16330 |
ORDER *ord=(ORDER*) thd->memdup((char*) order,sizeof(ORDER)); |
|
16331 |
if (!ord) |
|
16332 |
return 0; |
|
16333 |
*prev=ord; |
|
16334 |
prev= &ord->next; |
|
16335 |
(*ord->item)->marker=1; |
|
16336 |
} |
|
16337 |
else |
|
16338 |
*all_order_by_fields_used= 0; |
|
16339 |
} |
|
16340 |
||
16341 |
li.rewind(); |
|
16342 |
while ((item=li++)) |
|
16343 |
{ |
|
16344 |
if (!item->const_item() && !item->with_sum_func && !item->marker) |
|
16345 |
{ |
|
16346 |
/* |
|
16347 |
Don't put duplicate columns from the SELECT list into the |
|
16348 |
GROUP BY list. |
|
16349 |
*/ |
|
16350 |
ORDER *ord_iter; |
|
16351 |
for (ord_iter= group; ord_iter; ord_iter= ord_iter->next) |
|
16352 |
if ((*ord_iter->item)->eq(item, 1)) |
|
16353 |
goto next_item; |
|
16354 |
||
16355 |
ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER)); |
|
16356 |
if (!ord) |
|
16357 |
return 0; |
|
16358 |
||
62
by Brian Aker
First pass of removing BIT_TYPE |
16359 |
/* |
16360 |
We have here only field_list (not all_field_list), so we can use |
|
16361 |
simple indexing of ref_pointer_array (order in the array and in the |
|
16362 |
list are same) |
|
16363 |
*/ |
|
16364 |
ord->item= ref_pointer_array; |
|
1
by brian
clean slate |
16365 |
ord->asc=1; |
16366 |
*prev=ord; |
|
16367 |
prev= &ord->next; |
|
16368 |
} |
|
16369 |
next_item: |
|
16370 |
ref_pointer_array++; |
|
16371 |
} |
|
16372 |
*prev=0; |
|
16373 |
return group; |
|
16374 |
} |
|
16375 |
||
16376 |
||
16377 |
/** |
|
16378 |
Update join with count of the different type of fields. |
|
16379 |
*/ |
|
16380 |
||
16381 |
void |
|
16382 |
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, |
|
16383 |
List<Item> &fields, bool reset_with_sum_func) |
|
16384 |
{ |
|
16385 |
List_iterator<Item> li(fields); |
|
16386 |
Item *field; |
|
16387 |
||
16388 |
param->field_count=param->sum_func_count=param->func_count= |
|
16389 |
param->hidden_field_count=0; |
|
16390 |
param->quick_group=1; |
|
16391 |
while ((field=li++)) |
|
16392 |
{ |
|
16393 |
Item::Type real_type= field->real_item()->type(); |
|
16394 |
if (real_type == Item::FIELD_ITEM) |
|
16395 |
param->field_count++; |
|
16396 |
else if (real_type == Item::SUM_FUNC_ITEM) |
|
16397 |
{ |
|
16398 |
if (! field->const_item()) |
|
16399 |
{ |
|
16400 |
Item_sum *sum_item=(Item_sum*) field->real_item(); |
|
16401 |
if (!sum_item->depended_from() || |
|
16402 |
sum_item->depended_from() == select_lex) |
|
16403 |
{ |
|
16404 |
if (!sum_item->quick_group) |
|
16405 |
param->quick_group=0; // UDF SUM function |
|
16406 |
param->sum_func_count++; |
|
16407 |
||
16408 |
for (uint i=0 ; i < sum_item->arg_count ; i++) |
|
16409 |
{ |
|
16410 |
if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM) |
|
16411 |
param->field_count++; |
|
16412 |
else |
|
16413 |
param->func_count++; |
|
16414 |
} |
|
16415 |
} |
|
16416 |
param->func_count++; |
|
16417 |
} |
|
16418 |
} |
|
16419 |
else |
|
16420 |
{ |
|
16421 |
param->func_count++; |
|
16422 |
if (reset_with_sum_func) |
|
16423 |
field->with_sum_func=0; |
|
16424 |
} |
|
16425 |
} |
|
16426 |
} |
|
16427 |
||
16428 |
||
16429 |
/** |
|
16430 |
Return 1 if second is a subpart of first argument. |
|
16431 |
||
16432 |
If first parts has different direction, change it to second part |
|
16433 |
(group is sorted like order) |
|
16434 |
*/ |
|
16435 |
||
16436 |
static bool |
|
16437 |
test_if_subpart(ORDER *a,ORDER *b) |
|
16438 |
{ |
|
16439 |
for (; a && b; a=a->next,b=b->next) |
|
16440 |
{ |
|
16441 |
if ((*a->item)->eq(*b->item,1)) |
|
16442 |
a->asc=b->asc; |
|
16443 |
else |
|
16444 |
return 0; |
|
16445 |
} |
|
16446 |
return test(!b); |
|
16447 |
} |
|
16448 |
||
16449 |
/** |
|
16450 |
Return table number if there is only one table in sort order |
|
16451 |
and group and order is compatible, else return 0. |
|
16452 |
*/ |
|
16453 |
||
16454 |
static TABLE * |
|
16455 |
get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables) |
|
16456 |
{ |
|
16457 |
table_map map= (table_map) 0; |
|
16458 |
||
16459 |
if (!a) |
|
16460 |
a=b; // Only one need to be given |
|
16461 |
else if (!b) |
|
16462 |
b=a; |
|
16463 |
||
16464 |
for (; a && b; a=a->next,b=b->next) |
|
16465 |
{ |
|
16466 |
if (!(*a->item)->eq(*b->item,1)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16467 |
return(0); |
1
by brian
clean slate |
16468 |
map|=a->item[0]->used_tables(); |
16469 |
} |
|
16470 |
if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16471 |
return(0); |
1
by brian
clean slate |
16472 |
|
16473 |
for (; !(map & tables->table->map); tables= tables->next_leaf) {}; |
|
16474 |
if (map != tables->table->map) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16475 |
return(0); // More than one table |
16476 |
return(tables->table); |
|
1
by brian
clean slate |
16477 |
} |
16478 |
||
16479 |
||
16480 |
/** |
|
16481 |
calc how big buffer we need for comparing group entries. |
|
16482 |
*/ |
|
16483 |
||
16484 |
static void |
|
16485 |
calc_group_buffer(JOIN *join,ORDER *group) |
|
16486 |
{ |
|
16487 |
uint key_length=0, parts=0, null_parts=0; |
|
16488 |
||
16489 |
if (group) |
|
16490 |
join->group= 1; |
|
16491 |
for (; group ; group=group->next) |
|
16492 |
{ |
|
16493 |
Item *group_item= *group->item; |
|
16494 |
Field *field= group_item->get_tmp_table_field(); |
|
16495 |
if (field) |
|
16496 |
{ |
|
16497 |
enum_field_types type; |
|
16498 |
if ((type= field->type()) == MYSQL_TYPE_BLOB) |
|
16499 |
key_length+=MAX_BLOB_WIDTH; // Can't be used as a key |
|
16500 |
else if (type == MYSQL_TYPE_VARCHAR || type == MYSQL_TYPE_VAR_STRING) |
|
16501 |
key_length+= field->field_length + HA_KEY_BLOB_LENGTH; |
|
16502 |
else |
|
16503 |
key_length+= field->pack_length(); |
|
16504 |
} |
|
16505 |
else |
|
16506 |
{ |
|
16507 |
switch (group_item->result_type()) { |
|
16508 |
case REAL_RESULT: |
|
16509 |
key_length+= sizeof(double); |
|
16510 |
break; |
|
16511 |
case INT_RESULT: |
|
152
by Brian Aker
longlong replacement |
16512 |
key_length+= sizeof(int64_t); |
1
by brian
clean slate |
16513 |
break; |
16514 |
case DECIMAL_RESULT: |
|
16515 |
key_length+= my_decimal_get_binary_size(group_item->max_length - |
|
16516 |
(group_item->decimals ? 1 : 0), |
|
16517 |
group_item->decimals); |
|
16518 |
break; |
|
16519 |
case STRING_RESULT: |
|
16520 |
{ |
|
16521 |
enum enum_field_types type= group_item->field_type(); |
|
16522 |
/* |
|
16523 |
As items represented as DATE/TIME fields in the group buffer |
|
16524 |
have STRING_RESULT result type, we increase the length |
|
16525 |
by 8 as maximum pack length of such fields. |
|
16526 |
*/ |
|
16527 |
if (type == MYSQL_TYPE_TIME || |
|
97
by Brian Aker
DATE cleanup. |
16528 |
type == MYSQL_TYPE_NEWDATE || |
1
by brian
clean slate |
16529 |
type == MYSQL_TYPE_DATETIME || |
16530 |
type == MYSQL_TYPE_TIMESTAMP) |
|
16531 |
{ |
|
16532 |
key_length+= 8; |
|
16533 |
} |
|
16534 |
else |
|
16535 |
{ |
|
16536 |
/* |
|
16537 |
Group strings are taken as varstrings and require an length field. |
|
16538 |
A field is not yet created by create_tmp_field() |
|
16539 |
and the sizes should match up. |
|
16540 |
*/ |
|
16541 |
key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH; |
|
16542 |
} |
|
16543 |
break; |
|
16544 |
} |
|
16545 |
default: |
|
16546 |
/* This case should never be choosen */ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16547 |
assert(0); |
1
by brian
clean slate |
16548 |
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR)); |
16549 |
} |
|
16550 |
} |
|
16551 |
parts++; |
|
16552 |
if (group_item->maybe_null) |
|
16553 |
null_parts++; |
|
16554 |
} |
|
16555 |
join->tmp_table_param.group_length=key_length+null_parts; |
|
16556 |
join->tmp_table_param.group_parts=parts; |
|
16557 |
join->tmp_table_param.group_null_parts=null_parts; |
|
16558 |
} |
|
16559 |
||
16560 |
||
16561 |
/** |
|
16562 |
allocate group fields or take prepared (cached). |
|
16563 |
||
16564 |
@param main_join join of current select |
|
16565 |
@param curr_join current join (join of current select or temporary copy |
|
16566 |
of it) |
|
16567 |
||
16568 |
@retval |
|
16569 |
0 ok |
|
16570 |
@retval |
|
16571 |
1 failed |
|
16572 |
*/ |
|
16573 |
||
16574 |
static bool |
|
16575 |
make_group_fields(JOIN *main_join, JOIN *curr_join) |
|
16576 |
{ |
|
16577 |
if (main_join->group_fields_cache.elements) |
|
16578 |
{ |
|
16579 |
curr_join->group_fields= main_join->group_fields_cache; |
|
16580 |
curr_join->sort_and_group= 1; |
|
16581 |
} |
|
16582 |
else |
|
16583 |
{ |
|
16584 |
if (alloc_group_fields(curr_join, curr_join->group_list)) |
|
16585 |
return (1); |
|
16586 |
main_join->group_fields_cache= curr_join->group_fields; |
|
16587 |
} |
|
16588 |
return (0); |
|
16589 |
} |
|
16590 |
||
16591 |
||
16592 |
/** |
|
16593 |
Get a list of buffers for saveing last group. |
|
16594 |
||
16595 |
Groups are saved in reverse order for easyer check loop. |
|
16596 |
*/ |
|
16597 |
||
16598 |
static bool |
|
16599 |
alloc_group_fields(JOIN *join,ORDER *group) |
|
16600 |
{ |
|
16601 |
if (group) |
|
16602 |
{ |
|
16603 |
for (; group ; group=group->next) |
|
16604 |
{ |
|
55
by brian
Update for using real bool types. |
16605 |
Cached_item *tmp=new_Cached_item(join->thd, *group->item, false); |
1
by brian
clean slate |
16606 |
if (!tmp || join->group_fields.push_front(tmp)) |
55
by brian
Update for using real bool types. |
16607 |
return true; |
1
by brian
clean slate |
16608 |
} |
16609 |
} |
|
16610 |
join->sort_and_group=1; /* Mark for do_select */ |
|
55
by brian
Update for using real bool types. |
16611 |
return false; |
1
by brian
clean slate |
16612 |
} |
16613 |
||
16614 |
||
16615 |
/* |
|
16616 |
Test if a single-row cache of items changed, and update the cache. |
|
16617 |
||
16618 |
@details Test if a list of items that typically represents a result |
|
16619 |
row has changed. If the value of some item changed, update the cached |
|
16620 |
value for this item. |
|
16621 |
||
16622 |
@param list list of <item, cached_value> pairs stored as Cached_item. |
|
16623 |
||
16624 |
@return -1 if no item changed |
|
16625 |
@return index of the first item that changed |
|
16626 |
*/ |
|
16627 |
||
16628 |
int test_if_item_cache_changed(List<Cached_item> &list) |
|
16629 |
{ |
|
16630 |
List_iterator<Cached_item> li(list); |
|
16631 |
int idx= -1,i; |
|
16632 |
Cached_item *buff; |
|
16633 |
||
16634 |
for (i=(int) list.elements-1 ; (buff=li++) ; i--) |
|
16635 |
{ |
|
16636 |
if (buff->cmp()) |
|
16637 |
idx=i; |
|
16638 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16639 |
return(idx); |
1
by brian
clean slate |
16640 |
} |
16641 |
||
16642 |
||
16643 |
/** |
|
16644 |
Setup copy_fields to save fields at start of new group. |
|
16645 |
||
16646 |
Setup copy_fields to save fields at start of new group |
|
16647 |
||
16648 |
Only FIELD_ITEM:s and FUNC_ITEM:s needs to be saved between groups. |
|
16649 |
Change old item_field to use a new field with points at saved fieldvalue |
|
16650 |
This function is only called before use of send_fields. |
|
16651 |
||
16652 |
@param thd THD pointer |
|
16653 |
@param param temporary table parameters |
|
16654 |
@param ref_pointer_array array of pointers to top elements of filed list |
|
16655 |
@param res_selected_fields new list of items of select item list |
|
16656 |
@param res_all_fields new list of all items |
|
16657 |
@param elements number of elements in select item list |
|
16658 |
@param all_fields all fields list |
|
16659 |
||
16660 |
@todo |
|
16661 |
In most cases this result will be sent to the user. |
|
16662 |
This should be changed to use copy_int or copy_real depending |
|
16663 |
on how the value is to be used: In some cases this may be an |
|
16664 |
argument in a group function, like: IF(ISNULL(col),0,COUNT(*)) |
|
16665 |
||
16666 |
@retval |
|
16667 |
0 ok |
|
16668 |
@retval |
|
16669 |
!=0 error |
|
16670 |
*/ |
|
16671 |
||
16672 |
bool |
|
16673 |
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param, |
|
16674 |
Item **ref_pointer_array, |
|
16675 |
List<Item> &res_selected_fields, List<Item> &res_all_fields, |
|
16676 |
uint elements, List<Item> &all_fields) |
|
16677 |
{ |
|
16678 |
Item *pos; |
|
16679 |
List_iterator_fast<Item> li(all_fields); |
|
16680 |
Copy_field *copy= NULL; |
|
16681 |
res_selected_fields.empty(); |
|
16682 |
res_all_fields.empty(); |
|
16683 |
List_iterator_fast<Item> itr(res_all_fields); |
|
16684 |
List<Item> extra_funcs; |
|
16685 |
uint i, border= all_fields.elements - elements; |
|
16686 |
||
16687 |
if (param->field_count && |
|
16688 |
!(copy=param->copy_field= new Copy_field[param->field_count])) |
|
16689 |
goto err2; |
|
16690 |
||
16691 |
param->copy_funcs.empty(); |
|
16692 |
for (i= 0; (pos= li++); i++) |
|
16693 |
{ |
|
16694 |
Field *field; |
|
16695 |
uchar *tmp; |
|
16696 |
Item *real_pos= pos->real_item(); |
|
16697 |
if (real_pos->type() == Item::FIELD_ITEM) |
|
16698 |
{ |
|
16699 |
Item_field *item; |
|
16700 |
if (!(item= new Item_field(thd, ((Item_field*) real_pos)))) |
|
16701 |
goto err; |
|
16702 |
if (pos->type() == Item::REF_ITEM) |
|
16703 |
{ |
|
16704 |
/* preserve the names of the ref when dereferncing */ |
|
16705 |
Item_ref *ref= (Item_ref *) pos; |
|
16706 |
item->db_name= ref->db_name; |
|
16707 |
item->table_name= ref->table_name; |
|
16708 |
item->name= ref->name; |
|
16709 |
} |
|
16710 |
pos= item; |
|
16711 |
if (item->field->flags & BLOB_FLAG) |
|
16712 |
{ |
|
16713 |
if (!(pos= new Item_copy_string(pos))) |
|
16714 |
goto err; |
|
16715 |
/* |
|
16716 |
Item_copy_string::copy for function can call |
|
16717 |
Item_copy_string::val_int for blob via Item_ref. |
|
16718 |
But if Item_copy_string::copy for blob isn't called before, |
|
16719 |
it's value will be wrong |
|
16720 |
so let's insert Item_copy_string for blobs in the beginning of |
|
16721 |
copy_funcs |
|
16722 |
(to see full test case look at having.test, BUG #4358) |
|
16723 |
*/ |
|
16724 |
if (param->copy_funcs.push_front(pos)) |
|
16725 |
goto err; |
|
16726 |
} |
|
16727 |
else |
|
16728 |
{ |
|
16729 |
/* |
|
16730 |
set up save buffer and change result_field to point at |
|
16731 |
saved value |
|
16732 |
*/ |
|
16733 |
field= item->field; |
|
16734 |
item->result_field=field->new_field(thd->mem_root,field->table, 1); |
|
16735 |
/* |
|
16736 |
We need to allocate one extra byte for null handling and |
|
16737 |
another extra byte to not get warnings from purify in |
|
16738 |
Field_string::val_int |
|
16739 |
*/ |
|
16740 |
if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2))) |
|
16741 |
goto err; |
|
16742 |
if (copy) |
|
16743 |
{ |
|
16744 |
copy->set(tmp, item->result_field); |
|
16745 |
item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1); |
|
16746 |
#ifdef HAVE_purify |
|
16747 |
copy->to_ptr[copy->from_length]= 0; |
|
16748 |
#endif |
|
16749 |
copy++; |
|
16750 |
} |
|
16751 |
} |
|
16752 |
} |
|
16753 |
else if ((real_pos->type() == Item::FUNC_ITEM || |
|
16754 |
real_pos->type() == Item::SUBSELECT_ITEM || |
|
16755 |
real_pos->type() == Item::CACHE_ITEM || |
|
16756 |
real_pos->type() == Item::COND_ITEM) && |
|
16757 |
!real_pos->with_sum_func) |
|
16758 |
{ // Save for send fields |
|
16759 |
pos= real_pos; |
|
16760 |
/* TODO: |
|
16761 |
In most cases this result will be sent to the user. |
|
16762 |
This should be changed to use copy_int or copy_real depending |
|
16763 |
on how the value is to be used: In some cases this may be an |
|
16764 |
argument in a group function, like: IF(ISNULL(col),0,COUNT(*)) |
|
16765 |
*/ |
|
16766 |
if (!(pos=new Item_copy_string(pos))) |
|
16767 |
goto err; |
|
16768 |
if (i < border) // HAVING, ORDER and GROUP BY |
|
16769 |
{ |
|
16770 |
if (extra_funcs.push_back(pos)) |
|
16771 |
goto err; |
|
16772 |
} |
|
16773 |
else if (param->copy_funcs.push_back(pos)) |
|
16774 |
goto err; |
|
16775 |
} |
|
16776 |
res_all_fields.push_back(pos); |
|
16777 |
ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]= |
|
16778 |
pos; |
|
16779 |
} |
|
16780 |
param->copy_field_end= copy; |
|
16781 |
||
16782 |
for (i= 0; i < border; i++) |
|
16783 |
itr++; |
|
16784 |
itr.sublist(res_selected_fields, elements); |
|
16785 |
/* |
|
16786 |
Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any |
|
16787 |
reference used in these will resolve to a item that is already calculated |
|
16788 |
*/ |
|
16789 |
param->copy_funcs.concat(&extra_funcs); |
|
16790 |
||
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16791 |
return(0); |
1
by brian
clean slate |
16792 |
|
16793 |
err: |
|
16794 |
if (copy) |
|
16795 |
delete [] param->copy_field; // This is never 0 |
|
16796 |
param->copy_field=0; |
|
16797 |
err2: |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16798 |
return(true); |
1
by brian
clean slate |
16799 |
} |
16800 |
||
16801 |
||
16802 |
/** |
|
16803 |
Make a copy of all simple SELECT'ed items. |
|
16804 |
||
16805 |
This is done at the start of a new group so that we can retrieve |
|
16806 |
these later when the group changes. |
|
16807 |
*/ |
|
16808 |
||
16809 |
void |
|
16810 |
copy_fields(TMP_TABLE_PARAM *param) |
|
16811 |
{ |
|
16812 |
Copy_field *ptr=param->copy_field; |
|
16813 |
Copy_field *end=param->copy_field_end; |
|
16814 |
||
16815 |
for (; ptr != end; ptr++) |
|
16816 |
(*ptr->do_copy)(ptr); |
|
16817 |
||
16818 |
List_iterator_fast<Item> it(param->copy_funcs); |
|
16819 |
Item_copy_string *item; |
|
16820 |
while ((item = (Item_copy_string*) it++)) |
|
16821 |
item->copy(); |
|
16822 |
} |
|
16823 |
||
16824 |
||
16825 |
/** |
|
16826 |
Make an array of pointers to sum_functions to speed up |
|
16827 |
sum_func calculation. |
|
16828 |
||
16829 |
@retval |
|
16830 |
0 ok |
|
16831 |
@retval |
|
16832 |
1 Error |
|
16833 |
*/ |
|
16834 |
||
16835 |
bool JOIN::alloc_func_list() |
|
16836 |
{ |
|
16837 |
uint func_count, group_parts; |
|
16838 |
||
16839 |
func_count= tmp_table_param.sum_func_count; |
|
16840 |
/* |
|
16841 |
If we are using rollup, we need a copy of the summary functions for |
|
16842 |
each level |
|
16843 |
*/ |
|
16844 |
if (rollup.state != ROLLUP::STATE_NONE) |
|
16845 |
func_count*= (send_group_parts+1); |
|
16846 |
||
16847 |
group_parts= send_group_parts; |
|
16848 |
/* |
|
16849 |
If distinct, reserve memory for possible |
|
16850 |
disctinct->group_by optimization |
|
16851 |
*/ |
|
16852 |
if (select_distinct) |
|
16853 |
{ |
|
16854 |
group_parts+= fields_list.elements; |
|
16855 |
/* |
|
16856 |
If the ORDER clause is specified then it's possible that |
|
16857 |
it also will be optimized, so reserve space for it too |
|
16858 |
*/ |
|
16859 |
if (order) |
|
16860 |
{ |
|
16861 |
ORDER *ord; |
|
16862 |
for (ord= order; ord; ord= ord->next) |
|
16863 |
group_parts++; |
|
16864 |
} |
|
16865 |
} |
|
16866 |
||
16867 |
/* This must use calloc() as rollup_make_fields depends on this */ |
|
16868 |
sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) + |
|
16869 |
sizeof(Item_sum***) * (group_parts+1)); |
|
16870 |
sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16871 |
return(sum_funcs == 0); |
1
by brian
clean slate |
16872 |
} |
16873 |
||
16874 |
||
16875 |
/** |
|
16876 |
Initialize 'sum_funcs' array with all Item_sum objects. |
|
16877 |
||
16878 |
@param field_list All items |
|
16879 |
@param send_fields Items in select list |
|
16880 |
@param before_group_by Set to 1 if this is called before GROUP BY handling |
|
55
by brian
Update for using real bool types. |
16881 |
@param recompute Set to true if sum_funcs must be recomputed |
1
by brian
clean slate |
16882 |
|
16883 |
@retval |
|
16884 |
0 ok |
|
16885 |
@retval |
|
16886 |
1 error |
|
16887 |
*/ |
|
16888 |
||
16889 |
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields, |
|
16890 |
bool before_group_by, bool recompute) |
|
16891 |
{ |
|
16892 |
List_iterator_fast<Item> it(field_list); |
|
16893 |
Item_sum **func; |
|
16894 |
Item *item; |
|
16895 |
||
16896 |
if (*sum_funcs && !recompute) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16897 |
return(false); /* We have already initialized sum_funcs. */ |
1
by brian
clean slate |
16898 |
|
16899 |
func= sum_funcs; |
|
16900 |
while ((item=it++)) |
|
16901 |
{ |
|
16902 |
if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() && |
|
16903 |
(!((Item_sum*) item)->depended_from() || |
|
16904 |
((Item_sum *)item)->depended_from() == select_lex)) |
|
16905 |
*func++= (Item_sum*) item; |
|
16906 |
} |
|
16907 |
if (before_group_by && rollup.state == ROLLUP::STATE_INITED) |
|
16908 |
{ |
|
16909 |
rollup.state= ROLLUP::STATE_READY; |
|
16910 |
if (rollup_make_fields(field_list, send_fields, &func)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16911 |
return(true); // Should never happen |
1
by brian
clean slate |
16912 |
} |
16913 |
else if (rollup.state == ROLLUP::STATE_NONE) |
|
16914 |
{ |
|
16915 |
for (uint i=0 ; i <= send_group_parts ;i++) |
|
16916 |
sum_funcs_end[i]= func; |
|
16917 |
} |
|
16918 |
else if (rollup.state == ROLLUP::STATE_READY) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16919 |
return(false); // Don't put end marker |
1
by brian
clean slate |
16920 |
*func=0; // End marker |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16921 |
return(false); |
1
by brian
clean slate |
16922 |
} |
16923 |
||
16924 |
||
16925 |
/** |
|
16926 |
Change all funcs and sum_funcs to fields in tmp table, and create |
|
16927 |
new list of all items. |
|
16928 |
||
16929 |
@param thd THD pointer |
|
16930 |
@param ref_pointer_array array of pointers to top elements of filed list |
|
16931 |
@param res_selected_fields new list of items of select item list |
|
16932 |
@param res_all_fields new list of all items |
|
16933 |
@param elements number of elements in select item list |
|
16934 |
@param all_fields all fields list |
|
16935 |
||
16936 |
@retval |
|
16937 |
0 ok |
|
16938 |
@retval |
|
16939 |
!=0 error |
|
16940 |
*/ |
|
16941 |
||
16942 |
static bool |
|
16943 |
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array, |
|
16944 |
List<Item> &res_selected_fields, |
|
16945 |
List<Item> &res_all_fields, |
|
16946 |
uint elements, List<Item> &all_fields) |
|
16947 |
{ |
|
16948 |
List_iterator_fast<Item> it(all_fields); |
|
16949 |
Item *item_field,*item; |
|
16950 |
||
16951 |
res_selected_fields.empty(); |
|
16952 |
res_all_fields.empty(); |
|
16953 |
||
16954 |
uint i, border= all_fields.elements - elements; |
|
16955 |
for (i= 0; (item= it++); i++) |
|
16956 |
{ |
|
16957 |
Field *field; |
|
16958 |
||
16959 |
if ((item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM) || |
|
16960 |
(item->type() == Item::FUNC_ITEM && |
|
16961 |
((Item_func*)item)->functype() == Item_func::SUSERVAR_FUNC)) |
|
16962 |
item_field= item; |
|
16963 |
else |
|
16964 |
{ |
|
16965 |
if (item->type() == Item::FIELD_ITEM) |
|
16966 |
{ |
|
16967 |
item_field= item->get_tmp_table_item(thd); |
|
16968 |
} |
|
16969 |
else if ((field= item->get_tmp_table_field())) |
|
16970 |
{ |
|
16971 |
if (item->type() == Item::SUM_FUNC_ITEM && field->table->group) |
|
16972 |
item_field= ((Item_sum*) item)->result_item(field); |
|
16973 |
else |
|
16974 |
item_field= (Item*) new Item_field(field); |
|
16975 |
if (!item_field) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
16976 |
return(true); // Fatal error |
1
by brian
clean slate |
16977 |
|
16978 |
if (item->real_item()->type() != Item::FIELD_ITEM) |
|
16979 |
field->orig_table= 0; |
|
16980 |
item_field->name= item->name; |
|
16981 |
if (item->type() == Item::REF_ITEM) |
|
16982 |
{ |
|
16983 |
Item_field *ifield= (Item_field *) item_field; |
|
16984 |
Item_ref *iref= (Item_ref *) item; |
|
16985 |
ifield->table_name= iref->table_name; |
|
16986 |
ifield->db_name= iref->db_name; |
|
16987 |
} |
|
16988 |
} |
|
16989 |
else |
|
16990 |
item_field= item; |
|
16991 |
} |
|
16992 |
res_all_fields.push_back(item_field); |
|
16993 |
ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]= |
|
16994 |
item_field; |
|
16995 |
} |
|
16996 |
||
16997 |
List_iterator_fast<Item> itr(res_all_fields); |
|
16998 |
for (i= 0; i < border; i++) |
|
16999 |
itr++; |
|
17000 |
itr.sublist(res_selected_fields, elements); |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
17001 |
return(false); |
1
by brian
clean slate |
17002 |
} |
17003 |
||
17004 |
||
17005 |
/** |
|
17006 |
Change all sum_func refs to fields to point at fields in tmp table. |
|
17007 |
Change all funcs to be fields in tmp table. |
|
17008 |
||
17009 |
@param thd THD pointer |
|
17010 |
@param ref_pointer_array array of pointers to top elements of filed list |
|
17011 |
@param res_selected_fields new list of items of select item list |
|
17012 |
@param res_all_fields new list of all items |
|
17013 |
@param elements number of elements in select item list |
|
17014 |
@param all_fields all fields list |
|
17015 |
||
17016 |
@retval |
|
17017 |
0 ok |
|
17018 |
@retval |
|
17019 |
1 error |
|
17020 |
*/ |
|
17021 |
||
17022 |
static bool |
|
17023 |
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array, |
|
17024 |
List<Item> &res_selected_fields, |
|
17025 |
List<Item> &res_all_fields, uint elements, |
|
17026 |
List<Item> &all_fields) |
|
17027 |
{ |
|
17028 |
List_iterator_fast<Item> it(all_fields); |
|
17029 |
Item *item, *new_item; |
|
17030 |
res_selected_fields.empty(); |
|
17031 |
res_all_fields.empty(); |
|
17032 |
||
17033 |
uint i, border= all_fields.elements - elements; |
|
17034 |
for (i= 0; (item= it++); i++) |
|
17035 |
{ |
|
17036 |
res_all_fields.push_back(new_item= item->get_tmp_table_item(thd)); |
|
17037 |
ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]= |
|
17038 |
new_item; |
|
17039 |
} |
|
17040 |
||
17041 |
List_iterator_fast<Item> itr(res_all_fields); |
|
17042 |
for (i= 0; i < border; i++) |
|
17043 |
itr++; |
|
17044 |
itr.sublist(res_selected_fields, elements); |
|
17045 |
||
17046 |
return thd->is_fatal_error; |
|
17047 |
} |
|
17048 |
||
17049 |
||
17050 |
||
17051 |
/****************************************************************************** |
|
17052 |
Code for calculating functions |
|
17053 |
******************************************************************************/ |
|
17054 |
||
17055 |
||
17056 |
/** |
|
17057 |
Call ::setup for all sum functions. |
|
17058 |
||
17059 |
@param thd thread handler |
|
17060 |
@param func_ptr sum function list |
|
17061 |
||
17062 |
@retval |
|
55
by brian
Update for using real bool types. |
17063 |
false ok |
1
by brian
clean slate |
17064 |
@retval |
55
by brian
Update for using real bool types. |
17065 |
true error |
1
by brian
clean slate |
17066 |
*/ |
17067 |
||
17068 |
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr) |
|
17069 |
{ |
|
17070 |
Item_sum *func; |
|
17071 |
while ((func= *(func_ptr++))) |
|
17072 |
{ |
|
17073 |
if (func->setup(thd)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
17074 |
return(true); |
1
by brian
clean slate |
17075 |
} |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
17076 |
return(false); |
1
by brian
clean slate |
17077 |
} |
17078 |
||
17079 |
||
17080 |
static void |
|
17081 |
init_tmptable_sum_functions(Item_sum **func_ptr) |
|
17082 |
{ |
|
17083 |
Item_sum *func; |
|
17084 |
while ((func= *(func_ptr++))) |
|
17085 |
func->reset_field(); |
|
17086 |
} |
|
17087 |
||
17088 |
||
17089 |
/** Update record 0 in tmp_table from record 1. */ |
|
17090 |
||
17091 |
static void |
|
17092 |
update_tmptable_sum_func(Item_sum **func_ptr, |
|
17093 |
TABLE *tmp_table __attribute__((unused))) |
|
17094 |
{ |
|
17095 |
Item_sum *func; |
|
17096 |
while ((func= *(func_ptr++))) |
|
17097 |
func->update_field(); |
|
17098 |
} |
|
17099 |
||
17100 |
||
17101 |
/** Copy result of sum functions to record in tmp_table. */ |
|
17102 |
||
17103 |
static void |
|
17104 |
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr) |
|
17105 |
{ |
|
17106 |
for (; func_ptr != end_ptr ; func_ptr++) |
|
17107 |
(void) (*func_ptr)->save_in_result_field(1); |
|
17108 |
return; |
|
17109 |
} |
|
17110 |
||
17111 |
||
17112 |
static bool |
|
17113 |
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr) |
|
17114 |
{ |
|
17115 |
for (; func_ptr != end_ptr ;func_ptr++) |
|
17116 |
{ |
|
17117 |
if ((*func_ptr)->reset()) |
|
17118 |
return 1; |
|
17119 |
} |
|
17120 |
/* If rollup, calculate the upper sum levels */ |
|
17121 |
for ( ; *func_ptr ; func_ptr++) |
|
17122 |
{ |
|
17123 |
if ((*func_ptr)->add()) |
|
17124 |
return 1; |
|
17125 |
} |
|
17126 |
return 0; |
|
17127 |
} |
|
17128 |
||
17129 |
||
17130 |
static bool |
|
17131 |
update_sum_func(Item_sum **func_ptr) |
|
17132 |
{ |
|
17133 |
Item_sum *func; |
|
17134 |
for (; (func= (Item_sum*) *func_ptr) ; func_ptr++) |
|
17135 |
if (func->add()) |
|
17136 |
return 1; |
|
17137 |
return 0; |
|
17138 |
} |
|
17139 |
||
17140 |
/** Copy result of functions to record in tmp_table. */ |
|
17141 |
||
17142 |
void |
|
17143 |
copy_funcs(Item **func_ptr) |
|
17144 |
{ |
|
17145 |
Item *func; |
|
17146 |
for (; (func = *func_ptr) ; func_ptr++) |
|
17147 |
func->save_in_result_field(1); |
|
17148 |
} |
|
17149 |
||
17150 |
||
17151 |
/** |
|
17152 |
Create a condition for a const reference and add this to the |
|
17153 |
currenct select for the table. |
|
17154 |
*/ |
|
17155 |
||
17156 |
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab) |
|
17157 |
{ |
|
17158 |
if (!join_tab->ref.key_parts) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
17159 |
return(false); |
1
by brian
clean slate |
17160 |
|
17161 |
Item_cond_and *cond=new Item_cond_and(); |
|
17162 |
TABLE *table=join_tab->table; |
|
17163 |
int error; |
|
17164 |
if (!cond) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
17165 |
return(true); |
1
by brian
clean slate |
17166 |
|
17167 |
for (uint i=0 ; i < join_tab->ref.key_parts ; i++) |
|
17168 |
{ |
|
17169 |
Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i]. |
|
17170 |
fieldnr-1]; |
|
17171 |
Item *value=join_tab->ref.items[i]; |
|
17172 |
cond->add(new Item_func_equal(new Item_field(field), value)); |
|
17173 |
} |
|
17174 |
if (thd->is_fatal_error) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
17175 |
return(true); |
1
by brian
clean slate |
17176 |
|
17177 |
if (!cond->fixed) |
|
17178 |
cond->fix_fields(thd, (Item**)&cond); |
|
17179 |
if (join_tab->select) |
|
17180 |
{ |
|
17181 |
error=(int) cond->add(join_tab->select->cond); |
|
17182 |
join_tab->select_cond=join_tab->select->cond=cond; |
|
17183 |
} |
|
17184 |
else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0, |
|
17185 |
&error))) |
|
17186 |
join_tab->select_cond=cond; |
|
17187 |
||
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
17188 |
return(error ? true : false); |
1
by brian
clean slate |
17189 |
} |
17190 |
||
17191 |
||
17192 |
/** |
|
17193 |
Free joins of subselect of this select. |
|
17194 |
||
17195 |
@param thd THD pointer |
|
17196 |
@param select pointer to st_select_lex which subselects joins we will free |
|
17197 |
*/ |
|
17198 |
||
77.1.45
by Monty Taylor
Warning fixes. |
17199 |
void free_underlaid_joins(THD *thd __attribute__((__unused__)), |
17200 |
SELECT_LEX *select) |
|
1
by brian
clean slate |
17201 |
{ |
17202 |
for (SELECT_LEX_UNIT *unit= select->first_inner_unit(); |
|
17203 |
unit; |
|
17204 |
unit= unit->next_unit()) |
|
17205 |
unit->cleanup(); |
|
17206 |
} |
|
17207 |
||
17208 |
/**************************************************************************** |
|
17209 |
ROLLUP handling |
|
17210 |
****************************************************************************/ |
|
17211 |
||
17212 |
/** |
|
17213 |
Replace occurences of group by fields in an expression by ref items. |
|
17214 |
||
17215 |
The function replaces occurrences of group by fields in expr |
|
17216 |
by ref objects for these fields unless they are under aggregate |
|
17217 |
functions. |
|
17218 |
The function also corrects value of the the maybe_null attribute |
|
17219 |
for the items of all subexpressions containing group by fields. |
|
17220 |
||
17221 |
@b EXAMPLES |
|
17222 |
@code |
|
17223 |
SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP |
|
17224 |
SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP |
|
17225 |
@endcode |
|
17226 |
||
17227 |
@b IMPLEMENTATION |
|
17228 |
||
17229 |
The function recursively traverses the tree of the expr expression, |
|
17230 |
looks for occurrences of the group by fields that are not under |
|
17231 |
aggregate functions and replaces them for the corresponding ref items. |
|
17232 |
||
17233 |
@note |
|
17234 |
This substitution is needed GROUP BY queries with ROLLUP if |
|
17235 |
SELECT list contains expressions over group by attributes. |
|
17236 |
||
17237 |
@param thd reference to the context |
|
17238 |
@param expr expression to make replacement |
|
17239 |
@param group_list list of references to group by items |
|
17240 |
@param changed out: returns 1 if item contains a replaced field item |
|
17241 |
||
17242 |
@todo |
|
17243 |
- TODO: Some functions are not null-preserving. For those functions |
|
17244 |
updating of the maybe_null attribute is an overkill. |
|
17245 |
||
17246 |
@retval |
|
17247 |
0 if ok |
|
17248 |
@retval |
|
17249 |
1 on error |
|
17250 |
*/ |
|
17251 |
||
17252 |
static bool change_group_ref(THD *thd, Item_func *expr, ORDER *group_list, |
|
17253 |
bool *changed) |
|
17254 |
{ |
|
17255 |
if (expr->arg_count) |
|
17256 |
{ |
|
17257 |
Name_resolution_context *context= &thd->lex->current_select->context; |
|
17258 |
Item **arg,**arg_end; |
|
55
by brian
Update for using real bool types. |
17259 |
bool arg_changed= false; |
1
by brian
clean slate |
17260 |
for (arg= expr->arguments(), |
17261 |
arg_end= expr->arguments()+expr->arg_count; |
|
17262 |
arg != arg_end; arg++) |
|
17263 |
{ |
|
17264 |
Item *item= *arg; |
|
17265 |
if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM) |
|
17266 |
{ |
|
17267 |
ORDER *group_tmp; |
|
17268 |
for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next) |
|
17269 |
{ |
|
17270 |
if (item->eq(*group_tmp->item,0)) |
|
17271 |
{ |
|
17272 |
Item *new_item; |
|
17273 |
if (!(new_item= new Item_ref(context, group_tmp->item, 0, |
|
17274 |
item->name))) |
|
17275 |
return 1; // fatal_error is set |
|
17276 |
thd->change_item_tree(arg, new_item); |
|
55
by brian
Update for using real bool types. |
17277 |
arg_changed= true; |
1
by brian
clean slate |
17278 |
} |
17279 |
} |
|
17280 |
} |
|
17281 |
else if (item->type() == Item::FUNC_ITEM) |
|
17282 |
{ |
|
17283 |
if (change_group_ref(thd, (Item_func *) item, group_list, &arg_changed)) |
|
17284 |
return 1; |
|
17285 |
} |
|
17286 |
} |
|
17287 |
if (arg_changed) |
|
17288 |
{ |
|
17289 |
expr->maybe_null= 1; |
|
55
by brian
Update for using real bool types. |
17290 |
*changed= true; |
1
by brian
clean slate |
17291 |
} |
17292 |
} |
|
17293 |
return 0; |
|
17294 |
} |
|
17295 |
||
17296 |
||
17297 |
/** Allocate memory needed for other rollup functions. */ |
|
17298 |
||
17299 |
bool JOIN::rollup_init() |
|
17300 |
{ |
|
17301 |
uint i,j; |
|
17302 |
Item **ref_array; |
|
17303 |
||
17304 |
tmp_table_param.quick_group= 0; // Can't create groups in tmp table |
|
17305 |
rollup.state= ROLLUP::STATE_INITED; |
|
17306 |
||
17307 |
/* |
|
17308 |
Create pointers to the different sum function groups |
|
17309 |
These are updated by rollup_make_fields() |
|
17310 |
*/ |
|
17311 |
tmp_table_param.group_parts= send_group_parts; |
|
17312 |
||
17313 |
if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) + |
|
17314 |
sizeof(Item**) + |
|
17315 |
sizeof(List<Item>) + |
|
17316 |
ref_pointer_array_size) |
|
17317 |
* send_group_parts ))) |
|
17318 |
return 1; |
|
17319 |
||
17320 |
rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts); |
|
17321 |
rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts); |
|
17322 |
ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts); |
|
17323 |
||
17324 |
/* |
|
17325 |
Prepare space for field list for the different levels |
|
17326 |
These will be filled up in rollup_make_fields() |
|
17327 |
*/ |
|
17328 |
for (i= 0 ; i < send_group_parts ; i++) |
|
17329 |
{ |
|
17330 |
rollup.null_items[i]= new (thd->mem_root) Item_null_result(); |
|
17331 |
List<Item> *rollup_fields= &rollup.fields[i]; |
|
17332 |
rollup_fields->empty(); |
|
17333 |
rollup.ref_pointer_arrays[i]= ref_array; |
|
17334 |
ref_array+= all_fields.elements; |
|
17335 |
} |
|
17336 |
for (i= 0 ; i < send_group_parts; i++) |
|
17337 |
{ |
|
17338 |
for (j=0 ; j < fields_list.elements ; j++) |
|
17339 |
rollup.fields[i].push_back(rollup.null_items[i]); |
|
17340 |
} |
|
17341 |
List_iterator<Item> it(all_fields); |
|
17342 |
Item *item; |
|
17343 |
while ((item= it++)) |
|
17344 |
{ |
|
17345 |
ORDER *group_tmp; |
|
17346 |
bool found_in_group= 0; |
|
17347 |
||
17348 |
for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next) |
|
17349 |
{ |
|
17350 |
if (*group_tmp->item == item) |
|
17351 |
{ |
|
17352 |
item->maybe_null= 1; |
|
17353 |
found_in_group= 1; |
|
17354 |
if (item->const_item()) |
|
17355 |
{ |
|
17356 |
/* |
|
17357 |
For ROLLUP queries each constant item referenced in GROUP BY list |
|
17358 |
is wrapped up into an Item_func object yielding the same value |
|
17359 |
as the constant item. The objects of the wrapper class are never |
|
17360 |
considered as constant items and besides they inherit all |
|
17361 |
properties of the Item_result_field class. |
|
17362 |
This wrapping allows us to ensure writing constant items |
|
17363 |
into temporary tables whenever the result of the ROLLUP |
|
17364 |
operation has to be written into a temporary table, e.g. when |
|
17365 |
ROLLUP is used together with DISTINCT in the SELECT list. |
|
17366 |
Usually when creating temporary tables for a intermidiate |
|
17367 |
result we do not include fields for constant expressions. |
|
17368 |
*/ |
|
17369 |
Item* new_item= new Item_func_rollup_const(item); |
|
17370 |
if (!new_item) |
|
17371 |
return 1; |
|
17372 |
new_item->fix_fields(thd, (Item **) 0); |
|
17373 |
thd->change_item_tree(it.ref(), new_item); |
|
17374 |
for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next) |
|
17375 |
{ |
|
17376 |
if (*tmp->item == item) |
|
17377 |
thd->change_item_tree(tmp->item, new_item); |
|
17378 |
} |
|
17379 |
} |
|
17380 |
} |
|
17381 |
} |
|
17382 |
if (item->type() == Item::FUNC_ITEM && !found_in_group) |
|
17383 |
{ |
|
55
by brian
Update for using real bool types. |
17384 |
bool changed= false; |
1
by brian
clean slate |
17385 |
if (change_group_ref(thd, (Item_func *) item, group_list, &changed)) |
17386 |
return 1; |
|
17387 |
/* |
|
17388 |
We have to prevent creation of a field in a temporary table for |
|
17389 |
an expression that contains GROUP BY attributes. |
|
17390 |
Marking the expression item as 'with_sum_func' will ensure this. |
|
17391 |
*/ |
|
17392 |
if (changed) |
|
17393 |
item->with_sum_func= 1; |
|
17394 |
} |
|
17395 |
} |
|
17396 |
return 0; |
|
17397 |
} |
|
17398 |
||
17399 |
||
17400 |
/** |
|
17401 |
Fill up rollup structures with pointers to fields to use. |
|
17402 |
||
17403 |
Creates copies of item_sum items for each sum level. |
|
17404 |
||
17405 |
@param fields_arg List of all fields (hidden and real ones) |
|
17406 |
@param sel_fields Pointer to selected fields |
|
17407 |
@param func Store here a pointer to all fields |
|
17408 |
||
17409 |
@retval |
|
17410 |
0 if ok; |
|
17411 |
In this case func is pointing to next not used element. |
|
17412 |
@retval |
|
17413 |
1 on error |
|
17414 |
*/ |
|
17415 |
||
17416 |
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields, |
|
17417 |
Item_sum ***func) |
|
17418 |
{ |
|
17419 |
List_iterator_fast<Item> it(fields_arg); |
|
17420 |
Item *first_field= sel_fields.head(); |
|
17421 |
uint level; |
|
17422 |
||
17423 |
/* |
|
17424 |
Create field lists for the different levels |
|
17425 |
||
17426 |
The idea here is to have a separate field list for each rollup level to |
|
17427 |
avoid all runtime checks of which columns should be NULL. |
|
17428 |
||
17429 |
The list is stored in reverse order to get sum function in such an order |
|
17430 |
in func that it makes it easy to reset them with init_sum_functions() |
|
17431 |
||
17432 |
Assuming: SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP |
|
17433 |
||
17434 |
rollup.fields[0] will contain list where a,b,c is NULL |
|
17435 |
rollup.fields[1] will contain list where b,c is NULL |
|
17436 |
... |
|
17437 |
rollup.ref_pointer_array[#] points to fields for rollup.fields[#] |
|
17438 |
... |
|
17439 |
sum_funcs_end[0] points to all sum functions |
|
17440 |
sum_funcs_end[1] points to all sum functions, except grand totals |
|
17441 |
... |
|
17442 |
*/ |
|
17443 |
||
17444 |
for (level=0 ; level < send_group_parts ; level++) |
|
17445 |
{ |
|
17446 |
uint i; |
|
17447 |
uint pos= send_group_parts - level -1; |
|
17448 |
bool real_fields= 0; |
|
17449 |
Item *item; |
|
17450 |
List_iterator<Item> new_it(rollup.fields[pos]); |
|
17451 |
Item **ref_array_start= rollup.ref_pointer_arrays[pos]; |
|
17452 |
ORDER *start_group; |
|
17453 |
||
17454 |
/* Point to first hidden field */ |
|
17455 |
Item **ref_array= ref_array_start + fields_arg.elements-1; |
|
17456 |
||
17457 |
/* Remember where the sum functions ends for the previous level */ |
|
17458 |
sum_funcs_end[pos+1]= *func; |
|
17459 |
||
17460 |
/* Find the start of the group for this level */ |
|
17461 |
for (i= 0, start_group= group_list ; |
|
17462 |
i++ < pos ; |
|
17463 |
start_group= start_group->next) |
|
17464 |
; |
|
17465 |
||
17466 |
it.rewind(); |
|
17467 |
while ((item= it++)) |
|
17468 |
{ |
|
17469 |
if (item == first_field) |
|
17470 |
{ |
|
17471 |
real_fields= 1; // End of hidden fields |
|
17472 |
ref_array= ref_array_start; |
|
17473 |
} |
|
17474 |
||
17475 |
if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() && |
|
17476 |
(!((Item_sum*) item)->depended_from() || |
|
17477 |
((Item_sum *)item)->depended_from() == select_lex)) |
|
17478 |
||
17479 |
{ |
|
17480 |
/* |
|
17481 |
This is a top level summary function that must be replaced with |
|
17482 |
a sum function that is reset for this level. |
|
17483 |
||
17484 |
NOTE: This code creates an object which is not that nice in a |
|
17485 |
sub select. Fortunately it's not common to have rollup in |
|
17486 |
sub selects. |
|
17487 |
*/ |
|
17488 |
item= item->copy_or_same(thd); |
|
17489 |
((Item_sum*) item)->make_unique(); |
|
17490 |
*(*func)= (Item_sum*) item; |
|
17491 |
(*func)++; |
|
17492 |
} |
|
17493 |
else |
|
17494 |
{ |
|
17495 |
/* Check if this is something that is part of this group by */ |
|
17496 |
ORDER *group_tmp; |
|
17497 |
for (group_tmp= start_group, i= pos ; |
|
17498 |
group_tmp ; group_tmp= group_tmp->next, i++) |
|
17499 |
{ |
|
17500 |
if (*group_tmp->item == item) |
|
17501 |
{ |
|
17502 |
/* |
|
17503 |
This is an element that is used by the GROUP BY and should be |
|
17504 |
set to NULL in this level |
|
17505 |
*/ |
|
17506 |
Item_null_result *null_item= new (thd->mem_root) Item_null_result(); |
|
17507 |
if (!null_item) |
|
17508 |
return 1; |
|
17509 |
item->maybe_null= 1; // Value will be null sometimes |
|
17510 |
null_item->result_field= item->get_tmp_table_field(); |
|
17511 |
item= null_item; |
|
17512 |
break; |
|
17513 |
} |
|
17514 |
} |
|
17515 |
} |
|
17516 |
*ref_array= item; |
|
17517 |
if (real_fields) |
|
17518 |
{ |
|
17519 |
(void) new_it++; // Point to next item |
|
17520 |
new_it.replace(item); // Replace previous |
|
17521 |
ref_array++; |
|
17522 |
} |
|
17523 |
else |
|
17524 |
ref_array--; |
|
17525 |
} |
|
17526 |
} |
|
17527 |
sum_funcs_end[0]= *func; // Point to last function |
|
17528 |
return 0; |
|
17529 |
} |
|
17530 |
||
17531 |
/** |
|
17532 |
Send all rollup levels higher than the current one to the client. |
|
17533 |
||
17534 |
@b SAMPLE |
|
17535 |
@code |
|
17536 |
SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP |
|
17537 |
@endcode |
|
17538 |
||
17539 |
@param idx Level we are on: |
|
17540 |
- 0 = Total sum level |
|
17541 |
- 1 = First group changed (a) |
|
17542 |
- 2 = Second group changed (a,b) |
|
17543 |
||
17544 |
@retval |
|
17545 |
0 ok |
|
17546 |
@retval |
|
17547 |
1 If send_data_failed() |
|
17548 |
*/ |
|
17549 |
||
17550 |
int JOIN::rollup_send_data(uint idx) |
|
17551 |
{ |
|
17552 |
uint i; |
|
17553 |
for (i= send_group_parts ; i-- > idx ; ) |
|
17554 |
{ |
|
17555 |
/* Get reference pointers to sum functions in place */ |
|
17556 |
memcpy((char*) ref_pointer_array, |
|
17557 |
(char*) rollup.ref_pointer_arrays[i], |
|
17558 |
ref_pointer_array_size); |
|
17559 |
if ((!having || having->val_int())) |
|
17560 |
{ |
|
17561 |
if (send_records < unit->select_limit_cnt && do_send_rows && |
|
17562 |
result->send_data(rollup.fields[i])) |
|
17563 |
return 1; |
|
17564 |
send_records++; |
|
17565 |
} |
|
17566 |
} |
|
17567 |
/* Restore ref_pointer_array */ |
|
17568 |
set_items_ref_array(current_ref_pointer_array); |
|
17569 |
return 0; |
|
17570 |
} |
|
17571 |
||
17572 |
/** |
|
17573 |
Write all rollup levels higher than the current one to a temp table. |
|
17574 |
||
17575 |
@b SAMPLE |
|
17576 |
@code |
|
17577 |
SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP |
|
17578 |
@endcode |
|
17579 |
||
17580 |
@param idx Level we are on: |
|
17581 |
- 0 = Total sum level |
|
17582 |
- 1 = First group changed (a) |
|
17583 |
- 2 = Second group changed (a,b) |
|
17584 |
@param table reference to temp table |
|
17585 |
||
17586 |
@retval |
|
17587 |
0 ok |
|
17588 |
@retval |
|
17589 |
1 if write_data_failed() |
|
17590 |
*/ |
|
17591 |
||
17592 |
int JOIN::rollup_write_data(uint idx, TABLE *table_arg) |
|
17593 |
{ |
|
17594 |
uint i; |
|
17595 |
for (i= send_group_parts ; i-- > idx ; ) |
|
17596 |
{ |
|
17597 |
/* Get reference pointers to sum functions in place */ |
|
17598 |
memcpy((char*) ref_pointer_array, |
|
17599 |
(char*) rollup.ref_pointer_arrays[i], |
|
17600 |
ref_pointer_array_size); |
|
17601 |
if ((!having || having->val_int())) |
|
17602 |
{ |
|
17603 |
int write_error; |
|
17604 |
Item *item; |
|
17605 |
List_iterator_fast<Item> it(rollup.fields[i]); |
|
17606 |
while ((item= it++)) |
|
17607 |
{ |
|
17608 |
if (item->type() == Item::NULL_ITEM && item->is_result_field()) |
|
17609 |
item->save_in_result_field(1); |
|
17610 |
} |
|
17611 |
copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]); |
|
17612 |
if ((write_error= table_arg->file->ha_write_row(table_arg->record[0]))) |
|
17613 |
{ |
|
17614 |
if (create_myisam_from_heap(thd, table_arg, |
|
17615 |
tmp_table_param.start_recinfo, |
|
17616 |
&tmp_table_param.recinfo, |
|
17617 |
write_error, 0)) |
|
17618 |
return 1; |
|
17619 |
} |
|
17620 |
} |
|
17621 |
} |
|
17622 |
/* Restore ref_pointer_array */ |
|
17623 |
set_items_ref_array(current_ref_pointer_array); |
|
17624 |
return 0; |
|
17625 |
} |
|
17626 |
||
17627 |
/** |
|
17628 |
clear results if there are not rows found for group |
|
17629 |
(end_send_group/end_write_group) |
|
17630 |
*/ |
|
17631 |
||
17632 |
void JOIN::clear() |
|
17633 |
{ |
|
17634 |
clear_tables(this); |
|
17635 |
copy_fields(&tmp_table_param); |
|
17636 |
||
17637 |
if (sum_funcs) |
|
17638 |
{ |
|
17639 |
Item_sum *func, **func_ptr= sum_funcs; |
|
17640 |
while ((func= *(func_ptr++))) |
|
17641 |
func->clear(); |
|
17642 |
} |
|
17643 |
} |
|
17644 |
||
17645 |
/** |
|
17646 |
EXPLAIN handling. |
|
17647 |
||
17648 |
Send a description about what how the select will be done to stdout. |
|
17649 |
*/ |
|
17650 |
||
17651 |
void select_describe(JOIN *join, bool need_tmp_table, bool need_order, |
|
17652 |
bool distinct,const char *message) |
|
17653 |
{ |
|
17654 |
List<Item> field_list; |
|
17655 |
List<Item> item_list; |
|
17656 |
THD *thd=join->thd; |
|
17657 |
select_result *result=join->result; |
|
17658 |
Item *item_null= new Item_null(); |
|
17659 |
CHARSET_INFO *cs= system_charset_info; |
|
17660 |
int quick_type; |
|
17661 |
/* Don't log this into the slow query log */ |
|
17662 |
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED); |
|
17663 |
join->unit->offset_limit_cnt= 0; |
|
17664 |
||
17665 |
/* |
|
17666 |
NOTE: the number/types of items pushed into item_list must be in sync with |
|
17667 |
EXPLAIN column types as they're "defined" in THD::send_explain_fields() |
|
17668 |
*/ |
|
17669 |
if (message) |
|
17670 |
{ |
|
17671 |
item_list.push_back(new Item_int((int32) |
|
17672 |
join->select_lex->select_number)); |
|
17673 |
item_list.push_back(new Item_string(join->select_lex->type, |
|
17674 |
strlen(join->select_lex->type), cs)); |
|
17675 |
for (uint i=0 ; i < 7; i++) |
|
17676 |
item_list.push_back(item_null); |
|
17677 |
if (join->thd->lex->describe & DESCRIBE_EXTENDED) |
|
17678 |
item_list.push_back(item_null); |
|
17679 |
||
17680 |
item_list.push_back(new Item_string(message,strlen(message),cs)); |
|
17681 |
if (result->send_data(item_list)) |
|
17682 |
join->error= 1; |
|
17683 |
} |
|
17684 |
else if (join->select_lex == join->unit->fake_select_lex) |
|
17685 |
{ |
|
17686 |
/* |
|
17687 |
here we assume that the query will return at least two rows, so we |
|
17688 |
show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong |
|
17689 |
and no filesort will be actually done, but executing all selects in |
|
17690 |
the UNION to provide precise EXPLAIN information will hardly be |
|
17691 |
appreciated :) |
|
17692 |
*/ |
|
17693 |
char table_name_buffer[NAME_LEN]; |
|
17694 |
item_list.empty(); |
|
17695 |
/* id */ |
|
17696 |
item_list.push_back(new Item_null); |
|
17697 |
/* select_type */ |
|
17698 |
item_list.push_back(new Item_string(join->select_lex->type, |
|
17699 |
strlen(join->select_lex->type), |
|
17700 |
cs)); |
|
17701 |
/* table */ |
|
17702 |
{ |
|
17703 |
SELECT_LEX *sl= join->unit->first_select(); |
|
17704 |
uint len= 6, lastop= 0; |
|
17705 |
memcpy(table_name_buffer, STRING_WITH_LEN("<union")); |
|
17706 |
for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select()) |
|
17707 |
{ |
|
17708 |
len+= lastop; |
|
77.1.18
by Monty Taylor
Removed my_vsnprintf and my_snprintf. |
17709 |
lastop= snprintf(table_name_buffer + len, NAME_LEN - len, |
17710 |
"%u,", sl->select_number); |
|
1
by brian
clean slate |
17711 |
} |
17712 |
if (sl || len + lastop >= NAME_LEN) |
|
17713 |
{ |
|
17714 |
memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1); |
|
17715 |
len+= 4; |
|
17716 |
} |
|
17717 |
else |
|
17718 |
{ |
|
17719 |
len+= lastop; |
|
17720 |
table_name_buffer[len - 1]= '>'; // change ',' to '>' |
|
17721 |
} |
|
17722 |
item_list.push_back(new Item_string(table_name_buffer, len, cs)); |
|
17723 |
} |
|
17724 |
/* type */ |
|
17725 |
item_list.push_back(new Item_string(join_type_str[JT_ALL], |
|
17726 |
strlen(join_type_str[JT_ALL]), |
|
17727 |
cs)); |
|
17728 |
/* possible_keys */ |
|
17729 |
item_list.push_back(item_null); |
|
17730 |
/* key*/ |
|
17731 |
item_list.push_back(item_null); |
|
17732 |
/* key_len */ |
|
17733 |
item_list.push_back(item_null); |
|
17734 |
/* ref */ |
|
17735 |
item_list.push_back(item_null); |
|
17736 |
/* in_rows */ |
|
17737 |
if (join->thd->lex->describe & DESCRIBE_EXTENDED) |
|
17738 |
item_list.push_back(item_null); |
|
17739 |
/* rows */ |
|
17740 |
item_list.push_back(item_null); |
|
17741 |
/* extra */ |
|
17742 |
if (join->unit->global_parameters->order_list.first) |
|
17743 |
item_list.push_back(new Item_string("Using filesort", |
|
17744 |
14, cs)); |
|
17745 |
else |
|
17746 |
item_list.push_back(new Item_string("", 0, cs)); |
|
17747 |
||
17748 |
if (result->send_data(item_list)) |
|
17749 |
join->error= 1; |
|
17750 |
} |
|
17751 |
else |
|
17752 |
{ |
|
17753 |
table_map used_tables=0; |
|
17754 |
for (uint i=0 ; i < join->tables ; i++) |
|
17755 |
{ |
|
17756 |
JOIN_TAB *tab=join->join_tab+i; |
|
17757 |
TABLE *table=tab->table; |
|
17758 |
TABLE_LIST *table_list= tab->table->pos_in_table_list; |
|
17759 |
char buff[512]; |
|
17760 |
char buff1[512], buff2[512], buff3[512]; |
|
17761 |
char keylen_str_buf[64]; |
|
17762 |
String extra(buff, sizeof(buff),cs); |
|
17763 |
char table_name_buffer[NAME_LEN]; |
|
17764 |
String tmp1(buff1,sizeof(buff1),cs); |
|
17765 |
String tmp2(buff2,sizeof(buff2),cs); |
|
17766 |
String tmp3(buff3,sizeof(buff3),cs); |
|
17767 |
extra.length(0); |
|
17768 |
tmp1.length(0); |
|
17769 |
tmp2.length(0); |
|
17770 |
tmp3.length(0); |
|
17771 |
||
17772 |
quick_type= -1; |
|
17773 |
item_list.empty(); |
|
17774 |
/* id */ |
|
17775 |
item_list.push_back(new Item_uint((uint32) |
|
17776 |
join->select_lex->select_number)); |
|
17777 |
/* select_type */ |
|
17778 |
item_list.push_back(new Item_string(join->select_lex->type, |
|
17779 |
strlen(join->select_lex->type), |
|
17780 |
cs)); |
|
17781 |
if (tab->type == JT_ALL && tab->select && tab->select->quick) |
|
17782 |
{ |
|
17783 |
quick_type= tab->select->quick->get_type(); |
|
17784 |
if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) || |
|
17785 |
(quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) || |
|
17786 |
(quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION)) |
|
17787 |
tab->type = JT_INDEX_MERGE; |
|
17788 |
else |
|
17789 |
tab->type = JT_RANGE; |
|
17790 |
} |
|
17791 |
/* table */ |
|
17792 |
if (table->derived_select_number) |
|
17793 |
{ |
|
17794 |
/* Derived table name generation */ |
|
77.1.18
by Monty Taylor
Removed my_vsnprintf and my_snprintf. |
17795 |
int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1, |
17796 |
"<derived%u>", |
|
17797 |
table->derived_select_number); |
|
1
by brian
clean slate |
17798 |
item_list.push_back(new Item_string(table_name_buffer, len, cs)); |
17799 |
} |
|
17800 |
else |
|
17801 |
{ |
|
17802 |
TABLE_LIST *real_table= table->pos_in_table_list; |
|
17803 |
item_list.push_back(new Item_string(real_table->alias, |
|
17804 |
strlen(real_table->alias), |
|
17805 |
cs)); |
|
17806 |
} |
|
17807 |
/* "type" column */ |
|
17808 |
item_list.push_back(new Item_string(join_type_str[tab->type], |
|
17809 |
strlen(join_type_str[tab->type]), |
|
17810 |
cs)); |
|
17811 |
/* Build "possible_keys" value and add it to item_list */ |
|
17812 |
if (!tab->keys.is_clear_all()) |
|
17813 |
{ |
|
17814 |
uint j; |
|
17815 |
for (j=0 ; j < table->s->keys ; j++) |
|
17816 |
{ |
|
17817 |
if (tab->keys.is_set(j)) |
|
17818 |
{ |
|
17819 |
if (tmp1.length()) |
|
17820 |
tmp1.append(','); |
|
17821 |
tmp1.append(table->key_info[j].name, |
|
17822 |
strlen(table->key_info[j].name), |
|
17823 |
system_charset_info); |
|
17824 |
} |
|
17825 |
} |
|
17826 |
} |
|
17827 |
if (tmp1.length()) |
|
17828 |
item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs)); |
|
17829 |
else |
|
17830 |
item_list.push_back(item_null); |
|
17831 |
||
17832 |
/* Build "key", "key_len", and "ref" values and add them to item_list */ |
|
17833 |
if (tab->ref.key_parts) |
|
17834 |
{ |
|
17835 |
KEY *key_info=table->key_info+ tab->ref.key; |
|
17836 |
register uint length; |
|
17837 |
item_list.push_back(new Item_string(key_info->name, |
|
17838 |
strlen(key_info->name), |
|
17839 |
system_charset_info)); |
|
152
by Brian Aker
longlong replacement |
17840 |
length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) - |
1
by brian
clean slate |
17841 |
keylen_str_buf; |
17842 |
item_list.push_back(new Item_string(keylen_str_buf, length, |
|
17843 |
system_charset_info)); |
|
17844 |
for (store_key **ref=tab->ref.key_copy ; *ref ; ref++) |
|
17845 |
{ |
|
17846 |
if (tmp2.length()) |
|
17847 |
tmp2.append(','); |
|
17848 |
tmp2.append((*ref)->name(), strlen((*ref)->name()), |
|
17849 |
system_charset_info); |
|
17850 |
} |
|
17851 |
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs)); |
|
17852 |
} |
|
17853 |
else if (tab->type == JT_NEXT) |
|
17854 |
{ |
|
17855 |
KEY *key_info=table->key_info+ tab->index; |
|
17856 |
register uint length; |
|
17857 |
item_list.push_back(new Item_string(key_info->name, |
|
17858 |
strlen(key_info->name),cs)); |
|
152
by Brian Aker
longlong replacement |
17859 |
length= int64_t2str(key_info->key_length, keylen_str_buf, 10) - |
1
by brian
clean slate |
17860 |
keylen_str_buf; |
17861 |
item_list.push_back(new Item_string(keylen_str_buf, |
|
17862 |
length, |
|
17863 |
system_charset_info)); |
|
17864 |
item_list.push_back(item_null); |
|
17865 |
} |
|
17866 |
else if (tab->select && tab->select->quick) |
|
17867 |
{ |
|
17868 |
tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3); |
|
17869 |
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs)); |
|
17870 |
item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs)); |
|
17871 |
item_list.push_back(item_null); |
|
17872 |
} |
|
17873 |
else |
|
17874 |
{ |
|
17875 |
if (table_list->schema_table && |
|
17876 |
table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE) |
|
17877 |
{ |
|
17878 |
const char *tmp_buff; |
|
17879 |
int f_idx; |
|
17880 |
if (table_list->has_db_lookup_value) |
|
17881 |
{ |
|
17882 |
f_idx= table_list->schema_table->idx_field1; |
|
17883 |
tmp_buff= table_list->schema_table->fields_info[f_idx].field_name; |
|
17884 |
tmp2.append(tmp_buff, strlen(tmp_buff), cs); |
|
17885 |
} |
|
17886 |
if (table_list->has_table_lookup_value) |
|
17887 |
{ |
|
17888 |
if (table_list->has_db_lookup_value) |
|
17889 |
tmp2.append(','); |
|
17890 |
f_idx= table_list->schema_table->idx_field2; |
|
17891 |
tmp_buff= table_list->schema_table->fields_info[f_idx].field_name; |
|
17892 |
tmp2.append(tmp_buff, strlen(tmp_buff), cs); |
|
17893 |
} |
|
17894 |
if (tmp2.length()) |
|
17895 |
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs)); |
|
17896 |
else |
|
17897 |
item_list.push_back(item_null); |
|
17898 |
} |
|
17899 |
else |
|
17900 |
item_list.push_back(item_null); |
|
17901 |
item_list.push_back(item_null); |
|
17902 |
item_list.push_back(item_null); |
|
17903 |
} |
|
17904 |
||
17905 |
/* Add "rows" field to item_list. */ |
|
17906 |
if (table_list->schema_table) |
|
17907 |
{ |
|
17908 |
/* in_rows */ |
|
17909 |
if (join->thd->lex->describe & DESCRIBE_EXTENDED) |
|
17910 |
item_list.push_back(item_null); |
|
17911 |
/* rows */ |
|
17912 |
item_list.push_back(item_null); |
|
17913 |
} |
|
17914 |
else |
|
17915 |
{ |
|
17916 |
double examined_rows; |
|
17917 |
if (tab->select && tab->select->quick) |
|
17918 |
examined_rows= rows2double(tab->select->quick->records); |
|
17919 |
else if (tab->type == JT_NEXT || tab->type == JT_ALL) |
|
17920 |
examined_rows= rows2double(tab->limit ? tab->limit : |
|
17921 |
tab->table->file->records()); |
|
17922 |
else |
|
17923 |
examined_rows= join->best_positions[i].records_read; |
|
17924 |
||
152
by Brian Aker
longlong replacement |
17925 |
item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows, |
1
by brian
clean slate |
17926 |
MY_INT64_NUM_DECIMAL_DIGITS)); |
17927 |
||
17928 |
/* Add "filtered" field to item_list. */ |
|
17929 |
if (join->thd->lex->describe & DESCRIBE_EXTENDED) |
|
17930 |
{ |
|
17931 |
float f= 0.0; |
|
17932 |
if (examined_rows) |
|
17933 |
f= (float) (100.0 * join->best_positions[i].records_read / |
|
17934 |
examined_rows); |
|
17935 |
item_list.push_back(new Item_float(f, 2)); |
|
17936 |
} |
|
17937 |
} |
|
17938 |
||
17939 |
/* Build "Extra" field and add it to item_list. */ |
|
148
by Brian Aker
my_bool cleanup |
17940 |
bool key_read=table->key_read; |
1
by brian
clean slate |
17941 |
if ((tab->type == JT_NEXT || tab->type == JT_CONST) && |
17942 |
table->covering_keys.is_set(tab->index)) |
|
17943 |
key_read=1; |
|
17944 |
if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT && |
|
17945 |
!((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row) |
|
17946 |
key_read=1; |
|
17947 |
||
17948 |
if (tab->info) |
|
17949 |
item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs)); |
|
17950 |
else if (tab->packed_info & TAB_INFO_HAVE_VALUE) |
|
17951 |
{ |
|
17952 |
if (tab->packed_info & TAB_INFO_USING_INDEX) |
|
17953 |
extra.append(STRING_WITH_LEN("; Using index")); |
|
17954 |
if (tab->packed_info & TAB_INFO_USING_WHERE) |
|
17955 |
extra.append(STRING_WITH_LEN("; Using where")); |
|
17956 |
if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL) |
|
17957 |
extra.append(STRING_WITH_LEN("; Full scan on NULL key")); |
|
17958 |
/* Skip initial "; "*/ |
|
17959 |
const char *str= extra.ptr(); |
|
17960 |
uint32 len= extra.length(); |
|
17961 |
if (len) |
|
17962 |
{ |
|
17963 |
str += 2; |
|
17964 |
len -= 2; |
|
17965 |
} |
|
17966 |
item_list.push_back(new Item_string(str, len, cs)); |
|
17967 |
} |
|
17968 |
else |
|
17969 |
{ |
|
17970 |
uint keyno= MAX_KEY; |
|
17971 |
if (tab->ref.key_parts) |
|
17972 |
keyno= tab->ref.key; |
|
17973 |
else if (tab->select && tab->select->quick) |
|
17974 |
keyno = tab->select->quick->index; |
|
17975 |
||
17976 |
if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno && |
|
17977 |
table->file->pushed_idx_cond) |
|
17978 |
extra.append(STRING_WITH_LEN("; Using index condition")); |
|
17979 |
||
17980 |
if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || |
|
17981 |
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT || |
|
17982 |
quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) |
|
17983 |
{ |
|
17984 |
extra.append(STRING_WITH_LEN("; Using ")); |
|
17985 |
tab->select->quick->add_info_string(&extra); |
|
17986 |
} |
|
17987 |
if (tab->select) |
|
17988 |
{ |
|
17989 |
if (tab->use_quick == 2) |
|
17990 |
{ |
|
17991 |
/* 4 bits per 1 hex digit + terminating '\0' */ |
|
17992 |
char buf[MAX_KEY / 4 + 1]; |
|
17993 |
extra.append(STRING_WITH_LEN("; Range checked for each " |
|
17994 |
"record (index map: 0x")); |
|
17995 |
extra.append(tab->keys.print(buf)); |
|
17996 |
extra.append(')'); |
|
17997 |
} |
|
17998 |
else if (tab->select->cond) |
|
17999 |
{ |
|
18000 |
const COND *pushed_cond= tab->table->file->pushed_cond; |
|
18001 |
||
18002 |
if (thd->variables.engine_condition_pushdown && pushed_cond) |
|
18003 |
{ |
|
18004 |
extra.append(STRING_WITH_LEN("; Using where with pushed " |
|
18005 |
"condition")); |
|
18006 |
if (thd->lex->describe & DESCRIBE_EXTENDED) |
|
18007 |
{ |
|
18008 |
extra.append(STRING_WITH_LEN(": ")); |
|
18009 |
((COND *)pushed_cond)->print(&extra, QT_ORDINARY); |
|
18010 |
} |
|
18011 |
} |
|
18012 |
else |
|
18013 |
extra.append(STRING_WITH_LEN("; Using where")); |
|
18014 |
} |
|
18015 |
} |
|
18016 |
if (key_read) |
|
18017 |
{ |
|
18018 |
if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX) |
|
18019 |
extra.append(STRING_WITH_LEN("; Using index for group-by")); |
|
18020 |
else |
|
18021 |
extra.append(STRING_WITH_LEN("; Using index")); |
|
18022 |
} |
|
18023 |
if (table->reginfo.not_exists_optimize) |
|
18024 |
extra.append(STRING_WITH_LEN("; Not exists")); |
|
18025 |
||
18026 |
if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE && |
|
18027 |
!(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags & |
|
18028 |
HA_MRR_USE_DEFAULT_IMPL)) |
|
18029 |
{ |
|
18030 |
extra.append(STRING_WITH_LEN("; Using MRR")); |
|
18031 |
} |
|
18032 |
||
18033 |
if (table_list->schema_table && |
|
18034 |
table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE) |
|
18035 |
{ |
|
18036 |
if (!table_list->table_open_method) |
|
18037 |
extra.append(STRING_WITH_LEN("; Skip_open_table")); |
|
18038 |
else if (table_list->table_open_method == OPEN_FRM_ONLY) |
|
18039 |
extra.append(STRING_WITH_LEN("; Open_frm_only")); |
|
18040 |
else |
|
18041 |
extra.append(STRING_WITH_LEN("; Open_full_table")); |
|
18042 |
if (table_list->has_db_lookup_value && |
|
18043 |
table_list->has_table_lookup_value) |
|
18044 |
extra.append(STRING_WITH_LEN("; Scanned 0 databases")); |
|
18045 |
else if (table_list->has_db_lookup_value || |
|
18046 |
table_list->has_table_lookup_value) |
|
18047 |
extra.append(STRING_WITH_LEN("; Scanned 1 database")); |
|
18048 |
else |
|
18049 |
extra.append(STRING_WITH_LEN("; Scanned all databases")); |
|
18050 |
} |
|
18051 |
if (need_tmp_table) |
|
18052 |
{ |
|
18053 |
need_tmp_table=0; |
|
18054 |
extra.append(STRING_WITH_LEN("; Using temporary")); |
|
18055 |
} |
|
18056 |
if (need_order) |
|
18057 |
{ |
|
18058 |
need_order=0; |
|
18059 |
extra.append(STRING_WITH_LEN("; Using filesort")); |
|
18060 |
} |
|
18061 |
if (distinct & test_all_bits(used_tables,thd->used_tables)) |
|
18062 |
extra.append(STRING_WITH_LEN("; Distinct")); |
|
18063 |
||
18064 |
if (tab->insideout_match_tab) |
|
18065 |
{ |
|
18066 |
extra.append(STRING_WITH_LEN("; LooseScan")); |
|
18067 |
} |
|
18068 |
||
18069 |
if (tab->flush_weedout_table) |
|
18070 |
extra.append(STRING_WITH_LEN("; Start temporary")); |
|
18071 |
else if (tab->check_weed_out_table) |
|
18072 |
extra.append(STRING_WITH_LEN("; End temporary")); |
|
18073 |
else if (tab->do_firstmatch) |
|
18074 |
{ |
|
18075 |
extra.append(STRING_WITH_LEN("; FirstMatch(")); |
|
18076 |
TABLE *prev_table=tab->do_firstmatch->table; |
|
18077 |
if (prev_table->derived_select_number) |
|
18078 |
{ |
|
18079 |
char namebuf[NAME_LEN]; |
|
18080 |
/* Derived table name generation */ |
|
77.1.18
by Monty Taylor
Removed my_vsnprintf and my_snprintf. |
18081 |
int len= snprintf(namebuf, sizeof(namebuf)-1, |
18082 |
"<derived%u>", |
|
18083 |
prev_table->derived_select_number); |
|
1
by brian
clean slate |
18084 |
extra.append(namebuf, len); |
18085 |
} |
|
18086 |
else |
|
18087 |
extra.append(prev_table->pos_in_table_list->alias); |
|
18088 |
extra.append(STRING_WITH_LEN(")")); |
|
18089 |
} |
|
18090 |
||
18091 |
for (uint part= 0; part < tab->ref.key_parts; part++) |
|
18092 |
{ |
|
18093 |
if (tab->ref.cond_guards[part]) |
|
18094 |
{ |
|
18095 |
extra.append(STRING_WITH_LEN("; Full scan on NULL key")); |
|
18096 |
break; |
|
18097 |
} |
|
18098 |
} |
|
18099 |
||
18100 |
if (i > 0 && tab[-1].next_select == sub_select_cache) |
|
18101 |
extra.append(STRING_WITH_LEN("; Using join buffer")); |
|
18102 |
||
18103 |
/* Skip initial "; "*/ |
|
18104 |
const char *str= extra.ptr(); |
|
18105 |
uint32 len= extra.length(); |
|
18106 |
if (len) |
|
18107 |
{ |
|
18108 |
str += 2; |
|
18109 |
len -= 2; |
|
18110 |
} |
|
18111 |
item_list.push_back(new Item_string(str, len, cs)); |
|
18112 |
} |
|
18113 |
// For next iteration |
|
18114 |
used_tables|=table->map; |
|
18115 |
if (result->send_data(item_list)) |
|
18116 |
join->error= 1; |
|
18117 |
} |
|
18118 |
} |
|
18119 |
for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit(); |
|
18120 |
unit; |
|
18121 |
unit= unit->next_unit()) |
|
18122 |
{ |
|
18123 |
if (mysql_explain_union(thd, unit, result)) |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
18124 |
return; |
1
by brian
clean slate |
18125 |
} |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
18126 |
return; |
1
by brian
clean slate |
18127 |
} |
18128 |
||
18129 |
||
18130 |
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result) |
|
18131 |
{ |
|
18132 |
bool res= 0; |
|
18133 |
SELECT_LEX *first= unit->first_select(); |
|
18134 |
||
18135 |
for (SELECT_LEX *sl= first; |
|
18136 |
sl; |
|
18137 |
sl= sl->next_select()) |
|
18138 |
{ |
|
18139 |
// drop UNCACHEABLE_EXPLAIN, because it is for internal usage only |
|
18140 |
uint8 uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN); |
|
18141 |
sl->type= (((&thd->lex->select_lex)==sl)? |
|
18142 |
(sl->first_inner_unit() || sl->next_select() ? |
|
18143 |
"PRIMARY" : "SIMPLE"): |
|
18144 |
((sl == first)? |
|
18145 |
((sl->linkage == DERIVED_TABLE_TYPE) ? |
|
18146 |
"DERIVED": |
|
18147 |
((uncacheable & UNCACHEABLE_DEPENDENT) ? |
|
18148 |
"DEPENDENT SUBQUERY": |
|
18149 |
(uncacheable?"UNCACHEABLE SUBQUERY": |
|
18150 |
"SUBQUERY"))): |
|
18151 |
((uncacheable & UNCACHEABLE_DEPENDENT) ? |
|
18152 |
"DEPENDENT UNION": |
|
18153 |
uncacheable?"UNCACHEABLE UNION": |
|
18154 |
"UNION"))); |
|
18155 |
sl->options|= SELECT_DESCRIBE; |
|
18156 |
} |
|
18157 |
if (unit->is_union()) |
|
18158 |
{ |
|
18159 |
unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization |
|
18160 |
unit->fake_select_lex->type= "UNION RESULT"; |
|
18161 |
unit->fake_select_lex->options|= SELECT_DESCRIBE; |
|
18162 |
if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE))) |
|
18163 |
res= unit->exec(); |
|
18164 |
res|= unit->cleanup(); |
|
18165 |
} |
|
18166 |
else |
|
18167 |
{ |
|
18168 |
thd->lex->current_select= first; |
|
18169 |
unit->set_limit(unit->global_parameters); |
|
18170 |
res= mysql_select(thd, &first->ref_pointer_array, |
|
18171 |
(TABLE_LIST*) first->table_list.first, |
|
18172 |
first->with_wild, first->item_list, |
|
18173 |
first->where, |
|
18174 |
first->order_list.elements + |
|
18175 |
first->group_list.elements, |
|
18176 |
(ORDER*) first->order_list.first, |
|
18177 |
(ORDER*) first->group_list.first, |
|
18178 |
first->having, |
|
18179 |
(ORDER*) thd->lex->proc_list.first, |
|
18180 |
first->options | thd->options | SELECT_DESCRIBE, |
|
18181 |
result, unit, first); |
|
18182 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
18183 |
return(res || thd->is_error()); |
1
by brian
clean slate |
18184 |
} |
18185 |
||
18186 |
||
18187 |
static void print_table_array(THD *thd, String *str, TABLE_LIST **table, |
|
18188 |
TABLE_LIST **end) |
|
18189 |
{ |
|
18190 |
(*table)->print(thd, str, QT_ORDINARY); |
|
18191 |
||
18192 |
for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++) |
|
18193 |
{ |
|
18194 |
TABLE_LIST *curr= *tbl; |
|
18195 |
if (curr->outer_join) |
|
18196 |
{ |
|
18197 |
/* MySQL converts right to left joins */ |
|
18198 |
str->append(STRING_WITH_LEN(" left join ")); |
|
18199 |
} |
|
18200 |
else if (curr->straight) |
|
18201 |
str->append(STRING_WITH_LEN(" straight_join ")); |
|
18202 |
else if (curr->sj_inner_tables) |
|
18203 |
str->append(STRING_WITH_LEN(" semi join ")); |
|
18204 |
else |
|
18205 |
str->append(STRING_WITH_LEN(" join ")); |
|
18206 |
curr->print(thd, str, QT_ORDINARY); |
|
18207 |
if (curr->on_expr) |
|
18208 |
{ |
|
18209 |
str->append(STRING_WITH_LEN(" on(")); |
|
18210 |
curr->on_expr->print(str, QT_ORDINARY); |
|
18211 |
str->append(')'); |
|
18212 |
} |
|
18213 |
} |
|
18214 |
} |
|
18215 |
||
18216 |
||
18217 |
/** |
|
18218 |
Print joins from the FROM clause. |
|
18219 |
@param thd thread handler |
|
18220 |
@param str string where table should be printed |
|
18221 |
@param tables list of tables in join |
|
18222 |
@query_type type of the query is being generated |
|
18223 |
*/ |
|
18224 |
||
18225 |
static void print_join(THD *thd, |
|
18226 |
String *str, |
|
18227 |
List<TABLE_LIST> *tables, |
|
77.1.45
by Monty Taylor
Warning fixes. |
18228 |
enum_query_type query_type __attribute__((__unused__))) |
1
by brian
clean slate |
18229 |
{ |
18230 |
/* List is reversed => we should reverse it before using */ |
|
18231 |
List_iterator_fast<TABLE_LIST> ti(*tables); |
|
18232 |
TABLE_LIST **table= (TABLE_LIST **)thd->alloc(sizeof(TABLE_LIST*) * |
|
18233 |
tables->elements); |
|
18234 |
if (table == 0) |
|
18235 |
return; // out of memory |
|
18236 |
||
18237 |
for (TABLE_LIST **t= table + (tables->elements - 1); t >= table; t--) |
|
18238 |
*t= ti++; |
|
18239 |
||
18240 |
/* |
|
18241 |
If the first table is a semi-join nest, swap it with something that is |
|
18242 |
not a semi-join nest. |
|
18243 |
*/ |
|
18244 |
if ((*table)->sj_inner_tables) |
|
18245 |
{ |
|
18246 |
TABLE_LIST **end= table + tables->elements; |
|
18247 |
for (TABLE_LIST **t2= table; t2!=end; t2++) |
|
18248 |
{ |
|
18249 |
if (!(*t2)->sj_inner_tables) |
|
18250 |
{ |
|
18251 |
TABLE_LIST *tmp= *t2; |
|
18252 |
*t2= *table; |
|
18253 |
*table= tmp; |
|
18254 |
break; |
|
18255 |
} |
|
18256 |
} |
|
18257 |
} |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
18258 |
assert(tables->elements >= 1); |
1
by brian
clean slate |
18259 |
print_table_array(thd, str, table, table + tables->elements); |
18260 |
} |
|
18261 |
||
18262 |
||
18263 |
/** |
|
18264 |
@brief Print an index hint |
|
18265 |
||
18266 |
@details Prints out the USE|FORCE|IGNORE index hint. |
|
18267 |
||
18268 |
@param thd the current thread |
|
18269 |
@param[out] str appends the index hint here |
|
18270 |
@param hint what the hint is (as string : "USE INDEX"| |
|
18271 |
"FORCE INDEX"|"IGNORE INDEX") |
|
18272 |
@param hint_length the length of the string in 'hint' |
|
18273 |
@param indexes a list of index names for the hint |
|
18274 |
*/ |
|
18275 |
||
18276 |
void |
|
18277 |
Index_hint::print(THD *thd, String *str) |
|
18278 |
{ |
|
18279 |
switch (type) |
|
18280 |
{ |
|
18281 |
case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break; |
|
18282 |
case INDEX_HINT_USE: str->append(STRING_WITH_LEN("USE INDEX")); break; |
|
18283 |
case INDEX_HINT_FORCE: str->append(STRING_WITH_LEN("FORCE INDEX")); break; |
|
18284 |
} |
|
18285 |
str->append (STRING_WITH_LEN(" (")); |
|
18286 |
if (key_name.length) |
|
18287 |
{ |
|
18288 |
if (thd && !my_strnncoll(system_charset_info, |
|
18289 |
(const uchar *)key_name.str, key_name.length, |
|
18290 |
(const uchar *)primary_key_name, |
|
18291 |
strlen(primary_key_name))) |
|
18292 |
str->append(primary_key_name); |
|
18293 |
else |
|
18294 |
append_identifier(thd, str, key_name.str, key_name.length); |
|
18295 |
} |
|
18296 |
str->append(')'); |
|
18297 |
} |
|
18298 |
||
18299 |
||
18300 |
/** |
|
18301 |
Print table as it should be in join list. |
|
18302 |
||
18303 |
@param str string where table should be printed |
|
18304 |
*/ |
|
18305 |
||
18306 |
void TABLE_LIST::print(THD *thd, String *str, enum_query_type query_type) |
|
18307 |
{ |
|
18308 |
if (nested_join) |
|
18309 |
{ |
|
18310 |
str->append('('); |
|
18311 |
print_join(thd, str, &nested_join->join_list, query_type); |
|
18312 |
str->append(')'); |
|
18313 |
} |
|
18314 |
else |
|
18315 |
{ |
|
18316 |
const char *cmp_name; // Name to compare with alias |
|
18317 |
if (derived) |
|
18318 |
{ |
|
18319 |
// A derived table |
|
18320 |
str->append('('); |
|
18321 |
derived->print(str, query_type); |
|
18322 |
str->append(')'); |
|
18323 |
cmp_name= ""; // Force printing of alias |
|
18324 |
} |
|
18325 |
else |
|
18326 |
{ |
|
18327 |
// A normal table |
|
18328 |
{ |
|
18329 |
append_identifier(thd, str, db, db_length); |
|
18330 |
str->append('.'); |
|
18331 |
} |
|
18332 |
if (schema_table) |
|
18333 |
{ |
|
18334 |
append_identifier(thd, str, schema_table_name, |
|
18335 |
strlen(schema_table_name)); |
|
18336 |
cmp_name= schema_table_name; |
|
18337 |
} |
|
18338 |
else |
|
18339 |
{ |
|
18340 |
append_identifier(thd, str, table_name, table_name_length); |
|
18341 |
cmp_name= table_name; |
|
18342 |
} |
|
18343 |
} |
|
18344 |
if (my_strcasecmp(table_alias_charset, cmp_name, alias)) |
|
18345 |
{ |
|
18346 |
char t_alias_buff[MAX_ALIAS_NAME]; |
|
18347 |
const char *t_alias= alias; |
|
18348 |
||
18349 |
str->append(' '); |
|
18350 |
if (lower_case_table_names== 1) |
|
18351 |
{ |
|
18352 |
if (alias && alias[0]) |
|
18353 |
{ |
|
18354 |
strmov(t_alias_buff, alias); |
|
18355 |
my_casedn_str(files_charset_info, t_alias_buff); |
|
18356 |
t_alias= t_alias_buff; |
|
18357 |
} |
|
18358 |
} |
|
18359 |
||
18360 |
append_identifier(thd, str, t_alias, strlen(t_alias)); |
|
18361 |
} |
|
18362 |
||
18363 |
if (index_hints) |
|
18364 |
{ |
|
18365 |
List_iterator<Index_hint> it(*index_hints); |
|
18366 |
Index_hint *hint; |
|
18367 |
||
18368 |
while ((hint= it++)) |
|
18369 |
{ |
|
18370 |
str->append (STRING_WITH_LEN(" ")); |
|
18371 |
hint->print (thd, str); |
|
18372 |
} |
|
18373 |
} |
|
18374 |
} |
|
18375 |
} |
|
18376 |
||
18377 |
||
18378 |
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type) |
|
18379 |
{ |
|
18380 |
/* QQ: thd may not be set for sub queries, but this should be fixed */ |
|
18381 |
if (!thd) |
|
18382 |
thd= current_thd; |
|
18383 |
||
18384 |
str->append(STRING_WITH_LEN("select ")); |
|
18385 |
||
18386 |
/* First add options */ |
|
18387 |
if (options & SELECT_STRAIGHT_JOIN) |
|
18388 |
str->append(STRING_WITH_LEN("straight_join ")); |
|
18389 |
if ((thd->lex->lock_option == TL_READ_HIGH_PRIORITY) && |
|
18390 |
(this == &thd->lex->select_lex)) |
|
18391 |
str->append(STRING_WITH_LEN("high_priority ")); |
|
18392 |
if (options & SELECT_DISTINCT) |
|
18393 |
str->append(STRING_WITH_LEN("distinct ")); |
|
18394 |
if (options & SELECT_SMALL_RESULT) |
|
18395 |
str->append(STRING_WITH_LEN("sql_small_result ")); |
|
18396 |
if (options & SELECT_BIG_RESULT) |
|
18397 |
str->append(STRING_WITH_LEN("sql_big_result ")); |
|
18398 |
if (options & OPTION_BUFFER_RESULT) |
|
18399 |
str->append(STRING_WITH_LEN("sql_buffer_result ")); |
|
18400 |
if (options & OPTION_FOUND_ROWS) |
|
18401 |
str->append(STRING_WITH_LEN("sql_calc_found_rows ")); |
|
18402 |
||
18403 |
//Item List |
|
18404 |
bool first= 1; |
|
18405 |
List_iterator_fast<Item> it(item_list); |
|
18406 |
Item *item; |
|
18407 |
while ((item= it++)) |
|
18408 |
{ |
|
18409 |
if (first) |
|
18410 |
first= 0; |
|
18411 |
else |
|
18412 |
str->append(','); |
|
18413 |
item->print_item_w_name(str, query_type); |
|
18414 |
} |
|
18415 |
||
18416 |
/* |
|
18417 |
from clause |
|
18418 |
TODO: support USING/FORCE/IGNORE index |
|
18419 |
*/ |
|
18420 |
if (table_list.elements) |
|
18421 |
{ |
|
18422 |
str->append(STRING_WITH_LEN(" from ")); |
|
18423 |
/* go through join tree */ |
|
18424 |
print_join(thd, str, &top_join_list, query_type); |
|
18425 |
} |
|
18426 |
else if (where) |
|
18427 |
{ |
|
18428 |
/* |
|
18429 |
"SELECT 1 FROM DUAL WHERE 2" should not be printed as |
|
18430 |
"SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not. |
|
18431 |
*/ |
|
18432 |
str->append(STRING_WITH_LEN(" from DUAL ")); |
|
18433 |
} |
|
18434 |
||
18435 |
// Where |
|
18436 |
Item *cur_where= where; |
|
18437 |
if (join) |
|
18438 |
cur_where= join->conds; |
|
18439 |
if (cur_where || cond_value != Item::COND_UNDEF) |
|
18440 |
{ |
|
18441 |
str->append(STRING_WITH_LEN(" where ")); |
|
18442 |
if (cur_where) |
|
18443 |
cur_where->print(str, query_type); |
|
18444 |
else |
|
18445 |
str->append(cond_value != Item::COND_FALSE ? "1" : "0"); |
|
18446 |
} |
|
18447 |
||
18448 |
// group by & olap |
|
18449 |
if (group_list.elements) |
|
18450 |
{ |
|
18451 |
str->append(STRING_WITH_LEN(" group by ")); |
|
18452 |
print_order(str, (ORDER *) group_list.first, query_type); |
|
18453 |
switch (olap) |
|
18454 |
{ |
|
18455 |
case CUBE_TYPE: |
|
18456 |
str->append(STRING_WITH_LEN(" with cube")); |
|
18457 |
break; |
|
18458 |
case ROLLUP_TYPE: |
|
18459 |
str->append(STRING_WITH_LEN(" with rollup")); |
|
18460 |
break; |
|
18461 |
default: |
|
18462 |
; //satisfy compiler |
|
18463 |
} |
|
18464 |
} |
|
18465 |
||
18466 |
// having |
|
18467 |
Item *cur_having= having; |
|
18468 |
if (join) |
|
18469 |
cur_having= join->having; |
|
18470 |
||
18471 |
if (cur_having || having_value != Item::COND_UNDEF) |
|
18472 |
{ |
|
18473 |
str->append(STRING_WITH_LEN(" having ")); |
|
18474 |
if (cur_having) |
|
18475 |
cur_having->print(str, query_type); |
|
18476 |
else |
|
18477 |
str->append(having_value != Item::COND_FALSE ? "1" : "0"); |
|
18478 |
} |
|
18479 |
||
18480 |
if (order_list.elements) |
|
18481 |
{ |
|
18482 |
str->append(STRING_WITH_LEN(" order by ")); |
|
18483 |
print_order(str, (ORDER *) order_list.first, query_type); |
|
18484 |
} |
|
18485 |
||
18486 |
// limit |
|
18487 |
print_limit(thd, str, query_type); |
|
18488 |
||
18489 |
// PROCEDURE unsupported here |
|
18490 |
} |
|
18491 |
||
18492 |
||
18493 |
/** |
|
18494 |
change select_result object of JOIN. |
|
18495 |
||
18496 |
@param res new select_result object |
|
18497 |
||
18498 |
@retval |
|
55
by brian
Update for using real bool types. |
18499 |
false OK |
1
by brian
clean slate |
18500 |
@retval |
55
by brian
Update for using real bool types. |
18501 |
true error |
1
by brian
clean slate |
18502 |
*/ |
18503 |
||
18504 |
bool JOIN::change_result(select_result *res) |
|
18505 |
{ |
|
18506 |
result= res; |
|
18507 |
if (result->prepare(fields_list, select_lex->master_unit()) || |
|
18508 |
result->prepare2()) |
|
18509 |
{ |
|
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
18510 |
return(true); |
1
by brian
clean slate |
18511 |
} |
51.1.66
by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE |
18512 |
return(false); |
1
by brian
clean slate |
18513 |
} |
18514 |
||
18515 |
/** |
|
18516 |
@} (end of group Query_Optimizer) |
|
18517 |
*/ |