24 #include "sql_union.h"
30 #include "sql_optimizer.h"
31 #include "opt_explain_format.h"
33 bool mysql_union(THD *thd, LEX *lex, select_result *result,
34 SELECT_LEX_UNIT *unit, ulong setup_tables_done_option)
37 DBUG_ENTER(
"mysql_union");
39 res= unit->prepare(thd, result,
40 SELECT_NO_UNLOCK | setup_tables_done_option);
54 DBUG_ASSERT(! thd->lex->is_query_tables_locked());
55 if (
lock_tables(thd, lex->query_tables, lex->table_count, 0))
63 query_cache_store_query(thd, thd->lex->query_tables);
65 res= unit->optimize() || unit->exec();
66 res|= unit->cleanup();
69 (void) unit->cleanup();
78 int select_union::prepare(
List<Item> &list, SELECT_LEX_UNIT *u)
85 bool select_union::send_data(
List<Item> &values)
88 if (unit->offset_limit_cnt)
90 unit->offset_limit_cnt--;
93 fill_record(thd,
table->field, values, 1, NULL);
97 if ((error=
table->file->ha_write_row(
table->record[0])))
100 if (
table->file->is_fatal_error(error, HA_CHECK_DUP) &&
101 create_myisam_from_heap(thd,
table, tmp_table_param.start_recinfo,
102 &tmp_table_param.recinfo, error, TRUE, NULL))
109 bool select_union::send_eof()
115 bool select_union::flush()
118 if ((error=
table->file->extra(HA_EXTRA_NO_CACHE)))
120 table->file->print_error(error, MYF(0));
150 select_union::create_result_table(THD *thd_arg,
List<Item> *column_types,
151 bool is_union_distinct, ulonglong options,
152 const char *table_alias,
155 DBUG_ASSERT(
table == 0);
156 tmp_table_param.init();
157 tmp_table_param.field_count= column_types->elements;
159 tmp_table_param.bit_fields_as_long= bit_fields_as_long;
161 if (! (
table= create_tmp_table(thd_arg, &tmp_table_param, *column_types,
162 (
ORDER*) 0, is_union_distinct, 1,
163 options, HA_POS_ERROR, (
char*) table_alias)))
167 table->file->extra(HA_EXTRA_WRITE_CACHE);
168 table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
181 void select_union::cleanup()
183 table->file->extra(HA_EXTRA_RESET_STATE);
184 table->file->ha_delete_all_rows();
185 free_io_cache(
table);
186 filesort_free_buffers(
table,0);
202 st_select_lex_unit::init_prepare_fake_select_lex(THD *thd_arg,
203 bool no_const_tables)
205 DBUG_ENTER(
"st_select_lex_unit::init_prepare_fake_select_lex");
206 thd_arg->lex->current_select= fake_select_lex;
207 fake_select_lex->table_list.link_in_list(&result_table_list,
208 &result_table_list.next_local);
209 fake_select_lex->context.table_list=
210 fake_select_lex->context.first_name_resolution_table=
211 fake_select_lex->get_table_list();
212 if (!fake_select_lex->first_execution)
214 for (
ORDER *order= global_parameters->order_list.first;
217 order->item= &order->item_ptr;
219 for (
ORDER *order= global_parameters->order_list.first;
223 (*order->item)->walk(&Item::change_context_processor, 0,
224 (uchar*) &fake_select_lex->context);
226 if (!fake_select_lex->join)
236 if (!(fake_select_lex->join=
237 new JOIN(thd, item_list, fake_select_lex->options, result)))
239 fake_select_lex->table_list.empty();
242 fake_select_lex->join->init(thd, item_list, fake_select_lex->options,
244 fake_select_lex->join->no_const_tables= no_const_tables;
250 fake_select_lex->item_list= item_list;
258 if (fake_select_lex->ref_pointer_array.is_null())
259 fake_select_lex->n_child_sum_items+= global_parameters->n_sum_items;
265 bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result,
266 ulong additional_options)
268 SELECT_LEX *lex_select_save= thd_arg->lex->current_select;
269 SELECT_LEX *sl, *first_sl= first_select();
270 select_result *tmp_result;
271 bool is_union_select;
272 DBUG_ENTER(
"st_select_lex_unit::prepare");
274 describe=
test(additional_options & SELECT_DESCRIBE);
287 for (sl= first_sl; sl; sl= sl->next_select())
289 sl->join->result= result;
290 select_limit_cnt= HA_POS_ERROR;
292 if (result->prepare(sl->join->fields_list,
this))
296 sl->join->select_options|= SELECT_DESCRIBE;
299 if (fake_select_lex->join)
300 fake_select_lex->join->result= result;
307 thd_arg->lex->current_select= sl= first_sl;
308 found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
309 is_union_select= is_union() || fake_select_lex;
315 if (!(tmp_result= union_result=
new select_union))
318 tmp_result= sel_result;
321 tmp_result= sel_result;
323 sl->context.resolve_in_select_list= TRUE;
325 for (;sl; sl= sl->next_select())
327 bool can_skip_order_by;
328 sl->options|= SELECT_NO_UNLOCK;
329 JOIN *join=
new JOIN(thd_arg, sl->item_list,
330 sl->options | thd_arg->variables.option_bits | additional_options,
342 thd_arg->lex->current_select= sl;
344 can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
346 saved_error= join->
prepare(sl->table_list.first,
349 (can_skip_order_by ? 0 :
350 sl->order_list.elements) +
351 sl->group_list.elements,
353 NULL : sl->order_list.first,
354 sl->group_list.first,
360 if (saved_error || (saved_error= thd_arg->is_fatal_error))
366 if (!is_union_select)
367 types= first_sl->item_list;
368 else if (sl == first_sl)
373 while ((item_tmp= it++))
379 if (thd_arg->is_fatal_error)
384 if (types.elements != sl->item_list.elements)
386 my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
387 ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
393 while ((type= tp++, item_tmp= it++))
413 ulonglong create_options;
417 if (type->result_type() == STRING_RESULT &&
418 type->collation.derivation == DERIVATION_NONE)
420 my_error(ER_CANT_AGGREGATE_NCOLLATIONS, MYF(0),
"UNION");
438 if (global_parameters->ftfunc_list->elements &&
439 global_parameters->order_list.elements &&
440 global_parameters != fake_select_lex)
443 Item_func::Functype ft= Item_func::FT_FUNC;
444 for (ord= global_parameters->order_list.first; ord; ord= ord->next)
448 my_error (ER_CANT_USE_OPTION_HERE, MYF(0),
"MATCH()");
454 create_options= (first_sl->options | thd_arg->variables.option_bits |
455 TMP_TABLE_ALL_COLUMNS);
462 if (global_parameters->ftfunc_list->elements)
465 if (union_result->create_result_table(thd, &types,
test(union_distinct),
466 create_options,
"", FALSE, TRUE))
468 memset(&result_table_list, 0,
sizeof(result_table_list));
469 result_table_list.db= (
char*)
"";
470 result_table_list.table_name= result_table_list.alias= (
char*)
"union";
471 result_table_list.table=
table= union_result->table;
473 thd_arg->lex->current_select= lex_select_save;
474 if (!item_list.elements)
477 Prepared_stmt_arena_holder ps_arena_holder(thd);
479 saved_error=
table->fill_item_list(&item_list);
485 if (thd->stmt_arena->is_stmt_prepare())
488 init_prepare_fake_select_lex(thd,
false);
490 saved_error= fake_select_lex->join->
491 prepare(fake_select_lex->table_list.first,
494 global_parameters->order_list.elements,
495 global_parameters->order_list.first,
500 fake_select_lex->table_list.empty();
509 table->reset_item_list(&item_list);
513 thd_arg->lex->current_select= lex_select_save;
515 DBUG_RETURN(saved_error || thd_arg->is_fatal_error);
518 thd_arg->lex->current_select= lex_select_save;
531 bool st_select_lex_unit::optimize()
533 DBUG_ENTER(
"st_select_lex_unit::optimize");
535 if (optimized && item && item->assigned() && !uncacheable && !describe)
538 for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
540 DBUG_ASSERT(sl->join);
548 SELECT_LEX *lex_select_save= thd->lex->current_select;
549 thd->lex->current_select= sl;
551 if ((sl == global_parameters && is_union()) || describe)
558 if (sl->order_list.first || describe)
559 select_limit_cnt= HA_POS_ERROR;
567 sl->join->select_options=
568 (select_limit_cnt == HA_POS_ERROR || sl->braces) ?
569 sl->options & ~OPTION_FOUND_ROWS : sl->options | found_rows_for_union;
571 saved_error= sl->join->optimize();
576 result->estimated_rowcount+=
577 sl->with_sum_func && sl->group_list.elements == 0 ?
578 1 : sl->join->best_rowcount;
580 thd->lex->current_select= lex_select_save;
588 DBUG_RETURN(saved_error);
596 bool st_select_lex_unit::explain()
598 SELECT_LEX *lex_select_save= thd->lex->current_select;
600 DBUG_ENTER(
"st_select_lex_unit::explain");
604 DBUG_ASSERT((is_union() || fake_select_lex) && describe && optimized);
610 for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
614 DBUG_ASSERT(sl->join);
615 if (sl->join->explain() || thd->is_error())
621 if (init_prepare_fake_select_lex(thd,
true))
624 if (thd->is_fatal_error)
626 join= fake_select_lex->join;
642 &global_parameters->order_list,
644 fake_select_lex->options | SELECT_NO_UNLOCK,
645 result,
this, fake_select_lex);
650 thd->lex->current_select= lex_select_save;
652 if (saved_error || ret || thd->is_error())
664 bool st_select_lex_unit::exec()
666 SELECT_LEX *lex_select_save= thd->lex->current_select;
667 ulonglong add_rows=0;
668 ha_rows examined_rows= 0;
669 DBUG_ENTER(
"st_select_lex_unit::exec");
670 DBUG_ASSERT((is_union() || fake_select_lex) && !describe && optimized);
672 if (executed && !uncacheable)
676 if (uncacheable || !item || !item->assigned())
679 item->reset_value_registration();
680 if (optimized && item)
682 if (item->assigned())
686 table->file->ha_delete_all_rows();
689 if (union_distinct &&
table->file->ha_enable_indexes(HA_KEY_SWITCH_ALL))
695 for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
697 ha_rows records_at_start= 0;
698 DBUG_ASSERT(sl->join);
699 thd->lex->current_select= sl;
702 if (sl == global_parameters || describe)
709 if (sl->order_list.first || describe)
710 select_limit_cnt= HA_POS_ERROR;
714 records_at_start=
table->file->stats.records;
716 if (sl == union_distinct)
718 if (
table->file->ha_disable_indexes(HA_KEY_SWITCH_ALL))
722 saved_error= sl->join->error;
723 offset_limit_cnt= (ha_rows)(sl->offset_limit ?
724 sl->offset_limit->val_uint() :
728 examined_rows+= thd->get_examined_row_count();
729 if (union_result->flush())
731 thd->lex->current_select= lex_select_save;
738 thd->lex->current_select= lex_select_save;
739 DBUG_RETURN(saved_error);
742 int error=
table->file->info(HA_STATUS_VARIABLE);
745 table->file->print_error(error, MYF(0));
748 if (found_rows_for_union && !sl->braces &&
749 select_limit_cnt != HA_POS_ERROR)
757 add_rows+= (ulonglong) (thd->limit_found_rows -
758 (ulonglong)(
table->file->stats.records - records_at_start));
763 if (!saved_error && !thd->is_fatal_error)
770 set_limit(global_parameters);
771 if (init_prepare_fake_select_lex(thd,
true))
773 JOIN *join= fake_select_lex->join;
782 &global_parameters->order_list,
785 fake_select_lex->options | SELECT_NO_UNLOCK,
792 join->examined_rows= 0;
798 fake_select_lex->table_list.empty();
800 if (!saved_error && !thd->is_fatal_error)
803 thd->limit_found_rows = (ulonglong)
table->file->stats.records + add_rows;
804 thd->inc_examined_row_count(examined_rows);
806 thd->lex->current_select= lex_select_save;
807 DBUG_RETURN(saved_error);
820 bool st_select_lex_unit::cleanup()
823 DBUG_ENTER(
"st_select_lex_unit::cleanup");
831 for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
832 error|= sl->cleanup();
846 bool st_select_lex_unit::cleanup_level()
852 error|= fake_select_lex->cleanup();
864 DBUG_ASSERT(global_parameters);
865 if (global_parameters->order_list.elements)
868 for (ord= global_parameters->order_list.first; ord; ord= ord->next)
878 free_tmp_table(thd,
table);
882 explain_marker= CTX_NONE;
888 void st_select_lex_unit::reinit_exec_mechanism()
890 prepared= optimized= executed= 0;
896 while ((field= it++))
923 bool st_select_lex_unit::change_result(select_result_interceptor *new_result,
924 select_result_interceptor *old_result)
927 for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
929 if (sl->join && sl->join->result == old_result)
930 if (sl->join->change_result(new_result))
933 if (fake_select_lex && fake_select_lex->join)
934 res= fake_select_lex->join->change_result(new_result);
956 List<Item> *st_select_lex_unit::get_unit_column_types()
960 DBUG_ASSERT(prepared);
965 return &first_select()->item_list;
980 List<Item> *st_select_lex_unit::get_field_list()
984 DBUG_ASSERT(prepared);
989 return first_select()->join->fields;
999 bool st_select_lex::cleanup()
1002 DBUG_ENTER(
"st_select_lex::cleanup()");
1004 error= cleanup_level();
1005 for (SELECT_LEX_UNIT *lex_unit= first_inner_unit(); lex_unit;
1006 lex_unit= lex_unit->next_unit())
1008 error|= lex_unit->cleanup();
1021 bool st_select_lex::cleanup_level()
1027 DBUG_ASSERT((st_select_lex*)join->
select_lex ==
this);
1033 cur_pos_in_all_fields= ALL_FIELDS_UNDEF_POS;
1034 non_agg_fields.empty();
1035 inner_refs_list.empty();
1041 void st_select_lex::cleanup_all_joins(
bool full)
1043 SELECT_LEX_UNIT *unit;
1049 for (unit= first_inner_unit(); unit; unit= unit->next_unit())
1050 for (sl= unit->first_select(); sl; sl= sl->next_select())
1051 sl->cleanup_all_joins(full);