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))
357
create_options= (first_sl->options | session_arg->options |
358
TMP_TABLE_ALL_COLUMNS);
358
create_options= first_sl->options | session_arg->options | TMP_TABLE_ALL_COLUMNS;
360
if (union_result->create_result_table(session, &types, test(union_distinct),
360
if (union_result->create_result_table(session, &types, test(union_distinct), create_options, ""))
363
362
memset(&result_table_list, 0, sizeof(result_table_list));
364
result_table_list.setSchemaName((char*) "");
363
result_table_list.setSchemaName("");
365
364
result_table_list.alias= "union";
366
result_table_list.setTableName((char *) "union");
365
result_table_list.setTableName("union");
367
366
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);
368
session_arg->lex().current_select= lex_select_save;
369
if (item_list.is_empty())
370
table->fill_item_list(item_list);
379
374
We're in execution of a prepared statement or stored procedure:
380
375
reset field items to point at fields from the created temporary table.
386
session_arg->lex->current_select= lex_select_save;
381
session_arg->lex().current_select= lex_select_save;
388
383
return(saved_error || session_arg->is_fatal_error);
391
session_arg->lex->current_select= lex_select_save;
386
session_arg->lex().current_select= lex_select_save;
396
391
bool Select_Lex_Unit::exec()
398
Select_Lex *lex_select_save= session->lex->current_select;
393
Select_Lex *lex_select_save= session->lex().current_select;
399
394
Select_Lex *select_cursor=first_select();
400
395
uint64_t add_rows=0;
401
396
ha_rows examined_rows= 0;
425
420
for (Select_Lex *sl= select_cursor; sl; sl= sl->next_select())
427
422
ha_rows records_at_start= 0;
428
session->lex->current_select= sl;
423
session->lex().current_select= sl;
431
426
saved_error= sl->join->reinit();
473
468
examined_rows+= session->examined_row_count;
474
469
if (union_result->flush())
476
session->lex->current_select= lex_select_save;
471
session->lex().current_select= lex_select_save;
483
session->lex->current_select= lex_select_save;
478
session->lex().current_select= lex_select_save;
484
479
return(saved_error);
486
481
/* Needed for the following test and for records_at_start in next loop */
524
519
don't let it allocate the join. Perhaps this is because we need
525
520
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;
522
fake_select_lex->join= new Join(session, item_list, fake_select_lex->options, result);
523
fake_select_lex->join->no_const_tables= true;
536
526
Fake Select_Lex should have item list for correctref_array
540
530
saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
541
531
&result_table_list,
542
532
0, item_list, NULL,
543
global_parameters->order_list.elements,
533
global_parameters->order_list.size(),
544
534
(Order*)global_parameters->order_list.first,
545
535
(Order*) NULL, NULL,
546
536
fake_select_lex->options | SELECT_NO_UNLOCK,
563
553
saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
564
554
&result_table_list,
565
555
0, item_list, NULL,
566
global_parameters->order_list.elements,
556
global_parameters->order_list.size(),
567
557
(Order*)global_parameters->order_list.first,
568
558
(Order*) NULL, NULL,
569
559
fake_select_lex->options | SELECT_NO_UNLOCK,
710
699
assert((Select_Lex*)join->select_lex == this);
711
700
error= join->destroy();
715
703
for (Select_Lex_Unit *lex_unit= first_inner_unit(); lex_unit ;
716
704
lex_unit= lex_unit->next_unit())
718
706
error= (bool) ((uint32_t) error | (uint32_t) lex_unit->cleanup());
720
non_agg_fields.empty();
721
inner_refs_list.empty();
708
non_agg_fields.clear();
709
inner_refs_list.clear();