MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_union.cc
1 /* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /*
17  UNION of select's
18  UNION's were introduced by Monty and Sinisa <sinisa@mysql.com>
19 */
20 
21 
22 #include "sql_priv.h"
23 #include "unireg.h"
24 #include "sql_union.h"
25 #include "sql_select.h"
26 #include "sql_cursor.h"
27 #include "sql_base.h" // fill_record
28 #include "filesort.h" // filesort_free_buffers
29 #include "sql_tmp_table.h" // tmp tables
30 #include "sql_optimizer.h" // JOIN
31 #include "opt_explain_format.h"
32 
33 bool mysql_union(THD *thd, LEX *lex, select_result *result,
34  SELECT_LEX_UNIT *unit, ulong setup_tables_done_option)
35 {
36  bool res;
37  DBUG_ENTER("mysql_union");
38 
39  res= unit->prepare(thd, result,
40  SELECT_NO_UNLOCK | setup_tables_done_option);
41  if (res)
42  goto err;
43 
44 
45  /*
46  Tables are not locked at this point, it means that we have delayed
47  this step until after prepare stage (i.e. this moment). This allows to
48  do better partition pruning and avoid locking unused partitions.
49  As a consequence, in such a case, prepare stage can rely only on
50  metadata about tables used and not data from them.
51  We need to lock tables now in order to proceed with the remaning
52  stages of query optimization and execution.
53  */
54  DBUG_ASSERT(! thd->lex->is_query_tables_locked());
55  if (lock_tables(thd, lex->query_tables, lex->table_count, 0))
56  goto err;
57 
58  /*
59  Tables must be locked before storing the query in the query cache.
60  Transactional engines must been signalled that the statement started,
61  which external_lock signals.
62  */
63  query_cache_store_query(thd, thd->lex->query_tables);
64 
65  res= unit->optimize() || unit->exec();
66  res|= unit->cleanup();
67  DBUG_RETURN(res);
68 err:
69  (void) unit->cleanup();
70  DBUG_RETURN(true);
71 }
72 
73 
74 /***************************************************************************
75 ** store records in temporary table for UNION
76 ***************************************************************************/
77 
78 int select_union::prepare(List<Item> &list, SELECT_LEX_UNIT *u)
79 {
80  unit= u;
81  return 0;
82 }
83 
84 
85 bool select_union::send_data(List<Item> &values)
86 {
87  int error= 0;
88  if (unit->offset_limit_cnt)
89  { // using limit offset,count
90  unit->offset_limit_cnt--;
91  return 0;
92  }
93  fill_record(thd, table->field, values, 1, NULL);
94  if (thd->is_error())
95  return 1;
96 
97  if ((error= table->file->ha_write_row(table->record[0])))
98  {
99  /* create_myisam_from_heap will generate error if needed */
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))
103  return 1;
104  }
105  return 0;
106 }
107 
108 
109 bool select_union::send_eof()
110 {
111  return 0;
112 }
113 
114 
115 bool select_union::flush()
116 {
117  int error;
118  if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
119  {
120  table->file->print_error(error, MYF(0));
121  return 1;
122  }
123  return 0;
124 }
125 
126 /*
127  Create a temporary table to store the result of select_union.
128 
129  SYNOPSIS
130  select_union::create_result_table()
131  thd thread handle
132  column_types a list of items used to define columns of the
133  temporary table
134  is_union_distinct if set, the temporary table will eliminate
135  duplicates on insert
136  options create options
137  table_alias name of the temporary table
138  bit_fields_as_long convert bit fields to ulonglong
139 
140  DESCRIPTION
141  Create a temporary table that is used to store the result of a UNION,
142  derived table, or a materialized cursor.
143 
144  RETURN VALUE
145  0 The table has been created successfully.
146  1 create_tmp_table failed.
147 */
148 
149 bool
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,
153  bool bit_fields_as_long, bool create_table)
154 {
155  DBUG_ASSERT(table == 0);
156  tmp_table_param.init();
157  tmp_table_param.field_count= column_types->elements;
158  tmp_table_param.skip_create_table= !create_table;
159  tmp_table_param.bit_fields_as_long= bit_fields_as_long;
160 
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)))
164  return TRUE;
165  if (create_table)
166  {
167  table->file->extra(HA_EXTRA_WRITE_CACHE);
168  table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
169  }
170  return FALSE;
171 }
172 
173 
181 void select_union::cleanup()
182 {
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);
187 }
188 
189 
201 bool
202 st_select_lex_unit::init_prepare_fake_select_lex(THD *thd_arg,
203  bool no_const_tables)
204 {
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)
213  {
214  for (ORDER *order= global_parameters->order_list.first;
215  order;
216  order= order->next)
217  order->item= &order->item_ptr;
218  }
219  for (ORDER *order= global_parameters->order_list.first;
220  order;
221  order=order->next)
222  {
223  (*order->item)->walk(&Item::change_context_processor, 0,
224  (uchar*) &fake_select_lex->context);
225  }
226  if (!fake_select_lex->join)
227  {
228  /*
229  allocate JOIN for fake select only once (prevent
230  mysql_select automatic allocation)
231  TODO: The above is nonsense. mysql_select() will not allocate the
232  join if one already exists. There must be some other reason why we
233  don't let it allocate the join. Perhaps this is because we need
234  some special parameter values passed to join constructor?
235  */
236  if (!(fake_select_lex->join=
237  new JOIN(thd, item_list, fake_select_lex->options, result)))
238  {
239  fake_select_lex->table_list.empty();
240  DBUG_RETURN(true);
241  }
242  fake_select_lex->join->init(thd, item_list, fake_select_lex->options,
243  result);
244  fake_select_lex->join->no_const_tables= no_const_tables;
245 
246  /*
247  Fake st_select_lex should have item list for correct ref_array
248  allocation.
249  */
250  fake_select_lex->item_list= item_list;
251 
252  /*
253  We need to add up n_sum_items in order to make the correct
254  allocation in setup_ref_array().
255  Don't add more sum_items if we have already done JOIN::prepare
256  for this (with a different join object)
257  */
258  if (fake_select_lex->ref_pointer_array.is_null())
259  fake_select_lex->n_child_sum_items+= global_parameters->n_sum_items;
260  }
261  DBUG_RETURN(false);
262 }
263 
264 
265 bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result,
266  ulong additional_options)
267 {
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");
273 
274  describe= test(additional_options & SELECT_DESCRIBE);
275 
276  /*
277  result object should be reassigned even if preparing already done for
278  max/min subquery (ALL/ANY optimization)
279  */
280  result= sel_result;
281 
282  if (prepared)
283  {
284  if (describe)
285  {
286  /* fast reinit for EXPLAIN */
287  for (sl= first_sl; sl; sl= sl->next_select())
288  {
289  sl->join->result= result;
290  select_limit_cnt= HA_POS_ERROR;
291  offset_limit_cnt= 0;
292  if (result->prepare(sl->join->fields_list, this))
293  {
294  DBUG_RETURN(TRUE);
295  }
296  sl->join->select_options|= SELECT_DESCRIBE;
297  sl->join->reset();
298  }
299  if (fake_select_lex->join)
300  fake_select_lex->join->result= result;
301  }
302  DBUG_RETURN(FALSE);
303  }
304  prepared= 1;
305  saved_error= FALSE;
306 
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;
310 
311  /* Global option */
312 
313  if (is_union_select)
314  {
315  if (!(tmp_result= union_result= new select_union))
316  goto err;
317  if (describe)
318  tmp_result= sel_result;
319  }
320  else
321  tmp_result= sel_result;
322 
323  sl->context.resolve_in_select_list= TRUE;
324 
325  for (;sl; sl= sl->next_select())
326  {
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,
331  tmp_result);
332  /*
333  setup_tables_done_option should be set only for very first SELECT,
334  because it protect from secont setup_tables call for select-like non
335  select commands (DELETE/INSERT/...) and they use only very first
336  SELECT (for union it can be only INSERT ... SELECT).
337  */
338  additional_options&= ~OPTION_SETUP_TABLES_DONE;
339  if (!join)
340  goto err;
341 
342  thd_arg->lex->current_select= sl;
343 
344  can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
345 
346  saved_error= join->prepare(sl->table_list.first,
347  sl->with_wild,
348  sl->where,
349  (can_skip_order_by ? 0 :
350  sl->order_list.elements) +
351  sl->group_list.elements,
352  can_skip_order_by ?
353  NULL : sl->order_list.first,
354  sl->group_list.first,
355  sl->having,
356  sl, this);
357  /* There are no * in the statement anymore (for PS) */
358  sl->with_wild= 0;
359 
360  if (saved_error || (saved_error= thd_arg->is_fatal_error))
361  goto err;
362  /*
363  Use items list of underlaid select for derived tables to preserve
364  information about fields lengths and exact types
365  */
366  if (!is_union_select)
367  types= first_sl->item_list;
368  else if (sl == first_sl)
369  {
370  types.empty();
371  List_iterator_fast<Item> it(sl->item_list);
372  Item *item_tmp;
373  while ((item_tmp= it++))
374  {
375  /* Error's in 'new' will be detected after loop */
376  types.push_back(new Item_type_holder(thd_arg, item_tmp));
377  }
378 
379  if (thd_arg->is_fatal_error)
380  goto err; // out of memory
381  }
382  else
383  {
384  if (types.elements != sl->item_list.elements)
385  {
386  my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
387  ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
388  goto err;
389  }
390  List_iterator_fast<Item> it(sl->item_list);
391  List_iterator_fast<Item> tp(types);
392  Item *type, *item_tmp;
393  while ((type= tp++, item_tmp= it++))
394  {
395  if (((Item_type_holder*)type)->join_types(thd_arg, item_tmp))
396  DBUG_RETURN(TRUE);
397  }
398  }
399  }
400 
401  if (is_union_select)
402  {
403  /*
404  Check that it was possible to aggregate
405  all collations together for UNION.
406  We need this in case of UNION DISTINCT, to filter
407  out duplicates using the proper collation.
408 
409  TODO: consider removing this test in case of UNION ALL.
410  */
411  List_iterator_fast<Item> tp(types);
412  Item *type;
413  ulonglong create_options;
414 
415  while ((type= tp++))
416  {
417  if (type->result_type() == STRING_RESULT &&
418  type->collation.derivation == DERIVATION_NONE)
419  {
420  my_error(ER_CANT_AGGREGATE_NCOLLATIONS, MYF(0), "UNION");
421  goto err;
422  }
423  }
424 
425  /*
426  Disable the usage of fulltext searches in the last union branch.
427  This is a temporary 5.x limitation because of the way the fulltext
428  search functions are handled by the optimizer.
429  This is manifestation of the more general problems of "taking away"
430  parts of a SELECT statement post-fix_fields(). This is generally not
431  doable since various flags are collected in various places (e.g.
432  SELECT_LEX) that carry information about the presence of certain
433  expressions or constructs in the parts of the query.
434  When part of the query is taken away it's not clear how to "divide"
435  the meaning of these accumulated flags and what to carry over to the
436  recipient query (SELECT_LEX).
437  */
438  if (global_parameters->ftfunc_list->elements &&
439  global_parameters->order_list.elements &&
440  global_parameters != fake_select_lex)
441  {
442  ORDER *ord;
443  Item_func::Functype ft= Item_func::FT_FUNC;
444  for (ord= global_parameters->order_list.first; ord; ord= ord->next)
445  if ((*ord->item)->walk (&Item::find_function_processor, FALSE,
446  (uchar *) &ft))
447  {
448  my_error (ER_CANT_USE_OPTION_HERE, MYF(0), "MATCH()");
449  goto err;
450  }
451  }
452 
453 
454  create_options= (first_sl->options | thd_arg->variables.option_bits |
455  TMP_TABLE_ALL_COLUMNS);
456  /*
457  Force the temporary table to be a MyISAM table if we're going to use
458  fullext functions (MATCH ... AGAINST .. IN BOOLEAN MODE) when reading
459  from it (this should be removed in 5.2 when fulltext search is moved
460  out of MyISAM).
461  */
462  if (global_parameters->ftfunc_list->elements)
463  create_options= create_options | TMP_TABLE_FORCE_MYISAM;
464 
465  if (union_result->create_result_table(thd, &types, test(union_distinct),
466  create_options, "", FALSE, TRUE))
467  goto err;
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;
472 
473  thd_arg->lex->current_select= lex_select_save;
474  if (!item_list.elements)
475  {
476  {
477  Prepared_stmt_arena_holder ps_arena_holder(thd);
478 
479  saved_error= table->fill_item_list(&item_list);
480 
481  if (saved_error)
482  goto err;
483  }
484 
485  if (thd->stmt_arena->is_stmt_prepare())
486  {
487  /* Validate the global parameters of this union */
488  init_prepare_fake_select_lex(thd, false);
489 
490  saved_error= fake_select_lex->join->
491  prepare(fake_select_lex->table_list.first, // tables_init
492  0, // wild_num
493  0, // conds_init
494  global_parameters->order_list.elements, // og_num
495  global_parameters->order_list.first, // order
496  NULL, // group_init
497  NULL, // having_init
498  fake_select_lex, // select_lex_arg
499  this); // unit_arg
500  fake_select_lex->table_list.empty();
501  }
502  }
503  else
504  {
505  /*
506  We're in execution of a prepared statement or stored procedure:
507  reset field items to point at fields from the created temporary table.
508  */
509  table->reset_item_list(&item_list);
510  }
511  }
512 
513  thd_arg->lex->current_select= lex_select_save;
514 
515  DBUG_RETURN(saved_error || thd_arg->is_fatal_error);
516 
517 err:
518  thd_arg->lex->current_select= lex_select_save;
519  (void) cleanup();
520  DBUG_RETURN(TRUE);
521 }
522 
523 
531 bool st_select_lex_unit::optimize()
532 {
533  DBUG_ENTER("st_select_lex_unit::optimize");
534 
535  if (optimized && item && item->assigned() && !uncacheable && !describe)
536  DBUG_RETURN(FALSE);
537 
538  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
539  {
540  DBUG_ASSERT(sl->join);
541  if (optimized)
542  {
543  saved_error= false;
544  sl->join->reset();
545  }
546  else
547  {
548  SELECT_LEX *lex_select_save= thd->lex->current_select;
549  thd->lex->current_select= sl;
550  set_limit(sl);
551  if ((sl == global_parameters && is_union()) || describe)
552  {
553  offset_limit_cnt= 0;
554  /*
555  We can't use LIMIT at this stage if we are using ORDER BY for the
556  whole UNION.
557  */
558  if (sl->order_list.first || describe)
559  select_limit_cnt= HA_POS_ERROR;
560  }
561 
562  /*
563  When using braces, SQL_CALC_FOUND_ROWS affects the whole query:
564  we don't calculate found_rows() per union part.
565  Otherwise, SQL_CALC_FOUND_ROWS should be done on all sub parts.
566  */
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;
570 
571  saved_error= sl->join->optimize();
572  /*
573  Accumulate estimated number of rows. Notice that an implicitly grouped
574  query has one row (with HAVING it has zero or one rows).
575  */
576  result->estimated_rowcount+=
577  sl->with_sum_func && sl->group_list.elements == 0 ?
578  1 : sl->join->best_rowcount;
579 
580  thd->lex->current_select= lex_select_save;
581  }
582  if (saved_error)
583  break;
584  }
585  if (!saved_error)
586  optimized= 1;
587 
588  DBUG_RETURN(saved_error);
589 }
590 
591 
596 bool st_select_lex_unit::explain()
597 {
598  SELECT_LEX *lex_select_save= thd->lex->current_select;
599  Explain_format *fmt= thd->lex->explain_format;
600  DBUG_ENTER("st_select_lex_unit::explain");
601  JOIN *join;
602  bool ret= false;
603 
604  DBUG_ASSERT((is_union() || fake_select_lex) && describe && optimized);
605  executed= true;
606 
607  if (fmt->begin_context(CTX_UNION))
608  DBUG_RETURN(true);
609 
610  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
611  {
612  if (fmt->begin_context(CTX_QUERY_SPEC))
613  DBUG_RETURN(true);
614  DBUG_ASSERT(sl->join);
615  if (sl->join->explain() || thd->is_error())
616  DBUG_RETURN(true);
617  if (fmt->end_context(CTX_QUERY_SPEC))
618  DBUG_RETURN(true);
619  }
620 
621  if (init_prepare_fake_select_lex(thd, true))
622  DBUG_RETURN(true);
623 
624  if (thd->is_fatal_error)
625  DBUG_RETURN(true);
626  join= fake_select_lex->join;
627 
628  /*
629  In EXPLAIN command, constant subqueries that do not use any
630  tables are executed two times:
631  - 1st time is a real evaluation to get the subquery value
632  - 2nd time is to produce EXPLAIN output rows.
633  1st execution sets certain members (e.g. select_result) to perform
634  subquery execution rather than EXPLAIN line production. In order
635  to reset them back, we re-do all of the actions (yes it is ugly).
636  */
637  if (!join->optimized || !join->tables)
638  {
639  saved_error= mysql_select(thd,
640  &result_table_list,
641  0, item_list, NULL,
642  &global_parameters->order_list,
643  NULL, NULL,
644  fake_select_lex->options | SELECT_NO_UNLOCK,
645  result, this, fake_select_lex);
646  }
647  else
648  ret= join->explain();
649 
650  thd->lex->current_select= lex_select_save;
651 
652  if (saved_error || ret || thd->is_error())
653  DBUG_RETURN(true);
654  fmt->end_context(CTX_UNION);
655 
656  DBUG_RETURN(false);
657 }
658 
659 
664 bool st_select_lex_unit::exec()
665 {
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);
671 
672  if (executed && !uncacheable)
673  DBUG_RETURN(false);
674  executed= true;
675 
676  if (uncacheable || !item || !item->assigned())
677  {
678  if (item)
679  item->reset_value_registration();
680  if (optimized && item)
681  {
682  if (item->assigned())
683  {
684  item->assigned(0); // We will reinit & rexecute unit
685  item->reset();
686  table->file->ha_delete_all_rows();
687  }
688  /* re-enabling indexes for next subselect iteration */
689  if (union_distinct && table->file->ha_enable_indexes(HA_KEY_SWITCH_ALL))
690  {
691  DBUG_ASSERT(0);
692  }
693  }
694 
695  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
696  {
697  ha_rows records_at_start= 0;
698  DBUG_ASSERT(sl->join);
699  thd->lex->current_select= sl;
700 
701  set_limit(sl);
702  if (sl == global_parameters || describe)
703  {
704  offset_limit_cnt= 0;
705  /*
706  We can't use LIMIT at this stage if we are using ORDER BY for the
707  whole query
708  */
709  if (sl->order_list.first || describe)
710  select_limit_cnt= HA_POS_ERROR;
711  }
712  if (!saved_error)
713  {
714  records_at_start= table->file->stats.records;
715  sl->join->exec();
716  if (sl == union_distinct)
717  {
718  if (table->file->ha_disable_indexes(HA_KEY_SWITCH_ALL))
719  DBUG_RETURN(true);
720  table->no_keyread=1;
721  }
722  saved_error= sl->join->error;
723  offset_limit_cnt= (ha_rows)(sl->offset_limit ?
724  sl->offset_limit->val_uint() :
725  0);
726  if (!saved_error)
727  {
728  examined_rows+= thd->get_examined_row_count();
729  if (union_result->flush())
730  {
731  thd->lex->current_select= lex_select_save;
732  DBUG_RETURN(true);
733  }
734  }
735  }
736  if (saved_error)
737  {
738  thd->lex->current_select= lex_select_save;
739  DBUG_RETURN(saved_error);
740  }
741  /* Needed for the following test and for records_at_start in next loop */
742  int error= table->file->info(HA_STATUS_VARIABLE);
743  if(error)
744  {
745  table->file->print_error(error, MYF(0));
746  DBUG_RETURN(true);
747  }
748  if (found_rows_for_union && !sl->braces &&
749  select_limit_cnt != HA_POS_ERROR)
750  {
751  /*
752  This is a union without braces. Remember the number of rows that
753  could also have been part of the result set.
754  We get this from the difference of between total number of possible
755  rows and actual rows added to the temporary table.
756  */
757  add_rows+= (ulonglong) (thd->limit_found_rows -
758  (ulonglong)(table->file->stats.records - records_at_start));
759  }
760  }
761  }
762 
763  if (!saved_error && !thd->is_fatal_error)
764  {
765  /* Send result to 'result' */
766  saved_error= true;
767  List<Item_func_match> empty_list;
768  empty_list.empty();
769 
770  set_limit(global_parameters);
771  if (init_prepare_fake_select_lex(thd, true))
772  DBUG_RETURN(true);
773  JOIN *join= fake_select_lex->join;
774  if (!join->optimized)
775  {
776  saved_error=
777  mysql_select(thd,
778  &result_table_list, // tables
779  0, // wild_num
780  item_list, // fields
781  NULL, // conds
782  &global_parameters->order_list, // order
783  NULL, // group
784  NULL, // having
785  fake_select_lex->options | SELECT_NO_UNLOCK,
786  result, // result
787  this, // unit
788  fake_select_lex); // select_lex
789  }
790  else
791  {
792  join->examined_rows= 0;
793  saved_error= false;
794  join->reset();
795  join->exec();
796  }
797 
798  fake_select_lex->table_list.empty();
799  }
800  if (!saved_error && !thd->is_fatal_error)
801  {
802 
803  thd->limit_found_rows = (ulonglong)table->file->stats.records + add_rows;
804  thd->inc_examined_row_count(examined_rows);
805  }
806  thd->lex->current_select= lex_select_save;
807  DBUG_RETURN(saved_error);
808 }
809 
810 
820 bool st_select_lex_unit::cleanup()
821 {
822  bool error= false;
823  DBUG_ENTER("st_select_lex_unit::cleanup");
824 
825  if (cleaned)
826  {
827  DBUG_RETURN(FALSE);
828  }
829  cleaned= true;
830 
831  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
832  error|= sl->cleanup();
833 
834  cleanup_level();
835 
836  DBUG_RETURN(error);
837 }
838 
839 
846 bool st_select_lex_unit::cleanup_level()
847 {
848  bool error= false;
849 
850  if (fake_select_lex)
851  {
852  error|= fake_select_lex->cleanup();
853  /*
854  There are two cases when we should clean order items:
855  1. UNION with SELECTs which all enclosed into braces
856  in this case global_parameters == fake_select_lex
857  2. UNION where last SELECT is not enclosed into braces
858  in this case global_parameters == 'last select'
859  So we should use global_parameters->order_list for
860  proper order list clean up.
861  Note: global_parameters and fake_select_lex are always
862  initialized for UNION
863  */
864  DBUG_ASSERT(global_parameters);
865  if (global_parameters->order_list.elements)
866  {
867  ORDER *ord;
868  for (ord= global_parameters->order_list.first; ord; ord= ord->next)
869  (*ord->item)->walk (&Item::cleanup_processor, 0, 0);
870  }
871  }
872 
873  if (union_result)
874  {
875  delete union_result;
876  union_result=0; // Safety
877  if (table)
878  free_tmp_table(thd, table);
879  table= 0; // Safety
880  }
881 
882  explain_marker= CTX_NONE;
883 
884  return error;
885 }
886 
887 
888 void st_select_lex_unit::reinit_exec_mechanism()
889 {
890  prepared= optimized= executed= 0;
891 #ifndef DBUG_OFF
892  if (is_union())
893  {
894  List_iterator_fast<Item> it(item_list);
895  Item *field;
896  while ((field= it++))
897  {
898  /*
899  we can't cleanup here, because it broke link to temporary table field,
900  but have to drop fixed flag to allow next fix_field of this field
901  during re-executing
902  */
903  field->fixed= 0;
904  }
905  }
906 #endif
907 }
908 
909 
910 /*
911  change select_result object of unit
912 
913  SYNOPSIS
914  st_select_lex_unit::change_result()
915  result new select_result object
916  old_result old select_result object
917 
918  RETURN
919  FALSE - OK
920  TRUE - error
921 */
922 
923 bool st_select_lex_unit::change_result(select_result_interceptor *new_result,
924  select_result_interceptor *old_result)
925 {
926  bool res= FALSE;
927  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
928  {
929  if (sl->join && sl->join->result == old_result)
930  if (sl->join->change_result(new_result))
931  return TRUE;
932  }
933  if (fake_select_lex && fake_select_lex->join)
934  res= fake_select_lex->join->change_result(new_result);
935  return (res);
936 }
937 
938 /*
939  Get column type information for this unit.
940 
941  SYNOPSIS
942  st_select_lex_unit::get_unit_column_types()
943 
944  DESCRIPTION
945  For a single-select the column types are taken
946  from the list of selected items. For a union this function
947  assumes that st_select_lex_unit::prepare has been called
948  and returns the type holders that were created for unioned
949  column types of all selects.
950 
951  NOTES
952  The implementation of this function should be in sync with
953  st_select_lex_unit::prepare()
954 */
955 
956 List<Item> *st_select_lex_unit::get_unit_column_types()
957 {
958  if (is_union())
959  {
960  DBUG_ASSERT(prepared);
961  /* Types are generated during prepare */
962  return &types;
963  }
964 
965  return &first_select()->item_list;
966 }
967 
968 
980 List<Item> *st_select_lex_unit::get_field_list()
981 {
982  if (is_union())
983  {
984  DBUG_ASSERT(prepared);
985  /* Types are generated during prepare */
986  return &types;
987  }
988 
989  return first_select()->join->fields;
990 }
991 
992 
999 bool st_select_lex::cleanup()
1000 {
1001  bool error= FALSE;
1002  DBUG_ENTER("st_select_lex::cleanup()");
1003 
1004  error= cleanup_level();
1005  for (SELECT_LEX_UNIT *lex_unit= first_inner_unit(); lex_unit;
1006  lex_unit= lex_unit->next_unit())
1007  {
1008  error|= lex_unit->cleanup();
1009  }
1010 
1011  DBUG_RETURN(error);
1012 }
1013 
1014 
1021 bool st_select_lex::cleanup_level()
1022 {
1023  bool error= FALSE;
1024 
1025  if (join)
1026  {
1027  DBUG_ASSERT((st_select_lex*)join->select_lex == this);
1028  error= join->destroy();
1029  delete join;
1030  join= 0;
1031  }
1032 
1033  cur_pos_in_all_fields= ALL_FIELDS_UNDEF_POS;
1034  non_agg_fields.empty();
1035  inner_refs_list.empty();
1036 
1037  return error;
1038 }
1039 
1040 
1041 void st_select_lex::cleanup_all_joins(bool full)
1042 {
1043  SELECT_LEX_UNIT *unit;
1044  SELECT_LEX *sl;
1045 
1046  if (join)
1047  join->cleanup(full);
1048 
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);
1052 }