1
by brian
clean slate |
1 |
/* Copyright (C) 2000 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 |
subselect Item
|
|
21 |
||
22 |
@todo
|
|
23 |
- add function from mysql_select that use JOIN* as parameter to JOIN
|
|
24 |
methods (sql_select.h/sql_select.cc)
|
|
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 |
inline Item * and_items(Item* cond, Item *item) |
|
35 |
{
|
|
36 |
return (cond? (new Item_cond_and(cond, item)) : item); |
|
37 |
}
|
|
38 |
||
39 |
Item_subselect::Item_subselect(): |
|
40 |
Item_result_field(), value_assigned(0), thd(0), substitution(0), |
|
41 |
engine(0), old_engine(0), used_tables_cache(0), have_to_be_excluded(0), |
|
42 |
const_item_cache(1), engine_changed(0), changed(0), |
|
55
by brian
Update for using real bool types. |
43 |
is_correlated(false) |
1
by brian
clean slate |
44 |
{
|
45 |
with_subselect= 1; |
|
46 |
reset(); |
|
47 |
/*
|
|
48 |
Item value is NULL if select_result_interceptor didn't change this value
|
|
49 |
(i.e. some rows will be found returned)
|
|
50 |
*/
|
|
51 |
null_value= 1; |
|
52 |
}
|
|
53 |
||
54 |
||
55 |
void Item_subselect::init(st_select_lex *select_lex, |
|
56 |
select_result_interceptor *result) |
|
57 |
{
|
|
58 |
/*
|
|
59 |
Please see Item_singlerow_subselect::invalidate_and_restore_select_lex(),
|
|
60 |
which depends on alterations to the parse tree implemented here.
|
|
61 |
*/
|
|
62 |
||
63 |
unit= select_lex->master_unit(); |
|
64 |
||
65 |
if (unit->item) |
|
66 |
{
|
|
67 |
/*
|
|
68 |
Item can be changed in JOIN::prepare while engine in JOIN::optimize
|
|
69 |
=> we do not copy old_engine here
|
|
70 |
*/
|
|
71 |
engine= unit->item->engine; |
|
72 |
parsing_place= unit->item->parsing_place; |
|
73 |
unit->item->engine= 0; |
|
74 |
unit->item= this; |
|
75 |
engine->change_result(this, result); |
|
76 |
}
|
|
77 |
else
|
|
78 |
{
|
|
79 |
SELECT_LEX *outer_select= unit->outer_select(); |
|
80 |
/*
|
|
81 |
do not take into account expression inside aggregate functions because
|
|
82 |
they can access original table fields
|
|
83 |
*/
|
|
84 |
parsing_place= (outer_select->in_sum_expr ? |
|
85 |
NO_MATTER : |
|
86 |
outer_select->parsing_place); |
|
87 |
if (unit->is_union()) |
|
88 |
engine= new subselect_union_engine(unit, result, this); |
|
89 |
else
|
|
90 |
engine= new subselect_single_select_engine(select_lex, result, this); |
|
91 |
}
|
|
92 |
{
|
|
93 |
SELECT_LEX *upper= unit->outer_select(); |
|
94 |
if (upper->parsing_place == IN_HAVING) |
|
95 |
upper->subquery_in_having= 1; |
|
96 |
}
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
97 |
return; |
1
by brian
clean slate |
98 |
}
|
99 |
||
100 |
st_select_lex * |
|
101 |
Item_subselect::get_select_lex() |
|
102 |
{
|
|
103 |
return unit->first_select(); |
|
104 |
}
|
|
105 |
||
106 |
void Item_subselect::cleanup() |
|
107 |
{
|
|
108 |
Item_result_field::cleanup(); |
|
109 |
if (old_engine) |
|
110 |
{
|
|
111 |
if (engine) |
|
112 |
engine->cleanup(); |
|
113 |
engine= old_engine; |
|
114 |
old_engine= 0; |
|
115 |
}
|
|
116 |
if (engine) |
|
117 |
engine->cleanup(); |
|
118 |
reset(); |
|
119 |
value_assigned= 0; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
120 |
return; |
1
by brian
clean slate |
121 |
}
|
122 |
||
123 |
void Item_singlerow_subselect::cleanup() |
|
124 |
{
|
|
125 |
value= 0; row= 0; |
|
126 |
Item_subselect::cleanup(); |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
127 |
return; |
1
by brian
clean slate |
128 |
}
|
129 |
||
130 |
||
131 |
void Item_in_subselect::cleanup() |
|
132 |
{
|
|
133 |
if (left_expr_cache) |
|
134 |
{
|
|
135 |
left_expr_cache->delete_elements(); |
|
136 |
delete left_expr_cache; |
|
137 |
left_expr_cache= NULL; |
|
138 |
}
|
|
55
by brian
Update for using real bool types. |
139 |
first_execution= true; |
1
by brian
clean slate |
140 |
Item_subselect::cleanup(); |
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
141 |
return; |
1
by brian
clean slate |
142 |
}
|
143 |
||
144 |
Item_subselect::~Item_subselect() |
|
145 |
{
|
|
146 |
delete engine; |
|
147 |
}
|
|
148 |
||
149 |
Item_subselect::trans_res |
|
77.1.15
by Monty Taylor
Bunch of warning cleanups. |
150 |
Item_subselect::select_transformer(JOIN *join __attribute__((__unused__))) |
1
by brian
clean slate |
151 |
{
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
152 |
return(RES_OK); |
1
by brian
clean slate |
153 |
}
|
154 |
||
155 |
||
156 |
bool Item_subselect::fix_fields(THD *thd_param, Item **ref) |
|
157 |
{
|
|
158 |
char const *save_where= thd_param->where; |
|
159 |
uint8 uncacheable; |
|
160 |
bool res; |
|
161 |
||
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
162 |
assert(fixed == 0); |
1
by brian
clean slate |
163 |
engine->set_thd((thd= thd_param)); |
164 |
||
165 |
if (check_stack_overrun(thd, STACK_MIN_SIZE, (uchar*)&res)) |
|
55
by brian
Update for using real bool types. |
166 |
return true; |
1
by brian
clean slate |
167 |
|
168 |
res= engine->prepare(); |
|
169 |
||
170 |
// all transformation is done (used by prepared statements)
|
|
171 |
changed= 1; |
|
172 |
||
173 |
if (!res) |
|
174 |
{
|
|
175 |
/*
|
|
176 |
Substitute the current item with an Item_in_optimizer that was
|
|
177 |
created by Item_in_subselect::select_in_like_transformer and
|
|
178 |
call fix_fields for the substituted item which in turn calls
|
|
179 |
engine->prepare for the subquery predicate.
|
|
180 |
*/
|
|
181 |
if (substitution) |
|
182 |
{
|
|
183 |
int ret= 0; |
|
184 |
||
185 |
// did we changed top item of WHERE condition
|
|
186 |
if (unit->outer_select()->where == (*ref)) |
|
187 |
unit->outer_select()->where= substitution; // correct WHERE for PS |
|
188 |
else if (unit->outer_select()->having == (*ref)) |
|
189 |
unit->outer_select()->having= substitution; // correct HAVING for PS |
|
190 |
||
191 |
(*ref)= substitution; |
|
192 |
substitution->name= name; |
|
193 |
if (have_to_be_excluded) |
|
194 |
engine->exclude(); |
|
195 |
substitution= 0; |
|
196 |
thd->where= "checking transformed subquery"; |
|
197 |
if (!(*ref)->fixed) |
|
198 |
ret= (*ref)->fix_fields(thd, ref); |
|
199 |
thd->where= save_where; |
|
200 |
return ret; |
|
201 |
}
|
|
202 |
// Is it one field subselect?
|
|
203 |
if (engine->cols() > max_columns) |
|
204 |
{
|
|
205 |
my_error(ER_OPERAND_COLUMNS, MYF(0), 1); |
|
55
by brian
Update for using real bool types. |
206 |
return true; |
1
by brian
clean slate |
207 |
}
|
208 |
fix_length_and_dec(); |
|
209 |
}
|
|
210 |
else
|
|
211 |
goto err; |
|
212 |
||
213 |
if ((uncacheable= engine->uncacheable())) |
|
214 |
{
|
|
215 |
const_item_cache= 0; |
|
216 |
if (uncacheable & UNCACHEABLE_RAND) |
|
217 |
used_tables_cache|= RAND_TABLE_BIT; |
|
218 |
}
|
|
219 |
fixed= 1; |
|
220 |
||
221 |
err: |
|
222 |
thd->where= save_where; |
|
223 |
return res; |
|
224 |
}
|
|
225 |
||
226 |
||
227 |
bool Item_subselect::walk(Item_processor processor, bool walk_subquery, |
|
228 |
uchar *argument) |
|
229 |
{
|
|
230 |
||
231 |
if (walk_subquery) |
|
232 |
{
|
|
233 |
for (SELECT_LEX *lex= unit->first_select(); lex; lex= lex->next_select()) |
|
234 |
{
|
|
235 |
List_iterator<Item> li(lex->item_list); |
|
236 |
Item *item; |
|
237 |
ORDER *order; |
|
238 |
||
239 |
if (lex->where && (lex->where)->walk(processor, walk_subquery, argument)) |
|
240 |
return 1; |
|
241 |
if (lex->having && (lex->having)->walk(processor, walk_subquery, |
|
242 |
argument)) |
|
243 |
return 1; |
|
244 |
||
245 |
while ((item=li++)) |
|
246 |
{
|
|
247 |
if (item->walk(processor, walk_subquery, argument)) |
|
248 |
return 1; |
|
249 |
}
|
|
250 |
for (order= (ORDER*) lex->order_list.first ; order; order= order->next) |
|
251 |
{
|
|
252 |
if ((*order->item)->walk(processor, walk_subquery, argument)) |
|
253 |
return 1; |
|
254 |
}
|
|
255 |
for (order= (ORDER*) lex->group_list.first ; order; order= order->next) |
|
256 |
{
|
|
257 |
if ((*order->item)->walk(processor, walk_subquery, argument)) |
|
258 |
return 1; |
|
259 |
}
|
|
260 |
}
|
|
261 |
}
|
|
262 |
return (this->*processor)(argument); |
|
263 |
}
|
|
264 |
||
265 |
||
266 |
bool Item_subselect::exec() |
|
267 |
{
|
|
268 |
int res; |
|
269 |
||
270 |
if (thd->is_error()) |
|
271 |
/* Do not execute subselect in case of a fatal error */
|
|
272 |
return 1; |
|
273 |
||
274 |
res= engine->exec(); |
|
275 |
||
276 |
if (engine_changed) |
|
277 |
{
|
|
278 |
engine_changed= 0; |
|
279 |
return exec(); |
|
280 |
}
|
|
281 |
return (res); |
|
282 |
}
|
|
283 |
||
284 |
||
285 |
/*
|
|
286 |
Compute the IN predicate if the left operand's cache changed.
|
|
287 |
*/
|
|
288 |
||
289 |
bool Item_in_subselect::exec() |
|
290 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
291 |
assert(exec_method != MATERIALIZATION || |
1
by brian
clean slate |
292 |
(exec_method == MATERIALIZATION && |
293 |
engine->engine_type() == subselect_engine::HASH_SJ_ENGINE)); |
|
294 |
/*
|
|
295 |
Initialize the cache of the left predicate operand. This has to be done as
|
|
296 |
late as now, because Cached_item directly contains a resolved field (not
|
|
297 |
an item, and in some cases (when temp tables are created), these fields
|
|
298 |
end up pointing to the wrong field. One solution is to change Cached_item
|
|
299 |
to not resolve its field upon creation, but to resolve it dynamically
|
|
300 |
from a given Item_ref object.
|
|
301 |
TODO: the cache should be applied conditionally based on:
|
|
302 |
- rules - e.g. only if the left operand is known to be ordered, and/or
|
|
303 |
- on a cost-based basis, that takes into account the cost of a cache
|
|
304 |
lookup, the cache hit rate, and the savings per cache hit.
|
|
305 |
*/
|
|
306 |
if (!left_expr_cache && exec_method == MATERIALIZATION) |
|
307 |
init_left_expr_cache(); |
|
308 |
||
309 |
/* If the new left operand is already in the cache, reuse the old result. */
|
|
310 |
if (left_expr_cache && test_if_item_cache_changed(*left_expr_cache) < 0) |
|
311 |
{
|
|
312 |
/* Always compute IN for the first row as the cache is not valid for it. */
|
|
313 |
if (!first_execution) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
314 |
return(false); |
55
by brian
Update for using real bool types. |
315 |
first_execution= false; |
1
by brian
clean slate |
316 |
}
|
317 |
||
318 |
/*
|
|
319 |
The exec() method below updates item::value, and item::null_value, thus if
|
|
320 |
we don't call it, the next call to item::val_int() will return whatever
|
|
321 |
result was computed by its previous call.
|
|
322 |
*/
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
323 |
return(Item_subselect::exec()); |
1
by brian
clean slate |
324 |
}
|
325 |
||
326 |
||
327 |
Item::Type Item_subselect::type() const |
|
328 |
{
|
|
329 |
return SUBSELECT_ITEM; |
|
330 |
}
|
|
331 |
||
332 |
||
333 |
void Item_subselect::fix_length_and_dec() |
|
334 |
{
|
|
335 |
engine->fix_length_and_dec(0); |
|
336 |
}
|
|
337 |
||
338 |
||
339 |
table_map Item_subselect::used_tables() const |
|
340 |
{
|
|
341 |
return (table_map) (engine->uncacheable() ? used_tables_cache : 0L); |
|
342 |
}
|
|
343 |
||
344 |
||
345 |
bool Item_subselect::const_item() const |
|
346 |
{
|
|
347 |
return const_item_cache; |
|
348 |
}
|
|
349 |
||
350 |
Item *Item_subselect::get_tmp_table_item(THD *thd_arg) |
|
351 |
{
|
|
352 |
if (!with_sum_func && !const_item()) |
|
353 |
return new Item_field(result_field); |
|
354 |
return copy_or_same(thd_arg); |
|
355 |
}
|
|
356 |
||
357 |
void Item_subselect::update_used_tables() |
|
358 |
{
|
|
359 |
if (!engine->uncacheable()) |
|
360 |
{
|
|
361 |
// did all used tables become static?
|
|
362 |
if (!(used_tables_cache & ~engine->upper_select_const_tables())) |
|
363 |
const_item_cache= 1; |
|
364 |
}
|
|
365 |
}
|
|
366 |
||
367 |
||
368 |
void Item_subselect::print(String *str, enum_query_type query_type) |
|
369 |
{
|
|
370 |
str->append('('); |
|
371 |
engine->print(str, query_type); |
|
372 |
str->append(')'); |
|
373 |
}
|
|
374 |
||
375 |
||
376 |
Item_singlerow_subselect::Item_singlerow_subselect(st_select_lex *select_lex) |
|
377 |
:Item_subselect(), value(0) |
|
378 |
{
|
|
379 |
init(select_lex, new select_singlerow_subselect(this)); |
|
380 |
maybe_null= 1; |
|
381 |
max_columns= UINT_MAX; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
382 |
return; |
1
by brian
clean slate |
383 |
}
|
384 |
||
385 |
st_select_lex * |
|
386 |
Item_singlerow_subselect::invalidate_and_restore_select_lex() |
|
387 |
{
|
|
388 |
st_select_lex *result= get_select_lex(); |
|
389 |
||
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
390 |
assert(result); |
1
by brian
clean slate |
391 |
|
392 |
/*
|
|
393 |
This code restore the parse tree in it's state before the execution of
|
|
394 |
Item_singlerow_subselect::Item_singlerow_subselect(),
|
|
395 |
and in particular decouples this object from the SELECT_LEX,
|
|
396 |
so that the SELECT_LEX can be used with a different flavor
|
|
397 |
or Item_subselect instead, as part of query rewriting.
|
|
398 |
*/
|
|
399 |
unit->item= NULL; |
|
400 |
||
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
401 |
return(result); |
1
by brian
clean slate |
402 |
}
|
403 |
||
404 |
Item_maxmin_subselect::Item_maxmin_subselect(THD *thd_param, |
|
405 |
Item_subselect *parent, |
|
406 |
st_select_lex *select_lex, |
|
407 |
bool max_arg) |
|
55
by brian
Update for using real bool types. |
408 |
:Item_singlerow_subselect(), was_values(true) |
1
by brian
clean slate |
409 |
{
|
410 |
max= max_arg; |
|
411 |
init(select_lex, new select_max_min_finder_subselect(this, max_arg)); |
|
412 |
max_columns= 1; |
|
413 |
maybe_null= 1; |
|
414 |
max_columns= 1; |
|
415 |
||
416 |
/*
|
|
417 |
Following information was collected during performing fix_fields()
|
|
418 |
of Items belonged to subquery, which will be not repeated
|
|
419 |
*/
|
|
420 |
used_tables_cache= parent->get_used_tables_cache(); |
|
421 |
const_item_cache= parent->get_const_item_cache(); |
|
422 |
||
423 |
/*
|
|
424 |
this subquery always creates during preparation, so we can assign
|
|
425 |
thd here
|
|
426 |
*/
|
|
427 |
thd= thd_param; |
|
428 |
||
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
429 |
return; |
1
by brian
clean slate |
430 |
}
|
431 |
||
432 |
void Item_maxmin_subselect::cleanup() |
|
433 |
{
|
|
434 |
Item_singlerow_subselect::cleanup(); |
|
435 |
||
436 |
/*
|
|
55
by brian
Update for using real bool types. |
437 |
By default it is true to avoid true reporting by
|
1
by brian
clean slate |
438 |
Item_func_not_all/Item_func_nop_all if this item was never called.
|
439 |
||
55
by brian
Update for using real bool types. |
440 |
Engine exec() set it to false by reset_value_registration() call.
|
441 |
select_max_min_finder_subselect::send_data() set it back to true if some
|
|
1
by brian
clean slate |
442 |
value will be found.
|
443 |
*/
|
|
55
by brian
Update for using real bool types. |
444 |
was_values= true; |
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
445 |
return; |
1
by brian
clean slate |
446 |
}
|
447 |
||
448 |
||
449 |
void Item_maxmin_subselect::print(String *str, enum_query_type query_type) |
|
450 |
{
|
|
451 |
str->append(max?"<max>":"<min>", 5); |
|
452 |
Item_singlerow_subselect::print(str, query_type); |
|
453 |
}
|
|
454 |
||
455 |
||
456 |
void Item_singlerow_subselect::reset() |
|
457 |
{
|
|
458 |
null_value= 1; |
|
459 |
if (value) |
|
460 |
value->null_value= 1; |
|
461 |
}
|
|
462 |
||
463 |
||
464 |
/**
|
|
465 |
@todo
|
|
466 |
- We cant change name of Item_field or Item_ref, because it will
|
|
467 |
prevent it's correct resolving, but we should save name of
|
|
468 |
removed item => we do not make optimization if top item of
|
|
469 |
list is field or reference.
|
|
470 |
- switch off this optimization for prepare statement,
|
|
471 |
because we do not rollback this changes.
|
|
472 |
Make rollback for it, or special name resolving mode in 5.0.
|
|
473 |
*/
|
|
474 |
Item_subselect::trans_res |
|
475 |
Item_singlerow_subselect::select_transformer(JOIN *join) |
|
476 |
{
|
|
477 |
if (changed) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
478 |
return(RES_OK); |
1
by brian
clean slate |
479 |
|
480 |
SELECT_LEX *select_lex= join->select_lex; |
|
481 |
||
482 |
if (!select_lex->master_unit()->is_union() && |
|
483 |
!select_lex->table_list.elements && |
|
484 |
select_lex->item_list.elements == 1 && |
|
485 |
!select_lex->item_list.head()->with_sum_func && |
|
486 |
/*
|
|
487 |
We cant change name of Item_field or Item_ref, because it will
|
|
488 |
prevent it's correct resolving, but we should save name of
|
|
489 |
removed item => we do not make optimization if top item of
|
|
490 |
list is field or reference.
|
|
491 |
TODO: solve above problem
|
|
492 |
*/
|
|
493 |
!(select_lex->item_list.head()->type() == FIELD_ITEM || |
|
494 |
select_lex->item_list.head()->type() == REF_ITEM) && |
|
495 |
!join->conds && !join->having |
|
496 |
)
|
|
497 |
{
|
|
498 |
||
499 |
have_to_be_excluded= 1; |
|
500 |
if (thd->lex->describe) |
|
501 |
{
|
|
502 |
char warn_buff[MYSQL_ERRMSG_SIZE]; |
|
503 |
sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number); |
|
504 |
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE, |
|
505 |
ER_SELECT_REDUCED, warn_buff); |
|
506 |
}
|
|
507 |
substitution= select_lex->item_list.head(); |
|
508 |
/*
|
|
509 |
as far as we moved content to upper level, field which depend of
|
|
510 |
'upper' select is not really dependent => we remove this dependence
|
|
511 |
*/
|
|
512 |
substitution->walk(&Item::remove_dependence_processor, 0, |
|
513 |
(uchar *) select_lex->outer_select()); |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
514 |
return(RES_REDUCE); |
1
by brian
clean slate |
515 |
}
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
516 |
return(RES_OK); |
1
by brian
clean slate |
517 |
}
|
518 |
||
519 |
||
520 |
void Item_singlerow_subselect::store(uint i, Item *item) |
|
521 |
{
|
|
522 |
row[i]->store(item); |
|
523 |
}
|
|
524 |
||
525 |
enum Item_result Item_singlerow_subselect::result_type() const |
|
526 |
{
|
|
527 |
return engine->type(); |
|
528 |
}
|
|
529 |
||
530 |
/*
|
|
531 |
Don't rely on the result type to calculate field type.
|
|
532 |
Ask the engine instead.
|
|
533 |
*/
|
|
534 |
enum_field_types Item_singlerow_subselect::field_type() const |
|
535 |
{
|
|
536 |
return engine->field_type(); |
|
537 |
}
|
|
538 |
||
539 |
void Item_singlerow_subselect::fix_length_and_dec() |
|
540 |
{
|
|
541 |
if ((max_columns= engine->cols()) == 1) |
|
542 |
{
|
|
543 |
engine->fix_length_and_dec(row= &value); |
|
544 |
}
|
|
545 |
else
|
|
546 |
{
|
|
547 |
if (!(row= (Item_cache**) sql_alloc(sizeof(Item_cache*)*max_columns))) |
|
548 |
return; |
|
549 |
engine->fix_length_and_dec(row); |
|
550 |
value= *row; |
|
551 |
}
|
|
552 |
unsigned_flag= value->unsigned_flag; |
|
553 |
/*
|
|
554 |
If there are not tables in subquery then ability to have NULL value
|
|
555 |
depends on SELECT list (if single row subquery have tables then it
|
|
556 |
always can be NULL if there are not records fetched).
|
|
557 |
*/
|
|
558 |
if (engine->no_tables()) |
|
559 |
maybe_null= engine->may_be_null(); |
|
560 |
}
|
|
561 |
||
562 |
uint Item_singlerow_subselect::cols() |
|
563 |
{
|
|
564 |
return engine->cols(); |
|
565 |
}
|
|
566 |
||
567 |
bool Item_singlerow_subselect::check_cols(uint c) |
|
568 |
{
|
|
569 |
if (c != engine->cols()) |
|
570 |
{
|
|
571 |
my_error(ER_OPERAND_COLUMNS, MYF(0), c); |
|
572 |
return 1; |
|
573 |
}
|
|
574 |
return 0; |
|
575 |
}
|
|
576 |
||
577 |
bool Item_singlerow_subselect::null_inside() |
|
578 |
{
|
|
579 |
for (uint i= 0; i < max_columns ; i++) |
|
580 |
{
|
|
581 |
if (row[i]->null_value) |
|
582 |
return 1; |
|
583 |
}
|
|
584 |
return 0; |
|
585 |
}
|
|
586 |
||
587 |
void Item_singlerow_subselect::bring_value() |
|
588 |
{
|
|
589 |
exec(); |
|
590 |
}
|
|
591 |
||
592 |
double Item_singlerow_subselect::val_real() |
|
593 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
594 |
assert(fixed == 1); |
1
by brian
clean slate |
595 |
if (!exec() && !value->null_value) |
596 |
{
|
|
597 |
null_value= 0; |
|
598 |
return value->val_real(); |
|
599 |
}
|
|
600 |
else
|
|
601 |
{
|
|
602 |
reset(); |
|
603 |
return 0; |
|
604 |
}
|
|
605 |
}
|
|
606 |
||
152
by Brian Aker
longlong replacement |
607 |
int64_t Item_singlerow_subselect::val_int() |
1
by brian
clean slate |
608 |
{
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
609 |
assert(fixed == 1); |
1
by brian
clean slate |
610 |
if (!exec() && !value->null_value) |
611 |
{
|
|
612 |
null_value= 0; |
|
613 |
return value->val_int(); |
|
614 |
}
|
|
615 |
else
|
|
616 |
{
|
|
617 |
reset(); |
|
618 |
return 0; |
|
619 |
}
|
|
620 |
}
|
|
621 |
||
622 |
String *Item_singlerow_subselect::val_str(String *str) |
|
623 |
{
|
|
624 |
if (!exec() && !value->null_value) |
|
625 |
{
|
|
626 |
null_value= 0; |
|
627 |
return value->val_str(str); |
|
628 |
}
|
|
629 |
else
|
|
630 |
{
|
|
631 |
reset(); |
|
632 |
return 0; |
|
633 |
}
|
|
634 |
}
|
|
635 |
||
636 |
||
637 |
my_decimal *Item_singlerow_subselect::val_decimal(my_decimal *decimal_value) |
|
638 |
{
|
|
639 |
if (!exec() && !value->null_value) |
|
640 |
{
|
|
641 |
null_value= 0; |
|
642 |
return value->val_decimal(decimal_value); |
|
643 |
}
|
|
644 |
else
|
|
645 |
{
|
|
646 |
reset(); |
|
647 |
return 0; |
|
648 |
}
|
|
649 |
}
|
|
650 |
||
651 |
||
652 |
bool Item_singlerow_subselect::val_bool() |
|
653 |
{
|
|
654 |
if (!exec() && !value->null_value) |
|
655 |
{
|
|
656 |
null_value= 0; |
|
657 |
return value->val_bool(); |
|
658 |
}
|
|
659 |
else
|
|
660 |
{
|
|
661 |
reset(); |
|
662 |
return 0; |
|
663 |
}
|
|
664 |
}
|
|
665 |
||
666 |
||
667 |
Item_exists_subselect::Item_exists_subselect(st_select_lex *select_lex): |
|
668 |
Item_subselect() |
|
669 |
{
|
|
670 |
bool val_bool(); |
|
671 |
init(select_lex, new select_exists_subselect(this)); |
|
672 |
max_columns= UINT_MAX; |
|
673 |
null_value= 0; //can't be NULL |
|
674 |
maybe_null= 0; //can't be NULL |
|
675 |
value= 0; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
676 |
return; |
1
by brian
clean slate |
677 |
}
|
678 |
||
679 |
||
680 |
void Item_exists_subselect::print(String *str, enum_query_type query_type) |
|
681 |
{
|
|
682 |
str->append(STRING_WITH_LEN("exists")); |
|
683 |
Item_subselect::print(str, query_type); |
|
684 |
}
|
|
685 |
||
686 |
||
687 |
bool Item_in_subselect::test_limit(st_select_lex_unit *unit_arg) |
|
688 |
{
|
|
689 |
if (unit_arg->fake_select_lex && |
|
690 |
unit_arg->fake_select_lex->test_limit()) |
|
691 |
return(1); |
|
692 |
||
693 |
SELECT_LEX *sl= unit_arg->first_select(); |
|
694 |
for (; sl; sl= sl->next_select()) |
|
695 |
{
|
|
696 |
if (sl->test_limit()) |
|
697 |
return(1); |
|
698 |
}
|
|
699 |
return(0); |
|
700 |
}
|
|
701 |
||
702 |
Item_in_subselect::Item_in_subselect(Item * left_exp, |
|
703 |
st_select_lex *select_lex): |
|
55
by brian
Update for using real bool types. |
704 |
Item_exists_subselect(), left_expr_cache(0), first_execution(true), |
1
by brian
clean slate |
705 |
optimizer(0), pushed_cond_guards(NULL), exec_method(NOT_TRANSFORMED), |
706 |
upper_item(0) |
|
707 |
{
|
|
708 |
left_expr= left_exp; |
|
709 |
init(select_lex, new select_exists_subselect(this)); |
|
710 |
max_columns= UINT_MAX; |
|
711 |
maybe_null= 1; |
|
712 |
abort_on_null= 0; |
|
713 |
reset(); |
|
714 |
//if test_limit will fail then error will be reported to client
|
|
715 |
test_limit(select_lex->master_unit()); |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
716 |
return; |
1
by brian
clean slate |
717 |
}
|
718 |
||
719 |
Item_allany_subselect::Item_allany_subselect(Item * left_exp, |
|
720 |
chooser_compare_func_creator fc, |
|
721 |
st_select_lex *select_lex, |
|
722 |
bool all_arg) |
|
723 |
:Item_in_subselect(), func_creator(fc), all(all_arg) |
|
724 |
{
|
|
725 |
left_expr= left_exp; |
|
726 |
func= func_creator(all_arg); |
|
727 |
init(select_lex, new select_exists_subselect(this)); |
|
728 |
max_columns= 1; |
|
729 |
abort_on_null= 0; |
|
730 |
reset(); |
|
731 |
//if test_limit will fail then error will be reported to client
|
|
732 |
test_limit(select_lex->master_unit()); |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
733 |
return; |
1
by brian
clean slate |
734 |
}
|
735 |
||
736 |
||
737 |
void Item_exists_subselect::fix_length_and_dec() |
|
738 |
{
|
|
739 |
decimals= 0; |
|
740 |
max_length= 1; |
|
741 |
max_columns= engine->cols(); |
|
742 |
/* We need only 1 row to determine existence */
|
|
743 |
unit->global_parameters->select_limit= new Item_int((int32) 1); |
|
744 |
}
|
|
745 |
||
746 |
double Item_exists_subselect::val_real() |
|
747 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
748 |
assert(fixed == 1); |
1
by brian
clean slate |
749 |
if (exec()) |
750 |
{
|
|
751 |
reset(); |
|
752 |
return 0; |
|
753 |
}
|
|
754 |
return (double) value; |
|
755 |
}
|
|
756 |
||
152
by Brian Aker
longlong replacement |
757 |
int64_t Item_exists_subselect::val_int() |
1
by brian
clean slate |
758 |
{
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
759 |
assert(fixed == 1); |
1
by brian
clean slate |
760 |
if (exec()) |
761 |
{
|
|
762 |
reset(); |
|
763 |
return 0; |
|
764 |
}
|
|
765 |
return value; |
|
766 |
}
|
|
767 |
||
768 |
String *Item_exists_subselect::val_str(String *str) |
|
769 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
770 |
assert(fixed == 1); |
1
by brian
clean slate |
771 |
if (exec()) |
772 |
{
|
|
773 |
reset(); |
|
774 |
return 0; |
|
775 |
}
|
|
151
by Brian Aker
Ulonglong to uint64_t |
776 |
str->set((uint64_t)value,&my_charset_bin); |
1
by brian
clean slate |
777 |
return str; |
778 |
}
|
|
779 |
||
780 |
||
781 |
my_decimal *Item_exists_subselect::val_decimal(my_decimal *decimal_value) |
|
782 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
783 |
assert(fixed == 1); |
1
by brian
clean slate |
784 |
if (exec()) |
785 |
{
|
|
786 |
reset(); |
|
787 |
return 0; |
|
788 |
}
|
|
789 |
int2my_decimal(E_DEC_FATAL_ERROR, value, 0, decimal_value); |
|
790 |
return decimal_value; |
|
791 |
}
|
|
792 |
||
793 |
||
794 |
bool Item_exists_subselect::val_bool() |
|
795 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
796 |
assert(fixed == 1); |
1
by brian
clean slate |
797 |
if (exec()) |
798 |
{
|
|
799 |
reset(); |
|
800 |
return 0; |
|
801 |
}
|
|
802 |
return value != 0; |
|
803 |
}
|
|
804 |
||
805 |
||
806 |
double Item_in_subselect::val_real() |
|
807 |
{
|
|
808 |
/*
|
|
809 |
As far as Item_in_subselect called only from Item_in_optimizer this
|
|
810 |
method should not be used
|
|
811 |
*/
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
812 |
assert(0); |
813 |
assert(fixed == 1); |
|
1
by brian
clean slate |
814 |
null_value= 0; |
815 |
if (exec()) |
|
816 |
{
|
|
817 |
reset(); |
|
818 |
null_value= 1; |
|
819 |
return 0; |
|
820 |
}
|
|
821 |
if (was_null && !value) |
|
822 |
null_value= 1; |
|
823 |
return (double) value; |
|
824 |
}
|
|
825 |
||
826 |
||
152
by Brian Aker
longlong replacement |
827 |
int64_t Item_in_subselect::val_int() |
1
by brian
clean slate |
828 |
{
|
829 |
/*
|
|
830 |
As far as Item_in_subselect called only from Item_in_optimizer this
|
|
831 |
method should not be used
|
|
832 |
*/
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
833 |
assert(fixed == 1); |
1
by brian
clean slate |
834 |
null_value= 0; |
835 |
if (exec()) |
|
836 |
{
|
|
837 |
reset(); |
|
838 |
null_value= 1; |
|
839 |
return 0; |
|
840 |
}
|
|
841 |
if (was_null && !value) |
|
842 |
null_value= 1; |
|
843 |
return value; |
|
844 |
}
|
|
845 |
||
846 |
||
847 |
String *Item_in_subselect::val_str(String *str) |
|
848 |
{
|
|
849 |
/*
|
|
850 |
As far as Item_in_subselect called only from Item_in_optimizer this
|
|
851 |
method should not be used
|
|
852 |
*/
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
853 |
assert(0); |
854 |
assert(fixed == 1); |
|
1
by brian
clean slate |
855 |
null_value= 0; |
856 |
if (exec()) |
|
857 |
{
|
|
858 |
reset(); |
|
859 |
null_value= 1; |
|
860 |
return 0; |
|
861 |
}
|
|
862 |
if (was_null && !value) |
|
863 |
{
|
|
864 |
null_value= 1; |
|
865 |
return 0; |
|
866 |
}
|
|
151
by Brian Aker
Ulonglong to uint64_t |
867 |
str->set((uint64_t)value, &my_charset_bin); |
1
by brian
clean slate |
868 |
return str; |
869 |
}
|
|
870 |
||
871 |
||
872 |
bool Item_in_subselect::val_bool() |
|
873 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
874 |
assert(fixed == 1); |
1
by brian
clean slate |
875 |
null_value= 0; |
876 |
if (exec()) |
|
877 |
{
|
|
878 |
reset(); |
|
879 |
/*
|
|
880 |
Must mark the IN predicate as NULL so as to make sure an enclosing NOT
|
|
55
by brian
Update for using real bool types. |
881 |
predicate will return false. See the comments in
|
1
by brian
clean slate |
882 |
subselect_uniquesubquery_engine::copy_ref_key for further details.
|
883 |
*/
|
|
884 |
null_value= 1; |
|
885 |
return 0; |
|
886 |
}
|
|
887 |
if (was_null && !value) |
|
888 |
null_value= 1; |
|
889 |
return value; |
|
890 |
}
|
|
891 |
||
892 |
my_decimal *Item_in_subselect::val_decimal(my_decimal *decimal_value) |
|
893 |
{
|
|
894 |
/*
|
|
895 |
As far as Item_in_subselect called only from Item_in_optimizer this
|
|
896 |
method should not be used
|
|
897 |
*/
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
898 |
assert(0); |
1
by brian
clean slate |
899 |
null_value= 0; |
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
900 |
assert(fixed == 1); |
1
by brian
clean slate |
901 |
if (exec()) |
902 |
{
|
|
903 |
reset(); |
|
904 |
null_value= 1; |
|
905 |
return 0; |
|
906 |
}
|
|
907 |
if (was_null && !value) |
|
908 |
null_value= 1; |
|
909 |
int2my_decimal(E_DEC_FATAL_ERROR, value, 0, decimal_value); |
|
910 |
return decimal_value; |
|
911 |
}
|
|
912 |
||
913 |
||
914 |
/*
|
|
915 |
Rewrite a single-column IN/ALL/ANY subselect
|
|
916 |
||
917 |
SYNOPSIS
|
|
918 |
Item_in_subselect::single_value_transformer()
|
|
919 |
join Join object of the subquery (i.e. 'child' join).
|
|
920 |
func Subquery comparison creator
|
|
921 |
||
922 |
DESCRIPTION
|
|
923 |
Rewrite a single-column subquery using rule-based approach. The subquery
|
|
924 |
|
|
925 |
oe $cmp$ (SELECT ie FROM ... WHERE subq_where ... HAVING subq_having)
|
|
926 |
|
|
927 |
First, try to convert the subquery to scalar-result subquery in one of
|
|
928 |
the forms:
|
|
929 |
|
|
930 |
- oe $cmp$ (SELECT MAX(...) ) // handled by Item_singlerow_subselect
|
|
931 |
- oe $cmp$ <max>(SELECT ...) // handled by Item_maxmin_subselect
|
|
932 |
|
|
933 |
If that fails, the subquery will be handled with class Item_in_optimizer.
|
|
934 |
There are two possibilites:
|
|
935 |
- If the subquery execution method is materialization, then the subquery is
|
|
936 |
not transformed any further.
|
|
937 |
- Otherwise the IN predicates is transformed into EXISTS by injecting
|
|
938 |
equi-join predicates and possibly other helper predicates. For details
|
|
939 |
see method single_value_in_like_transformer().
|
|
940 |
||
941 |
RETURN
|
|
942 |
RES_OK Either subquery was transformed, or appopriate
|
|
943 |
predicates where injected into it.
|
|
944 |
RES_REDUCE The subquery was reduced to non-subquery
|
|
945 |
RES_ERROR Error
|
|
946 |
*/
|
|
947 |
||
948 |
Item_subselect::trans_res |
|
949 |
Item_in_subselect::single_value_transformer(JOIN *join, |
|
950 |
Comp_creator *func) |
|
951 |
{
|
|
952 |
SELECT_LEX *select_lex= join->select_lex; |
|
953 |
||
954 |
/*
|
|
955 |
Check that the right part of the subselect contains no more than one
|
|
956 |
column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
|
|
957 |
*/
|
|
958 |
if (select_lex->item_list.elements > 1) |
|
959 |
{
|
|
960 |
my_error(ER_OPERAND_COLUMNS, MYF(0), 1); |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
961 |
return(RES_ERROR); |
1
by brian
clean slate |
962 |
}
|
963 |
||
964 |
/*
|
|
965 |
If this is an ALL/ANY single-value subselect, try to rewrite it with
|
|
966 |
a MIN/MAX subselect. We can do that if a possible NULL result of the
|
|
967 |
subselect can be ignored.
|
|
968 |
E.g. SELECT * FROM t1 WHERE b > ANY (SELECT a FROM t2) can be rewritten
|
|
969 |
with SELECT * FROM t1 WHERE b > (SELECT MAX(a) FROM t2).
|
|
970 |
We can't check that this optimization is safe if it's not a top-level
|
|
971 |
item of the WHERE clause (e.g. because the WHERE clause can contain IS
|
|
972 |
NULL/IS NOT NULL functions). If so, we rewrite ALL/ANY with NOT EXISTS
|
|
973 |
later in this method.
|
|
974 |
*/
|
|
975 |
if ((abort_on_null || (upper_item && upper_item->top_level())) && |
|
976 |
!select_lex->master_unit()->uncacheable && !func->eqne_op()) |
|
977 |
{
|
|
978 |
if (substitution) |
|
979 |
{
|
|
980 |
// It is second (third, ...) SELECT of UNION => All is done
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
981 |
return(RES_OK); |
1
by brian
clean slate |
982 |
}
|
983 |
||
984 |
Item *subs; |
|
985 |
if (!select_lex->group_list.elements && |
|
986 |
!select_lex->having && |
|
987 |
!select_lex->with_sum_func && |
|
988 |
!(select_lex->next_select()) && |
|
989 |
select_lex->table_list.elements) |
|
990 |
{
|
|
991 |
Item_sum_hybrid *item; |
|
992 |
nesting_map save_allow_sum_func; |
|
993 |
if (func->l_op()) |
|
994 |
{
|
|
995 |
/*
|
|
996 |
(ALL && (> || =>)) || (ANY && (< || =<))
|
|
997 |
for ALL condition is inverted
|
|
998 |
*/
|
|
999 |
item= new Item_sum_max(*select_lex->ref_pointer_array); |
|
1000 |
}
|
|
1001 |
else
|
|
1002 |
{
|
|
1003 |
/*
|
|
1004 |
(ALL && (< || =<)) || (ANY && (> || =>))
|
|
1005 |
for ALL condition is inverted
|
|
1006 |
*/
|
|
1007 |
item= new Item_sum_min(*select_lex->ref_pointer_array); |
|
1008 |
}
|
|
1009 |
if (upper_item) |
|
1010 |
upper_item->set_sum_test(item); |
|
1011 |
*select_lex->ref_pointer_array= item; |
|
1012 |
{
|
|
1013 |
List_iterator<Item> it(select_lex->item_list); |
|
1014 |
it++; |
|
1015 |
it.replace(item); |
|
1016 |
}
|
|
1017 |
||
1018 |
save_allow_sum_func= thd->lex->allow_sum_func; |
|
1019 |
thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level; |
|
1020 |
/*
|
|
1021 |
Item_sum_(max|min) can't substitute other item => we can use 0 as
|
|
1022 |
reference, also Item_sum_(max|min) can't be fixed after creation, so
|
|
1023 |
we do not check item->fixed
|
|
1024 |
*/
|
|
1025 |
if (item->fix_fields(thd, 0)) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1026 |
return(RES_ERROR); |
1
by brian
clean slate |
1027 |
thd->lex->allow_sum_func= save_allow_sum_func; |
1028 |
/* we added aggregate function => we have to change statistic */
|
|
1029 |
count_field_types(select_lex, &join->tmp_table_param, join->all_fields, |
|
1030 |
0); |
|
1031 |
||
1032 |
subs= new Item_singlerow_subselect(select_lex); |
|
1033 |
}
|
|
1034 |
else
|
|
1035 |
{
|
|
1036 |
Item_maxmin_subselect *item; |
|
1037 |
subs= item= new Item_maxmin_subselect(thd, this, select_lex, func->l_op()); |
|
1038 |
if (upper_item) |
|
1039 |
upper_item->set_sub_test(item); |
|
1040 |
}
|
|
1041 |
/* fix fields is already called for left expression */
|
|
1042 |
substitution= func->create(left_expr, subs); |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1043 |
return(RES_OK); |
1
by brian
clean slate |
1044 |
}
|
1045 |
||
1046 |
if (!substitution) |
|
1047 |
{
|
|
1048 |
/* We're invoked for the 1st (or the only) SELECT in the subquery UNION */
|
|
1049 |
SELECT_LEX_UNIT *master_unit= select_lex->master_unit(); |
|
1050 |
substitution= optimizer; |
|
1051 |
||
1052 |
SELECT_LEX *current= thd->lex->current_select, *up; |
|
1053 |
||
1054 |
thd->lex->current_select= up= current->return_after_parsing(); |
|
1055 |
//optimizer never use Item **ref => we can pass 0 as parameter
|
|
1056 |
if (!optimizer || optimizer->fix_left(thd, 0)) |
|
1057 |
{
|
|
1058 |
thd->lex->current_select= current; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1059 |
return(RES_ERROR); |
1
by brian
clean slate |
1060 |
}
|
1061 |
thd->lex->current_select= current; |
|
1062 |
||
1063 |
/*
|
|
1064 |
As far as Item_ref_in_optimizer do not substitute itself on fix_fields
|
|
1065 |
we can use same item for all selects.
|
|
1066 |
*/
|
|
1067 |
expr= new Item_direct_ref(&select_lex->context, |
|
1068 |
(Item**)optimizer->get_cache(), |
|
1069 |
(char *)"<no matter>", |
|
1070 |
(char *)in_left_expr_name); |
|
1071 |
||
1072 |
master_unit->uncacheable|= UNCACHEABLE_DEPENDENT; |
|
1073 |
}
|
|
1074 |
||
1075 |
if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards) |
|
1076 |
{
|
|
1077 |
if (!(pushed_cond_guards= (bool*)join->thd->alloc(sizeof(bool)))) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1078 |
return(RES_ERROR); |
55
by brian
Update for using real bool types. |
1079 |
pushed_cond_guards[0]= true; |
1
by brian
clean slate |
1080 |
}
|
1081 |
||
1082 |
/*
|
|
1083 |
If this IN predicate can be computed via materialization, do not
|
|
1084 |
perform the IN -> EXISTS transformation.
|
|
1085 |
*/
|
|
1086 |
if (exec_method == MATERIALIZATION) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1087 |
return(RES_OK); |
1
by brian
clean slate |
1088 |
|
1089 |
/* Perform the IN=>EXISTS transformation. */
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1090 |
return(single_value_in_to_exists_transformer(join, func)); |
1
by brian
clean slate |
1091 |
}
|
1092 |
||
1093 |
||
1094 |
/**
|
|
1095 |
Transofrm an IN predicate into EXISTS via predicate injection.
|
|
1096 |
||
1097 |
@details The transformation injects additional predicates into the subquery
|
|
1098 |
(and makes the subquery correlated) as follows.
|
|
1099 |
||
1100 |
- If the subquery has aggregates, GROUP BY, or HAVING, convert to
|
|
1101 |
||
1102 |
SELECT ie FROM ... HAVING subq_having AND
|
|
1103 |
trigcond(oe $cmp$ ref_or_null_helper<ie>)
|
|
1104 |
|
|
1105 |
the addition is wrapped into trigger only when we want to distinguish
|
|
55
by brian
Update for using real bool types. |
1106 |
between NULL and false results.
|
1
by brian
clean slate |
1107 |
|
1108 |
- Otherwise (no aggregates/GROUP BY/HAVING) convert it to one of the
|
|
1109 |
following:
|
|
1110 |
||
55
by brian
Update for using real bool types. |
1111 |
= If we don't need to distinguish between NULL and false subquery:
|
1
by brian
clean slate |
1112 |
|
1113 |
SELECT 1 FROM ... WHERE (oe $cmp$ ie) AND subq_where
|
|
1114 |
||
1115 |
= If we need to distinguish between those:
|
|
1116 |
||
1117 |
SELECT 1 FROM ...
|
|
1118 |
WHERE subq_where AND trigcond((oe $cmp$ ie) OR (ie IS NULL))
|
|
1119 |
HAVING trigcond(<is_not_null_test>(ie))
|
|
1120 |
||
1121 |
@param join Join object of the subquery (i.e. 'child' join).
|
|
1122 |
@param func Subquery comparison creator
|
|
1123 |
||
1124 |
@retval RES_OK Either subquery was transformed, or appopriate
|
|
1125 |
predicates where injected into it.
|
|
1126 |
@retval RES_REDUCE The subquery was reduced to non-subquery
|
|
1127 |
@retval RES_ERROR Error
|
|
1128 |
*/
|
|
1129 |
||
1130 |
Item_subselect::trans_res |
|
1131 |
Item_in_subselect::single_value_in_to_exists_transformer(JOIN * join, Comp_creator *func) |
|
1132 |
{
|
|
1133 |
SELECT_LEX *select_lex= join->select_lex; |
|
1134 |
||
1135 |
select_lex->uncacheable|= UNCACHEABLE_DEPENDENT; |
|
1136 |
if (join->having || select_lex->with_sum_func || |
|
1137 |
select_lex->group_list.elements) |
|
1138 |
{
|
|
1139 |
bool tmp; |
|
1140 |
Item *item= func->create(expr, |
|
1141 |
new Item_ref_null_helper(&select_lex->context, |
|
1142 |
this, |
|
1143 |
select_lex-> |
|
1144 |
ref_pointer_array, |
|
1145 |
(char *)"<ref>", |
|
1146 |
this->full_name())); |
|
1147 |
if (!abort_on_null && left_expr->maybe_null) |
|
1148 |
{
|
|
1149 |
/*
|
|
1150 |
We can encounter "NULL IN (SELECT ...)". Wrap the added condition
|
|
1151 |
within a trig_cond.
|
|
1152 |
*/
|
|
1153 |
item= new Item_func_trig_cond(item, get_cond_guard(0)); |
|
1154 |
}
|
|
1155 |
||
1156 |
/*
|
|
1157 |
AND and comparison functions can't be changed during fix_fields()
|
|
1158 |
we can assign select_lex->having here, and pass 0 as last
|
|
1159 |
argument (reference) to fix_fields()
|
|
1160 |
*/
|
|
1161 |
select_lex->having= join->having= and_items(join->having, item); |
|
1162 |
if (join->having == item) |
|
1163 |
item->name= (char*)in_having_cond; |
|
1164 |
select_lex->having_fix_field= 1; |
|
1165 |
/*
|
|
1166 |
we do not check join->having->fixed, because Item_and (from and_items)
|
|
1167 |
or comparison function (from func->create) can't be fixed after creation
|
|
1168 |
*/
|
|
1169 |
tmp= join->having->fix_fields(thd, 0); |
|
1170 |
select_lex->having_fix_field= 0; |
|
1171 |
if (tmp) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1172 |
return(RES_ERROR); |
1
by brian
clean slate |
1173 |
}
|
1174 |
else
|
|
1175 |
{
|
|
1176 |
Item *item= (Item*) select_lex->item_list.head(); |
|
1177 |
||
1178 |
if (select_lex->table_list.elements) |
|
1179 |
{
|
|
1180 |
bool tmp; |
|
1181 |
Item *having= item, *orig_item= item; |
|
1182 |
select_lex->item_list.empty(); |
|
1183 |
select_lex->item_list.push_back(new Item_int("Not_used", |
|
152
by Brian Aker
longlong replacement |
1184 |
(int64_t) 1, |
1
by brian
clean slate |
1185 |
MY_INT64_NUM_DECIMAL_DIGITS)); |
1186 |
select_lex->ref_pointer_array[0]= select_lex->item_list.head(); |
|
1187 |
||
1188 |
item= func->create(expr, item); |
|
1189 |
if (!abort_on_null && orig_item->maybe_null) |
|
1190 |
{
|
|
1191 |
having= new Item_is_not_null_test(this, having); |
|
1192 |
if (left_expr->maybe_null) |
|
1193 |
{
|
|
1194 |
if (!(having= new Item_func_trig_cond(having, |
|
1195 |
get_cond_guard(0)))) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1196 |
return(RES_ERROR); |
1
by brian
clean slate |
1197 |
}
|
1198 |
/*
|
|
1199 |
Item_is_not_null_test can't be changed during fix_fields()
|
|
1200 |
we can assign select_lex->having here, and pass 0 as last
|
|
1201 |
argument (reference) to fix_fields()
|
|
1202 |
*/
|
|
1203 |
having->name= (char*)in_having_cond; |
|
1204 |
select_lex->having= join->having= having; |
|
1205 |
select_lex->having_fix_field= 1; |
|
1206 |
/*
|
|
1207 |
we do not check join->having->fixed, because Item_and (from
|
|
1208 |
and_items) or comparison function (from func->create) can't be
|
|
1209 |
fixed after creation
|
|
1210 |
*/
|
|
1211 |
tmp= join->having->fix_fields(thd, 0); |
|
1212 |
select_lex->having_fix_field= 0; |
|
1213 |
if (tmp) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1214 |
return(RES_ERROR); |
1
by brian
clean slate |
1215 |
item= new Item_cond_or(item, |
1216 |
new Item_func_isnull(orig_item)); |
|
1217 |
}
|
|
1218 |
/*
|
|
1219 |
If we may encounter NULL IN (SELECT ...) and care whether subquery
|
|
55
by brian
Update for using real bool types. |
1220 |
result is NULL or false, wrap condition in a trig_cond.
|
1
by brian
clean slate |
1221 |
*/
|
1222 |
if (!abort_on_null && left_expr->maybe_null) |
|
1223 |
{
|
|
1224 |
if (!(item= new Item_func_trig_cond(item, get_cond_guard(0)))) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1225 |
return(RES_ERROR); |
1
by brian
clean slate |
1226 |
}
|
1227 |
/*
|
|
1228 |
TODO: figure out why the following is done here in
|
|
1229 |
single_value_transformer but there is no corresponding action in
|
|
1230 |
row_value_transformer?
|
|
1231 |
*/
|
|
1232 |
item->name= (char *)in_additional_cond; |
|
1233 |
||
1234 |
/*
|
|
1235 |
AND can't be changed during fix_fields()
|
|
1236 |
we can assign select_lex->having here, and pass 0 as last
|
|
1237 |
argument (reference) to fix_fields()
|
|
1238 |
*/
|
|
1239 |
select_lex->where= join->conds= and_items(join->conds, item); |
|
1240 |
select_lex->where->top_level_item(); |
|
1241 |
/*
|
|
1242 |
we do not check join->conds->fixed, because Item_and can't be fixed
|
|
1243 |
after creation
|
|
1244 |
*/
|
|
1245 |
if (join->conds->fix_fields(thd, 0)) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1246 |
return(RES_ERROR); |
1
by brian
clean slate |
1247 |
}
|
1248 |
else
|
|
1249 |
{
|
|
1250 |
bool tmp; |
|
1251 |
if (select_lex->master_unit()->is_union()) |
|
1252 |
{
|
|
1253 |
/*
|
|
1254 |
comparison functions can't be changed during fix_fields()
|
|
1255 |
we can assign select_lex->having here, and pass 0 as last
|
|
1256 |
argument (reference) to fix_fields()
|
|
1257 |
*/
|
|
1258 |
Item *new_having= |
|
1259 |
func->create(expr, |
|
1260 |
new Item_ref_null_helper(&select_lex->context, this, |
|
1261 |
select_lex->ref_pointer_array, |
|
1262 |
(char *)"<no matter>", |
|
1263 |
(char *)"<result>")); |
|
1264 |
if (!abort_on_null && left_expr->maybe_null) |
|
1265 |
{
|
|
1266 |
if (!(new_having= new Item_func_trig_cond(new_having, |
|
1267 |
get_cond_guard(0)))) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1268 |
return(RES_ERROR); |
1
by brian
clean slate |
1269 |
}
|
1270 |
new_having->name= (char*)in_having_cond; |
|
1271 |
select_lex->having= join->having= new_having; |
|
1272 |
select_lex->having_fix_field= 1; |
|
1273 |
||
1274 |
/*
|
|
1275 |
we do not check join->having->fixed, because comparison function
|
|
1276 |
(from func->create) can't be fixed after creation
|
|
1277 |
*/
|
|
1278 |
tmp= join->having->fix_fields(thd, 0); |
|
1279 |
select_lex->having_fix_field= 0; |
|
1280 |
if (tmp) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1281 |
return(RES_ERROR); |
1
by brian
clean slate |
1282 |
}
|
1283 |
else
|
|
1284 |
{
|
|
1285 |
// it is single select without tables => possible optimization
|
|
1286 |
item= func->create(left_expr, item); |
|
1287 |
// fix_field of item will be done in time of substituting
|
|
1288 |
substitution= item; |
|
1289 |
have_to_be_excluded= 1; |
|
1290 |
if (thd->lex->describe) |
|
1291 |
{
|
|
1292 |
char warn_buff[MYSQL_ERRMSG_SIZE]; |
|
1293 |
sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number); |
|
1294 |
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE, |
|
1295 |
ER_SELECT_REDUCED, warn_buff); |
|
1296 |
}
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1297 |
return(RES_REDUCE); |
1
by brian
clean slate |
1298 |
}
|
1299 |
}
|
|
1300 |
}
|
|
1301 |
||
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1302 |
return(RES_OK); |
1
by brian
clean slate |
1303 |
}
|
1304 |
||
1305 |
||
1306 |
Item_subselect::trans_res |
|
1307 |
Item_in_subselect::row_value_transformer(JOIN *join) |
|
1308 |
{
|
|
1309 |
SELECT_LEX *select_lex= join->select_lex; |
|
1310 |
uint cols_num= left_expr->cols(); |
|
1311 |
||
1312 |
if (select_lex->item_list.elements != left_expr->cols()) |
|
1313 |
{
|
|
1314 |
my_error(ER_OPERAND_COLUMNS, MYF(0), left_expr->cols()); |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1315 |
return(RES_ERROR); |
1
by brian
clean slate |
1316 |
}
|
1317 |
||
1318 |
/*
|
|
1319 |
Wrap the current IN predicate in an Item_in_optimizer. The actual
|
|
1320 |
substitution in the Item tree takes place in Item_subselect::fix_fields.
|
|
1321 |
*/
|
|
1322 |
if (!substitution) |
|
1323 |
{
|
|
1324 |
//first call for this unit
|
|
1325 |
SELECT_LEX_UNIT *master_unit= select_lex->master_unit(); |
|
1326 |
substitution= optimizer; |
|
1327 |
||
1328 |
SELECT_LEX *current= thd->lex->current_select, *up; |
|
1329 |
thd->lex->current_select= up= current->return_after_parsing(); |
|
1330 |
//optimizer never use Item **ref => we can pass 0 as parameter
|
|
1331 |
if (!optimizer || optimizer->fix_left(thd, 0)) |
|
1332 |
{
|
|
1333 |
thd->lex->current_select= current; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1334 |
return(RES_ERROR); |
1
by brian
clean slate |
1335 |
}
|
1336 |
||
1337 |
// we will refer to upper level cache array => we have to save it in PS
|
|
1338 |
optimizer->keep_top_level_cache(); |
|
1339 |
||
1340 |
thd->lex->current_select= current; |
|
1341 |
master_unit->uncacheable|= UNCACHEABLE_DEPENDENT; |
|
1342 |
||
1343 |
if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards) |
|
1344 |
{
|
|
1345 |
if (!(pushed_cond_guards= (bool*)join->thd->alloc(sizeof(bool) * |
|
1346 |
left_expr->cols()))) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1347 |
return(RES_ERROR); |
1
by brian
clean slate |
1348 |
for (uint i= 0; i < cols_num; i++) |
55
by brian
Update for using real bool types. |
1349 |
pushed_cond_guards[i]= true; |
1
by brian
clean slate |
1350 |
}
|
1351 |
}
|
|
1352 |
||
1353 |
/*
|
|
1354 |
If this IN predicate can be computed via materialization, do not
|
|
1355 |
perform the IN -> EXISTS transformation.
|
|
1356 |
*/
|
|
1357 |
if (exec_method == MATERIALIZATION) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1358 |
return(RES_OK); |
1
by brian
clean slate |
1359 |
|
1360 |
/* Perform the IN=>EXISTS transformation. */
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1361 |
return(row_value_in_to_exists_transformer(join)); |
1
by brian
clean slate |
1362 |
}
|
1363 |
||
1364 |
||
1365 |
/**
|
|
1366 |
Tranform a (possibly non-correlated) IN subquery into a correlated EXISTS.
|
|
1367 |
||
1368 |
@todo
|
|
1369 |
The IF-ELSE below can be refactored so that there is no duplication of the
|
|
1370 |
statements that create the new conditions. For this we have to invert the IF
|
|
1371 |
and the FOR statements as this:
|
|
1372 |
for (each left operand)
|
|
1373 |
create the equi-join condition
|
|
1374 |
if (is_having_used || !abort_on_null)
|
|
1375 |
create the "is null" and is_not_null_test items
|
|
1376 |
if (is_having_used)
|
|
1377 |
add the equi-join and the null tests to HAVING
|
|
1378 |
else
|
|
1379 |
add the equi-join and the "is null" to WHERE
|
|
1380 |
add the is_not_null_test to HAVING
|
|
1381 |
*/
|
|
1382 |
||
1383 |
Item_subselect::trans_res |
|
1384 |
Item_in_subselect::row_value_in_to_exists_transformer(JOIN * join) |
|
1385 |
{
|
|
1386 |
SELECT_LEX *select_lex= join->select_lex; |
|
1387 |
Item *having_item= 0; |
|
1388 |
uint cols_num= left_expr->cols(); |
|
1389 |
bool is_having_used= (join->having || select_lex->with_sum_func || |
|
1390 |
select_lex->group_list.first || |
|
1391 |
!select_lex->table_list.elements); |
|
1392 |
||
1393 |
select_lex->uncacheable|= UNCACHEABLE_DEPENDENT; |
|
1394 |
if (is_having_used) |
|
1395 |
{
|
|
1396 |
/*
|
|
1397 |
(l1, l2, l3) IN (SELECT v1, v2, v3 ... HAVING having) =>
|
|
1398 |
EXISTS (SELECT ... HAVING having and
|
|
1399 |
(l1 = v1 or is null v1) and
|
|
1400 |
(l2 = v2 or is null v2) and
|
|
1401 |
(l3 = v3 or is null v3) and
|
|
1402 |
is_not_null_test(v1) and
|
|
1403 |
is_not_null_test(v2) and
|
|
1404 |
is_not_null_test(v3))
|
|
1405 |
where is_not_null_test used to register nulls in case if we have
|
|
1406 |
not found matching to return correct NULL value
|
|
1407 |
TODO: say here explicitly if the order of AND parts matters or not.
|
|
1408 |
*/
|
|
1409 |
Item *item_having_part2= 0; |
|
1410 |
for (uint i= 0; i < cols_num; i++) |
|
1411 |
{
|
|
133
by Brian Aker
Cleanup of warning/errors. |
1412 |
assert((left_expr->fixed && select_lex->ref_pointer_array[i]->fixed) || |
1
by brian
clean slate |
1413 |
(select_lex->ref_pointer_array[i]->type() == REF_ITEM && |
1414 |
((Item_ref*)(select_lex->ref_pointer_array[i]))->ref_type() == |
|
1415 |
Item_ref::OUTER_REF)); |
|
1416 |
if (select_lex->ref_pointer_array[i]-> |
|
1417 |
check_cols(left_expr->element_index(i)->cols())) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1418 |
return(RES_ERROR); |
1
by brian
clean slate |
1419 |
Item *item_eq= |
1420 |
new Item_func_eq(new |
|
1421 |
Item_ref(&select_lex->context, |
|
1422 |
(*optimizer->get_cache())-> |
|
1423 |
addr(i), |
|
1424 |
(char *)"<no matter>", |
|
1425 |
(char *)in_left_expr_name), |
|
1426 |
new
|
|
1427 |
Item_ref(&select_lex->context, |
|
1428 |
select_lex->ref_pointer_array + i, |
|
1429 |
(char *)"<no matter>", |
|
1430 |
(char *)"<list ref>") |
|
1431 |
);
|
|
1432 |
Item *item_isnull= |
|
1433 |
new Item_func_isnull(new |
|
1434 |
Item_ref(&select_lex->context, |
|
1435 |
select_lex->ref_pointer_array+i, |
|
1436 |
(char *)"<no matter>", |
|
1437 |
(char *)"<list ref>") |
|
1438 |
);
|
|
1439 |
Item *col_item= new Item_cond_or(item_eq, item_isnull); |
|
1440 |
if (!abort_on_null && left_expr->element_index(i)->maybe_null) |
|
1441 |
{
|
|
1442 |
if (!(col_item= new Item_func_trig_cond(col_item, get_cond_guard(i)))) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1443 |
return(RES_ERROR); |
1
by brian
clean slate |
1444 |
}
|
1445 |
having_item= and_items(having_item, col_item); |
|
1446 |
||
1447 |
Item *item_nnull_test= |
|
1448 |
new Item_is_not_null_test(this, |
|
1449 |
new Item_ref(&select_lex->context, |
|
1450 |
select_lex-> |
|
1451 |
ref_pointer_array + i, |
|
1452 |
(char *)"<no matter>", |
|
1453 |
(char *)"<list ref>")); |
|
1454 |
if (!abort_on_null && left_expr->element_index(i)->maybe_null) |
|
1455 |
{
|
|
1456 |
if (!(item_nnull_test= |
|
1457 |
new Item_func_trig_cond(item_nnull_test, get_cond_guard(i)))) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1458 |
return(RES_ERROR); |
1
by brian
clean slate |
1459 |
}
|
1460 |
item_having_part2= and_items(item_having_part2, item_nnull_test); |
|
1461 |
item_having_part2->top_level_item(); |
|
1462 |
}
|
|
1463 |
having_item= and_items(having_item, item_having_part2); |
|
1464 |
having_item->top_level_item(); |
|
1465 |
}
|
|
1466 |
else
|
|
1467 |
{
|
|
1468 |
/*
|
|
1469 |
(l1, l2, l3) IN (SELECT v1, v2, v3 ... WHERE where) =>
|
|
1470 |
EXISTS (SELECT ... WHERE where and
|
|
1471 |
(l1 = v1 or is null v1) and
|
|
1472 |
(l2 = v2 or is null v2) and
|
|
1473 |
(l3 = v3 or is null v3)
|
|
1474 |
HAVING is_not_null_test(v1) and
|
|
1475 |
is_not_null_test(v2) and
|
|
1476 |
is_not_null_test(v3))
|
|
1477 |
where is_not_null_test register NULLs values but reject rows
|
|
1478 |
||
1479 |
in case when we do not need correct NULL, we have simplier construction:
|
|
1480 |
EXISTS (SELECT ... WHERE where and
|
|
1481 |
(l1 = v1) and
|
|
1482 |
(l2 = v2) and
|
|
1483 |
(l3 = v3)
|
|
1484 |
*/
|
|
1485 |
Item *where_item= 0; |
|
1486 |
for (uint i= 0; i < cols_num; i++) |
|
1487 |
{
|
|
1488 |
Item *item, *item_isnull; |
|
133
by Brian Aker
Cleanup of warning/errors. |
1489 |
assert((left_expr->fixed && select_lex->ref_pointer_array[i]->fixed) || |
1
by brian
clean slate |
1490 |
(select_lex->ref_pointer_array[i]->type() == REF_ITEM && |
1491 |
((Item_ref*)(select_lex->ref_pointer_array[i]))->ref_type() == |
|
1492 |
Item_ref::OUTER_REF)); |
|
1493 |
if (select_lex->ref_pointer_array[i]-> |
|
1494 |
check_cols(left_expr->element_index(i)->cols())) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1495 |
return(RES_ERROR); |
1
by brian
clean slate |
1496 |
item= |
1497 |
new Item_func_eq(new |
|
1498 |
Item_direct_ref(&select_lex->context, |
|
1499 |
(*optimizer->get_cache())-> |
|
1500 |
addr(i), |
|
1501 |
(char *)"<no matter>", |
|
1502 |
(char *)in_left_expr_name), |
|
1503 |
new
|
|
1504 |
Item_direct_ref(&select_lex->context, |
|
1505 |
select_lex-> |
|
1506 |
ref_pointer_array+i, |
|
1507 |
(char *)"<no matter>", |
|
1508 |
(char *)"<list ref>") |
|
1509 |
);
|
|
1510 |
if (!abort_on_null) |
|
1511 |
{
|
|
1512 |
Item *having_col_item= |
|
1513 |
new Item_is_not_null_test(this, |
|
1514 |
new
|
|
1515 |
Item_ref(&select_lex->context, |
|
1516 |
select_lex->ref_pointer_array + i, |
|
1517 |
(char *)"<no matter>", |
|
1518 |
(char *)"<list ref>")); |
|
1519 |
||
1520 |
||
1521 |
item_isnull= new |
|
1522 |
Item_func_isnull(new |
|
1523 |
Item_direct_ref(&select_lex->context, |
|
1524 |
select_lex-> |
|
1525 |
ref_pointer_array+i, |
|
1526 |
(char *)"<no matter>", |
|
1527 |
(char *)"<list ref>") |
|
1528 |
);
|
|
1529 |
item= new Item_cond_or(item, item_isnull); |
|
1530 |
/*
|
|
1531 |
TODO: why we create the above for cases where the right part
|
|
1532 |
cant be NULL?
|
|
1533 |
*/
|
|
1534 |
if (left_expr->element_index(i)->maybe_null) |
|
1535 |
{
|
|
1536 |
if (!(item= new Item_func_trig_cond(item, get_cond_guard(i)))) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1537 |
return(RES_ERROR); |
1
by brian
clean slate |
1538 |
if (!(having_col_item= |
1539 |
new Item_func_trig_cond(having_col_item, get_cond_guard(i)))) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1540 |
return(RES_ERROR); |
1
by brian
clean slate |
1541 |
}
|
1542 |
having_item= and_items(having_item, having_col_item); |
|
1543 |
}
|
|
1544 |
where_item= and_items(where_item, item); |
|
1545 |
}
|
|
1546 |
/*
|
|
1547 |
AND can't be changed during fix_fields()
|
|
1548 |
we can assign select_lex->where here, and pass 0 as last
|
|
1549 |
argument (reference) to fix_fields()
|
|
1550 |
*/
|
|
1551 |
select_lex->where= join->conds= and_items(join->conds, where_item); |
|
1552 |
select_lex->where->top_level_item(); |
|
1553 |
if (join->conds->fix_fields(thd, 0)) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1554 |
return(RES_ERROR); |
1
by brian
clean slate |
1555 |
}
|
1556 |
if (having_item) |
|
1557 |
{
|
|
1558 |
bool res; |
|
1559 |
select_lex->having= join->having= and_items(join->having, having_item); |
|
1560 |
if (having_item == select_lex->having) |
|
1561 |
having_item->name= (char*)in_having_cond; |
|
1562 |
select_lex->having->top_level_item(); |
|
1563 |
/*
|
|
1564 |
AND can't be changed during fix_fields()
|
|
1565 |
we can assign select_lex->having here, and pass 0 as last
|
|
1566 |
argument (reference) to fix_fields()
|
|
1567 |
*/
|
|
1568 |
select_lex->having_fix_field= 1; |
|
1569 |
res= join->having->fix_fields(thd, 0); |
|
1570 |
select_lex->having_fix_field= 0; |
|
1571 |
if (res) |
|
1572 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1573 |
return(RES_ERROR); |
1
by brian
clean slate |
1574 |
}
|
1575 |
}
|
|
1576 |
||
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1577 |
return(RES_OK); |
1
by brian
clean slate |
1578 |
}
|
1579 |
||
1580 |
||
1581 |
Item_subselect::trans_res |
|
1582 |
Item_in_subselect::select_transformer(JOIN *join) |
|
1583 |
{
|
|
1584 |
return select_in_like_transformer(join, &eq_creator); |
|
1585 |
}
|
|
1586 |
||
1587 |
||
1588 |
/**
|
|
1589 |
Prepare IN/ALL/ANY/SOME subquery transformation and call appropriate
|
|
1590 |
transformation function.
|
|
1591 |
||
1592 |
To decide which transformation procedure (scalar or row) applicable here
|
|
1593 |
we have to call fix_fields() for left expression to be able to call
|
|
1594 |
cols() method on it. Also this method make arena management for
|
|
1595 |
underlying transformation methods.
|
|
1596 |
||
1597 |
@param join JOIN object of transforming subquery
|
|
1598 |
@param func creator of condition function of subquery
|
|
1599 |
||
1600 |
@retval
|
|
1601 |
RES_OK OK
|
|
1602 |
@retval
|
|
1603 |
RES_REDUCE OK, and current subquery was reduced during
|
|
1604 |
transformation
|
|
1605 |
@retval
|
|
1606 |
RES_ERROR Error
|
|
1607 |
*/
|
|
1608 |
||
1609 |
Item_subselect::trans_res |
|
1610 |
Item_in_subselect::select_in_like_transformer(JOIN *join, Comp_creator *func) |
|
1611 |
{
|
|
1612 |
SELECT_LEX *current= thd->lex->current_select, *up; |
|
1613 |
const char *save_where= thd->where; |
|
1614 |
Item_subselect::trans_res res= RES_ERROR; |
|
1615 |
bool result; |
|
1616 |
||
1617 |
{
|
|
1618 |
/*
|
|
1619 |
IN/SOME/ALL/ANY subqueries aren't support LIMIT clause. Without it
|
|
1620 |
ORDER BY clause becomes meaningless thus we drop it here.
|
|
1621 |
*/
|
|
1622 |
SELECT_LEX *sl= current->master_unit()->first_select(); |
|
1623 |
for (; sl; sl= sl->next_select()) |
|
1624 |
{
|
|
1625 |
if (sl->join) |
|
1626 |
sl->join->order= 0; |
|
1627 |
}
|
|
1628 |
}
|
|
1629 |
||
1630 |
if (changed) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1631 |
return(RES_OK); |
1
by brian
clean slate |
1632 |
|
1633 |
thd->where= "IN/ALL/ANY subquery"; |
|
1634 |
||
1635 |
/*
|
|
1636 |
In some optimisation cases we will not need this Item_in_optimizer
|
|
1637 |
object, but we can't know it here, but here we need address correct
|
|
1638 |
reference on left expresion.
|
|
1639 |
*/
|
|
1640 |
if (!optimizer) |
|
1641 |
{
|
|
1642 |
result= (!(optimizer= new Item_in_optimizer(left_expr, this))); |
|
1643 |
if (result) |
|
1644 |
goto err; |
|
1645 |
}
|
|
1646 |
||
1647 |
thd->lex->current_select= up= current->return_after_parsing(); |
|
1648 |
result= (!left_expr->fixed && |
|
1649 |
left_expr->fix_fields(thd, optimizer->arguments())); |
|
1650 |
/* fix_fields can change reference to left_expr, we need reassign it */
|
|
1651 |
left_expr= optimizer->arguments()[0]; |
|
1652 |
||
1653 |
thd->lex->current_select= current; |
|
1654 |
if (result) |
|
1655 |
goto err; |
|
1656 |
||
1657 |
/*
|
|
1658 |
If we didn't choose an execution method up to this point, we choose
|
|
1659 |
the IN=>EXISTS transformation.
|
|
1660 |
*/
|
|
1661 |
if (exec_method == NOT_TRANSFORMED) |
|
1662 |
exec_method= IN_TO_EXISTS; |
|
1663 |
||
1664 |
/*
|
|
1665 |
Both transformers call fix_fields() only for Items created inside them,
|
|
1666 |
and all those items do not make permanent changes in the current item arena
|
|
1667 |
which allows us to call them with changed arena (if we do not know the
|
|
1668 |
nature of Item, we have to call fix_fields() for it only with the original
|
|
1669 |
arena to avoid memory leak).
|
|
1670 |
*/
|
|
1671 |
if (left_expr->cols() == 1) |
|
1672 |
res= single_value_transformer(join, func); |
|
1673 |
else
|
|
1674 |
{
|
|
1675 |
/* we do not support row operation for ALL/ANY/SOME */
|
|
1676 |
if (func != &eq_creator) |
|
1677 |
{
|
|
1678 |
my_error(ER_OPERAND_COLUMNS, MYF(0), 1); |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1679 |
return(RES_ERROR); |
1
by brian
clean slate |
1680 |
}
|
1681 |
res= row_value_transformer(join); |
|
1682 |
}
|
|
1683 |
err: |
|
1684 |
thd->where= save_where; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1685 |
return(res); |
1
by brian
clean slate |
1686 |
}
|
1687 |
||
1688 |
||
1689 |
void Item_in_subselect::print(String *str, enum_query_type query_type) |
|
1690 |
{
|
|
1691 |
if (exec_method == IN_TO_EXISTS) |
|
1692 |
str->append(STRING_WITH_LEN("<exists>")); |
|
1693 |
else
|
|
1694 |
{
|
|
1695 |
left_expr->print(str, query_type); |
|
1696 |
str->append(STRING_WITH_LEN(" in ")); |
|
1697 |
}
|
|
1698 |
Item_subselect::print(str, query_type); |
|
1699 |
}
|
|
1700 |
||
1701 |
||
1702 |
bool Item_in_subselect::fix_fields(THD *thd_arg, Item **ref) |
|
1703 |
{
|
|
1704 |
bool result = 0; |
|
1705 |
||
1706 |
if (exec_method == SEMI_JOIN) |
|
1707 |
return !( (*ref)= new Item_int(1)); |
|
1708 |
||
1709 |
if (thd_arg->lex->view_prepare_mode && left_expr && !left_expr->fixed) |
|
1710 |
result = left_expr->fix_fields(thd_arg, &left_expr); |
|
1711 |
||
1712 |
return result || Item_subselect::fix_fields(thd_arg, ref); |
|
1713 |
}
|
|
1714 |
||
1715 |
||
1716 |
/**
|
|
1717 |
Try to create an engine to compute the subselect via materialization,
|
|
1718 |
and if this fails, revert to execution via the IN=>EXISTS transformation.
|
|
1719 |
||
1720 |
@details
|
|
1721 |
The purpose of this method is to hide the implementation details
|
|
1722 |
of this Item's execution. The method creates a new engine for
|
|
1723 |
materialized execution, and initializes the engine.
|
|
1724 |
||
1725 |
If this initialization fails
|
|
1726 |
- either because it wasn't possible to create the needed temporary table
|
|
1727 |
and its index,
|
|
1728 |
- or because of a memory allocation error,
|
|
1729 |
then we revert back to execution via the IN=>EXISTS tranformation.
|
|
1730 |
||
1731 |
The initialization of the new engine is divided in two parts - a permanent
|
|
1732 |
one that lives across prepared statements, and one that is repeated for each
|
|
1733 |
execution.
|
|
1734 |
||
1735 |
@returns
|
|
55
by brian
Update for using real bool types. |
1736 |
@retval true memory allocation error occurred
|
1737 |
@retval false an execution method was chosen successfully
|
|
1
by brian
clean slate |
1738 |
*/
|
1739 |
||
1740 |
bool Item_in_subselect::setup_engine() |
|
1741 |
{
|
|
1742 |
subselect_hash_sj_engine *new_engine= NULL; |
|
55
by brian
Update for using real bool types. |
1743 |
bool res= false; |
1
by brian
clean slate |
1744 |
|
1745 |
if (engine->engine_type() == subselect_engine::SINGLE_SELECT_ENGINE) |
|
1746 |
{
|
|
1747 |
/* Create/initialize objects in permanent memory. */
|
|
1748 |
subselect_single_select_engine *old_engine; |
|
1749 |
||
1750 |
old_engine= (subselect_single_select_engine*) engine; |
|
1751 |
||
1752 |
if (!(new_engine= new subselect_hash_sj_engine(thd, this, |
|
1753 |
old_engine)) || |
|
1754 |
new_engine->init_permanent(unit->get_unit_column_types())) |
|
1755 |
{
|
|
1756 |
Item_subselect::trans_res trans_res; |
|
1757 |
/*
|
|
1758 |
If for some reason we cannot use materialization for this IN predicate,
|
|
1759 |
delete all materialization-related objects, and apply the IN=>EXISTS
|
|
1760 |
transformation.
|
|
1761 |
*/
|
|
1762 |
delete new_engine; |
|
1763 |
new_engine= NULL; |
|
1764 |
exec_method= NOT_TRANSFORMED; |
|
1765 |
if (left_expr->cols() == 1) |
|
1766 |
trans_res= single_value_in_to_exists_transformer(old_engine->join, |
|
1767 |
&eq_creator); |
|
1768 |
else
|
|
1769 |
trans_res= row_value_in_to_exists_transformer(old_engine->join); |
|
1770 |
res= (trans_res != Item_subselect::RES_OK); |
|
1771 |
}
|
|
1772 |
if (new_engine) |
|
1773 |
engine= new_engine; |
|
1774 |
}
|
|
1775 |
else
|
|
1776 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1777 |
assert(engine->engine_type() == subselect_engine::HASH_SJ_ENGINE); |
1
by brian
clean slate |
1778 |
new_engine= (subselect_hash_sj_engine*) engine; |
1779 |
}
|
|
1780 |
||
1781 |
/* Initilizations done in runtime memory, repeated for each execution. */
|
|
1782 |
if (new_engine) |
|
1783 |
{
|
|
1784 |
/*
|
|
1785 |
Reset the LIMIT 1 set in Item_exists_subselect::fix_length_and_dec.
|
|
1786 |
TODO:
|
|
1787 |
Currently we set the subquery LIMIT to infinity, and this is correct
|
|
1788 |
because we forbid at parse time LIMIT inside IN subqueries (see
|
|
1789 |
Item_in_subselect::test_limit). However, once we allow this, here
|
|
1790 |
we should set the correct limit if given in the query.
|
|
1791 |
*/
|
|
1792 |
unit->global_parameters->select_limit= NULL; |
|
1793 |
if ((res= new_engine->init_runtime())) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1794 |
return(res); |
1
by brian
clean slate |
1795 |
}
|
1796 |
||
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1797 |
return(res); |
1
by brian
clean slate |
1798 |
}
|
1799 |
||
1800 |
||
1801 |
/**
|
|
1802 |
Initialize the cache of the left operand of the IN predicate.
|
|
1803 |
||
1804 |
@note This method has the same purpose as alloc_group_fields(),
|
|
1805 |
but it takes a different kind of collection of items, and the
|
|
1806 |
list we push to is dynamically allocated.
|
|
1807 |
||
55
by brian
Update for using real bool types. |
1808 |
@retval true if a memory allocation error occurred or the cache is
|
1
by brian
clean slate |
1809 |
not applicable to the current query
|
55
by brian
Update for using real bool types. |
1810 |
@retval false if success
|
1
by brian
clean slate |
1811 |
*/
|
1812 |
||
1813 |
bool Item_in_subselect::init_left_expr_cache() |
|
1814 |
{
|
|
1815 |
JOIN *outer_join; |
|
1816 |
Next_select_func end_select; |
|
55
by brian
Update for using real bool types. |
1817 |
bool use_result_field= false; |
1
by brian
clean slate |
1818 |
|
1819 |
outer_join= unit->outer_select()->join; |
|
1820 |
if (!outer_join || !outer_join->tables) |
|
55
by brian
Update for using real bool types. |
1821 |
return true; |
1
by brian
clean slate |
1822 |
/*
|
1823 |
If we use end_[send | write]_group to handle complete rows of the outer
|
|
1824 |
query, make the cache of the left IN operand use Item_field::result_field
|
|
1825 |
instead of Item_field::field. We need this because normally
|
|
1826 |
Cached_item_field uses Item::field to fetch field data, while
|
|
1827 |
copy_ref_key() that copies the left IN operand into a lookup key uses
|
|
1828 |
Item::result_field. In the case end_[send | write]_group result_field is
|
|
1829 |
one row behind field.
|
|
1830 |
*/
|
|
1831 |
end_select= outer_join->join_tab[outer_join->tables-1].next_select; |
|
1832 |
if (end_select == end_send_group || end_select == end_write_group) |
|
55
by brian
Update for using real bool types. |
1833 |
use_result_field= true; |
1
by brian
clean slate |
1834 |
|
1835 |
if (!(left_expr_cache= new List<Cached_item>)) |
|
55
by brian
Update for using real bool types. |
1836 |
return true; |
1
by brian
clean slate |
1837 |
|
1838 |
for (uint i= 0; i < left_expr->cols(); i++) |
|
1839 |
{
|
|
1840 |
Cached_item *cur_item_cache= new_Cached_item(thd, |
|
1841 |
left_expr->element_index(i), |
|
1842 |
use_result_field); |
|
1843 |
if (!cur_item_cache || left_expr_cache->push_front(cur_item_cache)) |
|
55
by brian
Update for using real bool types. |
1844 |
return true; |
1
by brian
clean slate |
1845 |
}
|
55
by brian
Update for using real bool types. |
1846 |
return false; |
1
by brian
clean slate |
1847 |
}
|
1848 |
||
1849 |
||
1850 |
/*
|
|
1851 |
Callback to test if an IN predicate is expensive.
|
|
1852 |
||
1853 |
@details
|
|
1854 |
IN predicates are considered expensive only if they will be executed via
|
|
1855 |
materialization. The return value affects the behavior of
|
|
1856 |
make_cond_for_table() in such a way that it is unchanged when we use
|
|
1857 |
the IN=>EXISTS transformation to compute IN.
|
|
1858 |
||
55
by brian
Update for using real bool types. |
1859 |
@retval true if the predicate is expensive
|
1860 |
@retval false otherwise
|
|
1
by brian
clean slate |
1861 |
*/
|
1862 |
||
77.1.15
by Monty Taylor
Bunch of warning cleanups. |
1863 |
bool Item_in_subselect::is_expensive_processor(uchar *arg __attribute__((__unused__))) |
1
by brian
clean slate |
1864 |
{
|
1865 |
return exec_method == MATERIALIZATION; |
|
1866 |
}
|
|
1867 |
||
1868 |
||
1869 |
Item_subselect::trans_res |
|
1870 |
Item_allany_subselect::select_transformer(JOIN *join) |
|
1871 |
{
|
|
1872 |
exec_method= IN_TO_EXISTS; |
|
1873 |
if (upper_item) |
|
1874 |
upper_item->show= 1; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1875 |
return(select_in_like_transformer(join, func)); |
1
by brian
clean slate |
1876 |
}
|
1877 |
||
1878 |
||
1879 |
void Item_allany_subselect::print(String *str, enum_query_type query_type) |
|
1880 |
{
|
|
1881 |
if (exec_method == IN_TO_EXISTS) |
|
1882 |
str->append(STRING_WITH_LEN("<exists>")); |
|
1883 |
else
|
|
1884 |
{
|
|
1885 |
left_expr->print(str, query_type); |
|
1886 |
str->append(' '); |
|
1887 |
str->append(func->symbol(all)); |
|
1888 |
str->append(all ? " all " : " any ", 5); |
|
1889 |
}
|
|
1890 |
Item_subselect::print(str, query_type); |
|
1891 |
}
|
|
1892 |
||
1893 |
||
1894 |
void subselect_engine::set_thd(THD *thd_arg) |
|
1895 |
{
|
|
1896 |
thd= thd_arg; |
|
1897 |
if (result) |
|
1898 |
result->set_thd(thd_arg); |
|
1899 |
}
|
|
1900 |
||
1901 |
||
1902 |
subselect_single_select_engine:: |
|
1903 |
subselect_single_select_engine(st_select_lex *select, |
|
1904 |
select_result_interceptor *result_arg, |
|
1905 |
Item_subselect *item_arg) |
|
1906 |
:subselect_engine(item_arg, result_arg), |
|
1907 |
prepared(0), executed(0), select_lex(select), join(0) |
|
1908 |
{
|
|
1909 |
select_lex->master_unit()->item= item_arg; |
|
1910 |
}
|
|
1911 |
||
1912 |
||
1913 |
void subselect_single_select_engine::cleanup() |
|
1914 |
{
|
|
1915 |
prepared= executed= 0; |
|
1916 |
join= 0; |
|
1917 |
result->cleanup(); |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1918 |
return; |
1
by brian
clean slate |
1919 |
}
|
1920 |
||
1921 |
||
1922 |
void subselect_union_engine::cleanup() |
|
1923 |
{
|
|
1924 |
unit->reinit_exec_mechanism(); |
|
1925 |
result->cleanup(); |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1926 |
return; |
1
by brian
clean slate |
1927 |
}
|
1928 |
||
1929 |
||
1930 |
bool subselect_union_engine::is_executed() const |
|
1931 |
{
|
|
1932 |
return unit->executed; |
|
1933 |
}
|
|
1934 |
||
1935 |
||
1936 |
/*
|
|
1937 |
Check if last execution of the subquery engine produced any rows
|
|
1938 |
||
1939 |
SYNOPSIS
|
|
1940 |
subselect_union_engine::no_rows()
|
|
1941 |
||
1942 |
DESCRIPTION
|
|
1943 |
Check if last execution of the subquery engine produced any rows. The
|
|
1944 |
return value is undefined if last execution ended in an error.
|
|
1945 |
||
1946 |
RETURN
|
|
55
by brian
Update for using real bool types. |
1947 |
true - Last subselect execution has produced no rows
|
1948 |
false - Otherwise
|
|
1
by brian
clean slate |
1949 |
*/
|
1950 |
||
1951 |
bool subselect_union_engine::no_rows() |
|
1952 |
{
|
|
1953 |
/* Check if we got any rows when reading UNION result from temp. table: */
|
|
1954 |
return test(!unit->fake_select_lex->join->send_records); |
|
1955 |
}
|
|
1956 |
||
1957 |
||
1958 |
void subselect_uniquesubquery_engine::cleanup() |
|
1959 |
{
|
|
1960 |
/* Tell handler we don't need the index anymore */
|
|
1961 |
if (tab->table->file->inited) |
|
1962 |
tab->table->file->ha_index_end(); |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1963 |
return; |
1
by brian
clean slate |
1964 |
}
|
1965 |
||
1966 |
||
1967 |
subselect_union_engine::subselect_union_engine(st_select_lex_unit *u, |
|
1968 |
select_result_interceptor *result_arg, |
|
1969 |
Item_subselect *item_arg) |
|
1970 |
:subselect_engine(item_arg, result_arg) |
|
1971 |
{
|
|
1972 |
unit= u; |
|
1973 |
unit->item= item_arg; |
|
1974 |
}
|
|
1975 |
||
1976 |
||
1977 |
/**
|
|
1978 |
Create and prepare the JOIN object that represents the query execution
|
|
1979 |
plan for the subquery.
|
|
1980 |
||
1981 |
@detail
|
|
1982 |
This method is called from Item_subselect::fix_fields. For prepared
|
|
1983 |
statements it is called both during the PREPARE and EXECUTE phases in the
|
|
1984 |
following ways:
|
|
1985 |
- During PREPARE the optimizer needs some properties
|
|
1986 |
(join->fields_list.elements) of the JOIN to proceed with preparation of
|
|
1987 |
the remaining query (namely to complete ::fix_fields for the subselect
|
|
1988 |
related classes. In the end of PREPARE the JOIN is deleted.
|
|
1989 |
- When we EXECUTE the query, Item_subselect::fix_fields is called again, and
|
|
1990 |
the JOIN object is re-created again, prepared and executed. In the end of
|
|
1991 |
execution it is deleted.
|
|
1992 |
In all cases the JOIN is created in runtime memory (not in the permanent
|
|
1993 |
memory root).
|
|
1994 |
||
1995 |
@todo
|
|
1996 |
Re-check what properties of 'join' are needed during prepare, and see if
|
|
1997 |
we can avoid creating a JOIN during JOIN::prepare of the outer join.
|
|
1998 |
||
1999 |
@retval 0 if success
|
|
2000 |
@retval 1 if error
|
|
2001 |
*/
|
|
2002 |
||
2003 |
int subselect_single_select_engine::prepare() |
|
2004 |
{
|
|
2005 |
if (prepared) |
|
2006 |
return 0; |
|
2007 |
join= new JOIN(thd, select_lex->item_list, |
|
2008 |
select_lex->options | SELECT_NO_UNLOCK, result); |
|
2009 |
if (!join || !result) |
|
2010 |
return 1; /* Fatal error is set already. */ |
|
2011 |
prepared= 1; |
|
2012 |
SELECT_LEX *save_select= thd->lex->current_select; |
|
2013 |
thd->lex->current_select= select_lex; |
|
2014 |
if (join->prepare(&select_lex->ref_pointer_array, |
|
2015 |
(TABLE_LIST*) select_lex->table_list.first, |
|
2016 |
select_lex->with_wild, |
|
2017 |
select_lex->where, |
|
2018 |
select_lex->order_list.elements + |
|
2019 |
select_lex->group_list.elements, |
|
2020 |
(ORDER*) select_lex->order_list.first, |
|
2021 |
(ORDER*) select_lex->group_list.first, |
|
2022 |
select_lex->having, |
|
2023 |
(ORDER*) 0, select_lex, |
|
2024 |
select_lex->master_unit())) |
|
2025 |
return 1; |
|
2026 |
thd->lex->current_select= save_select; |
|
2027 |
return 0; |
|
2028 |
}
|
|
2029 |
||
2030 |
int subselect_union_engine::prepare() |
|
2031 |
{
|
|
2032 |
return unit->prepare(thd, result, SELECT_NO_UNLOCK); |
|
2033 |
}
|
|
2034 |
||
2035 |
int subselect_uniquesubquery_engine::prepare() |
|
2036 |
{
|
|
2037 |
/* Should never be called. */
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2038 |
assert(false); |
1
by brian
clean slate |
2039 |
return 1; |
2040 |
}
|
|
2041 |
||
2042 |
||
2043 |
/*
|
|
2044 |
Check if last execution of the subquery engine produced any rows
|
|
2045 |
||
2046 |
SYNOPSIS
|
|
2047 |
subselect_single_select_engine::no_rows()
|
|
2048 |
||
2049 |
DESCRIPTION
|
|
2050 |
Check if last execution of the subquery engine produced any rows. The
|
|
2051 |
return value is undefined if last execution ended in an error.
|
|
2052 |
||
2053 |
RETURN
|
|
55
by brian
Update for using real bool types. |
2054 |
true - Last subselect execution has produced no rows
|
2055 |
false - Otherwise
|
|
1
by brian
clean slate |
2056 |
*/
|
2057 |
||
2058 |
bool subselect_single_select_engine::no_rows() |
|
2059 |
{
|
|
2060 |
return !item->assigned(); |
|
2061 |
}
|
|
2062 |
||
2063 |
||
2064 |
/*
|
|
2065 |
makes storage for the output values for the subquery and calcuates
|
|
2066 |
their data and column types and their nullability.
|
|
2067 |
*/
|
|
2068 |
void subselect_engine::set_row(List<Item> &item_list, Item_cache **row) |
|
2069 |
{
|
|
2070 |
Item *sel_item; |
|
2071 |
List_iterator_fast<Item> li(item_list); |
|
2072 |
res_type= STRING_RESULT; |
|
99
by Brian Aker
Second pass at removing old varchar. |
2073 |
res_field_type= MYSQL_TYPE_STRING; |
1
by brian
clean slate |
2074 |
for (uint i= 0; (sel_item= li++); i++) |
2075 |
{
|
|
2076 |
item->max_length= sel_item->max_length; |
|
2077 |
res_type= sel_item->result_type(); |
|
2078 |
res_field_type= sel_item->field_type(); |
|
2079 |
item->decimals= sel_item->decimals; |
|
2080 |
item->unsigned_flag= sel_item->unsigned_flag; |
|
2081 |
maybe_null= sel_item->maybe_null; |
|
2082 |
if (!(row[i]= Item_cache::get_cache(sel_item))) |
|
2083 |
return; |
|
2084 |
row[i]->setup(sel_item); |
|
2085 |
}
|
|
2086 |
if (item_list.elements > 1) |
|
2087 |
res_type= ROW_RESULT; |
|
2088 |
}
|
|
2089 |
||
2090 |
void subselect_single_select_engine::fix_length_and_dec(Item_cache **row) |
|
2091 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2092 |
assert(row || select_lex->item_list.elements==1); |
1
by brian
clean slate |
2093 |
set_row(select_lex->item_list, row); |
2094 |
item->collation.set(row[0]->collation); |
|
2095 |
if (cols() != 1) |
|
2096 |
maybe_null= 0; |
|
2097 |
}
|
|
2098 |
||
2099 |
void subselect_union_engine::fix_length_and_dec(Item_cache **row) |
|
2100 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2101 |
assert(row || unit->first_select()->item_list.elements==1); |
1
by brian
clean slate |
2102 |
|
2103 |
if (unit->first_select()->item_list.elements == 1) |
|
2104 |
{
|
|
2105 |
set_row(unit->types, row); |
|
2106 |
item->collation.set(row[0]->collation); |
|
2107 |
}
|
|
2108 |
else
|
|
2109 |
{
|
|
2110 |
bool maybe_null_saved= maybe_null; |
|
2111 |
set_row(unit->types, row); |
|
2112 |
maybe_null= maybe_null_saved; |
|
2113 |
}
|
|
2114 |
}
|
|
2115 |
||
77.1.15
by Monty Taylor
Bunch of warning cleanups. |
2116 |
void subselect_uniquesubquery_engine::fix_length_and_dec(Item_cache **row __attribute__((__unused__))) |
1
by brian
clean slate |
2117 |
{
|
2118 |
//this never should be called
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2119 |
assert(0); |
1
by brian
clean slate |
2120 |
}
|
2121 |
||
2122 |
int init_read_record_seq(JOIN_TAB *tab); |
|
2123 |
int join_read_always_key_or_null(JOIN_TAB *tab); |
|
2124 |
int join_read_next_same_or_null(READ_RECORD *info); |
|
2125 |
||
2126 |
int subselect_single_select_engine::exec() |
|
2127 |
{
|
|
2128 |
char const *save_where= thd->where; |
|
2129 |
SELECT_LEX *save_select= thd->lex->current_select; |
|
2130 |
thd->lex->current_select= select_lex; |
|
2131 |
if (!join->optimized) |
|
2132 |
{
|
|
2133 |
SELECT_LEX_UNIT *unit= select_lex->master_unit(); |
|
2134 |
||
2135 |
unit->set_limit(unit->global_parameters); |
|
2136 |
if (join->flatten_subqueries()) |
|
2137 |
{
|
|
55
by brian
Update for using real bool types. |
2138 |
thd->is_fatal_error= true; |
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2139 |
return(1); |
1
by brian
clean slate |
2140 |
}
|
2141 |
if (join->optimize()) |
|
2142 |
{
|
|
2143 |
thd->where= save_where; |
|
2144 |
executed= 1; |
|
2145 |
thd->lex->current_select= save_select; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2146 |
return(join->error ? join->error : 1); |
1
by brian
clean slate |
2147 |
}
|
2148 |
if (!select_lex->uncacheable && thd->lex->describe && |
|
2149 |
!(join->select_options & SELECT_DESCRIBE) && |
|
2150 |
join->need_tmp && item->const_item()) |
|
2151 |
{
|
|
2152 |
/*
|
|
2153 |
Force join->join_tmp creation, because this subquery will be replaced
|
|
2154 |
by a simple select from the materialization temp table by optimize()
|
|
2155 |
called by EXPLAIN and we need to preserve the initial query structure
|
|
2156 |
so we can display it.
|
|
2157 |
*/
|
|
2158 |
select_lex->uncacheable|= UNCACHEABLE_EXPLAIN; |
|
2159 |
select_lex->master_unit()->uncacheable|= UNCACHEABLE_EXPLAIN; |
|
2160 |
if (join->init_save_join_tab()) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2161 |
return(1); /* purecov: inspected */ |
1
by brian
clean slate |
2162 |
}
|
2163 |
if (item->engine_changed) |
|
2164 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2165 |
return(1); |
1
by brian
clean slate |
2166 |
}
|
2167 |
}
|
|
2168 |
if (select_lex->uncacheable && |
|
2169 |
select_lex->uncacheable != UNCACHEABLE_EXPLAIN |
|
2170 |
&& executed) |
|
2171 |
{
|
|
2172 |
if (join->reinit()) |
|
2173 |
{
|
|
2174 |
thd->where= save_where; |
|
2175 |
thd->lex->current_select= save_select; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2176 |
return(1); |
1
by brian
clean slate |
2177 |
}
|
2178 |
item->reset(); |
|
2179 |
item->assigned((executed= 0)); |
|
2180 |
}
|
|
2181 |
if (!executed) |
|
2182 |
{
|
|
2183 |
item->reset_value_registration(); |
|
2184 |
JOIN_TAB *changed_tabs[MAX_TABLES]; |
|
2185 |
JOIN_TAB **last_changed_tab= changed_tabs; |
|
2186 |
if (item->have_guarded_conds()) |
|
2187 |
{
|
|
2188 |
/*
|
|
2189 |
For at least one of the pushed predicates the following is true:
|
|
2190 |
We should not apply optimizations based on the condition that was
|
|
2191 |
pushed down into the subquery. Those optimizations are ref[_or_null]
|
|
2192 |
acceses. Change them to be full table scans.
|
|
2193 |
*/
|
|
2194 |
for (uint i=join->const_tables ; i < join->tables ; i++) |
|
2195 |
{
|
|
2196 |
JOIN_TAB *tab=join->join_tab+i; |
|
2197 |
if (tab && tab->keyuse) |
|
2198 |
{
|
|
2199 |
for (uint i= 0; i < tab->ref.key_parts; i++) |
|
2200 |
{
|
|
2201 |
bool *cond_guard= tab->ref.cond_guards[i]; |
|
2202 |
if (cond_guard && !*cond_guard) |
|
2203 |
{
|
|
2204 |
/* Change the access method to full table scan */
|
|
2205 |
tab->save_read_first_record= tab->read_first_record; |
|
2206 |
tab->save_read_record= tab->read_record.read_record; |
|
2207 |
tab->read_first_record= init_read_record_seq; |
|
2208 |
tab->read_record.record= tab->table->record[0]; |
|
2209 |
tab->read_record.thd= join->thd; |
|
2210 |
tab->read_record.ref_length= tab->table->file->ref_length; |
|
2211 |
*(last_changed_tab++)= tab; |
|
2212 |
break; |
|
2213 |
}
|
|
2214 |
}
|
|
2215 |
}
|
|
2216 |
}
|
|
2217 |
}
|
|
2218 |
||
2219 |
join->exec(); |
|
2220 |
||
2221 |
/* Enable the optimizations back */
|
|
2222 |
for (JOIN_TAB **ptab= changed_tabs; ptab != last_changed_tab; ptab++) |
|
2223 |
{
|
|
2224 |
JOIN_TAB *tab= *ptab; |
|
2225 |
tab->read_record.record= 0; |
|
2226 |
tab->read_record.ref_length= 0; |
|
2227 |
tab->read_first_record= tab->save_read_first_record; |
|
2228 |
tab->read_record.read_record= tab->save_read_record; |
|
2229 |
}
|
|
2230 |
executed= 1; |
|
2231 |
thd->where= save_where; |
|
2232 |
thd->lex->current_select= save_select; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2233 |
return(join->error||thd->is_fatal_error); |
1
by brian
clean slate |
2234 |
}
|
2235 |
thd->where= save_where; |
|
2236 |
thd->lex->current_select= save_select; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2237 |
return(0); |
1
by brian
clean slate |
2238 |
}
|
2239 |
||
2240 |
int subselect_union_engine::exec() |
|
2241 |
{
|
|
2242 |
char const *save_where= thd->where; |
|
2243 |
int res= unit->exec(); |
|
2244 |
thd->where= save_where; |
|
2245 |
return res; |
|
2246 |
}
|
|
2247 |
||
2248 |
||
2249 |
/*
|
|
2250 |
Search for at least one row satisfying select condition
|
|
2251 |
|
|
2252 |
SYNOPSIS
|
|
2253 |
subselect_uniquesubquery_engine::scan_table()
|
|
2254 |
||
2255 |
DESCRIPTION
|
|
2256 |
Scan the table using sequential access until we find at least one row
|
|
2257 |
satisfying select condition.
|
|
2258 |
|
|
55
by brian
Update for using real bool types. |
2259 |
The caller must set this->empty_result_set=false before calling this
|
2260 |
function. This function will set it to true if it finds a matching row.
|
|
1
by brian
clean slate |
2261 |
|
2262 |
RETURN
|
|
55
by brian
Update for using real bool types. |
2263 |
false - OK
|
2264 |
true - Error
|
|
1
by brian
clean slate |
2265 |
*/
|
2266 |
||
2267 |
int subselect_uniquesubquery_engine::scan_table() |
|
2268 |
{
|
|
2269 |
int error; |
|
2270 |
TABLE *table= tab->table; |
|
2271 |
||
2272 |
if (table->file->inited) |
|
2273 |
table->file->ha_index_end(); |
|
2274 |
||
2275 |
table->file->ha_rnd_init(1); |
|
2276 |
table->file->extra_opt(HA_EXTRA_CACHE, |
|
2277 |
current_thd->variables.read_buff_size); |
|
2278 |
table->null_row= 0; |
|
2279 |
for (;;) |
|
2280 |
{
|
|
2281 |
error=table->file->rnd_next(table->record[0]); |
|
2282 |
if (error && error != HA_ERR_END_OF_FILE) |
|
2283 |
{
|
|
2284 |
error= report_error(table, error); |
|
2285 |
break; |
|
2286 |
}
|
|
2287 |
/* No more rows */
|
|
2288 |
if (table->status) |
|
2289 |
break; |
|
2290 |
||
2291 |
if (!cond || cond->val_int()) |
|
2292 |
{
|
|
55
by brian
Update for using real bool types. |
2293 |
empty_result_set= false; |
1
by brian
clean slate |
2294 |
break; |
2295 |
}
|
|
2296 |
}
|
|
2297 |
||
2298 |
table->file->ha_rnd_end(); |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2299 |
return(error != 0); |
1
by brian
clean slate |
2300 |
}
|
2301 |
||
2302 |
||
2303 |
/*
|
|
2304 |
Copy ref key and check for null parts in it
|
|
2305 |
||
2306 |
SYNOPSIS
|
|
2307 |
subselect_uniquesubquery_engine::copy_ref_key()
|
|
2308 |
||
2309 |
DESCRIPTION
|
|
2310 |
Copy ref key and check for null parts in it.
|
|
2311 |
Depending on the nullability and conversion problems this function
|
|
2312 |
recognizes and processes the following states :
|
|
55
by brian
Update for using real bool types. |
2313 |
1. Partial match on top level. This means IN has a value of false
|
1
by brian
clean slate |
2314 |
regardless of the data in the subquery table.
|
2315 |
Detected by finding a NULL in the left IN operand of a top level
|
|
2316 |
expression.
|
|
55
by brian
Update for using real bool types. |
2317 |
We may actually skip reading the subquery, so return true to skip
|
1
by brian
clean slate |
2318 |
the table scan in subselect_uniquesubquery_engine::exec and make
|
55
by brian
Update for using real bool types. |
2319 |
the value of the IN predicate a NULL (that is equal to false on
|
1
by brian
clean slate |
2320 |
top level).
|
2321 |
2. No exact match when IN is nested inside another predicate.
|
|
2322 |
Detected by finding a NULL in the left IN operand when IN is not
|
|
2323 |
a top level predicate.
|
|
2324 |
We cannot have an exact match. But we must proceed further with a
|
|
2325 |
table scan to find out if it's a partial match (and IN has a value
|
|
55
by brian
Update for using real bool types. |
2326 |
of NULL) or no match (and IN has a value of false).
|
2327 |
So we return false to continue with the scan and see if there are
|
|
1
by brian
clean slate |
2328 |
any record that would constitute a partial match (as we cannot
|
2329 |
determine that from the index).
|
|
2330 |
3. Error converting the left IN operand to the column type of the
|
|
2331 |
right IN operand. This counts as no match (and IN has the value of
|
|
55
by brian
Update for using real bool types. |
2332 |
false). We mark the subquery table cursor as having no more rows
|
1
by brian
clean slate |
2333 |
(to ensure that the processing that follows will not find a match)
|
55
by brian
Update for using real bool types. |
2334 |
and return false, so IN is not treated as returning NULL.
|
1
by brian
clean slate |
2335 |
|
2336 |
||
2337 |
RETURN
|
|
55
by brian
Update for using real bool types. |
2338 |
false - The value of the IN predicate is not known. Proceed to find the
|
1
by brian
clean slate |
2339 |
value of the IN predicate using the determined values of
|
2340 |
null_keypart and table->status.
|
|
55
by brian
Update for using real bool types. |
2341 |
true - IN predicate has a value of NULL. Stop the processing right there
|
1
by brian
clean slate |
2342 |
and return NULL to the outer predicates.
|
2343 |
*/
|
|
2344 |
||
2345 |
bool subselect_uniquesubquery_engine::copy_ref_key() |
|
2346 |
{
|
|
2347 |
for (store_key **copy= tab->ref.key_copy ; *copy ; copy++) |
|
2348 |
{
|
|
2349 |
enum store_key::store_key_result store_res; |
|
2350 |
store_res= (*copy)->copy(); |
|
2351 |
tab->ref.key_err= store_res; |
|
2352 |
||
2353 |
/*
|
|
2354 |
When there is a NULL part in the key we don't need to make index
|
|
2355 |
lookup for such key thus we don't need to copy whole key.
|
|
2356 |
If we later should do a sequential scan return OK. Fail otherwise.
|
|
2357 |
||
2358 |
See also the comment for the subselect_uniquesubquery_engine::exec()
|
|
2359 |
function.
|
|
2360 |
*/
|
|
2361 |
null_keypart= (*copy)->null_key; |
|
2362 |
if (null_keypart) |
|
2363 |
{
|
|
2364 |
bool top_level= ((Item_in_subselect *) item)->is_top_level_item(); |
|
2365 |
if (top_level) |
|
2366 |
{
|
|
2367 |
/* Partial match on top level */
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2368 |
return(1); |
1
by brian
clean slate |
2369 |
}
|
2370 |
else
|
|
2371 |
{
|
|
2372 |
/* No exact match when IN is nested inside another predicate */
|
|
2373 |
break; |
|
2374 |
}
|
|
2375 |
}
|
|
2376 |
||
2377 |
/*
|
|
2378 |
Check if the error is equal to STORE_KEY_FATAL. This is not expressed
|
|
2379 |
using the store_key::store_key_result enum because ref.key_err is a
|
|
55
by brian
Update for using real bool types. |
2380 |
boolean and we want to detect both true and STORE_KEY_FATAL from the
|
2381 |
space of the union of the values of [true, false] and
|
|
1
by brian
clean slate |
2382 |
store_key::store_key_result.
|
2383 |
TODO: fix the variable an return types.
|
|
2384 |
*/
|
|
2385 |
if (store_res == store_key::STORE_KEY_FATAL) |
|
2386 |
{
|
|
2387 |
/*
|
|
2388 |
Error converting the left IN operand to the column type of the right
|
|
2389 |
IN operand.
|
|
2390 |
*/
|
|
2391 |
tab->table->status= STATUS_NOT_FOUND; |
|
2392 |
break; |
|
2393 |
}
|
|
2394 |
}
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2395 |
return(0); |
1
by brian
clean slate |
2396 |
}
|
2397 |
||
2398 |
||
2399 |
/*
|
|
2400 |
Execute subselect
|
|
2401 |
||
2402 |
SYNOPSIS
|
|
2403 |
subselect_uniquesubquery_engine::exec()
|
|
2404 |
||
2405 |
DESCRIPTION
|
|
2406 |
Find rows corresponding to the ref key using index access.
|
|
2407 |
If some part of the lookup key is NULL, then we're evaluating
|
|
2408 |
NULL IN (SELECT ... )
|
|
2409 |
This is a special case, we don't need to search for NULL in the table,
|
|
2410 |
instead, the result value is
|
|
2411 |
- NULL if select produces empty row set
|
|
55
by brian
Update for using real bool types. |
2412 |
- false otherwise.
|
1
by brian
clean slate |
2413 |
|
55
by brian
Update for using real bool types. |
2414 |
In some cases (IN subselect is a top level item, i.e. abort_on_null==true)
|
2415 |
the caller doesn't distinguish between NULL and false result and we just
|
|
2416 |
return false.
|
|
1
by brian
clean slate |
2417 |
Otherwise we make a full table scan to see if there is at least one
|
2418 |
matching row.
|
|
2419 |
|
|
2420 |
The result of this function (info about whether a row was found) is
|
|
2421 |
stored in this->empty_result_set.
|
|
2422 |
NOTE
|
|
2423 |
|
|
2424 |
RETURN
|
|
55
by brian
Update for using real bool types. |
2425 |
false - ok
|
2426 |
true - an error occured while scanning
|
|
1
by brian
clean slate |
2427 |
*/
|
2428 |
||
2429 |
int subselect_uniquesubquery_engine::exec() |
|
2430 |
{
|
|
2431 |
int error; |
|
2432 |
TABLE *table= tab->table; |
|
55
by brian
Update for using real bool types. |
2433 |
empty_result_set= true; |
1
by brian
clean slate |
2434 |
table->status= 0; |
2435 |
||
2436 |
/* TODO: change to use of 'full_scan' here? */
|
|
2437 |
if (copy_ref_key()) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2438 |
return(1); |
1
by brian
clean slate |
2439 |
if (table->status) |
2440 |
{
|
|
2441 |
/*
|
|
2442 |
We know that there will be no rows even if we scan.
|
|
2443 |
Can be set in copy_ref_key.
|
|
2444 |
*/
|
|
2445 |
((Item_in_subselect *) item)->value= 0; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2446 |
return(0); |
1
by brian
clean slate |
2447 |
}
|
2448 |
||
2449 |
if (null_keypart) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2450 |
return(scan_table()); |
1
by brian
clean slate |
2451 |
|
2452 |
if (!table->file->inited) |
|
2453 |
table->file->ha_index_init(tab->ref.key, 0); |
|
2454 |
error= table->file->index_read_map(table->record[0], |
|
2455 |
tab->ref.key_buff, |
|
2456 |
make_prev_keypart_map(tab->ref.key_parts), |
|
2457 |
HA_READ_KEY_EXACT); |
|
2458 |
if (error && |
|
2459 |
error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE) |
|
2460 |
error= report_error(table, error); |
|
2461 |
else
|
|
2462 |
{
|
|
2463 |
error= 0; |
|
2464 |
table->null_row= 0; |
|
2465 |
if (!table->status && (!cond || cond->val_int())) |
|
2466 |
{
|
|
2467 |
((Item_in_subselect *) item)->value= 1; |
|
55
by brian
Update for using real bool types. |
2468 |
empty_result_set= false; |
1
by brian
clean slate |
2469 |
}
|
2470 |
else
|
|
2471 |
((Item_in_subselect *) item)->value= 0; |
|
2472 |
}
|
|
2473 |
||
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2474 |
return(error != 0); |
1
by brian
clean slate |
2475 |
}
|
2476 |
||
2477 |
||
2478 |
/*
|
|
2479 |
Index-lookup subselect 'engine' - run the subquery
|
|
2480 |
||
2481 |
SYNOPSIS
|
|
2482 |
subselect_indexsubquery_engine:exec()
|
|
2483 |
full_scan
|
|
2484 |
||
2485 |
DESCRIPTION
|
|
2486 |
The engine is used to resolve subqueries in form
|
|
2487 |
||
2488 |
oe IN (SELECT key FROM tbl WHERE subq_where)
|
|
2489 |
||
2490 |
The value of the predicate is calculated as follows:
|
|
2491 |
1. If oe IS NULL, this is a special case, do a full table scan on
|
|
2492 |
table tbl and search for row that satisfies subq_where. If such
|
|
55
by brian
Update for using real bool types. |
2493 |
row is found, return NULL, otherwise return false.
|
1
by brian
clean slate |
2494 |
2. Make an index lookup via key=oe, search for a row that satisfies
|
55
by brian
Update for using real bool types. |
2495 |
subq_where. If found, return true.
|
2496 |
3. If check_null==true, make another lookup via key=NULL, search for a
|
|
1
by brian
clean slate |
2497 |
row that satisfies subq_where. If found, return NULL, otherwise
|
55
by brian
Update for using real bool types. |
2498 |
return false.
|
1
by brian
clean slate |
2499 |
|
2500 |
TODO
|
|
2501 |
The step #1 can be optimized further when the index has several key
|
|
2502 |
parts. Consider a subquery:
|
|
2503 |
|
|
2504 |
(oe1, oe2) IN (SELECT keypart1, keypart2 FROM tbl WHERE subq_where)
|
|
2505 |
||
2506 |
and suppose we need to evaluate it for {oe1, oe2}=={const1, NULL}.
|
|
2507 |
Current code will do a full table scan and obtain correct result. There
|
|
2508 |
is a better option: instead of evaluating
|
|
2509 |
||
2510 |
SELECT keypart1, keypart2 FROM tbl WHERE subq_where (1)
|
|
2511 |
||
2512 |
and checking if it has produced any matching rows, evaluate
|
|
2513 |
|
|
2514 |
SELECT keypart2 FROM tbl WHERE subq_where AND keypart1=const1 (2)
|
|
2515 |
||
2516 |
If this query produces a row, the result is NULL (as we're evaluating
|
|
2517 |
"(const1, NULL) IN { (const1, X), ... }", which has a value of UNKNOWN,
|
|
55
by brian
Update for using real bool types. |
2518 |
i.e. NULL). If the query produces no rows, the result is false.
|
1
by brian
clean slate |
2519 |
|
2520 |
We currently evaluate (1) by doing a full table scan. (2) can be
|
|
2521 |
evaluated by doing a "ref" scan on "keypart1=const1", which can be much
|
|
2522 |
cheaper. We can use index statistics to quickly check whether "ref" scan
|
|
2523 |
will be cheaper than full table scan.
|
|
2524 |
||
2525 |
RETURN
|
|
2526 |
0
|
|
2527 |
1
|
|
2528 |
*/
|
|
2529 |
||
2530 |
int subselect_indexsubquery_engine::exec() |
|
2531 |
{
|
|
2532 |
int error; |
|
2533 |
bool null_finding= 0; |
|
2534 |
TABLE *table= tab->table; |
|
2535 |
||
2536 |
((Item_in_subselect *) item)->value= 0; |
|
55
by brian
Update for using real bool types. |
2537 |
empty_result_set= true; |
1
by brian
clean slate |
2538 |
null_keypart= 0; |
2539 |
table->status= 0; |
|
2540 |
||
2541 |
if (check_null) |
|
2542 |
{
|
|
2543 |
/* We need to check for NULL if there wasn't a matching value */
|
|
2544 |
*tab->ref.null_ref_key= 0; // Search first for not null |
|
2545 |
((Item_in_subselect *) item)->was_null= 0; |
|
2546 |
}
|
|
2547 |
||
2548 |
/* Copy the ref key and check for nulls... */
|
|
2549 |
if (copy_ref_key()) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2550 |
return(1); |
1
by brian
clean slate |
2551 |
|
2552 |
if (table->status) |
|
2553 |
{
|
|
2554 |
/*
|
|
2555 |
We know that there will be no rows even if we scan.
|
|
2556 |
Can be set in copy_ref_key.
|
|
2557 |
*/
|
|
2558 |
((Item_in_subselect *) item)->value= 0; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2559 |
return(0); |
1
by brian
clean slate |
2560 |
}
|
2561 |
||
2562 |
if (null_keypart) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2563 |
return(scan_table()); |
1
by brian
clean slate |
2564 |
|
2565 |
if (!table->file->inited) |
|
2566 |
table->file->ha_index_init(tab->ref.key, 1); |
|
2567 |
error= table->file->index_read_map(table->record[0], |
|
2568 |
tab->ref.key_buff, |
|
2569 |
make_prev_keypart_map(tab->ref.key_parts), |
|
2570 |
HA_READ_KEY_EXACT); |
|
2571 |
if (error && |
|
2572 |
error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE) |
|
2573 |
error= report_error(table, error); |
|
2574 |
else
|
|
2575 |
{
|
|
2576 |
for (;;) |
|
2577 |
{
|
|
2578 |
error= 0; |
|
2579 |
table->null_row= 0; |
|
2580 |
if (!table->status) |
|
2581 |
{
|
|
2582 |
if ((!cond || cond->val_int()) && (!having || having->val_int())) |
|
2583 |
{
|
|
55
by brian
Update for using real bool types. |
2584 |
empty_result_set= false; |
1
by brian
clean slate |
2585 |
if (null_finding) |
2586 |
((Item_in_subselect *) item)->was_null= 1; |
|
2587 |
else
|
|
2588 |
((Item_in_subselect *) item)->value= 1; |
|
2589 |
break; |
|
2590 |
}
|
|
2591 |
error= table->file->index_next_same(table->record[0], |
|
2592 |
tab->ref.key_buff, |
|
2593 |
tab->ref.key_length); |
|
2594 |
if (error && error != HA_ERR_END_OF_FILE) |
|
2595 |
{
|
|
2596 |
error= report_error(table, error); |
|
2597 |
break; |
|
2598 |
}
|
|
2599 |
}
|
|
2600 |
else
|
|
2601 |
{
|
|
2602 |
if (!check_null || null_finding) |
|
2603 |
break; /* We don't need to check nulls */ |
|
2604 |
*tab->ref.null_ref_key= 1; |
|
2605 |
null_finding= 1; |
|
2606 |
/* Check if there exists a row with a null value in the index */
|
|
2607 |
if ((error= (safe_index_read(tab) == 1))) |
|
2608 |
break; |
|
2609 |
}
|
|
2610 |
}
|
|
2611 |
}
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2612 |
return(error != 0); |
1
by brian
clean slate |
2613 |
}
|
2614 |
||
2615 |
||
2616 |
uint subselect_single_select_engine::cols() |
|
2617 |
{
|
|
2618 |
return select_lex->item_list.elements; |
|
2619 |
}
|
|
2620 |
||
2621 |
||
2622 |
uint subselect_union_engine::cols() |
|
2623 |
{
|
|
2624 |
return unit->types.elements; |
|
2625 |
}
|
|
2626 |
||
2627 |
||
2628 |
uint8 subselect_single_select_engine::uncacheable() |
|
2629 |
{
|
|
2630 |
return select_lex->uncacheable; |
|
2631 |
}
|
|
2632 |
||
2633 |
||
2634 |
uint8 subselect_union_engine::uncacheable() |
|
2635 |
{
|
|
2636 |
return unit->uncacheable; |
|
2637 |
}
|
|
2638 |
||
2639 |
||
2640 |
void subselect_single_select_engine::exclude() |
|
2641 |
{
|
|
2642 |
select_lex->master_unit()->exclude_level(); |
|
2643 |
}
|
|
2644 |
||
2645 |
void subselect_union_engine::exclude() |
|
2646 |
{
|
|
2647 |
unit->exclude_level(); |
|
2648 |
}
|
|
2649 |
||
2650 |
||
2651 |
void subselect_uniquesubquery_engine::exclude() |
|
2652 |
{
|
|
2653 |
//this never should be called
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2654 |
assert(0); |
1
by brian
clean slate |
2655 |
}
|
2656 |
||
2657 |
||
2658 |
table_map subselect_engine::calc_const_tables(TABLE_LIST *table) |
|
2659 |
{
|
|
2660 |
table_map map= 0; |
|
2661 |
for (; table; table= table->next_leaf) |
|
2662 |
{
|
|
2663 |
TABLE *tbl= table->table; |
|
2664 |
if (tbl && tbl->const_table) |
|
2665 |
map|= tbl->map; |
|
2666 |
}
|
|
2667 |
return map; |
|
2668 |
}
|
|
2669 |
||
2670 |
||
2671 |
table_map subselect_single_select_engine::upper_select_const_tables() |
|
2672 |
{
|
|
2673 |
return calc_const_tables((TABLE_LIST *) select_lex->outer_select()-> |
|
2674 |
leaf_tables); |
|
2675 |
}
|
|
2676 |
||
2677 |
||
2678 |
table_map subselect_union_engine::upper_select_const_tables() |
|
2679 |
{
|
|
2680 |
return calc_const_tables((TABLE_LIST *) unit->outer_select()->leaf_tables); |
|
2681 |
}
|
|
2682 |
||
2683 |
||
2684 |
void subselect_single_select_engine::print(String *str, |
|
2685 |
enum_query_type query_type) |
|
2686 |
{
|
|
2687 |
select_lex->print(thd, str, query_type); |
|
2688 |
}
|
|
2689 |
||
2690 |
||
2691 |
void subselect_union_engine::print(String *str, enum_query_type query_type) |
|
2692 |
{
|
|
2693 |
unit->print(str, query_type); |
|
2694 |
}
|
|
2695 |
||
2696 |
||
2697 |
void subselect_uniquesubquery_engine::print(String *str, |
|
2698 |
enum_query_type query_type) |
|
2699 |
{
|
|
2700 |
char *table_name= tab->table->s->table_name.str; |
|
2701 |
str->append(STRING_WITH_LEN("<primary_index_lookup>(")); |
|
2702 |
tab->ref.items[0]->print(str, query_type); |
|
2703 |
str->append(STRING_WITH_LEN(" in ")); |
|
2704 |
if (tab->table->s->table_category == TABLE_CATEGORY_TEMPORARY) |
|
2705 |
{
|
|
2706 |
/*
|
|
2707 |
Temporary tables' names change across runs, so they can't be used for
|
|
2708 |
EXPLAIN EXTENDED.
|
|
2709 |
*/
|
|
2710 |
str->append(STRING_WITH_LEN("<temporary table>")); |
|
2711 |
}
|
|
2712 |
else
|
|
2713 |
str->append(table_name, tab->table->s->table_name.length); |
|
2714 |
KEY *key_info= tab->table->key_info+ tab->ref.key; |
|
2715 |
str->append(STRING_WITH_LEN(" on ")); |
|
2716 |
str->append(key_info->name); |
|
2717 |
if (cond) |
|
2718 |
{
|
|
2719 |
str->append(STRING_WITH_LEN(" where ")); |
|
2720 |
cond->print(str, query_type); |
|
2721 |
}
|
|
2722 |
str->append(')'); |
|
2723 |
}
|
|
2724 |
||
2725 |
/*
|
|
2726 |
TODO:
|
|
2727 |
The above ::print method should be changed as below. Do it after
|
|
2728 |
all other tests pass.
|
|
2729 |
||
2730 |
void subselect_uniquesubquery_engine::print(String *str)
|
|
2731 |
{
|
|
2732 |
KEY *key_info= tab->table->key_info + tab->ref.key;
|
|
2733 |
str->append(STRING_WITH_LEN("<primary_index_lookup>("));
|
|
2734 |
for (uint i= 0; i < key_info->key_parts; i++)
|
|
2735 |
tab->ref.items[i]->print(str);
|
|
2736 |
str->append(STRING_WITH_LEN(" in "));
|
|
2737 |
str->append(tab->table->s->table_name.str, tab->table->s->table_name.length);
|
|
2738 |
str->append(STRING_WITH_LEN(" on "));
|
|
2739 |
str->append(key_info->name);
|
|
2740 |
if (cond)
|
|
2741 |
{
|
|
2742 |
str->append(STRING_WITH_LEN(" where "));
|
|
2743 |
cond->print(str);
|
|
2744 |
}
|
|
2745 |
str->append(')');
|
|
2746 |
}
|
|
2747 |
*/
|
|
2748 |
||
2749 |
void subselect_indexsubquery_engine::print(String *str, |
|
2750 |
enum_query_type query_type) |
|
2751 |
{
|
|
2752 |
str->append(STRING_WITH_LEN("<index_lookup>(")); |
|
2753 |
tab->ref.items[0]->print(str, query_type); |
|
2754 |
str->append(STRING_WITH_LEN(" in ")); |
|
2755 |
str->append(tab->table->s->table_name.str, tab->table->s->table_name.length); |
|
2756 |
KEY *key_info= tab->table->key_info+ tab->ref.key; |
|
2757 |
str->append(STRING_WITH_LEN(" on ")); |
|
2758 |
str->append(key_info->name); |
|
2759 |
if (check_null) |
|
2760 |
str->append(STRING_WITH_LEN(" checking NULL")); |
|
2761 |
if (cond) |
|
2762 |
{
|
|
2763 |
str->append(STRING_WITH_LEN(" where ")); |
|
2764 |
cond->print(str, query_type); |
|
2765 |
}
|
|
2766 |
if (having) |
|
2767 |
{
|
|
2768 |
str->append(STRING_WITH_LEN(" having ")); |
|
2769 |
having->print(str, query_type); |
|
2770 |
}
|
|
2771 |
str->append(')'); |
|
2772 |
}
|
|
2773 |
||
2774 |
/**
|
|
2775 |
change select_result object of engine.
|
|
2776 |
||
2777 |
@param si new subselect Item
|
|
2778 |
@param res new select_result object
|
|
2779 |
||
2780 |
@retval
|
|
55
by brian
Update for using real bool types. |
2781 |
false OK
|
1
by brian
clean slate |
2782 |
@retval
|
55
by brian
Update for using real bool types. |
2783 |
true error
|
1
by brian
clean slate |
2784 |
*/
|
2785 |
||
2786 |
bool subselect_single_select_engine::change_result(Item_subselect *si, |
|
2787 |
select_result_interceptor *res) |
|
2788 |
{
|
|
2789 |
item= si; |
|
2790 |
result= res; |
|
2791 |
return select_lex->join->change_result(result); |
|
2792 |
}
|
|
2793 |
||
2794 |
||
2795 |
/**
|
|
2796 |
change select_result object of engine.
|
|
2797 |
||
2798 |
@param si new subselect Item
|
|
2799 |
@param res new select_result object
|
|
2800 |
||
2801 |
@retval
|
|
55
by brian
Update for using real bool types. |
2802 |
false OK
|
1
by brian
clean slate |
2803 |
@retval
|
55
by brian
Update for using real bool types. |
2804 |
true error
|
1
by brian
clean slate |
2805 |
*/
|
2806 |
||
2807 |
bool subselect_union_engine::change_result(Item_subselect *si, |
|
2808 |
select_result_interceptor *res) |
|
2809 |
{
|
|
2810 |
item= si; |
|
2811 |
int rc= unit->change_result(res, result); |
|
2812 |
result= res; |
|
2813 |
return rc; |
|
2814 |
}
|
|
2815 |
||
2816 |
||
2817 |
/**
|
|
2818 |
change select_result emulation, never should be called.
|
|
2819 |
||
2820 |
@param si new subselect Item
|
|
2821 |
@param res new select_result object
|
|
2822 |
||
2823 |
@retval
|
|
55
by brian
Update for using real bool types. |
2824 |
false OK
|
1
by brian
clean slate |
2825 |
@retval
|
55
by brian
Update for using real bool types. |
2826 |
true error
|
1
by brian
clean slate |
2827 |
*/
|
2828 |
||
77.1.15
by Monty Taylor
Bunch of warning cleanups. |
2829 |
bool subselect_uniquesubquery_engine::change_result(Item_subselect *si __attribute__((__unused__)), |
2830 |
select_result_interceptor *res __attribute__((__unused__))) |
|
1
by brian
clean slate |
2831 |
{
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2832 |
assert(0); |
55
by brian
Update for using real bool types. |
2833 |
return true; |
1
by brian
clean slate |
2834 |
}
|
2835 |
||
2836 |
||
2837 |
/**
|
|
2838 |
Report about presence of tables in subquery.
|
|
2839 |
||
2840 |
@retval
|
|
55
by brian
Update for using real bool types. |
2841 |
true there are not tables used in subquery
|
1
by brian
clean slate |
2842 |
@retval
|
55
by brian
Update for using real bool types. |
2843 |
false there are some tables in subquery
|
1
by brian
clean slate |
2844 |
*/
|
2845 |
bool subselect_single_select_engine::no_tables() |
|
2846 |
{
|
|
2847 |
return(select_lex->table_list.elements == 0); |
|
2848 |
}
|
|
2849 |
||
2850 |
||
2851 |
/*
|
|
2852 |
Check statically whether the subquery can return NULL
|
|
2853 |
||
2854 |
SINOPSYS
|
|
2855 |
subselect_single_select_engine::may_be_null()
|
|
2856 |
||
2857 |
RETURN
|
|
55
by brian
Update for using real bool types. |
2858 |
false can guarantee that the subquery never return NULL
|
2859 |
true otherwise
|
|
1
by brian
clean slate |
2860 |
*/
|
2861 |
bool subselect_single_select_engine::may_be_null() |
|
2862 |
{
|
|
2863 |
return ((no_tables() && !join->conds && !join->having) ? maybe_null : 1); |
|
2864 |
}
|
|
2865 |
||
2866 |
||
2867 |
/**
|
|
2868 |
Report about presence of tables in subquery.
|
|
2869 |
||
2870 |
@retval
|
|
55
by brian
Update for using real bool types. |
2871 |
true there are not tables used in subquery
|
1
by brian
clean slate |
2872 |
@retval
|
55
by brian
Update for using real bool types. |
2873 |
false there are some tables in subquery
|
1
by brian
clean slate |
2874 |
*/
|
2875 |
bool subselect_union_engine::no_tables() |
|
2876 |
{
|
|
2877 |
for (SELECT_LEX *sl= unit->first_select(); sl; sl= sl->next_select()) |
|
2878 |
{
|
|
2879 |
if (sl->table_list.elements) |
|
55
by brian
Update for using real bool types. |
2880 |
return false; |
1
by brian
clean slate |
2881 |
}
|
55
by brian
Update for using real bool types. |
2882 |
return true; |
1
by brian
clean slate |
2883 |
}
|
2884 |
||
2885 |
||
2886 |
/**
|
|
2887 |
Report about presence of tables in subquery.
|
|
2888 |
||
2889 |
@retval
|
|
55
by brian
Update for using real bool types. |
2890 |
true there are not tables used in subquery
|
1
by brian
clean slate |
2891 |
@retval
|
55
by brian
Update for using real bool types. |
2892 |
false there are some tables in subquery
|
1
by brian
clean slate |
2893 |
*/
|
2894 |
||
2895 |
bool subselect_uniquesubquery_engine::no_tables() |
|
2896 |
{
|
|
2897 |
/* returning value is correct, but this method should never be called */
|
|
2898 |
return 0; |
|
2899 |
}
|
|
2900 |
||
2901 |
||
2902 |
/******************************************************************************
|
|
2903 |
WL#1110 - Implementation of class subselect_hash_sj_engine
|
|
2904 |
******************************************************************************/
|
|
2905 |
||
2906 |
||
2907 |
/**
|
|
2908 |
Create all structures needed for IN execution that can live between PS
|
|
2909 |
reexecution.
|
|
2910 |
||
2911 |
@detail
|
|
2912 |
- Create a temporary table to store the result of the IN subquery. The
|
|
2913 |
temporary table has one hash index on all its columns.
|
|
2914 |
- Create a new result sink that sends the result stream of the subquery to
|
|
2915 |
the temporary table,
|
|
2916 |
- Create and initialize a new JOIN_TAB, and TABLE_REF objects to perform
|
|
2917 |
lookups into the indexed temporary table.
|
|
2918 |
||
2919 |
@notice:
|
|
2920 |
Currently Item_subselect::init() already chooses and creates at parse
|
|
2921 |
time an engine with a corresponding JOIN to execute the subquery.
|
|
2922 |
||
55
by brian
Update for using real bool types. |
2923 |
@retval true if error
|
2924 |
@retval false otherwise
|
|
1
by brian
clean slate |
2925 |
*/
|
2926 |
||
2927 |
bool subselect_hash_sj_engine::init_permanent(List<Item> *tmp_columns) |
|
2928 |
{
|
|
2929 |
/* The result sink where we will materialize the subquery result. */
|
|
2930 |
select_union *tmp_result_sink; |
|
2931 |
/* The table into which the subquery is materialized. */
|
|
2932 |
TABLE *tmp_table; |
|
2933 |
KEY *tmp_key; /* The only index on the temporary table. */ |
|
2934 |
uint tmp_key_parts; /* Number of keyparts in tmp_key. */ |
|
2935 |
Item_in_subselect *item_in= (Item_in_subselect *) item; |
|
2936 |
||
2937 |
/* 1. Create/initialize materialization related objects. */
|
|
2938 |
||
2939 |
/*
|
|
2940 |
Create and initialize a select result interceptor that stores the
|
|
2941 |
result stream in a temporary table. The temporary table itself is
|
|
2942 |
managed (created/filled/etc) internally by the interceptor.
|
|
2943 |
*/
|
|
2944 |
if (!(tmp_result_sink= new select_union)) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2945 |
return(true); |
1
by brian
clean slate |
2946 |
if (tmp_result_sink->create_result_table( |
55
by brian
Update for using real bool types. |
2947 |
thd, tmp_columns, true, |
1
by brian
clean slate |
2948 |
thd->options | TMP_TABLE_ALL_COLUMNS, |
55
by brian
Update for using real bool types. |
2949 |
"materialized subselect", true)) |
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2950 |
return(true); |
1
by brian
clean slate |
2951 |
|
2952 |
tmp_table= tmp_result_sink->table; |
|
2953 |
tmp_key= tmp_table->key_info; |
|
2954 |
tmp_key_parts= tmp_key->key_parts; |
|
2955 |
||
2956 |
/*
|
|
2957 |
If the subquery has blobs, or the total key lenght is bigger than some
|
|
2958 |
length, then the created index cannot be used for lookups and we
|
|
2959 |
can't use hash semi join. If this is the case, delete the temporary
|
|
2960 |
table since it will not be used, and tell the caller we failed to
|
|
2961 |
initialize the engine.
|
|
2962 |
*/
|
|
2963 |
if (tmp_table->s->keys == 0) |
|
2964 |
{
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2965 |
assert(tmp_table->s->db_type() == myisam_hton); |
2966 |
assert( |
|
1
by brian
clean slate |
2967 |
tmp_table->s->uniques || |
2968 |
tmp_table->key_info->key_length >= tmp_table->file->max_key_length() || |
|
2969 |
tmp_table->key_info->key_parts > tmp_table->file->max_key_parts()); |
|
2970 |
free_tmp_table(thd, tmp_table); |
|
2971 |
delete result; |
|
2972 |
result= NULL; |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2973 |
return(true); |
1
by brian
clean slate |
2974 |
}
|
2975 |
result= tmp_result_sink; |
|
2976 |
||
2977 |
/*
|
|
2978 |
Make sure there is only one index on the temp table, and it doesn't have
|
|
2979 |
the extra key part created when s->uniques > 0.
|
|
2980 |
*/
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2981 |
assert(tmp_table->s->keys == 1 && tmp_columns->elements == tmp_key_parts); |
1
by brian
clean slate |
2982 |
|
2983 |
||
2984 |
/* 2. Create/initialize execution related objects. */
|
|
2985 |
||
2986 |
/*
|
|
2987 |
Create and initialize the JOIN_TAB that represents an index lookup
|
|
2988 |
plan operator into the materialized subquery result. Notice that:
|
|
2989 |
- this JOIN_TAB has no corresponding JOIN (and doesn't need one), and
|
|
2990 |
- here we initialize only those members that are used by
|
|
2991 |
subselect_uniquesubquery_engine, so these objects are incomplete.
|
|
2992 |
*/
|
|
2993 |
if (!(tab= (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)))) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
2994 |
return(true); |
1
by brian
clean slate |
2995 |
tab->table= tmp_table; |
2996 |
tab->ref.key= 0; /* The only temp table index. */ |
|
2997 |
tab->ref.key_length= tmp_key->key_length; |
|
2998 |
if (!(tab->ref.key_buff= |
|
2999 |
(uchar*) thd->calloc(ALIGN_SIZE(tmp_key->key_length) * 2)) || |
|
3000 |
!(tab->ref.key_copy= |
|
3001 |
(store_key**) thd->alloc((sizeof(store_key*) * |
|
3002 |
(tmp_key_parts + 1)))) || |
|
3003 |
!(tab->ref.items= |
|
3004 |
(Item**) thd->alloc(sizeof(Item*) * tmp_key_parts))) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
3005 |
return(true); |
1
by brian
clean slate |
3006 |
|
3007 |
KEY_PART_INFO *cur_key_part= tmp_key->key_part; |
|
3008 |
store_key **ref_key= tab->ref.key_copy; |
|
3009 |
uchar *cur_ref_buff= tab->ref.key_buff; |
|
3010 |
||
3011 |
for (uint i= 0; i < tmp_key_parts; i++, cur_key_part++, ref_key++) |
|
3012 |
{
|
|
3013 |
tab->ref.items[i]= item_in->left_expr->element_index(i); |
|
3014 |
int null_count= test(cur_key_part->field->real_maybe_null()); |
|
3015 |
*ref_key= new store_key_item(thd, cur_key_part->field, |
|
3016 |
/* TODO:
|
|
3017 |
the NULL byte is taken into account in
|
|
3018 |
cur_key_part->store_length, so instead of
|
|
3019 |
cur_ref_buff + test(maybe_null), we could
|
|
3020 |
use that information instead.
|
|
3021 |
*/
|
|
3022 |
cur_ref_buff + null_count, |
|
3023 |
null_count ? tab->ref.key_buff : 0, |
|
3024 |
cur_key_part->length, tab->ref.items[i]); |
|
3025 |
cur_ref_buff+= cur_key_part->store_length; |
|
3026 |
}
|
|
3027 |
*ref_key= NULL; /* End marker. */ |
|
3028 |
tab->ref.key_err= 1; |
|
3029 |
tab->ref.key_parts= tmp_key_parts; |
|
3030 |
||
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
3031 |
return(false); |
1
by brian
clean slate |
3032 |
}
|
3033 |
||
3034 |
||
3035 |
/**
|
|
3036 |
Initialize members of the engine that need to be re-initilized at each
|
|
3037 |
execution.
|
|
3038 |
||
55
by brian
Update for using real bool types. |
3039 |
@retval true if a memory allocation error occurred
|
3040 |
@retval false if success
|
|
1
by brian
clean slate |
3041 |
*/
|
3042 |
||
3043 |
bool subselect_hash_sj_engine::init_runtime() |
|
3044 |
{
|
|
3045 |
/*
|
|
3046 |
Create and optimize the JOIN that will be used to materialize
|
|
3047 |
the subquery if not yet created.
|
|
3048 |
*/
|
|
3049 |
materialize_engine->prepare(); |
|
3050 |
/* Let our engine reuse this query plan for materialization. */
|
|
3051 |
materialize_join= materialize_engine->join; |
|
3052 |
materialize_join->change_result(result); |
|
55
by brian
Update for using real bool types. |
3053 |
return false; |
1
by brian
clean slate |
3054 |
}
|
3055 |
||
3056 |
||
3057 |
subselect_hash_sj_engine::~subselect_hash_sj_engine() |
|
3058 |
{
|
|
3059 |
delete result; |
|
3060 |
if (tab) |
|
3061 |
free_tmp_table(thd, tab->table); |
|
3062 |
}
|
|
3063 |
||
3064 |
||
3065 |
/**
|
|
3066 |
Cleanup performed after each PS execution.
|
|
3067 |
||
3068 |
@detail
|
|
3069 |
Called in the end of JOIN::prepare for PS from Item_subselect::cleanup.
|
|
3070 |
*/
|
|
3071 |
||
3072 |
void subselect_hash_sj_engine::cleanup() |
|
3073 |
{
|
|
55
by brian
Update for using real bool types. |
3074 |
is_materialized= false; |
1
by brian
clean slate |
3075 |
result->cleanup(); /* Resets the temp table as well. */ |
3076 |
materialize_engine->cleanup(); |
|
3077 |
subselect_uniquesubquery_engine::cleanup(); |
|
3078 |
}
|
|
3079 |
||
3080 |
||
3081 |
/**
|
|
3082 |
Execute a subquery IN predicate via materialization.
|
|
3083 |
||
3084 |
@detail
|
|
3085 |
If needed materialize the subquery into a temporary table, then
|
|
3086 |
copmpute the predicate via a lookup into this table.
|
|
3087 |
||
55
by brian
Update for using real bool types. |
3088 |
@retval true if error
|
3089 |
@retval false otherwise
|
|
1
by brian
clean slate |
3090 |
*/
|
3091 |
||
3092 |
int subselect_hash_sj_engine::exec() |
|
3093 |
{
|
|
3094 |
Item_in_subselect *item_in= (Item_in_subselect *) item; |
|
3095 |
||
3096 |
/*
|
|
3097 |
Optimize and materialize the subquery during the first execution of
|
|
3098 |
the subquery predicate.
|
|
3099 |
*/
|
|
3100 |
if (!is_materialized) |
|
3101 |
{
|
|
3102 |
int res= 0; |
|
3103 |
SELECT_LEX *save_select= thd->lex->current_select; |
|
3104 |
thd->lex->current_select= materialize_engine->select_lex; |
|
3105 |
if ((res= materialize_join->optimize())) |
|
3106 |
goto err; |
|
3107 |
materialize_join->exec(); |
|
3108 |
if ((res= test(materialize_join->error || thd->is_fatal_error))) |
|
3109 |
goto err; |
|
3110 |
||
3111 |
/*
|
|
3112 |
TODO:
|
|
3113 |
- Unlock all subquery tables as we don't need them. To implement this
|
|
3114 |
we need to add new functionality to JOIN::join_free that can unlock
|
|
3115 |
all tables in a subquery (and all its subqueries).
|
|
3116 |
- The temp table used for grouping in the subquery can be freed
|
|
3117 |
immediately after materialization (yet it's done together with
|
|
3118 |
unlocking).
|
|
3119 |
*/
|
|
55
by brian
Update for using real bool types. |
3120 |
is_materialized= true; |
1
by brian
clean slate |
3121 |
/*
|
3122 |
If the subquery returned no rows, the temporary table is empty, so we know
|
|
55
by brian
Update for using real bool types. |
3123 |
directly that the result of IN is false. We first update the table
|
1
by brian
clean slate |
3124 |
statistics, then we test if the temporary table for the query result is
|
3125 |
empty.
|
|
3126 |
*/
|
|
3127 |
tab->table->file->info(HA_STATUS_VARIABLE); |
|
3128 |
if (!tab->table->file->stats.records) |
|
3129 |
{
|
|
55
by brian
Update for using real bool types. |
3130 |
empty_result_set= true; |
3131 |
item_in->value= false; |
|
3132 |
/* TODO: check we need this: item_in->null_value= false; */
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
3133 |
return(false); |
1
by brian
clean slate |
3134 |
}
|
3135 |
/* Set tmp_param only if its usable, i.e. tmp_param->copy_field != NULL. */
|
|
3136 |
tmp_param= &(item_in->unit->outer_select()->join->tmp_table_param); |
|
3137 |
if (tmp_param && !tmp_param->copy_field) |
|
3138 |
tmp_param= NULL; |
|
3139 |
||
3140 |
err: |
|
3141 |
thd->lex->current_select= save_select; |
|
3142 |
if (res) |
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
3143 |
return(res); |
1
by brian
clean slate |
3144 |
}
|
3145 |
||
3146 |
/*
|
|
3147 |
Lookup the left IN operand in the hash index of the materialized subquery.
|
|
3148 |
*/
|
|
51.1.24
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
3149 |
return(subselect_uniquesubquery_engine::exec()); |
1
by brian
clean slate |
3150 |
}
|
3151 |
||
3152 |
||
3153 |
/**
|
|
3154 |
Print the state of this engine into a string for debugging and views.
|
|
3155 |
*/
|
|
3156 |
||
3157 |
void subselect_hash_sj_engine::print(String *str, enum_query_type query_type) |
|
3158 |
{
|
|
3159 |
str->append(STRING_WITH_LEN(" <materialize> (")); |
|
3160 |
materialize_engine->print(str, query_type); |
|
3161 |
str->append(STRING_WITH_LEN(" ), ")); |
|
3162 |
if (tab) |
|
3163 |
subselect_uniquesubquery_engine::print(str, query_type); |
|
3164 |
else
|
|
3165 |
str->append(STRING_WITH_LEN( |
|
3166 |
"<the access method for lookups is not yet created>"
|
|
3167 |
));
|
|
3168 |
}
|