1
by brian
clean slate |
1 |
/* Copyright (C) 2000-2003 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 |
/*
|
|
18 |
UNION of select's
|
|
19 |
UNION's were introduced by Monty and Sinisa <sinisa@mysql.com>
|
|
20 |
*/
|
|
21 |
||
22 |
||
23 |
#include "mysql_priv.h" |
|
24 |
#include "sql_select.h" |
|
25 |
||
77.1.46
by Monty Taylor
Finished the warnings work! |
26 |
bool mysql_union(THD *thd, |
212.1.3
by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)). |
27 |
LEX *lex __attribute__((unused)), |
77.1.46
by Monty Taylor
Finished the warnings work! |
28 |
select_result *result, |
1
by brian
clean slate |
29 |
SELECT_LEX_UNIT *unit, ulong setup_tables_done_option) |
30 |
{
|
|
31 |
bool res; |
|
32 |
if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | |
|
33 |
setup_tables_done_option))) |
|
34 |
res= unit->exec(); |
|
35 |
if (res) |
|
36 |
res|= unit->cleanup(); |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
37 |
return(res); |
1
by brian
clean slate |
38 |
}
|
39 |
||
40 |
||
41 |
/***************************************************************************
|
|
42 |
** store records in temporary table for UNION
|
|
43 |
***************************************************************************/
|
|
44 |
||
212.1.3
by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)). |
45 |
int select_union::prepare(List<Item> &list __attribute__((unused)), |
77.1.46
by Monty Taylor
Finished the warnings work! |
46 |
SELECT_LEX_UNIT *u) |
1
by brian
clean slate |
47 |
{
|
48 |
unit= u; |
|
49 |
return 0; |
|
50 |
}
|
|
51 |
||
52 |
||
53 |
bool select_union::send_data(List<Item> &values) |
|
54 |
{
|
|
55 |
int error= 0; |
|
56 |
if (unit->offset_limit_cnt) |
|
57 |
{ // using limit offset,count |
|
58 |
unit->offset_limit_cnt--; |
|
59 |
return 0; |
|
60 |
}
|
|
61 |
fill_record(thd, table->field, values, 1); |
|
62 |
if (thd->is_error()) |
|
63 |
return 1; |
|
64 |
||
65 |
if ((error= table->file->ha_write_row(table->record[0]))) |
|
66 |
{
|
|
67 |
/* create_myisam_from_heap will generate error if needed */
|
|
68 |
if (table->file->is_fatal_error(error, HA_CHECK_DUP) && |
|
69 |
create_myisam_from_heap(thd, table, tmp_table_param.start_recinfo, |
|
70 |
&tmp_table_param.recinfo, error, 1)) |
|
71 |
return 1; |
|
72 |
}
|
|
73 |
return 0; |
|
74 |
}
|
|
75 |
||
76 |
||
77 |
bool select_union::send_eof() |
|
78 |
{
|
|
79 |
return 0; |
|
80 |
}
|
|
81 |
||
82 |
||
83 |
bool select_union::flush() |
|
84 |
{
|
|
85 |
int error; |
|
86 |
if ((error=table->file->extra(HA_EXTRA_NO_CACHE))) |
|
87 |
{
|
|
88 |
table->file->print_error(error, MYF(0)); |
|
89 |
return 1; |
|
90 |
}
|
|
91 |
return 0; |
|
92 |
}
|
|
93 |
||
94 |
/*
|
|
95 |
Create a temporary table to store the result of select_union.
|
|
96 |
||
97 |
SYNOPSIS
|
|
98 |
select_union::create_result_table()
|
|
99 |
thd thread handle
|
|
100 |
column_types a list of items used to define columns of the
|
|
101 |
temporary table
|
|
102 |
is_union_distinct if set, the temporary table will eliminate
|
|
103 |
duplicates on insert
|
|
104 |
options create options
|
|
105 |
table_alias name of the temporary table
|
|
151
by Brian Aker
Ulonglong to uint64_t |
106 |
bit_fields_as_long convert bit fields to uint64_t
|
1
by brian
clean slate |
107 |
|
108 |
DESCRIPTION
|
|
109 |
Create a temporary table that is used to store the result of a UNION,
|
|
110 |
derived table, or a materialized cursor.
|
|
111 |
||
112 |
RETURN VALUE
|
|
113 |
0 The table has been created successfully.
|
|
114 |
1 create_tmp_table failed.
|
|
115 |
*/
|
|
116 |
||
117 |
bool
|
|
118 |
select_union::create_result_table(THD *thd_arg, List<Item> *column_types, |
|
151
by Brian Aker
Ulonglong to uint64_t |
119 |
bool is_union_distinct, uint64_t options, |
1
by brian
clean slate |
120 |
const char *table_alias, |
121 |
bool bit_fields_as_long) |
|
122 |
{
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
123 |
assert(table == 0); |
1
by brian
clean slate |
124 |
tmp_table_param.init(); |
125 |
tmp_table_param.field_count= column_types->elements; |
|
126 |
tmp_table_param.bit_fields_as_long= bit_fields_as_long; |
|
127 |
||
128 |
if (! (table= create_tmp_table(thd_arg, &tmp_table_param, *column_types, |
|
129 |
(ORDER*) 0, is_union_distinct, 1, |
|
130 |
options, HA_POS_ERROR, (char*) table_alias))) |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
131 |
return true; |
1
by brian
clean slate |
132 |
table->file->extra(HA_EXTRA_WRITE_CACHE); |
133 |
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
134 |
return false; |
1
by brian
clean slate |
135 |
}
|
136 |
||
137 |
||
138 |
/**
|
|
139 |
Reset and empty the temporary table that stores the materialized query result.
|
|
140 |
||
141 |
@note The cleanup performed here is exactly the same as for the two temp
|
|
142 |
tables of JOIN - exec_tmp_table_[1 | 2].
|
|
143 |
*/
|
|
144 |
||
145 |
void select_union::cleanup() |
|
146 |
{
|
|
147 |
table->file->extra(HA_EXTRA_RESET_STATE); |
|
148 |
table->file->ha_delete_all_rows(); |
|
149 |
free_io_cache(table); |
|
150 |
filesort_free_buffers(table,0); |
|
151 |
}
|
|
152 |
||
153 |
||
154 |
/*
|
|
155 |
initialization procedures before fake_select_lex preparation()
|
|
156 |
||
157 |
SYNOPSIS
|
|
158 |
st_select_lex_unit::init_prepare_fake_select_lex()
|
|
159 |
thd - thread handler
|
|
160 |
||
161 |
RETURN
|
|
162 |
options of SELECT
|
|
163 |
*/
|
|
164 |
||
165 |
void
|
|
166 |
st_select_lex_unit::init_prepare_fake_select_lex(THD *thd_arg) |
|
167 |
{
|
|
168 |
thd_arg->lex->current_select= fake_select_lex; |
|
169 |
fake_select_lex->table_list.link_in_list((uchar *)&result_table_list, |
|
170 |
(uchar **) |
|
171 |
&result_table_list.next_local); |
|
172 |
fake_select_lex->context.table_list= |
|
173 |
fake_select_lex->context.first_name_resolution_table= |
|
174 |
fake_select_lex->get_table_list(); |
|
175 |
if (!fake_select_lex->first_execution) |
|
176 |
{
|
|
177 |
for (ORDER *order= (ORDER *) global_parameters->order_list.first; |
|
178 |
order; |
|
179 |
order= order->next) |
|
180 |
order->item= &order->item_ptr; |
|
181 |
}
|
|
182 |
for (ORDER *order= (ORDER *)global_parameters->order_list.first; |
|
183 |
order; |
|
184 |
order=order->next) |
|
185 |
{
|
|
186 |
(*order->item)->walk(&Item::change_context_processor, 0, |
|
187 |
(uchar*) &fake_select_lex->context); |
|
188 |
}
|
|
189 |
}
|
|
190 |
||
191 |
||
192 |
bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, |
|
202
by Brian Aker
Cleanup sql_lex to modern types. |
193 |
uint32_t additional_options) |
1
by brian
clean slate |
194 |
{
|
195 |
SELECT_LEX *lex_select_save= thd_arg->lex->current_select; |
|
196 |
SELECT_LEX *sl, *first_sl= first_select(); |
|
197 |
select_result *tmp_result; |
|
198 |
bool is_union_select; |
|
199 |
TABLE *empty_table= 0; |
|
200 |
||
201 |
describe= test(additional_options & SELECT_DESCRIBE); |
|
202 |
||
203 |
/*
|
|
204 |
result object should be reassigned even if preparing already done for
|
|
205 |
max/min subquery (ALL/ANY optimization)
|
|
206 |
*/
|
|
207 |
result= sel_result; |
|
208 |
||
209 |
if (prepared) |
|
210 |
{
|
|
211 |
if (describe) |
|
212 |
{
|
|
213 |
/* fast reinit for EXPLAIN */
|
|
214 |
for (sl= first_sl; sl; sl= sl->next_select()) |
|
215 |
{
|
|
216 |
sl->join->result= result; |
|
217 |
select_limit_cnt= HA_POS_ERROR; |
|
218 |
offset_limit_cnt= 0; |
|
219 |
if (result->prepare(sl->join->fields_list, this)) |
|
220 |
{
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
221 |
return(true); |
1
by brian
clean slate |
222 |
}
|
223 |
sl->join->select_options|= SELECT_DESCRIBE; |
|
224 |
sl->join->reinit(); |
|
225 |
}
|
|
226 |
}
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
227 |
return(false); |
1
by brian
clean slate |
228 |
}
|
229 |
prepared= 1; |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
230 |
saved_error= false; |
1
by brian
clean slate |
231 |
|
232 |
thd_arg->lex->current_select= sl= first_sl; |
|
233 |
found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS; |
|
234 |
is_union_select= is_union() || fake_select_lex; |
|
235 |
||
236 |
/* Global option */
|
|
237 |
||
238 |
if (is_union_select) |
|
239 |
{
|
|
240 |
if (!(tmp_result= union_result= new select_union)) |
|
241 |
goto err; |
|
242 |
if (describe) |
|
243 |
tmp_result= sel_result; |
|
244 |
}
|
|
245 |
else
|
|
246 |
tmp_result= sel_result; |
|
247 |
||
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
248 |
sl->context.resolve_in_select_list= true; |
1
by brian
clean slate |
249 |
|
250 |
for (;sl; sl= sl->next_select()) |
|
251 |
{
|
|
252 |
bool can_skip_order_by; |
|
253 |
sl->options|= SELECT_NO_UNLOCK; |
|
254 |
JOIN *join= new JOIN(thd_arg, sl->item_list, |
|
255 |
sl->options | thd_arg->options | additional_options, |
|
256 |
tmp_result); |
|
257 |
/*
|
|
258 |
setup_tables_done_option should be set only for very first SELECT,
|
|
259 |
because it protect from secont setup_tables call for select-like non
|
|
260 |
select commands (DELETE/INSERT/...) and they use only very first
|
|
261 |
SELECT (for union it can be only INSERT ... SELECT).
|
|
262 |
*/
|
|
263 |
additional_options&= ~OPTION_SETUP_TABLES_DONE; |
|
264 |
if (!join) |
|
265 |
goto err; |
|
266 |
||
267 |
thd_arg->lex->current_select= sl; |
|
268 |
||
269 |
can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit); |
|
270 |
||
271 |
saved_error= join->prepare(&sl->ref_pointer_array, |
|
272 |
(TABLE_LIST*) sl->table_list.first, |
|
273 |
sl->with_wild, |
|
274 |
sl->where, |
|
275 |
(can_skip_order_by ? 0 : |
|
276 |
sl->order_list.elements) + |
|
277 |
sl->group_list.elements, |
|
278 |
can_skip_order_by ? |
|
279 |
(ORDER*) 0 : (ORDER *)sl->order_list.first, |
|
280 |
(ORDER*) sl->group_list.first, |
|
281 |
sl->having, |
|
282 |
(is_union_select ? (ORDER*) 0 : |
|
283 |
(ORDER*) thd_arg->lex->proc_list.first), |
|
284 |
sl, this); |
|
285 |
/* There are no * in the statement anymore (for PS) */
|
|
286 |
sl->with_wild= 0; |
|
287 |
||
288 |
if (saved_error || (saved_error= thd_arg->is_fatal_error)) |
|
289 |
goto err; |
|
290 |
/*
|
|
291 |
Use items list of underlaid select for derived tables to preserve
|
|
292 |
information about fields lengths and exact types
|
|
293 |
*/
|
|
294 |
if (!is_union_select) |
|
295 |
types= first_sl->item_list; |
|
296 |
else if (sl == first_sl) |
|
297 |
{
|
|
298 |
/*
|
|
299 |
We need to create an empty table object. It is used
|
|
300 |
to create tmp_table fields in Item_type_holder.
|
|
301 |
The main reason of this is that we can't create
|
|
302 |
field object without table.
|
|
303 |
*/
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
304 |
assert(!empty_table); |
1
by brian
clean slate |
305 |
empty_table= (TABLE*) thd->calloc(sizeof(TABLE)); |
306 |
types.empty(); |
|
307 |
List_iterator_fast<Item> it(sl->item_list); |
|
308 |
Item *item_tmp; |
|
309 |
while ((item_tmp= it++)) |
|
310 |
{
|
|
311 |
/* Error's in 'new' will be detected after loop */
|
|
312 |
types.push_back(new Item_type_holder(thd_arg, item_tmp)); |
|
313 |
}
|
|
314 |
||
315 |
if (thd_arg->is_fatal_error) |
|
316 |
goto err; // out of memory |
|
317 |
}
|
|
318 |
else
|
|
319 |
{
|
|
320 |
if (types.elements != sl->item_list.elements) |
|
321 |
{
|
|
322 |
my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT, |
|
323 |
ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0)); |
|
324 |
goto err; |
|
325 |
}
|
|
326 |
List_iterator_fast<Item> it(sl->item_list); |
|
327 |
List_iterator_fast<Item> tp(types); |
|
328 |
Item *type, *item_tmp; |
|
329 |
while ((type= tp++, item_tmp= it++)) |
|
330 |
{
|
|
331 |
if (((Item_type_holder*)type)->join_types(thd_arg, item_tmp)) |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
332 |
return(true); |
1
by brian
clean slate |
333 |
}
|
334 |
}
|
|
335 |
}
|
|
336 |
||
337 |
if (is_union_select) |
|
338 |
{
|
|
339 |
/*
|
|
340 |
Check that it was possible to aggregate
|
|
341 |
all collations together for UNION.
|
|
342 |
*/
|
|
343 |
List_iterator_fast<Item> tp(types); |
|
344 |
Item *type; |
|
151
by Brian Aker
Ulonglong to uint64_t |
345 |
uint64_t create_options; |
1
by brian
clean slate |
346 |
|
347 |
while ((type= tp++)) |
|
348 |
{
|
|
349 |
if (type->result_type() == STRING_RESULT && |
|
350 |
type->collation.derivation == DERIVATION_NONE) |
|
351 |
{
|
|
352 |
my_error(ER_CANT_AGGREGATE_NCOLLATIONS, MYF(0), "UNION"); |
|
353 |
goto err; |
|
354 |
}
|
|
355 |
}
|
|
356 |
||
357 |
create_options= (first_sl->options | thd_arg->options | |
|
358 |
TMP_TABLE_ALL_COLUMNS); |
|
359 |
||
360 |
if (union_result->create_result_table(thd, &types, test(union_distinct), |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
361 |
create_options, "", false)) |
1
by brian
clean slate |
362 |
goto err; |
212.6.1
by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file. |
363 |
memset((char*) &result_table_list, 0, sizeof(result_table_list)); |
1
by brian
clean slate |
364 |
result_table_list.db= (char*) ""; |
365 |
result_table_list.table_name= result_table_list.alias= (char*) "union"; |
|
366 |
result_table_list.table= table= union_result->table; |
|
367 |
||
368 |
thd_arg->lex->current_select= lex_select_save; |
|
369 |
if (!item_list.elements) |
|
370 |
{
|
|
371 |
saved_error= table->fill_item_list(&item_list); |
|
372 |
if (saved_error) |
|
373 |
goto err; |
|
374 |
}
|
|
375 |
else
|
|
376 |
{
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
377 |
assert(thd->stmt_arena->is_conventional() == false); |
1
by brian
clean slate |
378 |
/*
|
379 |
We're in execution of a prepared statement or stored procedure:
|
|
380 |
reset field items to point at fields from the created temporary table.
|
|
381 |
*/
|
|
382 |
table->reset_item_list(&item_list); |
|
383 |
}
|
|
384 |
}
|
|
385 |
||
386 |
thd_arg->lex->current_select= lex_select_save; |
|
387 |
||
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
388 |
return(saved_error || thd_arg->is_fatal_error); |
1
by brian
clean slate |
389 |
|
390 |
err: |
|
391 |
thd_arg->lex->current_select= lex_select_save; |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
392 |
return(true); |
1
by brian
clean slate |
393 |
}
|
394 |
||
395 |
||
396 |
bool st_select_lex_unit::exec() |
|
397 |
{
|
|
398 |
SELECT_LEX *lex_select_save= thd->lex->current_select; |
|
399 |
SELECT_LEX *select_cursor=first_select(); |
|
151
by Brian Aker
Ulonglong to uint64_t |
400 |
uint64_t add_rows=0; |
1
by brian
clean slate |
401 |
ha_rows examined_rows= 0; |
402 |
||
403 |
if (executed && !uncacheable && !describe) |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
404 |
return(false); |
1
by brian
clean slate |
405 |
executed= 1; |
406 |
||
407 |
if (uncacheable || !item || !item->assigned() || describe) |
|
408 |
{
|
|
409 |
if (item) |
|
410 |
item->reset_value_registration(); |
|
411 |
if (optimized && item) |
|
412 |
{
|
|
413 |
if (item->assigned()) |
|
414 |
{
|
|
415 |
item->assigned(0); // We will reinit & rexecute unit |
|
416 |
item->reset(); |
|
417 |
table->file->ha_delete_all_rows(); |
|
418 |
}
|
|
419 |
/* re-enabling indexes for next subselect iteration */
|
|
420 |
if (union_distinct && table->file->ha_enable_indexes(HA_KEY_SWITCH_ALL)) |
|
421 |
{
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
422 |
assert(0); |
1
by brian
clean slate |
423 |
}
|
424 |
}
|
|
425 |
for (SELECT_LEX *sl= select_cursor; sl; sl= sl->next_select()) |
|
426 |
{
|
|
427 |
ha_rows records_at_start= 0; |
|
428 |
thd->lex->current_select= sl; |
|
429 |
||
430 |
if (optimized) |
|
431 |
saved_error= sl->join->reinit(); |
|
432 |
else
|
|
433 |
{
|
|
434 |
set_limit(sl); |
|
435 |
if (sl == global_parameters || describe) |
|
436 |
{
|
|
437 |
offset_limit_cnt= 0; |
|
438 |
/*
|
|
439 |
We can't use LIMIT at this stage if we are using ORDER BY for the
|
|
440 |
whole query
|
|
441 |
*/
|
|
442 |
if (sl->order_list.first || describe) |
|
443 |
select_limit_cnt= HA_POS_ERROR; |
|
444 |
}
|
|
445 |
||
446 |
/*
|
|
447 |
When using braces, SQL_CALC_FOUND_ROWS affects the whole query:
|
|
448 |
we don't calculate found_rows() per union part.
|
|
449 |
Otherwise, SQL_CALC_FOUND_ROWS should be done on all sub parts.
|
|
450 |
*/
|
|
451 |
sl->join->select_options= |
|
452 |
(select_limit_cnt == HA_POS_ERROR || sl->braces) ? |
|
453 |
sl->options & ~OPTION_FOUND_ROWS : sl->options | found_rows_for_union; |
|
454 |
||
455 |
/* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
|
|
456 |
if (sl->join->flatten_subqueries()) |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
457 |
return(true); |
1
by brian
clean slate |
458 |
|
459 |
/* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
|
|
460 |
saved_error= sl->join->optimize(); |
|
461 |
}
|
|
462 |
if (!saved_error) |
|
463 |
{
|
|
464 |
records_at_start= table->file->stats.records; |
|
465 |
sl->join->exec(); |
|
466 |
if (sl == union_distinct) |
|
467 |
{
|
|
468 |
if (table->file->ha_disable_indexes(HA_KEY_SWITCH_ALL)) |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
469 |
return(true); |
1
by brian
clean slate |
470 |
table->no_keyread=1; |
471 |
}
|
|
472 |
saved_error= sl->join->error; |
|
473 |
offset_limit_cnt= (ha_rows)(sl->offset_limit ? |
|
474 |
sl->offset_limit->val_uint() : |
|
475 |
0); |
|
476 |
if (!saved_error) |
|
477 |
{
|
|
478 |
examined_rows+= thd->examined_row_count; |
|
479 |
if (union_result->flush()) |
|
480 |
{
|
|
481 |
thd->lex->current_select= lex_select_save; |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
482 |
return(1); |
1
by brian
clean slate |
483 |
}
|
484 |
}
|
|
485 |
}
|
|
486 |
if (saved_error) |
|
487 |
{
|
|
488 |
thd->lex->current_select= lex_select_save; |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
489 |
return(saved_error); |
1
by brian
clean slate |
490 |
}
|
491 |
/* Needed for the following test and for records_at_start in next loop */
|
|
492 |
int error= table->file->info(HA_STATUS_VARIABLE); |
|
493 |
if(error) |
|
494 |
{
|
|
495 |
table->file->print_error(error, MYF(0)); |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
496 |
return(1); |
1
by brian
clean slate |
497 |
}
|
498 |
if (found_rows_for_union && !sl->braces && |
|
499 |
select_limit_cnt != HA_POS_ERROR) |
|
500 |
{
|
|
501 |
/*
|
|
502 |
This is a union without braces. Remember the number of rows that
|
|
503 |
could also have been part of the result set.
|
|
504 |
We get this from the difference of between total number of possible
|
|
505 |
rows and actual rows added to the temporary table.
|
|
506 |
*/
|
|
151
by Brian Aker
Ulonglong to uint64_t |
507 |
add_rows+= (uint64_t) (thd->limit_found_rows - (uint64_t) |
1
by brian
clean slate |
508 |
((table->file->stats.records - records_at_start))); |
509 |
}
|
|
510 |
}
|
|
511 |
}
|
|
512 |
optimized= 1; |
|
513 |
||
514 |
/* Send result to 'result' */
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
515 |
saved_error= true; |
1
by brian
clean slate |
516 |
{
|
517 |
if (!thd->is_fatal_error) // Check if EOM |
|
518 |
{
|
|
519 |
set_limit(global_parameters); |
|
520 |
init_prepare_fake_select_lex(thd); |
|
521 |
JOIN *join= fake_select_lex->join; |
|
522 |
if (!join) |
|
523 |
{
|
|
524 |
/*
|
|
525 |
allocate JOIN for fake select only once (prevent
|
|
526 |
mysql_select automatic allocation)
|
|
527 |
TODO: The above is nonsense. mysql_select() will not allocate the
|
|
528 |
join if one already exists. There must be some other reason why we
|
|
529 |
don't let it allocate the join. Perhaps this is because we need
|
|
530 |
some special parameter values passed to join constructor?
|
|
531 |
*/
|
|
532 |
if (!(fake_select_lex->join= new JOIN(thd, item_list, |
|
533 |
fake_select_lex->options, result))) |
|
534 |
{
|
|
535 |
fake_select_lex->table_list.empty(); |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
536 |
return(true); |
1
by brian
clean slate |
537 |
}
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
538 |
fake_select_lex->join->no_const_tables= true; |
1
by brian
clean slate |
539 |
|
540 |
/*
|
|
541 |
Fake st_select_lex should have item list for correctref_array
|
|
542 |
allocation.
|
|
543 |
*/
|
|
544 |
fake_select_lex->item_list= item_list; |
|
545 |
saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array, |
|
546 |
&result_table_list, |
|
547 |
0, item_list, NULL, |
|
548 |
global_parameters->order_list.elements, |
|
549 |
(ORDER*)global_parameters->order_list.first, |
|
550 |
(ORDER*) NULL, NULL, (ORDER*) NULL, |
|
551 |
fake_select_lex->options | SELECT_NO_UNLOCK, |
|
552 |
result, this, fake_select_lex); |
|
553 |
}
|
|
554 |
else
|
|
555 |
{
|
|
556 |
if (describe) |
|
557 |
{
|
|
558 |
/*
|
|
559 |
In EXPLAIN command, constant subqueries that do not use any
|
|
560 |
tables are executed two times:
|
|
561 |
- 1st time is a real evaluation to get the subquery value
|
|
562 |
- 2nd time is to produce EXPLAIN output rows.
|
|
563 |
1st execution sets certain members (e.g. select_result) to perform
|
|
564 |
subquery execution rather than EXPLAIN line production. In order
|
|
565 |
to reset them back, we re-do all of the actions (yes it is ugly):
|
|
566 |
*/
|
|
567 |
join->init(thd, item_list, fake_select_lex->options, result); |
|
568 |
saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array, |
|
569 |
&result_table_list, |
|
570 |
0, item_list, NULL, |
|
571 |
global_parameters->order_list.elements, |
|
572 |
(ORDER*)global_parameters->order_list.first, |
|
573 |
(ORDER*) NULL, NULL, (ORDER*) NULL, |
|
574 |
fake_select_lex->options | SELECT_NO_UNLOCK, |
|
575 |
result, this, fake_select_lex); |
|
576 |
}
|
|
577 |
else
|
|
578 |
{
|
|
579 |
join->examined_rows= 0; |
|
580 |
saved_error= join->reinit(); |
|
581 |
join->exec(); |
|
582 |
}
|
|
583 |
}
|
|
584 |
||
585 |
fake_select_lex->table_list.empty(); |
|
586 |
if (!saved_error) |
|
587 |
{
|
|
151
by Brian Aker
Ulonglong to uint64_t |
588 |
thd->limit_found_rows = (uint64_t)table->file->stats.records + add_rows; |
1
by brian
clean slate |
589 |
thd->examined_row_count+= examined_rows; |
590 |
}
|
|
591 |
/*
|
|
592 |
Mark for slow query log if any of the union parts didn't use
|
|
593 |
indexes efficiently
|
|
594 |
*/
|
|
595 |
}
|
|
596 |
}
|
|
597 |
thd->lex->current_select= lex_select_save; |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
598 |
return(saved_error); |
1
by brian
clean slate |
599 |
}
|
600 |
||
601 |
||
602 |
bool st_select_lex_unit::cleanup() |
|
603 |
{
|
|
604 |
int error= 0; |
|
605 |
||
606 |
if (cleaned) |
|
607 |
{
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
608 |
return(false); |
1
by brian
clean slate |
609 |
}
|
610 |
cleaned= 1; |
|
611 |
||
612 |
if (union_result) |
|
613 |
{
|
|
614 |
delete union_result; |
|
615 |
union_result=0; // Safety |
|
616 |
if (table) |
|
617 |
free_tmp_table(thd, table); |
|
618 |
table= 0; // Safety |
|
619 |
}
|
|
620 |
||
621 |
for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select()) |
|
622 |
error|= sl->cleanup(); |
|
623 |
||
624 |
if (fake_select_lex) |
|
625 |
{
|
|
626 |
JOIN *join; |
|
627 |
if ((join= fake_select_lex->join)) |
|
628 |
{
|
|
629 |
join->tables_list= 0; |
|
630 |
join->tables= 0; |
|
631 |
}
|
|
632 |
error|= fake_select_lex->cleanup(); |
|
633 |
if (fake_select_lex->order_list.elements) |
|
634 |
{
|
|
635 |
ORDER *ord; |
|
636 |
for (ord= (ORDER*)fake_select_lex->order_list.first; ord; ord= ord->next) |
|
637 |
(*ord->item)->cleanup(); |
|
638 |
}
|
|
639 |
}
|
|
640 |
||
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
641 |
return(error); |
1
by brian
clean slate |
642 |
}
|
643 |
||
644 |
||
645 |
void st_select_lex_unit::reinit_exec_mechanism() |
|
646 |
{
|
|
647 |
prepared= optimized= executed= 0; |
|
648 |
}
|
|
649 |
||
650 |
||
651 |
/*
|
|
652 |
change select_result object of unit
|
|
653 |
||
654 |
SYNOPSIS
|
|
655 |
st_select_lex_unit::change_result()
|
|
656 |
result new select_result object
|
|
657 |
old_result old select_result object
|
|
658 |
||
659 |
RETURN
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
660 |
false - OK
|
661 |
true - error
|
|
1
by brian
clean slate |
662 |
*/
|
663 |
||
664 |
bool st_select_lex_unit::change_result(select_result_interceptor *new_result, |
|
665 |
select_result_interceptor *old_result) |
|
666 |
{
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
667 |
bool res= false; |
1
by brian
clean slate |
668 |
for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select()) |
669 |
{
|
|
670 |
if (sl->join && sl->join->result == old_result) |
|
671 |
if (sl->join->change_result(new_result)) |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
672 |
return true; |
1
by brian
clean slate |
673 |
}
|
674 |
if (fake_select_lex && fake_select_lex->join) |
|
675 |
res= fake_select_lex->join->change_result(new_result); |
|
676 |
return (res); |
|
677 |
}
|
|
678 |
||
679 |
/*
|
|
680 |
Get column type information for this unit.
|
|
681 |
||
682 |
SYNOPSIS
|
|
683 |
st_select_lex_unit::get_unit_column_types()
|
|
684 |
||
685 |
DESCRIPTION
|
|
686 |
For a single-select the column types are taken
|
|
687 |
from the list of selected items. For a union this function
|
|
688 |
assumes that st_select_lex_unit::prepare has been called
|
|
689 |
and returns the type holders that were created for unioned
|
|
690 |
column types of all selects.
|
|
691 |
||
692 |
NOTES
|
|
693 |
The implementation of this function should be in sync with
|
|
694 |
st_select_lex_unit::prepare()
|
|
695 |
*/
|
|
696 |
||
697 |
List<Item> *st_select_lex_unit::get_unit_column_types() |
|
698 |
{
|
|
699 |
SELECT_LEX *sl= first_select(); |
|
700 |
||
701 |
if (is_union()) |
|
702 |
{
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
703 |
assert(prepared); |
1
by brian
clean slate |
704 |
/* Types are generated during prepare */
|
705 |
return &types; |
|
706 |
}
|
|
707 |
||
708 |
return &sl->item_list; |
|
709 |
}
|
|
710 |
||
711 |
bool st_select_lex::cleanup() |
|
712 |
{
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
713 |
bool error= false; |
1
by brian
clean slate |
714 |
|
715 |
if (join) |
|
716 |
{
|
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
717 |
assert((st_select_lex*)join->select_lex == this); |
1
by brian
clean slate |
718 |
error= join->destroy(); |
719 |
delete join; |
|
720 |
join= 0; |
|
721 |
}
|
|
722 |
for (SELECT_LEX_UNIT *lex_unit= first_inner_unit(); lex_unit ; |
|
723 |
lex_unit= lex_unit->next_unit()) |
|
724 |
{
|
|
725 |
error= (bool) ((uint) error | (uint) lex_unit->cleanup()); |
|
726 |
}
|
|
727 |
non_agg_fields.empty(); |
|
728 |
inner_refs_list.empty(); |
|
51.1.68
by Jay Pipes
* Removed sql_test.cc custom debugging functions |
729 |
return(error); |
1
by brian
clean slate |
730 |
}
|
731 |
||
732 |
||
733 |
void st_select_lex::cleanup_all_joins(bool full) |
|
734 |
{
|
|
735 |
SELECT_LEX_UNIT *unit; |
|
736 |
SELECT_LEX *sl; |
|
737 |
||
738 |
if (join) |
|
739 |
join->cleanup(full); |
|
740 |
||
741 |
for (unit= first_inner_unit(); unit; unit= unit->next_unit()) |
|
742 |
for (sl= unit->first_select(); sl; sl= sl->next_select()) |
|
743 |
sl->cleanup_all_joins(full); |
|
744 |
}
|