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