18
18
UNION's were introduced by Monty and Sinisa <sinisa@mysql.com>
21
22
#include <drizzled/sql_select.h>
22
23
#include <drizzled/error.h>
23
24
#include <drizzled/item/type_holder.h>
24
25
#include <drizzled/sql_base.h>
25
26
#include <drizzled/sql_union.h>
27
#include <drizzled/select_union.h>
28
#include <drizzled/sql_lex.h>
29
#include <drizzled/session.h>
30
#include <drizzled/item/subselect.h>
30
34
bool drizzle_union(Session *session, LEX *, select_result *result,
31
35
Select_Lex_Unit *unit, uint64_t setup_tables_done_option)
34
if (!(res= unit->prepare(session, result, SELECT_NO_UNLOCK |
35
setup_tables_done_option)))
37
bool res= unit->prepare(session, result, SELECT_NO_UNLOCK | setup_tables_done_option);
38
res|= unit->cleanup();
123
126
assert(table == NULL);
124
127
tmp_table_param.init();
125
tmp_table_param.field_count= column_types->elements;
128
tmp_table_param.field_count= column_types->size();
127
130
if (! (table= create_tmp_table(session_arg, &tmp_table_param, *column_types,
128
131
(Order*) NULL, is_union_distinct, 1,
169
172
Select_Lex_Unit::init_prepare_fake_select_lex(Session *session_arg)
171
session_arg->lex->current_select= fake_select_lex;
174
session_arg->lex().current_select= fake_select_lex;
172
175
fake_select_lex->table_list.link_in_list((unsigned char *)&result_table_list,
173
176
(unsigned char **)
174
177
&result_table_list.next_local);
194
197
bool Select_Lex_Unit::prepare(Session *session_arg, select_result *sel_result,
195
198
uint64_t additional_options)
197
Select_Lex *lex_select_save= session_arg->lex->current_select;
200
Select_Lex *lex_select_save= session_arg->lex().current_select;
198
201
Select_Lex *sl, *first_sl= first_select();
199
202
select_result *tmp_result;
200
203
bool is_union_select;
220
223
offset_limit_cnt= 0;
221
224
if (result->prepare(sl->join->fields_list, this))
225
228
sl->join->select_options|= SELECT_DESCRIBE;
226
229
sl->join->reinit();
232
235
saved_error= false;
234
session_arg->lex->current_select= sl= first_sl;
237
session_arg->lex().current_select= sl= first_sl;
235
238
found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
236
239
is_union_select= is_union() || fake_select_lex;
302
304
field object without table.
304
306
assert(!empty_table);
305
empty_table= (Table*) session->calloc(sizeof(Table));
307
List_iterator_fast<Item> it(sl->item_list);
309
while ((item_tmp= it++))
307
empty_table= (Table*) session->mem.calloc(sizeof(Table));
309
List<Item>::iterator it(sl->item_list.begin());
310
while (Item* item_tmp= it++)
311
312
/* Error's in 'new' will be detected after loop */
312
313
types.push_back(new Item_type_holder(session_arg, item_tmp));
320
if (types.elements != sl->item_list.elements)
321
if (types.size() != sl->item_list.size())
322
323
my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
323
324
ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
326
List_iterator_fast<Item> it(sl->item_list);
327
List_iterator_fast<Item> tp(types);
327
List<Item>::iterator it(sl->item_list.begin());
328
List<Item>::iterator tp(types.begin());
328
329
Item *type, *item_tmp;
329
330
while ((type= tp++, item_tmp= it++))
331
332
if (((Item_type_holder*)type)->join_types(session_arg, item_tmp))
361
362
create_options, ""))
363
364
memset(&result_table_list, 0, sizeof(result_table_list));
364
result_table_list.setSchemaName((char*) "");
365
result_table_list.setSchemaName("", 0);
365
366
result_table_list.alias= "union";
366
result_table_list.setTableName((char *) "union");
367
result_table_list.setTableName("union", 5);
367
368
result_table_list.table= table= union_result->table;
369
session_arg->lex->current_select= lex_select_save;
370
if (!item_list.elements)
372
saved_error= table->fill_item_list(&item_list);
370
session_arg->lex().current_select= lex_select_save;
371
if (item_list.is_empty())
372
table->fill_item_list(item_list);
379
376
We're in execution of a prepared statement or stored procedure:
380
377
reset field items to point at fields from the created temporary table.
386
session_arg->lex->current_select= lex_select_save;
383
session_arg->lex().current_select= lex_select_save;
388
385
return(saved_error || session_arg->is_fatal_error);
391
session_arg->lex->current_select= lex_select_save;
388
session_arg->lex().current_select= lex_select_save;
396
393
bool Select_Lex_Unit::exec()
398
Select_Lex *lex_select_save= session->lex->current_select;
395
Select_Lex *lex_select_save= session->lex().current_select;
399
396
Select_Lex *select_cursor=first_select();
400
397
uint64_t add_rows=0;
401
398
ha_rows examined_rows= 0;
425
422
for (Select_Lex *sl= select_cursor; sl; sl= sl->next_select())
427
424
ha_rows records_at_start= 0;
428
session->lex->current_select= sl;
425
session->lex().current_select= sl;
431
428
saved_error= sl->join->reinit();
473
470
examined_rows+= session->examined_row_count;
474
471
if (union_result->flush())
476
session->lex->current_select= lex_select_save;
473
session->lex().current_select= lex_select_save;
483
session->lex->current_select= lex_select_save;
480
session->lex().current_select= lex_select_save;
484
481
return(saved_error);
486
483
/* Needed for the following test and for records_at_start in next loop */
520
517
allocate JOIN for fake select only once (prevent
521
mysql_select automatic allocation)
522
TODO: The above is nonsense. mysql_select() will not allocate the
518
select_query automatic allocation)
519
TODO: The above is nonsense. select_query() will not allocate the
523
520
join if one already exists. There must be some other reason why we
524
521
don't let it allocate the join. Perhaps this is because we need
525
522
some special parameter values passed to join constructor?
527
if (!(fake_select_lex->join= new Join(session, item_list,
528
fake_select_lex->options, result)))
530
fake_select_lex->table_list.empty();
533
fake_select_lex->join->no_const_tables= true;
524
fake_select_lex->join= new Join(session, item_list, fake_select_lex->options, result);
525
fake_select_lex->join->no_const_tables= true;
536
528
Fake Select_Lex should have item list for correctref_array
539
531
fake_select_lex->item_list= item_list;
540
saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
532
saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
541
533
&result_table_list,
542
534
0, item_list, NULL,
543
global_parameters->order_list.elements,
535
global_parameters->order_list.size(),
544
536
(Order*)global_parameters->order_list.first,
545
537
(Order*) NULL, NULL,
546
538
fake_select_lex->options | SELECT_NO_UNLOCK,
560
552
to reset them back, we re-do all of the actions (yes it is ugly):
562
554
join->reset(session, item_list, fake_select_lex->options, result);
563
saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
555
saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
564
556
&result_table_list,
565
557
0, item_list, NULL,
566
global_parameters->order_list.elements,
558
global_parameters->order_list.size(),
567
559
(Order*)global_parameters->order_list.first,
568
560
(Order*) NULL, NULL,
569
561
fake_select_lex->options | SELECT_NO_UNLOCK,
710
701
assert((Select_Lex*)join->select_lex == this);
711
702
error= join->destroy();
715
705
for (Select_Lex_Unit *lex_unit= first_inner_unit(); lex_unit ;
716
706
lex_unit= lex_unit->next_unit())
718
708
error= (bool) ((uint32_t) error | (uint32_t) lex_unit->cleanup());
720
non_agg_fields.empty();
721
inner_refs_list.empty();
710
non_agg_fields.clear();
711
inner_refs_list.clear();