1
/* Copyright (C) 2000-2003 MySQL AB
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.
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.
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 */
19
UNION's were introduced by Monty and Sinisa <sinisa@mysql.com>
23
#include "mysql_priv.h"
24
#include "sql_select.h"
26
bool mysql_union(THD *thd, LEX *lex, select_result *result,
27
SELECT_LEX_UNIT *unit, ulong setup_tables_done_option)
29
DBUG_ENTER("mysql_union");
31
if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK |
32
setup_tables_done_option)))
35
res|= unit->cleanup();
40
/***************************************************************************
41
** store records in temporary table for UNION
42
***************************************************************************/
44
int select_union::prepare(List<Item> &list, SELECT_LEX_UNIT *u)
51
bool select_union::send_data(List<Item> &values)
54
if (unit->offset_limit_cnt)
55
{ // using limit offset,count
56
unit->offset_limit_cnt--;
59
fill_record(thd, table->field, values, 1);
63
if ((error= table->file->ha_write_row(table->record[0])))
65
/* create_myisam_from_heap will generate error if needed */
66
if (table->file->is_fatal_error(error, HA_CHECK_DUP) &&
67
create_myisam_from_heap(thd, table, tmp_table_param.start_recinfo,
68
&tmp_table_param.recinfo, error, 1))
75
bool select_union::send_eof()
81
bool select_union::flush()
84
if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
86
table->file->print_error(error, MYF(0));
93
Create a temporary table to store the result of select_union.
96
select_union::create_result_table()
98
column_types a list of items used to define columns of the
100
is_union_distinct if set, the temporary table will eliminate
102
options create options
103
table_alias name of the temporary table
104
bit_fields_as_long convert bit fields to ulonglong
107
Create a temporary table that is used to store the result of a UNION,
108
derived table, or a materialized cursor.
111
0 The table has been created successfully.
112
1 create_tmp_table failed.
116
select_union::create_result_table(THD *thd_arg, List<Item> *column_types,
117
bool is_union_distinct, ulonglong options,
118
const char *table_alias,
119
bool bit_fields_as_long)
121
DBUG_ASSERT(table == 0);
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;
126
if (! (table= create_tmp_table(thd_arg, &tmp_table_param, *column_types,
127
(ORDER*) 0, is_union_distinct, 1,
128
options, HA_POS_ERROR, (char*) table_alias)))
130
table->file->extra(HA_EXTRA_WRITE_CACHE);
131
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
137
Reset and empty the temporary table that stores the materialized query result.
139
@note The cleanup performed here is exactly the same as for the two temp
140
tables of JOIN - exec_tmp_table_[1 | 2].
143
void select_union::cleanup()
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);
153
initialization procedures before fake_select_lex preparation()
156
st_select_lex_unit::init_prepare_fake_select_lex()
164
st_select_lex_unit::init_prepare_fake_select_lex(THD *thd_arg)
166
thd_arg->lex->current_select= fake_select_lex;
167
fake_select_lex->table_list.link_in_list((uchar *)&result_table_list,
169
&result_table_list.next_local);
170
fake_select_lex->context.table_list=
171
fake_select_lex->context.first_name_resolution_table=
172
fake_select_lex->get_table_list();
173
if (!fake_select_lex->first_execution)
175
for (ORDER *order= (ORDER *) global_parameters->order_list.first;
178
order->item= &order->item_ptr;
180
for (ORDER *order= (ORDER *)global_parameters->order_list.first;
184
(*order->item)->walk(&Item::change_context_processor, 0,
185
(uchar*) &fake_select_lex->context);
190
bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result,
191
ulong additional_options)
193
SELECT_LEX *lex_select_save= thd_arg->lex->current_select;
194
SELECT_LEX *sl, *first_sl= first_select();
195
select_result *tmp_result;
196
bool is_union_select;
197
TABLE *empty_table= 0;
198
DBUG_ENTER("st_select_lex_unit::prepare");
200
describe= test(additional_options & SELECT_DESCRIBE);
203
result object should be reassigned even if preparing already done for
204
max/min subquery (ALL/ANY optimization)
212
/* fast reinit for EXPLAIN */
213
for (sl= first_sl; sl; sl= sl->next_select())
215
sl->join->result= result;
216
select_limit_cnt= HA_POS_ERROR;
218
if (result->prepare(sl->join->fields_list, this))
222
sl->join->select_options|= SELECT_DESCRIBE;
231
thd_arg->lex->current_select= sl= first_sl;
232
found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
233
is_union_select= is_union() || fake_select_lex;
239
if (!(tmp_result= union_result= new select_union))
242
tmp_result= sel_result;
245
tmp_result= sel_result;
247
sl->context.resolve_in_select_list= TRUE;
249
for (;sl; sl= sl->next_select())
251
bool can_skip_order_by;
252
sl->options|= SELECT_NO_UNLOCK;
253
JOIN *join= new JOIN(thd_arg, sl->item_list,
254
sl->options | thd_arg->options | additional_options,
257
setup_tables_done_option should be set only for very first SELECT,
258
because it protect from secont setup_tables call for select-like non
259
select commands (DELETE/INSERT/...) and they use only very first
260
SELECT (for union it can be only INSERT ... SELECT).
262
additional_options&= ~OPTION_SETUP_TABLES_DONE;
266
thd_arg->lex->current_select= sl;
268
can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
270
saved_error= join->prepare(&sl->ref_pointer_array,
271
(TABLE_LIST*) sl->table_list.first,
274
(can_skip_order_by ? 0 :
275
sl->order_list.elements) +
276
sl->group_list.elements,
278
(ORDER*) 0 : (ORDER *)sl->order_list.first,
279
(ORDER*) sl->group_list.first,
281
(is_union_select ? (ORDER*) 0 :
282
(ORDER*) thd_arg->lex->proc_list.first),
284
/* There are no * in the statement anymore (for PS) */
287
if (saved_error || (saved_error= thd_arg->is_fatal_error))
290
Use items list of underlaid select for derived tables to preserve
291
information about fields lengths and exact types
293
if (!is_union_select)
294
types= first_sl->item_list;
295
else if (sl == first_sl)
298
We need to create an empty table object. It is used
299
to create tmp_table fields in Item_type_holder.
300
The main reason of this is that we can't create
301
field object without table.
303
DBUG_ASSERT(!empty_table);
304
empty_table= (TABLE*) thd->calloc(sizeof(TABLE));
306
List_iterator_fast<Item> it(sl->item_list);
308
while ((item_tmp= it++))
310
/* Error's in 'new' will be detected after loop */
311
types.push_back(new Item_type_holder(thd_arg, item_tmp));
314
if (thd_arg->is_fatal_error)
315
goto err; // out of memory
319
if (types.elements != sl->item_list.elements)
321
my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
322
ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
325
List_iterator_fast<Item> it(sl->item_list);
326
List_iterator_fast<Item> tp(types);
327
Item *type, *item_tmp;
328
while ((type= tp++, item_tmp= it++))
330
if (((Item_type_holder*)type)->join_types(thd_arg, item_tmp))
339
Check that it was possible to aggregate
340
all collations together for UNION.
342
List_iterator_fast<Item> tp(types);
344
ulonglong create_options;
348
if (type->result_type() == STRING_RESULT &&
349
type->collation.derivation == DERIVATION_NONE)
351
my_error(ER_CANT_AGGREGATE_NCOLLATIONS, MYF(0), "UNION");
356
create_options= (first_sl->options | thd_arg->options |
357
TMP_TABLE_ALL_COLUMNS);
359
if (union_result->create_result_table(thd, &types, test(union_distinct),
360
create_options, "", FALSE))
362
bzero((char*) &result_table_list, sizeof(result_table_list));
363
result_table_list.db= (char*) "";
364
result_table_list.table_name= result_table_list.alias= (char*) "union";
365
result_table_list.table= table= union_result->table;
367
thd_arg->lex->current_select= lex_select_save;
368
if (!item_list.elements)
370
saved_error= table->fill_item_list(&item_list);
376
DBUG_ASSERT(thd->stmt_arena->is_conventional() == false);
378
We're in execution of a prepared statement or stored procedure:
379
reset field items to point at fields from the created temporary table.
381
table->reset_item_list(&item_list);
385
thd_arg->lex->current_select= lex_select_save;
387
DBUG_RETURN(saved_error || thd_arg->is_fatal_error);
390
thd_arg->lex->current_select= lex_select_save;
395
bool st_select_lex_unit::exec()
397
SELECT_LEX *lex_select_save= thd->lex->current_select;
398
SELECT_LEX *select_cursor=first_select();
399
ulonglong add_rows=0;
400
ha_rows examined_rows= 0;
401
DBUG_ENTER("st_select_lex_unit::exec");
403
if (executed && !uncacheable && !describe)
407
if (uncacheable || !item || !item->assigned() || describe)
410
item->reset_value_registration();
411
if (optimized && item)
413
if (item->assigned())
415
item->assigned(0); // We will reinit & rexecute unit
417
table->file->ha_delete_all_rows();
419
/* re-enabling indexes for next subselect iteration */
420
if (union_distinct && table->file->ha_enable_indexes(HA_KEY_SWITCH_ALL))
425
for (SELECT_LEX *sl= select_cursor; sl; sl= sl->next_select())
427
ha_rows records_at_start= 0;
428
thd->lex->current_select= sl;
431
saved_error= sl->join->reinit();
435
if (sl == global_parameters || describe)
439
We can't use LIMIT at this stage if we are using ORDER BY for the
442
if (sl->order_list.first || describe)
443
select_limit_cnt= HA_POS_ERROR;
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.
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;
455
/* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
456
if (sl->join->flatten_subqueries())
459
/* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
460
saved_error= sl->join->optimize();
464
records_at_start= table->file->stats.records;
466
if (sl == union_distinct)
468
if (table->file->ha_disable_indexes(HA_KEY_SWITCH_ALL))
472
saved_error= sl->join->error;
473
offset_limit_cnt= (ha_rows)(sl->offset_limit ?
474
sl->offset_limit->val_uint() :
478
examined_rows+= thd->examined_row_count;
479
if (union_result->flush())
481
thd->lex->current_select= lex_select_save;
488
thd->lex->current_select= lex_select_save;
489
DBUG_RETURN(saved_error);
491
/* Needed for the following test and for records_at_start in next loop */
492
int error= table->file->info(HA_STATUS_VARIABLE);
495
table->file->print_error(error, MYF(0));
498
if (found_rows_for_union && !sl->braces &&
499
select_limit_cnt != HA_POS_ERROR)
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.
507
add_rows+= (ulonglong) (thd->limit_found_rows - (ulonglong)
508
((table->file->stats.records - records_at_start)));
514
/* Send result to 'result' */
517
if (!thd->is_fatal_error) // Check if EOM
519
set_limit(global_parameters);
520
init_prepare_fake_select_lex(thd);
521
JOIN *join= fake_select_lex->join;
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?
532
if (!(fake_select_lex->join= new JOIN(thd, item_list,
533
fake_select_lex->options, result)))
535
fake_select_lex->table_list.empty();
538
fake_select_lex->join->no_const_tables= TRUE;
541
Fake st_select_lex should have item list for correctref_array
544
fake_select_lex->item_list= item_list;
545
saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array,
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);
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):
567
join->init(thd, item_list, fake_select_lex->options, result);
568
saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array,
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);
579
join->examined_rows= 0;
580
saved_error= join->reinit();
585
fake_select_lex->table_list.empty();
588
thd->limit_found_rows = (ulonglong)table->file->stats.records + add_rows;
589
thd->examined_row_count+= examined_rows;
592
Mark for slow query log if any of the union parts didn't use
597
thd->lex->current_select= lex_select_save;
598
DBUG_RETURN(saved_error);
602
bool st_select_lex_unit::cleanup()
605
DBUG_ENTER("st_select_lex_unit::cleanup");
616
union_result=0; // Safety
618
free_tmp_table(thd, table);
622
for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
623
error|= sl->cleanup();
628
if ((join= fake_select_lex->join))
630
join->tables_list= 0;
633
error|= fake_select_lex->cleanup();
634
if (fake_select_lex->order_list.elements)
637
for (ord= (ORDER*)fake_select_lex->order_list.first; ord; ord= ord->next)
638
(*ord->item)->cleanup();
646
void st_select_lex_unit::reinit_exec_mechanism()
648
prepared= optimized= executed= 0;
652
List_iterator_fast<Item> it(item_list);
654
while ((field= it++))
657
we can't cleanup here, because it broke link to temporary table field,
658
but have to drop fixed flag to allow next fix_field of this field
669
change select_result object of unit
672
st_select_lex_unit::change_result()
673
result new select_result object
674
old_result old select_result object
681
bool st_select_lex_unit::change_result(select_result_interceptor *new_result,
682
select_result_interceptor *old_result)
685
for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
687
if (sl->join && sl->join->result == old_result)
688
if (sl->join->change_result(new_result))
691
if (fake_select_lex && fake_select_lex->join)
692
res= fake_select_lex->join->change_result(new_result);
697
Get column type information for this unit.
700
st_select_lex_unit::get_unit_column_types()
703
For a single-select the column types are taken
704
from the list of selected items. For a union this function
705
assumes that st_select_lex_unit::prepare has been called
706
and returns the type holders that were created for unioned
707
column types of all selects.
710
The implementation of this function should be in sync with
711
st_select_lex_unit::prepare()
714
List<Item> *st_select_lex_unit::get_unit_column_types()
716
SELECT_LEX *sl= first_select();
720
DBUG_ASSERT(prepared);
721
/* Types are generated during prepare */
725
return &sl->item_list;
728
bool st_select_lex::cleanup()
731
DBUG_ENTER("st_select_lex::cleanup()");
735
DBUG_ASSERT((st_select_lex*)join->select_lex == this);
736
error= join->destroy();
740
for (SELECT_LEX_UNIT *lex_unit= first_inner_unit(); lex_unit ;
741
lex_unit= lex_unit->next_unit())
743
error= (bool) ((uint) error | (uint) lex_unit->cleanup());
745
non_agg_fields.empty();
746
inner_refs_list.empty();
751
void st_select_lex::cleanup_all_joins(bool full)
753
SELECT_LEX_UNIT *unit;
759
for (unit= first_inner_unit(); unit; unit= unit->next_unit())
760
for (sl= unit->first_select(); sl; sl= sl->next_select())
761
sl->cleanup_all_joins(full);