MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_select.cc
Go to the documentation of this file.
1 /* Copyright (c) 2000, 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 
27 #include "sql_priv.h"
28 #include "sql_select.h"
29 #include "sql_table.h" // primary_key_name
30 #include "sql_derived.h"
31 #include "probes_mysql.h"
32 #include "opt_trace.h"
33 #include "key.h" // key_copy, key_cmp, key_cmp_if_same
34 #include "lock.h" // mysql_unlock_some_tables,
35  // mysql_unlock_read_tables
36 #include "sql_show.h" // append_identifier
37 #include "sql_base.h" // setup_wild, setup_fields, fill_record
38 #include "sql_acl.h" // *_ACL
39 #include "sql_test.h" // misc. debug printing utilities
40 #include "records.h" // init_read_record, end_read_record
41 #include "filesort.h" // filesort_free_buffers
42 #include "sql_union.h" // mysql_union
43 #include "opt_explain.h"
44 #include "sql_join_buffer.h" // JOIN_CACHE
45 #include "sql_optimizer.h" // JOIN
46 #include "sql_tmp_table.h" // tmp tables
47 
48 #include <algorithm>
49 using std::max;
50 using std::min;
51 
52 static store_key *get_store_key(THD *thd,
53  Key_use *keyuse, table_map used_tables,
54  KEY_PART_INFO *key_part, uchar *key_buff,
55  uint maybe_null);
56 bool const_expression_in_where(Item *conds,Item *item, Item **comp_item);
57 uint find_shortest_key(TABLE *table, const key_map *usable_keys);
58 static bool test_if_cheaper_ordering(const JOIN_TAB *tab,
59  ORDER *order, TABLE *table,
60  key_map usable_keys, int key,
61  ha_rows select_limit,
62  int *new_key, int *new_key_direction,
63  ha_rows *new_select_limit,
64  uint *new_used_key_parts= NULL,
65  uint *saved_best_key_parts= NULL);
66 static uint join_buffer_alg(const THD *thd);
67 static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok,
68  Opt_trace_object *trace_obj);
69 
74 bool handle_select(THD *thd, select_result *result,
75  ulong setup_tables_done_option)
76 {
77  bool res;
78  LEX *lex= thd->lex;
79  register SELECT_LEX *select_lex = &lex->select_lex;
80  DBUG_ENTER("handle_select");
81  MYSQL_SELECT_START(thd->query());
82 
83  if (lex->proc_analyse && lex->sql_command != SQLCOM_SELECT)
84  {
85  my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "non-SELECT");
86  DBUG_RETURN(true);
87  }
88 
89  if (select_lex->master_unit()->is_union() ||
90  select_lex->master_unit()->fake_select_lex)
91  res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
92  else
93  {
94  SELECT_LEX_UNIT *unit= &lex->unit;
95  unit->set_limit(unit->global_parameters);
96  /*
97  'options' of mysql_select will be set in JOIN, as far as JOIN for
98  every PS/SP execution new, we will not need reset this flag if
99  setup_tables_done_option changed for next rexecution
100  */
101  res= mysql_select(thd,
102  select_lex->table_list.first,
103  select_lex->with_wild, select_lex->item_list,
104  select_lex->where,
105  &select_lex->order_list,
106  &select_lex->group_list,
107  select_lex->having,
108  select_lex->options | thd->variables.option_bits |
109  setup_tables_done_option,
110  result, unit, select_lex);
111  }
112  DBUG_PRINT("info",("res: %d report_error: %d", res,
113  thd->is_error()));
114  res|= thd->is_error();
115  if (unlikely(res))
116  result->abort_result_set();
117 
118  MYSQL_SELECT_DONE((int) res, (ulong) thd->limit_found_rows);
119  DBUG_RETURN(res);
120 }
121 
122 
123 /*****************************************************************************
124  Check fields, find best join, do the select and output fields.
125  All tables must be opened.
126 *****************************************************************************/
127 
139 
140 {
141  if (outer->result_type() != inner->result_type())
142  return FALSE;
143  switch (outer->result_type()) {
144  case STRING_RESULT:
145  if (outer->is_temporal_with_date() != inner->is_temporal_with_date())
146  return FALSE;
147  if (!(outer->collation.collation == inner->collation.collation
148  /*&& outer->max_length <= inner->max_length */))
149  return FALSE;
150  /*case INT_RESULT:
151  if (!(outer->unsigned_flag ^ inner->unsigned_flag))
152  return FALSE; */
153  default:
154  ; /* suitable for materialization */
155  }
156  return TRUE;
157 }
158 
159 
160 /*
161  Check if the table's rowid is included in the temptable
162 
163  SYNOPSIS
164  sj_table_is_included()
165  join The join
166  join_tab The table to be checked
167 
168  DESCRIPTION
169  SemiJoinDuplicateElimination: check the table's rowid should be included
170  in the temptable. This is so if
171 
172  1. The table is not embedded within some semi-join nest
173  2. The has been pulled out of a semi-join nest, or
174 
175  3. The table is functionally dependent on some previous table
176 
177  [4. This is also true for constant tables that can't be
178  NULL-complemented but this function is not called for such tables]
179 
180  RETURN
181  TRUE - Include table's rowid
182  FALSE - Don't
183 */
184 
185 static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
186 {
187  if (join_tab->emb_sj_nest)
188  return FALSE;
189 
190  /* Check if this table is functionally dependent on the tables that
191  are within the same outer join nest
192  */
193  TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
194  if (join_tab->type == JT_EQ_REF)
195  {
196  table_map depends_on= 0;
197  uint idx;
198 
199  for (uint kp= 0; kp < join_tab->ref.key_parts; kp++)
200  depends_on |= join_tab->ref.items[kp]->used_tables();
201 
202  Table_map_iterator it(depends_on & ~PSEUDO_TABLE_BITS);
203  while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
204  {
205  JOIN_TAB *ref_tab= join->map2table[idx];
206  if (embedding != ref_tab->table->pos_in_table_list->embedding)
207  return TRUE;
208  }
209  /* Ok, functionally dependent */
210  return FALSE;
211  }
212  /* Not functionally dependent => need to include*/
213  return TRUE;
214 }
215 
235 static bool might_do_join_buffering(uint join_buffer_alg,
236  const JOIN_TAB *sj_tab)
237 {
238  /*
239  (1) sj_tab is not a const table
240  */
241  int sj_tabno= sj_tab - sj_tab->join->join_tab;
242  return (sj_tabno >= (int)sj_tab->join->const_tables && // (1)
243  sj_tab->use_quick != QS_DYNAMIC_RANGE &&
244  (((join_buffer_alg & JOIN_CACHE::ALG_BNL) &&
245  sj_tab->type == JT_ALL) ||
246  ((join_buffer_alg &
247  (JOIN_CACHE::ALG_BKA | JOIN_CACHE::ALG_BKA_UNIQUE)) &&
248  (sj_tab->type == JT_REF ||
249  sj_tab->type == JT_EQ_REF ||
250  sj_tab->type == JT_CONST))));
251 }
252 
386 static bool setup_semijoin_dups_elimination(JOIN *join, ulonglong options,
387  uint no_jbuf_after)
388 {
389  uint tableno;
390  THD *thd= join->thd;
391  DBUG_ENTER("setup_semijoin_dups_elimination");
392 
393  if (join->select_lex->sj_nests.is_empty())
394  DBUG_RETURN(FALSE);
395 
396  for (tableno= join->const_tables; tableno < join->primary_tables; )
397  {
398  JOIN_TAB *const tab= join->join_tab + tableno;
399  POSITION *const pos= tab->position;
400  uint keylen, keyno;
401  if (pos->sj_strategy == SJ_OPT_NONE)
402  {
403  tableno++; // nothing to do
404  continue;
405  }
406  JOIN_TAB *last_sj_tab= tab + pos->n_sj_tables - 1;
407  switch (pos->sj_strategy) {
408  case SJ_OPT_MATERIALIZE_LOOKUP:
409  case SJ_OPT_MATERIALIZE_SCAN:
410  DBUG_ASSERT(false); // Should not occur among "primary" tables
411  // Do nothing
412  tableno+= pos->n_sj_tables;
413  break;
414  case SJ_OPT_LOOSE_SCAN:
415  {
416  DBUG_ASSERT(tab->emb_sj_nest != NULL); // First table must be inner
417  /* We jump from the last table to the first one */
418  tab->match_tab= last_sj_tab;
419 
420  /* For LooseScan, duplicate elimination is based on rows being sorted
421  on key. We need to make sure that range select keeps the sorted index
422  order. (When using MRR it may not.)
423 
424  Note: need_sorted_output() implementations for range select classes
425  that do not support sorted output, will trigger an assert. This
426  should not happen since LooseScan strategy is only picked if sorted
427  output is supported.
428  */
429  if (tab->select && tab->select->quick)
430  {
431  if (tab->select->quick->index == pos->loosescan_key)
432  tab->select->quick->need_sorted_output();
433  else
434  tab->select->set_quick(NULL);
435  }
436  /* Calculate key length */
437  keylen= 0;
438  keyno= pos->loosescan_key;
439  for (uint kp=0; kp < pos->loosescan_parts; kp++)
440  keylen += tab->table->key_info[keyno].key_part[kp].store_length;
441 
442  tab->loosescan_key_len= keylen;
443  if (pos->n_sj_tables > 1)
444  {
445  last_sj_tab->firstmatch_return= tab;
446  last_sj_tab->match_tab= last_sj_tab;
447  }
448  tableno+= pos->n_sj_tables;
449  break;
450  }
451  case SJ_OPT_DUPS_WEEDOUT:
452  {
453  DBUG_ASSERT(tab->emb_sj_nest != NULL); // First table must be inner
454  /*
455  Consider a semijoin of one outer and one inner table, both
456  with two rows. The inner table is assumed to be confluent
457  (See sj_opt_materialize_lookup)
458 
459  If normal nested loop execution is used, we do not need to
460  include semi-join outer table rowids in the duplicate
461  weedout temp table since NL guarantees that outer table rows
462  are encountered only consecutively and because all rows in
463  the temp table are deleted for every new outer table
464  combination (example is with a confluent inner table):
465 
466  ot1.row1|it1.row1
467  '-> temp table's have_confluent_row == FALSE
468  |-> output ot1.row1
469  '-> set have_confluent_row= TRUE
470  ot1.row1|it1.row2
471  |-> temp table's have_confluent_row == TRUE
472  | '-> do not output ot1.row1
473  '-> no more join matches - set have_confluent_row= FALSE
474  ot1.row2|it1.row1
475  '-> temp table's have_confluent_row == FALSE
476  |-> output ot1.row2
477  '-> set have_confluent_row= TRUE
478  ...
479 
480  Note: not having outer table rowids in the temp table and
481  then emptying the temp table when a new outer table row
482  combinition is encountered is an optimization. Including
483  outer table rowids in the temp table is not harmful but
484  wastes memory.
485 
486  Now consider the join buffering algorithms (BNL/BKA). These
487  algorithms join each inner row with outer rows in "reverse"
488  order compared to NL. Effectively, this means that outer
489  table rows may be encountered multiple times in a
490  non-consecutive manner:
491 
492  NL: BNL/BKA:
493  ot1.row1|it1.row1 ot1.row1|it1.row1
494  ot1.row1|it1.row2 ot1.row2|it1.row1
495  ot1.row2|it1.row1 ot1.row1|it1.row2
496  ot1.row2|it1.row2 ot1.row2|it1.row2
497 
498  It is clear from the above that there is no place we can
499  empty the temp table like we do in NL to avoid storing outer
500  table rowids.
501 
502  Below we check if join buffering might be used. If so, set
503  first_table to the first non-constant table so that outer
504  table rowids are included in the temp table. Do not destroy
505  other duplicate elimination methods.
506  */
507  uint first_table= tableno;
508  for (uint sj_tableno= tableno;
509  sj_tableno < tableno + pos->n_sj_tables;
510  sj_tableno++)
511  {
512  /*
513  The final decision on whether or not join buffering will
514  be used is taken in setup_join_buffering(), which is
515  called from make_join_readinfo()'s main loop.
516  setup_join_buffering() needs to know if duplicate weedout is used,
517  so moving setup_semijoin_dups_elimination() from before the main
518  loop to after it is not possible. I.e.,
519  join->join_tab[sj_tableno]->position->use_join_buffer is not
520  trustworthy at this point.
521  */
533  if (sj_tableno <= no_jbuf_after &&
534  might_do_join_buffering(join_buffer_alg(thd),
535  join->join_tab + sj_tableno))
536 
537  {
538  /* Join buffering will probably be used */
539  first_table= join->const_tables;
540  break;
541  }
542  }
543 
544  JOIN_TAB *const first_sj_tab= join->join_tab + first_table;
545  if (last_sj_tab->first_inner != NULL &&
546  first_sj_tab->first_inner != last_sj_tab->first_inner)
547  {
548  /*
549  The first duplicate weedout table is an outer table of an outer join
550  and the last duplicate weedout table is one of the inner tables of
551  the outer join.
552  In this case, we must assure that all the inner tables of the
553  outer join are part of the duplicate weedout operation.
554  This is to assure that NULL-extension for inner tables of an
555  outer join is performed before duplicate elimination is performed,
556  otherwise we will have extra NULL-extended rows being output, which
557  should have been eliminated as duplicates.
558  */
559  JOIN_TAB *tab= last_sj_tab->first_inner;
560  /*
561  First, locate the table that is the first inner table of the
562  outer join operation that first_sj_tab is outer for.
563  */
564  while (tab->first_upper != NULL &&
565  tab->first_upper != first_sj_tab->first_inner)
566  tab= tab->first_upper;
567  // Then, extend the range with all inner tables of the join nest:
568  if (tab->first_inner->last_inner > last_sj_tab)
569  last_sj_tab= tab->first_inner->last_inner;
570  }
571 
573  SJ_TMP_TABLE::TAB *last_tab= sjtabs;
574  uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
575  uint jt_null_bits= 0; // # null bits in tuple bytes
576  /*
577  Walk through the range and remember
578  - tables that need their rowids to be put into temptable
579  - the last outer table
580  */
581  for (JOIN_TAB *tab_in_range= join->join_tab + first_table;
582  tab_in_range <= last_sj_tab;
583  tab_in_range++)
584  {
585  if (sj_table_is_included(join, tab_in_range))
586  {
587  last_tab->join_tab= tab_in_range;
588  last_tab->rowid_offset= jt_rowid_offset;
589  jt_rowid_offset += tab_in_range->table->file->ref_length;
590  if (tab_in_range->table->maybe_null)
591  {
592  last_tab->null_byte= jt_null_bits / 8;
593  last_tab->null_bit= jt_null_bits++;
594  }
595  last_tab++;
596  tab_in_range->table->prepare_for_position();
597  tab_in_range->keep_current_rowid= TRUE;
598  }
599  }
600 
601  SJ_TMP_TABLE *sjtbl;
602  if (jt_rowid_offset) /* Temptable has at least one rowid */
603  {
604  uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
605  if (!(sjtbl= new (thd->mem_root) SJ_TMP_TABLE) ||
606  !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
607  DBUG_RETURN(TRUE); /* purecov: inspected */
608  memcpy(sjtbl->tabs, sjtabs, tabs_size);
609  sjtbl->is_confluent= FALSE;
610  sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
611  sjtbl->rowid_len= jt_rowid_offset;
612  sjtbl->null_bits= jt_null_bits;
613  sjtbl->null_bytes= (jt_null_bits + 7)/8;
614  sjtbl->tmp_table=
615  create_duplicate_weedout_tmp_table(thd,
616  sjtbl->rowid_len +
617  sjtbl->null_bytes,
618  sjtbl);
619  join->sj_tmp_tables.push_back(sjtbl->tmp_table);
620  }
621  else
622  {
623  /*
624  This is confluent case where the entire subquery predicate does
625  not depend on anything at all, ie this is
626  WHERE const IN (uncorrelated select)
627  */
628  if (!(sjtbl= new (thd->mem_root) SJ_TMP_TABLE))
629  DBUG_RETURN(TRUE); /* purecov: inspected */
630  sjtbl->tmp_table= NULL;
631  sjtbl->is_confluent= TRUE;
632  sjtbl->have_confluent_row= FALSE;
633  }
634  join->join_tab[first_table].flush_weedout_table= sjtbl;
635  last_sj_tab->check_weed_out_table= sjtbl;
636 
637  tableno+= pos->n_sj_tables;
638  break;
639  }
640  case SJ_OPT_FIRST_MATCH:
641  {
642  /*
643  Setup a "jump" from the last table in the range of inner tables
644  to the last outer table before the inner tables.
645  If there are outer tables inbetween the inner tables, we have to
646  setup a "split jump": Jump from the last inner table to the last
647  outer table within the range, then from the last inner table
648  before the outer table(s), jump to the last outer table before
649  this range of inner tables, etc.
650  */
651  JOIN_TAB *jump_to= tab - 1;
652  DBUG_ASSERT(tab->emb_sj_nest != NULL); // First table must be inner
653  for (JOIN_TAB *tab_in_range= tab;
654  tab_in_range <= last_sj_tab;
655  tab_in_range++)
656  {
657  if (!tab_in_range->emb_sj_nest)
658  {
659  /*
660  Let last non-correlated table be jump target for
661  subsequent inner tables.
662  */
663  jump_to= tab_in_range;
664  }
665  else
666  {
667  /*
668  Assign jump target for last table in a consecutive range of
669  inner tables.
670  */
671  if (tab_in_range == last_sj_tab || !(tab_in_range+1)->emb_sj_nest)
672  {
673  tab_in_range->firstmatch_return= jump_to;
674  tab_in_range->match_tab= last_sj_tab;
675  }
676  }
677  }
678  tableno+= pos->n_sj_tables;
679  break;
680  }
681  }
682  }
683  DBUG_RETURN(FALSE);
684 }
685 
686 
687 /*
688  Destroy all temporary tables created by NL-semijoin runtime
689 */
690 
691 static void destroy_sj_tmp_tables(JOIN *join)
692 {
693  List_iterator<TABLE> it(join->sj_tmp_tables);
694  TABLE *table;
695  while ((table= it++))
696  {
697  /*
698  SJ-Materialization tables are initialized for either sequential reading
699  or index lookup, DuplicateWeedout tables are not initialized for read
700  (we only write to them), so need to call ha_index_or_rnd_end.
701  */
702  table->file->ha_index_or_rnd_end();
703  free_tmp_table(join->thd, table);
704  }
705  join->sj_tmp_tables.empty();
706 }
707 
708 
718 static int clear_sj_tmp_tables(JOIN *join)
719 {
720  int res;
721  List_iterator<TABLE> it(join->sj_tmp_tables);
722  TABLE *table;
723  while ((table= it++))
724  {
725  if ((res= table->file->ha_delete_all_rows()))
726  return res; /* purecov: inspected */
727  }
728  Semijoin_mat_exec *sjm;
729  List_iterator<Semijoin_mat_exec> it2(join->sjm_exec_list);
730  while ((sjm= it2++))
731  {
732  JOIN_TAB *const tab= join->join_tab + sjm->mat_table_index;
733  DBUG_ASSERT(tab->materialize_table);
734  tab->materialized= false;
735  // The materialized table must be re-read on next evaluation:
736  tab->table->status= STATUS_GARBAGE | STATUS_NOT_FOUND;
737  }
738  return 0;
739 }
740 
741 
748 {
749  DBUG_ENTER("JOIN::reset");
750 
751  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
752  select_lex->offset_limit->val_uint() :
753  ULL(0));
754 
755  first_record= false;
756  group_sent= false;
757 
758  if (tmp_tables)
759  {
760  for (uint tmp= primary_tables; tmp < primary_tables + tmp_tables; tmp++)
761  {
762  TABLE *tmp_table= join_tab[tmp].table;
763  if (!tmp_table->is_created())
764  continue;
765  tmp_table->file->extra(HA_EXTRA_RESET_STATE);
766  tmp_table->file->ha_delete_all_rows();
767  free_io_cache(tmp_table);
768  filesort_free_buffers(tmp_table,0);
769  }
770  }
771  clear_sj_tmp_tables(this);
772  if (current_ref_ptrs != items0)
773  {
774  set_items_ref_array(items0);
775  set_group_rpa= false;
776  }
777 
778  /* need to reset ref access state (see join_read_key) */
779  if (join_tab)
780  for (uint i= 0; i < tables; i++)
781  join_tab[i].ref.key_err= TRUE;
782 
783  /* Reset of sum functions */
784  if (sum_funcs)
785  {
786  Item_sum *func, **func_ptr= sum_funcs;
787  while ((func= *(func_ptr++)))
788  func->clear();
789  }
790 
791  if (!(select_options & SELECT_DESCRIBE))
792  init_ftfuncs(thd, select_lex, test(order));
793 
794  DBUG_VOID_RETURN;
795 }
796 
797 
809 bool JOIN::prepare_result(List<Item> **columns_list)
810 {
811  DBUG_ENTER("JOIN::prepare_result");
812 
813  error= 0;
814  /* Create result tables for materialized views. */
815  if (!zero_result_cause &&
816  select_lex->handle_derived(thd->lex, &mysql_derived_create))
817  goto err;
818 
819  if (result->prepare2())
820  goto err;
821 
822  if ((select_lex->options & OPTION_SCHEMA_TABLE) &&
823  get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
824  goto err;
825 
826  DBUG_RETURN(FALSE);
827 
828 err:
829  error= 1;
830  DBUG_RETURN(TRUE);
831 }
832 
833 
838 bool
840 {
841  Opt_trace_context * const trace= &thd->opt_trace;
842  Opt_trace_object trace_wrapper(trace);
843  Opt_trace_object trace_exec(trace, "join_explain");
844  trace_exec.add_select_number(select_lex->select_number);
845  Opt_trace_array trace_steps(trace, "steps");
846  List<Item> *columns_list= &fields_list;
847  bool ret;
848  DBUG_ENTER("JOIN::explain");
849 
850  THD_STAGE_INFO(thd, stage_explaining);
851 
852  if (prepare_result(&columns_list))
853  DBUG_RETURN(true);
854 
855  if (!tables_list && (tables || !select_lex->with_sum_func))
856  { // Only test of functions
857  ret= explain_no_table(thd, this, zero_result_cause ? zero_result_cause
858  : "No tables used");
859  /* Single select (without union) always returns 0 or 1 row */
860  thd->limit_found_rows= send_records;
861  thd->set_examined_row_count(0);
862  DBUG_RETURN(ret);
863  }
864  /*
865  Don't reset the found rows count if there're no tables as
866  FOUND_ROWS() may be called. Never reset the examined row count here.
867  It must be accumulated from all join iterations of all join parts.
868  */
869  if (tables)
870  thd->limit_found_rows= 0;
871 
872  if (zero_result_cause)
873  {
874  ret= explain_no_table(thd, this, zero_result_cause);
875  DBUG_RETURN(ret);
876  }
877 
878  if (tables)
879  ret= explain_query_specification(thd, this);
880  else
881  ret= explain_no_table(thd, this, "No tables used");
882 
883  DBUG_RETURN(ret);
884 }
885 
886 
894 {
895  DBUG_ENTER("JOIN::destroy");
896  select_lex->join= 0;
897 
898  cond_equal= 0;
899 
900  cleanup(1);
901 
902  if (join_tab) // We should not have tables > 0 and join_tab != NULL
903  for (uint i= 0; i < tables; i++)
904  {
905  JOIN_TAB *const tab= join_tab + i;
906 
907  DBUG_ASSERT(!tab->table || !tab->table->sort.record_pointers);
908  if (tab->op)
909  {
910  if (tab->op->type() == QEP_operation::OT_TMP_TABLE)
911  {
912  free_tmp_table(thd, tab->table);
913  delete tab->tmp_table_param;
914  tab->tmp_table_param= NULL;
915  }
916  tab->op->free();
917  tab->op= NULL;
918  }
919 
920  tab->table= NULL;
921  }
922  /* Cleanup items referencing temporary table columns */
923  cleanup_item_list(tmp_all_fields1);
924  cleanup_item_list(tmp_all_fields3);
925  destroy_sj_tmp_tables(this);
926 
927  List_iterator<Semijoin_mat_exec> sjm_list_it(sjm_exec_list);
928  Semijoin_mat_exec *sjm;
929  while ((sjm= sjm_list_it++))
930  delete sjm;
931  sjm_exec_list.empty();
932 
933  keyuse.clear();
934  DBUG_RETURN(test(error));
935 }
936 
937 
938 void JOIN::cleanup_item_list(List<Item> &items) const
939 {
940  if (!items.is_empty())
941  {
942  List_iterator_fast<Item> it(items);
943  Item *item;
944  while ((item= it++))
945  item->cleanup();
946  }
947 }
948 
949 
996 static bool
997 mysql_prepare_select(THD *thd,
998  TABLE_LIST *tables, uint wild_num, List<Item> &fields,
999  Item *conds, uint og_num, ORDER *order, ORDER *group,
1000  Item *having, ulonglong select_options,
1001  select_result *result, SELECT_LEX_UNIT *unit,
1002  SELECT_LEX *select_lex, bool *free_join)
1003 {
1004  bool err= false;
1005  JOIN *join;
1006 
1007  DBUG_ENTER("mysql_prepare_select");
1008  select_lex->context.resolve_in_select_list= TRUE;
1009  if (select_lex->join != 0)
1010  {
1011  join= select_lex->join;
1012  /*
1013  is it single SELECT in derived table, called in derived table
1014  creation
1015  */
1016  if (select_lex->linkage != DERIVED_TABLE_TYPE ||
1017  (select_options & SELECT_DESCRIBE))
1018  {
1019  if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
1020  {
1021  //here is EXPLAIN of subselect or derived table
1022  if (join->change_result(result))
1023  {
1024  DBUG_RETURN(TRUE);
1025  }
1026  /*
1027  Original join tabs might be overwritten at first
1028  subselect execution. So we need to restore them.
1029  */
1030  Item_subselect *subselect= select_lex->master_unit()->item;
1031  if (subselect && subselect->is_uncacheable())
1032  join->reset();
1033  }
1034  else
1035  {
1036  err= join->prepare(tables, wild_num,
1037  conds, og_num, order, group, having,
1038  select_lex, unit);
1039  if (err)
1040  DBUG_RETURN(true);
1041  }
1042  }
1043  *free_join= false;
1044  join->select_options= select_options;
1045  }
1046  else
1047  {
1048  if (!(join= new JOIN(thd, fields, select_options, result)))
1049  DBUG_RETURN(TRUE); /* purecov: inspected */
1050  THD_STAGE_INFO(thd, stage_init);
1051  thd->lex->used_tables=0; // Updated by setup_fields
1052  err= join->prepare(tables, wild_num,
1053  conds, og_num, order, group, having,
1054  select_lex, unit);
1055  if (err)
1056  DBUG_RETURN(true);
1057  }
1058 
1059  DBUG_RETURN(err);
1060 }
1061 
1062 
1077 static bool
1078 mysql_execute_select(THD *thd, SELECT_LEX *select_lex, bool free_join)
1079 {
1080  bool err;
1081  JOIN* join= select_lex->join;
1082 
1083  DBUG_ENTER("mysql_execute_select");
1084  DBUG_ASSERT(join);
1085 
1086  if ((err= join->optimize()))
1087  {
1088  goto err; // 1
1089  }
1090 
1091  if (thd->is_error())
1092  goto err;
1093 
1094  if (join->select_options & SELECT_DESCRIBE)
1095  {
1096  join->explain();
1097  free_join= false;
1098  }
1099  else
1100  join->exec();
1101 
1102 err:
1103  if (free_join)
1104  {
1105  THD_STAGE_INFO(thd, stage_end);
1106  err|= select_lex->cleanup();
1107  DBUG_RETURN(err || thd->is_error());
1108  }
1109  DBUG_RETURN(join->error);
1110 }
1111 
1112 
1152 bool
1153 mysql_select(THD *thd,
1154  TABLE_LIST *tables, uint wild_num, List<Item> &fields,
1155  Item *conds, SQL_I_List<ORDER> *order, SQL_I_List<ORDER> *group,
1156  Item *having, ulonglong select_options,
1157  select_result *result, SELECT_LEX_UNIT *unit,
1158  SELECT_LEX *select_lex)
1159 {
1160  bool free_join= true;
1161  uint og_num= 0;
1162  ORDER *first_order= NULL;
1163  ORDER *first_group= NULL;
1164  DBUG_ENTER("mysql_select");
1165 
1166  if (order)
1167  {
1168  og_num= order->elements;
1169  first_order= order->first;
1170  }
1171  if (group)
1172  {
1173  og_num+= group->elements;
1174  first_group= group->first;
1175  }
1176 
1177  if (mysql_prepare_select(thd, tables, wild_num, fields,
1178  conds, og_num, first_order, first_group, having,
1179  select_options, result, unit,
1180  select_lex, &free_join))
1181  {
1182  if (free_join)
1183  {
1184  THD_STAGE_INFO(thd, stage_end);
1185  (void) select_lex->cleanup();
1186  }
1187  DBUG_RETURN(true);
1188  }
1189 
1190  if (! thd->lex->is_query_tables_locked())
1191  {
1192  /*
1193  If tables are not locked at this point, it means that we have delayed
1194  this step until after prepare stage (i.e. this moment). This allows to
1195  do better partition pruning and avoid locking unused partitions.
1196  As a consequence, in such a case, prepare stage can rely only on
1197  metadata about tables used and not data from them.
1198  We need to lock tables now in order to proceed with the remaning
1199  stages of query optimization and execution.
1200  */
1201  if (lock_tables(thd, thd->lex->query_tables, thd->lex->table_count, 0))
1202  {
1203  if (free_join)
1204  {
1205  THD_STAGE_INFO(thd, stage_end);
1206  (void) select_lex->cleanup();
1207  }
1208  DBUG_RETURN(true);
1209  }
1210 
1211  /*
1212  Only register query in cache if it tables were locked above.
1213 
1214  Tables must be locked before storing the query in the query cache.
1215  Transactional engines must been signalled that the statement started,
1216  which external_lock signals.
1217  */
1218  query_cache_store_query(thd, thd->lex->query_tables);
1219  }
1220 
1221  DBUG_RETURN(mysql_execute_select(thd, select_lex, free_join));
1222 }
1223 
1224 /*****************************************************************************
1225  Go through all combinations of not marked tables and find the one
1226  which uses least records
1227 *****************************************************************************/
1228 
1237 static uint join_buffer_alg(const THD *thd)
1238 {
1239  uint alg= JOIN_CACHE::ALG_NONE;
1240 
1241  if (thd->optimizer_switch_flag(OPTIMIZER_SWITCH_BNL))
1242  alg|= JOIN_CACHE::ALG_BNL;
1243 
1244  if (thd->optimizer_switch_flag(OPTIMIZER_SWITCH_BKA))
1245  {
1246  bool use_bka_unique= false;
1247  DBUG_EXECUTE_IF("test_bka_unique", use_bka_unique= true;);
1248 
1249  if (use_bka_unique)
1250  alg|= JOIN_CACHE::ALG_BKA_UNIQUE;
1251  else
1252  alg|= JOIN_CACHE::ALG_BKA;
1253  }
1254 
1255  return alg;
1256 }
1257 
1258 
1263 void calc_used_field_length(THD *thd, JOIN_TAB *join_tab)
1264 {
1265  uint null_fields,blobs,fields,rec_length;
1266  Field **f_ptr,*field;
1267  uint uneven_bit_fields;
1268  MY_BITMAP *read_set= join_tab->table->read_set;
1269 
1270  uneven_bit_fields= null_fields= blobs= fields= rec_length=0;
1271  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
1272  {
1273  if (bitmap_is_set(read_set, field->field_index))
1274  {
1275  uint flags=field->flags;
1276  fields++;
1277  rec_length+=field->pack_length();
1278  if (flags & BLOB_FLAG)
1279  blobs++;
1280  if (!(flags & NOT_NULL_FLAG))
1281  null_fields++;
1282  if (field->type() == MYSQL_TYPE_BIT &&
1283  ((Field_bit*)field)->bit_len)
1284  uneven_bit_fields++;
1285  }
1286  }
1287  if (null_fields || uneven_bit_fields)
1288  rec_length+=(join_tab->table->s->null_fields+7)/8;
1289  if (join_tab->table->maybe_null)
1290  rec_length+=sizeof(my_bool);
1291  if (blobs)
1292  {
1293  uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
1294  (join_tab->table->s->reclength- rec_length));
1295  rec_length+= max<uint>(4U, blob_length);
1296  }
1301  join_tab->used_fields=fields;
1302  join_tab->used_fieldlength=rec_length;
1303  join_tab->used_blobs=blobs;
1304  join_tab->used_null_fields= null_fields;
1305  join_tab->used_uneven_bit_fields= uneven_bit_fields;
1306 }
1307 
1308 
1329 {
1330  DBUG_ENTER("JOIN::get_best_combination");
1331 
1332  // At this point "tables" and "primary"tables" represent the same:
1333  DBUG_ASSERT(tables == primary_tables);
1334 
1335  /*
1336  Allocate additional space for tmp tables.
1337  Number of plan nodes:
1338  # of regular input tables (including semi-joined ones) +
1339  # of semi-join nests for materialization +
1340  1? + // For GROUP BY
1341  1? + // For DISTINCT
1342  1? + // For aggregation functions aggregated in outer query
1343  // when used with distinct
1344  1? + // For ORDER BY
1345  1? // buffer result
1346  Up to 2 tmp tables are actually used, but it's hard to tell exact number
1347  at this stage.
1348  */
1349  uint tmp_tables= (group_list ? 1 : 0) +
1350  (select_distinct ?
1351  (tmp_table_param.outer_sum_func_count ? 2 : 1) : 0) +
1352  (order ? 1 : 0) +
1353  (select_options & (SELECT_BIG_RESULT | OPTION_BUFFER_RESULT) ? 1 : 0) ;
1354  if (tmp_tables > 2)
1355  tmp_tables= 2;
1356 
1357  /*
1358  Rearrange queries with materialized semi-join nests so that the semi-join
1359  nest is replaced with a reference to a materialized temporary table and all
1360  materialized subquery tables are placed after the intermediate tables.
1361  After the following loop, "inner_target" is the position of the first
1362  subquery table (if any). "outer_target" is the position of first outer
1363  table, and will later be used to track the position of any materialized
1364  temporary tables.
1365  */
1366  const bool has_semijoin= !select_lex->sj_nests.is_empty();
1367  uint outer_target= 0;
1368  uint inner_target= primary_tables + tmp_tables;
1369  uint sjm_nests= 0;
1370 
1371  if (has_semijoin)
1372  {
1373  for (uint tableno= 0; tableno < primary_tables; )
1374  {
1375  if (sj_is_materialize_strategy(best_positions[tableno].sj_strategy))
1376  {
1377  sjm_nests++;
1378  inner_target-= (best_positions[tableno].n_sj_tables - 1);
1379  tableno+= best_positions[tableno].n_sj_tables;
1380  }
1381  else
1382  tableno++;
1383  }
1384  }
1385  if (!(join_tab= new(thd->mem_root) JOIN_TAB[tables + sjm_nests + tmp_tables]))
1386  DBUG_RETURN(true);
1387 
1388  int sjm_index= tables; // Number assigned to materialized temporary table
1389  int remaining_sjm_inner= 0;
1390  for (uint tableno= 0; tableno < tables; tableno++)
1391  {
1392  if (has_semijoin &&
1393  sj_is_materialize_strategy(best_positions[tableno].sj_strategy))
1394  {
1395  DBUG_ASSERT(outer_target < inner_target);
1396 
1397  POSITION *const pos_table= best_positions + tableno;
1398  TABLE_LIST *const sj_nest= pos_table->table->emb_sj_nest;
1399 
1400  // Handle this many inner tables of materialized semi-join
1401  remaining_sjm_inner= pos_table->n_sj_tables;
1402 
1403  Semijoin_mat_exec *const sjm_exec=
1404  new (thd->mem_root)
1405  Semijoin_mat_exec(sj_nest,
1406  (pos_table->sj_strategy == SJ_OPT_MATERIALIZE_SCAN),
1407  remaining_sjm_inner, outer_target, inner_target);
1408  if (!sjm_exec)
1409  DBUG_RETURN(true);
1410 
1411  (join_tab + outer_target)->sj_mat_exec= sjm_exec;
1412 
1413  if (setup_materialized_table(join_tab + outer_target, sjm_index,
1414  pos_table, best_positions + sjm_index))
1415  DBUG_RETURN(true);
1416 
1417  map2table[sjm_exec->table->tablenr]= join_tab + outer_target;
1418 
1419  outer_target++;
1420  sjm_index++;
1421  }
1422  /*
1423  Locate join_tab target for the table we are considering.
1424  (remaining_sjm_inner becomes negative for non-SJM tables, this can be
1425  safely ignored).
1426  */
1427  const uint target=
1428  (remaining_sjm_inner--) > 0 ? inner_target++ : outer_target++;
1429  JOIN_TAB *const tab= join_tab + target;
1430 
1431  // Copy data from existing join_tab
1432  *tab= *best_positions[tableno].table;
1433 
1434  tab->position= best_positions + tableno;
1435 
1436  TABLE *const table= tab->table;
1437  table->reginfo.join_tab= tab;
1438  if (!*tab->on_expr_ref)
1439  table->reginfo.not_exists_optimize= false; // Only with LEFT JOIN
1440  map2table[table->tablenr]= tab;
1441  }
1442 
1443  // Count the materialized semi-join tables as regular input tables
1444  tables+= sjm_nests + tmp_tables;
1445  // Set the number of non-materialized tables:
1446  primary_tables= outer_target;
1447 
1448  if (has_semijoin)
1449  {
1450  set_semijoin_info();
1451 
1452  // Update equalities and keyuses after having added SJ materialization
1454  DBUG_RETURN(true);
1455  }
1456  // sjm is no longer needed, trash it. To reuse it, reset its members!
1457  List_iterator<TABLE_LIST> sj_list_it(select_lex->sj_nests);
1458  TABLE_LIST *sj_nest;
1459  while ((sj_nest= sj_list_it++))
1460  TRASH(&sj_nest->nested_join->sjm, sizeof(sj_nest->nested_join->sjm));
1461 
1462  DBUG_RETURN(false);
1463 }
1464 
1465 
1482 bool JOIN::set_access_methods()
1483 {
1484  DBUG_ENTER("JOIN::set_access_methods");
1485 
1486  full_join= false;
1487 
1488  for (uint tableno= const_tables; tableno < tables; tableno++)
1489  {
1490  JOIN_TAB *const tab= join_tab + tableno;
1491 
1492  if (!tab->position)
1493  continue;
1494 
1495  DBUG_PRINT("info",("type: %d", tab->type));
1496 
1497  // Set preliminary join cache setting based on decision from greedy search
1498  tab->use_join_cache= tab->position->use_join_buffer ?
1499  JOIN_CACHE::ALG_BNL : JOIN_CACHE::ALG_NONE;
1500 
1501  if (tab->type == JT_CONST || tab->type == JT_SYSTEM)
1502  continue; // Handled in make_join_statistics()
1503 
1504  Key_use *const keyuse= tab->position->key;
1505  if (tab->position->sj_strategy == SJ_OPT_LOOSE_SCAN)
1506  {
1507  DBUG_ASSERT(tab->keys.is_set(tab->position->loosescan_key));
1508  tab->type= JT_ALL; // @todo is this consistent for a LooseScan table ?
1509  tab->index= tab->position->loosescan_key;
1510  }
1511  else if (!keyuse)
1512  {
1513  tab->type= JT_ALL;
1514  if (tableno > const_tables)
1515  full_join= true;
1516  }
1517  else
1518  {
1519  if (create_ref_for_key(this, tab, keyuse, tab->prefix_tables()))
1520  DBUG_RETURN(true);
1521  }
1522  }
1523 
1524  DBUG_RETURN(false);
1525 }
1526 
1527 
1532 void JOIN::set_semijoin_info()
1533 {
1534  if (select_lex->sj_nests.is_empty())
1535  return;
1536 
1537  for (uint tableno= const_tables; tableno < tables; )
1538  {
1539  JOIN_TAB *const tab= join_tab + tableno;
1540  const POSITION *const pos= tab->position;
1541 
1542  if (!pos)
1543  {
1544  tableno++;
1545  continue;
1546  }
1547  switch (pos->sj_strategy)
1548  {
1549  case SJ_OPT_NONE:
1550  tableno++;
1551  break;
1552  case SJ_OPT_MATERIALIZE_LOOKUP:
1553  case SJ_OPT_MATERIALIZE_SCAN:
1554  case SJ_OPT_LOOSE_SCAN:
1555  case SJ_OPT_DUPS_WEEDOUT:
1556  case SJ_OPT_FIRST_MATCH:
1557  /*
1558  Remember the first and last semijoin inner tables; this serves to tell
1559  a JOIN_TAB's semijoin strategy (like in setup_join_buffering()).
1560  */
1561  JOIN_TAB *last_sj_tab= tab + pos->n_sj_tables - 1;
1562  JOIN_TAB *last_sj_inner=
1563  (pos->sj_strategy == SJ_OPT_DUPS_WEEDOUT) ?
1564  /* Range may end with non-inner table so cannot set last_sj_inner_tab */
1565  NULL : last_sj_tab;
1566  for (JOIN_TAB *tab_in_range= tab;
1567  tab_in_range <= last_sj_tab;
1568  tab_in_range++)
1569  {
1570  tab_in_range->first_sj_inner_tab= tab;
1571  tab_in_range->last_sj_inner_tab= last_sj_inner;
1572  }
1573  tableno+= pos->n_sj_tables;
1574  break;
1575  }
1576  }
1577 }
1578 
1579 
1602 bool create_ref_for_key(JOIN *join, JOIN_TAB *j, Key_use *org_keyuse,
1603  table_map used_tables)
1604 {
1605  DBUG_ENTER("create_ref_for_key");
1606 
1607  Key_use *keyuse= org_keyuse;
1608  const uint key= keyuse->key;
1609  const bool ftkey= (keyuse->keypart == FT_KEYPART);
1610  THD *const thd= join->thd;
1611  uint keyparts, length;
1612  TABLE *const table= j->table;
1613  KEY *const keyinfo= table->key_info+key;
1614  Key_use *chosen_keyuses[MAX_REF_PARTS];
1615 
1616  DBUG_ASSERT(j->keys.is_set(org_keyuse->key));
1617 
1618  if (ftkey)
1619  {
1620  Item_func_match *ifm=(Item_func_match *)keyuse->val;
1621 
1622  length=0;
1623  keyparts=1;
1624  ifm->join_key=1;
1625  }
1626  else
1627  {
1628  keyparts=length=0;
1629  uint found_part_ref_or_null= 0;
1630  // Calculate length for the used key. Remember chosen Key_use-s.
1631  do
1632  {
1633  /*
1634  This Key_use is chosen if:
1635  - it involves a key part at the right place (if index is (a,b) we
1636  can have a search criterion on 'b' only if we also have a criterion
1637  on 'a'),
1638  - it references only tables earlier in the plan.
1639  Moreover, the execution layer is limited to maximum one ref_or_null
1640  keypart, as TABLE_REF::null_ref_key is only one byte.
1641  */
1642  if (!(~used_tables & keyuse->used_tables) &&
1643  keyparts == keyuse->keypart &&
1644  !(found_part_ref_or_null & keyuse->optimize))
1645  {
1646  DBUG_ASSERT(keyparts <= MAX_REF_PARTS);
1647  chosen_keyuses[keyparts]= keyuse;
1648  keyparts++;
1649  length+= keyinfo->key_part[keyuse->keypart].store_length;
1650  found_part_ref_or_null|= keyuse->optimize;
1651  }
1652  keyuse++;
1653  } while (keyuse->table == table && keyuse->key == key);
1654  DBUG_ASSERT(length > 0 && keyparts != 0);
1655  } /* not ftkey */
1656 
1657  DBUG_ASSERT(keyparts > 0);
1658 
1659  /* set up fieldref */
1660  j->ref.key_parts=keyparts;
1661  j->ref.key_length=length;
1662  j->ref.key=(int) key;
1663  if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
1664  !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
1665  (keyparts)))) ||
1666  !(j->ref.items= (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
1667  !(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
1668  {
1669  DBUG_RETURN(TRUE);
1670  }
1671  j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length);
1672  j->ref.key_err=1;
1673  j->ref.has_record= FALSE;
1674  j->ref.null_rejecting= 0;
1675  j->ref.use_count= 0;
1676  j->ref.disable_cache= FALSE;
1677  keyuse=org_keyuse;
1678 
1679  uchar *key_buff= j->ref.key_buff;
1680  uchar *null_ref_key= NULL;
1681  bool keyuse_uses_no_tables= true;
1682  if (ftkey)
1683  {
1684  j->ref.items[0]=((Item_func*)(keyuse->val))->key_item();
1685  /* Predicates pushed down into subquery can't be used FT access */
1686  j->ref.cond_guards[0]= NULL;
1687  if (keyuse->used_tables)
1688  DBUG_RETURN(TRUE); // not supported yet. SerG
1689 
1690  j->type=JT_FT;
1691  memset(j->ref.key_copy, 0, sizeof(j->ref.key_copy[0]) * keyparts);
1692  }
1693  else
1694  {
1695  // Set up TABLE_REF based on chosen Key_use-s.
1696  for (uint part_no= 0 ; part_no < keyparts ; part_no++)
1697  {
1698  keyuse= chosen_keyuses[part_no];
1699  uint maybe_null= test(keyinfo->key_part[part_no].null_bit);
1700 
1701  if (keyuse->val->type() == Item::FIELD_ITEM)
1702  {
1703  // Look up the most appropriate field to base the ref access on.
1704  keyuse->val= get_best_field(static_cast<Item_field *>(keyuse->val),
1705  join->cond_equal);
1706  keyuse->used_tables= keyuse->val->used_tables();
1707  }
1708  j->ref.items[part_no]=keyuse->val; // Save for cond removal
1709  j->ref.cond_guards[part_no]= keyuse->cond_guard;
1710  if (keyuse->null_rejecting)
1711  j->ref.null_rejecting|= (key_part_map)1 << part_no;
1712  keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
1713 
1714  store_key* key= get_store_key(thd,
1715  keyuse,join->const_table_map,
1716  &keyinfo->key_part[part_no],
1717  key_buff, maybe_null);
1718  if (unlikely(!key || thd->is_fatal_error))
1719  DBUG_RETURN(TRUE);
1720 
1721  if (keyuse->used_tables || thd->lex->describe)
1722  /*
1723  Comparing against a non-constant or executing an EXPLAIN
1724  query (which refers to this info when printing the 'ref'
1725  column of the query plan)
1726  */
1727  j->ref.key_copy[part_no]= key;
1728  else
1729  {
1730  /*
1731  key is const, copy value now and possibly skip it while ::exec().
1732 
1733  Note:
1734  Result check of store_key::copy() is unnecessary,
1735  it could be an error returned by store_key::copy() method
1736  but stored value is not null and default value could be used
1737  in this case. Methods which used for storing the value
1738  should be responsible for proper null value setting
1739  in case of an error. Thus it's enough to check key->null_key
1740  value only.
1741  */
1742  (void) key->copy();
1743  /*
1744  It should be reevaluated in ::exec() if
1745  constant evaluated to NULL value which we might need to
1746  handle as a special case during JOIN::exec()
1747  (As in : 'Full scan on NULL key')
1748  */
1749  if (key->null_key)
1750  j->ref.key_copy[part_no]= key; // Reevaluate in JOIN::exec()
1751  else
1752  j->ref.key_copy[part_no]= NULL;
1753  }
1754  /*
1755  Remember if we are going to use REF_OR_NULL
1756  But only if field _really_ can be null i.e. we force JT_REF
1757  instead of JT_REF_OR_NULL in case if field can't be null
1758  */
1759  if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
1760  {
1761  DBUG_ASSERT(null_ref_key == NULL); // or we would overwrite it below
1762  null_ref_key= key_buff;
1763  }
1764  key_buff+=keyinfo->key_part[part_no].store_length;
1765  }
1766  } /* not ftkey */
1767  if (j->type == JT_FT)
1768  DBUG_RETURN(false);
1769  if (j->type == JT_CONST)
1770  j->table->const_table= 1;
1771  else if (((actual_key_flags(keyinfo) &
1772  (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
1773  keyparts != actual_key_parts(keyinfo) || null_ref_key)
1774  {
1775  /* Must read with repeat */
1776  j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
1777  j->ref.null_ref_key= null_ref_key;
1778  }
1779  else if (keyuse_uses_no_tables &&
1780  !(table->file->ha_table_flags() & HA_BLOCK_CONST_TABLE))
1781  {
1782  /*
1783  This happen if we are using a constant expression in the ON part
1784  of an LEFT JOIN.
1785  SELECT * FROM a LEFT JOIN b ON b.key=30
1786  Here we should not mark the table as a 'const' as a field may
1787  have a 'normal' value or a NULL value.
1788  */
1789  j->type=JT_CONST;
1790  }
1791  else
1792  j->type=JT_EQ_REF;
1793  DBUG_RETURN(false);
1794 }
1795 
1796 
1797 
1798 static store_key *
1799 get_store_key(THD *thd, Key_use *keyuse, table_map used_tables,
1800  KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
1801 {
1802  if (!((~used_tables) & keyuse->used_tables)) // if const item
1803  {
1804  return new store_key_const_item(thd,
1805  key_part->field,
1806  key_buff + maybe_null,
1807  maybe_null ? key_buff : 0,
1808  key_part->length,
1809  keyuse->val);
1810  }
1811 
1812  Item_field *field_item= NULL;
1813  if (keyuse->val->type() == Item::FIELD_ITEM)
1814  field_item= static_cast<Item_field*>(keyuse->val->real_item());
1815  else if (keyuse->val->type() == Item::REF_ITEM)
1816  {
1817  Item_ref *item_ref= static_cast<Item_ref*>(keyuse->val);
1818  if (item_ref->ref_type() == Item_ref::OUTER_REF)
1819  {
1820  if ((*item_ref->ref)->type() == Item::FIELD_ITEM)
1821  field_item= static_cast<Item_field*>(item_ref->real_item());
1822  else if ((*(Item_ref**)(item_ref)->ref)->ref_type()
1823  == Item_ref::DIRECT_REF
1824  &&
1825  item_ref->real_item()->type() == Item::FIELD_ITEM)
1826  field_item= static_cast<Item_field*>(item_ref->real_item());
1827  }
1828  }
1829  if (field_item)
1830  return new store_key_field(thd,
1831  key_part->field,
1832  key_buff + maybe_null,
1833  maybe_null ? key_buff : 0,
1834  key_part->length,
1835  field_item->field,
1836  keyuse->val->full_name());
1837 
1838  return new store_key_item(thd,
1839  key_part->field,
1840  key_buff + maybe_null,
1841  maybe_null ? key_buff : 0,
1842  key_part->length,
1843  keyuse->val);
1844 }
1845 
1846 
1860 bool and_conditions(Item **e1, Item *e2)
1861 {
1862  DBUG_ASSERT(!(*e1) || (*e1)->fixed);
1863  DBUG_ASSERT(!e2 || e2->fixed);
1864  if (*e1)
1865  {
1866  if (!e2)
1867  return false;
1868  Item *res= new Item_cond_and(*e1, e2);
1869  if (unlikely(!res))
1870  return true;
1871 
1872  *e1= res;
1873  res->quick_fix_field();
1874  res->update_used_tables();
1875 
1876  }
1877  else
1878  *e1= e2;
1879  return false;
1880 }
1881 
1882 
1883 #define ICP_COND_USES_INDEX_ONLY 10
1884 
1885 /*
1886  Get a part of the condition that can be checked using only index fields
1887 
1888  SYNOPSIS
1889  make_cond_for_index()
1890  cond The source condition
1891  table The table that is partially available
1892  keyno The index in the above table. Only fields covered by the index
1893  are available
1894  other_tbls_ok TRUE <=> Fields of other non-const tables are allowed
1895 
1896  DESCRIPTION
1897  Get a part of the condition that can be checked when for the given table
1898  we have values only of fields covered by some index. The condition may
1899  refer to other tables, it is assumed that we have values of all of their
1900  fields.
1901 
1902  Example:
1903  make_cond_for_index(
1904  "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
1905  t2, keyno(t2.key1))
1906  will return
1907  "cond(t1.field) AND cond(t2.key2)"
1908 
1909  RETURN
1910  Index condition, or NULL if no condition could be inferred.
1911 */
1912 
1913 static Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
1914  bool other_tbls_ok)
1915 {
1916  DBUG_ASSERT(cond != NULL);
1917 
1918  if (cond->type() == Item::COND_ITEM)
1919  {
1920  uint n_marked= 0;
1921  if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
1922  {
1923  table_map used_tables= 0;
1924  Item_cond_and *new_cond=new Item_cond_and;
1925  if (!new_cond)
1926  return NULL;
1927  List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1928  Item *item;
1929  while ((item=li++))
1930  {
1931  Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
1932  if (fix)
1933  {
1934  new_cond->argument_list()->push_back(fix);
1935  used_tables|= fix->used_tables();
1936  }
1937  n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
1938  }
1939  if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
1940  cond->marker= ICP_COND_USES_INDEX_ONLY;
1941  switch (new_cond->argument_list()->elements) {
1942  case 0:
1943  return NULL;
1944  case 1:
1945  new_cond->set_used_tables(used_tables);
1946  return new_cond->argument_list()->head();
1947  default:
1948  new_cond->quick_fix_field();
1949  new_cond->set_used_tables(used_tables);
1950  return new_cond;
1951  }
1952  }
1953  else /* It's OR */
1954  {
1955  Item_cond_or *new_cond=new Item_cond_or;
1956  if (!new_cond)
1957  return NULL;
1958  List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1959  Item *item;
1960  while ((item=li++))
1961  {
1962  Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
1963  if (!fix)
1964  return NULL;
1965  new_cond->argument_list()->push_back(fix);
1966  n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
1967  }
1968  if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
1969  cond->marker= ICP_COND_USES_INDEX_ONLY;
1970  new_cond->quick_fix_field();
1971  new_cond->set_used_tables(cond->used_tables());
1972  new_cond->top_level_item();
1973  return new_cond;
1974  }
1975  }
1976 
1977  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
1978  {
1979  /*
1980  Reset marker since it might have the value
1981  ICP_COND_USES_INDEX_ONLY if this condition is part of the select
1982  condition for multiple tables.
1983  */
1984  cond->marker= 0;
1985  return NULL;
1986  }
1987  cond->marker= ICP_COND_USES_INDEX_ONLY;
1988  return cond;
1989 }
1990 
1991 
1992 static Item *make_cond_remainder(Item *cond, bool exclude_index)
1993 {
1994  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
1995  return 0; /* Already checked */
1996 
1997  if (cond->type() == Item::COND_ITEM)
1998  {
1999  table_map tbl_map= 0;
2000  if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
2001  {
2002  /* Create new top level AND item */
2003  Item_cond_and *new_cond=new Item_cond_and;
2004  if (!new_cond)
2005  return (Item*) 0;
2006  List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2007  Item *item;
2008  while ((item=li++))
2009  {
2010  Item *fix= make_cond_remainder(item, exclude_index);
2011  if (fix)
2012  {
2013  new_cond->argument_list()->push_back(fix);
2014  tbl_map |= fix->used_tables();
2015  }
2016  }
2017  switch (new_cond->argument_list()->elements) {
2018  case 0:
2019  return (Item*) 0;
2020  case 1:
2021  return new_cond->argument_list()->head();
2022  default:
2023  new_cond->quick_fix_field();
2024  new_cond->set_used_tables(tbl_map);
2025  return new_cond;
2026  }
2027  }
2028  else /* It's OR */
2029  {
2030  Item_cond_or *new_cond=new Item_cond_or;
2031  if (!new_cond)
2032  return (Item*) 0;
2033  List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2034  Item *item;
2035  while ((item=li++))
2036  {
2037  Item *fix= make_cond_remainder(item, FALSE);
2038  if (!fix)
2039  return (Item*) 0;
2040  new_cond->argument_list()->push_back(fix);
2041  tbl_map |= fix->used_tables();
2042  }
2043  new_cond->quick_fix_field();
2044  new_cond->set_used_tables(tbl_map);
2045  new_cond->top_level_item();
2046  return new_cond;
2047  }
2048  }
2049  return cond;
2050 }
2051 
2052 
2063 static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok,
2064  Opt_trace_object *trace_obj)
2065 {
2066  DBUG_ENTER("push_index_cond");
2067 
2068  /*
2069  We will only attempt to push down an index condition when the
2070  following criteria are true:
2071  0. The table has a select condition
2072  1. The storage engine supports ICP.
2073  2. The system variable for enabling ICP is ON.
2074  3. The query is not a multi-table update or delete statement. The reason
2075  for this requirement is that the same handler will be used
2076  both for doing the select/join and the update. The pushed index
2077  condition might then also be applied by the storage engine
2078  when doing the update part and result in either not finding
2079  the record to update or updating the wrong record.
2080  4. The JOIN_TAB is not part of a subquery that has guarded conditions
2081  that can be turned on or off during execution of a 'Full scan on NULL
2082  key'.
2083  @see Item_in_optimizer::val_int()
2084  @see subselect_single_select_engine::exec()
2085  @see TABLE_REF::cond_guards
2086  @see setup_join_buffering
2087  5. The join type is not CONST or SYSTEM. The reason for excluding
2088  these join types, is that these are optimized to only read the
2089  record once from the storage engine and later re-use it. In a
2090  join where a pushed index condition evaluates fields from
2091  tables earlier in the join sequence, the pushed condition would
2092  only be evaluated the first time the record value was needed.
2093  6. The index is not a clustered index. The performance improvement
2094  of pushing an index condition on a clustered key is much lower
2095  than on a non-clustered key. This restriction should be
2096  re-evaluated when WL#6061 is implemented.
2097  */
2098  if (tab->condition() &&
2099  tab->table->file->index_flags(keyno, 0, 1) &
2100  HA_DO_INDEX_COND_PUSHDOWN &&
2101  tab->join->thd->optimizer_switch_flag(OPTIMIZER_SWITCH_INDEX_CONDITION_PUSHDOWN) &&
2102  tab->join->thd->lex->sql_command != SQLCOM_UPDATE_MULTI &&
2103  tab->join->thd->lex->sql_command != SQLCOM_DELETE_MULTI &&
2104  !tab->has_guarded_conds() &&
2105  tab->type != JT_CONST && tab->type != JT_SYSTEM &&
2106  !(keyno == tab->table->s->primary_key &&
2107  tab->table->file->primary_key_is_clustered()))
2108  {
2109  DBUG_EXECUTE("where", print_where(tab->condition(), "full cond",
2110  QT_ORDINARY););
2111  Item *idx_cond= make_cond_for_index(tab->condition(), tab->table,
2112  keyno, other_tbls_ok);
2113  DBUG_EXECUTE("where", print_where(idx_cond, "idx cond", QT_ORDINARY););
2114  if (idx_cond)
2115  {
2116  /*
2117  Check that the condition to push actually contains fields from
2118  the index. Without any fields from the index it is unlikely
2119  that it will filter out any records since the conditions on
2120  fields from other tables in most cases have already been
2121  evaluated.
2122  */
2123  idx_cond->update_used_tables();
2124  if ((idx_cond->used_tables() & tab->table->map) == 0)
2125  {
2126  DBUG_VOID_RETURN;
2127  }
2128 
2129  Item *idx_remainder_cond= 0;
2130  tab->pre_idx_push_cond= tab->condition();
2131 
2132  /*
2133  For BKA cache we store condition to special BKA cache field
2134  because evaluation of the condition requires additional operations
2135  before the evaluation. This condition is used in
2136  JOIN_CACHE_BKA[_UNIQUE]::skip_index_tuple() functions.
2137  */
2138  if (tab->use_join_cache &&
2139  /*
2140  if cache is used then the value is TRUE only
2141  for BKA[_UNIQUE] cache (see setup_join_buffering() func).
2142  In this case other_tbls_ok is an equivalent of
2143  cache->is_key_access().
2144  */
2145  other_tbls_ok &&
2146  (idx_cond->used_tables() &
2147  ~(tab->table->map | tab->join->const_table_map)))
2148  {
2149  tab->cache_idx_cond= idx_cond;
2150  trace_obj->add("pushed_to_BKA", true);
2151  }
2152  else
2153  {
2154  idx_remainder_cond= tab->table->file->idx_cond_push(keyno, idx_cond);
2155  tab->select->icp_cond= idx_cond;
2156  DBUG_EXECUTE("where",
2157  print_where(tab->select->icp_cond, "icp cond",
2158  QT_ORDINARY););
2159  }
2160  /*
2161  Disable eq_ref's "lookup cache" if we've pushed down an index
2162  condition.
2163  TODO: This check happens to work on current ICP implementations, but
2164  there may exist a compliant implementation that will not work
2165  correctly with it. Sort this out when we stabilize the condition
2166  pushdown APIs.
2167  */
2168  if (idx_remainder_cond != idx_cond)
2169  {
2170  tab->ref.disable_cache= TRUE;
2171  trace_obj->add("pushed_index_condition", idx_cond);
2172  }
2173 
2174  Item *row_cond= make_cond_remainder(tab->condition(), TRUE);
2175  DBUG_EXECUTE("where", print_where(row_cond, "remainder cond",
2176  QT_ORDINARY););
2177 
2178  if (row_cond)
2179  {
2180  if (!idx_remainder_cond)
2181  tab->set_condition(row_cond, __LINE__);
2182  else
2183  {
2184  and_conditions(&row_cond, idx_remainder_cond);
2185  tab->set_condition(row_cond, __LINE__);
2186  }
2187  }
2188  else
2189  tab->set_condition(idx_remainder_cond, __LINE__);
2190  trace_obj->add("table_condition_attached", tab->condition());
2191  if (tab->select)
2192  {
2193  DBUG_EXECUTE("where", print_where(tab->select->cond, "cond",
2194  QT_ORDINARY););
2195  tab->select->cond= tab->condition();
2196  }
2197  }
2198  }
2199  DBUG_VOID_RETURN;
2200 }
2201 
2202 
2203 /*
2204  Deny usage of join buffer for the specified table
2205 
2206  SYNOPSIS
2207  set_join_cache_denial()
2208  tab join table for which join buffer usage is to be denied
2209 
2210  DESCRIPTION
2211  The function denies usage of join buffer when joining the table 'tab'.
2212  The table is marked as not employing any join buffer. If a join cache
2213  object has been already allocated for the table this object is destroyed.
2214 
2215  RETURN
2216  none
2217 */
2218 
2219 static
2220 void set_join_cache_denial(JOIN_TAB *join_tab)
2221 {
2222  if (join_tab->op)
2223  {
2224  join_tab->op->free();
2225  join_tab->op= 0;
2226  }
2227  if (join_tab->use_join_cache)
2228  {
2229  join_tab->use_join_cache= JOIN_CACHE::ALG_NONE;
2230  /*
2231  It could be only sub_select(). It could not be sub_seject_sjm because we
2232  don't do join buffering for the first table in sjm nest.
2233  */
2234  join_tab[-1].next_select= sub_select;
2235  }
2236 }
2237 
2238 
2239 /*
2240  Revise usage of join buffer for the specified table and the whole nest
2241 
2242  SYNOPSIS
2243  revise_cache_usage()
2244  tab join table for which join buffer usage is to be revised
2245 
2246  DESCRIPTION
2247  The function revise the decision to use a join buffer for the table 'tab'.
2248  If this table happened to be among the inner tables of a nested outer join/
2249  semi-join the functions denies usage of join buffers for all of them
2250 
2251  RETURN
2252  none
2253 */
2254 
2255 static
2256 void revise_cache_usage(JOIN_TAB *join_tab)
2257 {
2258  JOIN_TAB *tab;
2259  JOIN_TAB *first_inner;
2260 
2261  if (join_tab->first_inner)
2262  {
2263  JOIN_TAB *end_tab= join_tab;
2264  for (first_inner= join_tab->first_inner;
2265  first_inner;
2266  first_inner= first_inner->first_upper)
2267  {
2268  for (tab= end_tab-1; tab >= first_inner; tab--)
2269  set_join_cache_denial(tab);
2270  end_tab= first_inner;
2271  }
2272  }
2273  else if (join_tab->get_sj_strategy() == SJ_OPT_FIRST_MATCH)
2274  {
2275  first_inner= join_tab->first_sj_inner_tab;
2276  for (tab= join_tab-1; tab >= first_inner; tab--)
2277  {
2278  if (tab->first_sj_inner_tab == first_inner)
2279  set_join_cache_denial(tab);
2280  }
2281  }
2282  else set_join_cache_denial(join_tab);
2283 }
2284 
2285 
2375 static bool setup_join_buffering(JOIN_TAB *tab, JOIN *join,
2376  ulonglong options, uint no_jbuf_after,
2377  bool *icp_other_tables_ok)
2378 {
2379  uint flags;
2380  Cost_estimate cost;
2381  ha_rows rows;
2382  uint bufsz= 4096;
2383  JOIN_CACHE *prev_cache;
2384  const bool bnl_on= join->thd->optimizer_switch_flag(OPTIMIZER_SWITCH_BNL);
2385  const bool bka_on= join->thd->optimizer_switch_flag(OPTIMIZER_SWITCH_BKA);
2386  const uint tableno= tab - join->join_tab;
2387  const uint tab_sj_strategy= tab->get_sj_strategy();
2388  bool use_bka_unique= false;
2389  DBUG_EXECUTE_IF("test_bka_unique", use_bka_unique= true;);
2390  *icp_other_tables_ok= TRUE;
2391 
2392  if (!(bnl_on || bka_on) || tableno == join->const_tables)
2393  {
2394  DBUG_ASSERT(tab->use_join_cache == JOIN_CACHE::ALG_NONE);
2395  return false;
2396  }
2397  if (options & SELECT_NO_JOIN_CACHE)
2398  goto no_join_cache;
2399  /*
2400  psergey-todo: why the below when execution code seems to handle the
2401  "range checked for each record" case?
2402  */
2403  if (tab->use_quick == QS_DYNAMIC_RANGE)
2404  goto no_join_cache;
2405 
2406  /* No join buffering if prevented by no_jbuf_after */
2407  if (tableno > no_jbuf_after)
2408  goto no_join_cache;
2409 
2410  /*
2411  An inner table of an outer join nest must not use join buffering if
2412  the first inner table of that outer join nest does not use join buffering.
2413  This condition is not handled by earlier optimizer stages.
2414  */
2415  if (tab->first_inner != NULL &&
2416  tab->first_inner != tab &&
2417  !tab->first_inner->use_join_cache)
2418  goto no_join_cache;
2419  /*
2420  The first inner table of an outer join nest must not use join buffering
2421  if the tables in the embedding outer join nest do not use join buffering.
2422  This condition is not handled by earlier optimizer stages.
2423  */
2424  if (tab->first_upper != NULL &&
2425  !tab->first_upper->use_join_cache)
2426  goto no_join_cache;
2427 
2428  switch (tab_sj_strategy)
2429  {
2430  case SJ_OPT_FIRST_MATCH:
2431  /*
2432  Use join cache with FirstMatch semi-join strategy only when semi-join
2433  contains only one table.
2434  */
2435  if (!tab->is_single_inner_of_semi_join())
2436  {
2437  DBUG_ASSERT(tab->use_join_cache == JOIN_CACHE::ALG_NONE);
2438  goto no_join_cache;
2439  }
2440  break;
2441 
2442  case SJ_OPT_LOOSE_SCAN:
2443  /* No join buffering if this semijoin nest is handled by loosescan */
2444  DBUG_ASSERT(tab->use_join_cache == JOIN_CACHE::ALG_NONE);
2445  goto no_join_cache;
2446 
2447  case SJ_OPT_MATERIALIZE_LOOKUP:
2448  case SJ_OPT_MATERIALIZE_SCAN:
2449  /*
2450  The Materialize strategies reuse the join_tab belonging to the
2451  first table that was materialized. Neither table can use join buffering:
2452  - The first table in a join never uses join buffering.
2453  - The join_tab used for looking up a row in the materialized table, or
2454  scanning the rows of a materialized table, cannot use join buffering.
2455  We allow join buffering for the remaining tables of the materialized
2456  semi-join nest.
2457  */
2458  if (tab->first_sj_inner_tab == tab)
2459  {
2460  DBUG_ASSERT(tab->use_join_cache == JOIN_CACHE::ALG_NONE);
2461  goto no_join_cache;
2462  }
2463  break;
2464 
2465  case SJ_OPT_DUPS_WEEDOUT:
2466  // This strategy allows the same join buffering as a regular join would.
2467  case SJ_OPT_NONE:
2468  break;
2469  }
2470 
2471  /*
2472  Link with the previous join cache, but make sure that we do not link
2473  join caches of two different operations when the previous operation was
2474  MaterializeLookup or MaterializeScan, ie if:
2475  1. the previous join_tab has join buffering enabled, and
2476  2. the previous join_tab belongs to a materialized semi-join nest, and
2477  3. this join_tab represents a regular table, or is part of a different
2478  semi-join interval than the previous join_tab.
2479  */
2480  prev_cache= (JOIN_CACHE*)(tab-1)->op;
2481  if (prev_cache != NULL && // 1
2482  sj_is_materialize_strategy((tab-1)->get_sj_strategy()) && // 2
2483  tab->first_sj_inner_tab != (tab-1)->first_sj_inner_tab) // 3
2484  prev_cache= NULL;
2485 
2486  /*
2487  The following code prevents use of join buffering when there is an
2488  outer join operation and first match semi-join strategy is used, because:
2489 
2490  Outer join needs a "match flag" to track that a row should be
2491  NULL-complemented, such flag being attached to first inner table's cache
2492  (tracks whether the cached row from outer table got a match, in which case
2493  no NULL-complemented row is needed).
2494 
2495  FirstMatch also needs a "match flag", such flag is attached to sj inner
2496  table's cache (tracks whether the cached row from outer table already got
2497  a first match in the sj-inner table, in which case we don't need to join
2498  this cached row again)
2499  - but a row in a cache has only one "match flag"
2500  - so if "sj inner table"=="first inner", there is a problem.
2501  */
2502  if (tab_sj_strategy == SJ_OPT_FIRST_MATCH &&
2503  tab->is_inner_table_of_outer_join())
2504  goto no_join_cache;
2505 
2506  switch (tab->type) {
2507  case JT_ALL:
2508  if (!bnl_on)
2509  {
2510  DBUG_ASSERT(tab->use_join_cache == JOIN_CACHE::ALG_NONE);
2511  goto no_join_cache;
2512  }
2513 
2514  if ((options & SELECT_DESCRIBE) ||
2515  ((tab->op= new JOIN_CACHE_BNL(join, tab, prev_cache)) &&
2516  !tab->op->init()))
2517  {
2518  *icp_other_tables_ok= FALSE;
2519  DBUG_ASSERT(might_do_join_buffering(join_buffer_alg(join->thd), tab));
2520  tab->use_join_cache= JOIN_CACHE::ALG_BNL;
2521  return false;
2522  }
2523  goto no_join_cache;
2524  case JT_SYSTEM:
2525  case JT_CONST:
2526  case JT_REF:
2527  case JT_EQ_REF:
2528  if (!bka_on)
2529  {
2530  DBUG_ASSERT(tab->use_join_cache == JOIN_CACHE::ALG_NONE);
2531  goto no_join_cache;
2532  }
2533 
2534  /*
2535  Disable BKA for materializable derived tables/views as they aren't
2536  instantiated yet.
2537  */
2538  if (tab->table->pos_in_table_list->uses_materialization())
2539  goto no_join_cache;
2540 
2541  /*
2542  Can't use BKA for subquery if dealing with a subquery that can
2543  turn a ref access into a "full scan on NULL key" table scan.
2544 
2545  @see Item_in_optimizer::val_int()
2546  @see subselect_single_select_engine::exec()
2547  @see TABLE_REF::cond_guards
2548  @see push_index_cond()
2549 
2550  @todo: This choice to not use BKA should be done before making
2551  cost estimates, e.g. in set_join_buffer_properties(). That
2552  happens before cond guards are set up, so instead of doing the
2553  check below, BKA should be disabled if
2554  - We are in an IN subquery, and
2555  - The IN predicate is not a top_level_item, and
2556  - The left_expr of the IN predicate may contain NULL values
2557  (left_expr->maybe_null)
2558  */
2559  if (tab->has_guarded_conds())
2560  goto no_join_cache;
2561 
2562  flags= HA_MRR_NO_NULL_ENDPOINTS;
2563  if (tab->table->covering_keys.is_set(tab->ref.key))
2564  flags|= HA_MRR_INDEX_ONLY;
2565  rows= tab->table->file->multi_range_read_info(tab->ref.key, 10, 20,
2566  &bufsz, &flags, &cost);
2567  /*
2568  Cannot use BKA/BKA_UNIQUE if
2569  1. MRR scan cannot be performed, or
2570  2. MRR default implementation is used
2571  Cannot use BKA if
2572  3. HA_MRR_NO_ASSOCIATION flag is set
2573  */
2574  if ((rows == HA_POS_ERROR) || // 1
2575  (flags & HA_MRR_USE_DEFAULT_IMPL) || // 2
2576  ((flags & HA_MRR_NO_ASSOCIATION) && !use_bka_unique)) // 3
2577  goto no_join_cache;
2578 
2579  if (!(options & SELECT_DESCRIBE))
2580  {
2581  if (use_bka_unique)
2582  tab->op= new JOIN_CACHE_BKA_UNIQUE(join, tab, flags, prev_cache);
2583  else
2584  tab->op= new JOIN_CACHE_BKA(join, tab, flags, prev_cache);
2585 
2586  if (!tab->op || tab->op->init())
2587  goto no_join_cache;
2588  }
2589 
2590  DBUG_ASSERT(might_do_join_buffering(join_buffer_alg(join->thd), tab));
2591  if (use_bka_unique)
2592  tab->use_join_cache= JOIN_CACHE::ALG_BKA_UNIQUE;
2593  else
2594  tab->use_join_cache= JOIN_CACHE::ALG_BKA;
2595 
2596  return false;
2597  default : ;
2598  }
2599 
2600 no_join_cache:
2601  if (bnl_on || bka_on)
2602  revise_cache_usage(tab);
2603  tab->use_join_cache= JOIN_CACHE::ALG_NONE;
2604  return false;
2605 }
2606 
2607 
2627 bool JOIN::setup_materialized_table(JOIN_TAB *tab, uint tableno,
2628  const POSITION *inner_pos,
2629  POSITION *sjm_pos)
2630 {
2631  DBUG_ENTER("JOIN::setup_materialized_table");
2632  const TABLE_LIST *const emb_sj_nest= inner_pos->table->emb_sj_nest;
2633  Semijoin_mat_optimize *const sjm_opt= &emb_sj_nest->nested_join->sjm;
2634  Semijoin_mat_exec *const sjm_exec= tab->sj_mat_exec;
2635  const uint field_count= emb_sj_nest->nested_join->sj_inner_exprs.elements;
2636 
2637  DBUG_ASSERT(inner_pos->sj_strategy == SJ_OPT_MATERIALIZE_LOOKUP ||
2638  inner_pos->sj_strategy == SJ_OPT_MATERIALIZE_SCAN);
2639 
2640  /*
2641  Set up the table to write to, do as select_union::create_result_table does
2642  */
2643  sjm_exec->table_param.init();
2644  sjm_exec->table_param.field_count= field_count;
2645  sjm_exec->table_param.bit_fields_as_long= true;
2646 
2647  char buffer[NAME_LEN];
2648  const size_t len= my_snprintf(buffer, sizeof(buffer) - 1, "<subquery%u>",
2649  emb_sj_nest->nested_join->query_block_id);
2650  char *name= (char *)alloc_root(thd->mem_root, len + 1);
2651  if (name == NULL)
2652  DBUG_RETURN(true); /* purecov: inspected */
2653 
2654  memcpy(name, buffer, len);
2655  name[len] = '\0';
2656  TABLE *table;
2657  if (!(table= create_tmp_table(thd, &sjm_exec->table_param,
2658  emb_sj_nest->nested_join->sj_inner_exprs,
2659  NULL,
2660  true /* distinct */,
2661  true /* save_sum_fields */,
2662  thd->variables.option_bits |
2663  TMP_TABLE_ALL_COLUMNS,
2664  HA_POS_ERROR /* rows_limit */,
2665  name)))
2666  DBUG_RETURN(true); /* purecov: inspected */
2667  sjm_exec->table= table;
2668  table->tablenr= tableno;
2669  table->map= (table_map)1 << tableno;
2670  table->file->extra(HA_EXTRA_WRITE_CACHE);
2671  table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
2672  table->reginfo.join_tab= tab;
2673  sj_tmp_tables.push_back(table);
2674  sjm_exec_list.push_back(sjm_exec);
2675 
2676  if (!(sjm_opt->mat_fields=
2677  (Item_field **) alloc_root(thd->mem_root,
2678  field_count * sizeof(Item_field **))))
2679  DBUG_RETURN(true);
2680 
2681  for (uint fieldno= 0; fieldno < field_count; fieldno++)
2682  {
2683  if (!(sjm_opt->mat_fields[fieldno]= new Item_field(table->field[fieldno])))
2684  DBUG_RETURN(true);
2685  }
2686 
2687  TABLE_LIST *tl;
2688  if (!(tl= (TABLE_LIST *) alloc_root(thd->mem_root, sizeof(TABLE_LIST))))
2689  DBUG_RETURN(true);
2690  // TODO: May have to setup outer-join info for this TABLE_LIST !!!
2691 
2692  tl->init_one_table("", 0, name, strlen(name), name, TL_IGNORE);
2693 
2694  tl->table= table;
2695 
2696  tab->table= table;
2697  tab->position= sjm_pos;
2698  tab->join= this;
2699 
2700  tab->worst_seeks= 1.0;
2701  tab->records= (ha_rows)emb_sj_nest->nested_join->sjm.expected_rowcount;
2702  tab->found_records= tab->records;
2703  tab->read_time= (ha_rows)emb_sj_nest->nested_join->sjm.scan_cost.total_cost();
2704 
2705  tab->on_expr_ref= tl->join_cond_ref();
2706 
2707  tab->materialize_table= join_materialize_semijoin;
2708 
2709  table->pos_in_table_list= tl;
2710  table->keys_in_use_for_query.set_all();
2711  sjm_pos->table= tab;
2712  sjm_pos->sj_strategy= SJ_OPT_NONE;
2713 
2714  sjm_pos->use_join_buffer= false;
2715 
2716  /*
2717  Key_use objects are required so that create_ref_for_key() can set up
2718  a proper ref access for this table.
2719  */
2720  Key_use_array *keyuse=
2721  create_keyuse_for_table(thd, table, field_count, sjm_opt->mat_fields,
2722  emb_sj_nest->nested_join->sj_outer_exprs);
2723  if (!keyuse)
2724  DBUG_RETURN(true);
2725 
2726  double fanout= (tab == join_tab + tab->join->const_tables) ?
2727  1.0 : (tab-1)->position->prefix_record_count;
2728  if (!sjm_exec->is_scan)
2729  {
2730  sjm_pos->key= keyuse->begin(); // MaterializeLookup will use the index
2731  tab->keyuse= keyuse->begin();
2732  tab->keys.set_bit(0); // There is one index - use it always
2733  tab->index= 0;
2734  sjm_pos->set_prefix_costs(1.0, fanout);
2735  sjm_pos->records_read= 1.0;
2736  sjm_pos->read_time= 1.0;
2737  }
2738  else
2739  {
2740  sjm_pos->key= NULL; // No index use for MaterializeScan
2741  sjm_pos->set_prefix_costs(tab->read_time, tab->records * fanout);
2742  sjm_pos->records_read= tab->records;
2743  sjm_pos->read_time= tab->read_time;
2744  }
2745 
2746  DBUG_RETURN(false);
2747 }
2748 
2749 
2768 bool
2769 make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
2770 {
2771  const bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
2772 
2773  DBUG_ENTER("make_join_readinfo");
2774 
2775  Opt_trace_context * const trace= &join->thd->opt_trace;
2776  Opt_trace_object wrapper(trace);
2777  Opt_trace_array trace_refine_plan(trace, "refine_plan");
2778 
2779  if (setup_semijoin_dups_elimination(join, options, no_jbuf_after))
2780  DBUG_RETURN(TRUE); /* purecov: inspected */
2781 
2782  for (uint i= join->const_tables; i < join->tables; i++)
2783  {
2784  JOIN_TAB *const tab= join->join_tab+i;
2785  TABLE *const table= tab->table;
2786  if (!tab->position)
2787  continue;
2788 
2789  bool icp_other_tables_ok;
2790  tab->read_record.table= table;
2791  tab->next_select=sub_select; /* normal select */
2792  tab->cache_idx_cond= 0;
2793  table->status= STATUS_GARBAGE | STATUS_NOT_FOUND;
2794  tab->read_first_record= NULL; // Access methods not set yet
2795  tab->read_record.read_record= NULL;
2796  tab->read_record.unlock_row= rr_unlock_row;
2797 
2798  Opt_trace_object trace_refine_table(trace);
2799  trace_refine_table.add_utf8_table(table);
2800 
2801  if (tab->do_loosescan())
2802  {
2803  if (!(tab->loosescan_buf= (uchar*)join->thd->alloc(tab->
2804  loosescan_key_len)))
2805  DBUG_RETURN(TRUE); /* purecov: inspected */
2806  }
2807  switch (tab->type) {
2808  case JT_EQ_REF:
2809  case JT_REF_OR_NULL:
2810  case JT_REF:
2811  if (tab->select)
2812  tab->select->set_quick(NULL);
2813  delete tab->quick;
2814  tab->quick=0;
2815  /* fall through */
2816  case JT_SYSTEM:
2817  case JT_CONST:
2818  /* Only happens with outer joins */
2819  if (setup_join_buffering(tab, join, options, no_jbuf_after,
2820  &icp_other_tables_ok))
2821  DBUG_RETURN(true);
2822  if (tab->use_join_cache != JOIN_CACHE::ALG_NONE)
2823  tab[-1].next_select= sub_select_op;
2824 
2825  if (table->covering_keys.is_set(tab->ref.key) &&
2826  !table->no_keyread)
2827  table->set_keyread(TRUE);
2828  else
2829  push_index_cond(tab, tab->ref.key, icp_other_tables_ok,
2830  &trace_refine_table);
2831  break;
2832  case JT_ALL:
2833  if (setup_join_buffering(tab, join, options, no_jbuf_after,
2834  &icp_other_tables_ok))
2835  DBUG_RETURN(true);
2836  if (tab->use_join_cache != JOIN_CACHE::ALG_NONE)
2837  tab[-1].next_select=sub_select_op;
2838 
2839  /* These init changes read_record */
2840  if (tab->use_quick == QS_DYNAMIC_RANGE)
2841  {
2842  join->thd->set_status_no_good_index_used();
2843  tab->read_first_record= join_init_quick_read_record;
2844  if (statistics)
2845  join->thd->inc_status_select_range_check();
2846  trace_refine_table.add_alnum("access_type", "dynamic_range");
2847  }
2848  else
2849  {
2851  if (i == join->const_tables)
2852  {
2853  if (tab->select && tab->select->quick)
2854  {
2855  if (statistics)
2856  join->thd->inc_status_select_range();
2857  }
2858  else
2859  {
2860  join->thd->set_status_no_index_used();
2861  if (statistics)
2862  join->thd->inc_status_select_scan();
2863  }
2864  }
2865  else
2866  {
2867  if (tab->select && tab->select->quick)
2868  {
2869  if (statistics)
2870  join->thd->inc_status_select_full_range_join();
2871  }
2872  else
2873  {
2874  join->thd->set_status_no_index_used();
2875  if (statistics)
2876  join->thd->inc_status_select_full_join();
2877  }
2878  }
2879  if (!table->no_keyread)
2880  {
2881  if (tab->select && tab->select->quick &&
2882  tab->select->quick->index != MAX_KEY && //not index_merge
2883  table->covering_keys.is_set(tab->select->quick->index))
2884  table->set_keyread(TRUE);
2885  else if (!table->covering_keys.is_clear_all() &&
2886  !(tab->select && tab->select->quick))
2887  { // Only read index tree
2888  /*
2889  It has turned out that the below change, while speeding things
2890  up for disk-bound loads, slows them down for cases when the data
2891  is in disk cache (see BUG#35850):
2892  // See bug #26447: "Using the clustered index for a table scan
2893  // is always faster than using a secondary index".
2894  if (table->s->primary_key != MAX_KEY &&
2895  table->file->primary_key_is_clustered())
2896  tab->index= table->s->primary_key;
2897  else
2898  tab->index=find_shortest_key(table, & table->covering_keys);
2899  */
2900  if (!tab->do_loosescan())
2901  tab->index=find_shortest_key(table, & table->covering_keys);
2902  tab->read_first_record= join_read_first;
2903  tab->type=JT_INDEX_SCAN; // Read with index_first / index_next
2904  }
2905  }
2906  if (tab->select && tab->select->quick &&
2907  tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
2908  push_index_cond(tab, tab->select->quick->index, icp_other_tables_ok,
2909  &trace_refine_table);
2910  trace_refine_table.add_alnum("access_type",
2911  tab->type == JT_INDEX_SCAN ?
2912  "index_scan" :
2913  (tab->select && tab->select->quick) ?
2914  "range" : "table_scan");
2915  }
2916  break;
2917  case JT_FT:
2918  break;
2919  default:
2920  DBUG_PRINT("error",("Table type %d found",tab->type)); /* purecov: deadcode */
2921  break; /* purecov: deadcode */
2922  case JT_UNKNOWN:
2923  abort(); /* purecov: deadcode */
2924  }
2925  // Materialize derived tables prior to accessing them.
2926  if (tab->table->pos_in_table_list->uses_materialization())
2927  tab->materialize_table= join_materialize_derived;
2928  }
2929 
2930  for (uint i= join->const_tables; i < join->primary_tables; i++)
2931  {
2932  if (join->join_tab[i].use_join_cache != JOIN_CACHE::ALG_NONE)
2933  {
2934  /*
2935  A join buffer is used for this table. We here inform the optimizer
2936  that it should not rely on rows of the first non-const table being in
2937  order thanks to an index scan; indeed join buffering of the present
2938  table subsequently changes the order of rows.
2939  */
2940  if (join->order != NULL)
2941  join->simple_order= false;
2942  if (join->group_list != NULL)
2943  join->simple_group= false;
2944  break;
2945  }
2946  }
2947 
2948  DBUG_RETURN(FALSE);
2949 }
2950 
2951 
2967 {
2968  for (uint i= 0; i < join->primary_tables; i++)
2969  {
2970  JOIN_TAB *const tab= join->join_tab + i;
2971 
2972  if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
2973  {
2974  /* This error should not be ignored. */
2975  join->select_lex->no_error= FALSE;
2976  my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
2977  ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
2978  return true;
2979  }
2980  }
2981  return false;
2982 }
2983 
2984 
2995 {
2996  delete select;
2997  select= 0;
2998  delete quick;
2999  quick= 0;
3000  limit= 0;
3001 
3002  // Free select that was created for filesort outside of create_sort_index
3003  if (filesort && filesort->select && !filesort->own_select)
3004  delete filesort->select;
3005  delete filesort;
3006  filesort= NULL;
3007  /* Skip non-existing derived tables/views result tables */
3008  if (table &&
3009  (table->s->tmp_table != INTERNAL_TMP_TABLE || table->is_created()))
3010  {
3011  table->set_keyread(FALSE);
3012  table->file->ha_index_or_rnd_end();
3013 
3014  free_io_cache(table);
3015  filesort_free_buffers(table, true);
3016  /*
3017  We need to reset this for next select
3018  (Tested in part_of_refkey)
3019  */
3020  table->reginfo.join_tab= NULL;
3021  if (table->pos_in_table_list)
3022  {
3023  table->pos_in_table_list->derived_keys_ready= false;
3024  table->pos_in_table_list->derived_key_list.empty();
3025  }
3026  }
3027  end_read_record(&read_record);
3028 }
3029 
3031 {
3032  return sj_is_materialize_strategy(get_sj_strategy()) ?
3033  first_sj_inner_tab->emb_sj_nest->nested_join->query_block_id : 0;
3034 }
3035 
3036 
3049 {
3050  if (and_with_condition(add_cond, line))
3051  return true;
3052 
3053  if (select)
3054  {
3055  DBUG_PRINT("info",
3056  ("select::cond extended. Change %p -> %p "
3057  "at line %u tab %p select %p",
3058  select->cond, m_condition, line, this, select));
3059  select->cond= m_condition;
3060  }
3061  return false;
3062 }
3063 
3074 bool JOIN_TAB::and_with_condition(Item *add_cond, uint line)
3075 {
3076  Item *old_cond __attribute__((unused))= m_condition;
3077  if (and_conditions(&m_condition, add_cond))
3078  return true;
3079  DBUG_PRINT("info", ("JOIN_TAB::m_condition extended. Change %p -> %p "
3080  "at line %u tab %p",
3081  old_cond, m_condition, line, this));
3082  return false;
3083 }
3084 
3085 
3093 {
3094  return filesort && filesort->select ? filesort->select->cond : condition();
3095 }
3096 
3097 
3142 {
3143  SELECT_LEX_UNIT *tmp_unit;
3144  SELECT_LEX *sl;
3145  /*
3146  Optimization: if not EXPLAIN and we are done with the JOIN,
3147  free all tables.
3148  */
3149  bool full= (!select_lex->uncacheable && !thd->lex->describe);
3150  bool can_unlock= full;
3151  DBUG_ENTER("JOIN::join_free");
3152 
3153  cleanup(full);
3154 
3155  for (tmp_unit= select_lex->first_inner_unit();
3156  tmp_unit;
3157  tmp_unit= tmp_unit->next_unit())
3158  for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
3159  {
3160  Item_subselect *subselect= sl->master_unit()->item;
3161  bool full_local= full && (!subselect || subselect->is_evaluated());
3162  /*
3163  If this join is evaluated, we can fully clean it up and clean up all
3164  its underlying joins even if they are correlated -- they will not be
3165  used any more anyway.
3166  If this join is not yet evaluated, we still must clean it up to
3167  close its table cursors -- it may never get evaluated, as in case of
3168  ... HAVING FALSE OR a IN (SELECT ...))
3169  but all table cursors must be closed before the unlock.
3170  */
3171  sl->cleanup_all_joins(full_local);
3172  /* Can't unlock if at least one JOIN is still needed */
3173  can_unlock= can_unlock && full_local;
3174  }
3175 
3176  /*
3177  We are not using tables anymore
3178  Unlock all tables. We may be in an INSERT .... SELECT statement.
3179  */
3180  if (can_unlock && lock && thd->lock && ! thd->locked_tables_mode &&
3181  !(select_options & SELECT_NO_UNLOCK) &&
3182  !select_lex->subquery_in_having &&
3183  (select_lex == (thd->lex->unit.fake_select_lex ?
3184  thd->lex->unit.fake_select_lex : &thd->lex->select_lex)))
3185  {
3186  /*
3187  TODO: unlock tables even if the join isn't top level select in the
3188  tree.
3189  */
3190  mysql_unlock_read_tables(thd, lock); // Don't free join->lock
3191  lock= 0;
3192  }
3193 
3194  DBUG_VOID_RETURN;
3195 }
3196 
3197 
3210 void JOIN::cleanup(bool full)
3211 {
3212  DBUG_ENTER("JOIN::cleanup");
3213 
3214  DBUG_ASSERT(const_tables <= primary_tables &&
3215  primary_tables <= tables);
3216 
3217  if (join_tab)
3218  {
3219  JOIN_TAB *tab,*end;
3220 
3221  if (full)
3222  {
3223  for (tab= join_tab, end= tab + tables; tab < end; tab++)
3224  tab->cleanup();
3225  }
3226  else
3227  {
3228  for (tab= join_tab, end= tab + tables; tab < end; tab++)
3229  {
3230  if (!tab->table)
3231  continue;
3232  if (tab->table->is_created())
3233  {
3234  tab->table->file->ha_index_or_rnd_end();
3235  if (tab->op &&
3236  tab->op->type() == QEP_operation::OT_TMP_TABLE)
3237  {
3238  int tmp= 0;
3239  if ((tmp= tab->table->file->extra(HA_EXTRA_NO_CACHE)))
3240  tab->table->file->print_error(tmp, MYF(0));
3241  }
3242  }
3243  free_io_cache(tab->table);
3244  filesort_free_buffers(tab->table, full);
3245  }
3246  }
3247  }
3248  /*
3249  We are not using tables anymore
3250  Unlock all tables. We may be in an INSERT .... SELECT statement.
3251  */
3252  if (full)
3253  {
3254  // Run Cached_item DTORs!
3255  group_fields.delete_elements();
3256 
3257  /*
3258  We can't call delete_elements() on copy_funcs as this will cause
3259  problems in free_elements() as some of the elements are then deleted.
3260  */
3261  tmp_table_param.copy_funcs.empty();
3262  tmp_table_param.cleanup();
3263  }
3264  /* Restore ref array to original state */
3265  if (current_ref_ptrs != items0)
3266  {
3267  set_items_ref_array(items0);
3268  set_group_rpa= false;
3269  }
3270  DBUG_VOID_RETURN;
3271 }
3272 
3273 
3291 {
3292  if (!order || !where)
3293  return order;
3294 
3295  ORDER *first= NULL, *prev= NULL;
3296  for (; order; order= order->next)
3297  {
3298  DBUG_ASSERT(!order->item[0]->with_sum_func); // should never happen
3299  if (!const_expression_in_where(where, order->item[0]))
3300  {
3301  if (!first)
3302  first= order;
3303  if (prev)
3304  prev->next= order;
3305  prev= order;
3306  }
3307  }
3308  if (prev)
3309  prev->next= NULL;
3310  return first;
3311 }
3312 
3313 
3314 
3315 
3316 /*
3317  Check if equality can be used in removing components of GROUP BY/DISTINCT
3318 
3319  SYNOPSIS
3320  test_if_equality_guarantees_uniqueness()
3321  l the left comparison argument (a field if any)
3322  r the right comparison argument (a const of any)
3323 
3324  DESCRIPTION
3325  Checks if an equality predicate can be used to take away
3326  DISTINCT/GROUP BY because it is known to be true for exactly one
3327  distinct value (e.g. <expr> == <const>).
3328  Arguments must be of the same type because e.g.
3329  <string_field> = <int_const> may match more than 1 distinct value from
3330  the column.
3331  We must take into consideration and the optimization done for various
3332  string constants when compared to dates etc (see Item_int_with_ref) as
3333  well as the collation of the arguments.
3334 
3335  RETURN VALUE
3336  TRUE can be used
3337  FALSE cannot be used
3338 */
3339 static bool
3340 test_if_equality_guarantees_uniqueness(Item *l, Item *r)
3341 {
3342  return r->const_item() &&
3343  /* elements must be compared as dates */
3344  (Arg_comparator::can_compare_as_dates(l, r, 0) ||
3345  /* or of the same result type */
3346  (r->result_type() == l->result_type() &&
3347  /* and must have the same collation if compared as strings */
3348  (l->result_type() != STRING_RESULT ||
3349  l->collation.collation == r->collation.collation)));
3350 }
3351 
3352 
3353 /*
3354  Return TRUE if i1 and i2 (if any) are equal items,
3355  or if i1 is a wrapper item around the f2 field.
3356 */
3357 
3358 static bool equal(Item *i1, Item *i2, Field *f2)
3359 {
3360  DBUG_ASSERT((i2 == NULL) ^ (f2 == NULL));
3361 
3362  if (i2 != NULL)
3363  return i1->eq(i2, 1);
3364  else if (i1->type() == Item::FIELD_ITEM)
3365  return f2->eq(((Item_field *) i1)->field);
3366  else
3367  return FALSE;
3368 }
3369 
3370 
3386 bool
3387 const_expression_in_where(Item *cond, Item *comp_item, Field *comp_field,
3388  Item **const_item)
3389 {
3390  DBUG_ASSERT((comp_item == NULL) ^ (comp_field == NULL));
3391 
3392  Item *intermediate= NULL;
3393  if (const_item == NULL)
3394  const_item= &intermediate;
3395 
3396  if (cond->type() == Item::COND_ITEM)
3397  {
3398  bool and_level= (((Item_cond*) cond)->functype()
3399  == Item_func::COND_AND_FUNC);
3400  List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
3401  Item *item;
3402  while ((item=li++))
3403  {
3404  bool res=const_expression_in_where(item, comp_item, comp_field,
3405  const_item);
3406  if (res) // Is a const value
3407  {
3408  if (and_level)
3409  return 1;
3410  }
3411  else if (!and_level)
3412  return 0;
3413  }
3414  return and_level ? 0 : 1;
3415  }
3416  else if (cond->eq_cmp_result() != Item::COND_OK)
3417  { // boolean compare function
3418  Item_func* func= (Item_func*) cond;
3419  if (func->functype() != Item_func::EQUAL_FUNC &&
3420  func->functype() != Item_func::EQ_FUNC)
3421  return 0;
3422  Item *left_item= ((Item_func*) cond)->arguments()[0];
3423  Item *right_item= ((Item_func*) cond)->arguments()[1];
3424  if (equal(left_item, comp_item, comp_field))
3425  {
3426  if (test_if_equality_guarantees_uniqueness (left_item, right_item))
3427  {
3428  if (*const_item)
3429  return right_item->eq(*const_item, 1);
3430  *const_item=right_item;
3431  return 1;
3432  }
3433  }
3434  else if (equal(right_item, comp_item, comp_field))
3435  {
3436  if (test_if_equality_guarantees_uniqueness (right_item, left_item))
3437  {
3438  if (*const_item)
3439  return left_item->eq(*const_item, 1);
3440  *const_item=left_item;
3441  return 1;
3442  }
3443  }
3444  }
3445  return 0;
3446 }
3447 
3448 
3474 int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
3475  uint *used_key_parts)
3476 {
3477  KEY_PART_INFO *key_part,*key_part_end;
3478  key_part=table->key_info[idx].key_part;
3479  key_part_end=key_part+table->key_info[idx].user_defined_key_parts;
3480  key_part_map const_key_parts=table->const_key_parts[idx];
3481  int reverse=0;
3482  uint key_parts;
3483  my_bool on_pk_suffix= FALSE;
3484  DBUG_ENTER("test_if_order_by_key");
3485 
3486  for (; order ; order=order->next, const_key_parts>>=1)
3487  {
3488 
3489  /*
3490  Since only fields can be indexed, ORDER BY <something> that is
3491  not a field cannot be resolved by using an index.
3492  */
3493  Item *real_itm= (*order->item)->real_item();
3494  if (real_itm->type() != Item::FIELD_ITEM)
3495  DBUG_RETURN(0);
3496 
3497  Field *field= static_cast<Item_field*>(real_itm)->field;
3498  int flag;
3499 
3500  /*
3501  Skip key parts that are constants in the WHERE clause.
3502  These are already skipped in the ORDER BY by const_expression_in_where()
3503  */
3504  for (; const_key_parts & 1 ; const_key_parts>>= 1)
3505  key_part++;
3506 
3507  if (key_part == key_part_end)
3508  {
3509  /*
3510  We are at the end of the key. Check if the engine has the primary
3511  key as a suffix to the secondary keys. If it has continue to check
3512  the primary key as a suffix.
3513  */
3514  if (!on_pk_suffix &&
3515  (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
3516  table->s->primary_key != MAX_KEY &&
3517  table->s->primary_key != idx)
3518  {
3519  on_pk_suffix= TRUE;
3520  key_part= table->key_info[table->s->primary_key].key_part;
3521  key_part_end=key_part +
3522  table->key_info[table->s->primary_key].user_defined_key_parts;
3523  const_key_parts=table->const_key_parts[table->s->primary_key];
3524 
3525  for (; const_key_parts & 1 ; const_key_parts>>= 1)
3526  key_part++;
3527  /*
3528  The primary and secondary key parts were all const (i.e. there's
3529  one row). The sorting doesn't matter.
3530  */
3531  if (key_part == key_part_end && reverse == 0)
3532  {
3533  key_parts= 0;
3534  reverse= 1;
3535  goto ok;
3536  }
3537  }
3538  else
3539  DBUG_RETURN(0);
3540  }
3541 
3542  if (key_part->field != field || !field->part_of_sortkey.is_set(idx))
3543  DBUG_RETURN(0);
3544 
3545  const ORDER::enum_order keypart_order=
3546  (key_part->key_part_flag & HA_REVERSE_SORT) ?
3547  ORDER::ORDER_DESC : ORDER::ORDER_ASC;
3548  /* set flag to 1 if we can use read-next on key, else to -1 */
3549  flag= (order->direction == keypart_order) ? 1 : -1;
3550  if (reverse && flag != reverse)
3551  DBUG_RETURN(0);
3552  reverse=flag; // Remember if reverse
3553  key_part++;
3554  }
3555  if (on_pk_suffix)
3556  {
3557  uint used_key_parts_secondary= table->key_info[idx].user_defined_key_parts;
3558  uint used_key_parts_pk=
3559  (uint) (key_part - table->key_info[table->s->primary_key].key_part);
3560  key_parts= used_key_parts_pk + used_key_parts_secondary;
3561 
3562  if (reverse == -1 &&
3563  (!(table->file->index_flags(idx, used_key_parts_secondary - 1, 1) &
3564  HA_READ_PREV) ||
3565  !(table->file->index_flags(table->s->primary_key,
3566  used_key_parts_pk - 1, 1) & HA_READ_PREV)))
3567  reverse= 0; // Index can't be used
3568  }
3569  else
3570  {
3571  key_parts= (uint) (key_part - table->key_info[idx].key_part);
3572  if (reverse == -1 &&
3573  !(table->file->index_flags(idx, key_parts-1, 1) & HA_READ_PREV))
3574  reverse= 0; // Index can't be used
3575  }
3576 ok:
3577  if (used_key_parts != NULL)
3578  *used_key_parts= key_parts;
3579  DBUG_RETURN(reverse);
3580 }
3581 
3582 
3611 uint find_shortest_key(TABLE *table, const key_map *usable_keys)
3612 {
3613  uint best= MAX_KEY;
3614  uint usable_clustered_pk= (table->file->primary_key_is_clustered() &&
3615  table->s->primary_key != MAX_KEY &&
3616  usable_keys->is_set(table->s->primary_key)) ?
3617  table->s->primary_key : MAX_KEY;
3618  if (!usable_keys->is_clear_all())
3619  {
3620  uint min_length= (uint) ~0;
3621  for (uint nr=0; nr < table->s->keys ; nr++)
3622  {
3623  if (nr == usable_clustered_pk)
3624  continue;
3625  if (usable_keys->is_set(nr))
3626  {
3627  if (table->key_info[nr].key_length < min_length)
3628  {
3629  min_length=table->key_info[nr].key_length;
3630  best=nr;
3631  }
3632  }
3633  }
3634  }
3635  if (usable_clustered_pk != MAX_KEY)
3636  {
3637  /*
3638  If the primary key is clustered and found shorter key covers all table
3639  fields then primary key scan normally would be faster because amount of
3640  data to scan is the same but PK is clustered.
3641  It's safe to compare key parts with table fields since duplicate key
3642  parts aren't allowed.
3643  */
3644  if (best == MAX_KEY ||
3645  table->key_info[best].user_defined_key_parts >= table->s->fields)
3646  best= usable_clustered_pk;
3647  }
3648  return best;
3649 }
3650 
3667 inline bool
3668 is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
3669  KEY_PART_INFO *ref_key_part_end)
3670 {
3671  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
3672  if (!key_part->field->eq(ref_key_part->field))
3673  return 0;
3674  return 1;
3675 }
3676 
3686 bool
3687 is_ref_or_null_optimized(const JOIN_TAB *tab, uint ref_key)
3688 {
3689  if (tab->keyuse)
3690  {
3691  const Key_use *keyuse= tab->keyuse;
3692  while (keyuse->key != ref_key && keyuse->table == tab->table)
3693  keyuse++;
3694 
3695  const table_map const_tables= tab->join->const_table_map;
3696  while (keyuse->key == ref_key && keyuse->table == tab->table)
3697  {
3698  if (!(keyuse->used_tables & ~const_tables))
3699  {
3700  if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
3701  return true;
3702  }
3703  keyuse++;
3704  }
3705  }
3706  return false;
3707 }
3708 
3721 static uint
3722 test_if_subkey(ORDER *order, JOIN_TAB *tab, uint ref, uint ref_key_parts,
3723  const key_map *usable_keys)
3724 {
3725  uint nr;
3726  uint min_length= (uint) ~0;
3727  uint best= MAX_KEY;
3728  TABLE *table= tab->table;
3729  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
3730  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
3731 
3732  for (nr= 0 ; nr < table->s->keys ; nr++)
3733  {
3734  if (usable_keys->is_set(nr) &&
3735  table->key_info[nr].key_length < min_length &&
3736  table->key_info[nr].user_defined_key_parts >= ref_key_parts &&
3737  is_subkey(table->key_info[nr].key_part, ref_key_part,
3738  ref_key_part_end) &&
3739  !is_ref_or_null_optimized(tab, nr) &&
3740  test_if_order_by_key(order, table, nr))
3741  {
3742  min_length= table->key_info[nr].key_length;
3743  best= nr;
3744  }
3745  }
3746  return best;
3747 }
3748 
3749 
3758 {
3759 #ifndef DBUG_OFF
3760 public:
3765  Plan_change_watchdog(const JOIN_TAB *tab_arg, const bool no_changes_arg)
3766  {
3767  // Only to keep gcc 4.1.2-44 silent about uninitialized variables
3768  quick= NULL;
3769  quick_index= 0;
3770  if (no_changes_arg)
3771  {
3772  tab= tab_arg;
3773  type= tab->type;
3774  if ((select= tab->select))
3775  if ((quick= tab->select->quick))
3776  quick_index= quick->index;
3777  use_quick= tab->use_quick;
3778  ref_key= tab->ref.key;
3779  ref_key_parts= tab->ref.key_parts;
3780  index= tab->index;
3781  }
3782  else
3783  {
3784  tab= NULL;
3785  // Only to keep gcc 4.1.2-44 silent about uninitialized variables
3786  type= JT_UNKNOWN;
3787  select= NULL;
3788  ref_key= ref_key_parts= index= 0;
3789  use_quick= QS_NONE;
3790  }
3791  }
3793  {
3794  if (tab == NULL)
3795  return;
3796  // changes are not allowed, we verify:
3797  DBUG_ASSERT(tab->type == type);
3798  DBUG_ASSERT(tab->select == select);
3799  if (select != NULL)
3800  {
3801  DBUG_ASSERT(tab->select->quick == quick);
3802  if (quick != NULL)
3803  DBUG_ASSERT(tab->select->quick->index == quick_index);
3804  }
3805  DBUG_ASSERT(tab->use_quick == use_quick);
3806  DBUG_ASSERT(tab->ref.key == ref_key);
3807  DBUG_ASSERT(tab->ref.key_parts == ref_key_parts);
3808  DBUG_ASSERT(tab->index == index);
3809  }
3810 private:
3811  const JOIN_TAB *tab;
3812  enum join_type type;
3813  // "Range / index merge" info
3814  const SQL_SELECT *select;
3815  const QUICK_SELECT_I *quick;
3816  uint quick_index;
3817  enum quick_type use_quick;
3818  // "ref access" info
3819  int ref_key;
3820  uint ref_key_parts;
3821  // Other index-related info
3822  uint index;
3823 #else // in non-debug build, empty class
3824 public:
3825  Plan_change_watchdog(const JOIN_TAB *tab_arg, const bool no_changes_arg) {}
3826 #endif
3827 };
3828 
3829 
3863 bool
3864 test_if_skip_sort_order(JOIN_TAB *tab, ORDER *order, ha_rows select_limit,
3865  const bool no_changes, const key_map *map,
3866  const char *clause_type)
3867 {
3868  int ref_key;
3869  uint ref_key_parts;
3870  int order_direction= 0;
3871  uint used_key_parts;
3872  TABLE *table=tab->table;
3873  SQL_SELECT *select=tab->select;
3874  QUICK_SELECT_I *save_quick= select ? select->quick : NULL;
3875  int best_key= -1;
3876  Item *orig_cond;
3877  bool orig_cond_saved= false, set_up_ref_access_to_key= false;
3878  bool can_skip_sorting= false; // used as return value
3879  int changed_key= -1;
3880  DBUG_ENTER("test_if_skip_sort_order");
3881  LINT_INIT(ref_key_parts);
3882  LINT_INIT(orig_cond);
3883 
3884  /* Check that we are always called with first non-const table */
3885  DBUG_ASSERT(tab == tab->join->join_tab + tab->join->const_tables);
3886 
3887  Plan_change_watchdog watchdog(tab, no_changes);
3888 
3889  /* Sorting a single row can always be skipped */
3890  if (tab->type == JT_EQ_REF ||
3891  tab->type == JT_CONST ||
3892  tab->type == JT_SYSTEM)
3893  {
3894  DBUG_RETURN(1);
3895  }
3896 
3897  /*
3898  Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
3899  been taken into account.
3900  */
3901  key_map usable_keys= *map;
3902 
3903  for (ORDER *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
3904  {
3905  Item *item= (*tmp_order->item)->real_item();
3906  if (item->type() != Item::FIELD_ITEM)
3907  {
3908  usable_keys.clear_all();
3909  DBUG_RETURN(0);
3910  }
3911  usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
3912  if (usable_keys.is_clear_all())
3913  DBUG_RETURN(0); // No usable keys
3914  }
3915 
3916  ref_key= -1;
3917  /* Test if constant range in WHERE */
3918  if (tab->ref.key >= 0 && tab->ref.key_parts)
3919  {
3920  if (tab->type == JT_REF_OR_NULL || tab->type == JT_FT)
3921  DBUG_RETURN(0);
3922  ref_key= tab->ref.key;
3923  ref_key_parts= tab->ref.key_parts;
3924  }
3925  else if (select && select->quick) // Range found by opt_range
3926  {
3927  int quick_type= select->quick->get_type();
3928  /*
3929  assume results are not ordered when index merge is used
3930  TODO: sergeyp: Results of all index merge selects actually are ordered
3931  by clustered PK values.
3932  */
3933 
3934  if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
3935  quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
3936  quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
3937  DBUG_RETURN(0);
3938  ref_key= select->quick->index;
3939  ref_key_parts= select->quick->used_key_parts;
3940  }
3941 
3942  /*
3943  If part of the select condition has been pushed we use the
3944  select condition as it was before pushing. The original
3945  select condition is saved so that it can be restored when
3946  exiting this function (if we have not changed index).
3947  */
3948  if (tab->pre_idx_push_cond)
3949  {
3950  orig_cond=
3951  tab->set_jt_and_sel_condition(tab->pre_idx_push_cond, __LINE__);
3952  orig_cond_saved= true;
3953  }
3954 
3955  Opt_trace_context * const trace= &tab->join->thd->opt_trace;
3956  Opt_trace_object trace_wrapper(trace);
3958  trace_skip_sort_order(trace, "reconsidering_access_paths_for_index_ordering");
3959  trace_skip_sort_order.add_alnum("clause", clause_type);
3960 
3961  if (ref_key >= 0)
3962  {
3963  /*
3964  We come here when there is a {ref or or ordered range access} key.
3965  */
3966  if (!usable_keys.is_set(ref_key))
3967  {
3968  /*
3969  We come here when ref_key is not among usable_keys, try to find a
3970  usable prefix key of that key.
3971  */
3972  uint new_ref_key;
3973  /*
3974  If using index only read, only consider other possible index only
3975  keys
3976  */
3977  if (table->covering_keys.is_set(ref_key))
3978  usable_keys.intersect(table->covering_keys);
3979 
3980  if ((new_ref_key= test_if_subkey(order, tab, ref_key, ref_key_parts,
3981  &usable_keys)) < MAX_KEY)
3982  {
3983  /* Found key that can be used to retrieve data in sorted order */
3984  if (tab->ref.key >= 0)
3985  {
3986  /*
3987  We'll use ref access method on key new_ref_key. The actual change
3988  is done further down in this function where we update the plan.
3989  */
3990  set_up_ref_access_to_key= true;
3991  }
3992  else if (!no_changes)
3993  {
3994  /*
3995  The range optimizer constructed QUICK_RANGE for ref_key, and
3996  we want to use instead new_ref_key as the index. We can't
3997  just change the index of the quick select, because this may
3998  result in an incosistent QUICK_SELECT object. Below we
3999  create a new QUICK_SELECT from scratch so that all its
4000  parameres are set correctly by the range optimizer.
4001 
4002  Note that the range optimizer is NOT called if
4003  no_changes==true. This reason is that the range optimizer
4004  cannot find a QUICK that can return ordered result unless
4005  index access (ref or index scan) is also able to do so
4006  (which test_if_order_by_key () will tell).
4007  Admittedly, range access may be much more efficient than
4008  e.g. index scan, but the only thing that matters when
4009  no_change==true is the answer to the question: "Is it
4010  possible to avoid sorting if an index is used to access
4011  this table?". The answer does not depend on the outcome of
4012  the range optimizer.
4013  */
4014  key_map new_ref_key_map; // Force the creation of quick select
4015  new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
4016 
4018  trace_recest(trace, "rows_estimation");
4019  trace_recest.add_utf8_table(tab->table).
4020  add_utf8("index", table->key_info[new_ref_key].name);
4021  select->quick= 0;
4022  if (select->test_quick_select(tab->join->thd,
4023  new_ref_key_map,
4024  0, // empty table_map
4025  (tab->join->select_options &
4026  OPTION_FOUND_ROWS) ?
4027  HA_POS_ERROR :
4028  tab->join->unit->select_limit_cnt,
4029  false, // don't force quick range
4030  order->direction) <= 0)
4031  {
4032  can_skip_sorting= false;
4033  goto fix_ICP;
4034  }
4035  }
4036  ref_key= new_ref_key;
4037  changed_key= new_ref_key;
4038  }
4039  }
4040  /* Check if we get the rows in requested sorted order by using the key */
4041  if (usable_keys.is_set(ref_key) &&
4042  (order_direction= test_if_order_by_key(order,table,ref_key,
4043  &used_key_parts)))
4044  goto check_reverse_order;
4045  }
4046  {
4047  /*
4048  There was no {ref or or ordered range access} key, or it was not
4049  satisfying, neither was any prefix of it. Do a cost-based search on all
4050  keys:
4051  */
4052  uint best_key_parts= 0;
4053  uint saved_best_key_parts= 0;
4054  int best_key_direction= 0;
4055  JOIN *join= tab->join;
4056  ha_rows table_records= table->file->stats.records;
4057 
4058  test_if_cheaper_ordering(tab, order, table, usable_keys,
4059  ref_key, select_limit,
4060  &best_key, &best_key_direction,
4061  &select_limit, &best_key_parts,
4062  &saved_best_key_parts);
4063 
4064  if (best_key < 0)
4065  {
4066  // No usable key has been found
4067  can_skip_sorting= false;
4068  goto fix_ICP;
4069  }
4070 
4071  /*
4072  filesort() and join cache are usually faster than reading in
4073  index order and not using join cache, except in case that chosen
4074  index is clustered primary key.
4075  */
4076  if ((select_limit >= table_records) &&
4077  (tab->type == JT_ALL &&
4078  tab->join->primary_tables > tab->join->const_tables + 1) &&
4079  ((unsigned) best_key != table->s->primary_key ||
4080  !table->file->primary_key_is_clustered()))
4081  {
4082  can_skip_sorting= false;
4083  goto fix_ICP;
4084  }
4085 
4086  if (select &&
4087  table->quick_keys.is_set(best_key) &&
4088  !tab->quick_order_tested.is_set(best_key) &&
4089  best_key != ref_key)
4090  {
4091  tab->quick_order_tested.set_bit(best_key);
4093  trace_recest(trace, "rows_estimation");
4094  trace_recest.add_utf8_table(tab->table).
4095  add_utf8("index", table->key_info[best_key].name);
4096 
4097  key_map map; // Force the creation of quick select
4098  map.set_bit(best_key); // only best_key.
4099  select->quick= 0;
4100  select->test_quick_select(join->thd,
4101  map,
4102  0, // empty table_map
4103  join->select_options & OPTION_FOUND_ROWS ?
4104  HA_POS_ERROR :
4105  join->unit->select_limit_cnt,
4106  true, // force quick range
4107  order->direction);
4108  }
4109  order_direction= best_key_direction;
4110  /*
4111  saved_best_key_parts is actual number of used keyparts found by the
4112  test_if_order_by_key function. It could differ from keyinfo->key_parts,
4113  thus we have to restore it in case of desc order as it affects
4114  QUICK_SELECT_DESC behaviour.
4115  */
4116  used_key_parts= (order_direction == -1) ?
4117  saved_best_key_parts : best_key_parts;
4118  changed_key= best_key;
4119  // We will use index scan or range scan:
4120  set_up_ref_access_to_key= false;
4121  }
4122 
4123 check_reverse_order:
4124  DBUG_ASSERT(order_direction != 0);
4125 
4126  if (order_direction == -1) // If ORDER BY ... DESC
4127  {
4128  if (select && select->quick)
4129  {
4130  /*
4131  Don't reverse the sort order, if it's already done.
4132  (In some cases test_if_order_by_key() can be called multiple times
4133  */
4134  if (select->quick->reverse_sorted())
4135  {
4136  can_skip_sorting= true;
4137  goto fix_ICP;
4138  }
4139 
4140  if (select->quick->reverse_sort_possible())
4141  can_skip_sorting= true;
4142  else
4143  {
4144  can_skip_sorting= false;
4145  goto fix_ICP;
4146  }
4147 
4148  /*
4149  test_quick_select() should not create a quick that cannot do
4150  reverse ordering
4151  */
4152  DBUG_ASSERT((select->quick == save_quick) || can_skip_sorting);
4153  }
4154  else
4155  {
4156  // Other index access (ref or scan) poses no problem
4157  can_skip_sorting= true;
4158  }
4159  }
4160  else
4161  {
4162  // ORDER BY ASC poses no problem
4163  can_skip_sorting= true;
4164  }
4165 
4166  DBUG_ASSERT(can_skip_sorting);
4167 
4168  /*
4169  Update query plan with access pattern for doing
4170  ordered access according to what we have decided
4171  above.
4172  */
4173  if (!no_changes) // We are allowed to update QEP
4174  {
4175  if (set_up_ref_access_to_key)
4176  {
4177  /*
4178  We'll use ref access method on key changed_key. In general case
4179  the index search tuple for changed_ref_key will be different (e.g.
4180  when one index is defined as (part1, part2, ...) and another as
4181  (part1, part2(N), ...) and the WHERE clause contains
4182  "part1 = const1 AND part2=const2".
4183  So we build tab->ref from scratch here.
4184  */
4185  Key_use *keyuse= tab->keyuse;
4186  while (keyuse->key != (uint)changed_key && keyuse->table == tab->table)
4187  keyuse++;
4188 
4189  if (create_ref_for_key(tab->join, tab, keyuse, tab->prefix_tables()))
4190  {
4191  can_skip_sorting= false;
4192  goto fix_ICP;
4193  }
4194 
4195  DBUG_ASSERT(tab->type != JT_REF_OR_NULL && tab->type != JT_FT);
4196  }
4197  else if (best_key >= 0)
4198  {
4199  bool quick_created=
4200  (select && select->quick && select->quick!=save_quick);
4201 
4202  /*
4203  If ref_key used index tree reading only ('Using index' in EXPLAIN),
4204  and best_key doesn't, then revert the decision.
4205  */
4206  if(!table->covering_keys.is_set(best_key))
4207  table->set_keyread(false);
4208  if (!quick_created)
4209  {
4210  if (select) // Throw any existing quick select
4211  select->quick= 0; // Cleanup either reset to save_quick,
4212  // or 'delete save_quick'
4213  tab->index= best_key;
4214  tab->read_first_record= order_direction > 0 ?
4215  join_read_first:join_read_last;
4216  tab->type=JT_INDEX_SCAN; // Read with index_first(), index_next()
4217 
4218  table->file->ha_index_or_rnd_end();
4219  if (tab->join->select_options & SELECT_DESCRIBE)
4220  {
4221  /*
4222  @todo this neutralizes add_ref_to_table_cond(); as a result
4223  EXPLAIN shows no "using where" though real SELECT has one.
4224  */
4225  tab->ref.key= -1;
4226  tab->ref.key_parts= 0;
4227  if (select_limit < table->file->stats.records)
4228  tab->limit= select_limit;
4229  }
4230  }
4231  else if (tab->type != JT_ALL)
4232  {
4233  /*
4234  We're about to use a quick access to the table.
4235  We need to change the access method so as the quick access
4236  method is actually used.
4237  */
4238  DBUG_ASSERT(tab->select->quick);
4239  DBUG_ASSERT(tab->select->quick->index==(uint)best_key);
4240  tab->type=JT_ALL;
4241  tab->use_quick=QS_RANGE;
4242  tab->ref.key= -1;
4243  tab->ref.key_parts=0; // Don't use ref key.
4245  if (tab->is_using_loose_index_scan())
4246  tab->join->tmp_table_param.precomputed_group_by= TRUE;
4247  /*
4248  TODO: update the number of records in tab->position
4249  */
4250  }
4251  } // best_key >= 0
4252 
4253  if (order_direction == -1) // If ORDER BY ... DESC
4254  {
4255  if (select && select->quick)
4256  {
4257  /* ORDER BY range_key DESC */
4258  QUICK_SELECT_I *tmp= select->quick->make_reverse(used_key_parts);
4259  if (!tmp)
4260  {
4261  tab->limit= 0;
4262  can_skip_sorting= false; // Reverse sort failed -> filesort
4263  goto fix_ICP;
4264  }
4265  if (select->quick == save_quick)
4266  save_quick= 0; // Because set_quick(tmp) frees it
4267  select->set_quick(tmp);
4268  }
4269  else if (tab->type != JT_INDEX_SCAN && tab->type != JT_REF_OR_NULL &&
4270  tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
4271  {
4272  /*
4273  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
4274 
4275  Use a traversal function that starts by reading the last row
4276  with key part (A) and then traverse the index backwards.
4277  */
4279  tab->read_record.read_record= join_read_prev_same;
4280  tab->read_record.unlock_row= rr_unlock_row;
4281 
4282  /*
4283  The current implementation of join_read_prev_same() does not
4284  work well in combination with ICP and can lead to increased
4285  execution time. Setting changed_key to the current key
4286  (based on that we change the access order for the key) will
4287  ensure that a pushed index condition will be cancelled.
4288  */
4289  changed_key= tab->ref.key;
4290  }
4291  }
4292  else if (select && select->quick)
4293  select->quick->need_sorted_output();
4294  } // QEP has been modified
4295 
4296 fix_ICP:
4297  /*
4298  Cleanup:
4299  We may have both a 'select->quick' and 'save_quick' (original)
4300  at this point. Delete the one that we won't use.
4301  */
4302  if (can_skip_sorting && !no_changes)
4303  {
4304  // Keep current (ordered) select->quick
4305  if (select && save_quick != select->quick)
4306  delete save_quick;
4307  }
4308  else
4309  {
4310  // Restore original save_quick
4311  if (select && select->quick != save_quick)
4312  select->set_quick(save_quick);
4313  }
4314 
4316  trace_change_index(trace, "index_order_summary");
4317  trace_change_index.add_utf8_table(tab->table)
4318  .add("index_provides_order", can_skip_sorting)
4319  .add_alnum("order_direction", order_direction == 1 ? "asc" :
4320  ((order_direction == -1) ? "desc" :
4321  "undefined"));
4322 
4323  if (changed_key >= 0)
4324  {
4325  bool cancelled_ICP= false;
4326  // switching to another index, makes pushed index condition obsolete
4327  if (!no_changes && table->file->pushed_idx_cond)
4328  {
4329  table->file->cancel_pushed_idx_cond();
4330  // and thus tab's m_condition must be how it was before ICP
4331  orig_cond_saved= false;
4332  cancelled_ICP= true;
4333  }
4334  if (unlikely(trace->is_started()))
4335  {
4336  if (cancelled_ICP)
4337  trace_change_index.add("disabled_pushed_condition_on_old_index", true);
4338  trace_change_index.add_utf8("index", table->key_info[changed_key].name);
4339  trace_change_index.add("plan_changed", !no_changes);
4340  if (!no_changes)
4341  {
4342  const char *new_type= tab->type == JT_INDEX_SCAN ? "index_scan" :
4343  (tab->select && tab->select->quick) ?
4344  "range" : join_type_str[tab->type];
4345  trace_change_index.add_alnum("access_type", new_type);
4346  }
4347  }
4348  }
4349  else if (unlikely(trace->is_started()))
4350  {
4351  trace_change_index.add_utf8("index",
4352  ref_key >= 0 ?
4353  table->key_info[ref_key].name : "unknown");
4354  trace_change_index.add("plan_changed", false);
4355  }
4356  if (orig_cond_saved)
4357  {
4358  // ICP set up prior to the call, is still valid:
4359  tab->set_jt_and_sel_condition(orig_cond, __LINE__);
4360  }
4361  DBUG_RETURN(can_skip_sorting);
4362 }
4363 
4364 
4365 
4370 void
4371 count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param,
4372  List<Item> &fields, bool reset_with_sum_func)
4373 {
4374  List_iterator<Item> li(fields);
4375  Item *field;
4376 
4377  param->field_count= 0;
4378  param->sum_func_count= 0;
4379  param->func_count= 0;
4380  param->hidden_field_count= 0;
4381  param->outer_sum_func_count= 0;
4382  param->quick_group=1;
4383  while ((field=li++))
4384  {
4385  Item::Type real_type= field->real_item()->type();
4386  if (real_type == Item::FIELD_ITEM)
4387  param->field_count++;
4388  else if (real_type == Item::SUM_FUNC_ITEM)
4389  {
4390  if (! field->const_item())
4391  {
4392  Item_sum *sum_item=(Item_sum*) field->real_item();
4393  if (!sum_item->depended_from() ||
4394  sum_item->depended_from() == select_lex)
4395  {
4396  if (!sum_item->quick_group)
4397  param->quick_group=0; // UDF SUM function
4398  param->sum_func_count++;
4399 
4400  for (uint i=0 ; i < sum_item->get_arg_count() ; i++)
4401  {
4402  if (sum_item->get_arg(i)->real_item()->type() == Item::FIELD_ITEM)
4403  param->field_count++;
4404  else
4405  param->func_count++;
4406  }
4407  }
4408  param->func_count++;
4409  }
4410  }
4411  else
4412  {
4413  param->func_count++;
4414  if (reset_with_sum_func)
4415  field->with_sum_func=0;
4416  if (field->with_sum_func)
4417  param->outer_sum_func_count++;
4418  }
4419  }
4420 }
4421 
4422 
4430 bool
4432 {
4433  for (; a && b; a=a->next,b=b->next)
4434  {
4435  if ((*a->item)->eq(*b->item,1))
4436  a->direction= b->direction;
4437  else
4438  return 0;
4439  }
4440  return test(!b);
4441 }
4442 
4447 void
4449 {
4450  uint key_length=0, parts=0, null_parts=0;
4451 
4452  if (group)
4453  join->group= 1;
4454  for (; group ; group=group->next)
4455  {
4456  Item *group_item= *group->item;
4457  Field *field= group_item->get_tmp_table_field();
4458  if (field)
4459  {
4460  enum_field_types type;
4461  if ((type= field->type()) == MYSQL_TYPE_BLOB)
4462  key_length+=MAX_BLOB_WIDTH; // Can't be used as a key
4463  else if (type == MYSQL_TYPE_VARCHAR || type == MYSQL_TYPE_VAR_STRING)
4464  key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
4465  else if (type == MYSQL_TYPE_BIT)
4466  {
4467  /* Bit is usually stored as a longlong key for group fields */
4468  key_length+= 8; // Big enough
4469  }
4470  else
4471  key_length+= field->pack_length();
4472  }
4473  else
4474  {
4475  switch (group_item->result_type()) {
4476  case REAL_RESULT:
4477  key_length+= sizeof(double);
4478  break;
4479  case INT_RESULT:
4480  key_length+= sizeof(longlong);
4481  break;
4482  case DECIMAL_RESULT:
4483  key_length+= my_decimal_get_binary_size(group_item->max_length -
4484  (group_item->decimals ? 1 : 0),
4485  group_item->decimals);
4486  break;
4487  case STRING_RESULT:
4488  {
4489  /*
4490  As items represented as DATE/TIME fields in the group buffer
4491  have STRING_RESULT result type, we increase the length
4492  by 8 as maximum pack length of such fields.
4493  */
4494  if (group_item->is_temporal())
4495  {
4496  key_length+= 8;
4497  }
4498  else if (group_item->field_type() == MYSQL_TYPE_BLOB)
4499  key_length+= MAX_BLOB_WIDTH; // Can't be used as a key
4500  else
4501  {
4502  /*
4503  Group strings are taken as varstrings and require an length field.
4504  A field is not yet created by create_tmp_field()
4505  and the sizes should match up.
4506  */
4507  key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
4508  }
4509  break;
4510  }
4511  default:
4512  /* This case should never be choosen */
4513  DBUG_ASSERT(0);
4514  my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
4515  }
4516  }
4517  parts++;
4518  if (group_item->maybe_null)
4519  null_parts++;
4520  }
4521  join->tmp_table_param.group_length=key_length+null_parts;
4522  join->tmp_table_param.group_parts=parts;
4523  join->tmp_table_param.group_null_parts=null_parts;
4524 }
4525 
4526 
4527 
4539 {
4540  uint func_count, group_parts;
4541  DBUG_ENTER("alloc_func_list");
4542 
4543  func_count= tmp_table_param.sum_func_count;
4544  /*
4545  If we are using rollup, we need a copy of the summary functions for
4546  each level
4547  */
4548  if (rollup.state != ROLLUP::STATE_NONE)
4549  func_count*= (send_group_parts+1);
4550 
4551  group_parts= send_group_parts;
4552  /*
4553  If distinct, reserve memory for possible
4554  disctinct->group_by optimization
4555  */
4556  if (select_distinct)
4557  {
4558  group_parts+= fields_list.elements;
4559  /*
4560  If the ORDER clause is specified then it's possible that
4561  it also will be optimized, so reserve space for it too
4562  */
4563  if (order)
4564  {
4565  ORDER *ord;
4566  for (ord= order; ord; ord= ord->next)
4567  group_parts++;
4568  }
4569  }
4570 
4571  /* This must use calloc() as rollup_make_fields depends on this */
4572  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
4573  sizeof(Item_sum***) * (group_parts+1));
4574  sum_funcs_end= (Item_sum***) (sum_funcs + func_count + 1);
4575  DBUG_RETURN(sum_funcs == 0);
4576 }
4577 
4578 
4594  List<Item> &send_result_set_metadata,
4595  bool before_group_by, bool recompute)
4596 {
4597  List_iterator_fast<Item> it(field_list);
4598  Item_sum **func;
4599  Item *item;
4600  DBUG_ENTER("make_sum_func_list");
4601 
4602  if (*sum_funcs && !recompute)
4603  DBUG_RETURN(FALSE); /* We have already initialized sum_funcs. */
4604 
4605  func= sum_funcs;
4606  while ((item=it++))
4607  {
4608  if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
4609  (!((Item_sum*) item)->depended_from() ||
4610  ((Item_sum *)item)->depended_from() == select_lex))
4611  *func++= (Item_sum*) item;
4612  }
4613  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
4614  {
4615  rollup.state= ROLLUP::STATE_READY;
4616  if (rollup_make_fields(field_list, send_result_set_metadata, &func))
4617  DBUG_RETURN(TRUE); // Should never happen
4618  }
4619  else if (rollup.state == ROLLUP::STATE_NONE)
4620  {
4621  for (uint i=0 ; i <= send_group_parts ;i++)
4622  sum_funcs_end[i]= func;
4623  }
4624  else if (rollup.state == ROLLUP::STATE_READY)
4625  DBUG_RETURN(FALSE); // Don't put end marker
4626  *func=0; // End marker
4627  DBUG_RETURN(FALSE);
4628 }
4629 
4630 
4638 void free_underlaid_joins(THD *thd, SELECT_LEX *select)
4639 {
4640  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
4641  unit;
4642  unit= unit->next_unit())
4643  unit->cleanup();
4644 }
4645 
4646 /****************************************************************************
4647  ROLLUP handling
4648 ****************************************************************************/
4649 
4672 {
4673  ORDER *group_tmp;
4674  Item *item;
4676 
4677  for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
4678  {
4679  if (!(*group_tmp->item)->const_item())
4680  continue;
4681  while ((item= it++))
4682  {
4683  if (*group_tmp->item == item)
4684  {
4685  Item* new_item= new Item_func_rollup_const(item);
4686  if (!new_item)
4687  return 1;
4688  new_item->fix_fields(thd, (Item **) 0);
4689  thd->change_item_tree(it.ref(), new_item);
4690  for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
4691  {
4692  if (*tmp->item == item)
4693  thd->change_item_tree(tmp->item, new_item);
4694  }
4695  break;
4696  }
4697  }
4698  it.rewind();
4699  }
4700  return 0;
4701 }
4702 
4703 
4720 bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
4721  Item_sum ***func)
4722 {
4723  List_iterator_fast<Item> it(fields_arg);
4724  Item *first_field= sel_fields.head();
4725  uint level;
4726 
4727  /*
4728  Create field lists for the different levels
4729 
4730  The idea here is to have a separate field list for each rollup level to
4731  avoid all runtime checks of which columns should be NULL.
4732 
4733  The list is stored in reverse order to get sum function in such an order
4734  in func that it makes it easy to reset them with init_sum_functions()
4735 
4736  Assuming: SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
4737 
4738  rollup.fields[0] will contain list where a,b,c is NULL
4739  rollup.fields[1] will contain list where b,c is NULL
4740  ...
4741  rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
4742  ...
4743  sum_funcs_end[0] points to all sum functions
4744  sum_funcs_end[1] points to all sum functions, except grand totals
4745  ...
4746  */
4747 
4748  for (level=0 ; level < send_group_parts ; level++)
4749  {
4750  uint i;
4751  uint pos= send_group_parts - level -1;
4752  bool real_fields= 0;
4753  Item *item;
4754  List_iterator<Item> new_it(rollup.fields[pos]);
4755  Ref_ptr_array ref_array_start= rollup.ref_pointer_arrays[pos];
4756  ORDER *start_group;
4757 
4758  /* Point to first hidden field */
4759  uint ref_array_ix= fields_arg.elements-1;
4760 
4761  /* Remember where the sum functions ends for the previous level */
4762  sum_funcs_end[pos+1]= *func;
4763 
4764  /* Find the start of the group for this level */
4765  for (i= 0, start_group= group_list ;
4766  i++ < pos ;
4767  start_group= start_group->next)
4768  ;
4769 
4770  it.rewind();
4771  while ((item= it++))
4772  {
4773  if (item == first_field)
4774  {
4775  real_fields= 1; // End of hidden fields
4776  ref_array_ix= 0;
4777  }
4778 
4779  if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
4780  (!((Item_sum*) item)->depended_from() ||
4781  ((Item_sum *)item)->depended_from() == select_lex))
4782 
4783  {
4784  /*
4785  This is a top level summary function that must be replaced with
4786  a sum function that is reset for this level.
4787 
4788  NOTE: This code creates an object which is not that nice in a
4789  sub select. Fortunately it's not common to have rollup in
4790  sub selects.
4791  */
4792  item= item->copy_or_same(thd);
4793  ((Item_sum*) item)->make_unique();
4794  *(*func)= (Item_sum*) item;
4795  (*func)++;
4796  }
4797  else
4798  {
4799  /* Check if this is something that is part of this group by */
4800  ORDER *group_tmp;
4801  for (group_tmp= start_group, i= pos ;
4802  group_tmp ; group_tmp= group_tmp->next, i++)
4803  {
4804  if (*group_tmp->item == item)
4805  {
4806  /*
4807  This is an element that is used by the GROUP BY and should be
4808  set to NULL in this level
4809  */
4810  Item_null_result *null_item=
4811  new (thd->mem_root) Item_null_result(item->field_type(),
4812  item->result_type());
4813  if (!null_item)
4814  return 1;
4815  item->maybe_null= 1; // Value will be null sometimes
4816  null_item->result_field= item->get_tmp_table_field();
4817  item= null_item;
4818  break;
4819  }
4820  }
4821  }
4822  ref_array_start[ref_array_ix]= item;
4823  if (real_fields)
4824  {
4825  (void) new_it++; // Point to next item
4826  new_it.replace(item); // Replace previous
4827  ref_array_ix++;
4828  }
4829  else
4830  ref_array_ix--;
4831  }
4832  }
4833  sum_funcs_end[0]= *func; // Point to last function
4834  return 0;
4835 }
4836 
4837 
4844 {
4845  /*
4846  must clear only the non-const tables, as const tables
4847  are not re-calculated.
4848  */
4849  for (uint tableno= const_tables; tableno < primary_tables; tableno++)
4850  mark_as_null_row(join_tab[tableno].table); // All fields are NULL
4851 
4852  copy_fields(&tmp_table_param);
4853 
4854  if (sum_funcs)
4855  {
4856  Item_sum *func, **func_ptr= sum_funcs;
4857  while ((func= *(func_ptr++)))
4858  func->clear();
4859  }
4860 }
4861 
4862 
4874 bool JOIN::change_result(select_result *res)
4875 {
4876  DBUG_ENTER("JOIN::change_result");
4877  result= res;
4878  if (result->prepare(fields_list, select_lex->master_unit()) ||
4879  result->prepare2())
4880  {
4881  DBUG_RETURN(TRUE);
4882  }
4883  DBUG_RETURN(FALSE);
4884 }
4885 
4886 
4914 bool JOIN::make_tmp_tables_info()
4915 {
4916  List<Item> *curr_all_fields= &all_fields;
4917  List<Item> *curr_fields_list= &fields_list;
4918  bool materialize_join= false;
4919  uint curr_tmp_table= const_tables;
4920  TABLE *exec_tmp_table= NULL;
4921  DBUG_ENTER("JOIN::make_tmp_tables_info");
4923 
4924  const bool has_group_by= this->group;
4925  /*
4926  Setup last table to provide fields and all_fields lists to the next
4927  node in the plan.
4928  */
4929  if (join_tab)
4930  {
4931  join_tab[primary_tables - 1].fields= &fields_list;
4932  join_tab[primary_tables - 1].all_fields= &all_fields;
4933  }
4934  /*
4935  The loose index scan access method guarantees that all grouping or
4936  duplicate row elimination (for distinct) is already performed
4937  during data retrieval, and that all MIN/MAX functions are already
4938  computed for each group. Thus all MIN/MAX functions should be
4939  treated as regular functions, and there is no need to perform
4940  grouping in the main execution loop.
4941  Notice that currently loose index scan is applicable only for
4942  single table queries, thus it is sufficient to test only the first
4943  join_tab element of the plan for its access method.
4944  */
4945  if (join_tab && join_tab->is_using_loose_index_scan())
4946  tmp_table_param.precomputed_group_by=
4947  !join_tab->is_using_agg_loose_index_scan();
4948 
4949  /* Create a tmp table if distinct or if the sort is too complicated */
4950  if (need_tmp)
4951  {
4952  curr_tmp_table= primary_tables;
4953  tmp_tables++;
4954  if (plan_is_const())
4955  first_select= sub_select_op;
4956 
4957  /*
4958  Create temporary table on first execution of this join.
4959  (Will be reused if this is a subquery that is executed several times.)
4960  */
4962 
4963  ORDER_with_src tmp_group;
4964  if (!simple_group && !(test_flags & TEST_NO_KEY_GROUP))
4965  tmp_group= group_list;
4966 
4967  tmp_table_param.hidden_field_count=
4968  all_fields.elements - fields_list.elements;
4969 
4970  if (create_intermediate_table(&join_tab[curr_tmp_table],
4971  &all_fields, tmp_group,
4972  group_list && simple_group))
4973  DBUG_RETURN(true);
4974  exec_tmp_table= join_tab[curr_tmp_table].table;
4975 
4976  if (exec_tmp_table->distinct)
4977  optimize_distinct();
4978 
4979  /*
4980  If there is no sorting or grouping, 'use_order'
4981  index result should not have been requested.
4982  Exception: LooseScan strategy for semijoin requires
4983  sorted access even if final result is not to be sorted.
4984  */
4985  DBUG_ASSERT(
4986  !(ordered_index_usage == ordered_index_void &&
4987  !plan_is_const() &&
4988  join_tab[const_tables].position->sj_strategy != SJ_OPT_LOOSE_SCAN &&
4989  join_tab[const_tables].use_order()));
4990 
4991  /*
4992  We don't have to store rows in temp table that doesn't match HAVING if:
4993  - we are sorting the table and writing complete group rows to the
4994  temp table.
4995  - We are using DISTINCT without resolving the distinct as a GROUP BY
4996  on all columns.
4997 
4998  If having is not handled here, it will be checked before the row
4999  is sent to the client.
5000  */
5001  if (having &&
5002  (sort_and_group || (exec_tmp_table->distinct && !group_list)))
5003  {
5004  // Attach HAVING to tmp table's condition
5005  join_tab[curr_tmp_table].having= having;
5006  having= NULL; // Already done
5007  }
5008 
5009  /* Change sum_fields reference to calculated fields in tmp_table */
5010  DBUG_ASSERT(items1.is_null());
5011  items1= ref_ptr_array_slice(2);
5012  if (sort_and_group || join_tab[curr_tmp_table].table->group ||
5013  tmp_table_param.precomputed_group_by)
5014  {
5015  if (change_to_use_tmp_fields(thd, items1,
5017  fields_list.elements, all_fields))
5018  DBUG_RETURN(true);
5019  }
5020  else
5021  {
5022  if (change_refs_to_tmp_fields(thd, items1,
5024  fields_list.elements, all_fields))
5025  DBUG_RETURN(true);
5026  }
5027  curr_all_fields= &tmp_all_fields1;
5028  curr_fields_list= &tmp_fields_list1;
5029  // Need to set them now for correct group_fields setup, reset at the end.
5030  set_items_ref_array(items1);
5031  join_tab[curr_tmp_table].ref_array= &items1;
5032  join_tab[curr_tmp_table].all_fields= &tmp_all_fields1;
5033  join_tab[curr_tmp_table].fields= &tmp_fields_list1;
5034  setup_tmptable_write_func(&join_tab[curr_tmp_table]);
5035 
5036  tmp_table_param.func_count= 0;
5037  tmp_table_param.field_count+= tmp_table_param.func_count;
5038  if (sort_and_group || join_tab[curr_tmp_table].table->group)
5039  {
5040  tmp_table_param.field_count+= tmp_table_param.sum_func_count;
5041  tmp_table_param.sum_func_count= 0;
5042  }
5043 
5044  if (exec_tmp_table->group)
5045  { // Already grouped
5046  if (!order && !no_order && !skip_sort_order)
5047  order= group_list; /* order by group */
5048  group_list= NULL;
5049  }
5050  /*
5051  If we have different sort & group then we must sort the data by group
5052  and copy it to another tmp table
5053  This code is also used if we are using distinct something
5054  we haven't been able to store in the temporary table yet
5055  like SEC_TO_TIME(SUM(...)).
5056  */
5057 
5058  if ((group_list &&
5059  (!test_if_subpart(group_list, order) || select_distinct)) ||
5060  (select_distinct && tmp_table_param.using_outer_summary_function))
5061  { /* Must copy to another table */
5062  DBUG_PRINT("info",("Creating group table"));
5063 
5064  calc_group_buffer(this, group_list);
5065  count_field_types(select_lex, &tmp_table_param, tmp_all_fields1,
5066  select_distinct && !group_list);
5067  tmp_table_param.hidden_field_count=
5068  tmp_all_fields1.elements - tmp_fields_list1.elements;
5069 
5070  if (!exec_tmp_table->group && !exec_tmp_table->distinct)
5071  {
5072  // 1st tmp table were materializing join result
5073  materialize_join= true;
5074  explain_flags.set(ESC_BUFFER_RESULT, ESP_USING_TMPTABLE);
5075  }
5076  curr_tmp_table++;
5077  tmp_tables++;
5078 
5079  /* group data to new table */
5080  /*
5081  If the access method is loose index scan then all MIN/MAX
5082  functions are precomputed, and should be treated as regular
5083  functions. See extended comment above.
5084  */
5085  if (join_tab->is_using_loose_index_scan())
5086  tmp_table_param.precomputed_group_by= TRUE;
5087 
5088  tmp_table_param.hidden_field_count=
5089  curr_all_fields->elements - curr_fields_list->elements;
5090  ORDER_with_src dummy= NULL; //TODO can use table->group here also
5091 
5092  if (create_intermediate_table(&join_tab[curr_tmp_table],
5093  curr_all_fields, dummy, true))
5094  DBUG_RETURN(true);
5095 
5096  if (group_list)
5097  {
5098  explain_flags.set(group_list.src, ESP_USING_TMPTABLE);
5099  if (!plan_is_const()) // No need to sort a single row
5100  {
5101  JOIN_TAB *sort_tab= &join_tab[curr_tmp_table - 1];
5102  if (add_sorting_to_table(sort_tab, &group_list))
5103  DBUG_RETURN(true);
5104  }
5105 
5106  if (make_group_fields(this, this))
5107  DBUG_RETURN(true);
5108  }
5109 
5110  /*
5111  If there is no sorting or grouping, 'use_order'
5112  index result should not have been requested.
5113  */
5114  DBUG_ASSERT(!(ordered_index_usage == ordered_index_void &&
5115  !plan_is_const() &&
5116  join_tab[const_tables].use_order()));
5117 
5118  // Setup sum funcs only when necessary, otherwise we might break info
5119  // for the first table
5120  if (group_list || tmp_table_param.sum_func_count)
5121  {
5122  if (make_sum_func_list(*curr_all_fields, *curr_fields_list, true, true))
5123  DBUG_RETURN(true);
5124  if (prepare_sum_aggregators(sum_funcs,
5125  !join_tab->is_using_agg_loose_index_scan()))
5126  DBUG_RETURN(true);
5127  group_list= NULL;
5128  if (setup_sum_funcs(thd, sum_funcs))
5129  DBUG_RETURN(true);
5130  }
5131  // No sum funcs anymore
5132  DBUG_ASSERT(items2.is_null());
5133 
5134  items2= ref_ptr_array_slice(3);
5135  if (change_to_use_tmp_fields(thd, items2,
5136  tmp_fields_list2, tmp_all_fields2,
5137  fields_list.elements, tmp_all_fields1))
5138  DBUG_RETURN(true);
5139 
5140  curr_fields_list= &tmp_fields_list2;
5141  curr_all_fields= &tmp_all_fields2;
5142  set_items_ref_array(items2);
5143  join_tab[curr_tmp_table].ref_array= &items2;
5144  join_tab[curr_tmp_table].all_fields= &tmp_all_fields2;
5145  join_tab[curr_tmp_table].fields= &tmp_fields_list2;
5146  setup_tmptable_write_func(&join_tab[curr_tmp_table]);
5147 
5148  tmp_table_param.field_count+= tmp_table_param.sum_func_count;
5149  tmp_table_param.sum_func_count= 0;
5150  }
5151  if (join_tab[curr_tmp_table].table->distinct)
5152  select_distinct= false; /* Each row is unique */
5153 
5154  if (select_distinct && !group_list)
5155  {
5156  if (having)
5157  {
5158  join_tab[curr_tmp_table].having= having;
5159  having->update_used_tables();
5160  }
5161  join_tab[curr_tmp_table].distinct= true;
5162  explain_flags.set(ESC_DISTINCT, ESP_DUPS_REMOVAL);
5163  having= NULL;
5164  select_distinct= false;
5165  }
5166  /* Clean tmp_table_param for the next tmp table. */
5167  tmp_table_param.field_count= tmp_table_param.sum_func_count=
5168  tmp_table_param.func_count= 0;
5169 
5170  tmp_table_param.copy_field= tmp_table_param.copy_field_end=0;
5171  first_record= sort_and_group=0;
5172 
5173  if (!group_optimized_away)
5174  {
5175  group= false;
5176  }
5177  else
5178  {
5179  /*
5180  If grouping has been optimized away, a temporary table is
5181  normally not needed unless we're explicitly requested to create
5182  one (e.g. due to a SQL_BUFFER_RESULT hint or INSERT ... SELECT).
5183 
5184  In this case (grouping was optimized away), temp_table was
5185  created without a grouping expression and JOIN::exec() will not
5186  perform the necessary grouping (by the use of end_send_group()
5187  or end_write_group()) if JOIN::group is set to false.
5188  */
5189  // the temporary table was explicitly requested
5190  DBUG_ASSERT(test(select_options & OPTION_BUFFER_RESULT));
5191  // the temporary table does not have a grouping expression
5192  DBUG_ASSERT(!join_tab[curr_tmp_table].table->group);
5193  }
5194  calc_group_buffer(this, group_list);
5195  count_field_types(select_lex, &tmp_table_param, *curr_all_fields, false);
5196  }
5197 
5198  if (group || implicit_grouping || tmp_table_param.sum_func_count)
5199  {
5200  if (make_group_fields(this, this))
5201  DBUG_RETURN(true);
5202 
5203  DBUG_ASSERT(items3.is_null());
5204 
5205  if (items0.is_null())
5207  items3= ref_ptr_array_slice(4);
5208  setup_copy_fields(thd, &tmp_table_param,
5209  items3, tmp_fields_list3, tmp_all_fields3,
5210  curr_fields_list->elements, *curr_all_fields);
5211 
5212  curr_fields_list= &tmp_fields_list3;
5213  curr_all_fields= &tmp_all_fields3;
5214  set_items_ref_array(items3);
5215  if (join_tab)
5216  {
5217  // Set grouped fields on the last table
5218  join_tab[primary_tables + tmp_tables - 1].ref_array= &items3;
5219  join_tab[primary_tables + tmp_tables - 1].all_fields= &tmp_all_fields3;
5220  join_tab[primary_tables + tmp_tables - 1].fields= &tmp_fields_list3;
5221  }
5222  if (make_sum_func_list(*curr_all_fields, *curr_fields_list, true, true))
5223  DBUG_RETURN(true);
5224  if (prepare_sum_aggregators(sum_funcs,
5225  !join_tab ||
5226  !join_tab-> is_using_agg_loose_index_scan()))
5227  DBUG_RETURN(true);
5228  if (setup_sum_funcs(thd, sum_funcs) || thd->is_fatal_error)
5229  DBUG_RETURN(true);
5230  }
5231  if (group_list || order)
5232  {
5233  DBUG_PRINT("info",("Sorting for send_result_set_metadata"));
5234  THD_STAGE_INFO(thd, stage_sorting_result);
5235  /* If we have already done the group, add HAVING to sorted table */
5236  if (having && !group_list && !sort_and_group)
5237  {
5238  // Some tables may have been const
5239  having->update_used_tables();
5240  JOIN_TAB *curr_table= &join_tab[curr_tmp_table];
5241  table_map used_tables= (const_table_map | curr_table->table->map);
5242 
5243  Item* sort_table_cond= make_cond_for_table(having, used_tables,
5244  (table_map) 0, false);
5245  if (sort_table_cond)
5246  {
5247  if (!curr_table->select)
5248  if (!(curr_table->select= new SQL_SELECT))
5249  DBUG_RETURN(true);
5250  if (!curr_table->select->cond)
5251  curr_table->select->cond= sort_table_cond;
5252  else
5253  {
5254  if (!(curr_table->select->cond=
5255  new Item_cond_and(curr_table->select->cond,
5256  sort_table_cond)))
5257  DBUG_RETURN(true);
5258  curr_table->select->cond->fix_fields(thd, 0);
5259  }
5260  curr_table->set_condition(curr_table->select->cond, __LINE__);
5261  curr_table->condition()->top_level_item();
5262  DBUG_EXECUTE("where",print_where(curr_table->select->cond,
5263  "select and having",
5264  QT_ORDINARY););
5265 
5266  having= make_cond_for_table(having, ~ (table_map) 0,
5267  ~used_tables, false);
5268  DBUG_EXECUTE("where",
5269  print_where(having, "having after sort", QT_ORDINARY););
5270  }
5271  }
5272 
5273  if (group)
5274  m_select_limit= HA_POS_ERROR;
5275  else if (!need_tmp)
5276  {
5277  /*
5278  We can abort sorting after thd->select_limit rows if there are no
5279  filter conditions for any tables after the sorted one.
5280  Filter conditions come in several forms:
5281  1. as a condition item attached to the join_tab, or
5282  2. as a keyuse attached to the join_tab (ref access).
5283  */
5284  for (uint i= const_tables + 1; i < primary_tables; i++)
5285  {
5286  JOIN_TAB *const tab= join_tab + i;
5287  if (tab->condition() || // 1
5288  (tab->keyuse && !tab->first_inner)) // 2
5289  {
5290  /* We have to sort all rows */
5291  m_select_limit= HA_POS_ERROR;
5292  break;
5293  }
5294  }
5295  }
5296  /*
5297  Here we add sorting stage for ORDER BY/GROUP BY clause, if the
5298  optimiser chose FILESORT to be faster than INDEX SCAN or there is
5299  no suitable index present.
5300  OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
5301  */
5302  DBUG_PRINT("info",("Sorting for order by/group by"));
5303  ORDER_with_src order_arg= group_list ? group_list : order;
5304  if (join_tab &&
5305  ordered_index_usage !=
5306  (group_list ? ordered_index_group_by : ordered_index_order_by) &&
5307  join_tab[curr_tmp_table].type != JT_CONST &&
5308  join_tab[curr_tmp_table].type != JT_EQ_REF) // Don't sort 1 row
5309  {
5310  // Sort either first non-const table or the last tmp table
5311  JOIN_TAB *sort_tab= &join_tab[curr_tmp_table];
5312  if (need_tmp && !materialize_join && !exec_tmp_table->group)
5313  explain_flags.set(order_arg.src, ESP_USING_TMPTABLE);
5314 
5315  if (add_sorting_to_table(sort_tab, &order_arg))
5316  DBUG_RETURN(true);
5317  /*
5318  filesort_limit: Return only this many rows from filesort().
5319  We can use select_limit_cnt only if we have no group_by and 1 table.
5320  This allows us to use Bounded_queue for queries like:
5321  "select SQL_CALC_FOUND_ROWS * from t1 order by b desc limit 1;"
5322  m_select_limit == HA_POS_ERROR (we need a full table scan)
5323  unit->select_limit_cnt == 1 (we only need one row in the result set)
5324  */
5325  sort_tab->filesort->limit=
5326  (has_group_by || (primary_tables > curr_tmp_table + 1)) ?
5327  m_select_limit : unit->select_limit_cnt;
5328  }
5329  if (!plan_is_const() &&
5330  !join_tab[const_tables].table->sort.io_cache)
5331  {
5332  /*
5333  If no IO cache exists for the first table then we are using an
5334  INDEX SCAN and no filesort. Thus we should not remove the sorted
5335  attribute on the INDEX SCAN.
5336  */
5337  skip_sort_order= true;
5338  }
5339  }
5340  fields= curr_fields_list;
5341  // Reset before execution
5342  set_items_ref_array(items0);
5343  if (join_tab)
5344  join_tab[primary_tables + tmp_tables - 1].next_select=
5345  setup_end_select_func(this, NULL);
5346  group= has_group_by;
5347 
5348  DBUG_RETURN(false);
5349 }
5350 
5351 
5363 bool
5365 {
5366  explain_flags.set(order->src, ESP_USING_FILESORT);
5367  tab->filesort= new (thd->mem_root) Filesort(*order, HA_POS_ERROR, tab->select);
5368  if (!tab->filesort)
5369  return true;
5370  /*
5371  Select was moved to filesort->select to force join_init_read_record to use
5372  sorted result instead of reading table through select.
5373  */
5374  if (tab->select)
5375  {
5376  tab->select= NULL;
5377  tab->set_condition(NULL, __LINE__);
5378  }
5380  return false;
5381 }
5382 
5412 static bool
5413 test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER *order, TABLE *table,
5414  key_map usable_keys, int ref_key,
5415  ha_rows select_limit,
5416  int *new_key, int *new_key_direction,
5417  ha_rows *new_select_limit, uint *new_used_key_parts,
5418  uint *saved_best_key_parts)
5419 {
5420  DBUG_ENTER("test_if_cheaper_ordering");
5421  /*
5422  Check whether there is an index compatible with the given order
5423  usage of which is cheaper than usage of the ref_key index (ref_key>=0)
5424  or a table scan.
5425  It may be the case if ORDER/GROUP BY is used with LIMIT.
5426  */
5427  ha_rows best_select_limit= HA_POS_ERROR;
5428  JOIN *join= tab ? tab->join : NULL;
5429  uint nr;
5430  key_map keys;
5431  uint best_key_parts= 0;
5432  int best_key_direction= 0;
5433  ha_rows best_records= 0;
5434  double read_time;
5435  int best_key= -1;
5436  bool is_best_covering= FALSE;
5437  double fanout= 1;
5438  ha_rows table_records= table->file->stats.records;
5439  bool group= join && join->group && order == join->group_list;
5440  ha_rows refkey_rows_estimate= table->quick_condition_rows;
5441  const bool has_limit= (select_limit != HA_POS_ERROR);
5442 
5443  /*
5444  If not used with LIMIT, only use keys if the whole query can be
5445  resolved with a key; This is because filesort() is usually faster than
5446  retrieving all rows through an index.
5447  */
5448  if (select_limit >= table_records)
5449  {
5450  keys= *table->file->keys_to_use_for_scanning();
5451  keys.merge(table->covering_keys);
5452 
5453  /*
5454  We are adding here also the index specified in FORCE INDEX clause,
5455  if any.
5456  This is to allow users to use index in ORDER BY.
5457  */
5458  if (table->force_index)
5459  keys.merge(group ? table->keys_in_use_for_group_by :
5460  table->keys_in_use_for_order_by);
5461  keys.intersect(usable_keys);
5462  }
5463  else
5464  keys= usable_keys;
5465 
5466  if (join)
5467  {
5468  read_time= tab->position->read_time;
5469  for (const JOIN_TAB *jt= tab + 1;
5470  jt < join->join_tab + join->primary_tables; jt++)
5471  fanout*= jt->position->records_read; // fanout is always >= 1
5472  }
5473  else
5474  read_time= table->file->scan_time();
5475 
5476  /*
5477  Calculate the selectivity of the ref_key for REF_ACCESS. For
5478  RANGE_ACCESS we use table->quick_condition_rows.
5479  */
5480  if (ref_key >= 0 && tab->type == JT_REF)
5481  {
5482  if (table->quick_keys.is_set(ref_key))
5483  refkey_rows_estimate= table->quick_rows[ref_key];
5484  else
5485  {
5486  const KEY *ref_keyinfo= table->key_info + ref_key;
5487  refkey_rows_estimate= ref_keyinfo->rec_per_key[tab->ref.key_parts - 1];
5488  }
5489  set_if_bigger(refkey_rows_estimate, 1);
5490  }
5491  for (nr=0; nr < table->s->keys ; nr++)
5492  {
5493  int direction;
5494  uint used_key_parts;
5495 
5496  if (keys.is_set(nr) &&
5497  (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
5498  {
5499  /*
5500  At this point we are sure that ref_key is a non-ordering
5501  key (where "ordering key" is a key that will return rows
5502  in the order required by ORDER BY).
5503  */
5504  DBUG_ASSERT (ref_key != (int) nr);
5505 
5506  bool is_covering= table->covering_keys.is_set(nr) ||
5507  (nr == table->s->primary_key &&
5508  table->file->primary_key_is_clustered());
5509 
5510  /*
5511  Don't use an index scan with ORDER BY without limit.
5512  For GROUP BY without limit always use index scan
5513  if there is a suitable index.
5514  Why we hold to this asymmetry hardly can be explained
5515  rationally. It's easy to demonstrate that using
5516  temporary table + filesort could be cheaper for grouping
5517  queries too.
5518  */
5519  if (is_covering ||
5520  select_limit != HA_POS_ERROR ||
5521  (ref_key < 0 && (group || table->force_index)))
5522  {
5523  double rec_per_key;
5524  double index_scan_time;
5525  KEY *keyinfo= table->key_info+nr;
5526  if (select_limit == HA_POS_ERROR)
5527  select_limit= table_records;
5528  if (group)
5529  {
5530  /*
5531  Used_key_parts can be larger than keyinfo->key_parts
5532  when using a secondary index clustered with a primary
5533  key (e.g. as in Innodb).
5534  See Bug #28591 for details.
5535  */
5536  rec_per_key= used_key_parts &&
5537  used_key_parts <= actual_key_parts(keyinfo) ?
5538  keyinfo->rec_per_key[used_key_parts-1] : 1;
5539  set_if_bigger(rec_per_key, 1);
5540  /*
5541  With a grouping query each group containing on average
5542  rec_per_key records produces only one row that will
5543  be included into the result set.
5544  */
5545  if (select_limit > table_records/rec_per_key)
5546  select_limit= table_records;
5547  else
5548  select_limit= (ha_rows) (select_limit*rec_per_key);
5549  }
5550  /*
5551  If tab=tk is not the last joined table tn then to get first
5552  L records from the result set we can expect to retrieve
5553  only L/fanout(tk,tn) where fanout(tk,tn) says how many
5554  rows in the record set on average will match each row tk.
5555  Usually our estimates for fanouts are too pessimistic.
5556  So the estimate for L/fanout(tk,tn) will be too optimistic
5557  and as result we'll choose an index scan when using ref/range
5558  access + filesort will be cheaper.
5559  */
5560  select_limit= (ha_rows) (select_limit < fanout ?
5561  1 : select_limit/fanout);
5562  /*
5563  We assume that each of the tested indexes is not correlated
5564  with ref_key. Thus, to select first N records we have to scan
5565  N/selectivity(ref_key) index entries.
5566  selectivity(ref_key) = #scanned_records/#table_records =
5567  refkey_rows_estimate/table_records.
5568  In any case we can't select more than #table_records.
5569  N/(refkey_rows_estimate/table_records) > table_records
5570  <=> N > refkey_rows_estimate.
5571  */
5572  if (select_limit > refkey_rows_estimate)
5573  select_limit= table_records;
5574  else
5575  select_limit= (ha_rows) (select_limit *
5576  (double) table_records /
5577  refkey_rows_estimate);
5578  rec_per_key= keyinfo->rec_per_key[keyinfo->user_defined_key_parts - 1];
5579  set_if_bigger(rec_per_key, 1);
5580  /*
5581  Here we take into account the fact that rows are
5582  accessed in sequences rec_per_key records in each.
5583  Rows in such a sequence are supposed to be ordered
5584  by rowid/primary key. When reading the data
5585  in a sequence we'll touch not more pages than the
5586  table file contains.
5587  TODO. Use the formula for a disk sweep sequential access
5588  to calculate the cost of accessing data rows for one
5589  index entry.
5590  */
5591  index_scan_time= select_limit/rec_per_key *
5592  min(rec_per_key, table->file->scan_time());
5593  if ((ref_key < 0 && is_covering) ||
5594  (ref_key < 0 && (group || table->force_index)) ||
5595  index_scan_time < read_time)
5596  {
5597  ha_rows quick_records= table_records;
5598  ha_rows refkey_select_limit= (ref_key >= 0 &&
5599  table->covering_keys.is_set(ref_key)) ?
5600  refkey_rows_estimate :
5601  HA_POS_ERROR;
5602  if ((is_best_covering && !is_covering) ||
5603  (is_covering && refkey_select_limit < select_limit))
5604  continue;
5605  if (table->quick_keys.is_set(nr))
5606  quick_records= table->quick_rows[nr];
5607  if (best_key < 0 ||
5608  (select_limit <= min(quick_records,best_records) ?
5609  keyinfo->user_defined_key_parts < best_key_parts :
5610  quick_records < best_records))
5611  {
5612  best_key= nr;
5613  best_key_parts= keyinfo->user_defined_key_parts;
5614  if (saved_best_key_parts)
5615  *saved_best_key_parts= used_key_parts;
5616  best_records= quick_records;
5617  is_best_covering= is_covering;
5618  best_key_direction= direction;
5619  best_select_limit= select_limit;
5620  }
5621  }
5622  }
5623  }
5624  }
5625 
5626  if (best_key < 0 || best_key == ref_key)
5627  DBUG_RETURN(FALSE);
5628 
5629  *new_key= best_key;
5630  *new_key_direction= best_key_direction;
5631  *new_select_limit= has_limit ? best_select_limit : table_records;
5632  if (new_used_key_parts != NULL)
5633  *new_used_key_parts= best_key_parts;
5634 
5635  DBUG_RETURN(TRUE);
5636 }
5637 
5638 
5662 uint get_index_for_order(ORDER *order, TABLE *table, SQL_SELECT *select,
5663  ha_rows limit, bool *need_sort, bool *reverse)
5664 {
5665  if (select && select->quick && select->quick->unique_key_range())
5666  { // Single row select (always "ordered"): Ok to use with key field UPDATE
5667  *need_sort= FALSE;
5668  /*
5669  Returning of MAX_KEY here prevents updating of used_key_is_modified
5670  in mysql_update(). Use quick select "as is".
5671  */
5672  return MAX_KEY;
5673  }
5674 
5675  if (!order)
5676  {
5677  *need_sort= FALSE;
5678  if (select && select->quick)
5679  return select->quick->index; // index or MAX_KEY, use quick select as is
5680  else
5681  return table->file->key_used_on_scan; // MAX_KEY or index for some engines
5682  }
5683 
5684  if (!is_simple_order(order)) // just to cut further expensive checks
5685  {
5686  *need_sort= TRUE;
5687  return MAX_KEY;
5688  }
5689 
5690  if (select && select->quick)
5691  {
5692  if (select->quick->index == MAX_KEY)
5693  {
5694  *need_sort= TRUE;
5695  return MAX_KEY;
5696  }
5697 
5698  uint used_key_parts;
5699  switch (test_if_order_by_key(order, table, select->quick->index,
5700  &used_key_parts)) {
5701  case 1: // desired order
5702  *need_sort= FALSE;
5703  return select->quick->index;
5704  case 0: // unacceptable order
5705  *need_sort= TRUE;
5706  return MAX_KEY;
5707  case -1: // desired order, but opposite direction
5708  {
5709  QUICK_SELECT_I *reverse_quick;
5710  if ((reverse_quick=
5711  select->quick->make_reverse(used_key_parts)))
5712  {
5713  select->set_quick(reverse_quick);
5714  *need_sort= FALSE;
5715  return select->quick->index;
5716  }
5717  else
5718  {
5719  *need_sort= TRUE;
5720  return MAX_KEY;
5721  }
5722  }
5723  }
5724  DBUG_ASSERT(0);
5725  }
5726  else if (limit != HA_POS_ERROR)
5727  { // check if some index scan & LIMIT is more efficient than filesort
5728 
5729  /*
5730  Update quick_condition_rows since single table UPDATE/DELETE procedures
5731  don't call make_join_statistics() and leave this variable uninitialized.
5732  */
5733  table->quick_condition_rows= table->file->stats.records;
5734 
5735  int key, direction;
5736  if (test_if_cheaper_ordering(NULL, order, table,
5737  table->keys_in_use_for_order_by, -1,
5738  limit,
5739  &key, &direction, &limit) &&
5740  !is_key_used(table, key, table->write_set))
5741  {
5742  *need_sort= FALSE;
5743  *reverse= (direction < 0);
5744  return key;
5745  }
5746  }
5747  *need_sort= TRUE;
5748  return MAX_KEY;
5749 }
5750 
5751 
5761 uint actual_key_parts(KEY *key_info)
5762 {
5763  return key_info->table->in_use->
5764  optimizer_switch_flag(OPTIMIZER_SWITCH_USE_INDEX_EXTENSIONS) ?
5765  key_info->actual_key_parts : key_info->user_defined_key_parts;
5766 }
5767 
5768 
5778 uint actual_key_flags(KEY *key_info)
5779 {
5780  return key_info->table->in_use->
5781  optimizer_switch_flag(OPTIMIZER_SWITCH_USE_INDEX_EXTENSIONS) ?
5782  key_info->actual_flags : key_info->flags;
5783 }
5784