MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_optimizer.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_select.h"
28 #include "sql_optimizer.h"
29 #include "sql_resolver.h" // subquery_allows_materialization
30 #include "sql_executor.h"
31 #include "sql_planner.h"
32 #include "debug_sync.h" // DEBUG_SYNC
33 #include "opt_trace.h"
34 #include "sql_derived.h"
35 #include "sql_test.h"
36 #include "sql_base.h"
37 #include "sql_parse.h"
38 #include "my_bit.h"
39 #include "lock.h"
40 #include "abstract_query_plan.h"
41 #include "opt_explain_format.h" // Explain_format_flags
42 
43 #include <algorithm>
44 using std::max;
45 using std::min;
46 
47 static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, Item *conds,
48  Key_use_array *keyuse,
49  bool first_optimization);
50 static bool optimize_semijoin_nests_for_materialization(JOIN *join);
51 static void calculate_materialization_costs(JOIN *join, TABLE_LIST *sj_nest,
52  uint n_tables,
54 static void make_outerjoin_info(JOIN *join);
55 static bool make_join_select(JOIN *join, Item *item);
56 static bool list_contains_unique_index(JOIN_TAB *tab,
57  bool (*find_func) (Field *, void *), void *data);
58 static bool find_field_in_item_list (Field *field, void *data);
59 static bool find_field_in_order_list (Field *field, void *data);
60 static ORDER *create_distinct_group(THD *thd, Ref_ptr_array ref_pointer_array,
61  ORDER *order, List<Item> &fields,
62  List<Item> &all_fields,
63  bool *all_order_by_fields_used);
64 static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
65 static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
66 static Item *remove_additional_cond(Item* conds);
67 static bool simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
68  Item *conds, bool top, bool in_sj,
69  Item **new_conds,
70  uint *changelog= NULL);
71 static bool record_join_nest_info(st_select_lex *select,
72  List<TABLE_LIST> *tables);
73 static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
74  uint first_unused);
75 static ORDER *remove_const(JOIN *join,ORDER *first_order, Item *cond,
76  bool change_list, bool *simple_order,
77  const char *clause_type);
78 static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where);
79 static void trace_table_dependencies(Opt_trace_context * trace,
80  JOIN_TAB *join_tabs,
81  uint table_count);
82 static bool
83 update_ref_and_keys(THD *thd, Key_use_array *keyuse,JOIN_TAB *join_tab,
84  uint tables, Item *cond, COND_EQUAL *cond_equal,
85  table_map normal_tables, SELECT_LEX *select_lex,
86  SARGABLE_PARAM **sargables);
87 static bool pull_out_semijoin_tables(JOIN *join);
88 static void set_position(JOIN *join, uint idx, JOIN_TAB *table, Key_use *key);
89 static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
90 static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
91  TABLE *table,
92  const key_map *keys,ha_rows limit);
93 static void optimize_keyuse(JOIN *join, Key_use_array *keyuse_array);
94 static Item *
95 make_cond_for_table_from_pred(Item *root_cond, Item *cond,
96  table_map tables, table_map used_table,
97  bool exclude_expensive_cond);
98 static bool
99 only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables,
100  table_map *cached_eq_ref_tables, table_map
101  *eq_ref_tables);
102 
103 
116 int
118 {
119  ulonglong select_opts_for_readinfo;
120  uint no_jbuf_after= UINT_MAX;
121 
122  DBUG_ENTER("JOIN::optimize");
123  DBUG_ASSERT(!tables || thd->lex->is_query_tables_locked());
124 
125  // to prevent double initialization on EXPLAIN
126  if (optimized)
127  DBUG_RETURN(0);
128 
129  // We may do transformations (like semi-join):
130  Prepare_error_tracker tracker(thd);
131 
132  optimized= true;
133  const bool first_optimization= select_lex->first_cond_optimization;
134  select_lex->first_cond_optimization= false;
135 
136  DEBUG_SYNC(thd, "before_join_optimize");
137 
138  THD_STAGE_INFO(thd, stage_optimizing);
139 
140  Opt_trace_context * const trace= &thd->opt_trace;
141  Opt_trace_object trace_wrapper(trace);
142  Opt_trace_object trace_optimize(trace, "join_optimization");
143  trace_optimize.add_select_number(select_lex->select_number);
144  Opt_trace_array trace_steps(trace, "steps");
145 
146  // Needed in case optimizer short-cuts, set properly in make_tmp_tables_info()
147  fields= &select_lex->item_list;
148 
149  /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
150  if (flatten_subqueries())
151  DBUG_RETURN(1); /* purecov: inspected */
152 
153  /*
154  Run optimize phase for all derived tables/views used in this SELECT,
155  including those in semi-joins.
156  */
157  if (select_lex->handle_derived(thd->lex, &mysql_derived_optimize))
158  DBUG_RETURN(1);
159 
160  /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
161 
162  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
163  unit->select_limit_cnt);
164  // m_select_limit is used to decide if we are likely to scan the whole table.
165  m_select_limit= unit->select_limit_cnt;
166  if (having || (select_options & OPTION_FOUND_ROWS))
167  m_select_limit= HA_POS_ERROR;
168  do_send_rows = (unit->select_limit_cnt > 0) ? 1 : 0;
169 
170 #ifdef HAVE_REF_TO_FIELDS // Not done yet
171  /* Add HAVING to WHERE if possible */
172  if (having && !group_list && !sum_func_count)
173  {
174  if (!conds)
175  {
176  conds= having;
177  having= 0;
178  }
179  else if ((conds=new Item_cond_and(conds,having)))
180  {
181  /*
182  Item_cond_and can't be fixed after creation, so we do not check
183  conds->fixed
184  */
185  conds->fix_fields(thd, &conds);
186  conds->change_ref_to_fields(thd, tables_list);
187  conds->top_level_item();
188  having= 0;
189  }
190  }
191 #endif
192  if (first_optimization)
193  {
194  /*
195  These are permanent transformations, so new items must be
196  allocated in the statement mem root
197  */
198  Prepared_stmt_arena_holder ps_arena_holder(thd);
199 
200  /* Convert all outer joins to inner joins if possible */
201  if (simplify_joins(this, join_list, conds, true, false, &conds))
202  {
203  DBUG_PRINT("error",("Error from simplify_joins"));
204  DBUG_RETURN(1);
205  }
206  if (record_join_nest_info(select_lex, join_list))
207  {
208  DBUG_PRINT("error",("Error from record_join_nest_info"));
209  DBUG_RETURN(1);
210  }
211  build_bitmap_for_nested_joins(join_list, 0);
212 
213  /*
214  After permanent transformations above, prep_where created in
215  st_select_lex::fix_prepare_information() is out-of-date, we need to
216  refresh it.
217  For that We must copy "conds" because it contains AND/OR items in a
218  non-permanent memroot. And this copy must contain real items only,
219  because the new AND/OR items will not have their argument pointers
220  restored by rollback_item_tree_changes().
221  @see st_select_lex::fix_prepare_information() for problems with this.
222  @todo in WL#7082 move transformations above to before
223  st_select_lex::fix_prepare_information(), and remove this second copy
224  below.
225  */
226  select_lex->prep_where=
227  conds ? conds->copy_andor_structure(thd, true) : NULL;
228  }
229 
230  /*
231  Note: optimize_cond() makes changes to conds. Since
232  select_lex->where and conds points to the same condition, this
233  function call effectively changes select_lex->where as well.
234  */
235  conds= optimize_cond(thd, conds, &cond_equal,
236  join_list, true, &select_lex->cond_value);
237  if (thd->is_error())
238  {
239  error= 1;
240  DBUG_PRINT("error",("Error from optimize_cond"));
241  DBUG_RETURN(1);
242  }
243 
244  {
245  // Note above about optimize_cond() also applies to selec_lex->having
246  having= optimize_cond(thd, having, &cond_equal, join_list, false,
247  &select_lex->having_value);
248  if (thd->is_error())
249  {
250  error= 1;
251  DBUG_PRINT("error",("Error from optimize_cond"));
252  DBUG_RETURN(1);
253  }
254  if (select_lex->cond_value == Item::COND_FALSE ||
255  select_lex->having_value == Item::COND_FALSE ||
256  (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
257  { /* Impossible cond */
258  zero_result_cause= select_lex->having_value == Item::COND_FALSE ?
259  "Impossible HAVING" : "Impossible WHERE";
260  tables= 0;
261  primary_tables= 0;
262  best_rowcount= 0;
263  goto setup_subq_exit;
264  }
265  }
266 
267 #ifdef WITH_PARTITION_STORAGE_ENGINE
268  if (select_lex->partitioned_table_count && prune_table_partitions(thd))
269  {
270  error= 1;
271  DBUG_PRINT("error", ("Error from prune_partitions"));
272  DBUG_RETURN(1);
273  }
274 #endif
275 
276  optimize_fts_limit_query();
277 
278  /*
279  Try to optimize count(*), min() and max() to const fields if
280  there is implicit grouping (aggregate functions but no
281  group_list). In this case, the result set shall only contain one
282  row.
283  */
284  if (tables_list && implicit_grouping)
285  {
286  int res;
287  /*
288  opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
289  to the WHERE conditions,
290  or 1 if all items were resolved (optimized away),
291  or 0, or an error number HA_ERR_...
292 
293  If all items were resolved by opt_sum_query, there is no need to
294  open any tables.
295  */
296  if ((res=opt_sum_query(thd, select_lex->leaf_tables, all_fields, conds)))
297  {
298  best_rowcount= 0;
299  if (res == HA_ERR_KEY_NOT_FOUND)
300  {
301  DBUG_PRINT("info",("No matching min/max row"));
302  zero_result_cause= "No matching min/max row";
303  tables= 0;
304  primary_tables= 0;
305  goto setup_subq_exit;
306  }
307  if (res > 1)
308  {
309  error= res;
310  DBUG_PRINT("error",("Error from opt_sum_query"));
311  DBUG_RETURN(1);
312  }
313  if (res < 0)
314  {
315  DBUG_PRINT("info",("No matching min/max row"));
316  zero_result_cause= "No matching min/max row";
317  tables= 0;
318  primary_tables= 0;
319  goto setup_subq_exit;
320  }
321  DBUG_PRINT("info",("Select tables optimized away"));
322  zero_result_cause= "Select tables optimized away";
323  tables_list= 0; // All tables resolved
324  best_rowcount= 1;
326  /*
327  Extract all table-independent conditions and replace the WHERE
328  clause with them. All other conditions were computed by opt_sum_query
329  and the MIN/MAX/COUNT function(s) have been replaced by constants,
330  so there is no need to compute the whole WHERE clause again.
331  Notice that make_cond_for_table() will always succeed to remove all
332  computed conditions, because opt_sum_query() is applicable only to
333  conjunctions.
334  Preserve conditions for EXPLAIN.
335  */
336  if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
337  {
338  Item *table_independent_conds=
339  make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
340  DBUG_EXECUTE("where",
341  print_where(table_independent_conds,
342  "where after opt_sum_query()",
343  QT_ORDINARY););
344  conds= table_independent_conds;
345  }
346  goto setup_subq_exit;
347  }
348  }
349  if (!tables_list)
350  {
351  DBUG_PRINT("info",("No tables"));
352  best_rowcount= 1;
353  error= 0;
354  if (make_tmp_tables_info())
355  DBUG_RETURN(1);
356  DBUG_RETURN(0);
357  }
358  error= -1; // Error is sent to client
359  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
360 
361  /* Calculate how to do the join */
362  THD_STAGE_INFO(thd, stage_statistics);
363  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse,
364  first_optimization))
365  {
366  DBUG_PRINT("error",("Error: make_join_statistics() failed"));
367  DBUG_RETURN(1);
368  }
369 
370  if (rollup.state != ROLLUP::STATE_NONE)
371  {
373  {
374  DBUG_PRINT("error", ("Error: rollup_process_fields() failed"));
375  DBUG_RETURN(1);
376  }
377  }
378  else
379  {
380  /* Remove distinct if only const tables */
382  }
383 
385  !(select_options & SELECT_DESCRIBE))
386  {
387  // There is at least one empty const table
388  zero_result_cause= "no matching row in const table";
389  DBUG_PRINT("error",("Error: %s", zero_result_cause));
390  goto setup_subq_exit;
391  }
392  if (!(thd->variables.option_bits & OPTION_BIG_SELECTS) &&
393  best_read > (double) thd->variables.max_join_size &&
394  !(select_options & SELECT_DESCRIBE))
395  { /* purecov: inspected */
396  my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
397  error= -1;
398  DBUG_RETURN(1);
399  }
400  if (const_tables && !thd->locked_tables_mode &&
401  !(select_options & SELECT_NO_UNLOCK))
402  {
403  TABLE *ct[MAX_TABLES];
404  for (uint i= 0; i < const_tables; i++)
405  ct[i]= join_tab[i].table;
406  mysql_unlock_some_tables(thd, ct, const_tables);
407  }
408  if (!conds && outer_join)
409  {
410  /* Handle the case where we have an OUTER JOIN without a WHERE */
411  conds=new Item_int((longlong) 1,1); // Always true
412  }
413 
414  error= 0;
415  if (outer_join)
416  {
418  make_outerjoin_info(this);
419  }
420  // Assign map of "available" tables to all tables belonging to query block
421  if (!plan_is_const())
422  set_prefix_tables();
423 
424  /*
425  Among the equal fields belonging to the same multiple equality
426  choose the one that is to be retrieved first and substitute
427  all references to these in where condition for a reference for
428  the selected field.
429  */
430  if (conds)
431  {
433  if (thd->is_error())
434  {
435  error= 1;
436  DBUG_PRINT("error",("Error from substitute_for_best_equal"));
437  DBUG_RETURN(1);
438  }
439  conds->update_used_tables();
440  DBUG_EXECUTE("where",
441  print_where(conds,
442  "after substitute_best_equal",
443  QT_ORDINARY););
444  }
445 
446  /*
447  Perform the same optimization on field evaluation for all join conditions.
448  */
449  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
450  {
451  if (tab->on_expr_ref && *tab->on_expr_ref)
452  {
454  tab->cond_equal,
455  map2table);
456  if (thd->is_error())
457  {
458  error= 1;
459  DBUG_PRINT("error",("Error from substitute_for_best_equal"));
460  DBUG_RETURN(1);
461  }
462  (*tab->on_expr_ref)->update_used_tables();
463  }
464  }
465 
467  (select_options & SELECT_DESCRIBE))
468  {
469  conds=new Item_int((longlong) 0,1); // Always false
470  }
471 
472  if (select_lex->materialized_table_count)
474 
475  if (set_access_methods())
476  {
477  error= 1;
478  DBUG_PRINT("error",("Error from set_access_methods"));
479  DBUG_RETURN(1);
480  }
481 
482  // Update table dependencies after assigning ref access fields
483  update_depend_map(this);
484 
485  THD_STAGE_INFO(thd, stage_preparing);
486  if (result->initialize_tables(this))
487  {
488  DBUG_PRINT("error",("Error: initialize_tables() failed"));
489  DBUG_RETURN(1); // error == -1
490  }
491 
492  if (make_join_select(this, conds))
493  {
495  "Impossible WHERE noticed after reading const tables";
496  goto setup_subq_exit;
497  }
498 
499  error= -1; /* if goto err */
500 
501  /* Optimize distinct away if possible */
502  {
503  ORDER *org_order= order;
504  order= ORDER_with_src(remove_const(this, order, conds, 1, &simple_order, "ORDER BY"), order.src);;
505  if (thd->is_error())
506  {
507  error= 1;
508  DBUG_PRINT("error",("Error from remove_const"));
509  DBUG_RETURN(1);
510  }
511 
512  /*
513  If we are using ORDER BY NULL or ORDER BY const_expression,
514  return result in any order (even if we are using a GROUP BY)
515  */
516  if (!order && org_order)
517  skip_sort_order= 1;
518  }
519  /*
520  Check if we can optimize away GROUP BY/DISTINCT.
521  We can do that if there are no aggregate functions, the
522  fields in DISTINCT clause (if present) and/or columns in GROUP BY
523  (if present) contain direct references to all key parts of
524  an unique index (in whatever order) and if the key parts of the
525  unique index cannot contain NULLs.
526  Note that the unique keys for DISTINCT and GROUP BY should not
527  be the same (as long as they are unique).
528 
529  The FROM clause must contain a single non-constant table.
530  */
531  if (plan_is_single_table() &&
532  (group_list || select_distinct) &&
533  !tmp_table_param.sum_func_count &&
534  (!join_tab[const_tables].select ||
535  !join_tab[const_tables].select->quick ||
536  join_tab[const_tables].select->quick->get_type() !=
537  QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
538  {
539  if (group_list && rollup.state == ROLLUP::STATE_NONE &&
540  list_contains_unique_index(&join_tab[const_tables],
541  find_field_in_order_list,
542  (void *) group_list))
543  {
544  /*
545  We have found that grouping can be removed since groups correspond to
546  only one row anyway, but we still have to guarantee correct result
547  order. The line below effectively rewrites the query from GROUP BY
548  <fields> to ORDER BY <fields>. There are three exceptions:
549  - if skip_sort_order is set (see above), then we can simply skip
550  GROUP BY;
551  - if we are in a subquery, we don't have to maintain order
552  - we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
553  with the GROUP BY ones, i.e. either one is a prefix of another.
554  We only check if the ORDER BY is a prefix of GROUP BY. In this case
555  test_if_subpart() copies the ASC/DESC attributes from the original
556  ORDER BY fields.
557  If GROUP BY is a prefix of ORDER BY, then it is safe to leave
558  'order' as is.
559  */
560  if (!order || test_if_subpart(group_list, order))
561  {
562  if (skip_sort_order ||
563  select_lex->master_unit()->item) // This is a subquery
564  order= NULL;
565  else
566  order= group_list;
567  }
568  /*
569  If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
570  rewritten to IGNORE INDEX FOR ORDER BY(fields).
571  */
572  join_tab->table->keys_in_use_for_order_by=
573  join_tab->table->keys_in_use_for_group_by;
574  group_list= 0;
575  group= 0;
576  }
577  if (select_distinct &&
578  list_contains_unique_index(&join_tab[const_tables],
579  find_field_in_item_list,
580  (void *) &fields_list))
581  {
582  select_distinct= 0;
583  }
584  }
585  if (group_list || tmp_table_param.sum_func_count)
586  {
587  if (hidden_group_field_count == 0 && rollup.state == ROLLUP::STATE_NONE)
588  {
589  /*
590  All GROUP expressions are in SELECT list, so resulting rows are
591  distinct. ROLLUP is not specified, so adds no row. So all rows in the
592  result set are distinct, DISTINCT is useless.
593  @todo could remove DISTINCT if ROLLUP were specified and all GROUP
594  expressions were non-nullable, because ROLLUP adds only NULL
595  values. Currently, ROLLUP+DISTINCT is rejected because executor
596  cannot handle it in all cases.
597  */
598  select_distinct= false;
599  }
600  }
601  else if (select_distinct &&
603  rollup.state == ROLLUP::STATE_NONE)
604  {
605  /*
606  We are only using one table. In this case we change DISTINCT to a
607  GROUP BY query if:
608  - The GROUP BY can be done through indexes (no sort) and the ORDER
609  BY only uses selected fields.
610  (In this case we can later optimize away GROUP BY and ORDER BY)
611  - We are scanning the whole table without LIMIT
612  This can happen if:
613  - We are using CALC_FOUND_ROWS
614  - We are using an ORDER BY that can't be optimized away.
615 
616  We don't want to use this optimization when we are using LIMIT
617  because in this case we can just create a temporary table that
618  holds LIMIT rows and stop when this table is full.
619  */
620  JOIN_TAB *tab= &join_tab[const_tables];
621  bool all_order_fields_used;
622  if (order)
623  {
625  test_if_skip_sort_order(tab, order, m_select_limit,
626  true, // no_changes
627  &tab->table->keys_in_use_for_order_by,
628  "ORDER BY");
629  }
630  ORDER *o;
631  if ((o= create_distinct_group(thd, ref_ptrs,
633  &all_order_fields_used)))
634  {
635  group_list= ORDER_with_src(o, ESC_DISTINCT);
636  const bool skip_group=
637  skip_sort_order &&
638  test_if_skip_sort_order(tab, group_list, m_select_limit,
639  true, // no_changes
640  &tab->table->keys_in_use_for_group_by,
641  "GROUP BY");
642  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
643  if ((skip_group && all_order_fields_used) ||
644  m_select_limit == HA_POS_ERROR ||
645  (order && !skip_sort_order))
646  {
647  /* Change DISTINCT to GROUP BY */
648  select_distinct= 0;
649  no_order= !order;
650  if (all_order_fields_used)
651  {
652  if (order && skip_sort_order)
653  {
654  /*
655  Force MySQL to read the table in sorted order to get result in
656  ORDER BY order.
657  */
658  tmp_table_param.quick_group=0;
659  }
660  order=0;
661  }
662  group=1; // For end_write_group
663  }
664  else
665  group_list= 0;
666  }
667  else if (thd->is_fatal_error) // End of memory
668  DBUG_RETURN(1);
669  }
670  simple_group= 0;
671  {
672  ORDER *old_group_list= group_list;
673  group_list= ORDER_with_src(remove_const(this, group_list, conds,
674  rollup.state == ROLLUP::STATE_NONE,
675  &simple_group, "GROUP BY"),
676  group_list.src);
677 
678  if (thd->is_error())
679  {
680  error= 1;
681  DBUG_PRINT("error",("Error from remove_const"));
682  DBUG_RETURN(1);
683  }
684  if (old_group_list && !group_list)
685  select_distinct= 0;
686  }
687  if (!group_list && group)
688  {
689  order=0; // The output has only one row
690  simple_order=1;
691  select_distinct= 0; // No need in distinct for 1 row
693  }
694 
695  calc_group_buffer(this, group_list);
696  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
697 
698  if (test_if_subpart(group_list, order) ||
699  (!group_list && tmp_table_param.sum_func_count))
700  {
701  order=0;
702  if (is_indexed_agg_distinct(this, NULL))
703  sort_and_group= 0;
704  }
705 
706  /*
707  If the hint FORCE INDEX FOR ORDER BY/GROUP BY is used for the first
708  table (it does not make sense for other tables) then we cannot do join
709  buffering.
710  */
711  if (!plan_is_const())
712  {
713  const TABLE * const first= join_tab[const_tables].table;
714  if ((first->force_index_order && order) ||
715  (first->force_index_group && group_list))
716  no_jbuf_after= 0;
717  }
718 
719  select_opts_for_readinfo=
720  (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) |
721  (select_lex->ftfunc_list->elements ? SELECT_NO_JOIN_CACHE : 0);
722 
723  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
724  DBUG_RETURN(1);
725 
726  /*
727  Check if we need to create a temporary table.
728  This has to be done if all tables are not already read (const tables)
729  and one of the following conditions holds:
730  - We are using DISTINCT (simple distinct's are already optimized away)
731  - We are using an ORDER BY or GROUP BY on fields not in the first table
732  - We are using different ORDER BY and GROUP BY orders
733  - The user wants us to buffer the result.
734  When the WITH ROLLUP modifier is present, we cannot skip temporary table
735  creation for the DISTINCT clause just because there are only const tables.
736  */
737  need_tmp= ((!plan_is_const() &&
738  ((select_distinct || !simple_order || !simple_group) ||
739  (group_list && order) ||
740  test(select_options & OPTION_BUFFER_RESULT))) ||
741  (rollup.state != ROLLUP::STATE_NONE && select_distinct));
742 
743  /* Perform FULLTEXT search before all regular searches */
744  if (!(select_options & SELECT_DESCRIBE))
745  {
746  init_ftfuncs(thd, select_lex, test(order));
747  optimize_fts_query();
748  }
749 
750  /*
751  By setting child_subquery_can_materialize so late we gain the following:
752  JOIN::compare_costs_of_subquery_strategies() can test this variable to
753  know if we are have finished evaluating constant conditions, which itself
754  helps determining fanouts.
755  */
757 
758  /*
759  It's necessary to check const part of HAVING cond as
760  there is a chance that some cond parts may become
761  const items after make_join_statisctics(for example
762  when Item is a reference to cost table field from
763  outer join).
764  This check is performed only for those conditions
765  which do not use aggregate functions. In such case
766  temporary table may not be used and const condition
767  elements may be lost during further having
768  condition transformation in JOIN::exec.
769  */
770  if (having && const_table_map && !having->with_sum_func)
771  {
772  having->update_used_tables();
773  having= remove_eq_conds(thd, having, &select_lex->having_value);
774  if (select_lex->having_value == Item::COND_FALSE)
775  {
776  having= having_for_explain= new Item_int((longlong) 0,1);
777  zero_result_cause= "Impossible HAVING noticed after reading const tables";
778  error= 0;
779  DBUG_RETURN(0);
780  }
781  }
782 
783  /* Cache constant expressions in WHERE, HAVING, ON clauses. */
784  if (!plan_is_const() && cache_const_exprs())
785  DBUG_RETURN(1);
786 
787  // See if this subquery can be evaluated with subselect_indexsubquery_engine
788  if (!group_list && !order &&
789  unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
790  primary_tables == 1 && conds &&
791  !unit->is_union())
792  {
793  bool changed= FALSE;
794  subselect_engine *engine= 0;
795  Item_in_subselect * const in_subs=
796  static_cast<Item_in_subselect *>(unit->item);
797  if (in_subs->exec_method == Item_exists_subselect::EXEC_MATERIALIZATION)
798  {
799  // We cannot have two engines at the same time
800  }
801  else if (!having)
802  {
803  Item *where= conds;
804  if (join_tab[0].type == JT_EQ_REF &&
805  join_tab[0].ref.items[0]->item_name.ptr() == in_left_expr_name)
806  {
807  remove_subq_pushed_predicates(&where);
808  save_index_subquery_explain_info(join_tab, where);
809  join_tab[0].type= JT_UNIQUE_SUBQUERY;
810  error= 0;
811  changed= TRUE;
812  engine= new subselect_indexsubquery_engine(thd, join_tab, unit->item,
813  where, NULL /* having */,
814  false /* check_null */,
815  true /* unique */);
816  }
817  else if (join_tab[0].type == JT_REF &&
818  join_tab[0].ref.items[0]->item_name.ptr() == in_left_expr_name)
819  {
820  remove_subq_pushed_predicates(&where);
821  save_index_subquery_explain_info(join_tab, where);
822  join_tab[0].type= JT_INDEX_SUBQUERY;
823  error= 0;
824  changed= TRUE;
825  engine= new subselect_indexsubquery_engine(thd, join_tab, unit->item,
826  where, NULL, false, false);
827  }
828  } else if (join_tab[0].type == JT_REF_OR_NULL &&
829  join_tab[0].ref.items[0]->item_name.ptr() == in_left_expr_name &&
830  having->item_name.ptr() == in_having_cond)
831  {
832  join_tab[0].type= JT_INDEX_SUBQUERY;
833  error= 0;
834  changed= TRUE;
835  conds= remove_additional_cond(conds);
836  save_index_subquery_explain_info(join_tab, conds);
837  engine= new subselect_indexsubquery_engine(thd, join_tab, unit->item,
838  conds, having, true, false);
845  }
846  if (changed)
847  {
848  /*
849  We leave optimize() because the rest of it is only about order/group
850  which those subqueries don't have.
851  @todo: let execution flow down instead, to be future-proof.
852  */
853  DBUG_RETURN(unit->item->change_engine(engine));
854  }
855  }
856  /*
857  Need to tell handlers that to play it safe, it should fetch all
858  columns of the primary key of the tables: this is because MySQL may
859  build row pointers for the rows, and for all columns of the primary key
860  the read set has not necessarily been set by the server code.
861  */
862  if (need_tmp || select_distinct || group_list || order)
863  {
864  for (uint i = const_tables; i < primary_tables; i++)
865  join_tab[i].table->prepare_for_position();
866  }
867  DBUG_EXECUTE("info", TEST_join(this););
868 
869  if (!plan_is_const())
870  {
871  JOIN_TAB *tab= &join_tab[const_tables];
872 
873  if (order)
874  {
875  /*
876  Force using of tmp table if sorting by a SP or UDF function due to
877  their expensive and probably non-deterministic nature.
878  */
879  for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
880  {
881  Item *item= *tmp_order->item;
882  if (item->is_expensive())
883  {
884  /* Force tmp table without sort */
885  need_tmp=1; simple_order=simple_group=0;
886  break;
887  }
888  }
889  }
890 
891  /*
892  Because filesort always does a full table scan or a quick range scan
893  we must add the removed reference to the select for the table.
894  We only need to do this when we have a simple_order or simple_group
895  as in other cases the join is done before the sort.
896  */
897  if ((order || group_list) &&
898  tab->type != JT_ALL &&
899  tab->type != JT_FT &&
900  tab->type != JT_REF_OR_NULL &&
901  ((order && simple_order) || (group_list && simple_group)))
902  {
903  if (add_ref_to_table_cond(thd,tab)) {
904  DBUG_RETURN(1);
905  }
906  }
907 
908  /*
909  Investigate whether we may use an ordered index as part of either
910  DISTINCT, GROUP BY or ORDER BY execution. An ordered index may be
911  used for only the first of any of these terms to be executed. This
912  is reflected in the order which we check for test_if_skip_sort_order()
913  below. However we do not check for DISTINCT here, as it would have
914  been transformed to a GROUP BY at this stage if it is a candidate for
915  ordered index optimization.
916  If a decision was made to use an ordered index, the availability
917  if such an access path is stored in 'ordered_index_usage' for later
918  use by 'execute' or 'explain'
919  */
920  DBUG_ASSERT(ordered_index_usage == ordered_index_void);
921 
922  if (group_list) // GROUP BY honoured first
923  // (DISTINCT was rewritten to GROUP BY if skippable)
924  {
925  /*
926  When there is SQL_BIG_RESULT do not sort using index for GROUP BY,
927  and thus force sorting on disk unless a group min-max optimization
928  is going to be used as it is applied now only for one table queries
929  with covering indexes.
930  */
931  if (!(select_options & SELECT_BIG_RESULT) ||
932  (tab->select &&
933  tab->select->quick &&
934  tab->select->quick->get_type() ==
935  QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
936  {
937  if (simple_group && // GROUP BY is possibly skippable
938  !select_distinct) // .. if not preceded by a DISTINCT
939  {
940  /*
941  Calculate a possible 'limit' of table rows for 'GROUP BY':
942  A specified 'LIMIT' is relative to the final resultset.
943  'need_tmp' implies that there will be more postprocessing
944  so the specified 'limit' should not be enforced yet.
945  */
946  const ha_rows limit = need_tmp ? HA_POS_ERROR : m_select_limit;
947 
948  if (test_if_skip_sort_order(tab, group_list, limit, false,
949  &tab->table->keys_in_use_for_group_by,
950  "GROUP BY"))
951  {
952  ordered_index_usage= ordered_index_group_by;
953  }
954  }
955 
956  /*
957  If we are going to use semi-join LooseScan, it will depend
958  on the selected index scan to be used. If index is not used
959  for the GROUP BY, we risk that sorting is put on the LooseScan
960  table. In order to avoid this, force use of temporary table.
961  TODO: Explain the quick_group part of the test below.
962  */
963  if ((ordered_index_usage != ordered_index_group_by) &&
964  (tmp_table_param.quick_group ||
965  (tab->emb_sj_nest &&
966  tab->position->sj_strategy == SJ_OPT_LOOSE_SCAN)))
967  {
968  need_tmp=1;
969  simple_order= simple_group= false; // Force tmp table without sort
970  }
971  }
972  }
973  else if (order && // ORDER BY wo/ preceeding GROUP BY
974  (simple_order || skip_sort_order)) // which is possibly skippable
975  {
976  if (test_if_skip_sort_order(tab, order, m_select_limit, false,
977  &tab->table->keys_in_use_for_order_by,
978  "ORDER BY"))
979  {
980  ordered_index_usage= ordered_index_order_by;
981  }
982  }
983  }
984 
996  if (!plan_is_const() && !plan_is_single_table())
997  {
998  const AQP::Join_plan plan(this);
999  if (ha_make_pushed_joins(thd, &plan))
1000  DBUG_RETURN(1);
1001  }
1002 
1007  for (uint i= const_tables; i < tables; i++)
1008  {
1009  pick_table_access_method (&join_tab[i]);
1010  }
1011 
1012  if (make_tmp_tables_info())
1013  DBUG_RETURN(1);
1014 
1015  error= 0;
1016  DBUG_RETURN(0);
1017 
1018 setup_subq_exit:
1019 
1020  DBUG_ASSERT(zero_result_cause != NULL);
1021  /*
1022  Even with zero matching rows, subqueries in the HAVING clause may
1023  need to be evaluated if there are aggregate functions in the
1024  query. If this JOIN is part of an outer query, subqueries in HAVING may
1025  be evaluated several times in total; so subquery materialization makes
1026  sense.
1027  */
1029  trace_steps.end(); // because all steps are done
1030  Opt_trace_object(trace, "empty_result")
1031  .add_alnum("cause", zero_result_cause);
1032 
1034  error= 0;
1035  DBUG_RETURN(0);
1036 }
1037 
1038 
1039 #ifdef WITH_PARTITION_STORAGE_ENGINE
1040 
1050 bool JOIN::prune_table_partitions(THD *thd)
1051 {
1052  DBUG_ASSERT(select_lex->partitioned_table_count);
1053 
1054  for (TABLE_LIST *tbl= select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
1055  {
1056  /*
1057  If tbl->embedding!=NULL that means that this table is in the inner
1058  part of the nested outer join, and we can't do partition pruning
1059  (TODO: check if this limitation can be lifted.
1060  This also excludes semi-joins. Is that intentional?)
1061  This will try to prune non-static conditions, which can
1062  be used after the tables are locked.
1063  */
1064  if (!tbl->embedding)
1065  {
1066  if (prune_partitions(thd, tbl->table,
1067  tbl->join_cond() ? tbl->join_cond() : conds))
1068  return true;
1069  }
1070  }
1071 
1072  return false;
1073 }
1074 
1075 #endif
1076 
1077 
1089 {
1090  List_iterator<TABLE_LIST> li(*join_list);
1091  TABLE_LIST *table;
1092  DBUG_ENTER("reset_nj_counters");
1093  while ((table= li++))
1094  {
1095  NESTED_JOIN *nested_join;
1096  if ((nested_join= table->nested_join))
1097  {
1098  nested_join->nj_counter= 0;
1099  reset_nj_counters(&nested_join->join_list);
1100  }
1101  }
1102  DBUG_VOID_RETURN;
1103 }
1104 
1105 
1106 /*****************************************************************************
1107  Make some simple condition optimization:
1108  If there is a test 'field = const' change all refs to 'field' to 'const'
1109  Remove all dummy tests 'item = item', 'const op const'.
1110  Remove all 'item is NULL', when item can never be null!
1111  item->marker should be 0 for all items on entry
1112  Return in cond_value FALSE if condition is impossible (1 = 2)
1113 *****************************************************************************/
1114 
1115 class COND_CMP :public ilink<COND_CMP> {
1116 public:
1117  static void *operator new(size_t size)
1118  {
1119  return (void*) sql_alloc((uint) size);
1120  }
1121  static void operator delete(void *ptr __attribute__((unused)),
1122  size_t size __attribute__((unused)))
1123  { TRASH(ptr, size); }
1124 
1125  Item *and_level;
1126  Item_func *cmp_func;
1127  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
1128 };
1129 
1130 
1151  bool *inherited_fl)
1152 {
1153  Item_equal *item= 0;
1154  bool in_upper_level= FALSE;
1155  while (cond_equal)
1156  {
1157  List_iterator_fast<Item_equal> li(cond_equal->current_level);
1158  while ((item= li++))
1159  {
1160  if (item->contains(field))
1161  goto finish;
1162  }
1163  in_upper_level= TRUE;
1164  cond_equal= cond_equal->upper_levels;
1165  }
1166  in_upper_level= FALSE;
1167 finish:
1168  *inherited_fl= in_upper_level;
1169  return item;
1170 }
1171 
1172 
1189 {
1190  bool dummy;
1191  Item_equal *item_eq= find_item_equal(cond_equal, item_field->field, &dummy);
1192  if (!item_eq)
1193  return item_field;
1194 
1195  return item_eq->get_subst_item(item_field);
1196 }
1197 
1198 
1281 static bool check_simple_equality(Item *left_item, Item *right_item,
1282  Item *item, COND_EQUAL *cond_equal)
1283 {
1284  if (left_item->type() == Item::REF_ITEM &&
1285  ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
1286  {
1287  if (((Item_ref*)left_item)->depended_from)
1288  return FALSE;
1289  left_item= left_item->real_item();
1290  }
1291  if (right_item->type() == Item::REF_ITEM &&
1292  ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
1293  {
1294  if (((Item_ref*)right_item)->depended_from)
1295  return FALSE;
1296  right_item= right_item->real_item();
1297  }
1298  if (left_item->type() == Item::FIELD_ITEM &&
1299  right_item->type() == Item::FIELD_ITEM &&
1300  !((Item_field*)left_item)->depended_from &&
1301  !((Item_field*)right_item)->depended_from)
1302  {
1303  /* The predicate the form field1=field2 is processed */
1304 
1305  Field *left_field= ((Item_field*) left_item)->field;
1306  Field *right_field= ((Item_field*) right_item)->field;
1307 
1308  if (!left_field->eq_def(right_field))
1309  return FALSE;
1310 
1311  /* Search for multiple equalities containing field1 and/or field2 */
1312  bool left_copyfl, right_copyfl;
1313  Item_equal *left_item_equal=
1314  find_item_equal(cond_equal, left_field, &left_copyfl);
1315  Item_equal *right_item_equal=
1316  find_item_equal(cond_equal, right_field, &right_copyfl);
1317 
1318  /* As (NULL=NULL) != TRUE we can't just remove the predicate f=f */
1319  if (left_field->eq(right_field)) /* f = f */
1320  return (!(left_field->maybe_null() && !left_item_equal));
1321 
1322  if (left_item_equal && left_item_equal == right_item_equal)
1323  {
1324  /*
1325  The equality predicate is inference of one of the existing
1326  multiple equalities, i.e the condition is already covered
1327  by upper level equalities
1328  */
1329  return TRUE;
1330  }
1331 
1332  /* Copy the found multiple equalities at the current level if needed */
1333  if (left_copyfl)
1334  {
1335  /* left_item_equal of an upper level contains left_item */
1336  left_item_equal= new Item_equal(left_item_equal);
1337  cond_equal->current_level.push_back(left_item_equal);
1338  }
1339  if (right_copyfl)
1340  {
1341  /* right_item_equal of an upper level contains right_item */
1342  right_item_equal= new Item_equal(right_item_equal);
1343  cond_equal->current_level.push_back(right_item_equal);
1344  }
1345 
1346  if (left_item_equal)
1347  {
1348  /* left item was found in the current or one of the upper levels */
1349  if (! right_item_equal)
1350  left_item_equal->add((Item_field *) right_item);
1351  else
1352  {
1353  /* Merge two multiple equalities forming a new one */
1354  left_item_equal->merge(right_item_equal);
1355  /* Remove the merged multiple equality from the list */
1356  List_iterator<Item_equal> li(cond_equal->current_level);
1357  while ((li++) != right_item_equal) ;
1358  li.remove();
1359  }
1360  }
1361  else
1362  {
1363  /* left item was not found neither the current nor in upper levels */
1364  if (right_item_equal)
1365  {
1366  right_item_equal->add((Item_field *) left_item);
1367  }
1368  else
1369  {
1370  /* None of the fields was found in multiple equalities */
1371  Item_equal *item_equal= new Item_equal((Item_field *) left_item,
1372  (Item_field *) right_item);
1373  cond_equal->current_level.push_back(item_equal);
1374  }
1375  }
1376  return TRUE;
1377  }
1378 
1379  {
1380  /* The predicate of the form field=const/const=field is processed */
1381  Item *const_item= 0;
1382  Item_field *field_item= 0;
1383  if (left_item->type() == Item::FIELD_ITEM &&
1384  !((Item_field*)left_item)->depended_from &&
1385  right_item->const_item())
1386  {
1387  field_item= (Item_field*) left_item;
1388  const_item= right_item;
1389  }
1390  else if (right_item->type() == Item::FIELD_ITEM &&
1391  !((Item_field*)right_item)->depended_from &&
1392  left_item->const_item())
1393  {
1394  field_item= (Item_field*) right_item;
1395  const_item= left_item;
1396  }
1397 
1398  if (const_item &&
1399  field_item->result_type() == const_item->result_type())
1400  {
1401  bool copyfl;
1402 
1403  if (field_item->result_type() == STRING_RESULT)
1404  {
1405  const CHARSET_INFO *cs= field_item->field->charset();
1406  if (!item)
1407  {
1408  Item_func_eq *eq_item;
1409  if (!(eq_item= new Item_func_eq(left_item, right_item)) ||
1410  eq_item->set_cmp_func())
1411  return FALSE;
1412  eq_item->quick_fix_field();
1413  item= eq_item;
1414  }
1415  if ((cs != ((Item_func *) item)->compare_collation()) ||
1416  !cs->coll->propagate(cs, 0, 0))
1417  return FALSE;
1418  }
1419 
1420  Item_equal *item_equal = find_item_equal(cond_equal,
1421  field_item->field, &copyfl);
1422  if (copyfl)
1423  {
1424  item_equal= new Item_equal(item_equal);
1425  cond_equal->current_level.push_back(item_equal);
1426  }
1427  if (item_equal)
1428  {
1429  /*
1430  The flag cond_false will be set to 1 after this, if item_equal
1431  already contains a constant and its value is not equal to
1432  the value of const_item.
1433  */
1434  item_equal->add(const_item, field_item);
1435  }
1436  else
1437  {
1438  item_equal= new Item_equal(const_item, field_item);
1439  cond_equal->current_level.push_back(item_equal);
1440  }
1441  return TRUE;
1442  }
1443  }
1444  return FALSE;
1445 }
1446 
1447 
1474 static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
1475  COND_EQUAL *cond_equal, List<Item>* eq_list)
1476 {
1477  uint n= left_row->cols();
1478  for (uint i= 0 ; i < n; i++)
1479  {
1480  bool is_converted;
1481  Item *left_item= left_row->element_index(i);
1482  Item *right_item= right_row->element_index(i);
1483  if (left_item->type() == Item::ROW_ITEM &&
1484  right_item->type() == Item::ROW_ITEM)
1485  {
1486  is_converted= check_row_equality(thd,
1487  (Item_row *) left_item,
1488  (Item_row *) right_item,
1489  cond_equal, eq_list);
1490  if (!is_converted)
1491  thd->lex->current_select->cond_count++;
1492  }
1493  else
1494  {
1495  is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1496  thd->lex->current_select->cond_count++;
1497  }
1498 
1499  if (!is_converted)
1500  {
1501  Item_func_eq *eq_item;
1502  if (!(eq_item= new Item_func_eq(left_item, right_item)) ||
1503  eq_item->set_cmp_func())
1504  return FALSE;
1505  eq_item->quick_fix_field();
1506  eq_list->push_back(eq_item);
1507  }
1508  }
1509  return TRUE;
1510 }
1511 
1512 
1553 static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal,
1554  List<Item> *eq_list)
1555 {
1556  if (item->type() == Item::FUNC_ITEM &&
1557  ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
1558  {
1559  Item *left_item= ((Item_func*) item)->arguments()[0];
1560  Item *right_item= ((Item_func*) item)->arguments()[1];
1561 
1562  if (item->created_by_in2exists() && !left_item->const_item())
1563  return false; // See note above
1564 
1565  if (left_item->type() == Item::ROW_ITEM &&
1566  right_item->type() == Item::ROW_ITEM)
1567  {
1568  thd->lex->current_select->cond_count--;
1569  return check_row_equality(thd,
1570  (Item_row *) left_item,
1571  (Item_row *) right_item,
1572  cond_equal, eq_list);
1573  }
1574  else
1575  return check_simple_equality(left_item, right_item, item, cond_equal);
1576  }
1577 
1578  return FALSE;
1579 }
1580 
1581 
1648 static Item *build_equal_items_for_cond(THD *thd, Item *cond,
1649  COND_EQUAL *inherited,
1650  bool do_inherit)
1651 {
1652  Item_equal *item_equal;
1653  COND_EQUAL cond_equal;
1654  cond_equal.upper_levels= inherited;
1655 
1656  if (cond->type() == Item::COND_ITEM)
1657  {
1658  List<Item> eq_list;
1659  bool and_level= ((Item_cond*) cond)->functype() ==
1660  Item_func::COND_AND_FUNC;
1661  List<Item> *args= ((Item_cond*) cond)->argument_list();
1662 
1663  List_iterator<Item> li(*args);
1664  Item *item;
1665 
1666  if (and_level)
1667  {
1668  /*
1669  Retrieve all conjuncts of this level detecting the equality
1670  that are subject to substitution by multiple equality items and
1671  removing each such predicate from the conjunction after having
1672  found/created a multiple equality whose inference the predicate is.
1673  */
1674  while ((item= li++))
1675  {
1676  /*
1677  PS/SP note: we can safely remove a node from AND-OR
1678  structure here because it's restored before each
1679  re-execution of any prepared statement/stored procedure.
1680  */
1681  if (check_equality(thd, item, &cond_equal, &eq_list))
1682  li.remove();
1683  }
1684 
1685  /*
1686  Check if we eliminated all the predicates of the level, e.g.
1687  (a=a AND b=b AND a=a).
1688  */
1689  if (!args->elements &&
1690  !cond_equal.current_level.elements &&
1691  !eq_list.elements)
1692  return new Item_int((longlong) 1, 1);
1693 
1694  List_iterator_fast<Item_equal> it(cond_equal.current_level);
1695  while ((item_equal= it++))
1696  {
1697  item_equal->fix_length_and_dec();
1698  item_equal->update_used_tables();
1699  set_if_bigger(thd->lex->current_select->max_equal_elems,
1700  item_equal->members());
1701  }
1702 
1703  ((Item_cond_and*)cond)->cond_equal= cond_equal;
1704  inherited= &(((Item_cond_and*)cond)->cond_equal);
1705  }
1706  /*
1707  Make replacement of equality predicates for lower levels
1708  of the condition expression.
1709  */
1710  li.rewind();
1711  while ((item= li++))
1712  {
1713  Item *new_item=
1714  build_equal_items_for_cond(thd, item, inherited, do_inherit);
1715  if (new_item != item)
1716  {
1717  /* This replacement happens only for standalone equalities */
1718  /*
1719  This is ok with PS/SP as the replacement is done for
1720  arguments of an AND/OR item, which are restored for each
1721  execution of PS/SP.
1722  */
1723  li.replace(new_item);
1724  }
1725  }
1726  if (and_level)
1727  {
1728  args->concat(&eq_list);
1729  args->concat((List<Item> *)&cond_equal.current_level);
1730  }
1731  }
1732  else if (cond->type() == Item::FUNC_ITEM)
1733  {
1734  List<Item> eq_list;
1735  /*
1736  If an equality predicate forms the whole and level,
1737  we call it standalone equality and it's processed here.
1738  E.g. in the following where condition
1739  WHERE a=5 AND (b=5 or a=c)
1740  (b=5) and (a=c) are standalone equalities.
1741  In general we can't leave alone standalone eqalities:
1742  for WHERE a=b AND c=d AND (b=c OR d=5)
1743  b=c is replaced by =(a,b,c,d).
1744  */
1745  if (check_equality(thd, cond, &cond_equal, &eq_list))
1746  {
1747  int n= cond_equal.current_level.elements + eq_list.elements;
1748  if (n == 0)
1749  return new Item_int((longlong) 1,1);
1750  else if (n == 1)
1751  {
1752  if ((item_equal= cond_equal.current_level.pop()))
1753  {
1754  item_equal->fix_length_and_dec();
1755  item_equal->update_used_tables();
1756  set_if_bigger(thd->lex->current_select->max_equal_elems,
1757  item_equal->members());
1758  return item_equal;
1759  }
1760 
1761  return eq_list.pop();
1762  }
1763  else
1764  {
1765  /*
1766  Here a new AND level must be created. It can happen only
1767  when a row equality is processed as a standalone predicate.
1768  */
1769  Item_cond_and *and_cond= new Item_cond_and(eq_list);
1770  and_cond->quick_fix_field();
1771  List<Item> *args= and_cond->argument_list();
1772  List_iterator_fast<Item_equal> it(cond_equal.current_level);
1773  while ((item_equal= it++))
1774  {
1775  item_equal->fix_length_and_dec();
1776  item_equal->update_used_tables();
1777  set_if_bigger(thd->lex->current_select->max_equal_elems,
1778  item_equal->members());
1779  }
1780  and_cond->cond_equal= cond_equal;
1781  args->concat((List<Item> *)&cond_equal.current_level);
1782 
1783  return and_cond;
1784  }
1785  }
1786 
1787  if (do_inherit)
1788  {
1789  /*
1790  For each field reference in cond, not from equal item predicates,
1791  set a pointer to the multiple equality it belongs to (if there is any)
1792  as soon the field is not of a string type or the field reference is
1793  an argument of a comparison predicate.
1794  */
1795  uchar *is_subst_valid= (uchar *) 1;
1796  cond= cond->compile(&Item::subst_argument_checker,
1797  &is_subst_valid,
1798  &Item::equal_fields_propagator,
1799  (uchar *) inherited);
1800  }
1801  cond->update_used_tables();
1802  }
1803  return cond;
1804 }
1805 
1806 
1875 Item *build_equal_items(THD *thd, Item *cond, COND_EQUAL *inherited,
1876  bool do_inherit, List<TABLE_LIST> *join_list,
1877  COND_EQUAL **cond_equal_ref)
1878 {
1879  COND_EQUAL *cond_equal= 0;
1880 
1881  if (cond)
1882  {
1883  cond= build_equal_items_for_cond(thd, cond, inherited, do_inherit);
1884  cond->update_used_tables();
1885  if (cond->type() == Item::COND_ITEM &&
1886  ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
1887  cond_equal= &((Item_cond_and*) cond)->cond_equal;
1888  else if (cond->type() == Item::FUNC_ITEM &&
1889  ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
1890  {
1891  cond_equal= new COND_EQUAL;
1892  cond_equal->current_level.push_back((Item_equal *) cond);
1893  }
1894  }
1895  if (cond_equal)
1896  {
1897  cond_equal->upper_levels= inherited;
1898  inherited= cond_equal;
1899  }
1900  *cond_equal_ref= cond_equal;
1901 
1902  if (join_list)
1903  {
1904  TABLE_LIST *table;
1905  List_iterator<TABLE_LIST> li(*join_list);
1906 
1907  while ((table= li++))
1908  {
1909  if (table->join_cond())
1910  {
1911  List<TABLE_LIST> *nested_join_list= table->nested_join ?
1912  &table->nested_join->join_list : NULL;
1913  /*
1914  We can modify table->join_cond() because its old value will
1915  be restored before re-execution of PS/SP.
1916  */
1917  table->set_join_cond(build_equal_items(thd, table->join_cond(),
1918  inherited, do_inherit,
1919  nested_join_list,
1920  &table->cond_equal));
1921  }
1922  }
1923  }
1924 
1925  return cond;
1926 }
1927 
1928 
1949 static int compare_fields_by_table_order(Item_field *field1,
1950  Item_field *field2,
1951  void *table_join_idx)
1952 {
1953  int cmp= 0;
1954  bool outer_ref= 0;
1955  if (field1->used_tables() & OUTER_REF_TABLE_BIT)
1956  {
1957  outer_ref= 1;
1958  cmp= -1;
1959  }
1960  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
1961  {
1962  outer_ref= 1;
1963  cmp++;
1964  }
1965  if (outer_ref)
1966  return cmp;
1967  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
1968 
1969  /*
1970  idx is NULL if this function was not called from JOIN::optimize()
1971  but from e.g. mysql_delete() or mysql_update(). In these cases
1972  there is only one table and both fields belong to it. Example
1973  condition where this is the case: t1.fld1=t1.fld2
1974  */
1975  if (!idx)
1976  return 0;
1977 
1978  cmp= idx[field1->field->table->tablenr]-idx[field2->field->table->tablenr];
1979  return cmp < 0 ? -1 : (cmp ? 1 : 0);
1980 }
1981 
1982 
2023 static Item *eliminate_item_equal(Item *cond, COND_EQUAL *upper_levels,
2024  Item_equal *item_equal)
2025 {
2026  List<Item> eq_list;
2027  Item_func_eq *eq_item= NULL;
2028  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
2029  return new Item_int((longlong) 0,1);
2030  Item *const item_const= item_equal->get_const();
2031  Item_equal_iterator it(*item_equal);
2032  if (!item_const)
2033  {
2034  /*
2035  If there is a const item, match all field items with the const item,
2036  otherwise match the second and subsequent field items with the first one:
2037  */
2038  it++;
2039  }
2040  Item_field *item_field; // Field to generate equality for.
2041  while ((item_field= it++))
2042  {
2043  /*
2044  Generate an equality of the form:
2045  item_field = some previous field in item_equal's list.
2046 
2047  First see if we really need to generate it:
2048  */
2049  Item_equal *const upper= item_field->find_item_equal(upper_levels);
2050  if (upper) // item_field is in this upper equality
2051  {
2052  if (item_const && upper->get_const())
2053  continue; // Const at both levels, no need to generate at current level
2054  /*
2055  If the upper-level multiple equality contains this item, there is no
2056  need to generate the equality, unless item_field belongs to a
2057  semi-join nest that is used for Materialization, and refers to tables
2058  that are outside of the materialized semi-join nest,
2059  As noted in Item_equal::get_subst_item(), subquery materialization
2060  does not have this problem.
2061  */
2062  if (!sj_is_materialize_strategy(
2063  item_field->field->table->reginfo.join_tab->get_sj_strategy()))
2064  {
2065  Item_field *item_match;
2066  Item_equal_iterator li(*item_equal);
2067  while ((item_match= li++) != item_field)
2068  {
2069  if (item_match->find_item_equal(upper_levels) == upper)
2070  break; // (item_match, item_field) is also in upper level equality
2071  }
2072  if (item_match != item_field)
2073  continue;
2074  }
2075  } // ... if (upper).
2076 
2077  /*
2078  item_field should be compared with the head of the multiple equality
2079  list.
2080  item_field may refer to a table that is within a semijoin materialization
2081  nest. In that case, the order of the join_tab entries may look like:
2082 
2083  ot1 ot2 <subquery> ot5 SJM(it3 it4)
2084 
2085  If we have a multiple equality
2086 
2087  (ot1.c1, ot2.c2, <subquery>.c it3.c3, it4.c4, ot5.c5),
2088 
2089  we should generate the following equalities:
2090  1. ot1.c1 = ot2.c2
2091  2. ot1.c1 = <subquery>.c
2092  3. it3.c3 = it4.c4
2093  4. ot1.c1 = ot5.c5
2094 
2095  Equalities 1) and 4) are regular equalities between two outer tables.
2096  Equality 2) is an equality that matches the outer query with a
2097  materialized temporary table. It is either performed as a lookup
2098  into the materialized table (SJM-lookup), or as a condition on the
2099  outer table (SJM-scan).
2100  Equality 3) is evaluated during semijoin materialization.
2101 
2102  If there is a const item, match against this one.
2103  Otherwise, match against the first field item in the multiple equality,
2104  unless the item is within a materialized semijoin nest, in case it will
2105  be matched against the first item within the SJM nest.
2106  @see JOIN::set_access_methods()
2107  @see JOIN::set_prefix_tables()
2108  @see Item_equal::get_subst_item()
2109  */
2110 
2111  Item *const head=
2112  item_const ? item_const : item_equal->get_subst_item(item_field);
2113  if (head == item_field)
2114  continue;
2115 
2116  // we have a pair, can generate 'item_field=head'
2117  if (eq_item)
2118  eq_list.push_back(eq_item);
2119 
2120  eq_item= new Item_func_eq(item_field, head);
2121  if (!eq_item || eq_item->set_cmp_func())
2122  return NULL;
2123  eq_item->quick_fix_field();
2124  } // ... while ((item_field= it++))
2125 
2126  if (!cond && !eq_list.head())
2127  {
2128  if (!eq_item)
2129  return new Item_int((longlong) 1,1);
2130  return eq_item;
2131  }
2132 
2133  if (eq_item)
2134  eq_list.push_back(eq_item);
2135  if (!cond)
2136  cond= new Item_cond_and(eq_list);
2137  else
2138  {
2139  DBUG_ASSERT(cond->type() == Item::COND_ITEM);
2140  if (eq_list.elements)
2141  ((Item_cond *) cond)->add_at_head(&eq_list);
2142  }
2143 
2144  cond->quick_fix_field();
2145  cond->update_used_tables();
2146 
2147  return cond;
2148 }
2149 
2150 
2181  COND_EQUAL *cond_equal,
2182  void *table_join_idx)
2183 {
2184  Item_equal *item_equal;
2185 
2186  if (cond->type() == Item::COND_ITEM)
2187  {
2188  List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
2189 
2190  bool and_level= ((Item_cond*) cond)->functype() ==
2191  Item_func::COND_AND_FUNC;
2192  if (and_level)
2193  {
2194  cond_equal= &((Item_cond_and *) cond)->cond_equal;
2195  cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2196 
2197  List_iterator_fast<Item_equal> it(cond_equal->current_level);
2198  while ((item_equal= it++))
2199  {
2200  item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2201  }
2202  }
2203 
2204  List_iterator<Item> li(*cond_list);
2205  Item *item;
2206  while ((item= li++))
2207  {
2208  Item *new_item =substitute_for_best_equal_field(item, cond_equal,
2209  table_join_idx);
2210  /*
2211  This works OK with PS/SP re-execution as changes are made to
2212  the arguments of AND/OR items only
2213  */
2214  if (new_item != item)
2215  li.replace(new_item);
2216  }
2217 
2218  if (and_level)
2219  {
2220  List_iterator_fast<Item_equal> it(cond_equal->current_level);
2221  while ((item_equal= it++))
2222  {
2223  cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
2224  if (cond == NULL)
2225  return NULL;
2226  // This occurs when eliminate_item_equal() founds that cond is
2227  // always false and substitutes it with Item_int 0.
2228  // Due to this, value of item_equal will be 0, so just return it.
2229  if (cond->type() != Item::COND_ITEM)
2230  break;
2231  }
2232  }
2233  if (cond->type() == Item::COND_ITEM &&
2234  !((Item_cond*)cond)->argument_list()->elements)
2235  cond= new Item_int((int32)cond->val_bool());
2236 
2237  }
2238  else if (cond->type() == Item::FUNC_ITEM &&
2239  ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2240  {
2241  item_equal= (Item_equal *) cond;
2242  item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2243  if (cond_equal && cond_equal->current_level.head() == item_equal)
2244  cond_equal= cond_equal->upper_levels;
2245  return eliminate_item_equal(0, cond_equal, item_equal);
2246  }
2247  else
2248  cond->transform(&Item::replace_equal_field, 0);
2249  return cond;
2250 }
2251 
2252 
2253 /*
2254  change field = field to field = const for each found field = const in the
2255  and_level
2256 */
2257 
2258 static void
2259 change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
2260  Item *and_father, Item *cond,
2261  Item *field, Item *value)
2262 {
2263  if (cond->type() == Item::COND_ITEM)
2264  {
2265  bool and_level= ((Item_cond*) cond)->functype() ==
2266  Item_func::COND_AND_FUNC;
2267  List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2268  Item *item;
2269  while ((item=li++))
2270  change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item,
2271  field, value);
2272  return;
2273  }
2274  if (cond->eq_cmp_result() == Item::COND_OK)
2275  return; // Not a boolean function
2276 
2277  Item_bool_func2 *func= (Item_bool_func2*) cond;
2278  Item **args= func->arguments();
2279  Item *left_item= args[0];
2280  Item *right_item= args[1];
2281  Item_func::Functype functype= func->functype();
2282 
2283  if (right_item->eq(field,0) && left_item != value &&
2284  right_item->cmp_context == field->cmp_context &&
2285  (left_item->result_type() != STRING_RESULT ||
2286  value->result_type() != STRING_RESULT ||
2287  left_item->collation.collation == value->collation.collation))
2288  {
2289  Item *tmp=value->clone_item();
2290  if (tmp)
2291  {
2292  tmp->collation.set(right_item->collation);
2293  thd->change_item_tree(args + 1, tmp);
2294  func->update_used_tables();
2295  if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
2296  && and_father != cond && !left_item->const_item())
2297  {
2298  cond->marker=1;
2299  COND_CMP *tmp2;
2300  if ((tmp2=new COND_CMP(and_father,func)))
2301  save_list->push_back(tmp2);
2302  }
2303  func->set_cmp_func();
2304  }
2305  }
2306  else if (left_item->eq(field,0) && right_item != value &&
2307  left_item->cmp_context == field->cmp_context &&
2308  (right_item->result_type() != STRING_RESULT ||
2309  value->result_type() != STRING_RESULT ||
2310  right_item->collation.collation == value->collation.collation))
2311  {
2312  Item *tmp= value->clone_item();
2313  if (tmp)
2314  {
2315  tmp->collation.set(left_item->collation);
2316  thd->change_item_tree(args, tmp);
2317  value= tmp;
2318  func->update_used_tables();
2319  if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
2320  && and_father != cond && !right_item->const_item())
2321  {
2322  args[0]= args[1]; // For easy check
2323  thd->change_item_tree(args + 1, value);
2324  cond->marker=1;
2325  COND_CMP *tmp2;
2326  if ((tmp2=new COND_CMP(and_father,func)))
2327  save_list->push_back(tmp2);
2328  }
2329  func->set_cmp_func();
2330  }
2331  }
2332 }
2333 
2334 static void
2335 propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list,
2336  Item *and_father, Item *cond)
2337 {
2338  if (cond->type() == Item::COND_ITEM)
2339  {
2340  bool and_level= ((Item_cond*) cond)->functype() ==
2341  Item_func::COND_AND_FUNC;
2342  List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2343  Item *item;
2344  I_List<COND_CMP> save;
2345  while ((item=li++))
2346  {
2347  propagate_cond_constants(thd, &save,and_level ? cond : item, item);
2348  }
2349  if (and_level)
2350  { // Handle other found items
2351  I_List_iterator<COND_CMP> cond_itr(save);
2352  COND_CMP *cond_cmp;
2353  while ((cond_cmp=cond_itr++))
2354  {
2355  Item **args= cond_cmp->cmp_func->arguments();
2356  if (!args[0]->const_item())
2357  change_cond_ref_to_const(thd, &save,cond_cmp->and_level,
2358  cond_cmp->and_level, args[0], args[1]);
2359  }
2360  }
2361  }
2362  else if (and_father != cond && !cond->marker) // In a AND group
2363  {
2364  if (cond->type() == Item::FUNC_ITEM &&
2365  (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2366  ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2367  {
2368  Item_func_eq *func=(Item_func_eq*) cond;
2369  Item **args= func->arguments();
2370  bool left_const= args[0]->const_item();
2371  bool right_const= args[1]->const_item();
2372  if (!(left_const && right_const) &&
2373  args[0]->result_type() == args[1]->result_type())
2374  {
2375  if (right_const)
2376  {
2377  resolve_const_item(thd, &args[1], args[0]);
2378  func->update_used_tables();
2379  change_cond_ref_to_const(thd, save_list, and_father, and_father,
2380  args[0], args[1]);
2381  }
2382  else if (left_const)
2383  {
2384  resolve_const_item(thd, &args[0], args[1]);
2385  func->update_used_tables();
2386  change_cond_ref_to_const(thd, save_list, and_father, and_father,
2387  args[1], args[0]);
2388  }
2389  }
2390  }
2391  }
2392 }
2393 
2394 
2519 static bool
2520 simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, Item *conds, bool top,
2521  bool in_sj, Item **new_conds, uint *changelog)
2522 {
2523 
2524  /*
2525  Each type of change done by this function, or its recursive calls, is
2526  tracked in a bitmap:
2527  */
2528  enum change
2529  {
2530  NONE= 0,
2531  OUTER_JOIN_TO_INNER= 1 << 0,
2532  JOIN_COND_TO_WHERE= 1 << 1,
2533  PAREN_REMOVAL= 1 << 2,
2534  SEMIJOIN= 1 << 3
2535  };
2536  uint changes= 0; // To keep track of changes.
2537  if (changelog == NULL) // This is the top call.
2538  changelog= &changes;
2539 
2540  TABLE_LIST *table;
2541  NESTED_JOIN *nested_join;
2542  TABLE_LIST *prev_table= 0;
2543  List_iterator<TABLE_LIST> li(*join_list);
2544  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
2545  DBUG_ENTER("simplify_joins");
2546 
2547  /*
2548  Try to simplify join operations from join_list.
2549  The most outer join operation is checked for conversion first.
2550  */
2551  while ((table= li++))
2552  {
2553  table_map used_tables;
2554  table_map not_null_tables= (table_map) 0;
2555 
2556  if ((nested_join= table->nested_join))
2557  {
2558  /*
2559  If the element of join_list is a nested join apply
2560  the procedure to its nested join list first.
2561  */
2562  if (table->join_cond())
2563  {
2564  Item *join_cond= table->join_cond();
2565  /*
2566  If a join condition JC is attached to the table,
2567  check all null rejected predicates in this condition.
2568  If such a predicate over an attribute belonging to
2569  an inner table of an embedded outer join is found,
2570  the outer join is converted to an inner join and
2571  the corresponding join condition is added to JC.
2572  */
2573  if (simplify_joins(join, &nested_join->join_list,
2574  join_cond, false, in_sj || table->sj_on_expr,
2575  &join_cond, changelog))
2576  DBUG_RETURN(true);
2577 
2578  if (join_cond != table->join_cond())
2579  {
2580  DBUG_ASSERT(join_cond);
2581 
2582  table->set_join_cond(join_cond);
2583  }
2584  }
2585  nested_join->used_tables= (table_map) 0;
2586  nested_join->not_null_tables=(table_map) 0;
2587  if (simplify_joins(join, &nested_join->join_list, conds, top,
2588  in_sj || table->sj_on_expr, &conds, changelog))
2589  DBUG_RETURN(true);
2590  used_tables= nested_join->used_tables;
2591  not_null_tables= nested_join->not_null_tables;
2592  }
2593  else
2594  {
2595  used_tables= table->table->map;
2596  if (conds)
2597  not_null_tables= conds->not_null_tables();
2598  }
2599 
2600  if (table->embedding)
2601  {
2602  table->embedding->nested_join->used_tables|= used_tables;
2603  table->embedding->nested_join->not_null_tables|= not_null_tables;
2604  }
2605 
2606  if (!table->outer_join || (used_tables & not_null_tables))
2607  {
2608  /*
2609  For some of the inner tables there are conjunctive predicates
2610  that reject nulls => the outer join can be replaced by an inner join.
2611  */
2612  if (table->outer_join)
2613  {
2614  *changelog|= OUTER_JOIN_TO_INNER;
2615  table->outer_join= 0;
2616  }
2617  if (table->join_cond())
2618  {
2619  *changelog|= JOIN_COND_TO_WHERE;
2620  /* Add join condition to the WHERE or upper-level join condition. */
2621  if (conds)
2622  {
2623  Item_cond_and *new_cond=
2624  static_cast<Item_cond_and*>(and_conds(conds, table->join_cond()));
2625  if (!new_cond)
2626  DBUG_RETURN(true);
2627  conds= new_cond;
2628  conds->top_level_item();
2629  /*
2630  conds is always a new item as both the upper-level condition and a
2631  join condition existed
2632  */
2633  DBUG_ASSERT(!conds->fixed);
2634  if (conds->fix_fields(join->thd, &conds))
2635  DBUG_RETURN(true);
2636 
2637  /* If join condition has a pending rollback in THD::change_list */
2638  List_iterator<Item> lit(*new_cond->argument_list());
2639  Item *arg;
2640  while ((arg= lit++))
2641  {
2642  /*
2643  The join condition isn't necessarily the second argument anymore,
2644  since fix_fields may have merged it into an existing AND expr.
2645  */
2646  if (arg == table->join_cond())
2647  join->thd->
2648  change_item_tree_place(table->join_cond_ref(), lit.ref());
2649  }
2650  }
2651  else
2652  {
2653  conds= table->join_cond();
2654  /* If join condition has a pending rollback in THD::change_list */
2655  join->thd->change_item_tree_place(table->join_cond_ref(), &conds);
2656  }
2657  table->set_join_cond(NULL);
2658  }
2659  }
2660 
2661  if (!top)
2662  continue;
2663 
2664  /*
2665  Only inner tables of non-convertible outer joins remain with
2666  the join condition.
2667  */
2668  if (table->join_cond())
2669  {
2670  table->dep_tables|= table->join_cond()->used_tables();
2671  if (table->embedding)
2672  {
2673  table->dep_tables&= ~table->embedding->nested_join->used_tables;
2674 
2675  // Embedding table depends on tables used in embedded join conditions.
2676  table->embedding->on_expr_dep_tables|=
2677  table->join_cond()->used_tables();
2678  }
2679  else
2680  table->dep_tables&= ~table->table->map;
2681  }
2682 
2683  if (prev_table)
2684  {
2685  /* The order of tables is reverse: prev_table follows table */
2686  if (prev_table->straight || straight_join)
2687  prev_table->dep_tables|= used_tables;
2688  if (prev_table->join_cond())
2689  {
2690  prev_table->dep_tables|= table->on_expr_dep_tables;
2691  table_map prev_used_tables= prev_table->nested_join ?
2692  prev_table->nested_join->used_tables :
2693  prev_table->table->map;
2694  /*
2695  If join condition contains only references to inner tables
2696  we still make the inner tables dependent on the outer tables.
2697  It would be enough to set dependency only on one outer table
2698  for them. Yet this is really a rare case.
2699  Note:
2700  RAND_TABLE_BIT mask should not be counted as it
2701  prevents update of inner table dependences.
2702  For example it might happen if RAND() function
2703  is used in JOIN ON clause.
2704  */
2705  if (!((prev_table->join_cond()->used_tables() & ~RAND_TABLE_BIT) &
2706  ~prev_used_tables))
2707  prev_table->dep_tables|= used_tables;
2708  }
2709  }
2710  prev_table= table;
2711  }
2712 
2713  /*
2714  Flatten nested joins that can be flattened.
2715  no join condition and not a semi-join => can be flattened.
2716  */
2717  li.rewind();
2718  while ((table= li++))
2719  {
2720  nested_join= table->nested_join;
2721  if (table->sj_on_expr && !in_sj)
2722  {
2723  /*
2724  If this is a semi-join that is not contained within another semi-join,
2725  leave it intact (otherwise it is flattened)
2726  */
2727  *changelog|= SEMIJOIN;
2728  }
2729  else if (nested_join && !table->join_cond())
2730  {
2731  *changelog|= PAREN_REMOVAL;
2732  TABLE_LIST *tbl;
2733  List_iterator<TABLE_LIST> it(nested_join->join_list);
2734  while ((tbl= it++))
2735  {
2736  tbl->embedding= table->embedding;
2737  tbl->join_list= table->join_list;
2738  tbl->dep_tables|= table->dep_tables;
2739  }
2740  li.replace(nested_join->join_list);
2741  }
2742  }
2743  *new_conds= conds;
2744 
2745  if (changes)
2746  {
2747  Opt_trace_context * trace= &join->thd->opt_trace;
2748  if (unlikely(trace->is_started()))
2749  {
2750  Opt_trace_object trace_wrapper(trace);
2751  Opt_trace_object trace_object(trace, "transformations_to_nested_joins");
2752  {
2753  Opt_trace_array trace_changes(trace, "transformations");
2754  if (changes & SEMIJOIN)
2755  trace_changes.add_alnum("semijoin");
2756  if (changes & OUTER_JOIN_TO_INNER)
2757  trace_changes.add_alnum("outer_join_to_inner_join");
2758  if (changes & JOIN_COND_TO_WHERE)
2759  trace_changes.add_alnum("JOIN_condition_to_WHERE");
2760  if (changes & PAREN_REMOVAL)
2761  trace_changes.add_alnum("parenthesis_removal");
2762  }
2763  // the newly transformed query is worth printing
2764  opt_trace_print_expanded_query(join->thd, join->select_lex,
2765  &trace_object);
2766  }
2767  }
2768  DBUG_RETURN(false);
2769 }
2770 
2771 
2788 static bool record_join_nest_info(st_select_lex *select,
2789  List<TABLE_LIST> *tables)
2790 
2791 {
2792  TABLE_LIST *table;
2793  List_iterator<TABLE_LIST> li(*tables);
2794  DBUG_ENTER("record_join_nest_info");
2795 
2796  while ((table= li++))
2797  {
2798  table->prep_join_cond= table->join_cond() ?
2799  table->join_cond()->copy_andor_structure(select->join->thd, true) : NULL;
2800 
2801  if (table->nested_join == NULL)
2802  continue;
2803 
2804  if (record_join_nest_info(select, &table->nested_join->join_list))
2805  DBUG_RETURN(true);
2806  /*
2807  sj_inner_tables is set properly later in pull_out_semijoin_tables().
2808  This assignment is required in case pull_out_semijoin_tables()
2809  is not called.
2810  */
2811  if (table->sj_on_expr)
2812  table->sj_inner_tables= table->nested_join->used_tables;
2813  if (table->sj_on_expr && select->sj_nests.push_back(table))
2814  DBUG_RETURN(true);
2815  }
2816  DBUG_RETURN(false);
2817 }
2818 
2819 
2839 static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
2840  uint first_unused)
2841 {
2842  List_iterator<TABLE_LIST> li(*join_list);
2843  TABLE_LIST *table;
2844  DBUG_ENTER("build_bitmap_for_nested_joins");
2845  while ((table= li++))
2846  {
2847  NESTED_JOIN *nested_join;
2848  if ((nested_join= table->nested_join))
2849  {
2850  // We should have either a join condition or a semi-join condition
2851  DBUG_ASSERT((table->join_cond() == NULL) == (table->sj_on_expr != NULL));
2852 
2853  nested_join->nj_map= 0;
2854  nested_join->nj_total= 0;
2855  /*
2856  We only record nested join information for outer join nests.
2857  Tables belonging in semi-join nests are recorded in the
2858  embedding outer join nest, if one exists.
2859  */
2860  if (table->join_cond())
2861  {
2862  DBUG_ASSERT(first_unused < sizeof(nested_join_map)*8);
2863  nested_join->nj_map= (nested_join_map) 1 << first_unused++;
2864  nested_join->nj_total= nested_join->join_list.elements;
2865  }
2866  else if (table->sj_on_expr)
2867  {
2868  NESTED_JOIN *const outer_nest=
2869  table->embedding ? table->embedding->nested_join : NULL;
2870  /*
2871  The semi-join nest has already been counted into the table count
2872  for the outer join nest as one table, so subtract 1 from the
2873  table count.
2874  */
2875  if (outer_nest)
2876  outer_nest->nj_total+= (nested_join->join_list.elements - 1);
2877  }
2878  else
2879  DBUG_ASSERT(false);
2880 
2881  first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
2882  first_unused);
2883  }
2884  }
2885  DBUG_RETURN(first_unused);
2886 }
2887 
2888 
2892 {
2893  for (uint tableno = 0; tableno < join->tables; tableno++)
2894  {
2895  JOIN_TAB *const join_tab= join->join_tab + tableno;
2896  TABLE_REF *const ref= &join_tab->ref;
2897  table_map depend_map=0;
2898  Item **item=ref->items;
2899  uint i;
2900  for (i=0 ; i < ref->key_parts ; i++,item++)
2901  depend_map|=(*item)->used_tables();
2902  depend_map&= ~PSEUDO_TABLE_BITS;
2903  ref->depend_map= depend_map;
2904  for (JOIN_TAB **tab=join->map2table;
2905  depend_map ;
2906  tab++,depend_map>>=1 )
2907  {
2908  if (depend_map & 1)
2909  ref->depend_map|=(*tab)->ref.depend_map;
2910  }
2911  }
2912 }
2913 
2914 
2917 static void update_depend_map(JOIN *join, ORDER *order)
2918 {
2919  for (; order ; order=order->next)
2920  {
2921  table_map depend_map;
2922  order->item[0]->update_used_tables();
2923  order->depend_map=depend_map=order->item[0]->used_tables();
2924  order->used= 0;
2925  // Not item_sum(), RAND() and no reference to table outside of sub select
2926  if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
2927  && !order->item[0]->with_sum_func)
2928  {
2929  for (JOIN_TAB **tab=join->map2table;
2930  depend_map ;
2931  tab++, depend_map>>=1)
2932  {
2933  if (depend_map & 1)
2934  order->depend_map|=(*tab)->ref.depend_map;
2935  }
2936  }
2937  }
2938 }
2939 
2940 
2974 {
2975  List_iterator<Semijoin_mat_exec> it(sjm_exec_list);
2976  Semijoin_mat_exec *sjm_exec;
2977  while ((sjm_exec= it++))
2978  {
2979  TABLE_LIST *const sj_nest= sjm_exec->sj_nest;
2980 
2981  DBUG_ASSERT(!sj_nest->outer_join_nest());
2982  /*
2983  A materialized semi-join nest cannot actually be an inner part of an
2984  outer join yet, this is just a preparatory step,
2985  ie sj_nest->outer_join_nest() is always NULL here.
2986  @todo: Enable outer joining here later.
2987  */
2988  Item *cond= sj_nest->outer_join_nest() ?
2989  sj_nest->outer_join_nest()->join_cond() :
2990  conds;
2991  if (!cond)
2992  continue;
2993 
2994  uchar *dummy= NULL;
2995  cond= cond->compile(&Item::equality_substitution_analyzer, &dummy,
2996  &Item::equality_substitution_transformer,
2997  (uchar *)sj_nest);
2998  if (cond == NULL)
2999  return true;
3000 
3001  cond->update_used_tables();
3002 
3003  // Loop over all primary tables that follow the materialized table
3004  for (uint j= sjm_exec->mat_table_index + 1; j < primary_tables; j++)
3005  {
3006  JOIN_TAB *const tab= join_tab + j;
3007  for (Key_use *keyuse= tab->position->key;
3008  keyuse && keyuse->table == tab->table &&
3009  keyuse->key == tab->position->key->key;
3010  keyuse++)
3011  {
3012  List_iterator<Item> it(sj_nest->nested_join->sj_inner_exprs);
3013  Item *old;
3014  uint fieldno= 0;
3015  while ((old= it++))
3016  {
3017  if (old->real_item()->eq(keyuse->val, false))
3018  {
3019  /*
3020  Replace the expression selected from the subquery with the
3021  corresponding column of the materialized temporary table.
3022  */
3023  keyuse->val= sj_nest->nested_join->sjm.mat_fields[fieldno];
3024  keyuse->used_tables= keyuse->val->used_tables();
3025  break;
3026  }
3027  fieldno++;
3028  }
3029  }
3030  }
3031  }
3032 
3033  return false;
3034 }
3035 
3036 
3045 void JOIN::set_prefix_tables()
3046 {
3047  DBUG_ASSERT(!plan_is_const());
3048  /*
3049  The const tables are available together with the first non-const table in
3050  the join order.
3051  */
3052  table_map const initial_tables_map= const_table_map |
3053  (allow_outer_refs ? OUTER_REF_TABLE_BIT : 0);
3054 
3055  table_map current_tables_map= initial_tables_map;
3056  table_map prev_tables_map= (table_map) 0;
3057  table_map saved_tables_map= (table_map) 0;
3058 
3059  JOIN_TAB *last_non_sjm_tab= NULL; // Track the last non-sjm table
3060 
3061  for (uint i= const_tables; i < tables; i++)
3062  {
3063  JOIN_TAB *const tab= join_tab + i;
3064  if (!tab->table)
3065  continue;
3066  /*
3067  Tables that are within SJ-Materialization nests cannot have their
3068  conditions referring to preceding non-const tables.
3069  - If we're looking at the first SJM table, reset current_tables_map
3070  to refer to only allowed tables
3071  @see Item_equal::get_subst_item()
3072  @see eliminate_item_equal()
3073  */
3074  if (sj_is_materialize_strategy(tab->get_sj_strategy()))
3075  {
3076  const table_map sjm_inner_tables= tab->emb_sj_nest->sj_inner_tables;
3077  if (!(sjm_inner_tables & current_tables_map))
3078  {
3079  saved_tables_map= current_tables_map;
3080  current_tables_map= initial_tables_map;
3081  prev_tables_map= (table_map) 0;
3082  }
3083 
3084  current_tables_map|= tab->table->map;
3085  tab->set_prefix_tables(current_tables_map, prev_tables_map);
3086  prev_tables_map= current_tables_map;
3087 
3088  if (!(sjm_inner_tables & ~current_tables_map))
3089  {
3090  // At the end of a semi-join materialization nest, restore previous map
3091  current_tables_map= saved_tables_map;
3092  prev_tables_map= last_non_sjm_tab ?
3093  last_non_sjm_tab->prefix_tables() : (table_map) 0;
3094  }
3095  }
3096  else
3097  {
3098  last_non_sjm_tab= tab;
3099  current_tables_map|= tab->table->map;
3100  tab->set_prefix_tables(current_tables_map, prev_tables_map);
3101  prev_tables_map= current_tables_map;
3102  }
3103  }
3104  /*
3105  Random expressions must be added to the last table's condition.
3106  It solves problem with queries like SELECT * FROM t1 WHERE rand() > 0.5
3107  */
3108  if (last_non_sjm_tab != NULL)
3109  last_non_sjm_tab->add_prefix_tables(RAND_TABLE_BIT);
3110 }
3111 
3112 
3154 static bool
3155 make_join_statistics(JOIN *join, TABLE_LIST *tables_arg, Item *conds,
3156  Key_use_array *keyuse_array, bool first_optimization)
3157 {
3158  int error;
3159  THD *const thd= join->thd;
3160  TABLE_LIST *tables= tables_arg;
3161  uint i,const_count,key;
3162  const uint table_count= join->tables;
3163  table_map found_ref, refs;
3164  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
3165  Key_use *keyuse, *start_keyuse;
3166  table_map outer_join= 0;
3167  SARGABLE_PARAM *sargables= 0;
3168  JOIN_TAB *stat_vector[MAX_TABLES+1];
3169  Opt_trace_context * const trace= &join->thd->opt_trace;
3170  DBUG_ENTER("make_join_statistics");
3171 
3172  stat= new (thd->mem_root) JOIN_TAB[table_count];
3173  stat_ref= (JOIN_TAB**) thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3174  if (!stat || !stat_ref)
3175  DBUG_RETURN(true);
3176 
3177  if (!(join->positions=
3178  new (thd->mem_root) POSITION[table_count+1]))
3179  DBUG_RETURN(true);
3180 
3181  // Up to one extra slot per semi-join nest is needed (if materialized)
3182  uint sj_nests= join->select_lex->sj_nests.elements;
3183  if (!(join->best_positions=
3184  new (thd->mem_root) POSITION[table_count + sj_nests + 1]))
3185  DBUG_RETURN(true);
3186 
3187  join->best_ref= stat_vector;
3188 
3189  stat_end= stat+table_count;
3190  join->const_table_map= 0;
3191  join->found_const_table_map= 0;
3192  join->all_table_map= 0;
3193  const_count= 0;
3194 
3195  /*
3196  Initialize data structures for tables to be joined.
3197  Initialize dependencies between tables.
3198  */
3199  for (s= stat, i= 0;
3200  tables;
3201  s++, tables= tables->next_leaf, i++)
3202  {
3203  stat_vector[i]=s;
3204  TABLE *const table= tables->table;
3205  s->table= table;
3206  table->pos_in_table_list= tables;
3207  error= tables->fetch_number_of_rows();
3208 
3209  DBUG_EXECUTE_IF("bug11747970_raise_error",
3210  {
3211  if (!error)
3212  {
3213  my_error(ER_UNKNOWN_ERROR, MYF(0));
3214  goto error;
3215  }
3216  });
3217 
3218  if (error)
3219  {
3220  table->file->print_error(error, MYF(0));
3221  goto error;
3222  }
3223  table->quick_keys.clear_all();
3224  table->possible_quick_keys.clear_all();
3225  table->reginfo.join_tab=s;
3226  table->reginfo.not_exists_optimize=0;
3227  memset(table->const_key_parts, 0, sizeof(key_part_map)*table->s->keys);
3228  join->all_table_map|= table->map;
3229  s->join=join;
3230 
3231  s->dependent= tables->dep_tables;
3232  if (tables->schema_table)
3233  table->file->stats.records= 2;
3234  table->quick_condition_rows= table->file->stats.records;
3235 
3236  s->on_expr_ref= tables->join_cond_ref();
3237 
3238  if (tables->outer_join_nest())
3239  {
3240  /* s belongs to a nested join, maybe to several embedding joins */
3241  s->embedding_map= 0;
3242  for (TABLE_LIST *embedding= tables->embedding;
3243  embedding;
3244  embedding= embedding->embedding)
3245  {
3246  NESTED_JOIN *nested_join= embedding->nested_join;
3247  s->embedding_map|=nested_join->nj_map;
3248  s->dependent|= embedding->dep_tables;
3249  outer_join|= nested_join->used_tables;
3250  }
3251  }
3252  else if (*s->on_expr_ref)
3253  {
3254  /* s is the only inner table of an outer join */
3255  outer_join|= table->map;
3256  s->embedding_map= 0;
3257  for (TABLE_LIST *embedding= tables->embedding;
3258  embedding;
3259  embedding= embedding->embedding)
3260  s->embedding_map|= embedding->nested_join->nj_map;
3261  }
3262  }
3263  stat_vector[i]=0;
3264  join->outer_join=outer_join;
3265 
3266  if (join->outer_join)
3267  {
3268  /*
3269  Complete the dependency analysis.
3270  Build transitive closure for relation 'to be dependent on'.
3271  This will speed up the plan search for many cases with outer joins,
3272  as well as allow us to catch illegal cross references.
3273  Warshall's algorithm is used to build the transitive closure.
3274  As we may restart the outer loop upto 'table_count' times, the
3275  complexity of the algorithm is O((number of tables)^3).
3276  However, most of the iterations will be shortcircuited when
3277  there are no pedendencies to propogate.
3278  */
3279  for (i= 0 ; i < table_count ; i++)
3280  {
3281  TABLE *const table= stat[i].table;
3282 
3283  if (!table->reginfo.join_tab->dependent)
3284  continue;
3285 
3286  uint j;
3287  /* Add my dependencies to other tables depending on me */
3288  for (j= 0, s= stat ; j < table_count ; j++, s++)
3289  {
3290  if (s->dependent & table->map)
3291  {
3292  table_map was_dependent= s->dependent;
3293  s->dependent |= table->reginfo.join_tab->dependent;
3294  /*
3295  If we change dependencies for a table we already have
3296  processed: Redo dependency propagation from this table.
3297  */
3298  if (i > j && s->dependent != was_dependent)
3299  {
3300  i = j-1;
3301  break;
3302  }
3303  }
3304  }
3305  }
3306 
3307  for (i= 0, s= stat ; i < table_count ; i++, s++)
3308  {
3309  /* Catch illegal cross references for outer joins */
3310  if (s->dependent & s->table->map)
3311  {
3312  join->tables=0; // Don't use join->table
3313  join->primary_tables= 0;
3314  my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
3315  goto error;
3316  }
3317 
3318  if (outer_join & s->table->map)
3319  s->table->maybe_null= 1;
3320  s->key_dependent= s->dependent;
3321  }
3322  }
3323 
3324  if (unlikely(trace->is_started()))
3325  trace_table_dependencies(trace, stat, table_count);
3326 
3327  if (conds || outer_join)
3328  if (update_ref_and_keys(thd, keyuse_array, stat, join->tables,
3329  conds, join->cond_equal,
3330  ~outer_join, join->select_lex, &sargables))
3331  goto error;
3332 
3333  /*
3334  Pull out semi-join tables based on dependencies. Dependencies are valid
3335  throughout the lifetime of a query, so this operation can be performed
3336  on the first optimization only.
3337  */
3338  if (first_optimization && sj_nests)
3339  {
3340  if (pull_out_semijoin_tables(join))
3341  DBUG_RETURN(true);
3342  sj_nests= join->select_lex->sj_nests.elements;
3343  }
3344 
3345  /*
3346  Extract const tables based on row counts, must be done for each execution.
3347  Tables containing exactly zero or one rows are marked as const, but
3348  notice the additional constraints checked below.
3349  Tables that are extracted have their rows read before actual execution
3350  starts and are placed in the beginning of the join_tab array.
3351  Thus, they do not take part in join order optimization process,
3352  which can significantly reduce the optimization time.
3353  The data read from these tables can also be regarded as "constant"
3354  throughout query execution, hence the column values can be used for
3355  additional constant propagation and extraction of const tables based
3356  on eq-ref properties.
3357  */
3358  enum enum_const_table_extraction
3359  {
3360  extract_no_table= 0,
3361  extract_empty_table= 1,
3362  extract_const_table= 2
3363  };
3364 
3365  if (join->no_const_tables)
3366  goto const_table_extraction_done;
3367 
3368  for (i= 0, s= stat; i < table_count; i++, s++)
3369  {
3370  TABLE *const table= s->table;
3371  TABLE_LIST *const tables= table->pos_in_table_list;
3372  enum enum_const_table_extraction extract_method= extract_const_table;
3373 
3374 #ifdef WITH_PARTITION_STORAGE_ENGINE
3375  const bool all_partitions_pruned_away= table->all_partitions_pruned_away;
3376 #else
3377  const bool all_partitions_pruned_away= false;
3378 #endif
3379 
3380  if (tables->outer_join_nest())
3381  {
3382  /*
3383  Table belongs to a nested join, no candidate for const table extraction.
3384  */
3385  extract_method= extract_no_table;
3386  }
3387  else if (tables->embedding && tables->embedding->sj_on_expr)
3388  {
3389  /*
3390  Table belongs to a semi-join.
3391  We do not currently pull out const tables from semi-join nests.
3392  */
3393  extract_method= extract_no_table;
3394  }
3395  else if (*s->on_expr_ref)
3396  {
3397  /* s is the only inner table of an outer join, extract empty tables */
3398  extract_method= extract_empty_table;
3399  }
3400  switch (extract_method)
3401  {
3402  case extract_no_table:
3403  break;
3404 
3405  case extract_empty_table:
3406  /* Extract tables with zero rows, but only if statistics are exact */
3407  if ((table->file->stats.records == 0 ||
3408  all_partitions_pruned_away) &&
3409  (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT))
3410  set_position(join, const_count++, s, NULL);
3411  break;
3412 
3413  case extract_const_table:
3414  /*
3415  Extract tables with zero or one rows, but do not extract tables that
3416  1. are dependent upon other tables, or
3417  2. have no exact statistics, or
3418  3. are full-text searched
3419  */
3420  if ((table->s->system ||
3421  table->file->stats.records <= 1 ||
3422  all_partitions_pruned_away) &&
3423  !s->dependent && // 1
3424  (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && // 2
3425  !table->fulltext_searched) // 3
3426  set_position(join, const_count++, s, NULL);
3427  break;
3428  }
3429  }
3430  /* Read const tables (tables matching no more than 1 rows) */
3431 
3432  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
3433  p_pos < p_end ;
3434  p_pos++)
3435  {
3436  int tmp;
3437  s= p_pos->table;
3438  s->type=JT_SYSTEM;
3439  join->const_table_map|=s->table->map;
3440  if ((tmp=join_read_const_table(s, p_pos)))
3441  {
3442  if (tmp > 0)
3443  goto error; // Fatal error
3444  }
3445  else
3446  {
3447  join->found_const_table_map|= s->table->map;
3448  s->table->pos_in_table_list->optimized_away= TRUE;
3449  }
3450  }
3451 
3452 const_table_extraction_done:
3453  /* loop until no more const tables are found */
3454  int ref_changed;
3455  do
3456  {
3457  more_const_tables_found:
3458  ref_changed = 0;
3459  found_ref=0;
3460 
3461  /*
3462  We only have to loop from stat_vector + const_count as
3463  set_position() will move all const_tables first in stat_vector
3464  */
3465 
3466  for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
3467  {
3468  TABLE *const table= s->table;
3469  TABLE_LIST *const tl= table->pos_in_table_list;
3470  /*
3471  If equi-join condition by a key is null rejecting and after a
3472  substitution of a const table the key value happens to be null
3473  then we can state that there are no matches for this equi-join.
3474  */
3475  if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
3476  {
3477  /*
3478  When performing an outer join operation if there are no matching rows
3479  for the single row of the outer table all the inner tables are to be
3480  null complemented and thus considered as constant tables.
3481  Here we apply this consideration to the case of outer join operations
3482  with a single inner table only because the case with nested tables
3483  would require a more thorough analysis.
3484  TODO. Apply single row substitution to null complemented inner tables
3485  for nested outer join operations.
3486  */
3487  while (keyuse->table == table)
3488  {
3489  if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
3490  keyuse->val->is_null() && keyuse->null_rejecting)
3491  {
3492  s->type= JT_CONST;
3493  mark_as_null_row(table);
3494  join->found_const_table_map|= table->map;
3495  join->const_table_map|= table->map;
3496  set_position(join, const_count++, s, NULL);
3497  goto more_const_tables_found;
3498  }
3499  keyuse++;
3500  }
3501  }
3502 
3503  if (s->dependent) // If dependent on some table
3504  {
3505  // All dep. must be constants
3506  if (s->dependent & ~(join->const_table_map))
3507  continue;
3508  /*
3509  Mark a dependent table as constant if
3510  1. it has exactly zero or one rows (it is a system table), and
3511  2. it is not within a nested outer join, and
3512  3. it does not have an expensive outer join condition.
3513  This is because we have to determine whether an outer-joined table
3514  has a real row or a null-extended row in the optimizer phase.
3515  We have no possibility to evaluate its join condition at
3516  execution time, when it is marked as a system table.
3517  */
3518  if (table->file->stats.records <= 1L && // 1
3519  (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && // 1
3520  !tl->outer_join_nest() && // 2
3521  !(*s->on_expr_ref && (*s->on_expr_ref)->is_expensive())) // 3
3522  { // system table
3523  int tmp= 0;
3524  s->type=JT_SYSTEM;
3525  join->const_table_map|=table->map;
3526  set_position(join, const_count++, s, NULL);
3527  if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
3528  {
3529  if (tmp > 0)
3530  goto error; // Fatal error
3531  }
3532  else
3533  join->found_const_table_map|= table->map;
3534  continue;
3535  }
3536  }
3537  /* check if table can be read by key or table only uses const refs */
3538  if ((keyuse=s->keyuse))
3539  {
3540  s->type= JT_REF;
3541  while (keyuse->table == table)
3542  {
3543  start_keyuse=keyuse;
3544  key=keyuse->key;
3545  s->keys.set_bit(key); // QQ: remove this ?
3546 
3547  refs=0;
3548  key_map const_ref, eq_part;
3549  do
3550  {
3551  if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
3552  {
3553  if (!((~join->found_const_table_map) & keyuse->used_tables))
3554  const_ref.set_bit(keyuse->keypart);
3555  else
3556  refs|=keyuse->used_tables;
3557  eq_part.set_bit(keyuse->keypart);
3558  }
3559  keyuse++;
3560  } while (keyuse->table == table && keyuse->key == key);
3561 
3562  /*
3563  Extract const tables with proper key dependencies.
3564  Exclude tables that
3565  1. are full-text searched, or
3566  2. are part of nested outer join, or
3567  3. are part of semi-join, or
3568  4. have an expensive outer join condition.
3569  5. are blocked by handler for const table optimize.
3570  */
3571  if (eq_part.is_prefix(table->key_info[key].user_defined_key_parts) &&
3572  !table->fulltext_searched && // 1
3573  !tl->outer_join_nest() && // 2
3574  !(tl->embedding && tl->embedding->sj_on_expr) && // 3
3575  !(*s->on_expr_ref && (*s->on_expr_ref)->is_expensive()) &&// 4
3576  !(table->file->ha_table_flags() & HA_BLOCK_CONST_TABLE)) // 5
3577  {
3578  if (table->key_info[key].flags & HA_NOSAME)
3579  {
3580  if (const_ref == eq_part)
3581  { // Found everything for ref.
3582  int tmp;
3583  ref_changed = 1;
3584  s->type= JT_CONST;
3585  join->const_table_map|=table->map;
3586  set_position(join,const_count++,s,start_keyuse);
3587  if (create_ref_for_key(join, s, start_keyuse,
3588  join->found_const_table_map))
3589  goto error;
3590  if ((tmp=join_read_const_table(s,
3591  join->positions+const_count-1)))
3592  {
3593  if (tmp > 0)
3594  goto error; // Fatal error
3595  }
3596  else
3597  join->found_const_table_map|= table->map;
3598  break;
3599  }
3600  else
3601  found_ref|= refs; // Table is const if all refs are const
3602  }
3603  else if (const_ref == eq_part)
3604  s->const_keys.set_bit(key);
3605  }
3606  }
3607  }
3608  }
3609  } while (join->const_table_map & found_ref && ref_changed);
3610 
3611  /*
3612  Update info on indexes that can be used for search lookups as
3613  reading const tables may has added new sargable predicates.
3614  */
3615  if (const_count && sargables)
3616  {
3617  for( ; sargables->field ; sargables++)
3618  {
3619  Field *field= sargables->field;
3620  JOIN_TAB *join_tab= field->table->reginfo.join_tab;
3621  key_map possible_keys= field->key_start;
3622  possible_keys.intersect(field->table->keys_in_use_for_query);
3623  bool is_const= 1;
3624  for (uint j=0; j < sargables->num_values; j++)
3625  is_const&= sargables->arg_value[j]->const_item();
3626  if (is_const)
3627  {
3628  join_tab->const_keys.merge(possible_keys);
3629  join_tab->keys.merge(possible_keys);
3630  }
3631  }
3632  }
3633 
3634  {
3635  Opt_trace_object trace_wrapper(trace);
3636  /* Calc how many (possible) matched records in each table */
3637  Opt_trace_array trace_records(trace, "rows_estimation");
3638 
3639  for (s= stat ; s < stat_end ; s++)
3640  {
3641  Opt_trace_object trace_table(trace);
3642  trace_table.add_utf8_table(s->table);
3643  if (s->type == JT_SYSTEM || s->type == JT_CONST)
3644  {
3645  trace_table.add("rows", 1).add("cost", 1)
3646  .add_alnum("table_type", (s->type == JT_SYSTEM) ? "system": "const")
3647  .add("empty", static_cast<bool>(s->table->null_row));
3648 
3649  /* Only one matching row */
3650  s->found_records= s->records= s->read_time=1; s->worst_seeks= 1.0;
3651  continue;
3652  }
3653  /* Approximate found rows and time to read them */
3654  s->found_records= s->records= s->table->file->stats.records;
3655  s->read_time= (ha_rows) s->table->file->scan_time();
3656 
3657  /*
3658  Set a max range of how many seeks we can expect when using keys
3659  This is can't be to high as otherwise we are likely to use
3660  table scan.
3661  */
3662  s->worst_seeks= min((double) s->found_records / 10,
3663  (double) s->read_time * 3);
3664  if (s->worst_seeks < 2.0) // Fix for small tables
3665  s->worst_seeks= 2.0;
3666 
3667  /*
3668  Add to stat->const_keys those indexes for which all group fields or
3669  all select distinct fields participate in one index.
3670  */
3671  add_group_and_distinct_keys(join, s);
3672 
3673  /*
3674  Perform range analysis if there are keys it could use (1).
3675  Don't do range analysis if on the inner side of an outer join (2).
3676  Do range analysis if on the inner side of a semi-join (3).
3677  */
3678  TABLE_LIST *const tl= s->table->pos_in_table_list;
3679  if (!s->const_keys.is_clear_all() && // (1)
3680  (!tl->embedding || // (2)
3681  (tl->embedding && tl->embedding->sj_on_expr))) // (3)
3682  {
3683  ha_rows records;
3684  SQL_SELECT *select;
3685  select= make_select(s->table, join->found_const_table_map,
3686  join->found_const_table_map,
3687  *s->on_expr_ref ? *s->on_expr_ref : conds,
3688  1, &error);
3689  if (!select)
3690  goto error;
3691  records= get_quick_record_count(thd, select, s->table,
3692  &s->const_keys, join->row_limit);
3693 
3694  if (records == 0 && thd->is_fatal_error)
3695  DBUG_RETURN(true);
3696 
3697  s->quick= select->quick;
3698  s->needed_reg= select->needed_reg;
3699  select->quick= 0;
3700  /*
3701  Check for "impossible range", but make sure that we do not attempt
3702  to mark semi-joined tables as "const" (only semi-joined tables that
3703  are functionally dependent can be marked "const", and subsequently
3704  pulled out of their semi-join nests).
3705  */
3706  if (records == 0 &&
3707  s->table->reginfo.impossible_range &&
3708  (!(tl->embedding && tl->embedding->sj_on_expr)))
3709  {
3710  /*
3711  Impossible WHERE or ON expression
3712  In case of ON, we mark that the we match one empty NULL row.
3713  In case of WHERE, don't set found_const_table_map to get the
3714  caller to abort with a zero row result.
3715  */
3716  join->const_table_map|= s->table->map;
3717  set_position(join, const_count++, s, NULL);
3718  s->type= JT_CONST;
3719  if (*s->on_expr_ref)
3720  {
3721  /* Generate empty row */
3722  s->info= ET_IMPOSSIBLE_ON_CONDITION;
3723  trace_table.add("returning_empty_null_row", true).
3724  add_alnum("cause", "impossible_on_condition");
3725  join->found_const_table_map|= s->table->map;
3726  s->type= JT_CONST;
3727  mark_as_null_row(s->table); // All fields are NULL
3728  }
3729  else
3730  {
3731  trace_table.add("rows", 0).
3732  add_alnum("cause", "impossible_where_condition");
3733  }
3734  }
3735  if (records != HA_POS_ERROR)
3736  {
3737  s->found_records= records;
3738  s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
3739  }
3740  delete select;
3741  }
3742  else
3743  Opt_trace_object(trace, "table_scan").
3744  add("rows", s->found_records).
3745  add("cost", s->read_time);
3746  }
3747  }
3748 
3749  join->join_tab=stat;
3750  join->map2table=stat_ref;
3751  join->const_tables=const_count;
3752 
3753  if (sj_nests)
3754  join->set_semijoin_embedding();
3755 
3756  if (!join->plan_is_const())
3757  optimize_keyuse(join, keyuse_array);
3758 
3759  join->allow_outer_refs= true;
3760 
3761  if (sj_nests && optimize_semijoin_nests_for_materialization(join))
3762  DBUG_RETURN(true);
3763 
3764  if (Optimize_table_order(thd, join, NULL).choose_table_order())
3765  DBUG_RETURN(true);
3766 
3767  DBUG_EXECUTE_IF("bug13820776_1", thd->killed= THD::KILL_QUERY;);
3768  if (thd->killed || thd->is_error())
3769  DBUG_RETURN(true);
3770 
3771  if (join->unit->item && join->decide_subquery_strategy())
3772  DBUG_RETURN(true);
3773 
3774  join->refine_best_rowcount();
3775 
3776  // Only best_positions should be needed from now on.
3777  join->positions= NULL;
3778  join->best_ref= NULL;
3779 
3780  /*
3781  Store the cost of this query into a user variable
3782  Don't update last_query_cost for statements that are not "flat joins" :
3783  i.e. they have subqueries, unions or call stored procedures.
3784  TODO: calculate a correct cost for a query with subqueries and UNIONs.
3785  */
3786  if (thd->lex->is_single_level_stmt())
3787  thd->status_var.last_query_cost= join->best_read;
3788 
3789  /* Generate an execution plan from the found optimal join order. */
3790  if (join->get_best_combination())
3791  DBUG_RETURN(true);
3792 
3793  // No need for this struct after new JOIN_TAB array is set up.
3794  join->best_positions= NULL;
3795 
3796  /* Some called function may still set thd->is_fatal_error unnoticed */
3797  if (thd->is_fatal_error)
3798  DBUG_RETURN(true);
3799 
3800  DBUG_RETURN(false);
3801 
3802 error:
3803  /*
3804  Need to clean up join_tab from TABLEs in case of error.
3805  They won't get cleaned up by JOIN::cleanup() because JOIN::join_tab
3806  may not be assigned yet by this function (which is building join_tab).
3807  Dangling TABLE::reginfo.join_tab may cause part_of_refkey to choke.
3808  */
3809  for (tables= tables_arg; tables; tables= tables->next_leaf)
3810  tables->table->reginfo.join_tab= NULL;
3811  DBUG_RETURN(true);
3812 }
3813 
3814 
3828 {
3829  DBUG_ASSERT(!select_lex->sj_nests.is_empty());
3830 
3831  JOIN_TAB *const tab_end= join_tab + primary_tables;
3832 
3833  for (JOIN_TAB *tab= join_tab; tab < tab_end; tab++)
3834  {
3835  for (TABLE_LIST *tr= tab->table->pos_in_table_list;
3836  tr->embedding;
3837  tr= tr->embedding)
3838  {
3839  if (tr->embedding->sj_on_expr)
3840  {
3841  tab->emb_sj_nest= tr->embedding;
3842  break;
3843  }
3844  }
3845  }
3846 }
3847 
3848 
3878 static
3879 void semijoin_types_allow_materialization(TABLE_LIST *sj_nest)
3880 {
3881  DBUG_ENTER("semijoin_types_allow_materialization");
3882 
3883  DBUG_ASSERT(sj_nest->nested_join->sj_outer_exprs.elements ==
3884  sj_nest->nested_join->sj_inner_exprs.elements);
3885 
3886  if (sj_nest->nested_join->sj_outer_exprs.elements > MAX_REF_PARTS)
3887  {
3888  sj_nest->nested_join->sjm.scan_allowed= false;
3889  sj_nest->nested_join->sjm.lookup_allowed= false;
3890  DBUG_VOID_RETURN;
3891  }
3892 
3893  List_iterator<Item> it1(sj_nest->nested_join->sj_outer_exprs);
3894  List_iterator<Item> it2(sj_nest->nested_join->sj_inner_exprs);
3895 
3896  sj_nest->nested_join->sjm.scan_allowed= false;
3897  sj_nest->nested_join->sjm.lookup_allowed= false;
3898 
3899  bool blobs_involved= false;
3900  Item *outer, *inner;
3901  while (outer= it1++, inner= it2++)
3902  {
3903  if (!types_allow_materialization(outer, inner))
3904  DBUG_VOID_RETURN;
3905  blobs_involved|= inner->is_blob_field();
3906  }
3907  sj_nest->nested_join->sjm.scan_allowed= true;
3908  sj_nest->nested_join->sjm.lookup_allowed= !blobs_involved;
3909 
3910  if (sj_nest->embedding)
3911  {
3912  DBUG_ASSERT(sj_nest->embedding->join_cond());
3913  /*
3914  There are two issues that prevent materialization strategy from being
3915  used when a semi-join nest is on the inner side of an outer join:
3916  1. If the semi-join contains dependencies to outer tables,
3917  materialize-scan strategy cannot be used.
3918  2. Make sure that executor is able to evaluate triggered conditions
3919  for semi-join materialized tables. It should be correct, but needs
3920  verification.
3921  TODO: Remove this limitation!
3922  Handle this by disabling materialization strategies:
3923  */
3924  sj_nest->nested_join->sjm.scan_allowed= false;
3925  sj_nest->nested_join->sjm.lookup_allowed= false;
3926  DBUG_VOID_RETURN;
3927  }
3928 
3929  DBUG_PRINT("info",("semijoin_types_allow_materialization: ok, allowed"));
3930 
3931  DBUG_VOID_RETURN;
3932 }
3933 
3934 
3935 /*****************************************************************************
3936  Create JOIN_TABS, make a guess about the table types,
3937  Approximate how many records will be used in each table
3938 *****************************************************************************/
3939 
3959 static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
3960  TABLE *table,
3961  const key_map *keys,ha_rows limit)
3962 {
3963  DBUG_ENTER("get_quick_record_count");
3964  uchar buff[STACK_BUFF_ALLOC];
3965  if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
3966  DBUG_RETURN(0); // Fatal error flag is set
3967 
3968  DBUG_ASSERT(select);
3969 
3970  TABLE_LIST *const tl= table->pos_in_table_list;
3971 
3972  // Derived tables aren't filled yet, so no stats are available.
3973  if (!tl->uses_materialization())
3974  {
3975  select->head=table;
3976  int error= select->test_quick_select(thd,
3977  *keys,
3978  0, //empty table_map
3979  limit,
3980  false, //don't force quick range
3981  ORDER::ORDER_NOT_RELEVANT);
3982  if (error == 1)
3983  DBUG_RETURN(select->quick->records);
3984  if (error == -1)
3985  {
3986  table->reginfo.impossible_range=1;
3987  DBUG_RETURN(0);
3988  }
3989  DBUG_PRINT("warning",("Couldn't use record count on const keypart"));
3990  }
3991  else if (tl->materializable_is_const())
3992  {
3993  DBUG_RETURN(tl->get_unit()->get_result()->estimated_rowcount);
3994  }
3995  DBUG_RETURN(HA_POS_ERROR);
3996 }
3997 
3998 /*
3999  Get estimated record length for semi-join materialization temptable
4000 
4001  SYNOPSIS
4002  get_tmp_table_rec_length()
4003  items IN subquery's select list.
4004 
4005  DESCRIPTION
4006  Calculate estimated record length for semi-join materialization
4007  temptable. It's an estimate because we don't follow every bit of
4008  create_tmp_table()'s logic. This isn't necessary as the return value of
4009  this function is used only for cost calculations.
4010 
4011  RETURN
4012  Length of the temptable record, in bytes
4013 */
4014 
4015 static uint get_tmp_table_rec_length(List<Item> &items)
4016 {
4017  uint len= 0;
4018  Item *item;
4019  List_iterator<Item> it(items);
4020  while ((item= it++))
4021  {
4022  switch (item->result_type()) {
4023  case REAL_RESULT:
4024  len += sizeof(double);
4025  break;
4026  case INT_RESULT:
4027  if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
4028  len += 8;
4029  else
4030  len += 4;
4031  break;
4032  case STRING_RESULT:
4033  /* DATE/TIME and GEOMETRY fields have STRING_RESULT result type. */
4034  if (item->is_temporal() || item->field_type() == MYSQL_TYPE_GEOMETRY)
4035  len += 8;
4036  else
4037  len += item->max_length;
4038  break;
4039  case DECIMAL_RESULT:
4040  len += 10;
4041  break;
4042  case ROW_RESULT:
4043  default:
4044  DBUG_ASSERT(0); /* purecov: deadcode */
4045  break;
4046  }
4047  }
4048  return len;
4049 }
4050 
4051 
4059 static void trace_table_dependencies(Opt_trace_context * trace,
4060  JOIN_TAB *join_tabs,
4061  uint table_count)
4062 {
4063  Opt_trace_object trace_wrapper(trace);
4064  Opt_trace_array trace_dep(trace, "table_dependencies");
4065  for (uint i= 0 ; i < table_count ; i++)
4066  {
4067  const TABLE *table= join_tabs[i].table;
4068  Opt_trace_object trace_one_table(trace);
4069  trace_one_table.add_utf8_table(table).
4070  add("row_may_be_null", table->maybe_null != 0);
4071  DBUG_ASSERT(table->map < (1ULL << table_count));
4072  for (uint j= 0; j < table_count; j++)
4073  {
4074  if (table->map & (1ULL << j))
4075  {
4076  trace_one_table.add("map_bit", j);
4077  break;
4078  }
4079  }
4080  Opt_trace_array depends_on(trace, "depends_on_map_bits");
4081  // RAND_TABLE_BIT may be in join_tabs[i].dependent, so we test all 64 bits
4082  compile_time_assert(sizeof(table->map) <= 64);
4083  for (uint j= 0; j < 64; j++)
4084  {
4085  if (join_tabs[i].dependent & (1ULL << j))
4086  depends_on.add(j);
4087  }
4088  }
4089 }
4090 
4091 
4143 static void add_not_null_conds(JOIN *join)
4144 {
4145  DBUG_ENTER("add_not_null_conds");
4146  for (uint i=join->const_tables ; i < join->tables ; i++)
4147  {
4148  JOIN_TAB *tab=join->join_tab+i;
4149  if ((tab->type == JT_REF || tab->type == JT_EQ_REF ||
4150  tab->type == JT_REF_OR_NULL) &&
4151  !tab->table->maybe_null)
4152  {
4153  for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
4154  {
4155  if (tab->ref.null_rejecting & ((key_part_map)1 << keypart))
4156  {
4157  Item *item= tab->ref.items[keypart];
4158  Item *notnull;
4159  Item *real= item->real_item();
4160  DBUG_ASSERT(real->type() == Item::FIELD_ITEM);
4161  Item_field *not_null_item= (Item_field*)real;
4162  JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
4163  /*
4164  For UPDATE queries such as:
4165  UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
4166  not_null_item is the t1.f1, but it's referred_tab is 0.
4167  */
4168  if (!referred_tab || referred_tab->join != join)
4169  continue;
4170  if (!(notnull= new Item_func_isnotnull(not_null_item)))
4171  DBUG_VOID_RETURN;
4172  /*
4173  We need to do full fix_fields() call here in order to have correct
4174  notnull->const_item(). This is needed e.g. by test_quick_select
4175  when it is called from make_join_select after this function is
4176  called.
4177  */
4178  if (notnull->fix_fields(join->thd, &notnull))
4179  DBUG_VOID_RETURN;
4180  DBUG_EXECUTE("where",print_where(notnull,
4181  referred_tab->table->alias,
4182  QT_ORDINARY););
4183  referred_tab->and_with_condition(notnull, __LINE__);
4184  }
4185  }
4186  }
4187  }
4188  DBUG_VOID_RETURN;
4189 }
4190 
4191 
4192 /*
4193  Check if given expression uses only table fields covered by the given index
4194 
4195  SYNOPSIS
4196  uses_index_fields_only()
4197  item Expression to check
4198  tbl The table having the index
4199  keyno The index number
4200  other_tbls_ok TRUE <=> Fields of other non-const tables are allowed
4201 
4202  DESCRIPTION
4203  Check if given expression only uses fields covered by index #keyno in the
4204  table tbl. The expression can use any fields in any other tables.
4205 
4206  The expression is guaranteed not to be AND or OR - those constructs are
4207  handled outside of this function.
4208 
4209  RETURN
4210  TRUE Yes
4211  FALSE No
4212 */
4213 
4214 bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno,
4215  bool other_tbls_ok)
4216 {
4217  if (item->const_item())
4218  {
4219  /*
4220  const_item() might not return correct value if the item tree
4221  contains a subquery. If this is the case we do not include this
4222  part of the condition.
4223  */
4224  return !item->has_subquery();
4225  }
4226 
4227  const Item::Type item_type= item->type();
4228 
4229  switch (item_type) {
4230  case Item::FUNC_ITEM:
4231  {
4232  Item_func *item_func= (Item_func*)item;
4233  const Item_func::Functype func_type= item_func->functype();
4234 
4235  /*
4236  Avoid some function types from being pushed down to storage engine:
4237  - Don't push down the triggered conditions. Nested outer joins
4238  execution code may need to evaluate a condition several times
4239  (both triggered and untriggered).
4240  TODO: Consider cloning the triggered condition and using the
4241  copies for:
4242  1. push the first copy down, to have most restrictive
4243  index condition possible.
4244  2. Put the second copy into tab->m_condition.
4245  - Stored functions contain a statement that might start new operations
4246  against the storage engine. This does not work against all storage
4247  engines.
4248  */
4249  if (func_type == Item_func::TRIG_COND_FUNC ||
4250  func_type == Item_func::FUNC_SP)
4251  return false;
4252 
4253  /* This is a function, apply condition recursively to arguments */
4254  if (item_func->argument_count() > 0)
4255  {
4256  Item **item_end= (item_func->arguments()) + item_func->argument_count();
4257  for (Item **child= item_func->arguments(); child != item_end; child++)
4258  {
4259  if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
4260  return FALSE;
4261  }
4262  }
4263  return TRUE;
4264  }
4265  case Item::COND_ITEM:
4266  {
4267  /*
4268  This is a AND/OR condition. Regular AND/OR clauses are handled by
4269  make_cond_for_index() which will chop off the part that can be
4270  checked with index. This code is for handling non-top-level AND/ORs,
4271  e.g. func(x AND y).
4272  */
4273  List_iterator<Item> li(*((Item_cond*)item)->argument_list());
4274  Item *item;
4275  while ((item=li++))
4276  {
4277  if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
4278  return FALSE;
4279  }
4280  return TRUE;
4281  }
4282  case Item::FIELD_ITEM:
4283  {
4284  Item_field *item_field= (Item_field*)item;
4285  if (item_field->field->table != tbl)
4286  return other_tbls_ok;
4287  /*
4288  The below is probably a repetition - the first part checks the
4289  other two, but let's play it safe:
4290  */
4291  return item_field->field->part_of_key.is_set(keyno) &&
4292  item_field->field->type() != MYSQL_TYPE_GEOMETRY &&
4293  item_field->field->type() != MYSQL_TYPE_BLOB;
4294  }
4295  case Item::REF_ITEM:
4296  return uses_index_fields_only(item->real_item(), tbl, keyno,
4297  other_tbls_ok);
4298  default:
4299  return FALSE; /* Play it safe, don't push unknown non-const items */
4300  }
4301 }
4302 
4303 
4324 static bool optimize_semijoin_nests_for_materialization(JOIN *join)
4325 {
4326  DBUG_ENTER("optimize_semijoin_nests_for_materialization");
4327  List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
4328  TABLE_LIST *sj_nest;
4329  Opt_trace_context * const trace= &join->thd->opt_trace;
4330 
4331  while ((sj_nest= sj_list_it++))
4332  {
4333  /* As a precaution, reset pointers that were used in prior execution */
4334  sj_nest->nested_join->sjm.positions= NULL;
4335 
4336  /* Calculate the cost of materialization if materialization is allowed. */
4337  if (join->thd->optimizer_switch_flag(OPTIMIZER_SWITCH_SEMIJOIN) &&
4338  join->thd->optimizer_switch_flag(OPTIMIZER_SWITCH_MATERIALIZATION))
4339  {
4340  /* A semi-join nest should not contain tables marked as const */
4341  DBUG_ASSERT(!(sj_nest->sj_inner_tables & join->const_table_map));
4342 
4343  Opt_trace_object trace_wrapper(trace);
4345  trace_sjmat(trace, "execution_plan_for_potential_materialization");
4346  Opt_trace_array trace_sjmat_steps(trace, "steps");
4347  /*
4348  Try semijoin materialization if the semijoin is classified as
4349  non-trivially-correlated.
4350  */
4351  if (sj_nest->nested_join->sj_corr_tables)
4352  continue;
4353  /*
4354  Check whether data types allow execution with materialization.
4355  */
4356  semijoin_types_allow_materialization(sj_nest);
4357 
4358  if (!sj_nest->nested_join->sjm.scan_allowed &&
4359  !sj_nest->nested_join->sjm.lookup_allowed)
4360  continue;
4361 
4362  if (Optimize_table_order(join->thd, join, sj_nest).choose_table_order())
4363  DBUG_RETURN(true);
4364  const uint n_tables= my_count_bits(sj_nest->sj_inner_tables);
4365  calculate_materialization_costs(join, sj_nest, n_tables,
4366  &sj_nest->nested_join->sjm);
4367  /*
4368  Cost data is in sj_nest->nested_join->sjm. We also need to save the
4369  plan:
4370  */
4371  if (!(sj_nest->nested_join->sjm.positions=
4372  (st_position*)join->thd->alloc(sizeof(st_position)*n_tables)))
4373  DBUG_RETURN(true);
4374  memcpy(sj_nest->nested_join->sjm.positions,
4375  join->best_positions + join->const_tables,
4376  sizeof(st_position) * n_tables);
4377  }
4378  }
4379  DBUG_RETURN(false);
4380 }
4381 
4382 
4383 /*
4384  Check if table's Key_use elements have an eq_ref(outer_tables) candidate
4385 
4386  SYNOPSIS
4387  find_eq_ref_candidate()
4388  table Table to be checked
4389  sj_inner_tables Bitmap of inner tables. eq_ref(inner_table) doesn't
4390  count.
4391 
4392  DESCRIPTION
4393  Check if table's Key_use elements have an eq_ref(outer_tables) candidate
4394 
4395  TODO
4396  Check again if it is feasible to factor common parts with constant table
4397  search
4398 
4399  RETURN
4400  TRUE - There exists an eq_ref(outer-tables) candidate
4401  FALSE - Otherwise
4402 */
4403 
4404 static bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
4405 {
4406  Key_use *keyuse= table->reginfo.join_tab->keyuse;
4407  uint key;
4408 
4409  if (keyuse)
4410  {
4411  while (1) /* For each key */
4412  {
4413  key= keyuse->key;
4414  KEY *keyinfo= table->key_info + key;
4415  key_part_map bound_parts= 0;
4416  if ((keyinfo->flags & (HA_NOSAME)) == HA_NOSAME)
4417  {
4418  do /* For all equalities on all key parts */
4419  {
4420  /* Check if this is "t.keypart = expr(outer_tables) */
4421  if (!(keyuse->used_tables & sj_inner_tables) &&
4422  !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
4423  {
4424  bound_parts|= (key_part_map)1 << keyuse->keypart;
4425  }
4426  keyuse++;
4427  } while (keyuse->key == key && keyuse->table == table);
4428 
4429  if (bound_parts == LOWER_BITS(uint, keyinfo->user_defined_key_parts))
4430  return TRUE;
4431  if (keyuse->table != table)
4432  return FALSE;
4433  }
4434  else
4435  {
4436  do
4437  {
4438  keyuse++;
4439  if (keyuse->table != table)
4440  return FALSE;
4441  }
4442  while (keyuse->key == key);
4443  }
4444  }
4445  }
4446  return FALSE;
4447 }
4448 
4449 
4492 static bool pull_out_semijoin_tables(JOIN *join)
4493 {
4494  TABLE_LIST *sj_nest;
4495  DBUG_ENTER("pull_out_semijoin_tables");
4496 
4497  DBUG_ASSERT(!join->select_lex->sj_nests.is_empty());
4498 
4499  List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
4500  Opt_trace_context * const trace= &join->thd->opt_trace;
4501  Opt_trace_object trace_wrapper(trace);
4502  Opt_trace_array trace_pullout(trace, "pulled_out_semijoin_tables");
4503 
4504  /* Try pulling out tables from each semi-join nest */
4505  while ((sj_nest= sj_list_it++))
4506  {
4507  table_map pulled_tables= 0;
4508  List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
4509  TABLE_LIST *tbl;
4510  /*
4511  Calculate set of tables within this semi-join nest that have
4512  other dependent tables
4513  */
4514  table_map dep_tables= 0;
4515  while ((tbl= child_li++))
4516  {
4517  TABLE *const table= tbl->table;
4518  if (table &&
4519  (table->reginfo.join_tab->dependent &
4520  sj_nest->nested_join->used_tables))
4521  dep_tables|= table->reginfo.join_tab->dependent;
4522  }
4523  /*
4524  Find which tables we can pull out based on key dependency data.
4525  Note that pulling one table out can allow us to pull out some
4526  other tables too.
4527  */
4528  bool pulled_a_table;
4529  do
4530  {
4531  pulled_a_table= FALSE;
4532  child_li.rewind();
4533  while ((tbl= child_li++))
4534  {
4535  if (tbl->table &&
4536  !(pulled_tables & tbl->table->map) &&
4537  !(dep_tables & tbl->table->map))
4538  {
4539  if (find_eq_ref_candidate(tbl->table,
4540  sj_nest->nested_join->used_tables &
4541  ~pulled_tables))
4542  {
4543  pulled_a_table= TRUE;
4544  pulled_tables |= tbl->table->map;
4545  Opt_trace_object(trace).add_utf8_table(tbl->table).
4546  add("functionally_dependent", true);
4547  /*
4548  Pulling a table out of uncorrelated subquery in general makes
4549  makes it correlated. See the NOTE to this function.
4550  */
4551  sj_nest->nested_join->sj_corr_tables|= tbl->table->map;
4552  sj_nest->nested_join->sj_depends_on|= tbl->table->map;
4553  }
4554  }
4555  }
4556  } while (pulled_a_table);
4557 
4558  child_li.rewind();
4559  /*
4560  Move the pulled out TABLE_LIST elements to the parents.
4561  */
4562  sj_nest->nested_join->used_tables&= ~pulled_tables;
4563  sj_nest->nested_join->not_null_tables&= ~pulled_tables;
4564 
4565  /* sj_inner_tables is a copy of nested_join->used_tables */
4566  sj_nest->sj_inner_tables= sj_nest->nested_join->used_tables;
4567 
4568  if (pulled_tables)
4569  {
4570  List<TABLE_LIST> *upper_join_list= (sj_nest->embedding != NULL) ?
4571  &sj_nest->embedding->nested_join->join_list :
4572  &join->select_lex->top_join_list;
4573 
4574  Prepared_stmt_arena_holder ps_arena_holder(join->thd);
4575 
4576  while ((tbl= child_li++))
4577  {
4578  if (tbl->table &&
4579  !(sj_nest->nested_join->used_tables & tbl->table->map))
4580  {
4581  /*
4582  Pull the table up in the same way as simplify_joins() does:
4583  update join_list and embedding pointers but keep next[_local]
4584  pointers.
4585  */
4586  child_li.remove();
4587 
4588  if (upper_join_list->push_back(tbl))
4589  DBUG_RETURN(TRUE);
4590 
4591  tbl->join_list= upper_join_list;
4592  tbl->embedding= sj_nest->embedding;
4593  }
4594  }
4595 
4596  /* Remove the sj-nest itself if we've removed everything from it */
4597  if (!sj_nest->nested_join->used_tables)
4598  {
4599  List_iterator<TABLE_LIST> li(*upper_join_list);
4600  /* Find the sj_nest in the list. */
4601  while (sj_nest != li++)
4602  {}
4603  li.remove();
4604  /* Also remove it from the list of SJ-nests: */
4605  sj_list_it.remove();
4606  }
4607  }
4608  }
4609  DBUG_RETURN(FALSE);
4610 }
4611 
4612 
4613 /*****************************************************************************
4614  Check with keys are used and with tables references with tables
4615  Updates in stat:
4616  keys Bitmap of all used keys
4617  const_keys Bitmap of all keys with may be used with quick_select
4618  keyuse Pointer to possible keys
4619 *****************************************************************************/
4620 
4622 struct Key_field {
4623  Key_field(Field *field, Item *val, uint level, uint optimize, bool eq_func,
4624  bool null_rejecting, bool *cond_guard, uint sj_pred_no)
4625  : field(field), val(val), level(level), optimize(optimize), eq_func(eq_func),
4626  null_rejecting(null_rejecting), cond_guard(cond_guard),
4627  sj_pred_no(sj_pred_no)
4628  {}
4629  Field *field;
4631  uint level;
4632  uint optimize; // KEY_OPTIMIZE_*
4633  bool eq_func;
4640  bool *cond_guard;
4641  uint sj_pred_no;
4642 };
4643 
4644 /* Values in optimize */
4645 #define KEY_OPTIMIZE_EXISTS 1
4646 #define KEY_OPTIMIZE_REF_OR_NULL 2
4647 
4672 static Key_field *
4673 merge_key_fields(Key_field *start, Key_field *new_fields, Key_field *end,
4674  uint and_level)
4675 {
4676  if (start == new_fields)
4677  return start; // Impossible or
4678  if (new_fields == end)
4679  return start; // No new fields, skip all
4680 
4681  Key_field *first_free=new_fields;
4682 
4683  /* Mark all found fields in old array */
4684  for (; new_fields != end ; new_fields++)
4685  {
4686  for (Key_field *old=start ; old != first_free ; old++)
4687  {
4688  if (old->field == new_fields->field)
4689  {
4690  /*
4691  NOTE: below const_item() call really works as "!used_tables()", i.e.
4692  it can return FALSE where it is feasible to make it return TRUE.
4693 
4694  The cause is as follows: Some of the tables are already known to be
4695  const tables (the detection code is in make_join_statistics(),
4696  above the update_ref_and_keys() call), but we didn't propagate
4697  information about this: TABLE::const_table is not set to TRUE, and
4698  Item::update_used_tables() hasn't been called for each item.
4699  The result of this is that we're missing some 'ref' accesses.
4700  TODO: OptimizerTeam: Fix this
4701  */
4702  if (!new_fields->val->const_item())
4703  {
4704  /*
4705  If the value matches, we can use the key reference.
4706  If not, we keep it until we have examined all new values
4707  */
4708  if (old->val->eq(new_fields->val, old->field->binary()))
4709  {
4710  old->level= and_level;
4711  old->optimize= ((old->optimize & new_fields->optimize &
4712  KEY_OPTIMIZE_EXISTS) |
4713  ((old->optimize | new_fields->optimize) &
4714  KEY_OPTIMIZE_REF_OR_NULL));
4715  old->null_rejecting= (old->null_rejecting &&
4716  new_fields->null_rejecting);
4717  }
4718  }
4719  else if (old->eq_func && new_fields->eq_func &&
4720  old->val->eq_by_collation(new_fields->val,
4721  old->field->binary(),
4722  old->field->charset()))
4723 
4724  {
4725  old->level= and_level;
4726  old->optimize= ((old->optimize & new_fields->optimize &
4727  KEY_OPTIMIZE_EXISTS) |
4728  ((old->optimize | new_fields->optimize) &
4729  KEY_OPTIMIZE_REF_OR_NULL));
4730  old->null_rejecting= (old->null_rejecting &&
4731  new_fields->null_rejecting);
4732  }
4733  else if (old->eq_func && new_fields->eq_func &&
4734  ((old->val->const_item() && old->val->is_null()) ||
4735  new_fields->val->is_null()))
4736  {
4737  /* field = expression OR field IS NULL */
4738  old->level= and_level;
4739  old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
4740  /*
4741  Remember the NOT NULL value unless the value does not depend
4742  on other tables.
4743  */
4744  if (!old->val->used_tables() && old->val->is_null())
4745  old->val= new_fields->val;
4746  /* The referred expression can be NULL: */
4747  old->null_rejecting= 0;
4748  }
4749  else
4750  {
4751  /*
4752  We are comparing two different const. In this case we can't
4753  use a key-lookup on this so it's better to remove the value
4754  and let the range optimzier handle it
4755  */
4756  if (old == --first_free) // If last item
4757  break;
4758  *old= *first_free; // Remove old value
4759  old--; // Retry this value
4760  }
4761  }
4762  }
4763  }
4764  /* Remove all not used items */
4765  for (Key_field *old=start ; old != first_free ;)
4766  {
4767  if (old->level != and_level)
4768  { // Not used in all levels
4769  if (old == --first_free)
4770  break;
4771  *old= *first_free; // Remove old value
4772  continue;
4773  }
4774  old++;
4775  }
4776  return first_free;
4777 }
4778 
4779 
4797 static uint get_semi_join_select_list_index(Field *field)
4798 {
4799  TABLE_LIST *emb_sj_nest= field->table->pos_in_table_list->embedding;
4800  if (emb_sj_nest && emb_sj_nest->sj_on_expr)
4801  {
4802  List<Item> &items= emb_sj_nest->nested_join->sj_inner_exprs;
4803  List_iterator<Item> it(items);
4804  for (uint i= 0; i < items.elements; i++)
4805  {
4806  Item *sel_item= it++;
4807  if (sel_item->type() == Item::FIELD_ITEM &&
4808  ((Item_field*)sel_item)->field->eq(field))
4809  return i;
4810  }
4811  }
4812  return UINT_MAX;
4813 }
4814 
4839 static void
4840 warn_index_not_applicable(THD *thd, const Field *field,
4841  const key_map cant_use_index)
4842 {
4843  if (thd->lex->describe & DESCRIBE_EXTENDED)
4844  for (uint j=0 ; j < field->table->s->keys ; j++)
4845  if (cant_use_index.is_set(j))
4846  push_warning_printf(thd,
4847  Sql_condition::WARN_LEVEL_WARN,
4848  ER_WARN_INDEX_NOT_APPLICABLE,
4849  ER(ER_WARN_INDEX_NOT_APPLICABLE),
4850  "ref",
4851  field->table->key_info[j].name,
4852  field->field_name);
4853 }
4854 
4876 static void
4877 add_key_field(Key_field **key_fields,uint and_level, Item_func *cond,
4878  Field *field, bool eq_func, Item **value, uint num_values,
4879  table_map usable_tables, SARGABLE_PARAM **sargables)
4880 {
4881  DBUG_PRINT("info",("add_key_field for field %s",field->field_name));
4882  uint exists_optimize= 0;
4883  TABLE_LIST *table= field->table->pos_in_table_list;
4884  if (!table->derived_keys_ready && table->uses_materialization() &&
4885  !field->table->is_created() &&
4886  table->update_derived_keys(field, value, num_values))
4887  return;
4888  if (!(field->flags & PART_KEY_FLAG))
4889  {
4890  // Don't remove column IS NULL on a LEFT JOIN table
4891  if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4892  !field->table->maybe_null || field->real_maybe_null())
4893  return; // Not a key. Skip it
4894  exists_optimize= KEY_OPTIMIZE_EXISTS;
4895  DBUG_ASSERT(num_values == 1);
4896  }
4897  else
4898  {
4899  table_map used_tables=0;
4900  bool optimizable=0;
4901  for (uint i=0; i<num_values; i++)
4902  {
4903  used_tables|=(value[i])->used_tables();
4904  if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
4905  optimizable=1;
4906  }
4907  if (!optimizable)
4908  return;
4909  if (!(usable_tables & field->table->map))
4910  {
4911  if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4912  !field->table->maybe_null || field->real_maybe_null())
4913  return; // Can't use left join optimize
4914  exists_optimize= KEY_OPTIMIZE_EXISTS;
4915  }
4916  else
4917  {
4918  JOIN_TAB *stat=field->table->reginfo.join_tab;
4919  key_map possible_keys=field->key_start;
4920  possible_keys.intersect(field->table->keys_in_use_for_query);
4921  stat[0].keys.merge(possible_keys); // Add possible keys
4922 
4923  /*
4924  Save the following cases:
4925  Field op constant
4926  Field LIKE constant where constant doesn't start with a wildcard
4927  Field = field2 where field2 is in a different table
4928  Field op formula
4929  Field IS NULL
4930  Field IS NOT NULL
4931  Field BETWEEN ...
4932  Field IN ...
4933  */
4934  stat[0].key_dependent|=used_tables;
4935 
4936  bool is_const=1;
4937  for (uint i=0; i<num_values; i++)
4938  {
4939  if (!(is_const&= value[i]->const_item()))
4940  break;
4941  }
4942  if (is_const)
4943  stat[0].const_keys.merge(possible_keys);
4944  else if (!eq_func)
4945  {
4946  /*
4947  Save info to be able check whether this predicate can be
4948  considered as sargable for range analisis after reading const tables.
4949  We do not save info about equalities as update_const_equal_items
4950  will take care of updating info on keys from sargable equalities.
4951  */
4952  (*sargables)--;
4953  (*sargables)->field= field;
4954  (*sargables)->arg_value= value;
4955  (*sargables)->num_values= num_values;
4956  }
4957  /*
4958  We can't always use indexes when comparing a string index to a
4959  number. cmp_type() is checked to allow compare of dates to numbers.
4960  eq_func is NEVER true when num_values > 1
4961  */
4962  if (!eq_func)
4963  return;
4964  if (field->result_type() == STRING_RESULT)
4965  {
4966  if ((*value)->result_type() != STRING_RESULT)
4967  {
4968  if (field->cmp_type() != (*value)->result_type())
4969  {
4970  warn_index_not_applicable(stat->join->thd, field, possible_keys);
4971  return;
4972  }
4973  }
4974  else
4975  {
4976  /*
4977  Can't optimize datetime_column=indexed_varchar_column,
4978  also can't use indexes if the effective collation
4979  of the operation differ from the field collation.
4980  IndexedTimeComparedToDate: can't optimize
4981  'indexed_time = temporal_expr_with_date_part' because:
4982  - without index, a TIME column with value '48:00:00' is equal to a
4983  DATETIME column with value 'CURDATE() + 2 days'
4984  - with ref access into the TIME column, CURDATE() + 2 days becomes
4985  "00:00:00" (Field_timef::store_internal() simply extracts the time
4986  part from the datetime) which is a lookup key which does not match
4987  "48:00:00"; so ref access is not be able to give the same result
4988  as without index, so is disabled.
4989  On the other hand, we can optimize indexed_datetime = time
4990  because Field_temporal_with_date::store_time() will convert
4991  48:00:00 to CURDATE() + 2 days which is the correct lookup key.
4992  */
4993  if ((!field->is_temporal() && value[0]->is_temporal()) ||
4994  (field->cmp_type() == STRING_RESULT &&
4995  field->charset() != cond->compare_collation()) ||
4996  field_time_cmp_date(field, value[0]))
4997  {
4998  warn_index_not_applicable(stat->join->thd, field, possible_keys);
4999  return;
5000  }
5001  }
5002  }
5003  }
5004  }
5005  /*
5006  For the moment eq_func is always true. This slot is reserved for future
5007  extensions where we want to remembers other things than just eq comparisons
5008  */
5009  DBUG_ASSERT(eq_func);
5010  /*
5011  If the condition has form "tbl.keypart = othertbl.field" and
5012  othertbl.field can be NULL, there will be no matches if othertbl.field
5013  has NULL value.
5014  We use null_rejecting in add_not_null_conds() to add
5015  'othertbl.field IS NOT NULL' to tab->m_condition, if this is not an outer
5016  join. We also use it to shortcut reading "tbl" when othertbl.field is
5017  found to be a NULL value (in join_read_always_key() and BKA).
5018  */
5019  bool null_rejecting;
5020  Item *real= (*value)->real_item();
5021  if (((cond->functype() == Item_func::EQ_FUNC) ||
5022  (cond->functype() == Item_func::MULT_EQUAL_FUNC)) &&
5023  (real->type() == Item::FIELD_ITEM) &&
5024  ((Item_field*)real)->field->maybe_null())
5025  null_rejecting= true;
5026  else
5027  null_rejecting= false;
5028 
5029  /* Store possible eq field */
5030  new (*key_fields)
5031  Key_field(field, *value, and_level, exists_optimize, eq_func,
5032  null_rejecting, NULL, get_semi_join_select_list_index(field));
5033  (*key_fields)++;
5034 }
5035 
5058 static void
5059 add_key_equal_fields(Key_field **key_fields, uint and_level,
5060  Item_func *cond, Item_field *field_item,
5061  bool eq_func, Item **val,
5062  uint num_values, table_map usable_tables,
5063  SARGABLE_PARAM **sargables)
5064 {
5065  Field *field= field_item->field;
5066  add_key_field(key_fields, and_level, cond, field,
5067  eq_func, val, num_values, usable_tables, sargables);
5068  Item_equal *item_equal= field_item->item_equal;
5069  if (item_equal)
5070  {
5071  /*
5072  Add to the set of possible key values every substitution of
5073  the field for an equal field included into item_equal
5074  */
5075  Item_equal_iterator it(*item_equal);
5076  Item_field *item;
5077  while ((item= it++))
5078  {
5079  if (!field->eq(item->field))
5080  {
5081  add_key_field(key_fields, and_level, cond, item->field,
5082  eq_func, val, num_values, usable_tables,
5083  sargables);
5084  }
5085  }
5086  }
5087 }
5088 
5089 
5102 static bool
5103 is_local_field (Item *field)
5104 {
5105  return field->real_item()->type() == Item::FIELD_ITEM
5106  && !(field->used_tables() & OUTER_REF_TABLE_BIT)
5107  && !((Item_field *)field->real_item())->depended_from;
5108 }
5109 
5110 
5111 static void
5112 add_key_fields(JOIN *join, Key_field **key_fields, uint *and_level,
5113  Item *cond, table_map usable_tables,
5114  SARGABLE_PARAM **sargables)
5115 {
5116  if (cond->type() == Item_func::COND_ITEM)
5117  {
5118  List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
5119  Key_field *org_key_fields= *key_fields;
5120 
5121  if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
5122  {
5123  Item *item;
5124  while ((item=li++))
5125  add_key_fields(join, key_fields, and_level, item, usable_tables,
5126  sargables);
5127  for (; org_key_fields != *key_fields ; org_key_fields++)
5128  org_key_fields->level= *and_level;
5129  }
5130  else
5131  {
5132  (*and_level)++;
5133  add_key_fields(join, key_fields, and_level, li++, usable_tables,
5134  sargables);
5135  Item *item;
5136  while ((item=li++))
5137  {
5138  Key_field *start_key_fields= *key_fields;
5139  (*and_level)++;
5140  add_key_fields(join, key_fields, and_level, item, usable_tables,
5141  sargables);
5142  *key_fields=merge_key_fields(org_key_fields,start_key_fields,
5143  *key_fields,++(*and_level));
5144  }
5145  }
5146  return;
5147  }
5148 
5149  /*
5150  Subquery optimization: Conditions that are pushed down into subqueries
5151  are wrapped into Item_func_trig_cond. We process the wrapped condition
5152  but need to set cond_guard for Key_use elements generated from it.
5153  */
5154  {
5155  if (cond->type() == Item::FUNC_ITEM &&
5156  ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
5157  {
5158  Item *cond_arg= ((Item_func*)cond)->arguments()[0];
5159  if (!join->group_list && !join->order &&
5160  join->unit->item &&
5161  join->unit->item->substype() == Item_subselect::IN_SUBS &&
5162  !join->unit->is_union())
5163  {
5164  Key_field *save= *key_fields;
5165  add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
5166  sargables);
5167  // Indicate that this ref access candidate is for subquery lookup:
5168  for (; save != *key_fields; save++)
5169  save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
5170  }
5171  return;
5172  }
5173  }
5174 
5175  /* If item is of type 'field op field/constant' add it to key_fields */
5176  if (cond->type() != Item::FUNC_ITEM)
5177  return;
5178  Item_func *cond_func= (Item_func*) cond;
5179  switch (cond_func->select_optimize()) {
5180  case Item_func::OPTIMIZE_NONE:
5181  break;
5182  case Item_func::OPTIMIZE_KEY:
5183  {
5184  Item **values;
5185  /*
5186  Build list of possible keys for 'a BETWEEN low AND high'.
5187  It is handled similar to the equivalent condition
5188  'a >= low AND a <= high':
5189  */
5190  if (cond_func->functype() == Item_func::BETWEEN)
5191  {
5192  Item_field *field_item;
5193  bool equal_func= FALSE;
5194  uint num_values= 2;
5195  values= cond_func->arguments();
5196 
5197  bool binary_cmp= (values[0]->real_item()->type() == Item::FIELD_ITEM)
5198  ? ((Item_field*)values[0]->real_item())->field->binary()
5199  : TRUE;
5200 
5201  /*
5202  Additional optimization: If 'low = high':
5203  Handle as if the condition was "t.key = low".
5204  */
5205  if (!((Item_func_between*)cond_func)->negated &&
5206  values[1]->eq(values[2], binary_cmp))
5207  {
5208  equal_func= TRUE;
5209  num_values= 1;
5210  }
5211 
5212  /*
5213  Append keys for 'field <cmp> value[]' if the
5214  condition is of the form::
5215  '<field> BETWEEN value[1] AND value[2]'
5216  */
5217  if (is_local_field (values[0]))
5218  {
5219  field_item= (Item_field *) (values[0]->real_item());
5220  add_key_equal_fields(key_fields, *and_level, cond_func,
5221  field_item, equal_func, &values[1],
5222  num_values, usable_tables, sargables);
5223  }
5224  /*
5225  Append keys for 'value[0] <cmp> field' if the
5226  condition is of the form:
5227  'value[0] BETWEEN field1 AND field2'
5228  */
5229  for (uint i= 1; i <= num_values; i++)
5230  {
5231  if (is_local_field (values[i]))
5232  {
5233  field_item= (Item_field *) (values[i]->real_item());
5234  add_key_equal_fields(key_fields, *and_level, cond_func,
5235  field_item, equal_func, values,
5236  1, usable_tables, sargables);
5237  }
5238  }
5239  } // if ( ... Item_func::BETWEEN)
5240 
5241  // IN, NE
5242  else if (is_local_field (cond_func->key_item()) &&
5243  !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
5244  {
5245  values= cond_func->arguments()+1;
5246  if (cond_func->functype() == Item_func::NE_FUNC &&
5247  is_local_field (cond_func->arguments()[1]))
5248  values--;
5249  DBUG_ASSERT(cond_func->functype() != Item_func::IN_FUNC ||
5250  cond_func->argument_count() != 2);
5251  add_key_equal_fields(key_fields, *and_level, cond_func,
5252  (Item_field*) (cond_func->key_item()->real_item()),
5253  0, values,
5254  cond_func->argument_count()-1,
5255  usable_tables, sargables);
5256  }
5257  break;
5258  }
5259  case Item_func::OPTIMIZE_OP:
5260  {
5261  bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
5262  cond_func->functype() == Item_func::EQUAL_FUNC);
5263 
5264  if (is_local_field (cond_func->arguments()[0]))
5265  {
5266  add_key_equal_fields(key_fields, *and_level, cond_func,
5267  (Item_field*) (cond_func->arguments()[0])->real_item(),
5268  equal_func,
5269  cond_func->arguments()+1, 1, usable_tables,
5270  sargables);
5271  }
5272  if (is_local_field (cond_func->arguments()[1]) &&
5273  cond_func->functype() != Item_func::LIKE_FUNC)
5274  {
5275  add_key_equal_fields(key_fields, *and_level, cond_func,
5276  (Item_field*) (cond_func->arguments()[1])->real_item(),
5277  equal_func,
5278  cond_func->arguments(),1,usable_tables,
5279  sargables);
5280  }
5281  break;
5282  }
5283  case Item_func::OPTIMIZE_NULL:
5284  /* column_name IS [NOT] NULL */
5285  if (is_local_field (cond_func->arguments()[0]) &&
5286  !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
5287  {
5288  Item *tmp=new Item_null;
5289  if (unlikely(!tmp)) // Should never be true
5290  return;
5291  add_key_equal_fields(key_fields, *and_level, cond_func,
5292  (Item_field*) (cond_func->arguments()[0])->real_item(),
5293  cond_func->functype() == Item_func::ISNULL_FUNC,
5294  &tmp, 1, usable_tables, sargables);
5295  }
5296  break;
5297  case Item_func::OPTIMIZE_EQUAL:
5298  Item_equal *item_equal= (Item_equal *) cond;
5299  Item *const_item= item_equal->get_const();
5300  Item_equal_iterator it(*item_equal);
5301  Item_field *item;
5302  if (const_item)
5303  {
5304  /*
5305  For each field field1 from item_equal consider the equality
5306  field1=const_item as a condition allowing an index access of the table
5307  with field1 by the keys value of field1.
5308  */
5309  while ((item= it++))
5310  {
5311  add_key_field(key_fields, *and_level, cond_func, item->field,
5312  TRUE, &const_item, 1, usable_tables, sargables);
5313  }
5314  }
5315  else
5316  {
5317  /*
5318  Consider all pairs of different fields included into item_equal.
5319  For each of them (field1, field1) consider the equality
5320  field1=field2 as a condition allowing an index access of the table
5321  with field1 by the keys value of field2.
5322  */
5323  Item_equal_iterator fi(*item_equal);
5324  while ((item= fi++))
5325  {
5326  Field *field= item->field;
5327  while ((item= it++))
5328  {
5329  if (!field->eq(item->field))
5330  {
5331  add_key_field(key_fields, *and_level, cond_func, field,
5332  TRUE, (Item **) &item, 1, usable_tables,
5333  sargables);
5334  }
5335  }
5336  it.rewind();
5337  }
5338  }
5339  break;
5340  }
5341 }
5342 
5343 
5344 /*
5345  Add all keys with uses 'field' for some keypart
5346  If field->and_level != and_level then only mark key_part as const_part
5347 
5348  RETURN
5349  0 - OK
5350  1 - Out of memory.
5351 */
5352 
5353 static bool
5354 add_key_part(Key_use_array *keyuse_array, Key_field *key_field)
5355 {
5356  Field *field=key_field->field;
5357  TABLE *form= field->table;
5358 
5359  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
5360  {
5361  for (uint key=0 ; key < form->s->keys ; key++)
5362  {
5363  if (!(form->keys_in_use_for_query.is_set(key)))
5364  continue;
5365  if (form->key_info[key].flags & (HA_FULLTEXT | HA_SPATIAL))
5366  continue; // ToDo: ft-keys in non-ft queries. SerG
5367 
5368  uint key_parts= actual_key_parts(&form->key_info[key]);
5369  for (uint part=0 ; part < key_parts ; part++)
5370  {
5371  if (field->eq(form->key_info[key].key_part[part].field))
5372  {
5373  const Key_use keyuse(field->table,
5374  key_field->val,
5375  key_field->val->used_tables(),
5376  key,
5377  part,
5378  key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL,
5379  (key_part_map) 1 << part,
5380  ~(ha_rows) 0, // will be set in optimize_keyuse
5381  key_field->null_rejecting,
5382  key_field->cond_guard,
5383  key_field->sj_pred_no);
5384  if (keyuse_array->push_back(keyuse))
5385  return TRUE;
5386  }
5387  }
5388  }
5389  }
5390  return FALSE;
5391 }
5392 
5393 
5394 static bool
5395 add_ft_keys(Key_use_array *keyuse_array,
5396  JOIN_TAB *stat,Item *cond,table_map usable_tables)
5397 {
5398  Item_func_match *cond_func=NULL;
5399 
5400  if (!cond)
5401  return FALSE;
5402 
5403  if (cond->type() == Item::FUNC_ITEM)
5404  {
5405  Item_func *func=(Item_func *)cond;
5406  Item_func::Functype functype= func->functype();
5407  if (functype == Item_func::FT_FUNC)
5408  cond_func=(Item_func_match *)cond;
5409  else if (func->arg_count == 2)
5410  {
5411  Item *arg0=(Item *)(func->arguments()[0]),
5412  *arg1=(Item *)(func->arguments()[1]);
5413  if (arg1->const_item() && arg1->cols() == 1 &&
5414  arg0->type() == Item::FUNC_ITEM &&
5415  ((Item_func *) arg0)->functype() == Item_func::FT_FUNC &&
5416  ((functype == Item_func::GE_FUNC && arg1->val_real() > 0) ||
5417  (functype == Item_func::GT_FUNC && arg1->val_real() >=0)))
5418  cond_func= (Item_func_match *) arg0;
5419  else if (arg0->const_item() &&
5420  arg1->type() == Item::FUNC_ITEM &&
5421  ((Item_func *) arg1)->functype() == Item_func::FT_FUNC &&
5422  ((functype == Item_func::LE_FUNC && arg0->val_real() > 0) ||
5423  (functype == Item_func::LT_FUNC && arg0->val_real() >=0)))
5424  cond_func= (Item_func_match *) arg1;
5425  }
5426  }
5427  else if (cond->type() == Item::COND_ITEM)
5428  {
5429  List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
5430 
5431  if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
5432  {
5433  Item *item;
5434  while ((item=li++))
5435  {
5436  if (add_ft_keys(keyuse_array,stat,item,usable_tables))
5437  return TRUE;
5438  }
5439  }
5440  }
5441 
5442  if (!cond_func || cond_func->key == NO_SUCH_KEY ||
5443  !(usable_tables & cond_func->table->map))
5444  return FALSE;
5445 
5446  const Key_use keyuse(cond_func->table,
5447  cond_func,
5448  cond_func->key_item()->used_tables(),
5449  cond_func->key,
5450  FT_KEYPART,
5451  0, // optimize
5452  0, // keypart_map
5453  ~(ha_rows)0, // ref_table_rows
5454  false, // null_rejecting
5455  NULL, // cond_guard
5456  UINT_MAX); // sj_pred_no
5457  return keyuse_array->push_back(keyuse);
5458 }
5459 
5460 
5461 static int sort_keyuse(Key_use *a, Key_use *b)
5462 {
5463  int res;
5464  if (a->table->tablenr != b->table->tablenr)
5465  return (int) (a->table->tablenr - b->table->tablenr);
5466  if (a->key != b->key)
5467  return (int) (a->key - b->key);
5468  if (a->keypart != b->keypart)
5469  return (int) (a->keypart - b->keypart);
5470  // Place const values before other ones
5471  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
5472  test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
5473  return res;
5474  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
5475  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
5476  (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
5477 }
5478 
5479 
5480 /*
5481  Add to Key_field array all 'ref' access candidates within nested join.
5482 
5483  This function populates Key_field array with entries generated from the
5484  ON condition of the given nested join, and does the same for nested joins
5485  contained within this nested join.
5486 
5487  @param[in] nested_join_table Nested join pseudo-table to process
5488  @param[in,out] end End of the key field array
5489  @param[in,out] and_level And-level
5490  @param[in,out] sargables Array of found sargable candidates
5491 
5492 
5493  @note
5494  We can add accesses to the tables that are direct children of this nested
5495  join (1), and are not inner tables w.r.t their neighbours (2).
5496 
5497  Example for #1 (outer brackets pair denotes nested join this function is
5498  invoked for):
5499  @code
5500  ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
5501  @endcode
5502  Example for #2:
5503  @code
5504  ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
5505  @endcode
5506  In examples 1-2 for condition cond, we can add 'ref' access candidates to
5507  t1 only.
5508  Example #3:
5509  @code
5510  ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
5511  @endcode
5512  Here we can add 'ref' access candidates for t1 and t2, but not for t3.
5513 */
5514 
5515 static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
5516  Key_field **end, uint *and_level,
5517  SARGABLE_PARAM **sargables)
5518 {
5519  List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
5520  List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
5521  bool have_another = FALSE;
5522  table_map tables= 0;
5523  TABLE_LIST *table;
5524  DBUG_ASSERT(nested_join_table->nested_join);
5525 
5526  while ((table= li++) || (have_another && (li=li2, have_another=FALSE,
5527  (table= li++))))
5528  {
5529  if (table->nested_join)
5530  {
5531  if (!table->join_cond())
5532  {
5533  /* It's a semi-join nest. Walk into it as if it wasn't a nest */
5534  have_another= TRUE;
5535  li2= li;
5536  li= List_iterator<TABLE_LIST>(table->nested_join->join_list);
5537  }
5538  else
5539  add_key_fields_for_nj(join, table, end, and_level, sargables);
5540  }
5541  else
5542  if (!table->join_cond())
5543  tables |= table->table->map;
5544  }
5545  if (nested_join_table->join_cond())
5546  add_key_fields(join, end, and_level, nested_join_table->join_cond(), tables,
5547  sargables);
5548 }
5549 
5550 
5583 bool
5585 {
5586  Item_sum **sum_item_ptr;
5587  bool result= false;
5588  Field_map first_aggdistinct_fields;
5589 
5590  if (join->primary_tables > 1 || /* reference more than 1 table */
5591  join->select_distinct || /* or a DISTINCT */
5592  join->select_lex->olap == ROLLUP_TYPE) /* Check (B3) for ROLLUP */
5593  return false;
5594 
5595  if (join->make_sum_func_list(join->all_fields, join->fields_list, true))
5596  return false;
5597 
5598  for (sum_item_ptr= join->sum_funcs; *sum_item_ptr; sum_item_ptr++)
5599  {
5600  Item_sum *sum_item= *sum_item_ptr;
5601  Field_map cur_aggdistinct_fields;
5602  Item *expr;
5603  /* aggregate is not AGGFN(DISTINCT) or more than 1 argument to it */
5604  switch (sum_item->sum_func())
5605  {
5606  case Item_sum::MIN_FUNC:
5607  case Item_sum::MAX_FUNC:
5608  continue;
5609  case Item_sum::COUNT_DISTINCT_FUNC:
5610  break;
5611  case Item_sum::AVG_DISTINCT_FUNC:
5612  case Item_sum::SUM_DISTINCT_FUNC:
5613  if (sum_item->get_arg_count() == 1)
5614  break;
5615  /* fall through */
5616  default: return false;
5617  }
5618 
5619  for (uint i= 0; i < sum_item->get_arg_count(); i++)
5620  {
5621  expr= sum_item->get_arg(i);
5622  /* The AGGFN(DISTINCT) arg is not an attribute? */
5623  if (expr->real_item()->type() != Item::FIELD_ITEM)
5624  return false;
5625 
5626  Item_field* item= static_cast<Item_field*>(expr->real_item());
5627  if (out_args)
5628  out_args->push_back(item);
5629 
5630  cur_aggdistinct_fields.set_bit(item->field->field_index);
5631  result= true;
5632  }
5633  /*
5634  If there are multiple aggregate functions, make sure that they all
5635  refer to exactly the same set of columns.
5636  */
5637  if (first_aggdistinct_fields.is_clear_all())
5638  first_aggdistinct_fields.merge(cur_aggdistinct_fields);
5639  else if (first_aggdistinct_fields != cur_aggdistinct_fields)
5640  return false;
5641  }
5642 
5643  return result;
5644 }
5645 
5646 
5660 static void trace_indices_added_group_distinct(Opt_trace_context *trace,
5661  const JOIN_TAB *join_tab,
5662  const key_map new_keys,
5663  const char* cause)
5664 {
5665 #ifdef OPTIMIZER_TRACE
5666  if (likely(!trace->is_started()))
5667  return;
5668 
5669  KEY *key_info= join_tab->table->key_info;
5670  key_map existing_keys= join_tab->const_keys;
5671  uint nbrkeys= join_tab->table->s->keys;
5672 
5673  Opt_trace_object trace_summary(trace, "const_keys_added");
5674  {
5675  Opt_trace_array trace_key(trace,"keys");
5676  for (uint j= 0 ; j < nbrkeys ; j++)
5677  if (new_keys.is_set(j) && !existing_keys.is_set(j))
5678  trace_key.add_utf8(key_info[j].name);
5679  }
5680  trace_summary.add_alnum("cause", cause);
5681 #endif
5682 }
5683 
5684 
5712 static void
5713 add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
5714 {
5715  List<Item_field> indexed_fields;
5716  List_iterator<Item_field> indexed_fields_it(indexed_fields);
5717  ORDER *cur_group;
5718  Item_field *cur_item;
5719  const char *cause;
5720 
5721  if (join->group_list)
5722  { /* Collect all query fields referenced in the GROUP clause. */
5723  for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
5724  (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
5725  (uchar*) &indexed_fields);
5726  cause= "group_by";
5727  }
5728  else if (join->select_distinct)
5729  { /* Collect all query fields referenced in the SELECT clause. */
5730  List<Item> &select_items= join->fields_list;
5731  List_iterator<Item> select_items_it(select_items);
5732  Item *item;
5733  while ((item= select_items_it++))
5734  item->walk(&Item::collect_item_field_processor, 0,
5735  (uchar*) &indexed_fields);
5736  cause= "distinct";
5737  }
5738  else if (join->tmp_table_param.sum_func_count &&
5739  is_indexed_agg_distinct(join, &indexed_fields))
5740  {
5741  /*
5742  SELECT list with AGGFN(distinct col). The query qualifies for
5743  loose index scan, and is_indexed_agg_distinct() has already
5744  collected all referenced fields into indexed_fields.
5745  */
5746  join->sort_and_group= 1;
5747  cause= "indexed_distinct_aggregate";
5748  }
5749  else
5750  return;
5751 
5752  if (indexed_fields.elements == 0)
5753  return;
5754 
5755  key_map possible_keys;
5756  possible_keys.set_all();
5757 
5758  /* Intersect the keys of all group fields. */
5759  while ((cur_item= indexed_fields_it++))
5760  {
5761  if (cur_item->used_tables() != join_tab->table->map)
5762  {
5763  /*
5764  Doing GROUP BY or DISTINCT on a field in another table so no
5765  index in this table is usable
5766  */
5767  return;
5768  }
5769  else
5770  possible_keys.intersect(cur_item->field->part_of_key);
5771  }
5772 
5773  /*
5774  At this point, possible_keys has key bits set only for usable
5775  indexes because indexed_fields is non-empty and if any of the
5776  fields belong to a different table the function would exit in the
5777  loop above.
5778  */
5779 
5780  if (!possible_keys.is_clear_all() &&
5781  !possible_keys.is_subset(join_tab->const_keys))
5782  {
5783  trace_indices_added_group_distinct(&join->thd->opt_trace, join_tab,
5784  possible_keys, cause);
5785  join_tab->const_keys.merge(possible_keys);
5786  join_tab->keys.merge(possible_keys);
5787  }
5788 
5789 }
5790 
5812 static bool
5813 update_ref_and_keys(THD *thd, Key_use_array *keyuse,JOIN_TAB *join_tab,
5814  uint tables, Item *cond, COND_EQUAL *cond_equal,
5815  table_map normal_tables, SELECT_LEX *select_lex,
5816  SARGABLE_PARAM **sargables)
5817 {
5818  uint and_level,i,found_eq_constant;
5819  Key_field *key_fields, *end, *field;
5820  uint sz;
5821  uint m= max(select_lex->max_equal_elems, 1U);
5822 
5823  /*
5824  We use the same piece of memory to store both Key_field
5825  and SARGABLE_PARAM structure.
5826  Key_field values are placed at the beginning this memory
5827  while SARGABLE_PARAM values are put at the end.
5828  All predicates that are used to fill arrays of Key_field
5829  and SARGABLE_PARAM structures have at most 2 arguments
5830  except BETWEEN predicates that have 3 arguments and
5831  IN predicates.
5832  This any predicate if it's not BETWEEN/IN can be used
5833  directly to fill at most 2 array elements, either of Key_field
5834  or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
5835  can be filled as this predicate is considered as
5836  saragable with respect to each of its argument.
5837  An IN predicate can require at most 1 element as currently
5838  it is considered as sargable only for its first argument.
5839  Multiple equality can add elements that are filled after
5840  substitution of field arguments by equal fields. There
5841  can be not more than select_lex->max_equal_elems such
5842  substitutions.
5843  */
5844  sz= max(sizeof(Key_field), sizeof(SARGABLE_PARAM)) *
5845  (((select_lex->cond_count + 1) * 2 +
5846  select_lex->between_count) * m + 1);
5847  if (!(key_fields=(Key_field*) thd->alloc(sz)))
5848  return TRUE; /* purecov: inspected */
5849  and_level= 0;
5850  field= end= key_fields;
5851  *sargables= (SARGABLE_PARAM *) key_fields +
5852  (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
5853  /* set a barrier for the array of SARGABLE_PARAM */
5854  (*sargables)[0].field= 0;
5855 
5856  if (cond)
5857  {
5858  add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
5859  sargables);
5860  for (Key_field *fld= field; fld != end ; fld++)
5861  {
5862  /* Mark that we can optimize LEFT JOIN */
5863  if (fld->val->type() == Item::NULL_ITEM &&
5864  !fld->field->real_maybe_null())
5865  {
5866  /*
5867  Example:
5868  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.a WHERE t2.a IS NULL;
5869  this just wants rows of t1 where t1.a does not exist in t2.
5870  */
5871  fld->field->table->reginfo.not_exists_optimize=1;
5872  }
5873  }
5874  }
5875 
5876  for (i=0 ; i < tables ; i++)
5877  {
5878  /*
5879  Block the creation of keys for inner tables of outer joins.
5880  Here only the outer joins that can not be converted to
5881  inner joins are left and all nests that can be eliminated
5882  are flattened.
5883  In the future when we introduce conditional accesses
5884  for inner tables in outer joins these keys will be taken
5885  into account as well.
5886  */
5887  if (*join_tab[i].on_expr_ref)
5888  add_key_fields(join_tab->join, &end, &and_level,
5889  *join_tab[i].on_expr_ref,
5890  join_tab[i].table->map, sargables);
5891  }
5892 
5893  /* Process ON conditions for the nested joins */
5894  {
5895  List_iterator<TABLE_LIST> li(*join_tab->join->join_list);
5896  TABLE_LIST *table;
5897  while ((table= li++))
5898  {
5899  if (table->nested_join)
5900  add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
5901  sargables);
5902  }
5903  }
5904 
5905  /* Generate keys descriptions for derived tables */
5906  if (select_lex->materialized_table_count)
5907  {
5908  if (select_lex->join->generate_derived_keys())
5909  return true;
5910  }
5911  /* fill keyuse with found key parts */
5912  for ( ; field != end ; field++)
5913  {
5914  if (add_key_part(keyuse,field))
5915  return true;
5916  }
5917 
5918  if (select_lex->ftfunc_list->elements)
5919  {
5920  if (add_ft_keys(keyuse,join_tab,cond,normal_tables))
5921  return true;
5922  }
5923 
5924  /*
5925  Sort the array of possible keys and remove the following key parts:
5926  - ref if there is a keypart which is a ref and a const.
5927  (e.g. if there is a key(a,b) and the clause is a=3 and b=7 and b=t2.d,
5928  then we skip the key part corresponding to b=t2.d)
5929  - keyparts without previous keyparts
5930  (e.g. if there is a key(a,b,c) but only b < 5 (or a=2 and c < 3) is
5931  used in the query, we drop the partial key parts from consideration).
5932  Special treatment for ft-keys.
5933  */
5934  if (!keyuse->empty())
5935  {
5936  Key_use *save_pos, *use;
5937 
5938  my_qsort(keyuse->begin(), keyuse->size(), keyuse->element_size(),
5939  reinterpret_cast<qsort_cmp>(sort_keyuse));
5940 
5941  const Key_use key_end(NULL, NULL, 0, 0, 0, 0, 0, 0, false, NULL, 0);
5942  if (keyuse->push_back(key_end)) // added for easy testing
5943  return TRUE;
5944 
5945  use= save_pos= keyuse->begin();
5946  const Key_use *prev= &key_end;
5947  found_eq_constant=0;
5948  for (i=0 ; i < keyuse->size()-1 ; i++,use++)
5949  {
5950  if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
5951  use->table->const_key_parts[use->key]|= use->keypart_map;
5952  if (use->keypart != FT_KEYPART)
5953  {
5954  if (use->key == prev->key && use->table == prev->table)
5955  {
5956  if (prev->keypart+1 < use->keypart ||
5957  (prev->keypart == use->keypart && found_eq_constant))
5958  continue; /* remove */
5959  }
5960  else if (use->keypart != 0) // First found must be 0
5961  continue;
5962  }
5963 
5964 #if defined(__GNUC__) && !MY_GNUC_PREREQ(4,4)
5965  /*
5966  Old gcc used a memcpy(), which is undefined if save_pos==use:
5967  http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19410
5968  http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39480
5969  */
5970  if (save_pos != use)
5971 #endif
5972  *save_pos= *use;
5973  prev=use;
5974  found_eq_constant= !use->used_tables;
5975  /* Save ptr to first use */
5976  if (!use->table->reginfo.join_tab->keyuse)
5977  use->table->reginfo.join_tab->keyuse=save_pos;
5978  use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
5979  save_pos++;
5980  }
5981  i= (uint) (save_pos - keyuse->begin());
5982  keyuse->at(i) = key_end;
5983  keyuse->chop(i);
5984  }
5985  print_keyuse_array(&thd->opt_trace, keyuse);
5986 
5987  return false;
5988 }
5989 
5990 
6002 Key_use_array *create_keyuse_for_table(THD *thd, TABLE *table, uint keyparts,
6003  Item_field **fields,
6004  List<Item> outer_exprs)
6005 {
6006  void *mem= thd->alloc(sizeof(Key_use_array));
6007  if (!mem)
6008  return NULL;
6009  Key_use_array *keyuses= new (mem) Key_use_array(thd->mem_root);
6010 
6011  List_iterator<Item> outer_expr(outer_exprs);
6012 
6013  for (uint keypartno= 0; keypartno < keyparts; keypartno++)
6014  {
6015  Item *const item= outer_expr++;
6016  Key_field key_field(fields[keypartno]->field, item, 0, 0, true,
6017  // null_rejecting must be true for field items only,
6018  // add_not_null_conds() is incapable of handling
6019  // other item types.
6020  (item->type() == Item::FIELD_ITEM),
6021  NULL, UINT_MAX);
6022  if (add_key_part(keyuses, &key_field))
6023  return NULL;
6024  }
6025  const Key_use key_end(NULL, NULL, 0, 0, 0, 0, 0, 0, false, NULL, 0);
6026  if (keyuses->push_back(key_end)) // added for easy testing
6027  return NULL;
6028 
6029  return keyuses;
6030 }
6031 
6032 
6035 static void
6036 set_position(JOIN *join, uint idx, JOIN_TAB *table, Key_use *key)
6037 {
6038  join->positions[idx].table= table;
6039  join->positions[idx].key=key;
6040  join->positions[idx].records_read=1.0; /* This is a const table */
6041  join->positions[idx].ref_depend_map= 0;
6042 
6043  join->positions[idx].loosescan_key= MAX_KEY; /* Not a LooseScan */
6044  join->positions[idx].sj_strategy= SJ_OPT_NONE;
6045  join->positions[idx].use_join_buffer= FALSE;
6046 
6047  /* Move the const table as down as possible in best_ref */
6048  JOIN_TAB **pos=join->best_ref+idx+1;
6049  JOIN_TAB *next=join->best_ref[idx];
6050  for (;next != table ; pos++)
6051  {
6052  JOIN_TAB *tmp=pos[0];
6053  pos[0]=next;
6054  next=tmp;
6055  }
6056  join->best_ref[idx]=table;
6057 }
6058 
6059 
6102 static void
6103 make_outerjoin_info(JOIN *join)
6104 {
6105  DBUG_ENTER("make_outerjoin_info");
6106 
6107  DBUG_ASSERT(join->outer_join);
6108 
6109  for (uint i= join->const_tables; i < join->tables; i++)
6110  {
6111  JOIN_TAB *const tab= join->join_tab + i;
6112  TABLE *const table= tab->table;
6113 
6114  if (!table)
6115  continue;
6116 
6117  TABLE_LIST *const tbl= table->pos_in_table_list;
6118 
6119  if (tbl->outer_join)
6120  {
6121  /*
6122  Table tab is the only one inner table for outer join.
6123  (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
6124  is in the query above.)
6125  */
6126  tab->last_inner= tab->first_inner= tab;
6127  tab->on_expr_ref= tbl->join_cond_ref();
6128  tab->cond_equal= tbl->cond_equal;
6129  /*
6130  If this outer join nest is embedded in another join nest,
6131  link the join-tabs:
6132  */
6133  TABLE_LIST *const outer_join_nest= tbl->outer_join_nest();
6134  if (outer_join_nest)
6135  tab->first_upper= outer_join_nest->nested_join->first_nested;
6136  }
6137  for (TABLE_LIST *embedding= tbl->embedding;
6138  embedding;
6139  embedding= embedding->embedding)
6140  {
6141  // Ignore join nests that are not outer join nests:
6142  if (!embedding->join_cond())
6143  continue;
6144  NESTED_JOIN *const nested_join= embedding->nested_join;
6145  if (!nested_join->nj_counter)
6146  {
6147  /*
6148  Table tab is the first inner table for nested_join.
6149  Save reference to it in the nested join structure.
6150  */
6151  nested_join->first_nested= tab;
6152  tab->on_expr_ref= embedding->join_cond_ref();
6153  tab->cond_equal= tbl->cond_equal;
6154 
6155  TABLE_LIST *const outer_join_nest= embedding->outer_join_nest();
6156  if (outer_join_nest)
6157  tab->first_upper= outer_join_nest->nested_join->first_nested;
6158  }
6159  if (!tab->first_inner)
6160  tab->first_inner= nested_join->first_nested;
6161  if (++nested_join->nj_counter < nested_join->nj_total)
6162  break;
6163  /* Table tab is the last inner table for nested join. */
6164  nested_join->first_nested->last_inner= tab;
6165  }
6166  }
6167  DBUG_VOID_RETURN;
6168 }
6169 
6186 static Item*
6187 add_found_match_trig_cond(JOIN_TAB *tab, Item *cond, JOIN_TAB *root_tab)
6188 {
6189  Item *tmp;
6190  DBUG_ASSERT(cond != 0);
6191  if (tab == root_tab)
6192  return cond;
6193  if ((tmp= add_found_match_trig_cond(tab->first_upper, cond, root_tab)))
6194  tmp= new Item_func_trig_cond(tmp, &tab->found, tab,
6196  if (tmp)
6197  {
6198  tmp->quick_fix_field();
6199  tmp->update_used_tables();
6200  }
6201  return tmp;
6202 }
6203 
6204 
6215 static bool pushdown_on_conditions(JOIN* join, JOIN_TAB *last_tab)
6216 {
6217  DBUG_ENTER("pushdown_on_conditions");
6218 
6219  /* First push down constant conditions from on expressions */
6220  for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
6221  join_tab < join->join_tab+join->tables ; join_tab++)
6222  {
6223  if (join_tab->on_expr_ref && *join_tab->on_expr_ref)
6224  {
6225  JOIN_TAB *cond_tab= join_tab->first_inner;
6226  Item *tmp_cond= make_cond_for_table(*join_tab->on_expr_ref,
6227  join->const_table_map,
6228  (table_map) 0, 0);
6229  if (!tmp_cond)
6230  continue;
6231  tmp_cond= new
6232  Item_func_trig_cond(tmp_cond, &cond_tab->not_null_compl, cond_tab,
6234  if (!tmp_cond)
6235  DBUG_RETURN(true);
6236  tmp_cond->quick_fix_field();
6237 
6238  if (cond_tab->and_with_jt_and_sel_condition(tmp_cond, __LINE__))
6239  DBUG_RETURN(true);
6240  }
6241  }
6242 
6243  JOIN_TAB *first_inner_tab= last_tab->first_inner;
6244 
6245  /* Push down non-constant conditions from on expressions */
6246  while (first_inner_tab && first_inner_tab->last_inner == last_tab)
6247  {
6248  /*
6249  Table last_tab is the last inner table of an outer join.
6250  An on expression is always attached to it.
6251  */
6252  Item *on_expr= *first_inner_tab->on_expr_ref;
6253 
6254  for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
6255  join_tab <= last_tab ; join_tab++)
6256  {
6257  table_map prefix_tables= join_tab->prefix_tables();
6258  table_map added_tables= join_tab->added_tables();
6259 
6260  if (join_tab == last_tab)
6261  {
6262  /*
6263  Need RAND_TABLE_BIT on the last inner table, in case there is a
6264  non-deterministic function in the join condition.
6265  (RAND_TABLE_BIT is set for the last table of the join plan,
6266  but this is not sufficient for join conditions, which may have a
6267  last inner table that is ahead of the last table of the join plan).
6268  */
6269  prefix_tables|= RAND_TABLE_BIT;
6270  added_tables|= RAND_TABLE_BIT;
6271  }
6272  Item *tmp_cond= make_cond_for_table(on_expr, prefix_tables, added_tables,
6273  false);
6274  if (!tmp_cond)
6275  continue;
6276 
6277  JOIN_TAB *cond_tab=
6278  join_tab < first_inner_tab ? first_inner_tab : join_tab;
6279  /*
6280  First add the guards for match variables of
6281  all embedding outer join operations.
6282  */
6283  if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
6284  tmp_cond,
6285  first_inner_tab)))
6286  DBUG_RETURN(1);
6287  /*
6288  Now add the guard turning the predicate off for
6289  the null complemented row.
6290  */
6291  tmp_cond=
6292  new Item_func_trig_cond(tmp_cond, &first_inner_tab->not_null_compl,
6293  first_inner_tab,
6295  if (!tmp_cond)
6296  DBUG_RETURN(true);
6297  tmp_cond->quick_fix_field();
6298 
6299  /* Add the predicate to other pushed down predicates */
6300  if (cond_tab->and_with_jt_and_sel_condition(tmp_cond, __LINE__))
6301  DBUG_RETURN(true);
6302  }
6303  first_inner_tab= first_inner_tab->first_upper;
6304  }
6305  DBUG_RETURN(0);
6306 }
6307 
6308 
6309 /*****************************************************************************
6310  Remove calculation with tables that aren't yet read. Remove also tests
6311  against fields that are read through key where the table is not a
6312  outer join table.
6313  We can't remove tests that are made against columns which are stored
6314  in sorted order.
6315 *****************************************************************************/
6316 
6317 static Item *
6318 part_of_refkey(TABLE *table,Field *field)
6319 {
6320  if (!table->reginfo.join_tab)
6321  return NULL; // field from outer non-select (UPDATE,...)
6322 
6323  uint ref_parts=table->reginfo.join_tab->ref.key_parts;
6324  if (ref_parts)
6325  {
6326  if (table->reginfo.join_tab->has_guarded_conds())
6327  return NULL;
6328 
6329  const KEY_PART_INFO *key_part=
6330  table->key_info[table->reginfo.join_tab->ref.key].key_part;
6331 
6332  for (uint part=0 ; part < ref_parts ; part++,key_part++)
6333  if (field->eq(key_part->field) &&
6334  !(key_part->key_part_flag & HA_PART_KEY_SEG))
6335  return table->reginfo.join_tab->ref.items[part];
6336  }
6337  return NULL;
6338 }
6339 
6340 
6349 static bool test_if_ref(Item *root_cond,
6350  Item_field *left_item,Item *right_item)
6351 {
6352  Field *field=left_item->field;
6353  JOIN_TAB *join_tab= field->table->reginfo.join_tab;
6354  // No need to change const test
6355  if (!field->table->const_table && join_tab &&
6356  (!join_tab->first_inner ||
6357  *join_tab->first_inner->on_expr_ref == root_cond) &&
6358  /* "ref_or_null" implements "x=y or x is null", not "x=y" */
6359  (join_tab->type != JT_REF_OR_NULL))
6360  {
6361  Item *ref_item=part_of_refkey(field->table,field);
6362  if (ref_item && ref_item->eq(right_item,1))
6363  {
6364  right_item= right_item->real_item();
6365  if (right_item->type() == Item::FIELD_ITEM)
6366  return (field->eq_def(((Item_field *) right_item)->field));
6367  /* remove equalities injected by IN->EXISTS transformation */
6368  else if (right_item->type() == Item::CACHE_ITEM)
6369  return ((Item_cache *)right_item)->eq_def (field);
6370  if (right_item->const_item() && !(right_item->is_null()))
6371  {
6372  /*
6373  We can remove binary fields and numerical fields except float,
6374  as float comparison isn't 100 % secure
6375  We have to keep normal strings to be able to check for end spaces
6376 
6377  sergefp: the above seems to be too restrictive. Counterexample:
6378  create table t100 (v varchar(10), key(v)) default charset=latin1;
6379  insert into t100 values ('a'),('a ');
6380  explain select * from t100 where v='a';
6381  The EXPLAIN shows 'using Where'. Running the query returns both
6382  rows, so it seems there are no problems with endspace in the most
6383  frequent case?
6384  */
6385  if (field->binary() &&
6386  field->real_type() != MYSQL_TYPE_STRING &&
6387  field->real_type() != MYSQL_TYPE_VARCHAR &&
6388  (field->type() != MYSQL_TYPE_FLOAT || field->decimals() == 0))
6389  {
6390  return !right_item->save_in_field_no_warnings(field, true);
6391  }
6392  }
6393  }
6394  }
6395  return 0; // keep test
6396 }
6397 
6461 static bool replace_subcondition(JOIN *join, Item **tree,
6462  Item *old_cond, Item *new_cond,
6463  bool do_fix_fields)
6464 {
6465  if (*tree == old_cond)
6466  {
6467  *tree= new_cond;
6468  if (do_fix_fields && new_cond->fix_fields(join->thd, tree))
6469  return TRUE;
6470  join->select_lex->where= *tree;
6471  return FALSE;
6472  }
6473  else if ((*tree)->type() == Item::COND_ITEM)
6474  {
6475  List_iterator<Item> li(*((Item_cond*)(*tree))->argument_list());
6476  Item *item;
6477  while ((item= li++))
6478  {
6479  if (item == old_cond)
6480  {
6481  li.replace(new_cond);
6482  if (do_fix_fields && new_cond->fix_fields(join->thd, li.ref()))
6483  return TRUE;
6484  return FALSE;
6485  }
6486  }
6487  }
6488  else
6489  // If we came here it means there were an error during prerequisites check.
6490  DBUG_ASSERT(FALSE);
6491 
6492  return TRUE;
6493 }
6494 
6495 
6496 static int subq_sj_candidate_cmp(Item_exists_subselect* const *el1,
6497  Item_exists_subselect* const *el2)
6498 {
6499  /*
6500  Remove this assert when we support semijoin on non-IN subqueries.
6501  */
6502  DBUG_ASSERT((*el1)->substype() == Item_subselect::IN_SUBS &&
6503  (*el2)->substype() == Item_subselect::IN_SUBS);
6504  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 :
6505  ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
6506 }
6507 
6508 
6509 static void fix_list_after_tbl_changes(st_select_lex *parent_select,
6510  st_select_lex *removed_select,
6511  List<TABLE_LIST> *tlist)
6512 {
6513  List_iterator<TABLE_LIST> it(*tlist);
6514  TABLE_LIST *table;
6515  while ((table= it++))
6516  {
6517  if (table->join_cond())
6518  table->join_cond()->fix_after_pullout(parent_select, removed_select);
6519  if (table->nested_join)
6520  fix_list_after_tbl_changes(parent_select, removed_select,
6521  &table->nested_join->join_list);
6522  }
6523 }
6524 
6525 
6573 static bool convert_subquery_to_semijoin(JOIN *parent_join,
6574  Item_exists_subselect *subq_pred)
6575 {
6576  SELECT_LEX *parent_lex= parent_join->select_lex;
6577  TABLE_LIST *emb_tbl_nest= NULL;
6578  List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
6579  THD *thd= parent_join->thd;
6580  DBUG_ENTER("convert_subquery_to_semijoin");
6581 
6582  DBUG_ASSERT(subq_pred->substype() == Item_subselect::IN_SUBS);
6583 
6584  /*
6585  Find out where to insert the semi-join nest and the generated condition.
6586 
6587  For t1 LEFT JOIN t2, embedding_join_nest will be t2.
6588  Note that t2 may be a simple table or may itself be a join nest
6589  (e.g. in the case t1 LEFT JOIN (t2 JOIN t3))
6590  */
6591  if ((void*)subq_pred->embedding_join_nest != NULL)
6592  {
6593  if (subq_pred->embedding_join_nest->nested_join)
6594  {
6595  /*
6596  We're dealing with
6597 
6598  ... [LEFT] JOIN ( ... ) ON (subquery AND condition) ...
6599 
6600  The sj-nest will be inserted into the brackets nest.
6601  */
6602  emb_tbl_nest= subq_pred->embedding_join_nest;
6603  emb_join_list= &emb_tbl_nest->nested_join->join_list;
6604  }
6605  else if (!subq_pred->embedding_join_nest->outer_join)
6606  {
6607  /*
6608  We're dealing with
6609 
6610  ... INNER JOIN tblX ON (subquery AND condition) ...
6611 
6612  The sj-nest will be tblX's "sibling", i.e. another child of its
6613  parent. This is ok because tblX is joined as an inner join.
6614  */
6615  emb_tbl_nest= subq_pred->embedding_join_nest->embedding;
6616  if (emb_tbl_nest)
6617  emb_join_list= &emb_tbl_nest->nested_join->join_list;
6618  }
6619  else if (!subq_pred->embedding_join_nest->nested_join)
6620  {
6621  TABLE_LIST *outer_tbl= subq_pred->embedding_join_nest;
6622  /*
6623  We're dealing with
6624 
6625  ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
6626 
6627  we'll need to convert it into:
6628 
6629  ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
6630  | |
6631  |<----- wrap_nest ---->|
6632 
6633  Q: other subqueries may be pointing to this element. What to do?
6634  A1: simple solution: copy *subq_pred->embedding_join_nest= *parent_nest.
6635  But we'll need to fix other pointers.
6636  A2: Another way: have TABLE_LIST::next_ptr so the following
6637  subqueries know the table has been nested.
6638  A3: changes in the TABLE_LIST::outer_join will make everything work
6639  automatically.
6640  */
6641  TABLE_LIST *const wrap_nest=
6642  TABLE_LIST::new_nested_join(thd->mem_root, "(sj-wrap)",
6643  outer_tbl->embedding, outer_tbl->join_list,
6644  parent_lex);
6645  if (wrap_nest == NULL)
6646  DBUG_RETURN(true);
6647 
6648  wrap_nest->nested_join->join_list.push_back(outer_tbl);
6649 
6650  outer_tbl->embedding= wrap_nest;
6651  outer_tbl->join_list= &wrap_nest->nested_join->join_list;
6652 
6653  /*
6654  wrap_nest will take place of outer_tbl, so move the outer join flag
6655  and join condition.
6656  */
6657  wrap_nest->outer_join= outer_tbl->outer_join;
6658  outer_tbl->outer_join= 0;
6659 
6660  wrap_nest->set_join_cond(outer_tbl->join_cond());
6661  outer_tbl->set_join_cond(NULL);
6662 
6663  List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
6664  TABLE_LIST *tbl;
6665  while ((tbl= li++))
6666  {
6667  if (tbl == outer_tbl)
6668  {
6669  li.replace(wrap_nest);
6670  break;
6671  }
6672  }
6673  /*
6674  Ok now wrap_nest 'contains' outer_tbl and we're ready to add the
6675  semi-join nest into it
6676  */
6677  emb_join_list= &wrap_nest->nested_join->join_list;
6678  emb_tbl_nest= wrap_nest;
6679  }
6680  }
6681 
6682  TABLE_LIST *const sj_nest=
6683  TABLE_LIST::new_nested_join(thd->mem_root, "(sj-nest)",
6684  emb_tbl_nest, emb_join_list, parent_lex);
6685  if (sj_nest == NULL)
6686  DBUG_RETURN(true);
6687 
6688  NESTED_JOIN *const nested_join= sj_nest->nested_join;
6689 
6690  /* Nests do not participate in those 'chains', so: */
6691  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
6692  emb_join_list->push_back(sj_nest);
6693 
6694  /*
6695  nested_join->used_tables and nested_join->not_null_tables are
6696  initialized in simplify_joins().
6697  */
6698 
6699  /*
6700  2. Walk through subquery's top list and set 'embedding' to point to the
6701  sj-nest.
6702  */
6703  st_select_lex *subq_lex= subq_pred->unit->first_select();
6704  nested_join->query_block_id= subq_lex->select_number;
6705  nested_join->join_list.empty();
6706  List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
6707  TABLE_LIST *tl;
6708  while ((tl= li++))
6709  {
6710  tl->embedding= sj_nest;
6711  tl->join_list= &nested_join->join_list;
6712  nested_join->join_list.push_back(tl);
6713  }
6714 
6715  /*
6716  Reconnect the next_leaf chain.
6717  TODO: Do we have to put subquery's tables at the end of the chain?
6718  Inserting them at the beginning would be a bit faster.
6719  NOTE: We actually insert them at the front! That's because the order is
6720  reversed in this list.
6721  */
6722  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf)
6723  {}
6724  tl->next_leaf= subq_lex->leaf_tables;
6725 
6726  /*
6727  Same as above for next_local chain. This needed only for re-execution.
6728  (The next_local chain always starts with SELECT_LEX::table_list)
6729  */
6730  for (tl= parent_lex->get_table_list(); tl->next_local; tl= tl->next_local)
6731  {}
6732  tl->next_local= subq_lex->get_table_list();
6733 
6734  /* A theory: no need to re-connect the next_global chain */
6735 
6736  /* 3. Remove the original subquery predicate from the WHERE/ON */
6737 
6738  // The subqueries were replaced for Item_int(1) earlier
6739  /*TODO: also reset the 'with_subselect' there. */
6740 
6741  /* n. Adjust the parent_join->tables counter */
6742  uint table_no= parent_join->tables;
6743  /* n. Walk through child's tables and adjust table->map */
6744  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
6745  {
6746  tl->table->tablenr= table_no;
6747  tl->table->map= ((table_map)1) << table_no;
6748  SELECT_LEX *old_sl= tl->select_lex;
6749  tl->select_lex= parent_join->select_lex;
6750  for (TABLE_LIST *emb= tl->embedding;
6751  emb && emb->select_lex == old_sl;
6752  emb= emb->embedding)
6753  emb->select_lex= parent_join->select_lex;
6754  }
6755  parent_join->tables+= subq_lex->join->tables;
6756  parent_join->primary_tables+= subq_lex->join->tables;
6757 
6758  parent_lex->derived_table_count+= subq_lex->derived_table_count;
6759  parent_lex->materialized_table_count+= subq_lex->materialized_table_count;
6760  parent_lex->partitioned_table_count+= subq_lex->partitioned_table_count;
6761 
6762  nested_join->sj_outer_exprs.empty();
6763  nested_join->sj_inner_exprs.empty();
6764 
6765  /*
6766  @todo: Add similar conversion for subqueries other than IN.
6767  */
6768  if (subq_pred->substype() == Item_subselect::IN_SUBS)
6769  {
6770  Item_in_subselect *in_subq_pred= (Item_in_subselect *)subq_pred;
6771 
6772  /* Left side of IN predicate is already resolved */
6773  DBUG_ASSERT(in_subq_pred->left_expr->fixed);
6774 
6775  in_subq_pred->exec_method= Item_exists_subselect::EXEC_SEMI_JOIN;
6776  /*
6777  sj_corr_tables is supposed to contain non-trivially correlated tables,
6778  but here it is set to contain all correlated tables.
6779  @todo: Add analysis step that assigns only the set of non-trivially
6780  correlated tables to sj_corr_tables.
6781  */
6782  nested_join->sj_corr_tables= subq_pred->used_tables();
6783  /*
6784  sj_depends_on contains the set of outer tables referred in the
6785  subquery's WHERE clause as well as tables referred in the IN predicate's
6786  left-hand side.
6787  */
6788  nested_join->sj_depends_on= subq_pred->used_tables() |
6789  in_subq_pred->left_expr->used_tables();
6790  /* Put the subquery's WHERE into semi-join's condition. */
6791  sj_nest->sj_on_expr= subq_lex->where;
6792 
6793  /*
6794  Create the IN-equalities and inject them into semi-join's ON condition.
6795  Additionally, for LooseScan strategy
6796  - Record the number of IN-equalities.
6797  - Create list of pointers to (oe1, ..., ieN). We'll need the list to
6798  see which of the expressions are bound and which are not (for those
6799  we'll produce a distinct stream of (ie_i1,...ie_ik).
6800 
6801  (TODO: can we just create a list of pointers and hope the expressions
6802  will not substitute themselves on fix_fields()? or we need to wrap
6803  them into Item_direct_view_refs and store pointers to those. The
6804  pointers to Item_direct_view_refs are guaranteed to be stable as
6805  Item_direct_view_refs doesn't substitute itself with anything in
6806  Item_direct_view_ref::fix_fields.
6807  */
6808 
6809  for (uint i= 0; i < in_subq_pred->left_expr->cols(); i++)
6810  {
6811  nested_join->sj_outer_exprs.push_back(in_subq_pred->left_expr->
6812  element_index(i));
6813  nested_join->sj_inner_exprs.push_back(subq_lex->ref_pointer_array[i]);
6814 
6815  Item_func_eq *item_eq=
6816  new Item_func_eq(in_subq_pred->left_expr->element_index(i),
6817  subq_lex->ref_pointer_array[i]);
6818  if (item_eq == NULL)
6819  DBUG_RETURN(TRUE);
6820 
6821  sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
6822  if (sj_nest->sj_on_expr == NULL)
6823  DBUG_RETURN(TRUE);
6824  }
6825  /* Fix the created equality and AND */
6826 
6827  Opt_trace_array sj_on_trace(&thd->opt_trace,
6828  "evaluating_constant_semijoin_conditions");
6829  sj_nest->sj_on_expr->top_level_item();
6830  if (sj_nest->sj_on_expr->fix_fields(thd, &sj_nest->sj_on_expr))
6831  DBUG_RETURN(true);
6832  }
6833 
6834  /* Unlink the child select_lex: */
6835  subq_lex->master_unit()->exclude_level();
6836  parent_lex->removed_select= subq_lex;
6837  /*
6838  Update the resolver context - needed for Item_field objects that have been
6839  replaced in the item tree for this execution, but are still needed for
6840  subsequent executions.
6841  */
6842  for (st_select_lex *select= parent_lex->removed_select;
6843  select != NULL;
6844  select= select->removed_select)
6845  select->context.select_lex= parent_lex;
6846  /*
6847  Walk through sj nest's WHERE and ON expressions and call
6848  item->fix_table_changes() for all items.
6849  */
6850  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, subq_lex);
6851  fix_list_after_tbl_changes(parent_lex, subq_lex,
6852  &sj_nest->nested_join->join_list);
6853 
6854  //TODO fix QT_
6855  DBUG_EXECUTE("where",
6856  print_where(sj_nest->sj_on_expr,"SJ-EXPR", QT_ORDINARY););
6857 
6858  if (emb_tbl_nest)
6859  {
6860  /* Inject sj_on_expr into the parent's ON condition */
6861  emb_tbl_nest->set_join_cond(and_items(emb_tbl_nest->join_cond(),
6862  sj_nest->sj_on_expr));
6863  if (emb_tbl_nest->join_cond() == NULL)
6864  DBUG_RETURN(true);
6865  emb_tbl_nest->join_cond()->top_level_item();
6866  if (!emb_tbl_nest->join_cond()->fixed &&
6867  emb_tbl_nest->join_cond()->fix_fields(parent_join->thd,
6868  emb_tbl_nest->join_cond_ref()))
6869  DBUG_RETURN(true);
6870  }
6871  else
6872  {
6873  /* Inject sj_on_expr into the parent's WHERE condition */
6874  parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
6875  if (parent_join->conds == NULL)
6876  DBUG_RETURN(true);
6877  parent_join->conds->top_level_item();
6878  if (parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds))
6879  DBUG_RETURN(true);
6880  parent_join->select_lex->where= parent_join->conds;
6881  }
6882 
6883  if (subq_lex->ftfunc_list->elements)
6884  {
6885  Item_func_match *ifm;
6886  List_iterator_fast<Item_func_match> li(*(subq_lex->ftfunc_list));
6887  while ((ifm= li++))
6888  parent_lex->ftfunc_list->push_front(ifm);
6889  }
6890 
6891  DBUG_RETURN(false);
6892 }
6893 
6894 
6895 /*
6896  Convert semi-join subquery predicates into semi-join join nests
6897 
6898  SYNOPSIS
6899  JOIN::flatten_subqueries()
6900 
6901  DESCRIPTION
6902 
6903  Convert candidate subquery predicates into semi-join join nests. This
6904  transformation is performed once in query lifetime and is irreversible.
6905 
6906  Conversion of one subquery predicate
6907  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6908  We start with a join that has a semi-join subquery:
6909 
6910  SELECT ...
6911  FROM ot, ...
6912  WHERE oe IN (SELECT ie FROM it1 ... itN WHERE subq_where) AND outer_where
6913 
6914  and convert it into a semi-join nest:
6915 
6916  SELECT ...
6917  FROM ot SEMI JOIN (it1 ... itN), ...
6918  WHERE outer_where AND subq_where AND oe=ie
6919 
6920  that is, in order to do the conversion, we need to
6921 
6922  * Create the "SEMI JOIN (it1 .. itN)" part and add it into the parent
6923  query's FROM structure.
6924  * Add "AND subq_where AND oe=ie" into parent query's WHERE (or ON if
6925  the subquery predicate was in an ON expression)
6926  * Remove the subquery predicate from the parent query's WHERE
6927 
6928  Considerations when converting many predicates
6929  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6930  A join may have at most MAX_TABLES tables. This may prevent us from
6931  flattening all subqueries when the total number of tables in parent and
6932  child selects exceeds MAX_TABLES. In addition, one slot is reserved per
6933  semi-join nest, in case the subquery needs to be materialized in a
6934  temporary table.
6935  We deal with this problem by flattening children's subqueries first and
6936  then using a heuristic rule to determine each subquery predicate's
6937  "priority".
6938 
6939  RETURN
6940  FALSE OK
6941  TRUE Error
6942 */
6943 
6944 bool JOIN::flatten_subqueries()
6945 {
6946  Item_exists_subselect **subq;
6947  Item_exists_subselect **subq_end;
6948  bool outer_join_objection= false;
6949  Opt_trace_context * const trace= &thd->opt_trace;
6950  DBUG_ENTER("JOIN::flatten_subqueries");
6951 
6952  if (sj_subselects.empty())
6953  DBUG_RETURN(FALSE);
6954 
6955  /* First, convert child join's subqueries. We proceed bottom-up here */
6956  for (subq= sj_subselects.begin(), subq_end= sj_subselects.end();
6957  subq < subq_end;
6958  subq++)
6959  {
6960  /*
6961  Currently, we only support transformation of IN subqueries.
6962  */
6963  DBUG_ASSERT((*subq)->substype() == Item_subselect::IN_SUBS);
6964 
6965  st_select_lex *child_select= (*subq)->unit->first_select();
6966  JOIN *child_join= child_select->join;
6967 
6968  /*
6969  child_select->where contains only the WHERE predicate of the
6970  subquery itself here. We may be selecting from a VIEW, which has its
6971  own predicate. The combined predicates are available in child_join->conds,
6972  which was built by setup_conds() doing prepare_where() for all views.
6973  */
6974  child_select->where= child_join->conds;
6975 
6976  if (child_join->flatten_subqueries())
6977  DBUG_RETURN(TRUE);
6978 
6979  (*subq)->sj_convert_priority=
6980  (((*subq)->unit->uncacheable & UNCACHEABLE_DEPENDENT) ? MAX_TABLES : 0) +
6981  child_join->tables;
6982  }
6983 
6984  //dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
6985  /*
6986  2. Pick which subqueries to convert:
6987  sort the subquery array
6988  - prefer correlated subqueries over uncorrelated;
6989  - prefer subqueries that have greater number of outer tables;
6990  */
6991  my_qsort(sj_subselects.begin(),
6992  sj_subselects.size(), sj_subselects.element_size(),
6993  reinterpret_cast<qsort_cmp>(subq_sj_candidate_cmp));
6994 
6995  Prepared_stmt_arena_holder ps_arena_holder(thd);
6996 
6997  // #tables-in-parent-query + #tables-in-subquery + sj nests <= MAX_TABLES
6998  /* Replace all subqueries to be flattened with Item_int(1) */
6999 
7000  uint table_count= tables;
7001  for (subq= sj_subselects.begin(); subq < subq_end; subq++)
7002  {
7003  // Add the tables in the subquery nest plus one in case of materialization:
7004  const uint tables_added= (*subq)->unit->first_select()->join->tables + 1;
7005  (*subq)->sj_chosen= table_count + tables_added <= MAX_TABLES;
7006 
7007  if (!(*subq)->sj_chosen)
7008  continue;
7009 
7010  table_count+= tables_added;
7011 
7012  Item **tree= ((*subq)->embedding_join_nest == NULL) ?
7013  &conds : ((*subq)->embedding_join_nest->join_cond_ref());
7014  if (replace_subcondition(this, tree, *subq, new Item_int(1), FALSE))
7015  DBUG_RETURN(TRUE); /* purecov: inspected */
7016  }
7017 
7018  for (subq= sj_subselects.begin(); subq < subq_end; subq++)
7019  {
7020  if (!(*subq)->sj_chosen)
7021  continue;
7022 
7023  OPT_TRACE_TRANSFORM(trace, oto0, oto1,
7024  (*subq)->unit->first_select()->select_number,
7025  "IN (SELECT)", "semijoin");
7026  oto1.add("chosen", true);
7027  if (convert_subquery_to_semijoin(this, *subq))
7028  DBUG_RETURN(TRUE);
7029  }
7030  /*
7031  3. Finalize the subqueries that we did not convert,
7032  ie. perform IN->EXISTS rewrite.
7033  */
7034  for (subq= sj_subselects.begin(); subq < subq_end; subq++)
7035  {
7036  if ((*subq)->sj_chosen)
7037  continue;
7038  {
7039  OPT_TRACE_TRANSFORM(trace, oto0, oto1,
7040  (*subq)->unit->first_select()->select_number,
7041  "IN (SELECT)", "semijoin");
7042  if (outer_join_objection)
7043  oto1.add_alnum("cause", "outer_join");
7044  oto1.add("chosen", false);
7045  }
7046  JOIN *child_join= (*subq)->unit->first_select()->join;
7047  Item_subselect::trans_res res;
7048  (*subq)->changed= 0;
7049  (*subq)->fixed= 0;
7050 
7051  SELECT_LEX *save_select_lex= thd->lex->current_select;
7052  thd->lex->current_select= (*subq)->unit->first_select();
7053 
7054  res= (*subq)->select_transformer(child_join);
7055 
7056  thd->lex->current_select= save_select_lex;
7057 
7058  if (res == Item_subselect::RES_ERROR)
7059  DBUG_RETURN(TRUE);
7060 
7061  (*subq)->changed= 1;
7062  (*subq)->fixed= 1;
7063 
7064  Item *substitute= (*subq)->substitution;
7065  const bool do_fix_fields= !(*subq)->substitution->fixed;
7066  const bool subquery_in_join_clause= (*subq)->embedding_join_nest != NULL;
7067 
7068  Item **tree= subquery_in_join_clause ?
7069  ((*subq)->embedding_join_nest->join_cond_ref()) : &conds;
7070  if (replace_subcondition(this, tree, *subq, substitute, do_fix_fields))
7071  DBUG_RETURN(TRUE);
7072  (*subq)->substitution= NULL;
7073 
7074  if (!thd->stmt_arena->is_conventional())
7075  {
7076  if (subquery_in_join_clause)
7077  {
7078  tree= &((*subq)->embedding_join_nest->prep_join_cond);
7079  /*
7080  Some precaution is needed when dealing with PS/SP:
7081  fix_prepare_info_in_table_list() sets prep_join_cond, but only for
7082  tables, not for join nest objects. This is instead populated in
7083  record_join_nest_info(), which is called after this function.
7084  The case where *tree is NULL is handled by this procedure.
7085  */
7086  }
7087  else
7088  tree= &select_lex->prep_where;
7089 
7090  if (*tree && replace_subcondition(this, tree, *subq, substitute, false))
7091  DBUG_RETURN(true);
7092  }
7093  }
7094 
7095  sj_subselects.clear();
7096  DBUG_RETURN(FALSE);
7097 }
7098 
7099 
7100 /*
7101  Remove the predicates pushed down into the subquery
7102 
7103  SYNOPSIS
7104  JOIN::remove_subq_pushed_predicates()
7105  where IN Must be NULL
7106  OUT The remaining WHERE condition, or NULL
7107 
7108  DESCRIPTION
7109  Given that this join will be executed using (unique|index)_subquery,
7110  without "checking NULL", remove the predicates that were pushed down
7111  into the subquery.
7112 
7113  If the subquery compares scalar values, we can remove the condition that
7114  was wrapped into trig_cond (it will be checked when needed by the subquery
7115  engine)
7116 
7117  If the subquery compares row values, we need to keep the wrapped
7118  equalities in the WHERE clause: when the left (outer) tuple has both NULL
7119  and non-NULL values, we'll do a full table scan and will rely on the
7120  equalities corresponding to non-NULL parts of left tuple to filter out
7121  non-matching records.
7122 
7123  If '*where' is a triggered condition, or contains 'OR x IS NULL', or
7124  contains a condition coming from the original subquery's WHERE clause, or
7125  if there are more than one outer expressions, then WHERE is not of the
7126  simple form:
7127  outer_expr = inner_expr
7128  and thus this function does nothing.
7129 
7130  If the index is on prefix (=> test_if_ref() is false), then the equality
7131  is needed as post-filter, so this function does nothing.
7132 
7133  TODO: We can remove the equalities that will be guaranteed to be true by the
7134  fact that subquery engine will be using index lookup. This must be done only
7135  for cases where there are no conversion errors of significance, e.g. 257
7136  that is searched in a byte. But this requires homogenization of the return
7137  codes of all Field*::store() methods.
7138 */
7139 void JOIN::remove_subq_pushed_predicates(Item **where)
7140 {
7141  if (conds->type() == Item::FUNC_ITEM &&
7142  ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
7143  ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
7144  ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
7145  test_if_ref (this->conds,
7146  (Item_field *)((Item_func *)conds)->arguments()[1],
7147  ((Item_func *)conds)->arguments()[0]))
7148  {
7149  *where= 0;
7150  return;
7151  }
7152 }
7153 
7154 
7171 {
7172  DBUG_ASSERT(select_lex->materialized_table_count);
7173 
7174  for (TABLE_LIST *table= select_lex->leaf_tables;
7175  table;
7176  table= table->next_leaf)
7177  {
7178  table->derived_keys_ready= TRUE;
7179  /* Process tables that aren't materialized yet. */
7180  if (table->uses_materialization() && !table->table->is_created() &&
7181  table->generate_keys())
7182  return TRUE;
7183  }
7184  return FALSE;
7185 }
7186 
7187 
7199 {
7200  DBUG_ASSERT(select_lex->materialized_table_count);
7201 
7202  for (uint i= 0 ; i < tables ; i++)
7203  {
7204  JOIN_TAB *tab= join_tab + i;
7205  TABLE *table= tab->table;
7206  /*
7207  Save chosen key description if:
7208  1) it's a materialized derived table
7209  2) it's not yet instantiated
7210  3) some keys are defined for it
7211  */
7212  if (table &&
7213  table->pos_in_table_list->uses_materialization() && // (1)
7214  !table->is_created() && // (2)
7215  table->max_keys > 0) // (3)
7216  {
7217  Key_use *keyuse= tab->position->key;
7218 
7219  table->use_index(keyuse ? keyuse->key : -1);
7220 
7221  const bool key_is_const= keyuse && tab->const_keys.is_set(keyuse->key);
7222  tab->const_keys.clear_all();
7223  tab->keys.clear_all();
7224 
7225  if (!keyuse)
7226  continue;
7227 
7228  /*
7229  Update the selected "keyuse" to point to key number 0.
7230  Notice that unused keyuse entries still point to the deleted
7231  candidate keys. tab->keys (and tab->const_keys if the chosen key
7232  is constant) should reference key object no. 0 as well.
7233  */
7234  tab->keys.set_bit(0);
7235  if (key_is_const)
7236  tab->const_keys.set_bit(0);
7237 
7238  const uint oldkey= keyuse->key;
7239  for (; keyuse->table == table && keyuse->key == oldkey; keyuse++)
7240  keyuse->key= 0;
7241  }
7242  }
7243 }
7244 
7245 
7257 {
7258  /* No need in cache if all tables are constant. */
7259  DBUG_ASSERT(!plan_is_const());
7260 
7261  for (uint i= const_tables; i < tables; i++)
7262  {
7263  Item *condition= join_tab[i].condition();
7264  if (condition == NULL)
7265  continue;
7266  Item *cache_item= NULL;
7267  Item **analyzer_arg= &cache_item;
7268  condition=
7269  condition->compile(&Item::cache_const_expr_analyzer,
7270  (uchar **)&analyzer_arg,
7272  (uchar *)&cache_item);
7273  if (condition == NULL)
7274  return true;
7275  if (condition != join_tab[i].condition())
7276  join_tab[i].set_condition(condition, __LINE__);
7277  }
7278  if (having)
7279  {
7280  Item *cache_item= NULL;
7281  Item **analyzer_arg= &cache_item;
7282  having=
7283  having->compile(&Item::cache_const_expr_analyzer, (uchar **)&analyzer_arg,
7284  &Item::cache_const_expr_transformer,(uchar *)&cache_item);
7285  if (having == NULL)
7286  return true;
7287  }
7288  return false;
7289 }
7290 
7291 
7292 void JOIN::replace_item_field(const char* field_name, Item* new_item)
7293 {
7294  if (conds)
7295  {
7296  conds= conds->compile(&Item::item_field_by_name_analyzer,
7297  (uchar **)&field_name,
7299  (uchar *)new_item);
7300  conds->update_used_tables();
7301  }
7302 
7304  Item *item;
7305  while ((item= it++))
7306  {
7307  item= item->compile(&Item::item_field_by_name_analyzer,
7308  (uchar **)&field_name,
7310  (uchar *)new_item);
7311  it.replace(item);
7312  item->update_used_tables();
7313  }
7314 }
7315 
7316 
7366 Item *
7367 make_cond_for_table(Item *cond, table_map tables, table_map used_table,
7368  bool exclude_expensive_cond)
7369 {
7370  return make_cond_for_table_from_pred(cond, cond, tables, used_table,
7371  exclude_expensive_cond);
7372 }
7373 
7374 static Item *
7375 make_cond_for_table_from_pred(Item *root_cond, Item *cond,
7376  table_map tables, table_map used_table,
7377  bool exclude_expensive_cond)
7378 {
7379  /*
7380  Ignore this condition if
7381  1. We are extracting conditions for a specific table, and
7382  2. that table is not referenced by the condition, but not if
7383  3. this is a constant condition not checked at optimization time and
7384  this is the first table we are extracting conditions for.
7385  (Assuming that used_table == tables for the first table.)
7386  */
7387  if (used_table && // 1
7388  !(cond->used_tables() & used_table) && // 2
7389  !(cond->is_expensive() && used_table == tables)) // 3
7390  return NULL;
7391 
7392  if (cond->type() == Item::COND_ITEM)
7393  {
7394  if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7395  {
7396  /* Create new top level AND item */
7397  Item_cond_and *new_cond= new Item_cond_and;
7398  if (!new_cond)
7399  return NULL;
7400  List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7401  Item *item;
7402  while ((item= li++))
7403  {
7404  Item *fix= make_cond_for_table_from_pred(root_cond, item,
7405  tables, used_table,
7406  exclude_expensive_cond);
7407  if (fix)
7408  new_cond->argument_list()->push_back(fix);
7409  }
7410  switch (new_cond->argument_list()->elements) {
7411  case 0:
7412  return NULL; // Always true
7413  case 1:
7414  return new_cond->argument_list()->head();
7415  default:
7416  if (new_cond->fix_fields(current_thd, NULL))
7417  return NULL;
7418  return new_cond;
7419  }
7420  }
7421  else
7422  { // Or list
7423  Item_cond_or *new_cond= new Item_cond_or;
7424  if (!new_cond)
7425  return NULL;
7426  List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7427  Item *item;
7428  while ((item= li++))
7429  {
7430  Item *fix= make_cond_for_table_from_pred(root_cond, item,
7431  tables, 0L,
7432  exclude_expensive_cond);
7433  if (!fix)
7434  return NULL; // Always true
7435  new_cond->argument_list()->push_back(fix);
7436  }
7437  if (new_cond->fix_fields(current_thd, NULL))
7438  return NULL;
7439  return new_cond;
7440  }
7441  }
7442 
7443  /*
7444  Omit this condition if
7445  1. It has been marked as omittable before, or
7446  2. Some tables referred by the condition are not available, or
7447  3. We are extracting conditions for all tables, the condition is
7448  considered 'expensive', and we want to delay evaluation of such
7449  conditions to the execution phase.
7450  */
7451  if (cond->marker == 3 || // 1
7452  (cond->used_tables() & ~tables) || // 2
7453  (!used_table && exclude_expensive_cond && cond->is_expensive())) // 3
7454  return NULL;
7455 
7456  /*
7457  Extract this condition if
7458  1. It has already been marked as applicable, or
7459  2. It is not a <comparison predicate> (=, <, >, <=, >=, <=>)
7460  */
7461  if (cond->marker == 2 || // 1
7462  cond->eq_cmp_result() == Item::COND_OK) // 2
7463  return cond;
7464 
7465  /*
7466  Remove equalities that are guaranteed to be true by use of 'ref' access
7467  method.
7468  Note that ref access implements "table1.field1 <=> table2.indexed_field2",
7469  i.e. if it passed a NULL field1, it will return NULL indexed_field2 if
7470  there are.
7471  Thus the equality "table1.field1 = table2.indexed_field2",
7472  is equivalent to "ref access AND table1.field1 IS NOT NULL"
7473  i.e. "ref access and proper setting/testing of ref->null_rejecting".
7474  Thus, we must be careful, that when we remove equalities below we also
7475  set ref->null_rejecting, and test it at execution; otherwise wrong NULL
7476  matches appear.
7477  So:
7478  - for the optimization phase, the code which is below, and the code in
7479  test_if_ref(), and in add_key_field(), must be kept in sync: if the
7480  applicability conditions in one place are relaxed, they should also be
7481  relaxed elsewhere.
7482  - for the execution phase, all possible execution methods must test
7483  ref->null_rejecting.
7484  */
7485  if (cond->type() == Item::FUNC_ITEM &&
7486  ((Item_func*) cond)->functype() == Item_func::EQ_FUNC)
7487  {
7488  Item *left_item= ((Item_func*) cond)->arguments()[0]->real_item();
7489  Item *right_item= ((Item_func*) cond)->arguments()[1]->real_item();
7490  if ((left_item->type() == Item::FIELD_ITEM &&
7491  test_if_ref(root_cond, (Item_field*) left_item, right_item)) ||
7492  (right_item->type() == Item::FIELD_ITEM &&
7493  test_if_ref(root_cond, (Item_field*) right_item, left_item)))
7494  {
7495  cond->marker= 3; // Condition can be omitted
7496  return NULL;
7497  }
7498  }
7499  cond->marker= 2; // Mark condition as applicable
7500  return cond;
7501 }
7502 
7503 
7521 static bool make_join_select(JOIN *join, Item *cond)
7522 {
7523  THD *thd= join->thd;
7524  Opt_trace_context * const trace= &thd->opt_trace;
7525  DBUG_ENTER("make_join_select");
7526  {
7527  add_not_null_conds(join);
7528  /*
7529  Step #1: Extract constant condition
7530  - Extract and check the constant part of the WHERE
7531  - Extract constant parts of ON expressions from outer
7532  joins and attach them appropriately.
7533  */
7534  if (cond) /* Because of QUICK_GROUP_MIN_MAX_SELECT */
7535  { /* there may be a select without a cond. */
7536  if (join->primary_tables > 1)
7537  cond->update_used_tables(); // Tablenr may have changed
7538  if (join->plan_is_const() &&
7539  thd->lex->current_select->master_unit() ==
7540  &thd->lex->unit) // not upper level SELECT
7541  join->const_table_map|=RAND_TABLE_BIT;
7542 
7543  /*
7544  Extract expressions that depend on constant tables
7545  1. Const part of the join's WHERE clause can be checked immediately
7546  and if it is not satisfied then the join has empty result
7547  2. Constant parts of outer joins' ON expressions must be attached
7548  there inside the triggers.
7549  */
7550  {
7551  Item *const_cond=
7552  make_cond_for_table(cond,
7553  join->const_table_map,
7554  (table_map) 0, 1);
7555  /* Add conditions added by add_not_null_conds(). */
7556  for (uint i= 0 ; i < join->const_tables ; i++)
7557  {
7558  if (and_conditions(&const_cond, join->join_tab[i].condition()))
7559  DBUG_RETURN(true);
7560  }
7561 
7562  DBUG_EXECUTE("where",print_where(const_cond,"constants", QT_ORDINARY););
7563  for (JOIN_TAB *tab= join->join_tab+join->const_tables;
7564  tab < join->join_tab+join->tables ; tab++)
7565  {
7566  if (tab->on_expr_ref && *tab->on_expr_ref)
7567  {
7568  JOIN_TAB *cond_tab= tab->first_inner;
7569  Item *tmp= make_cond_for_table(*tab->on_expr_ref,
7570  join->const_table_map,
7571  ( table_map) 0, 0);
7572  if (!tmp)
7573  continue;
7574  tmp= new
7575  Item_func_trig_cond(tmp, &cond_tab->not_null_compl, cond_tab,
7577  if (!tmp)
7578  DBUG_RETURN(true);
7579 
7580  tmp->quick_fix_field();
7581  if (cond_tab->and_with_condition(tmp, __LINE__))
7582  DBUG_RETURN(true);
7583  }
7584  }
7585  if (const_cond != NULL)
7586  {
7587  const bool const_cond_is_true= const_cond->val_int() != 0;
7588  Opt_trace_object trace_const_cond(trace);
7589  trace_const_cond.add("condition_on_constant_tables", const_cond)
7590  .add("condition_value", const_cond_is_true);
7591  if (!const_cond_is_true)
7592  {
7593  DBUG_PRINT("info",("Found impossible WHERE condition"));
7594  DBUG_RETURN(1); // Impossible const condition
7595  }
7596  }
7597  }
7598  }
7599 
7600  /*
7601  Step #2: Extract WHERE/ON parts
7602  */
7603  Opt_trace_object trace_wrapper(trace);
7605  trace_conditions(trace, "attaching_conditions_to_tables");
7606  trace_conditions.add("original_condition", cond);
7608  trace_attached_comp(trace, "attached_conditions_computation");
7609 
7610  for (uint i=join->const_tables ; i < join->tables ; i++)
7611  {
7612  JOIN_TAB *const tab= join->join_tab + i;
7613 
7614  if (!tab->position)
7615  continue;
7616  /*
7617  first_inner is the X in queries like:
7618  SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
7619  */
7620  JOIN_TAB *const first_inner_tab= tab->first_inner;
7621  const table_map used_tables= tab->prefix_tables();
7622  const table_map current_map= tab->added_tables();
7623  bool use_quick_range=0;
7624  Item *tmp;
7625 
7626  /*
7627  Heuristic: Switch from 'ref' to 'range' access if 'range'
7628  access can utilize more keyparts than 'ref' access. Conditions
7629  for doing switching:
7630 
7631  1) Current decision is to use 'ref' access
7632  2) 'ref' access depends on a constant, not a value read from a
7633  table earlier in the join sequence.
7634 
7635  Rationale: if 'ref' depends on a value from another table,
7636  the join condition is not used to limit the rows read by
7637  'range' access (that would require dynamic range - 'Range
7638  checked for each record'). In other words, if 'ref' depends
7639  on a value from another table, we have a query with
7640  conditions of the form
7641 
7642  this_table.idx_col1 = other_table.col AND <<- used by 'ref'
7643  this_table.idx_col1 OP <const> AND <<- used by 'range'
7644  this_table.idx_col2 OP <const> AND ... <<- used by 'range'
7645 
7646  and an index on (idx_col1,idx_col2,...). But the fact that
7647  'range' access uses more keyparts does not mean that it is
7648  more selective than 'ref' access because these access types
7649  utilize different parts of the query condition. We
7650  therefore trust the cost based choice made by
7651  best_access_path() instead of forcing a heuristic choice
7652  here.
7653  3) Range access is possible, and it is less costly than
7654  table/index scan
7655  4) 'ref' access and 'range' access uses the same index
7656  5) 'range' access uses more keyparts than 'ref' access
7657 
7658  @todo: This decision should rather be made in best_access_path()
7659  */
7660  if (tab->type == JT_REF && // 1)
7661  !tab->ref.depend_map && // 2)
7662  tab->quick && // 3)
7663  (uint) tab->ref.key == tab->quick->index && // 4)
7664  tab->ref.key_length < tab->quick->max_used_key_length) // 5)
7665  {
7666  Opt_trace_object wrapper(trace);
7667  Opt_trace_object (trace, "access_type_changed").
7668  add_utf8_table(tab->table).
7669  add_utf8("index", tab->table->key_info[tab->ref.key].name).
7670  add_alnum("old_type", "ref").
7671  add_alnum("new_type", "range").
7672  add_alnum("cause", "uses_more_keyparts");
7673 
7674  tab->type=JT_ALL;
7675  use_quick_range=1;
7676  tab->use_quick=QS_RANGE;
7677  tab->ref.key= -1;
7678  tab->ref.key_parts=0; // Don't use ref key.
7679  tab->position->records_read= rows2double(tab->quick->records);
7680  /*
7681  We will use join cache here : prevent sorting of the first
7682  table only and sort at the end.
7683  */
7684  if (i != join->const_tables &&
7685  join->primary_tables > join->const_tables + 1)
7686  join->full_join= true;
7687  }
7688 
7689  tmp= NULL;
7690  if (cond)
7691  tmp= make_cond_for_table(cond,used_tables,current_map, 0);
7692  /* Add conditions added by add_not_null_conds(). */
7693  if (tab->condition() && and_conditions(&tmp, tab->condition()))
7694  DBUG_RETURN(true);
7695 
7696 
7697  if (cond && !tmp && tab->quick)
7698  { // Outer join
7699  if (tab->type != JT_ALL)
7700  {
7701  /*
7702  Don't use the quick method
7703  We come here in the case where we have 'key=constant' and
7704  the test is removed by make_cond_for_table()
7705  */
7706  delete tab->quick;
7707  tab->quick= 0;
7708  }
7709  else
7710  {
7711  /*
7712  Hack to handle the case where we only refer to a table
7713  in the ON part of an OUTER JOIN. In this case we want the code
7714  below to check if we should use 'quick' instead.
7715  */
7716  DBUG_PRINT("info", ("Item_int"));
7717  tmp= new Item_int((longlong) 1,1); // Always true
7718  }
7719 
7720  }
7721  if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
7722  tab->type == JT_EQ_REF || first_inner_tab)
7723  {
7724  DBUG_EXECUTE("where",print_where(tmp,tab->table->alias, QT_ORDINARY););
7725  SQL_SELECT *sel= tab->select= new (thd->mem_root) SQL_SELECT;
7726  if (!sel)
7727  DBUG_RETURN(1); // End of memory
7728  sel->read_tables= sel->const_tables= join->const_table_map;
7729  /*
7730  If tab is an inner table of an outer join operation,
7731  add a match guard to the pushed down predicate.
7732  The guard will turn the predicate on only after
7733  the first match for outer tables is encountered.
7734  */
7735  if (cond && tmp)
7736  {
7737  /*
7738  Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
7739  a cond, so neutralize the hack above.
7740  */
7741  if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
7742  DBUG_RETURN(true);
7743  sel->cond= tmp;
7744  tab->set_condition(tmp, __LINE__);
7745  /* Push condition to storage engine if this is enabled
7746  and the condition is not guarded */
7747  if (thd->optimizer_switch_flag(OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN) &&
7748  !first_inner_tab)
7749  {
7750  Item *push_cond=
7751  make_cond_for_table(tmp, tab->table->map, tab->table->map, 0);
7752  if (push_cond)
7753  {
7754  /* Push condition to handler */
7755  if (!tab->table->file->cond_push(push_cond))
7756  tab->table->file->pushed_cond= push_cond;
7757  }
7758  }
7759  }
7760  else
7761  {
7762  sel->cond= NULL;
7763  tab->set_condition(NULL, __LINE__);
7764  }
7765 
7766  sel->head=tab->table;
7767  DBUG_EXECUTE("where",print_where(tmp,tab->table->alias, QT_ORDINARY););
7768  if (tab->quick)
7769  {
7770  /* Use quick key read if it's a constant and it's not used
7771  with key reading */
7772  if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF &&
7773  tab->type != JT_FT &&
7774  ((tab->type != JT_CONST && tab->type != JT_REF) ||
7775  (uint)tab->ref.key == tab->quick->index))
7776  {
7777  DBUG_ASSERT(tab->quick->is_valid());
7778  sel->quick=tab->quick; // Use value from get_quick_...
7779  sel->quick_keys.clear_all();
7780  sel->needed_reg.clear_all();
7781  }
7782  else
7783  {
7784  delete tab->quick;
7785  }
7786  tab->quick=0;
7787  }
7788  uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
7789  if (i == join->const_tables && ref_key)
7790  {
7791  if (!tab->const_keys.is_clear_all() &&
7792  tab->table->reginfo.impossible_range)
7793  DBUG_RETURN(1);
7794  }
7795  else if (tab->type == JT_ALL && ! use_quick_range)
7796  {
7797  if (!tab->const_keys.is_clear_all() &&
7798  tab->table->reginfo.impossible_range)
7799  DBUG_RETURN(1); // Impossible range
7800  /*
7801  We plan to scan (table/index/range scan).
7802  Check again if we should use an index. We can use an index if:
7803 
7804  1a) There is a condition that range optimizer can work on, and
7805  1b) There are non-constant conditions on one or more keys, and
7806  1c) Some of the non-constant fields may have been read
7807  already. This may be the case if this is not the first
7808  table in the join OR this is a subselect with
7809  non-constant conditions referring to an outer table
7810  (dependent subquery)
7811  or,
7812  2a) There are conditions only relying on constants
7813  2b) This is the first non-constant table
7814  2c) There is a limit of rows to read that is lower than
7815  the fanout for this table (i.e., the estimated number
7816  of rows that will be produced for this table per row
7817  combination of previous tables)
7818  2d) The query is NOT run with FOUND_ROWS() (because in that
7819  case we have to scan through all rows to count them anyway)
7820  */
7821  enum { DONT_RECHECK, NOT_FIRST_TABLE, LOW_LIMIT }
7822  recheck_reason= DONT_RECHECK;
7823 
7824  if (cond && // 1a
7825  (tab->keys != tab->const_keys) && // 1b
7826  (i > 0 || // 1c
7827  (join->select_lex->master_unit()->item &&
7828  cond->used_tables() & OUTER_REF_TABLE_BIT)))
7829  recheck_reason= NOT_FIRST_TABLE;
7830  else if (!tab->const_keys.is_clear_all() && // 2a
7831  i == join->const_tables && // 2b
7832  (join->unit->select_limit_cnt <
7833  tab->position->records_read) && // 2c
7834  !(join->select_options & OPTION_FOUND_ROWS)) // 2d
7835  recheck_reason= LOW_LIMIT;
7836 
7837  if (recheck_reason != DONT_RECHECK)
7838  {
7839  Opt_trace_object trace_one_table(trace);
7840  trace_one_table.add_utf8_table(tab->table);
7841  Opt_trace_object trace_table(trace, "rechecking_index_usage");
7842  if (recheck_reason == NOT_FIRST_TABLE)
7843  trace_table.add_alnum("recheck_reason", "not_first_table");
7844  else
7845  trace_table.add_alnum("recheck_reason", "low_limit").
7846  add("limit", join->unit->select_limit_cnt).
7847  add("row_estimate", tab->position->records_read);
7848 
7849  /* Join with outer join condition */
7850  Item *orig_cond=sel->cond;
7851  sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
7852 
7853  /*
7854  We can't call sel->cond->fix_fields,
7855  as it will break tab->join_cond() if it's AND condition
7856  (fix_fields currently removes extra AND/OR levels).
7857  Yet attributes of the just built condition are not needed.
7858  Thus we call sel->cond->quick_fix_field for safety.
7859  */
7860  if (sel->cond && !sel->cond->fixed)
7861  sel->cond->quick_fix_field();
7862 
7863  key_map usable_keys= tab->keys;
7864  ORDER::enum_order interesting_order= ORDER::ORDER_NOT_RELEVANT;
7865 
7866  if (recheck_reason == LOW_LIMIT)
7867  {
7868  /*
7869  When optimizing for ORDER BY ... LIMIT, only indexes
7870  that give correct ordering are of interest. The block
7871  below removes all other indexes from usable_keys so
7872  the range optimizer (see test_quick_select() below)
7873  does not consider them.
7874  */
7875  for (uint idx= 0; idx < tab->table->s->keys; idx++)
7876  {
7877  /*
7878  No need to check if indexes that we're not allowed
7879  to use can provide required ordering.
7880  */
7881  if (!usable_keys.is_set(idx))
7882  continue;
7883 
7884  const int read_direction=
7885  test_if_order_by_key(join->order, tab->table, idx);
7886  if (read_direction == 0)
7887  {
7888  // The index cannot provide required ordering
7889  usable_keys.clear_bit(idx);
7890  continue;
7891  }
7892 
7893  /*
7894  Currently, only ASC ordered indexes are availabe,
7895  which means that if ordering can be achieved by
7896  reading the index in forward direction, then we have
7897  ORDER BY... ASC. Likewise, if ordering can be
7898  achieved by reading the index in backward direction,
7899  then we have ORDER BY ... DESC.
7900 
7901  Furthermore, if correct order can be achieved by
7902  reading one index in either forward or backward
7903  direction, then all other applicable indexes will
7904  need to be read in the same direction (so no reason
7905  to check that read_direction is the same for all
7906  applicable indexes).
7907 
7908  If DESC/mixed ordered indexes will be possible in
7909  the future, the implied connection between index
7910  read direction and ASC/DESC ordering will no longer
7911  hold.
7912  */
7913  interesting_order= (read_direction == -1 ? ORDER::ORDER_DESC :
7914  ORDER::ORDER_ASC);
7915  }
7916 
7917  if (usable_keys.is_clear_all())
7918  recheck_reason= DONT_RECHECK; // No usable keys
7919 
7920  /*
7921  If the current plan is to use a range access on an
7922  index that provides the order dictated by the ORDER BY
7923  clause there is no need to recheck index usage; we
7924  already know from the former call to
7925  test_quick_select() that a range scan on the chosen
7926  index is cheapest. Note that previous calls to
7927  test_quick_select() did not take order direction
7928  (ASC/DESC) into account, so in case of DESC ordering
7929  we still need to recheck.
7930  */
7931  if (sel->quick && (sel->quick->index != MAX_KEY) &&
7932  usable_keys.is_set(sel->quick->index) &&
7933  (interesting_order != ORDER::ORDER_DESC ||
7934  sel->quick->reverse_sorted()))
7935  {
7936  recheck_reason= DONT_RECHECK;
7937  }
7938  }
7939 
7940  if ((recheck_reason != DONT_RECHECK) &&
7941  sel->test_quick_select(thd, usable_keys,
7942  used_tables & ~tab->table->map,
7943  (join->select_options &
7944  OPTION_FOUND_ROWS ?
7945  HA_POS_ERROR :
7946  join->unit->select_limit_cnt),
7947  false, // don't force quick range
7948  interesting_order) < 0)
7949  {
7950  /*
7951  Before reporting "Impossible WHERE" for the whole query
7952  we have to check isn't it only "impossible ON" instead
7953  */
7954  sel->cond=orig_cond;
7955  if (!*tab->on_expr_ref)
7956  DBUG_RETURN(1); // Impossible WHERE
7957  Opt_trace_object trace_without_on(trace, "without_ON_clause");
7958  if (sel->test_quick_select(thd, tab->keys,
7959  used_tables & ~tab->table->map,
7960  (join->select_options &
7961  OPTION_FOUND_ROWS ?
7962  HA_POS_ERROR :
7963  join->unit->select_limit_cnt),
7964  false, //don't force quick range
7965  ORDER::ORDER_NOT_RELEVANT) < 0)
7966  DBUG_RETURN(1); // Impossible WHERE
7967  }
7968  else
7969  sel->cond=orig_cond;
7970 
7971  /* Fix for EXPLAIN */
7972  if (sel->quick)
7973  tab->position->records_read= (double)sel->quick->records;
7974  }
7975  else
7976  {
7977  sel->needed_reg=tab->needed_reg;
7978  sel->quick_keys.clear_all();
7979  }
7980  if (!sel->quick_keys.is_subset(tab->checked_keys) ||
7981  !sel->needed_reg.is_subset(tab->checked_keys))
7982  {
7983  tab->keys=sel->quick_keys;
7984  tab->keys.merge(sel->needed_reg);
7985  tab->use_quick= (!sel->needed_reg.is_clear_all() &&
7986  (sel->quick_keys.is_clear_all() ||
7987  (sel->quick &&
7988  (sel->quick->records >= 100L)))) ?
7989  QS_DYNAMIC_RANGE : QS_RANGE;
7990  sel->read_tables= used_tables & ~current_map;
7991  }
7992  if (i != join->const_tables && tab->use_quick != QS_DYNAMIC_RANGE &&
7993  !tab->first_inner)
7994  { /* Read with cache */
7995  if (cond &&
7996  (tmp=make_cond_for_table(cond,
7997  join->const_table_map |
7998  current_map,
7999  current_map, 0)))
8000  {
8001  DBUG_EXECUTE("where",print_where(tmp,"cache", QT_ORDINARY););
8002  tab->cache_select=(SQL_SELECT*)
8003  thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
8004  tab->cache_select->cond=tmp;
8005  tab->cache_select->read_tables=join->const_table_map;
8006  }
8007  }
8008  }
8009  }
8010 
8011  if (pushdown_on_conditions(join, tab))
8012  DBUG_RETURN(1);
8013  }
8014  trace_attached_comp.end();
8015 
8016  /*
8017  In outer joins the loop above, in iteration for table #i, may push
8018  conditions to a table before #i. Thus, the processing below has to be in
8019  a separate loop:
8020  */
8021  Opt_trace_array trace_attached_summary(trace,
8022  "attached_conditions_summary");
8023  for (uint i= join->const_tables ; i < join->tables ; i++)
8024  {
8025  JOIN_TAB * const tab= &join->join_tab[i];
8026  if (!tab->table)
8027  continue;
8028  Item * const cond= tab->condition();
8029  Opt_trace_object trace_one_table(trace);
8030  trace_one_table.add_utf8_table(tab->table).
8031  add("attached", cond);
8032  if (cond &&
8033  cond->has_subquery() /* traverse only if needed */ )
8034  {
8035  /*
8036  Why we pass walk_subquery=false: imagine
8037  WHERE t1.col IN (SELECT * FROM t2
8038  WHERE t2.col IN (SELECT * FROM t3)
8039  and tab==t1. The grandchild subquery (SELECT * FROM t3) should not
8040  be marked as "in condition of t1" but as "in condition of t2", for
8041  correct calculation of the number of its executions.
8042  */
8043  int idx= tab - join->join_tab;
8044  cond->walk(&Item::inform_item_in_cond_of_tab, false,
8045  reinterpret_cast<uchar * const>(&idx));
8046  }
8047 
8048  }
8049  }
8050  DBUG_RETURN(0);
8051 }
8052 
8053 
8083 static bool
8084 eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab,
8085  table_map *cached_eq_ref_tables, table_map *eq_ref_tables)
8086 {
8087  /* We can skip const tables only if not an outer table */
8088  if (tab->type == JT_CONST && !tab->first_inner)
8089  return true;
8090  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
8091  return false;
8092 
8093  const table_map map= tab->table->map;
8094  uint found= 0;
8095 
8096  for (Item **ref_item= tab->ref.items, **end= ref_item + tab->ref.key_parts ;
8097  ref_item != end ; ref_item++)
8098  {
8099  if (! (*ref_item)->const_item())
8100  { // Not a const ref
8101  ORDER *order;
8102  for (order=start_order ; order ; order=order->next)
8103  {
8104  if ((*ref_item)->eq(order->item[0],0))
8105  break;
8106  }
8107  if (order)
8108  {
8109  if (!(order->used & map))
8110  {
8111  found++;
8112  order->used|= map;
8113  }
8114  continue; // Used in ORDER BY
8115  }
8116  if (!only_eq_ref_tables(join, start_order, (*ref_item)->used_tables(),
8117  cached_eq_ref_tables, eq_ref_tables))
8118  return false;
8119  }
8120  }
8121  /* Check that there was no reference to table before sort order */
8122  for (; found && start_order ; start_order=start_order->next)
8123  {
8124  if (start_order->used & map)
8125  {
8126  found--;
8127  continue;
8128  }
8129  if (start_order->depend_map & map)
8130  return false;
8131  }
8132  return true;
8133 }
8134 
8135 
8137 static bool
8138 only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables,
8139  table_map *cached_eq_ref_tables, table_map *eq_ref_tables)
8140 {
8141  tables&= ~PSEUDO_TABLE_BITS;
8142  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
8143  {
8144  if (tables & 1)
8145  {
8146  const table_map map= (*tab)->table->map;
8147  bool is_eq_ref;
8148  if (*cached_eq_ref_tables & map) // then there exists a cached bit
8149  is_eq_ref= *eq_ref_tables & map;
8150  else
8151  {
8152  is_eq_ref= eq_ref_table(join, order, *tab,
8153  cached_eq_ref_tables, eq_ref_tables);
8154  if (is_eq_ref)
8155  *eq_ref_tables|= map;
8156  else
8157  *eq_ref_tables&= ~map;
8158  *cached_eq_ref_tables|= map; // now there exists a cached bit
8159  }
8160  if (!is_eq_ref)
8161  return false;
8162  }
8163  }
8164  return true;
8165 }
8166 
8167 
8180 static bool duplicate_order(const ORDER *first_order,
8181  const ORDER *possible_dup)
8182 {
8183  const ORDER *order;
8184  for (order=first_order; order ; order=order->next)
8185  {
8186  if (order == possible_dup)
8187  {
8188  // all expressions preceding possible_dup have been checked.
8189  return false;
8190  }
8191  else
8192  {
8193  const Item *it1= order->item[0]->real_item();
8194  const Item *it2= possible_dup->item[0]->real_item();
8195 
8196  if (it1->type() == Item::FIELD_ITEM &&
8197  it2->type() == Item::FIELD_ITEM &&
8198  (static_cast<const Item_field*>(it1)->field ==
8199  static_cast<const Item_field*>(it2)->field))
8200  {
8201  return true;
8202  }
8203  }
8204  }
8205  return false;
8206 }
8207 
8228 static ORDER *
8229 remove_const(JOIN *join,ORDER *first_order, Item *cond,
8230  bool change_list, bool *simple_order, const char *clause_type)
8231 {
8232  if (join->plan_is_const())
8233  return change_list ? 0 : first_order; // No need to sort
8234 
8235  Opt_trace_context * const trace= &join->thd->opt_trace;
8236  Opt_trace_disable_I_S trace_disabled(trace, first_order == NULL);
8237  Opt_trace_object trace_wrapper(trace);
8238  Opt_trace_object trace_simpl(trace, "clause_processing");
8239  if (trace->is_started())
8240  {
8241  trace_simpl.add_alnum("clause", clause_type);
8242  String str;
8243  st_select_lex::print_order(&str, first_order,
8244  enum_query_type(QT_TO_SYSTEM_CHARSET |
8245  QT_SHOW_SELECT_NUMBER |
8246  QT_NO_DEFAULT_DB));
8247  trace_simpl.add_utf8("original_clause", str.ptr(), str.length());
8248  }
8249  Opt_trace_array trace_each_item(trace, "items");
8250 
8251  ORDER *order,**prev_ptr;
8252  table_map first_table= join->join_tab[join->const_tables].table->map;
8253  table_map not_const_tables= ~join->const_table_map;
8254  table_map ref;
8255  // Caches to avoid repeating eq_ref_table() calls, @see eq_ref_table()
8256  table_map eq_ref_tables= 0, cached_eq_ref_tables= 0;
8257  DBUG_ENTER("remove_const");
8258 
8259  prev_ptr= &first_order;
8260  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
8261 
8262  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
8263 
8264  update_depend_map(join, first_order);
8265  for (order=first_order; order ; order=order->next)
8266  {
8267  Opt_trace_object trace_one_item(trace);
8268  trace_one_item.add("item", order->item[0]);
8269  table_map order_tables=order->item[0]->used_tables();
8270  if (order->item[0]->with_sum_func ||
8271  /*
8272  If the outer table of an outer join is const (either by itself or
8273  after applying WHERE condition), grouping on a field from such a
8274  table will be optimized away and filesort without temporary table
8275  will be used unless we prevent that now. Filesort is not fit to
8276  handle joins and the join condition is not applied. We can't detect
8277  the case without an expensive test, however, so we force temporary
8278  table for all queries containing more than one table, ROLLUP, and an
8279  outer join.
8280  */
8281  (join->primary_tables > 1 &&
8282  join->rollup.state == ROLLUP::STATE_INITED &&
8283  join->outer_join))
8284  *simple_order=0; // Must do a temp table to sort
8285  else if (!(order_tables & not_const_tables))
8286  {
8287  if (order->item[0]->has_subquery() &&
8288  !(join->select_lex->options & SELECT_DESCRIBE))
8289  {
8290  Opt_trace_array trace_subselect(trace, "subselect_evaluation");
8291  order->item[0]->val_str(&order->item[0]->str_value);
8292  }
8293  trace_one_item.add("uses_only_constant_tables", true);
8294  continue; // skip const item
8295  }
8296  else if (duplicate_order(first_order, order))
8297  {
8298  /*
8299  If 'order' is a duplicate of an expression earlier in the
8300  ORDER/GROUP BY sequence, it can be removed from the ORDER BY
8301  or GROUP BY clause.
8302  */
8303  trace_one_item.add("duplicate_item", true);
8304  continue;
8305  }
8306  else if (order->in_field_list && order->item[0]->has_subquery())
8307  /*
8308  If the order item is a subquery that is also in the field
8309  list, a temp table should be used to avoid evaluating the
8310  subquery for each row both when a) creating a sort index and
8311  b) getting the value.
8312  Example: "SELECT (SELECT ... ) as a ... GROUP BY a;"
8313  */
8314  *simple_order= false;
8315  else
8316  {
8317  if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
8318  *simple_order=0;
8319  else
8320  {
8321  if (cond && const_expression_in_where(cond,order->item[0]))
8322  {
8323  trace_one_item.add("equals_constant_in_where", true);
8324  continue;
8325  }
8326  if ((ref=order_tables & (not_const_tables ^ first_table)))
8327  {
8328  if (!(order_tables & first_table) &&
8329  only_eq_ref_tables(join, first_order, ref,
8330  &cached_eq_ref_tables, &eq_ref_tables))
8331  {
8332  trace_one_item.add("eq_ref_to_preceding_items", true);
8333  continue;
8334  }
8335  *simple_order=0; // Must do a temp table to sort
8336  }
8337  }
8338  }
8339  if (change_list)
8340  *prev_ptr= order; // use this entry
8341  prev_ptr= &order->next;
8342  }
8343  if (change_list)
8344  *prev_ptr=0;
8345  if (prev_ptr == &first_order) // Nothing to sort/group
8346  *simple_order=1;
8347  DBUG_PRINT("exit",("simple_order: %d",(int) *simple_order));
8348 
8349  trace_each_item.end();
8350  trace_simpl.add("resulting_clause_is_simple", *simple_order);
8351  if (trace->is_started() && change_list)
8352  {
8353  String str;
8354  st_select_lex::print_order(&str, first_order,
8355  enum_query_type(QT_TO_SYSTEM_CHARSET |
8356  QT_SHOW_SELECT_NUMBER |
8357  QT_NO_DEFAULT_DB));
8358  trace_simpl.add_utf8("resulting_clause", str.ptr(), str.length());
8359  }
8360 
8361  DBUG_RETURN(first_order);
8362 }
8363 
8364 
8387 Item *
8388 optimize_cond(THD *thd, Item *conds, COND_EQUAL **cond_equal,
8389  List<TABLE_LIST> *join_list,
8390  bool build_equalities, Item::cond_result *cond_value)
8391 {
8392  Opt_trace_context * const trace= &thd->opt_trace;
8393  DBUG_ENTER("optimize_cond");
8394 
8395  if (conds)
8396  {
8397  Opt_trace_object trace_wrapper(trace);
8398  Opt_trace_object trace_cond(trace, "condition_processing");
8399  trace_cond.add_alnum("condition", build_equalities ? "WHERE" : "HAVING");
8400  trace_cond.add("original_condition", conds);
8401  Opt_trace_array trace_steps(trace, "steps");
8402 
8403  /*
8404  Build all multiple equality predicates and eliminate equality
8405  predicates that can be inferred from these multiple equalities.
8406  For each reference of a field included into a multiple equality
8407  that occurs in a function set a pointer to the multiple equality
8408  predicate. Substitute a constant instead of this field if the
8409  multiple equality contains a constant.
8410  */
8411  if (build_equalities)
8412  {
8413  Opt_trace_object step_wrapper(trace);
8414  step_wrapper.add_alnum("transformation", "equality_propagation");
8415  {
8417  disable_trace_wrapper(trace, !conds->has_subquery());
8419  trace_subselect(trace, "subselect_evaluation");
8420  conds= build_equal_items(thd, conds, NULL, true,
8421  join_list, cond_equal);
8422  }
8423  step_wrapper.add("resulting_condition", conds);
8424  }
8425 
8426  /* change field = field to field = const for each found field = const */
8427  {
8428  Opt_trace_object step_wrapper(trace);
8429  step_wrapper.add_alnum("transformation", "constant_propagation");
8430  {
8432  disable_trace_wrapper(trace, !conds->has_subquery());
8434  trace_subselect(trace, "subselect_evaluation");
8435  propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
8436  }
8437  step_wrapper.add("resulting_condition", conds);
8438  }
8439 
8440  /*
8441  Remove all instances of item == item
8442  Remove all and-levels where CONST item != CONST item
8443  */
8444  DBUG_EXECUTE("where",print_where(conds,"after const change", QT_ORDINARY););
8445  {
8446  Opt_trace_object step_wrapper(trace);
8447  step_wrapper.add_alnum("transformation", "trivial_condition_removal");
8448  {
8450  disable_trace_wrapper(trace, !conds->has_subquery());
8451  Opt_trace_array trace_subselect(trace, "subselect_evaluation");
8452  conds= remove_eq_conds(thd, conds, cond_value) ;
8453  }
8454  step_wrapper.add("resulting_condition", conds);
8455  }
8456  }
8457  DBUG_RETURN(conds);
8458 }
8459 
8460 
8481 static Item *
8482 internal_remove_eq_conds(THD *thd, Item *cond, Item::cond_result *cond_value)
8483 {
8484  if (cond->type() == Item::COND_ITEM)
8485  {
8486  bool and_level= ((Item_cond*) cond)->functype()
8487  == Item_func::COND_AND_FUNC;
8488  List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
8489  Item::cond_result tmp_cond_value;
8490  bool should_fix_fields=0;
8491 
8492  *cond_value=Item::COND_UNDEF;
8493  Item *item;
8494  while ((item=li++))
8495  {
8496  Item *new_item=internal_remove_eq_conds(thd, item, &tmp_cond_value);
8497  if (!new_item)
8498  li.remove();
8499  else if (item != new_item)
8500  {
8501  (void) li.replace(new_item);
8502  should_fix_fields=1;
8503  }
8504  if (*cond_value == Item::COND_UNDEF)
8505  *cond_value=tmp_cond_value;
8506  switch (tmp_cond_value) {
8507  case Item::COND_OK: // Not TRUE or FALSE
8508  if (and_level || *cond_value == Item::COND_FALSE)
8509  *cond_value=tmp_cond_value;
8510  break;
8511  case Item::COND_FALSE:
8512  if (and_level)
8513  {
8514  *cond_value=tmp_cond_value;
8515  return (Item*) 0; // Always false
8516  }
8517  break;
8518  case Item::COND_TRUE:
8519  if (!and_level)
8520  {
8521  *cond_value= tmp_cond_value;
8522  return (Item*) 0; // Always true
8523  }
8524  break;
8525  case Item::COND_UNDEF: // Impossible
8526  break; /* purecov: deadcode */
8527  }
8528  }
8529  if (should_fix_fields)
8530  cond->update_used_tables();
8531 
8532  if (!((Item_cond*) cond)->argument_list()->elements ||
8533  *cond_value != Item::COND_OK)
8534  return (Item*) 0;
8535  if (((Item_cond*) cond)->argument_list()->elements == 1)
8536  {
8537  /*
8538  BUG#11765699:
8539  We're dealing with an AND or OR item that has only one
8540  argument. However, it is not an option to empty the list
8541  because:
8542 
8543  - this function is called for either JOIN::conds or
8544  JOIN::having, but these point to the same condition as
8545  SELECT_LEX::where and SELECT_LEX::having do.
8546 
8547  - The return value of remove_eq_conds() is assigned to
8548  JOIN::conds and JOIN::having, so emptying the list and
8549  returning the only remaining item "replaces" the AND or OR
8550  with item for the variables in JOIN. However, the return
8551  value is not assigned to the SELECT_LEX counterparts. Thus,
8552  if argument_list is emptied, SELECT_LEX forgets the item in
8553  argument_list()->head().
8554 
8555  item is therefore returned, but argument_list is not emptied.
8556  */
8557  item= ((Item_cond*) cond)->argument_list()->head();
8558  /*
8559  Consider reenabling the line below when the optimizer has been
8560  split into properly separated phases.
8561 
8562  ((Item_cond*) cond)->argument_list()->empty();
8563  */
8564  return item;
8565  }
8566  }
8567  else if (cond->type() == Item::FUNC_ITEM &&
8568  ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
8569  {
8570  Item_func_isnull *func=(Item_func_isnull*) cond;
8571  Item **args= func->arguments();
8572  if (args[0]->type() == Item::FIELD_ITEM)
8573  {
8574  Field *field=((Item_field*) args[0])->field;
8575  /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
8576  /*
8577  See BUG#12594011
8578  Documentation says that
8579  SELECT datetime_notnull d FROM t1 WHERE d IS NULL
8580  shall return rows where d=='0000-00-00'
8581 
8582  Thus, for DATE and DATETIME columns defined as NOT NULL,
8583  "date_notnull IS NULL" has to be modified to
8584  "date_notnull IS NULL OR date_notnull == 0" (if outer join)
8585  "date_notnull == 0" (otherwise)
8586 
8587  */
8588  if (((field->type() == MYSQL_TYPE_DATE) ||
8589  (field->type() == MYSQL_TYPE_DATETIME)) &&
8590  (field->flags & NOT_NULL_FLAG))
8591  {
8592  Item *item0= new(thd->mem_root) Item_int((longlong)0, 1);
8593  Item *eq_cond= new(thd->mem_root) Item_func_eq(args[0], item0);
8594  if (!eq_cond)
8595  return cond;
8596 
8597  if (args[0]->is_outer_field())
8598  {
8599  // outer join: transform "col IS NULL" to "col IS NULL or col=0"
8600  Item *or_cond= new(thd->mem_root) Item_cond_or(eq_cond, cond);
8601  if (!or_cond)
8602  return cond;
8603  cond= or_cond;
8604  }
8605  else
8606  {
8607  // not outer join: transform "col IS NULL" to "col=0"
8608  cond= eq_cond;
8609  }
8610 
8611  cond->fix_fields(thd, &cond);
8612  }
8613  }
8614  if (cond->const_item())
8615  {
8616  *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
8617  return (Item*) 0;
8618  }
8619  }
8620  else if (cond->const_item() && !cond->is_expensive())
8621  {
8622  *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
8623  return (Item*) 0;
8624  }
8625  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
8626  { // boolan compare function
8627  Item *left_item= ((Item_func*) cond)->arguments()[0];
8628  Item *right_item= ((Item_func*) cond)->arguments()[1];
8629  if (left_item->eq(right_item,1))
8630  {
8631  if (!left_item->maybe_null ||
8632  ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
8633  return (Item*) 0; // Compare of identical items
8634  }
8635  }
8636  *cond_value=Item::COND_OK;
8637  return cond; // Point at next and level
8638 }
8639 
8640 
8661 Item *
8662 remove_eq_conds(THD *thd, Item *cond, Item::cond_result *cond_value)
8663 {
8664  if (cond->type() == Item::FUNC_ITEM &&
8665  ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
8666  {
8667  /*
8668  Handles this special case for some ODBC applications:
8669  The are requesting the row that was just updated with a auto_increment
8670  value with this construct:
8671 
8672  SELECT * from table_name where auto_increment_column IS NULL
8673  This will be changed to:
8674  SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
8675  */
8676 
8677  Item_func_isnull *func=(Item_func_isnull*) cond;
8678  Item **args= func->arguments();
8679  if (args[0]->type() == Item::FIELD_ITEM)
8680  {
8681  Field *field=((Item_field*) args[0])->field;
8682  if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
8683  (thd->variables.option_bits & OPTION_AUTO_IS_NULL) &&
8684  (thd->first_successful_insert_id_in_prev_stmt > 0 &&
8685  thd->substitute_null_with_insert_id))
8686  {
8687 #ifdef HAVE_QUERY_CACHE
8688  query_cache_abort(&thd->query_cache_tls);
8689 #endif
8690  Item *new_cond;
8691  if ((new_cond= new Item_func_eq(args[0],
8692  new Item_int(NAME_STRING("last_insert_id()"),
8693  thd->read_first_successful_insert_id_in_prev_stmt(),
8694  MY_INT64_NUM_DECIMAL_DIGITS))))
8695  {
8696  cond=new_cond;
8697  /*
8698  Item_func_eq can't be fixed after creation so we do not check
8699  cond->fixed, also it do not need tables so we use 0 as second
8700  argument.
8701  */
8702  cond->fix_fields(thd, &cond);
8703  }
8704  /*
8705  IS NULL should be mapped to LAST_INSERT_ID only for first row, so
8706  clear for next row
8707  */
8708  thd->substitute_null_with_insert_id= FALSE;
8709 
8710  *cond_value= Item::COND_OK;
8711  return cond;
8712  }
8713  }
8714  }
8715  return internal_remove_eq_conds(thd, cond, cond_value); // Scan all the condition
8716 }
8717 
8718 
8755 static bool
8756 list_contains_unique_index(JOIN_TAB *tab,
8757  bool (*find_func) (Field *, void *), void *data)
8758 {
8759  TABLE *table= tab->table;
8760 
8761  if (tab->is_inner_table_of_outer_join())
8762  return 0;
8763  for (uint keynr= 0; keynr < table->s->keys; keynr++)
8764  {
8765  if (keynr == table->s->primary_key ||
8766  (table->key_info[keynr].flags & HA_NOSAME))
8767  {
8768  KEY *keyinfo= table->key_info + keynr;
8769  KEY_PART_INFO *key_part, *key_part_end;
8770 
8771  for (key_part=keyinfo->key_part,
8772  key_part_end=key_part+ keyinfo->user_defined_key_parts;
8773  key_part < key_part_end;
8774  key_part++)
8775  {
8776  if (key_part->field->real_maybe_null() ||
8777  !find_func(key_part->field, data))
8778  break;
8779  }
8780  if (key_part == key_part_end)
8781  return 1;
8782  }
8783  }
8784  return 0;
8785 }
8786 
8787 
8802 static bool
8803 find_field_in_order_list (Field *field, void *data)
8804 {
8805  ORDER *group= (ORDER *) data;
8806  bool part_found= 0;
8807  for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
8808  {
8809  Item *item= (*tmp_group->item)->real_item();
8810  if (item->type() == Item::FIELD_ITEM &&
8811  ((Item_field*) item)->field->eq(field))
8812  {
8813  part_found= 1;
8814  break;
8815  }
8816  }
8817  return part_found;
8818 }
8819 
8820 
8835 static bool
8836 find_field_in_item_list (Field *field, void *data)
8837 {
8838  List<Item> *fields= (List<Item> *) data;
8839  bool part_found= 0;
8840  List_iterator<Item> li(*fields);
8841  Item *item;
8842 
8843  while ((item= li++))
8844  {
8845  if (item->type() == Item::FIELD_ITEM &&
8846  ((Item_field*) item)->field->eq(field))
8847  {
8848  part_found= 1;
8849  break;
8850  }
8851  }
8852  return part_found;
8853 }
8854 
8855 
8863 static ORDER *
8864 create_distinct_group(THD *thd, Ref_ptr_array ref_pointer_array,
8865  ORDER *order_list, List<Item> &fields,
8866  List<Item> &all_fields,
8867  bool *all_order_by_fields_used)
8868 {
8869  List_iterator<Item> li(fields);
8870  Item *item;
8871  Ref_ptr_array orig_ref_pointer_array= ref_pointer_array;
8872  ORDER *order,*group,**prev;
8873 
8874  *all_order_by_fields_used= 1;
8875  while ((item=li++))
8876  item->marker=0; /* Marker that field is not used */
8877 
8878  prev= &group; group=0;
8879  for (order=order_list ; order; order=order->next)
8880  {
8881  if (order->in_field_list)
8882  {
8883  ORDER *ord=(ORDER*) thd->memdup((char*) order,sizeof(ORDER));
8884  if (!ord)
8885  return 0;
8886  *prev=ord;
8887  prev= &ord->next;
8888  (*ord->item)->marker=1;
8889  }
8890  else
8891  *all_order_by_fields_used= 0;
8892  }
8893 
8894  li.rewind();
8895  while ((item=li++))
8896  {
8897  if (!item->const_item() && !item->with_sum_func && !item->marker)
8898  {
8899  /*
8900  Don't put duplicate columns from the SELECT list into the
8901  GROUP BY list.
8902  */
8903  ORDER *ord_iter;
8904  for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
8905  if ((*ord_iter->item)->eq(item, 1))
8906  goto next_item;
8907 
8908  ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER));
8909  if (!ord)
8910  return 0;
8911 
8912  if (item->type() == Item::FIELD_ITEM &&
8913  item->field_type() == MYSQL_TYPE_BIT)
8914  {
8915  /*
8916  Because HEAP tables can't index BIT fields we need to use an
8917  additional hidden field for grouping because later it will be
8918  converted to a LONG field. Original field will remain of the
8919  BIT type and will be returned to a client.
8920  */
8921  Item_field *new_item= new Item_field(thd, (Item_field*)item);
8922  int el= all_fields.elements;
8923  orig_ref_pointer_array[el]= new_item;
8924  all_fields.push_front(new_item);
8925  ord->item= &orig_ref_pointer_array[el];
8926  }
8927  else
8928  {
8929  /*
8930  We have here only field_list (not all_field_list), so we can use
8931  simple indexing of ref_pointer_array (order in the array and in the
8932  list are same)
8933  */
8934  ord->item= &ref_pointer_array[0];
8935  }
8936  ord->direction= ORDER::ORDER_ASC;
8937  *prev=ord;
8938  prev= &ord->next;
8939  }
8940 next_item:
8941  ref_pointer_array.pop_front();
8942  }
8943  *prev=0;
8944  return group;
8945 }
8946 
8947 
8953 static TABLE *
8954 get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables)
8955 {
8956  table_map map= (table_map) 0;
8957  DBUG_ENTER("get_sort_by_table");
8958 
8959  if (!a)
8960  a=b; // Only one need to be given
8961  else if (!b)
8962  b=a;
8963 
8964  for (; a && b; a=a->next,b=b->next)
8965  {
8966  if (!(*a->item)->eq(*b->item,1))
8967  DBUG_RETURN(0);
8968  map|=a->item[0]->used_tables();
8969  }
8970  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
8971  DBUG_RETURN(0);
8972 
8973  for (; !(map & tables->table->map); tables= tables->next_leaf) ;
8974  if (map != tables->table->map)
8975  DBUG_RETURN(0); // More than one table
8976  DBUG_PRINT("exit",("sort by table: %d",tables->table->tablenr));
8977  DBUG_RETURN(tables->table);
8978 }
8979 
8980 
8992 static Item_cond_and *create_cond_for_const_ref(THD *thd, JOIN_TAB *join_tab)
8993 {
8994  DBUG_ENTER("create_cond_for_const_ref");
8995  DBUG_ASSERT(join_tab->ref.key_parts);
8996 
8997  TABLE *table= join_tab->table;
8998  Item_cond_and *cond= new Item_cond_and();
8999  if (!cond)
9000  DBUG_RETURN(NULL);
9001 
9002  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
9003  {
9004  Field *field= table->field[table->key_info[join_tab->ref.key].key_part[i].
9005  fieldnr-1];
9006  Item *value= join_tab->ref.items[i];
9007  Item *item= new Item_field(field);
9008  if (!item)
9009  DBUG_RETURN(NULL);
9010  item= join_tab->ref.null_rejecting & ((key_part_map)1 << i) ?
9011  (Item *)new Item_func_eq(item, value) :
9012  (Item *)new Item_func_equal(item, value);
9013  if (!item)
9014  DBUG_RETURN(NULL);
9015  if (cond->add(item))
9016  DBUG_RETURN(NULL);
9017  }
9018  cond->fix_fields(thd, (Item**)&cond);
9019 
9020  DBUG_RETURN(cond);
9021 }
9022 
9028 static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
9029 {
9030  DBUG_ENTER("add_ref_to_table_cond");
9031  if (!join_tab->ref.key_parts)
9032  DBUG_RETURN(FALSE);
9033 
9034  int error= 0;
9035 
9036  /* Create a condition representing the const reference. */
9037  Item_cond_and *cond= create_cond_for_const_ref(thd, join_tab);
9038  if (!cond)
9039  DBUG_RETURN(TRUE);
9040 
9041  /* Add this condition to the existing select condtion */
9042  if (join_tab->select)
9043  {
9044  if (join_tab->select->cond)
9045  {
9046  error=(int) cond->add(join_tab->select->cond);
9047  cond->update_used_tables();
9048  }
9049  join_tab->set_jt_and_sel_condition(cond, __LINE__);
9050  }
9051  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
9052  &error)))
9053  join_tab->set_condition(cond, __LINE__);
9054 
9055  if (join_tab->select)
9056  Opt_trace_object(&thd->opt_trace).add("added_back_ref_condition", cond);
9057  /*
9058  If we have pushed parts of the select condition down to the
9059  storage engine we also need to add the condition for the const
9060  reference to the pre_idx_push_cond since this might be used
9061  later (in test_if_skip_sort_order()) instead of the condition.
9062  */
9063  if (join_tab->pre_idx_push_cond)
9064  {
9065  cond= create_cond_for_const_ref(thd, join_tab);
9066  if (!cond)
9067  DBUG_RETURN(TRUE);
9068  if (cond->add(join_tab->pre_idx_push_cond))
9069  DBUG_RETURN(TRUE);
9070  join_tab->pre_idx_push_cond = cond;
9071  }
9072 
9073  DBUG_RETURN(error ? TRUE : FALSE);
9074 }
9075 
9076 
9088 static Item *remove_additional_cond(Item* conds)
9089 {
9090  // Because it uses in_additional_cond it applies only to the scalar case.
9091  if (conds->item_name.ptr() == in_additional_cond)
9092  return 0;
9093  if (conds->type() == Item::COND_ITEM)
9094  {
9095  Item_cond *cnd= (Item_cond*) conds;
9096  List_iterator<Item> li(*(cnd->argument_list()));
9097  Item *item;
9098  while ((item= li++))
9099  {
9100  if (item->item_name.ptr() == in_additional_cond)
9101  {
9102  li.remove();
9103  if (cnd->argument_list()->elements == 1)
9104  return cnd->argument_list()->head();
9105  return conds;
9106  }
9107  }
9108  }
9109  return conds;
9110 }
9111 
9112 
9113 /*
9114  Index lookup-based subquery: save some flags for EXPLAIN output
9115 
9116  SYNOPSIS
9117  save_index_subquery_explain_info()
9118  join_tab Subquery's join tab (there is only one as index lookup is
9119  only used for subqueries that are single-table SELECTs)
9120  where Subquery's WHERE clause
9121 
9122  DESCRIPTION
9123  For index lookup-based subquery (subselect_indexsubquery_engine),
9124  check its EXPLAIN output row should contain
9125  "Using index" (TAB_INFO_FULL_SCAN_ON_NULL)
9126  "Using Where" (TAB_INFO_USING_WHERE)
9127  "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
9128  and set appropriate flags in join_tab->packed_info.
9129 
9130  TODO:
9131  packed_info causes duplication in EXPLAIN code. For example, we print
9132  "using where" in 2 places of EXPLAIN code: if tab->condition(), OR if
9133  'packed_info & TAB_INFO_USING_WHERE'.
9134  indexsubquery_engine is the only user of
9135  save_index_subquery_explain_info().
9136  packed_info is almost useless today, it would be good to get rid of it
9137  (and thus of save_index_subquery_explain_info()).
9138 */
9139 
9140 static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
9141 {
9142  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
9143 
9144  /*
9145  This is actually not needed, 'non-packed-info' branch of EXPLAIN naturally
9146  reads covering_keys and produces the desired 'Using index'
9147  */
9148  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
9149  join_tab->packed_info |= TAB_INFO_USING_INDEX;
9150 
9151  /*
9152  This is needed, because 'where' (==join->conds) may be NULL, or
9153  shorter than select->cond/tab->condition(), due to
9154  remove_subq_pushed_predicates() and remove_additional_cond(); the real
9155  condition which will be checked for each row is
9156  indexsubquery_engine::cond (==join->conds).
9157  Still this should be solvable without TAB_INFO_USING_WHERE.
9158  */
9159  if (where)
9160  join_tab->packed_info |= TAB_INFO_USING_WHERE;
9161 
9162  /*
9163  This is actually not needed, 'non-packed-info' branch of EXPLAIN naturally
9164  reads has_guarded_conds() and produces the desired 'Full scan on NULL
9165  key'.
9166  */
9167  if (join_tab->has_guarded_conds())
9168  join_tab->packed_info|= TAB_INFO_FULL_SCAN_ON_NULL;
9169 }
9170 
9171 
9176 static void optimize_keyuse(JOIN *join, Key_use_array *keyuse_array)
9177 {
9178  for (size_t ix= 0; ix < keyuse_array->size(); ++ix)
9179  {
9180  Key_use *keyuse= &keyuse_array->at(ix);
9181  table_map map;
9182  /*
9183  If we find a ref, assume this table matches a proportional
9184  part of this table.
9185  For example 100 records matching a table with 5000 records
9186  gives 5000/100 = 50 records per key
9187  Constant tables are ignored.
9188  To avoid bad matches, we don't make ref_table_rows less than 100.
9189  */
9190  keyuse->ref_table_rows= ~(ha_rows) 0; // If no ref
9191  if (keyuse->used_tables &
9192  (map= (keyuse->used_tables & ~join->const_table_map &
9193  ~OUTER_REF_TABLE_BIT)))
9194  {
9195  uint tablenr;
9196  for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
9197  if (map == 1) // Only one table
9198  {
9199  TABLE *tmp_table= join->join_tab[tablenr].table;
9200  keyuse->ref_table_rows= max<ha_rows>(tmp_table->file->stats.records, 100);
9201  }
9202  }
9203  /*
9204  Outer reference (external field) is constant for single executing
9205  of subquery
9206  */
9207  if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
9208  keyuse->ref_table_rows= 1;
9209  }
9210 }
9211 
9212 
9213 void JOIN::optimize_fts_query()
9214 {
9215  if (primary_tables > 1)
9216  return; // We only optimize single table FTS queries
9217 
9218  JOIN_TAB * const tab= &(join_tab[0]);
9219  if (tab->type != JT_FT)
9220  return; // Access is not using FTS result
9221 
9222  if ((tab->table->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0)
9223  return; // Optimizations requires extended FTS support by table engine
9224 
9225  Item_func_match* fts_result= static_cast<Item_func_match*>(tab->keyuse->val);
9226 
9227  /* If we are ordering on the rank of the same result as is used for access,
9228  and the table engine deliver result ordered by rank, we can drop ordering.
9229  */
9230  if (order != NULL
9231  && order->next == NULL &&
9232  order->direction == ORDER::ORDER_DESC &&
9233  fts_result->eq(*(order->item), true))
9234  {
9235  Item_func_match* fts_item=
9236  static_cast<Item_func_match*>(*(order->item));
9237 
9238  /* If we applied the LIMIT optimization @see optimize_fts_limit_query,
9239  check that the number of matching rows is sufficient.
9240  Otherwise, revert this optimization and use table scan instead.
9241  */
9242  if (min_ft_matches != HA_POS_ERROR &&
9243  min_ft_matches > fts_item->get_count())
9244  {
9245  // revert to table scan, do things make_join_readinfo would have done
9246  tab->type= JT_ALL;
9248  tab->use_quick= QS_NONE;
9249  tab->ref.key= -1;
9250 
9251  // Reset join condition
9252  tab->select->cond= NULL;
9253  conds= NULL;
9254 
9255  thd->set_status_no_index_used();
9256  // make_join_readinfo only calls inc_status_select_scan()
9257  // when this is not SELECT_DESCRIBE
9258  DBUG_ASSERT((select_options & SELECT_DESCRIBE) == 0);
9259  thd->inc_status_select_scan();
9260 
9261  return;
9262  }
9263  else if (fts_item->ordered_result())
9264  order= NULL;
9265  }
9266 
9267  /* Check whether the FTS result is covering. If only document id
9268  and rank is needed, there is no need to access table rows.
9269  */
9270  List_iterator<Item> it(all_fields);
9271  Item *item;
9272  // This optimization does not work with filesort nor GROUP BY
9273  bool covering= (!order && !group);
9274  bool docid_found= false;
9275  while (covering && (item= it++))
9276  {
9277  switch (item->type()) {
9278  case Item::FIELD_ITEM:
9279  {
9280  Item_field *item_field= static_cast<Item_field*>(item);
9281  if (strcmp(item_field->field_name, FTS_DOC_ID_COL_NAME) == 0)
9282  {
9283  docid_found= true;
9284  covering= fts_result->docid_in_result();
9285  }
9286  else
9287  covering= false;
9288  break;
9289  }
9290  case Item::FUNC_ITEM:
9291  if (static_cast<Item_func*>(item)->functype() == Item_func::FT_FUNC)
9292  {
9293  Item_func_match* fts_item= static_cast<Item_func_match*>(item);
9294  if (fts_item->eq(fts_result, true))
9295  break;
9296  }
9297  // Fall-through when not an equivalent MATCH expression
9298  default:
9299  covering= false;
9300  }
9301  }
9302 
9303  if (covering)
9304  {
9305  if (docid_found)
9306  {
9307  replace_item_field(FTS_DOC_ID_COL_NAME,
9308  new Item_func_docid(reinterpret_cast<FT_INFO_EXT*>
9309  (fts_result->ft_handler)));
9310  }
9311 
9312  // Tell storage engine that row access is not necessary
9313  fts_result->table->set_keyread(true);
9314  fts_result->table->covering_keys.set_bit(fts_result->key);
9315  }
9316 }
9317 
9318 
9336 void
9337 JOIN::optimize_fts_limit_query()
9338 {
9339  /*
9340  Only do this optimization if
9341  1. It is a single table query
9342  2. There is no WHERE condition
9343  3. There is a single ORDER BY element
9344  4. Ordering is descending
9345  5. There is a LIMIT clause
9346  6. Ordering is on a MATCH expression
9347  */
9348  if (primary_tables == 1 && // 1
9349  conds == NULL && // 2
9350  order && order->next == NULL && // 3
9351  order->direction == ORDER::ORDER_DESC && // 4
9352  m_select_limit != HA_POS_ERROR) // 5
9353  {
9354  DBUG_ASSERT(order->item);
9355  Item* item= *order->item;
9356  DBUG_ASSERT(item);
9357 
9358  if (item->type() == Item::FUNC_ITEM &&
9359  static_cast<Item_func*>(item)->functype() == Item_func::FT_FUNC) // 6
9360  {
9361  conds= item;
9362  min_ft_matches= m_select_limit;
9363  }
9364  }
9365 }
9366 
9367 
9381 static void calculate_materialization_costs(JOIN *join,
9382  TABLE_LIST *sj_nest,
9383  uint n_tables,
9384  Semijoin_mat_optimize *sjm)
9385 {
9386  double mat_cost; // Estimated cost of materialization
9387  double mat_rowcount; // Estimated row count before duplicate removal
9388  double distinct_rowcount; // Estimated rowcount after duplicate removal
9389  List<Item> *inner_expr_list;
9390 
9391  if (sj_nest)
9392  {
9393  /*
9394  get_partial_join_cost() assumes a regular join, which is correct when
9395  we optimize a sj-materialization nest (always executed as regular
9396  join).
9397  @todo consider using join->best_rowcount instead.
9398  */
9399  get_partial_join_cost(join, n_tables,
9400  &mat_cost, &mat_rowcount);
9401  n_tables+= join->const_tables;
9402  inner_expr_list= &sj_nest->nested_join->sj_inner_exprs;
9403  }
9404  else
9405  {
9406  mat_cost= join->best_read;
9407  mat_rowcount= join->best_rowcount;
9408  inner_expr_list= &join->select_lex->item_list;
9409  }
9410 
9411  /*
9412  Adjust output cardinality estimates. If the subquery has form
9413 
9414  ... oe IN (SELECT t1.colX, t2.colY, func(X,Y,Z) )
9415 
9416  then the number of distinct output record combinations has an
9417  upper bound of product of number of records matching the tables
9418  that are used by the SELECT clause.
9419  TODO:
9420  We can get a more precise estimate if we
9421  - use rec_per_key cardinality estimates. For simple cases like
9422  "oe IN (SELECT t.key ...)" it is trivial.
9423  - Functional dependencies between the tables in the semi-join
9424  nest (the payoff is probably less here?)
9425  */
9426  {
9427  for (uint i=0 ; i < n_tables ; i++)
9428  {
9429  JOIN_TAB * const tab= join->best_positions[i].table;
9430  join->map2table[tab->table->tablenr]= tab;
9431  }
9432  List_iterator<Item> it(*inner_expr_list);
9433  Item *item;
9434  table_map map= 0;
9435  while ((item= it++))
9436  map|= item->used_tables();
9437  map&= ~PSEUDO_TABLE_BITS;
9438  Table_map_iterator tm_it(map);
9439  int tableno;
9440  double rows= 1.0;
9441  while ((tableno = tm_it.next_bit()) != Table_map_iterator::BITMAP_END)
9442  rows*= join->map2table[tableno]->table->quick_condition_rows;
9443  distinct_rowcount= min(mat_rowcount, rows);
9444  }
9445  /*
9446  Calculate temporary table parameters and usage costs
9447  */
9448  const uint rowlen= get_tmp_table_rec_length(*inner_expr_list);
9449 
9450  double row_cost; // The cost to write or lookup a row in temp. table
9451  double create_cost; // The cost to create a temporary table
9452  if (rowlen * distinct_rowcount <
9453  join->thd->variables.max_heap_table_size)
9454  {
9455  row_cost= HEAP_TEMPTABLE_ROW_COST;
9456  create_cost= HEAP_TEMPTABLE_CREATE_COST;
9457  }
9458  else
9459  {
9460  row_cost= DISK_TEMPTABLE_ROW_COST;
9461  create_cost= DISK_TEMPTABLE_CREATE_COST;
9462  }
9463 
9464  /*
9465  Let materialization cost include the cost to create the temporary
9466  table and write the rows into it:
9467  */
9468  mat_cost+= create_cost + (mat_rowcount * row_cost);
9469  sjm->materialization_cost.reset();
9471  .add_io(mat_cost);
9472 
9473  sjm->expected_rowcount= distinct_rowcount;
9474 
9475  /*
9476  Set the cost to do a full scan of the temptable (will need this to
9477  consider doing sjm-scan):
9478  */
9479  sjm->scan_cost.reset();
9480  if (distinct_rowcount > 0.0)
9481  sjm->scan_cost.add_io(distinct_rowcount * row_cost);
9482 
9483  sjm->lookup_cost.reset();
9484  sjm->lookup_cost.add_io(row_cost);
9485 }
9486 
9487 
9497 {
9498  DBUG_ASSERT(unit->item);
9499 
9500  switch (unit->item->substype())
9501  {
9502  case Item_subselect::IN_SUBS:
9503  case Item_subselect::ALL_SUBS:
9504  case Item_subselect::ANY_SUBS:
9505  // All of those are children of Item_in_subselect and may use EXISTS
9506  break;
9507  default:
9508  return false;
9509  }
9510 
9511  Item_in_subselect * const in_pred=
9512  static_cast<Item_in_subselect *>(unit->item);
9513 
9514  Item_exists_subselect::enum_exec_method chosen_method= in_pred->exec_method;
9515  // Materialization does not allow UNION so this can't happen:
9516  DBUG_ASSERT(chosen_method != Item_exists_subselect::EXEC_MATERIALIZATION);
9517 
9518  if ((chosen_method == Item_exists_subselect::EXEC_EXISTS_OR_MAT) &&
9519  compare_costs_of_subquery_strategies(&chosen_method))
9520  return true;
9521 
9522  switch (chosen_method)
9523  {
9525  return in_pred->finalize_exists_transform(select_lex);
9527  return in_pred->finalize_materialization_transform(this);
9528  default:
9529  DBUG_ASSERT(false);
9530  return true;
9531  }
9532 }
9533 
9534 
9555 bool JOIN::compare_costs_of_subquery_strategies(
9557 {
9559 
9560  if (!thd->optimizer_switch_flag(OPTIMIZER_SWITCH_MATERIALIZATION))
9561  return false;
9562 
9563  const JOIN *parent_join= unit->outer_select()->join;
9564  if (!parent_join || !parent_join->child_subquery_can_materialize)
9565  return false;
9566 
9567  Item_in_subselect * const in_pred=
9568  static_cast<Item_in_subselect *>(unit->item);
9569 
9570  /*
9571  Testing subquery_allows_etc() at each optimization is necessary as each
9572  execution of a prepared statement may use a different type of parameter.
9573  */
9574  if (!subquery_allows_materialization(in_pred, thd, select_lex,
9575  select_lex->outer_select()))
9576  return false;
9577 
9578  Opt_trace_context * const trace= &thd->opt_trace;
9579  Opt_trace_object trace_wrapper(trace);
9581  trace_subqmat(trace, "execution_plan_for_potential_materialization");
9582  const double saved_best_read= best_read;
9583  const ha_rows saved_best_rowcount= best_rowcount;
9584  POSITION * const saved_best_pos= best_positions;
9585 
9586  if (in_pred->in2exists_added_to_where())
9587  {
9588  Opt_trace_array trace_subqmat_steps(trace, "steps");
9589 
9590  // Up to one extra slot per semi-join nest is needed (if materialized)
9591  const uint sj_nests= select_lex->sj_nests.elements;
9592 
9593  if (!(best_positions= new (thd->mem_root) POSITION[tables + sj_nests + 1]))
9594  return true;
9595 
9596  // Compute plans which do not use outer references
9597 
9598  DBUG_ASSERT(allow_outer_refs);
9599  allow_outer_refs= false;
9600 
9601  if (optimize_semijoin_nests_for_materialization(this))
9602  return true;
9603 
9604  if (Optimize_table_order(thd, this, NULL).choose_table_order())
9605  return true;
9606  }
9607  else
9608  {
9609  /*
9610  If IN->EXISTS didn't add any condition to WHERE (only to HAVING, which
9611  can happen if subquery has aggregates) then the plan for materialization
9612  will be the same as for EXISTS - don't compute it again.
9613  */
9614  trace_subqmat.add("surely_same_plan_as_EXISTS", true).
9615  add_alnum("cause", "EXISTS_did_not_change_WHERE");
9616  }
9617 
9619  calculate_materialization_costs(this, NULL, primary_tables, &sjm);
9620 
9621  /*
9622  The number of evaluations of the subquery influences costs, we need to
9623  compute it.
9624  */
9625  Opt_trace_object trace_subq_mat_decision(trace, "subq_mat_decision");
9626  Opt_trace_array trace_parents(trace, "parent_fanouts");
9627  const Item_subselect *subs= in_pred;
9628  double subq_executions= 1.0;
9629  for(;;)
9630  {
9631  Opt_trace_object trace_parent(trace);
9632  trace_parent.add_select_number(parent_join->select_lex->select_number);
9633  double parent_fanout;
9634  if (// safety, not sure needed
9635  parent_join->plan_is_const() ||
9636  // if subq is in condition on constant table:
9637  !parent_join->child_subquery_can_materialize)
9638  {
9639  parent_fanout= 1.0;
9640  trace_parent.add("subq_attached_to_const_table", true);
9641  }
9642  else
9643  {
9644  if (subs->in_cond_of_tab != INT_MIN)
9645  {
9646  /*
9647  Subquery is attached to a certain 'pos', pos[-1].prefix_record_count
9648  is the number of times we'll start a loop accessing 'pos'; each such
9649  loop will read pos->records_read records of 'pos', so subquery will
9650  be evaluated pos[-1].prefix_record_count * pos->records_read times.
9651  Exceptions:
9652  - if 'pos' is first, use 1 instead of pos[-1].prefix_record_count
9653  - if 'pos' is first of a sjerialization-mat nest, same.
9654 
9655  If in a sj-materialization nest, pos->records_read and
9656  pos[-1].prefix_record_count are of the "nest materialization" plan
9657  (copied back in fix_semijoin_strategies()), which is
9658  appropriate as it corresponds to evaluations of our subquery.
9659  */
9660  const uint idx= subs->in_cond_of_tab;
9661  DBUG_ASSERT((int)idx >= 0 && idx < parent_join->tables);
9662  trace_parent.add("subq_attached_to_table", true);
9663  trace_parent.add_utf8_table(parent_join->join_tab[idx].table);
9664  parent_fanout= parent_join->join_tab[idx].position->records_read;
9665  if ((idx > parent_join->const_tables) &&
9666  !sj_is_materialize_strategy(parent_join
9667  ->join_tab[idx].position->sj_strategy))
9668  parent_fanout*=
9669  parent_join->join_tab[idx - 1].position->prefix_record_count;
9670  }
9671  else
9672  {
9673  /*
9674  Subquery is SELECT list, GROUP BY, ORDER BY, HAVING: it is evaluated
9675  at the end of the parent join's execution.
9676  It can be evaluated once per row-before-grouping:
9677  SELECT SUM(t1.col IN (subq)) FROM t1 GROUP BY expr;
9678  or once per row-after-grouping:
9679  SELECT SUM(t1.col) AS s FROM t1 GROUP BY expr HAVING s IN (subq),
9680  SELECT SUM(t1.col) IN (subq) FROM t1 GROUP BY expr
9681  It's hard to tell. We simply assume 'once per
9682  row-before-grouping'.
9683 
9684  Another approximation:
9685  SELECT ... HAVING x IN (subq) LIMIT 1
9686  best_rowcount=1 due to LIMIT, though HAVING (and thus the subquery)
9687  may be evaluated many times before HAVING becomes true and the limit
9688  is reached.
9689  */
9690  trace_parent.add("subq_attached_to_join_result", true);
9691  parent_fanout= parent_join->best_rowcount;
9692  }
9693  }
9694  subq_executions*= parent_fanout;
9695  trace_parent.add("fanout", parent_fanout);
9696  const bool cacheable= parent_join->select_lex->is_cacheable();
9697  trace_parent.add("cacheable", cacheable);
9698  if (cacheable)
9699  {
9700  // Parent executed only once
9701  break;
9702  }
9703  /*
9704  Parent query is executed once per outer row => go up to find number of
9705  outer rows. Example:
9706  SELECT ... IN(subq-with-in2exists WHERE ... IN (subq-with-mat))
9707  */
9708  if (!(subs= parent_join->unit->item))
9709  {
9710  // derived table, materialized only once
9711  break;
9712  }
9713  parent_join= parent_join->unit->outer_select()->join;
9714  if (!parent_join)
9715  {
9716  /*
9717  May be single-table UPDATE/DELETE, has no join.
9718  @todo we should find how many rows it plans to UPDATE/DELETE, taking
9719  inspiration in Explain_table::explain_rows_and_filtered().
9720  This is not a priority as it applies only to
9721  UPDATE - child(non-mat-subq) - grandchild(may-be-mat-subq).
9722  And it will autosolve the day UPDATE gets a JOIN.
9723  */
9724  break;
9725  }
9726  } // for(;;)
9727  trace_parents.end();
9728 
9729  const double cost_exists= subq_executions * saved_best_read;
9730  const double cost_mat_table= sjm.materialization_cost.total_cost();
9731  const double cost_mat= cost_mat_table + subq_executions *
9732  sjm.lookup_cost.total_cost();
9733  const bool mat_chosen=
9734  thd->optimizer_switch_flag(OPTIMIZER_SWITCH_SUBQ_MAT_COST_BASED) ?
9735  (cost_mat < cost_exists) : true;
9736  trace_subq_mat_decision
9737  .add("cost_to_create_and_fill_materialized_table",
9738  cost_mat_table)
9739  .add("cost_of_one_EXISTS", saved_best_read)
9740  .add("number_of_subquery_evaluations", subq_executions)
9741  .add("cost_of_materialization", cost_mat)
9742  .add("cost_of_EXISTS", cost_exists)
9743  .add("chosen", mat_chosen);
9744  if (mat_chosen)
9746  else
9747  {
9748  best_read= saved_best_read;
9749  best_rowcount= saved_best_rowcount;
9750  best_positions= saved_best_pos;
9751  /*
9752  Don't restore JOIN::positions or best_ref, they're not used
9753  afterwards. best_positions is (like: by get_sj_strategy()).
9754  */
9755  }
9756  return false;
9757 }
9758 
9759 
9765 {
9766  // If plan is const, 0 or 1 rows should be returned
9767  DBUG_ASSERT(!plan_is_const() || best_rowcount <= 1);
9768 
9769  if (plan_is_const())
9770  return;
9771 
9772  /*
9773  If a derived table, or a member of a UNION which itself forms a derived
9774  table:
9775  setting estimate to 0 or 1 row would mark the derived table as const.
9776  The row count is bumped to the nearest higher value, so that the
9777  query block will not be evaluated during optimization.
9778  */
9779  if (best_rowcount <= 1 &&
9780  select_lex->master_unit()->first_select()->linkage ==
9781  DERIVED_TABLE_TYPE)
9782  best_rowcount= 2;
9783 
9784  /*
9785  There will be no more rows than defined in the LIMIT clause. Use it
9786  as an estimate. If LIMIT 1 is specified, the query block will be
9787  considered "const", with actual row count 0 or 1.
9788  */
9789  set_if_smaller(best_rowcount, unit->select_limit_cnt);
9790 }
9791