Drizzled Public API Documentation

sql_select.cc
Go to the documentation of this file.
1 /* Copyright (C) 2000-2006 MySQL AB
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 
25 #include <config.h>
26 
27 #include <string>
28 #include <iostream>
29 #include <algorithm>
30 #include <vector>
31 
32 #include <drizzled/sql_select.h> /* include join.h */
33 
34 #include <drizzled/error.h>
35 #include <drizzled/gettext.h>
36 #include <drizzled/util/test.h>
37 #include <drizzled/name_resolution_context_state.h>
38 #include <drizzled/nested_join.h>
39 #include <drizzled/probes.h>
40 #include <drizzled/show.h>
41 #include <drizzled/item/cache.h>
42 #include <drizzled/item/cmpfunc.h>
43 #include <drizzled/item/copy_string.h>
44 #include <drizzled/item/uint.h>
45 #include <drizzled/cached_item.h>
46 #include <drizzled/sql_base.h>
47 #include <drizzled/field/blob.h>
48 #include <drizzled/check_stack_overrun.h>
49 #include <drizzled/lock.h>
50 #include <drizzled/item/outer_ref.h>
51 #include <drizzled/index_hint.h>
52 #include <drizzled/records.h>
53 #include <drizzled/internal/iocache.h>
54 #include <drizzled/drizzled.h>
55 #include <drizzled/plugin/storage_engine.h>
56 #include <drizzled/sql_union.h>
57 #include <drizzled/optimizer/key_field.h>
58 #include <drizzled/optimizer/position.h>
59 #include <drizzled/optimizer/sargable_param.h>
60 #include <drizzled/optimizer/key_use.h>
61 #include <drizzled/optimizer/range.h>
62 #include <drizzled/optimizer/quick_range_select.h>
63 #include <drizzled/optimizer/quick_ror_intersect_select.h>
64 #include <drizzled/filesort.h>
65 #include <drizzled/sql_lex.h>
66 #include <drizzled/session.h>
67 #include <drizzled/sort_field.h>
68 #include <drizzled/select_result.h>
69 #include <drizzled/key.h>
70 #include <drizzled/my_hash.h>
71 
72 using namespace std;
73 
74 namespace drizzled {
75 
76 static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
77 static COND *build_equal_items(Session *session, COND *cond,
78  COND_EQUAL *inherited,
79  List<TableList> *join_list,
80  COND_EQUAL **cond_equal_ref);
81 
82 static Item* part_of_refkey(Table *form,Field *field);
83 static bool cmp_buffer_with_ref(JoinTable *tab);
84 static void change_cond_ref_to_const(Session *session,
85  list<COND_CMP>& save_list,
86  Item *and_father,
87  Item *cond,
88  Item *field,
89  Item *value);
90 static void copy_blobs(Field **ptr);
91 
92 static bool eval_const_cond(COND *cond)
93 {
94  return ((Item_func*) cond)->val_int() ? true : false;
95 }
96 
97 /*
98  This is used to mark equalities that were made from i-th IN-equality.
99  We limit semi-join InsideOut optimization to handling max 64 inequalities,
100  The following variable occupies 64 addresses.
101 */
102 const char *subq_sj_cond_name= "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
103 
104 static void copy_blobs(Field **ptr)
105 {
106  for (; *ptr ; ptr++)
107  {
108  if ((*ptr)->flags & BLOB_FLAG)
109  {
110  ((Field_blob *) (*ptr))->copy();
111  }
112  }
113 }
114 
118 bool handle_select(Session *session, LEX *lex, select_result *result,
119  uint64_t setup_tables_done_option)
120 {
121  bool res;
122  Select_Lex *select_lex= &lex->select_lex;
123  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
124 
125  if (select_lex->master_unit()->is_union() or
126  select_lex->master_unit()->fake_select_lex)
127  {
128  res= drizzle_union(session, lex, result, &lex->unit,
129  setup_tables_done_option);
130  }
131  else
132  {
133  Select_Lex_Unit *unit= &lex->unit;
134  unit->set_limit(unit->global_parameters);
135  session->session_marker= 0;
136 
137  /*
138  'options' of select_query will be set in JOIN, as far as JOIN for
139  every PS/SP execution new, we will not need reset this flag if
140  setup_tables_done_option changed for next rexecution
141  */
142  res= select_query(session,
143  &select_lex->ref_pointer_array,
144  (TableList*) select_lex->table_list.first,
145  select_lex->with_wild,
146  select_lex->item_list,
147  select_lex->where,
148  select_lex->order_list.size() +
149  select_lex->group_list.size(),
150  (Order*) select_lex->order_list.first,
151  (Order*) select_lex->group_list.first,
152  select_lex->having,
153  select_lex->options | session->options |
154  setup_tables_done_option,
155  result, unit, select_lex);
156  }
157  res|= session->is_error();
158 
159  if (unlikely(res))
160  {
161  result->abort();
162  }
163 
164  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
165  return res;
166 }
167 
168 /*
169  Fix fields referenced from inner selects.
170 
171  SYNOPSIS
172  fix_inner_refs()
173  session Thread handle
174  all_fields List of all fields used in select
175  select Current select
176  ref_pointer_array Array of references to Items used in current select
177 
178  DESCRIPTION
179  The function serves 3 purposes - adds fields referenced from inner
180  selects to the current select list, resolves which class to use
181  to access referenced item (Item_ref of Item_direct_ref) and fixes
182  references (Item_ref objects) to these fields.
183 
184  If a field isn't already in the select list and the ref_pointer_array
185  is provided then it is added to the all_fields list and the pointer to
186  it is saved in the ref_pointer_array.
187 
188  The class to access the outer field is determined by the following rules:
189  1. If the outer field isn't used under an aggregate function
190  then the Item_ref class should be used.
191  2. If the outer field is used under an aggregate function and this
192  function is aggregated in the select where the outer field was
193  resolved or in some more inner select then the Item_direct_ref
194  class should be used.
195  The resolution is done here and not at the fix_fields() stage as
196  it can be done only after sum functions are fixed and pulled up to
197  selects where they are have to be aggregated.
198  When the class is chosen it substitutes the original field in the
199  Item_outer_ref object.
200 
201  After this we proceed with fixing references (Item_outer_ref objects) to
202  this field from inner subqueries.
203 
204  RETURN
205  true an error occured
206  false ok
207 */
208 bool fix_inner_refs(Session *session,
209  List<Item> &all_fields,
210  Select_Lex *select,
211  Item **ref_pointer_array)
212 {
213  Item_outer_ref *ref;
214  bool res= false;
215  bool direct_ref= false;
216 
217  List<Item_outer_ref>::iterator ref_it(select->inner_refs_list.begin());
218  while ((ref= ref_it++))
219  {
220  Item *item= ref->outer_ref;
221  Item **item_ref= ref->ref;
222  Item_ref *new_ref;
223 
224  /*
225  @todo this field item already might be present in the select list.
226  In this case instead of adding new field item we could use an
227  existing one. The change will lead to less operations for copying fields,
228  smaller temporary tables and less data passed through filesort.
229  */
230  if (ref_pointer_array && !ref->found_in_select_list)
231  {
232  int el= all_fields.size();
233  ref_pointer_array[el]= item;
234  /* Add the field item to the select list of the current select. */
235  all_fields.push_front(item);
236  /*
237  If it's needed reset each Item_ref item that refers this field with
238  a new reference taken from ref_pointer_array.
239  */
240  item_ref= ref_pointer_array + el;
241  }
242 
243  if (ref->in_sum_func)
244  {
245  Item_sum *sum_func;
246  if (ref->in_sum_func->nest_level > select->nest_level)
247  {
248  direct_ref= true;
249  }
250  else
251  {
252  for (sum_func= ref->in_sum_func; sum_func &&
253  sum_func->aggr_level >= select->nest_level;
254  sum_func= sum_func->in_sum_func)
255  {
256  if (sum_func->aggr_level == select->nest_level)
257  {
258  direct_ref= true;
259  break;
260  }
261  }
262  }
263  }
264 
265  new_ref= direct_ref ?
266  new Item_direct_ref(ref->context, item_ref, ref->table_name,
267  ref->field_name, ref->alias_name_used) :
268  new Item_ref(ref->context, item_ref, ref->table_name,
269  ref->field_name, ref->alias_name_used);
270 
271  ref->outer_ref= new_ref;
272  ref->ref= &ref->outer_ref;
273 
274  if (!ref->fixed && ref->fix_fields(session, 0))
275  {
276  return true;
277  }
278  session->used_tables|= item->used_tables();
279  }
280  return res;
281 }
282 
283 /*****************************************************************************
284  Check fields, find best join, do the select and output fields.
285  select_query assumes that all tables are already opened
286 *****************************************************************************/
287 
288 /*
289  Index lookup-based subquery: save some flags for EXPLAIN output
290 
291  SYNOPSIS
292  save_index_subquery_explain_info()
293  join_tab Subquery's join tab (there is only one as index lookup is
294  only used for subqueries that are single-table SELECTs)
295  where Subquery's WHERE clause
296 
297  DESCRIPTION
298  For index lookup-based subquery (i.e. one executed with
299  subselect_uniquesubquery_engine or subselect_indexsubquery_engine),
300  check its EXPLAIN output row should contain
301  "Using index" (TAB_INFO_FULL_SCAN_ON_NULL)
302  "Using Where" (TAB_INFO_USING_WHERE)
303  "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
304  and set appropriate flags in join_tab->packed_info.
305 */
306 void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
307 {
308  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
309 
310  if (join_tab->table->covering_keys.test(join_tab->ref.key))
311  join_tab->packed_info |= TAB_INFO_USING_INDEX;
312 
313  if (where)
314  join_tab->packed_info |= TAB_INFO_USING_WHERE;
315 
316  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
317  {
318  if (join_tab->ref.cond_guards[i])
319  {
320  join_tab->packed_info |= TAB_INFO_FULL_SCAN_ON_NULL;
321  break;
322  }
323  }
324 }
325 
368 bool select_query(Session *session,
369  Item ***rref_pointer_array,
370  TableList *tables,
371  uint32_t wild_num,
372  List<Item> &fields,
373  COND *conds,
374  uint32_t og_num,
375  Order *order,
376  Order *group,
377  Item *having,
378  uint64_t select_options,
379  select_result *result,
380  Select_Lex_Unit *unit,
381  Select_Lex *select_lex)
382 {
383  bool err;
384  bool free_join= 1;
385 
386  select_lex->context.resolve_in_select_list= true;
387  Join *join;
388  if (select_lex->join != 0)
389  {
390  join= select_lex->join;
391  /*
392  is it single SELECT in derived table, called in derived table
393  creation
394  */
395  if (select_lex->linkage != DERIVED_TABLE_TYPE ||
396  (select_options & SELECT_DESCRIBE))
397  {
398  if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
399  {
400  //here is EXPLAIN of subselect or derived table
401  if (join->change_result(result))
402  {
403  return true;
404  }
405  }
406  else
407  {
408  if ((err= join->prepare(rref_pointer_array, tables, wild_num,
409  conds, og_num, order, group, having, select_lex, unit)))
410  {
411  goto err;
412  }
413  }
414  }
415  free_join= 0;
416  join->select_options= select_options;
417  }
418  else
419  {
420  join= new Join(session, fields, select_options, result);
421  session->set_proc_info("init");
422  session->used_tables=0; // Updated by setup_fields
423  if ((err= join->prepare(rref_pointer_array, tables, wild_num, conds, og_num, order, group, having, select_lex, unit)))
424  {
425  goto err;
426  }
427  }
428 
429  err= join->optimize();
430  if (err)
431  {
432  goto err; // 1
433  }
434 
435  if (session->lex().describe & DESCRIBE_EXTENDED)
436  {
437  join->conds_history= join->conds;
438  join->having_history= (join->having?join->having:join->tmp_having);
439  }
440 
441  if (session->is_error())
442  {
443  goto err;
444  }
445 
446  join->exec();
447 
448  if (session->lex().describe & DESCRIBE_EXTENDED)
449  {
450  select_lex->where= join->conds_history;
451  select_lex->having= join->having_history;
452  }
453 
454 err:
455  if (free_join)
456  {
457  session->set_proc_info("end");
458  err|= select_lex->cleanup();
459  return(err || session->is_error());
460  }
461  return(join->error);
462 }
463 
464 inline Item *and_items(Item* cond, Item *item)
465 {
466  return (cond? (new Item_cond_and(cond, item)) : item);
467 }
468 
469 /*****************************************************************************
470  Create JoinTableS, make a guess about the table types,
471  Approximate how many records will be used in each table
472 *****************************************************************************/
473 ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
474 {
475  int error;
476  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
477  {
478  return 0; // Fatal error flag is set
479  }
480 
481  if (select)
482  {
483  select->head=table;
484  table->reginfo.impossible_range=0;
485  if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
486  limit, 0, false)) == 1)
487  {
488  return(select->quick->records);
489  }
490 
491  if (error == -1)
492  {
493  table->reginfo.impossible_range=1;
494  return 0;
495  }
496  }
497 
498  return(HA_POS_ERROR); /* This shouldn't happend */
499 }
500 
501 /*****************************************************************************
502  Check with keys are used and with tables references with tables
503  Updates in stat:
504  keys Bitmap of all used keys
505  const_keys Bitmap of all keys with may be used with quick_select
506  keyuse Pointer to possible keys
507 *****************************************************************************/
508 
509 
515 uint32_t max_part_bit(key_part_map bits)
516 {
517  uint32_t found;
518  for (found=0; bits & 1 ; found++,bits>>=1) ;
519 
520  return found;
521 }
522 
523 static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
524 {
525  int res;
526  if (a->getTable()->tablenr != b->getTable()->tablenr)
527  return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
528 
529  if (a->getKey() != b->getKey())
530  return static_cast<int>((a->getKey() - b->getKey()));
531 
532  if (a->getKeypart() != b->getKeypart())
533  return static_cast<int>((a->getKeypart() - b->getKeypart()));
534 
535  // Place const values before other ones
536  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
537  test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
538  return res;
539 
540  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
541  return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
542  (b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
543 }
544 
545 
567  DYNAMIC_ARRAY *keyuse,
568  JoinTable *join_tab,
569  uint32_t tables,
570  COND *cond,
571  COND_EQUAL *,
572  table_map normal_tables,
573  Select_Lex *select_lex,
574  vector<optimizer::SargableParam> &sargables)
575 {
576  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
577 
578  /*
579  All predicates that are used to fill arrays of KeyField
580  and SargableParam classes have at most 2 arguments
581  except BETWEEN predicates that have 3 arguments and
582  IN predicates.
583  This any predicate if it's not BETWEEN/IN can be used
584  directly to fill at most 2 array elements, either of KeyField
585  or SargableParam type. For a BETWEEN predicate 3 elements
586  can be filled as this predicate is considered as
587  saragable with respect to each of its argument.
588  An IN predicate can require at most 1 element as currently
589  it is considered as sargable only for its first argument.
590  Multiple equality can add elements that are filled after
591  substitution of field arguments by equal fields. There
592  can be not more than select_lex->max_equal_elems such
593  substitutions.
594  */
595  optimizer::KeyField* key_fields= new (session->mem) optimizer::KeyField[((session->lex().current_select->cond_count+1)*2 + session->lex().current_select->between_count)*m+1];
596  uint and_level= 0;
597  optimizer::KeyField* end, *field;
598  field= end= key_fields;
599 
600  keyuse->init(sizeof(optimizer::KeyUse), 20, 64);
601  if (cond)
602  {
603  add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
604  sargables);
605  for (; field != end; field++)
606  {
607  add_key_part(keyuse, field);
608  /* Mark that we can optimize LEFT JOIN */
609  if (field->getValue()->type() == Item::NULL_ITEM &&
610  ! field->getField()->real_maybe_null())
611  {
612  field->getField()->getTable()->reginfo.not_exists_optimize= 1;
613  }
614  }
615  }
616  for (uint32_t i= 0; i < tables; i++)
617  {
618  /*
619  Block the creation of keys for inner tables of outer joins.
620  Here only the outer joins that can not be converted to
621  inner joins are left and all nests that can be eliminated
622  are flattened.
623  In the future when we introduce conditional accesses
624  for inner tables in outer joins these keys will be taken
625  into account as well.
626  */
627  if (*join_tab[i].on_expr_ref)
628  add_key_fields(join_tab->join, &end, &and_level,
629  *join_tab[i].on_expr_ref,
630  join_tab[i].table->map, sargables);
631  }
632 
633  /* Process ON conditions for the nested joins */
634  {
635  List<TableList>::iterator li(join_tab->join->join_list->begin());
636  TableList *table;
637  while ((table= li++))
638  {
639  if (table->getNestedJoin())
640  add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
641  sargables);
642  }
643  }
644 
645  /* fill keyuse with found key parts */
646  for ( ; field != end ; field++)
647  add_key_part(keyuse,field);
648 
649  /*
650  Sort the array of possible keys and remove the following key parts:
651  - ref if there is a keypart which is a ref and a const.
652  (e.g. if there is a key(a,b) and the clause is a=3 and b=7 and b=t2.d,
653  then we skip the key part corresponding to b=t2.d)
654  - keyparts without previous keyparts
655  (e.g. if there is a key(a,b,c) but only b < 5 (or a=2 and c < 3) is
656  used in the query, we drop the partial key parts from consideration).
657  */
658  if (keyuse->size())
659  {
660  optimizer::KeyUse key_end,*prev,*save_pos,*use;
661 
662  internal::my_qsort(keyuse->buffer,keyuse->size(),sizeof(optimizer::KeyUse),
663  (qsort_cmp) sort_keyuse);
664 
665  memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
666  keyuse->push_back(&key_end);
667 
668  use= save_pos= (optimizer::KeyUse*)keyuse->buffer;
669  prev= &key_end;
670  uint found_eq_constant= 0;
671  {
672  for (uint32_t i= 0; i < keyuse->size()-1; i++, use++)
673  {
674  if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
675  use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
676 
677  if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
678  {
679  if (prev->getKeypart() + 1 < use->getKeypart() ||
680  ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
681  {
682  continue; /* remove */
683  }
684  }
685  else if (use->getKeypart() != 0) // First found must be 0
686  {
687  continue;
688  }
689 
690 #ifdef HAVE_VALGRIND
691  /* Valgrind complains about overlapped memcpy when save_pos==use. */
692  if (save_pos != use)
693 #endif
694  *save_pos= *use;
695  prev=use;
696  found_eq_constant= ! use->getUsedTables();
697  /* Save ptr to first use */
698  if (! use->getTable()->reginfo.join_tab->keyuse)
699  {
700  use->getTable()->reginfo.join_tab->keyuse= save_pos;
701  }
702  use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
703  save_pos++;
704  }
705 
706  uint32_t i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
707  reinterpret_cast<optimizer::KeyUse*>(keyuse->buffer)[i] = key_end;
708  keyuse->set_size(i);
709  }
710  }
711 }
712 
716 void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
717 {
718  optimizer::KeyUse* keyuse= (optimizer::KeyUse*)keyuse_array->buffer;
719  for (optimizer::KeyUse* end= keyuse+ keyuse_array->size() ; keyuse < end ; keyuse++)
720  {
721  table_map map;
722  /*
723  If we find a ref, assume this table matches a proportional
724  part of this table.
725  For example 100 records matching a table with 5000 records
726  gives 5000/100 = 50 records per key
727  Constant tables are ignored.
728  To avoid bad matches, we don't make ref_table_rows less than 100.
729  */
730  keyuse->setTableRows(~(ha_rows) 0); // If no ref
731 
732  if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
733  {
734  uint32_t tablenr;
735  for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
736  if (map == 1) // Only one table
737  {
738  Table *tmp_table=join->all_tables[tablenr];
739  keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
740  }
741  }
742 
743  /*
744  Outer reference (external field) is constant for single executing
745  of subquery
746  */
747  if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
748  {
749  keyuse->setTableRows(1);
750  }
751  }
752 }
753 
754 
773 {
774  List<Item_field> indexed_fields;
775  List<Item_field>::iterator indexed_fields_it(indexed_fields.begin());
776  Order *cur_group;
777  Item_field *cur_item;
778  key_map possible_keys(0);
779 
780  if (join->group_list)
781  { /* Collect all query fields referenced in the GROUP clause. */
782  for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
783  {
784  (*cur_group->item)->walk(&Item::collect_item_field_processor, 0, (unsigned char*) &indexed_fields);
785  }
786  }
787  else if (join->select_distinct)
788  { /* Collect all query fields referenced in the SELECT clause. */
789  List<Item> &select_items= join->fields_list;
790  List<Item>::iterator select_items_it(select_items.begin());
791  Item *item;
792  while ((item= select_items_it++))
793  {
794  item->walk(&Item::collect_item_field_processor, 0, (unsigned char*) &indexed_fields);
795  }
796  }
797  else
798  {
799  return;
800  }
801 
802  if (indexed_fields.size() == 0)
803  {
804  return;
805  }
806 
807  /* Intersect the keys of all group fields. */
808  cur_item= indexed_fields_it++;
809  possible_keys|= cur_item->field->part_of_key;
810  while ((cur_item= indexed_fields_it++))
811  {
812  possible_keys&= cur_item->field->part_of_key;
813  }
814 
815  if (possible_keys.any())
816  join_tab->const_keys|= possible_keys;
817 }
818 
843 int join_tab_cmp(const void* ptr1, const void* ptr2)
844 {
845  JoinTable *jt1= *(JoinTable**) ptr1;
846  JoinTable *jt2= *(JoinTable**) ptr2;
847 
848  if (jt1->dependent & jt2->table->map)
849  return 1;
850 
851  if (jt2->dependent & jt1->table->map)
852  return -1;
853 
854  if (jt1->found_records > jt2->found_records)
855  return 1;
856 
857  if (jt1->found_records < jt2->found_records)
858  return -1;
859 
860  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
861 }
862 
866 int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
867 {
868  JoinTable *jt1= *(JoinTable**) ptr1;
869  JoinTable *jt2= *(JoinTable**) ptr2;
870 
871  if (jt1->dependent & jt2->table->map)
872  return 1;
873 
874  if (jt2->dependent & jt1->table->map)
875  return -1;
876 
877  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
878 }
879 
884 {
885  uint32_t null_fields,blobs,fields,rec_length;
886  Field **f_ptr,*field;
887 
888  null_fields= blobs= fields= rec_length=0;
889  for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
890  {
891  if (field->isReadSet())
892  {
893  uint32_t flags=field->flags;
894  fields++;
895  rec_length+=field->pack_length();
896 
897  if (flags & BLOB_FLAG)
898  {
899  blobs++;
900  }
901 
902  if (!(flags & NOT_NULL_FLAG))
903  {
904  null_fields++;
905  }
906  }
907  }
908 
909  if (null_fields)
910  {
911  rec_length+=(join_tab->table->getNullFields() + 7)/8;
912  }
913 
914  if (join_tab->table->maybe_null)
915  {
916  rec_length+=sizeof(bool);
917  }
918 
919  if (blobs)
920  {
921  uint32_t blob_length=(uint32_t) (join_tab->table->cursor->stats.mean_rec_length-
922  (join_tab->table->getRecordLength()- rec_length));
923  rec_length+= max((uint32_t)4,blob_length);
924  }
925  join_tab->used_fields= fields;
926  join_tab->used_fieldlength= rec_length;
927  join_tab->used_blobs= blobs;
928 }
929 
930 StoredKey *get_store_key(Session *session,
931  optimizer::KeyUse *keyuse,
932  table_map used_tables,
933  KeyPartInfo *key_part,
934  unsigned char *key_buff,
935  uint32_t maybe_null)
936 {
937  Item_ref *key_use_val= static_cast<Item_ref *>(keyuse->getVal());
938  if (! ((~used_tables) & keyuse->getUsedTables())) // if const item
939  {
940  return new store_key_const_item(session,
941  key_part->field,
942  key_buff + maybe_null,
943  maybe_null ? key_buff : 0,
944  key_part->length,
945  key_use_val);
946  }
947  else if (key_use_val->type() == Item::FIELD_ITEM ||
948  (key_use_val->type() == Item::REF_ITEM &&
949  key_use_val->ref_type() == Item_ref::OUTER_REF &&
950  (*(Item_ref**)((Item_ref*)key_use_val)->ref)->ref_type() == Item_ref::DIRECT_REF &&
951  key_use_val->real_item()->type() == Item::FIELD_ITEM))
952  {
953  return new store_key_field(session,
954  key_part->field,
955  key_buff + maybe_null,
956  maybe_null ? key_buff : 0,
957  key_part->length,
958  ((Item_field*) key_use_val->real_item())->field,
959  key_use_val->full_name());
960  }
961  return new store_key_item(session,
962  key_part->field,
963  key_buff + maybe_null,
964  maybe_null ? key_buff : 0,
965  key_part->length,
966  key_use_val);
967 }
968 
975 bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
976 {
977  bool error;
978  Table *table= field->getTable();
979  Session *session= table->in_use;
980  ha_rows cuted_fields=session->cuted_fields;
981 
982  /*
983  we should restore old value of count_cuted_fields because
984  store_val_in_field can be called from insert_query
985  with select_insert, which make count_cuted_fields= 1
986  */
987  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
988  session->count_cuted_fields= check_flag;
989  error= item->save_in_field(field, 1);
990  session->count_cuted_fields= old_count_cuted_fields;
991  return error || cuted_fields != session->cuted_fields;
992 }
993 
994 inline void add_cond_and_fix(Item **e1, Item *e2)
995 {
996  if (*e1)
997  {
998  Item* res= new Item_cond_and(*e1, e2);
999  *e1= res;
1000  res->quick_fix_field();
1001  }
1002  else
1003  {
1004  *e1= e2;
1005  }
1006 }
1007 
1008 bool create_ref_for_key(Join *join,
1009  JoinTable *j,
1010  optimizer::KeyUse *org_keyuse,
1011  table_map used_tables)
1012 {
1013  optimizer::KeyUse *keyuse= org_keyuse;
1014  Session *session= join->session;
1015  uint32_t keyparts;
1016  uint32_t length;
1017  uint32_t key;
1018  Table *table= NULL;
1019  KeyInfo *keyinfo= NULL;
1020 
1021  /* Use best key from find_best */
1022  table= j->table;
1023  key= keyuse->getKey();
1024  keyinfo= table->key_info + key;
1025 
1026  {
1027  keyparts= length= 0;
1028  uint32_t found_part_ref_or_null= 0;
1029  /*
1030  Calculate length for the used key
1031  Stop if there is a missing key part or when we find second key_part
1032  with KEY_OPTIMIZE_REF_OR_NULL
1033  */
1034  do
1035  {
1036  if (! (~used_tables & keyuse->getUsedTables()))
1037  {
1038  if (keyparts == keyuse->getKeypart() &&
1039  ! (found_part_ref_or_null & keyuse->getOptimizeFlags()))
1040  {
1041  keyparts++;
1042  length+= keyinfo->key_part[keyuse->getKeypart()].store_length;
1043  found_part_ref_or_null|= keyuse->getOptimizeFlags();
1044  }
1045  }
1046  keyuse++;
1047  } while (keyuse->getTable() == table && keyuse->getKey() == key);
1048  }
1049 
1050  /* set up fieldref */
1051  keyinfo=table->key_info+key;
1052  j->ref.key_parts=keyparts;
1053  j->ref.key_length=length;
1054  j->ref.key=(int) key;
1055  j->ref.key_buff= (unsigned char*) session->mem.calloc(ALIGN_SIZE(length)*2);
1056  j->ref.key_copy= new (session->mem) StoredKey*[keyparts + 1];
1057  j->ref.items= new (session->mem) Item*[keyparts];
1058  j->ref.cond_guards= new (session->mem) bool*[keyparts];
1059  j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length);
1060  j->ref.key_err=1;
1061  j->ref.null_rejecting= 0;
1062  j->ref.disable_cache= false;
1063  keyuse=org_keyuse;
1064 
1065  StoredKey **ref_key= j->ref.key_copy;
1066  unsigned char *key_buff= j->ref.key_buff, *null_ref_key= 0;
1067  bool keyuse_uses_no_tables= true;
1068  {
1069  for (uint32_t i= 0; i < keyparts; keyuse++, i++)
1070  {
1071  while (keyuse->getKeypart() != i or ((~used_tables) & keyuse->getUsedTables()))
1072  {
1073  keyuse++; /* Skip other parts */
1074  }
1075 
1076  uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
1077  j->ref.items[i]= keyuse->getVal(); // Save for cond removal
1078  j->ref.cond_guards[i]= keyuse->getConditionalGuard();
1079  if (keyuse->isNullRejected())
1080  {
1081  j->ref.null_rejecting |= 1 << i;
1082  }
1083 
1084  keyuse_uses_no_tables= keyuse_uses_no_tables && ! keyuse->getUsedTables();
1085  if (! keyuse->getUsedTables() && !(join->select_options & SELECT_DESCRIBE))
1086  { // Compare against constant
1087  store_key_item tmp(session, keyinfo->key_part[i].field,
1088  key_buff + maybe_null,
1089  maybe_null ? key_buff : 0,
1090  keyinfo->key_part[i].length, keyuse->getVal());
1091  if (session->is_fatal_error)
1092  {
1093  return true;
1094  }
1095  tmp.copy();
1096  }
1097  else
1098  {
1099  *ref_key++= get_store_key(session,
1100  keyuse,join->const_table_map,
1101  &keyinfo->key_part[i],
1102  key_buff, maybe_null);
1103  }
1104 
1105  /*
1106  Remember if we are going to use REF_OR_NULL
1107  But only if field _really_ can be null i.e. we force AM_REF
1108  instead of AM_REF_OR_NULL in case if field can't be null
1109  */
1110  if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
1111  null_ref_key= key_buff;
1112  key_buff+=keyinfo->key_part[i].store_length;
1113  }
1114  }
1115  *ref_key= 0; // end_marker
1116  if (j->type == AM_CONST)
1117  {
1118  j->table->const_table= 1;
1119  }
1120  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) || keyparts != keyinfo->key_parts || null_ref_key)
1121  {
1122  /* Must read with repeat */
1123  j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
1124  j->ref.null_ref_key= null_ref_key;
1125  }
1126  else if (keyuse_uses_no_tables)
1127  {
1128  /*
1129  This happen if we are using a constant expression in the ON part
1130  of an LEFT JOIN.
1131  SELECT * FROM a LEFT JOIN b ON b.key=30
1132  Here we should not mark the table as a 'const' as a field may
1133  have a 'normal' value or a NULL value.
1134  */
1135  j->type= AM_CONST;
1136  }
1137  else
1138  {
1139  j->type= AM_EQ_REF;
1140  }
1141 
1142  return 0;
1143 }
1144 
1195 void add_not_null_conds(Join *join)
1196 {
1197  for (uint32_t i= join->const_tables; i < join->tables; i++)
1198  {
1199  JoinTable *tab=join->join_tab+i;
1200  if ((tab->type == AM_REF || tab->type == AM_EQ_REF ||
1201  tab->type == AM_REF_OR_NULL) &&
1202  !tab->table->maybe_null)
1203  {
1204  for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
1205  {
1206  if (tab->ref.null_rejecting & (1 << keypart))
1207  {
1208  Item *item= tab->ref.items[keypart];
1209  Item *notnull;
1210  assert(item->type() == Item::FIELD_ITEM);
1211  Item_field *not_null_item= (Item_field*)item;
1212  JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
1213  /*
1214  For UPDATE queries such as:
1215  UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1216  not_null_item is the t1.f1, but it's referred_tab is 0.
1217  */
1218  if (!referred_tab || referred_tab->join != join)
1219  {
1220  continue;
1221  }
1222  notnull= new Item_func_isnotnull(not_null_item);
1223 
1224  /*
1225  We need to do full fix_fields() call here in order to have correct
1226  notnull->const_item(). This is needed e.g. by test_quick_select
1227  when it is called from make_join_select after this function is
1228  called.
1229  */
1230  if (notnull->fix_fields(join->session, &notnull))
1231  {
1232  return;
1233  }
1234 
1235  add_cond_and_fix(&referred_tab->select_cond, notnull);
1236  }
1237  }
1238  }
1239  }
1240 }
1241 
1257 COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
1258 {
1259  COND *tmp;
1260  assert(cond != 0);
1261  if (tab == root_tab)
1262  return cond;
1263  if ((tmp= add_found_match_trig_cond(tab->first_upper, cond, root_tab)))
1264  tmp= new Item_func_trig_cond(tmp, &tab->found);
1265  if (tmp)
1266  {
1267  tmp->quick_fix_field();
1268  tmp->update_used_tables();
1269  }
1270  return tmp;
1271 }
1272 
1276 void JoinTable::cleanup()
1277 {
1278  safe_delete(select);
1279  safe_delete(quick);
1280 
1281  if (cache.buff)
1282  {
1283  size_t size= cache.end - cache.buff;
1284  global_join_buffer.sub(size);
1285  free(cache.buff);
1286  }
1287  cache.buff= 0;
1288  limit= 0;
1289  if (table)
1290  {
1291  if (table->key_read)
1292  {
1293  table->key_read= 0;
1294  table->cursor->extra(HA_EXTRA_NO_KEYREAD);
1295  }
1296  table->cursor->ha_index_or_rnd_end();
1297  /*
1298  We need to reset this for next select
1299  (Tested in part_of_refkey)
1300  */
1301  table->reginfo.join_tab= 0;
1302  }
1303  read_record.end_read_record();
1304 }
1305 
1306 bool only_eq_ref_tables(Join *join,Order *order,table_map tables)
1307 {
1308  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
1309  {
1310  if (tables & 1 && !eq_ref_table(join, order, *tab))
1311  {
1312  return 0;
1313  }
1314  }
1315  return 1;
1316 }
1317 
1337 bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
1338 {
1339  if (tab->cached_eq_ref_table) // If cached
1340  {
1341  return tab->eq_ref_table;
1342  }
1343 
1344  tab->cached_eq_ref_table=1;
1345 
1346  /* We can skip const tables only if not an outer table */
1347  if (tab->type == AM_CONST && !tab->first_inner)
1348  {
1349  return (tab->eq_ref_table=1);
1350  }
1351 
1352  if (tab->type != AM_EQ_REF || tab->table->maybe_null)
1353  {
1354  return (tab->eq_ref_table=0); // We must use this
1355  }
1356 
1357  Item **ref_item=tab->ref.items;
1358  Item **end=ref_item+tab->ref.key_parts;
1359  uint32_t found=0;
1360  table_map map=tab->table->map;
1361 
1362  for (; ref_item != end ; ref_item++)
1363  {
1364  if (! (*ref_item)->const_item())
1365  { // Not a const ref
1366  Order *order;
1367  for (order=start_order ; order ; order=order->next)
1368  {
1369  if ((*ref_item)->eq(order->item[0],0))
1370  break;
1371  }
1372 
1373  if (order)
1374  {
1375  found++;
1376  assert(!(order->used & map));
1377  order->used|=map;
1378  continue; // Used in order_st BY
1379  }
1380 
1381  if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1382  {
1383  return (tab->eq_ref_table= 0);
1384  }
1385  }
1386  }
1387  /* Check that there was no reference to table before sort order */
1388  for (; found && start_order ; start_order=start_order->next)
1389  {
1390  if (start_order->used & map)
1391  {
1392  found--;
1393  continue;
1394  }
1395  if (start_order->depend_map & map)
1396  return (tab->eq_ref_table= 0);
1397  }
1398  return tab->eq_ref_table= 1;
1399 }
1400 
1419 static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
1420 {
1421  Item_equal *item= 0;
1422  bool in_upper_level= false;
1423  while (cond_equal)
1424  {
1425  List<Item_equal>::iterator li(cond_equal->current_level.begin());
1426  while ((item= li++))
1427  {
1428  if (item->contains(field))
1429  {
1430  goto finish;
1431  }
1432  }
1433  in_upper_level= true;
1434  cond_equal= cond_equal->upper_levels;
1435  }
1436  in_upper_level= false;
1437 
1438 finish:
1439  *inherited_fl= in_upper_level;
1440  return item;
1441 }
1442 
1524 static bool check_simple_equality(Item *left_item,
1525  Item *right_item,
1526  Item *item,
1527  COND_EQUAL *cond_equal)
1528 {
1529  if (left_item->type() == Item::FIELD_ITEM &&
1530  right_item->type() == Item::FIELD_ITEM &&
1531  !((Item_field*)left_item)->depended_from &&
1532  !((Item_field*)right_item)->depended_from)
1533  {
1534  /* The predicate the form field1=field2 is processed */
1535 
1536  Field *left_field= ((Item_field*) left_item)->field;
1537  Field *right_field= ((Item_field*) right_item)->field;
1538 
1539  if (!left_field->eq_def(right_field))
1540  return false;
1541 
1542  /* Search for multiple equalities containing field1 and/or field2 */
1543  bool left_copyfl, right_copyfl;
1544  Item_equal *left_item_equal=
1545  find_item_equal(cond_equal, left_field, &left_copyfl);
1546  Item_equal *right_item_equal=
1547  find_item_equal(cond_equal, right_field, &right_copyfl);
1548 
1549  /* As (NULL=NULL) != true we can't just remove the predicate f=f */
1550  if (left_field->eq(right_field)) /* f = f */
1551  return (!(left_field->maybe_null() && !left_item_equal));
1552 
1553  if (left_item_equal && left_item_equal == right_item_equal)
1554  {
1555  /*
1556  The equality predicate is inference of one of the existing
1557  multiple equalities, i.e the condition is already covered
1558  by upper level equalities
1559  */
1560  return true;
1561  }
1562 
1563  bool copy_item_name= test(item && item->name >= subq_sj_cond_name &&
1564  item->name < subq_sj_cond_name + 64);
1565  /* Copy the found multiple equalities at the current level if needed */
1566  if (left_copyfl)
1567  {
1568  /* left_item_equal of an upper level contains left_item */
1569  left_item_equal= new Item_equal(left_item_equal);
1570  cond_equal->current_level.push_back(left_item_equal);
1571  if (copy_item_name)
1572  {
1573  left_item_equal->name = item->name;
1574  }
1575  }
1576  if (right_copyfl)
1577  {
1578  /* right_item_equal of an upper level contains right_item */
1579  right_item_equal= new Item_equal(right_item_equal);
1580  cond_equal->current_level.push_back(right_item_equal);
1581  if (copy_item_name)
1582  {
1583  right_item_equal->name = item->name;
1584  }
1585  }
1586 
1587  if (left_item_equal)
1588  {
1589  /* left item was found in the current or one of the upper levels */
1590  if (! right_item_equal)
1591  left_item_equal->add((Item_field *) right_item);
1592  else
1593  {
1594  /* Merge two multiple equalities forming a new one */
1595  left_item_equal->merge(right_item_equal);
1596  /* Remove the merged multiple equality from the list */
1597  List<Item_equal>::iterator li(cond_equal->current_level.begin());
1598  while ((li++) != right_item_equal) {};
1599  li.remove();
1600  }
1601  }
1602  else
1603  {
1604  /* left item was not found neither the current nor in upper levels */
1605  if (right_item_equal)
1606  {
1607  right_item_equal->add((Item_field *) left_item);
1608  if (copy_item_name)
1609  {
1610  right_item_equal->name = item->name;
1611  }
1612  }
1613  else
1614  {
1615  /* None of the fields was found in multiple equalities */
1616  Item_equal *item_equal= new Item_equal((Item_field *) left_item,
1617  (Item_field *) right_item);
1618  cond_equal->current_level.push_back(item_equal);
1619  if (copy_item_name)
1620  {
1621  item_equal->name = item->name;
1622  }
1623  }
1624  }
1625  return true;
1626  }
1627 
1628  {
1629  /* The predicate of the form field=const/const=field is processed */
1630  Item *const_item= 0;
1631  Item_field *field_item= 0;
1632  if (left_item->type() == Item::FIELD_ITEM &&
1633  !((Item_field*)left_item)->depended_from &&
1634  right_item->const_item())
1635  {
1636  field_item= (Item_field*) left_item;
1637  const_item= right_item;
1638  }
1639  else if (right_item->type() == Item::FIELD_ITEM &&
1640  !((Item_field*)right_item)->depended_from &&
1641  left_item->const_item())
1642  {
1643  field_item= (Item_field*) right_item;
1644  const_item= left_item;
1645  }
1646 
1647  if (const_item &&
1648  field_item->result_type() == const_item->result_type())
1649  {
1650  bool copyfl;
1651 
1652  if (field_item->result_type() == STRING_RESULT)
1653  {
1654  const charset_info_st * const cs= ((Field_str*) field_item->field)->charset();
1655  if (!item)
1656  {
1657  Item_func_eq *eq_item;
1658  eq_item= new Item_func_eq(left_item, right_item);
1659  eq_item->set_cmp_func();
1660  eq_item->quick_fix_field();
1661  item= eq_item;
1662  }
1663 
1664  if ((cs != ((Item_func *) item)->compare_collation()) || !cs->coll->propagate())
1665  {
1666  return false;
1667  }
1668  }
1669 
1670  Item_equal *item_equal = find_item_equal(cond_equal,
1671  field_item->field, &copyfl);
1672  if (copyfl)
1673  {
1674  item_equal= new Item_equal(item_equal);
1675  cond_equal->current_level.push_back(item_equal);
1676  }
1677  if (item_equal)
1678  {
1679  /*
1680  The flag cond_false will be set to 1 after this, if item_equal
1681  already contains a constant and its value is not equal to
1682  the value of const_item.
1683  */
1684  item_equal->add(const_item);
1685  }
1686  else
1687  {
1688  item_equal= new Item_equal(const_item, field_item);
1689  cond_equal->current_level.push_back(item_equal);
1690  }
1691  return true;
1692  }
1693  }
1694 
1695  return false;
1696 }
1697 
1723 static bool check_row_equality(Session *session,
1724  Item *left_row,
1725  Item_row *right_row,
1726  COND_EQUAL *cond_equal,
1727  List<Item>* eq_list)
1728 {
1729  uint32_t n= left_row->cols();
1730  for (uint32_t i= 0 ; i < n; i++)
1731  {
1732  bool is_converted;
1733  Item *left_item= left_row->element_index(i);
1734  Item *right_item= right_row->element_index(i);
1735  if (left_item->type() == Item::ROW_ITEM &&
1736  right_item->type() == Item::ROW_ITEM)
1737  {
1738  is_converted= check_row_equality(session,
1739  (Item_row *) left_item,
1740  (Item_row *) right_item,
1741  cond_equal, eq_list);
1742  if (!is_converted)
1743  {
1744  session->lex().current_select->cond_count++;
1745  }
1746  }
1747  else
1748  {
1749  is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1750  session->lex().current_select->cond_count++;
1751  }
1752 
1753  if (!is_converted)
1754  {
1755  Item_func_eq *eq_item;
1756  eq_item= new Item_func_eq(left_item, right_item);
1757  eq_item->set_cmp_func();
1758  eq_item->quick_fix_field();
1759  eq_list->push_back(eq_item);
1760  }
1761  }
1762  return true;
1763 }
1764 
1794 static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
1795 {
1796  if (item->type() == Item::FUNC_ITEM &&
1797  ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
1798  {
1799  Item *left_item= ((Item_func*) item)->arguments()[0];
1800  Item *right_item= ((Item_func*) item)->arguments()[1];
1801 
1802  if (left_item->type() == Item::ROW_ITEM &&
1803  right_item->type() == Item::ROW_ITEM)
1804  {
1805  session->lex().current_select->cond_count--;
1806  return check_row_equality(session,
1807  (Item_row *) left_item,
1808  (Item_row *) right_item,
1809  cond_equal, eq_list);
1810  }
1811  else
1812  {
1813  return check_simple_equality(left_item, right_item, item, cond_equal);
1814  }
1815  }
1816  return false;
1817 }
1818 
1882 static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
1883 {
1884  Item_equal *item_equal;
1885  COND_EQUAL cond_equal;
1886  cond_equal.upper_levels= inherited;
1887 
1888  if (cond->type() == Item::COND_ITEM)
1889  {
1890  List<Item> eq_list;
1891  bool and_level= ((Item_cond*) cond)->functype() ==
1892  Item_func::COND_AND_FUNC;
1893  List<Item> *args= ((Item_cond*) cond)->argument_list();
1894 
1895  List<Item>::iterator li(args->begin());
1896  Item *item;
1897 
1898  if (and_level)
1899  {
1900  /*
1901  Retrieve all conjucts of this level detecting the equality
1902  that are subject to substitution by multiple equality items and
1903  removing each such predicate from the conjunction after having
1904  found/created a multiple equality whose inference the predicate is.
1905  */
1906  while ((item= li++))
1907  {
1908  /*
1909  PS/SP note: we can safely remove a node from AND-OR
1910  structure here because it's restored before each
1911  re-execution of any prepared statement/stored procedure.
1912  */
1913  if (check_equality(session, item, &cond_equal, &eq_list))
1914  {
1915  li.remove();
1916  }
1917  }
1918 
1919  List<Item_equal>::iterator it(cond_equal.current_level.begin());
1920  while ((item_equal= it++))
1921  {
1922  item_equal->fix_length_and_dec();
1923  item_equal->update_used_tables();
1924  set_if_bigger(session->lex().current_select->max_equal_elems,
1925  item_equal->members());
1926  }
1927 
1928  ((Item_cond_and*)cond)->cond_equal= cond_equal;
1929  inherited= &(((Item_cond_and*)cond)->cond_equal);
1930  }
1931  /*
1932  Make replacement of equality predicates for lower levels
1933  of the condition expression.
1934  */
1935  li= args->begin();
1936  while ((item= li++))
1937  {
1938  Item *new_item;
1939  if ((new_item= build_equal_items_for_cond(session, item, inherited)) != item)
1940  {
1941  /* This replacement happens only for standalone equalities */
1942  /*
1943  This is ok with PS/SP as the replacement is done for
1944  arguments of an AND/OR item, which are restored for each
1945  execution of PS/SP.
1946  */
1947  li.replace(new_item);
1948  }
1949  }
1950  if (and_level)
1951  {
1952  args->concat(&eq_list);
1953  args->concat((List<Item> *)&cond_equal.current_level);
1954  }
1955  }
1956  else if (cond->type() == Item::FUNC_ITEM)
1957  {
1958  List<Item> eq_list;
1959  /*
1960  If an equality predicate forms the whole and level,
1961  we call it standalone equality and it's processed here.
1962  E.g. in the following where condition
1963  WHERE a=5 AND (b=5 or a=c)
1964  (b=5) and (a=c) are standalone equalities.
1965  In general we can't leave alone standalone eqalities:
1966  for WHERE a=b AND c=d AND (b=c OR d=5)
1967  b=c is replaced by =(a,b,c,d).
1968  */
1969  if (check_equality(session, cond, &cond_equal, &eq_list))
1970  {
1971  int n= cond_equal.current_level.size() + eq_list.size();
1972 
1973  if (n == 0)
1974  {
1975  return new Item_int((int64_t) 1,1);
1976  }
1977  else if (n == 1)
1978  {
1979  if ((item_equal= cond_equal.current_level.pop()))
1980  {
1981  item_equal->fix_length_and_dec();
1982  item_equal->update_used_tables();
1983  }
1984  else
1985  {
1986  item_equal= (Item_equal *) eq_list.pop();
1987  }
1988  set_if_bigger(session->lex().current_select->max_equal_elems,
1989  item_equal->members());
1990  return item_equal;
1991  }
1992  else
1993  {
1994  /*
1995  Here a new AND level must be created. It can happen only
1996  when a row equality is processed as a standalone predicate.
1997  */
1998  Item_cond_and *and_cond= new Item_cond_and(eq_list);
1999  and_cond->quick_fix_field();
2000  List<Item> *args= and_cond->argument_list();
2001  List<Item_equal>::iterator it(cond_equal.current_level.begin());
2002  while ((item_equal= it++))
2003  {
2004  item_equal->fix_length_and_dec();
2005  item_equal->update_used_tables();
2006  set_if_bigger(session->lex().current_select->max_equal_elems,
2007  item_equal->members());
2008  }
2009  and_cond->cond_equal= cond_equal;
2010  args->concat((List<Item> *)&cond_equal.current_level);
2011 
2012  return and_cond;
2013  }
2014  }
2015  /*
2016  For each field reference in cond, not from equal item predicates,
2017  set a pointer to the multiple equality it belongs to (if there is any)
2018  as soon the field is not of a string type or the field reference is
2019  an argument of a comparison predicate.
2020  */
2021  unsigned char *is_subst_valid= (unsigned char *) 1;
2022  cond= cond->compile(&Item::subst_argument_checker,
2023  &is_subst_valid,
2024  &Item::equal_fields_propagator,
2025  (unsigned char *) inherited);
2026  cond->update_used_tables();
2027  }
2028  return cond;
2029 }
2030 
2096 static COND *build_equal_items(Session *session, COND *cond,
2097  COND_EQUAL *inherited,
2098  List<TableList> *join_list,
2099  COND_EQUAL **cond_equal_ref)
2100 {
2101  COND_EQUAL *cond_equal= 0;
2102 
2103  if (cond)
2104  {
2105  cond= build_equal_items_for_cond(session, cond, inherited);
2106  cond->update_used_tables();
2107 
2108  if (cond->type() == Item::COND_ITEM && ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
2109  {
2110  cond_equal= &((Item_cond_and*) cond)->cond_equal;
2111  }
2112  else if (cond->type() == Item::FUNC_ITEM &&
2113  ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2114  {
2115  cond_equal= new COND_EQUAL;
2116  cond_equal->current_level.push_back((Item_equal *) cond);
2117  }
2118  }
2119  if (cond_equal)
2120  {
2121  cond_equal->upper_levels= inherited;
2122  inherited= cond_equal;
2123  }
2124  *cond_equal_ref= cond_equal;
2125 
2126  if (join_list)
2127  {
2128  TableList *table;
2129  List<TableList>::iterator li(join_list->begin());
2130 
2131  while ((table= li++))
2132  {
2133  if (table->on_expr)
2134  {
2135  List<TableList> *nested_join_list= table->getNestedJoin() ?
2136  &table->getNestedJoin()->join_list : NULL;
2137  /*
2138  We can modify table->on_expr because its old value will
2139  be restored before re-execution of PS/SP.
2140  */
2141  table->on_expr= build_equal_items(session, table->on_expr, inherited,
2142  nested_join_list,
2143  &table->cond_equal);
2144  }
2145  }
2146  }
2147 
2148  return cond;
2149 }
2150 
2171  Item_field *field2,
2172  void *table_join_idx)
2173 {
2174  int cmp= 0;
2175  bool outer_ref= 0;
2176  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
2177  {
2178  outer_ref= 1;
2179  cmp= -1;
2180  }
2181 
2182  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
2183  {
2184  outer_ref= 1;
2185  cmp++;
2186  }
2187 
2188  if (outer_ref)
2189  {
2190  return cmp;
2191  }
2192 
2193  JoinTable **idx= (JoinTable **) table_join_idx;
2194  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
2195 
2196  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2197 }
2198 
2238 static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
2239 {
2240  List<Item> eq_list;
2241  Item_func_eq *eq_item= 0;
2242  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
2243  return new Item_int((int64_t) 0,1);
2244  Item *item_const= item_equal->get_const();
2245  Item_equal_iterator it(item_equal->begin());
2246  Item *head;
2247  if (item_const)
2248  {
2249  head= item_const;
2250  }
2251  else
2252  {
2253  head= item_equal->get_first();
2254  it++;
2255  }
2256  Item_field *item_field;
2257  while ((item_field= it++))
2258  {
2259  Item_equal *upper= item_field->find_item_equal(upper_levels);
2260  Item_field *item= item_field;
2261  if (upper)
2262  {
2263  if (item_const && upper->get_const())
2264  {
2265  item= 0;
2266  }
2267  else
2268  {
2269  Item_equal_iterator li(item_equal->begin());
2270  while ((item= li++) != item_field)
2271  {
2272  if (item->find_item_equal(upper_levels) == upper)
2273  {
2274  break;
2275  }
2276  }
2277  }
2278  }
2279  if (item == item_field)
2280  {
2281  if (eq_item)
2282  {
2283  eq_list.push_back(eq_item);
2284  }
2285 
2286  eq_item= new Item_func_eq(item_field, head);
2287 
2288  if (!eq_item)
2289  {
2290  return 0;
2291  }
2292  eq_item->set_cmp_func();
2293  eq_item->quick_fix_field();
2294  }
2295  }
2296 
2297  if (!cond && !&eq_list.front())
2298  {
2299  if (!eq_item)
2300  {
2301  return new Item_int((int64_t) 1,1);
2302  }
2303  return eq_item;
2304  }
2305 
2306  if (eq_item)
2307  {
2308  eq_list.push_back(eq_item);
2309  }
2310 
2311  if (!cond)
2312  {
2313  cond= new Item_cond_and(eq_list);
2314  }
2315  else
2316  {
2317  assert(cond->type() == Item::COND_ITEM);
2318  ((Item_cond *) cond)->add_at_head(&eq_list);
2319  }
2320 
2321  cond->quick_fix_field();
2322  cond->update_used_tables();
2323 
2324  return cond;
2325 }
2326 
2354 COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
2355 {
2356  Item_equal *item_equal;
2357 
2358  if (cond->type() == Item::COND_ITEM)
2359  {
2360  List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
2361 
2362  bool and_level= ((Item_cond*) cond)->functype() ==
2363  Item_func::COND_AND_FUNC;
2364  if (and_level)
2365  {
2366  cond_equal= &((Item_cond_and *) cond)->cond_equal;
2367  cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2368 
2369  List<Item_equal>::iterator it(cond_equal->current_level.begin());
2370  while ((item_equal= it++))
2371  {
2372  item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2373  }
2374  }
2375 
2376  List<Item>::iterator li(cond_list->begin());
2377  Item *item;
2378  while ((item= li++))
2379  {
2380  Item *new_item =substitute_for_best_equal_field(item, cond_equal,
2381  table_join_idx);
2382  /*
2383  This works OK with PS/SP re-execution as changes are made to
2384  the arguments of AND/OR items only
2385  */
2386  if (new_item != item)
2387  li.replace(new_item);
2388  }
2389 
2390  if (and_level)
2391  {
2392  List<Item_equal>::iterator it(cond_equal->current_level.begin());
2393  while ((item_equal= it++))
2394  {
2395  cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
2396  // This occurs when eliminate_item_equal() founds that cond is
2397  // always false and substitutes it with Item_int 0.
2398  // Due to this, value of item_equal will be 0, so just return it.
2399  if (cond->type() != Item::COND_ITEM)
2400  break;
2401  }
2402  }
2403 
2404  if (cond->type() == Item::COND_ITEM && !((Item_cond*)cond)->argument_list()->size())
2405  {
2406  cond= new Item_int((int32_t)cond->val_bool());
2407  }
2408 
2409  }
2410  else if (cond->type() == Item::FUNC_ITEM &&
2411  ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2412  {
2413  item_equal= (Item_equal *) cond;
2414  item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2415  if (cond_equal && &cond_equal->current_level.front() == item_equal)
2416  {
2417  cond_equal= 0;
2418  }
2419 
2420  return eliminate_item_equal(0, cond_equal, item_equal);
2421  }
2422  else
2423  {
2424  cond->transform(&Item::replace_equal_field, 0);
2425  }
2426 
2427  return cond;
2428 }
2429 
2443 {
2444  if (!(cond->used_tables() & tab->table->map))
2445  return;
2446 
2447  if (cond->type() == Item::COND_ITEM)
2448  {
2449  List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
2450  List<Item>::iterator li(cond_list->begin());
2451  Item *item;
2452  while ((item= li++))
2453  update_const_equal_items(item, tab);
2454  }
2455  else if (cond->type() == Item::FUNC_ITEM &&
2456  ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2457  {
2458  Item_equal *item_equal= (Item_equal *) cond;
2459  bool contained_const= item_equal->get_const() != NULL;
2460  item_equal->update_const();
2461  if (!contained_const && item_equal->get_const())
2462  {
2463  /* Update keys for range analysis */
2464  Item_equal_iterator it(item_equal->begin());
2465  Item_field *item_field;
2466  while ((item_field= it++))
2467  {
2468  Field *field= item_field->field;
2469  JoinTable *stat= field->getTable()->reginfo.join_tab;
2470  key_map possible_keys= field->key_start;
2471  possible_keys&= field->getTable()->keys_in_use_for_query;
2472  stat[0].const_keys|= possible_keys;
2473 
2474  /*
2475  For each field in the multiple equality (for which we know that it
2476  is a constant) we have to find its corresponding key part, and set
2477  that key part in const_key_parts.
2478  */
2479  if (possible_keys.any())
2480  {
2481  Table *field_tab= field->getTable();
2482  optimizer::KeyUse *use;
2483  for (use= stat->keyuse; use && use->getTable() == field_tab; use++)
2484  if (possible_keys.test(use->getKey()) &&
2485  field_tab->key_info[use->getKey()].key_part[use->getKeypart()].field ==
2486  field)
2487  field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
2488  }
2489  }
2490  }
2491  }
2492 }
2493 
2494 /*
2495  change field = field to field = const for each found field = const in the
2496  and_level
2497 */
2498 static void change_cond_ref_to_const(Session *session,
2499  list<COND_CMP>& save_list,
2500  Item *and_father,
2501  Item *cond,
2502  Item *field,
2503  Item *value)
2504 {
2505  if (cond->type() == Item::COND_ITEM)
2506  {
2507  bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2508  List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
2509  Item *item;
2510  while ((item=li++))
2511  change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2512 
2513  return;
2514  }
2515 
2516  if (cond->eq_cmp_result() == Item::COND_OK)
2517  {
2518  return; // Not a boolean function
2519  }
2520 
2521  Item_bool_func2 *func= (Item_bool_func2*) cond;
2522  Item **args= func->arguments();
2523  Item *left_item= args[0];
2524  Item *right_item= args[1];
2525  Item_func::Functype functype= func->functype();
2526 
2527  if (right_item->eq(field,0) && left_item != value &&
2528  right_item->cmp_context == field->cmp_context &&
2529  (left_item->result_type() != STRING_RESULT ||
2530  value->result_type() != STRING_RESULT ||
2531  left_item->collation.collation == value->collation.collation))
2532  {
2533  Item *tmp=value->clone_item();
2534  if (tmp)
2535  {
2536  tmp->collation.set(right_item->collation);
2537  args[1]= tmp;
2538  func->update_used_tables();
2539  if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2540  and_father != cond &&
2541  ! left_item->const_item())
2542  {
2543  cond->marker=1;
2544  save_list.push_back( COND_CMP(and_father, func) );
2545  }
2546  func->set_cmp_func();
2547  }
2548  }
2549  else if (left_item->eq(field,0) && right_item != value &&
2550  left_item->cmp_context == field->cmp_context &&
2551  (right_item->result_type() != STRING_RESULT ||
2552  value->result_type() != STRING_RESULT ||
2553  right_item->collation.collation == value->collation.collation))
2554  {
2555  Item *tmp= value->clone_item();
2556  if (tmp)
2557  {
2558  tmp->collation.set(left_item->collation);
2559  *args= tmp;
2560  value= tmp;
2561  func->update_used_tables();
2562  if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2563  and_father != cond &&
2564  ! right_item->const_item())
2565  {
2566  args[0]= args[1]; // For easy check
2567  args[1]= value;
2568  cond->marker=1;
2569  save_list.push_back( COND_CMP(and_father, func) );
2570  }
2571  func->set_cmp_func();
2572  }
2573  }
2574 }
2575 
2585 {
2586  if (conds->name == in_additional_cond)
2587  {
2588  return 0;
2589  }
2590 
2591  if (conds->type() == Item::COND_ITEM)
2592  {
2593  Item_cond *cnd= (Item_cond*) conds;
2594  List<Item>::iterator li(cnd->argument_list()->begin());
2595  Item *item;
2596  while ((item= li++))
2597  {
2598  if (item->name == in_additional_cond)
2599  {
2600  li.remove();
2601  if (cnd->argument_list()->size() == 1)
2602  {
2603  return &cnd->argument_list()->front();
2604  }
2605 
2606  return conds;
2607  }
2608  }
2609  }
2610  return conds;
2611 }
2612 
2613 static void propagate_cond_constants(Session *session,
2614  list<COND_CMP>& save_list,
2615  COND *and_father,
2616  COND *cond)
2617 {
2618  if (cond->type() == Item::COND_ITEM)
2619  {
2620  bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2621  List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
2622  Item *item;
2623  list<COND_CMP> save;
2624  while ((item=li++))
2625  {
2626  propagate_cond_constants(session, save, and_level ? cond : item, item);
2627  }
2628  if (and_level)
2629  {
2630  // Handle other found items
2631  for (list<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
2632  {
2633  Item **args= iter->second->arguments();
2634  if (not args[0]->const_item())
2635  {
2636  change_cond_ref_to_const(session, save, iter->first,
2637  iter->first, args[0], args[1] );
2638  }
2639  }
2640  }
2641  }
2642  else if (and_father != cond && !cond->marker) // In a AND group
2643  {
2644  if (cond->type() == Item::FUNC_ITEM &&
2645  (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2646  ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2647  {
2648  Item_func_eq *func=(Item_func_eq*) cond;
2649  Item **args= func->arguments();
2650  bool left_const= args[0]->const_item();
2651  bool right_const= args[1]->const_item();
2652  if (!(left_const && right_const) && args[0]->result_type() == args[1]->result_type())
2653  {
2654  if (right_const)
2655  {
2656  resolve_const_item(session, &args[1], args[0]);
2657  func->update_used_tables();
2658  change_cond_ref_to_const(session, save_list, and_father, and_father,
2659  args[0], args[1]);
2660  }
2661  else if (left_const)
2662  {
2663  resolve_const_item(session, &args[0], args[1]);
2664  func->update_used_tables();
2665  change_cond_ref_to_const(session, save_list, and_father, and_father,
2666  args[1], args[0]);
2667  }
2668  }
2669  }
2670  }
2671 }
2672 
2764 {
2765  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
2766  Join *join= next_tab->join;
2767 
2768  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
2769  {
2770  /*
2771  next_tab is outside of the "pair of brackets" we're currently in.
2772  Cannot add it.
2773  */
2774  return true;
2775  }
2776 
2777  /*
2778  Do update counters for "pairs of brackets" that we've left (marked as
2779  X,Y,Z in the above picture)
2780  */
2781  for (;next_emb; next_emb= next_emb->getEmbedding())
2782  {
2783  next_emb->getNestedJoin()->counter_++;
2784  if (next_emb->getNestedJoin()->counter_ == 1)
2785  {
2786  /*
2787  next_emb is the first table inside a nested join we've "entered". In
2788  the picture above, we're looking at the 'X' bracket. Don't exit yet as
2789  X bracket might have Y pair bracket.
2790  */
2791  join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
2792  }
2793 
2794  if (next_emb->getNestedJoin()->join_list.size() != next_emb->getNestedJoin()->counter_)
2795  {
2796  break;
2797  }
2798 
2799  /*
2800  We're currently at Y or Z-bracket as depicted in the above picture.
2801  Mark that we've left it and continue walking up the brackets hierarchy.
2802  */
2803  join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
2804  }
2805  return false;
2806 }
2807 
2808 COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
2809 {
2810  Session *session= join->session;
2811 
2812  if (conds == NULL)
2813  {
2814  *cond_value= Item::COND_TRUE;
2815  }
2816  else
2817  {
2818  /*
2819  Build all multiple equality predicates and eliminate equality
2820  predicates that can be inferred from these multiple equalities.
2821  For each reference of a field included into a multiple equality
2822  that occurs in a function set a pointer to the multiple equality
2823  predicate. Substitute a constant instead of this field if the
2824  multiple equality contains a constant.
2825  */
2826  conds= build_equal_items(join->session, conds, NULL, join_list,
2827  &join->cond_equal);
2828 
2829  /* change field = field to field = const for each found field = const */
2830  list<COND_CMP> temp;
2831  propagate_cond_constants(session, temp, conds, conds);
2832  /*
2833  Remove all instances of item == item
2834  Remove all and-levels where CONST item != CONST item
2835  */
2836  conds= remove_eq_conds(session, conds, cond_value) ;
2837  }
2838  return(conds);
2839 }
2840 
2851 COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
2852 {
2853  if (cond->type() == Item::COND_ITEM)
2854  {
2855  bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
2856 
2857  List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
2858  Item::cond_result tmp_cond_value;
2859  bool should_fix_fields= false;
2860 
2861  *cond_value= Item::COND_UNDEF;
2862  Item *item;
2863  while ((item= li++))
2864  {
2865  Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
2866  if (! new_item)
2867  {
2868  li.remove();
2869  }
2870  else if (item != new_item)
2871  {
2872  li.replace(new_item);
2873  should_fix_fields= true;
2874  }
2875 
2876  if (*cond_value == Item::COND_UNDEF)
2877  {
2878  *cond_value= tmp_cond_value;
2879  }
2880 
2881  switch (tmp_cond_value)
2882  {
2883  case Item::COND_OK: /* Not true or false */
2884  if (and_level || (*cond_value == Item::COND_FALSE))
2885  *cond_value= tmp_cond_value;
2886  break;
2887  case Item::COND_FALSE:
2888  if (and_level)
2889  {
2890  *cond_value= tmp_cond_value;
2891  return (COND *) NULL; /* Always false */
2892  }
2893  break;
2894  case Item::COND_TRUE:
2895  if (! and_level)
2896  {
2897  *cond_value= tmp_cond_value;
2898  return (COND *) NULL; /* Always true */
2899  }
2900  break;
2901  case Item::COND_UNDEF: /* Impossible */
2902  break;
2903  }
2904  }
2905 
2906  if (should_fix_fields)
2907  {
2908  cond->update_used_tables();
2909  }
2910 
2911  if (! ((Item_cond*) cond)->argument_list()->size() || *cond_value != Item::COND_OK)
2912  {
2913  return (COND*) NULL;
2914  }
2915 
2916  if (((Item_cond*) cond)->argument_list()->size() == 1)
2917  {
2918  /* Argument list contains only one element, so reduce it so a single item, then remove list */
2919  item= &((Item_cond*) cond)->argument_list()->front();
2920  ((Item_cond*) cond)->argument_list()->clear();
2921 
2922  return item;
2923  }
2924  }
2925  else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
2926  {
2927  /*
2928  Handles this special case for some ODBC applications:
2929  The are requesting the row that was just updated with a auto_increment
2930  value with this construct:
2931 
2932  SELECT * from table_name where auto_increment_column IS NULL
2933  This will be changed to:
2934  SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
2935  */
2936 
2937  Item_func_isnull *func= (Item_func_isnull*) cond;
2938  Item **args= func->arguments();
2939  if (args[0]->type() == Item::FIELD_ITEM)
2940  {
2941  Field *field= ((Item_field*) args[0])->field;
2942  if (field->flags & AUTO_INCREMENT_FLAG
2943  && ! field->getTable()->maybe_null
2944  && session->options & OPTION_AUTO_IS_NULL
2945  && (
2947  && session->substitute_null_with_insert_id
2948  )
2949  )
2950  {
2951  COND *new_cond= new Item_func_eq(args[0], new Item_int("last_insert_id()",
2952  session->read_first_successful_insert_id_in_prev_stmt(), MY_INT64_NUM_DECIMAL_DIGITS));
2953  cond= new_cond;
2954  /*
2955  Item_func_eq can't be fixed after creation so we do not check
2956  cond->fixed, also it do not need tables so we use 0 as second
2957  argument.
2958  */
2959  cond->fix_fields(session, &cond);
2960  /*
2961  IS NULL should be mapped to LAST_INSERT_ID only for first row, so
2962  clear for next row
2963  */
2964  session->substitute_null_with_insert_id= false;
2965  }
2966 #ifdef NOTDEFINED
2967  /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
2968  else if (
2969  ((field->type() == DRIZZLE_TYPE_DATE) || (field->type() == DRIZZLE_TYPE_DATETIME))
2970  && (field->flags & NOT_NULL_FLAG)
2971  && ! field->table->maybe_null)
2972  {
2973  COND* new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2));
2974  cond= new_cond;
2975  /*
2976  Item_func_eq can't be fixed after creation so we do not check
2977  cond->fixed, also it do not need tables so we use 0 as second
2978  argument.
2979  */
2980  cond->fix_fields(session, &cond);
2981  }
2982 #endif /* NOTDEFINED */
2983  }
2984  if (cond->const_item())
2985  {
2986  *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2987  return (COND *) NULL;
2988  }
2989  }
2990  else if (cond->const_item() && !cond->is_expensive())
2991  /*
2992  @todo
2993  Excluding all expensive functions is too restritive we should exclude only
2994  materialized IN subquery predicates because they can't yet be evaluated
2995  here (they need additional initialization that is done later on).
2996 
2997  The proper way to exclude the subqueries would be to walk the cond tree and
2998  check for materialized subqueries there.
2999 
3000  */
3001  {
3002  *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
3003  return (COND *) NULL;
3004  }
3005  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
3006  {
3007  /* boolan compare function */
3008  Item *left_item= ((Item_func*) cond)->arguments()[0];
3009  Item *right_item= ((Item_func*) cond)->arguments()[1];
3010  if (left_item->eq(right_item,1))
3011  {
3012  if (!left_item->maybe_null || ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
3013  {
3014  return (COND*) NULL; /* Comparison of identical items */
3015  }
3016  }
3017  }
3018  *cond_value= Item::COND_OK;
3019  return cond; /* Point at next and return into recursion */
3020 }
3021 
3022 /*
3023  Check if equality can be used in removing components of GROUP BY/DISTINCT
3024 
3025  SYNOPSIS
3026  test_if_equality_guarantees_uniqueness()
3027  l the left comparison argument (a field if any)
3028  r the right comparison argument (a const of any)
3029 
3030  DESCRIPTION
3031  Checks if an equality predicate can be used to take away
3032  DISTINCT/GROUP BY because it is known to be true for exactly one
3033  distinct value (e.g. <expr> == <const>).
3034  Arguments must be of the same type because e.g.
3035  <string_field> = <int_const> may match more than 1 distinct value from
3036  the column.
3037  We must take into consideration and the optimization done for various
3038  string constants when compared to dates etc (see Item_int_with_ref) as
3039  well as the collation of the arguments.
3040 
3041  RETURN VALUE
3042  true can be used
3043  false cannot be used
3044 */
3045 static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
3046 {
3047  return r->const_item() &&
3048  /* elements must be compared as dates */
3049  (Arg_comparator::can_compare_as_dates(l, r, 0) ||
3050  /* or of the same result type */
3051  (r->result_type() == l->result_type() &&
3052  /* and must have the same collation if compared as strings */
3053  (l->result_type() != STRING_RESULT ||
3054  l->collation.collation == r->collation.collation)));
3055 }
3056 
3060 bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
3061 {
3062  if (cond->type() == Item::COND_ITEM)
3063  {
3064  bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
3065  List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
3066 
3067  Item *item;
3068  while ((item=li++))
3069  {
3070  bool res=const_expression_in_where(item, comp_item, const_item);
3071  if (res) // Is a const value
3072  {
3073  if (and_level)
3074  {
3075  return true;
3076  }
3077  }
3078  else if (and_level == false)
3079  {
3080  return false;
3081  }
3082  }
3083  return and_level ? false : true;
3084  }
3085  else if (cond->eq_cmp_result() != Item::COND_OK)
3086  { // boolan compare function
3087  Item_func* func= (Item_func*) cond;
3088  if (func->functype() != Item_func::EQUAL_FUNC &&
3089  func->functype() != Item_func::EQ_FUNC)
3090  {
3091  return false;
3092  }
3093 
3094  Item *left_item= ((Item_func*) cond)->arguments()[0];
3095  Item *right_item= ((Item_func*) cond)->arguments()[1];
3096 
3097  if (left_item->eq(comp_item,1))
3098  {
3099  if (test_if_equality_guarantees_uniqueness (left_item, right_item))
3100  {
3101  if (*const_item)
3102  {
3103  return right_item->eq(*const_item, 1);
3104  }
3105  *const_item=right_item;
3106  return true;
3107  }
3108  }
3109  else if (right_item->eq(comp_item,1))
3110  {
3111  if (test_if_equality_guarantees_uniqueness (right_item, left_item))
3112  {
3113  if (*const_item)
3114  {
3115  return left_item->eq(*const_item, 1);
3116  }
3117  *const_item=left_item;
3118  return true;
3119  }
3120  }
3121  }
3122 
3123  return false;
3124 }
3125 
3137 Next_select_func setup_end_select_func(Join *join)
3138 {
3139  Table *table= join->tmp_table;
3140  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
3141  Next_select_func end_select;
3142 
3143  /* Set up select_end */
3144  if (table)
3145  {
3146  if (table->group && tmp_tbl->sum_func_count &&
3147  !tmp_tbl->precomputed_group_by)
3148  {
3149  if (table->getShare()->sizeKeys())
3150  {
3151  end_select= end_update;
3152  }
3153  else
3154  {
3155  end_select= end_unique_update;
3156  }
3157  }
3158  else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
3159  {
3160  end_select= end_write_group;
3161  }
3162  else
3163  {
3164  end_select= end_write;
3165  if (tmp_tbl->precomputed_group_by)
3166  {
3167  /*
3168  A preceding call to create_tmp_table in the case when loose
3169  index scan is used guarantees that
3170  Tmp_Table_Param::items_to_copy has enough space for the group
3171  by functions. It is OK here to use memcpy since we copy
3172  Item_sum pointers into an array of Item pointers.
3173  */
3174  memcpy(tmp_tbl->items_to_copy + tmp_tbl->func_count,
3175  join->sum_funcs,
3176  sizeof(Item*)*tmp_tbl->sum_func_count);
3177  tmp_tbl->items_to_copy[tmp_tbl->func_count+tmp_tbl->sum_func_count]= 0;
3178  }
3179  }
3180  }
3181  else
3182  {
3183  if ((join->sort_and_group) && !tmp_tbl->precomputed_group_by)
3184  {
3185  end_select= end_send_group;
3186  }
3187  else
3188  {
3189  end_select= end_send;
3190  }
3191  }
3192 
3193  return end_select;
3194 }
3195 
3206 int do_select(Join *join, List<Item> *fields, Table *table)
3207 {
3208  int rc= 0;
3209  enum_nested_loop_state error= NESTED_LOOP_OK;
3210  JoinTable *join_tab= NULL;
3211 
3212  join->tmp_table= table; /* Save for easy recursion */
3213  join->fields= fields;
3214 
3215  if (table)
3216  {
3217  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
3218  table->emptyRecord();
3219  if (table->group && join->tmp_table_param.sum_func_count &&
3220  table->getShare()->sizeKeys() && !table->cursor->inited)
3221  {
3222  int tmp_error;
3223  tmp_error= table->cursor->startIndexScan(0, 0);
3224  if (tmp_error != 0)
3225  {
3226  table->print_error(tmp_error, MYF(0));
3227  return -1;
3228  }
3229  }
3230  }
3231 
3232  /* Set up select_end */
3233  Next_select_func end_select= setup_end_select_func(join);
3234  if (join->tables)
3235  {
3236  join->join_tab[join->tables-1].next_select= end_select;
3237 
3238  join_tab=join->join_tab+join->const_tables;
3239  }
3240 
3241  join->send_records=0;
3242  if (join->tables == join->const_tables)
3243  {
3244  /*
3245  HAVING will be checked after processing aggregate functions,
3246  But WHERE should checkd here (we alredy have read tables)
3247  */
3248  if (!join->conds || join->conds->val_int())
3249  {
3250  error= (*end_select)(join, 0, 0);
3251  if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
3252  error= (*end_select)(join, 0, 1);
3253 
3254  /*
3255  If we don't go through evaluate_join_record(), do the counting
3256  here. join->send_records is increased on success in end_send(),
3257  so we don't touch it here.
3258  */
3259  join->examined_rows++;
3260  join->session->row_count++;
3261  assert(join->examined_rows <= 1);
3262  }
3263  else if (join->send_row_on_empty_set())
3264  {
3265  List<Item> *columns_list= fields;
3266  rc= join->result->send_data(*columns_list);
3267  }
3268  }
3269  else
3270  {
3271  assert(join->tables);
3272  error= sub_select(join,join_tab,0);
3273  if (error == NESTED_LOOP_OK || error == NESTED_LOOP_NO_MORE_ROWS)
3274  {
3275  error= sub_select(join,join_tab,1);
3276  }
3277 
3278  if (error == NESTED_LOOP_QUERY_LIMIT)
3279  {
3280  error= NESTED_LOOP_OK; /* select_limit used */
3281  }
3282  }
3283 
3284  if (error == NESTED_LOOP_NO_MORE_ROWS)
3285  {
3286  error= NESTED_LOOP_OK;
3287  }
3288 
3289  if (error == NESTED_LOOP_OK)
3290  {
3291  /*
3292  Sic: this branch works even if rc != 0, e.g. when
3293  send_data above returns an error.
3294  */
3295  if (!table) // If sending data to client
3296  {
3297  /*
3298  The following will unlock all cursors if the command wasn't an
3299  update command
3300  */
3301  join->join_free(); // Unlock all cursors
3302  if (join->result->send_eof())
3303  {
3304  rc= 1; // Don't send error
3305  }
3306  }
3307  }
3308  else
3309  {
3310  rc= -1;
3311  }
3312 
3313  if (table)
3314  {
3315  int tmp, new_errno= 0;
3316  if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
3317  {
3318  new_errno= tmp;
3319  }
3320 
3321  if ((tmp=table->cursor->ha_index_or_rnd_end()))
3322  {
3323  new_errno= tmp;
3324  }
3325 
3326  if (new_errno)
3327  {
3328  table->print_error(new_errno,MYF(0));
3329  }
3330  }
3331  return(join->session->is_error() ? -1 : rc);
3332 }
3333 
3334 enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records)
3335 {
3336  enum_nested_loop_state rc;
3337 
3338  if (end_of_records)
3339  {
3340  rc= flush_cached_records(join,join_tab,false);
3341  if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS)
3342  rc= sub_select(join,join_tab,end_of_records);
3343  return rc;
3344  }
3345 
3346  if (join->session->getKilled()) // If aborted by user
3347  {
3348  join->session->send_kill_message();
3349  return NESTED_LOOP_KILLED;
3350  }
3351 
3352  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3353  {
3354  if (! join_tab->cache.store_record_in_cache())
3355  return NESTED_LOOP_OK; // There is more room in cache
3356  return flush_cached_records(join,join_tab,false);
3357  }
3358  rc= flush_cached_records(join, join_tab, true);
3359  if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS)
3360  {
3361  rc= sub_select(join, join_tab, end_of_records);
3362  }
3363 
3364  return rc;
3365 }
3366 
3486 enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
3487 {
3488  join_tab->table->null_row=0;
3489  if (end_of_records)
3490  {
3491  return (*join_tab->next_select)(join,join_tab+1,end_of_records);
3492  }
3493 
3494  int error;
3496  ReadRecord *info= &join_tab->read_record;
3497 
3498  if (join->resume_nested_loop)
3499  {
3500  /* If not the last table, plunge down the nested loop */
3501  if (join_tab < join->join_tab + join->tables - 1)
3502  {
3503  rc= (*join_tab->next_select)(join, join_tab + 1, 0);
3504  }
3505  else
3506  {
3507  join->resume_nested_loop= false;
3508  rc= NESTED_LOOP_OK;
3509  }
3510  }
3511  else
3512  {
3513  join->return_tab= join_tab;
3514 
3515  if (join_tab->last_inner)
3516  {
3517  /* join_tab is the first inner table for an outer join operation. */
3518 
3519  /* Set initial state of guard variables for this table.*/
3520  join_tab->found=0;
3521  join_tab->not_null_compl= 1;
3522 
3523  /* Set first_unmatched for the last inner table of this group */
3524  join_tab->last_inner->first_unmatched= join_tab;
3525  }
3526  join->session->row_count= 0;
3527 
3528  error= (*join_tab->read_first_record)(join_tab);
3529  rc= evaluate_join_record(join, join_tab, error);
3530  }
3531 
3532  /*
3533  Note: psergey has added the 2nd part of the following condition; the
3534  change should probably be made in 5.1, too.
3535  */
3536  while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
3537  {
3538  error= info->read_record(info);
3539  rc= evaluate_join_record(join, join_tab, error);
3540  }
3541 
3542  if (rc == NESTED_LOOP_NO_MORE_ROWS and join_tab->last_inner && !join_tab->found)
3543  {
3544  rc= evaluate_null_complemented_join_record(join, join_tab);
3545  }
3546 
3547  if (rc == NESTED_LOOP_NO_MORE_ROWS)
3548  {
3549  rc= NESTED_LOOP_OK;
3550  }
3551 
3552  return rc;
3553 }
3554 
3555 int safe_index_read(JoinTable *tab)
3556 {
3557  int error;
3558  Table *table= tab->table;
3559  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
3560  tab->ref.key_buff,
3561  make_prev_keypart_map(tab->ref.key_parts),
3562  HA_READ_KEY_EXACT)))
3563  return table->report_error(error);
3564  return 0;
3565 }
3566 
3580 {
3581  int error;
3582  Table *table= tab->table;
3583  if (table->status & STATUS_GARBAGE) // If first read
3584  {
3585  table->status= 0;
3586  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3587  {
3588  error= HA_ERR_KEY_NOT_FOUND;
3589  }
3590  else
3591  {
3592  error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
3593  (unsigned char*) tab->ref.key_buff,
3594  make_prev_keypart_map(tab->ref.key_parts),
3595  HA_READ_KEY_EXACT);
3596  }
3597  if (error)
3598  {
3599  table->status= STATUS_NOT_FOUND;
3600  tab->table->mark_as_null_row();
3601  table->emptyRecord();
3602  if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3603  {
3604  return table->report_error(error);
3605  }
3606  return -1;
3607  }
3608  table->storeRecord();
3609  }
3610  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
3611  {
3612  table->status=0;
3613  table->restoreRecord(); // restore old record
3614  }
3615  table->null_row=0;
3616  return table->status ? -1 : 0;
3617 }
3618 
3619 /*
3620  eq_ref access method implementation: "read_first" function
3621 
3622  SYNOPSIS
3623  join_read_key()
3624  tab JoinTable of the accessed table
3625 
3626  DESCRIPTION
3627  This is "read_fist" function for the "ref" access method. The difference
3628  from "ref" is that it has a one-element "cache" (see cmp_buffer_with_ref)
3629 
3630  RETURN
3631  0 - Ok
3632  -1 - Row not found
3633  1 - Error
3634 */
3635 int join_read_key(JoinTable *tab)
3636 {
3637  int error;
3638  Table *table= tab->table;
3639 
3640  if (!table->cursor->inited)
3641  {
3642  error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3643  if (error != 0)
3644  {
3645  table->print_error(error, MYF(0));
3646  }
3647  }
3648 
3649  /* @todo Why don't we do "Late NULLs Filtering" here? */
3650  if (cmp_buffer_with_ref(tab) ||
3651  (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
3652  {
3653  if (tab->ref.key_err)
3654  {
3655  table->status=STATUS_NOT_FOUND;
3656  return -1;
3657  }
3658  error=table->cursor->index_read_map(table->getInsertRecord(),
3659  tab->ref.key_buff,
3660  make_prev_keypart_map(tab->ref.key_parts),
3661  HA_READ_KEY_EXACT);
3662  if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3663  {
3664  return table->report_error(error);
3665  }
3666  }
3667  table->null_row=0;
3668  return table->status ? -1 : 0;
3669 }
3670 
3671 /*
3672  ref access method implementation: "read_first" function
3673 
3674  SYNOPSIS
3675  join_read_always_key()
3676  tab JoinTable of the accessed table
3677 
3678  DESCRIPTION
3679  This is "read_first" function for the "ref" access method.
3680 
3681  The functon must leave the index initialized when it returns.
3682  ref_or_null access implementation depends on that.
3683 
3684  RETURN
3685  0 - Ok
3686  -1 - Row not found
3687  1 - Error
3688 */
3689 int join_read_always_key(JoinTable *tab)
3690 {
3691  int error;
3692  Table *table= tab->table;
3693 
3694  /* Initialize the index first */
3695  if (!table->cursor->inited)
3696  {
3697  error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3698  if (error != 0)
3699  {
3700  return table->report_error(error);
3701  }
3702  }
3703 
3704  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3705  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
3706  {
3707  if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
3708  {
3709  return -1;
3710  }
3711  }
3712 
3713  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3714  {
3715  return -1;
3716  }
3717 
3718  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
3719  tab->ref.key_buff,
3720  make_prev_keypart_map(tab->ref.key_parts),
3721  HA_READ_KEY_EXACT)))
3722  {
3723  if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3724  {
3725  return table->report_error(error);
3726  }
3727  return -1;
3728  }
3729 
3730  return 0;
3731 }
3732 
3738 {
3739  int error;
3740  Table *table= tab->table;
3741 
3742  if (!table->cursor->inited)
3743  {
3744  error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3745  if (error != 0)
3746  return table->report_error(error);
3747  }
3748 
3749  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3750  {
3751  return -1;
3752  }
3753 
3754  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
3755  tab->ref.key_buff,
3756  make_prev_keypart_map(tab->ref.key_parts))))
3757  {
3758  if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3759  {
3760  return table->report_error(error);
3761  }
3762  return -1;
3763  }
3764 
3765  return 0;
3766 }
3767 
3768 int join_no_more_records(ReadRecord *)
3769 {
3770  return -1;
3771 }
3772 
3773 int join_read_next_same_diff(ReadRecord *info)
3774 {
3775  Table *table= info->table;
3776  JoinTable *tab=table->reginfo.join_tab;
3777  if (tab->insideout_match_tab->found_match)
3778  {
3779  KeyInfo *key= tab->table->key_info + tab->index;
3780  do
3781  {
3782  int error;
3783  /* Save index tuple from record to the buffer */
3784  key_copy(tab->insideout_buf, info->record, key, 0);
3785 
3786  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
3787  tab->ref.key_buff,
3788  tab->ref.key_length)))
3789  {
3790  if (error != HA_ERR_END_OF_FILE)
3791  return table->report_error(error);
3792  table->status= STATUS_GARBAGE;
3793  return -1;
3794  }
3795  } while (!key_cmp(tab->table->key_info[tab->index].key_part,
3796  tab->insideout_buf, key->key_length));
3797  tab->insideout_match_tab->found_match= 0;
3798  return 0;
3799  }
3800  else
3801  {
3802  return join_read_next_same(info);
3803  }
3804 }
3805 
3806 int join_read_next_same(ReadRecord *info)
3807 {
3808  int error;
3809  Table *table= info->table;
3810  JoinTable *tab=table->reginfo.join_tab;
3811 
3812  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
3813  tab->ref.key_buff,
3814  tab->ref.key_length)))
3815  {
3816  if (error != HA_ERR_END_OF_FILE)
3817  return table->report_error(error);
3818  table->status= STATUS_GARBAGE;
3819  return -1;
3820  }
3821 
3822  return 0;
3823 }
3824 
3825 int join_read_prev_same(ReadRecord *info)
3826 {
3827  int error;
3828  Table *table= info->table;
3829  JoinTable *tab=table->reginfo.join_tab;
3830 
3831  if ((error=table->cursor->index_prev(table->getInsertRecord())))
3832  {
3833  return table->report_error(error);
3834  }
3835 
3836  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3837  tab->ref.key_length))
3838  {
3839  table->status=STATUS_NOT_FOUND;
3840  error= -1;
3841  }
3842 
3843  return error;
3844 }
3845 
3846 int join_init_quick_read_record(JoinTable *tab)
3847 {
3848  if (test_if_quick_select(tab) == -1)
3849  {
3850  return -1; /* No possible records */
3851  }
3852 
3853  return join_init_read_record(tab);
3854 }
3855 
3856 int init_read_record_seq(JoinTable *tab)
3857 {
3858  tab->read_record.init_reard_record_sequential();
3859 
3860  if (tab->read_record.cursor->startTableScan(1))
3861  {
3862  return 1;
3863  }
3864  return (*tab->read_record.read_record)(&tab->read_record);
3865 }
3866 
3867 int test_if_quick_select(JoinTable *tab)
3868 {
3869  safe_delete(tab->select->quick);
3870 
3871  return tab->select->test_quick_select(tab->join->session, tab->keys,
3872  (table_map) 0, HA_POS_ERROR, 0, false);
3873 }
3874 
3875 int join_init_read_record(JoinTable *tab)
3876 {
3877  if (tab->select && tab->select->quick && tab->select->quick->reset())
3878  {
3879  return 1;
3880  }
3881 
3882  if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true))
3883  {
3884  return 1;
3885  }
3886 
3887  return (*tab->read_record.read_record)(&tab->read_record);
3888 }
3889 
3890 int join_read_first(JoinTable *tab)
3891 {
3892  int error;
3893  Table *table=tab->table;
3894  if (!table->key_read && table->covering_keys.test(tab->index) &&
3895  !table->no_keyread)
3896  {
3897  table->key_read= 1;
3898  table->cursor->extra(HA_EXTRA_KEYREAD);
3899  }
3900  tab->table->status= 0;
3901  tab->read_record.table=table;
3902  tab->read_record.cursor=table->cursor;
3903  tab->read_record.index=tab->index;
3904  tab->read_record.record=table->getInsertRecord();
3905  if (tab->insideout_match_tab)
3906  {
3907  tab->read_record.do_insideout_scan= tab;
3908  tab->read_record.read_record=join_read_next_different;
3909  tab->insideout_match_tab->found_match= 0;
3910  }
3911  else
3912  {
3913  tab->read_record.read_record=join_read_next;
3914  tab->read_record.do_insideout_scan= 0;
3915  }
3916 
3917  if (!table->cursor->inited)
3918  {
3919  error= table->cursor->startIndexScan(tab->index, tab->sorted);
3920  if (error != 0)
3921  {
3922  table->report_error(error);
3923  return -1;
3924  }
3925  }
3926  if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
3927  {
3928  if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3929  table->report_error(error);
3930  return -1;
3931  }
3932 
3933  return 0;
3934 }
3935 
3936 int join_read_next_different(ReadRecord *info)
3937 {
3938  JoinTable *tab= info->do_insideout_scan;
3939  if (tab->insideout_match_tab->found_match)
3940  {
3941  KeyInfo *key= tab->table->key_info + tab->index;
3942  do
3943  {
3944  int error;
3945  /* Save index tuple from record to the buffer */
3946  key_copy(tab->insideout_buf, info->record, key, 0);
3947 
3948  if ((error=info->cursor->index_next(info->record)))
3949  {
3950  return info->table->report_error(error);
3951  }
3952  } while (!key_cmp(tab->table->key_info[tab->index].key_part,
3953  tab->insideout_buf, key->key_length));
3954  tab->insideout_match_tab->found_match= 0;
3955  return 0;
3956  }
3957  else
3958  {
3959  return join_read_next(info);
3960  }
3961 }
3962 
3963 int join_read_next(ReadRecord *info)
3964 {
3965  int error;
3966  if ((error=info->cursor->index_next(info->record)))
3967  {
3968  return info->table->report_error(error);
3969  }
3970  return 0;
3971 }
3972 
3973 int join_read_last(JoinTable *tab)
3974 {
3975  Table *table=tab->table;
3976  int error;
3977  if (!table->key_read && table->covering_keys.test(tab->index) &&
3978  !table->no_keyread)
3979  {
3980  table->key_read=1;
3981  table->cursor->extra(HA_EXTRA_KEYREAD);
3982  }
3983  tab->table->status=0;
3984  tab->read_record.read_record=join_read_prev;
3985  tab->read_record.table=table;
3986  tab->read_record.cursor=table->cursor;
3987  tab->read_record.index=tab->index;
3988  tab->read_record.record=table->getInsertRecord();
3989 
3990  if (!table->cursor->inited)
3991  {
3992  error= table->cursor->startIndexScan(tab->index, 1);
3993  if (error != 0)
3994  {
3995  return table->report_error(error);
3996  }
3997  }
3998 
3999  if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
4000  {
4001  return table->report_error(error);
4002  }
4003 
4004  return 0;
4005 }
4006 
4007 int join_read_prev(ReadRecord *info)
4008 {
4009  int error;
4010  if ((error= info->cursor->index_prev(info->record)))
4011  {
4012  return info->table->report_error(error);
4013  }
4014 
4015  return 0;
4016 }
4017 
4022 {
4023  int res;
4024 
4025  /* First read according to key which is NOT NULL */
4026  *tab->ref.null_ref_key= 0; // Clear null byte
4027  if ((res= join_read_always_key(tab)) >= 0)
4028  {
4029  return res;
4030  }
4031 
4032  /* Then read key with null value */
4033  *tab->ref.null_ref_key= 1; // Set null byte
4034  return safe_index_read(tab);
4035 }
4036 
4037 int join_read_next_same_or_null(ReadRecord *info)
4038 {
4039  int error;
4040  if ((error= join_read_next_same(info)) >= 0)
4041  {
4042  return error;
4043  }
4044  JoinTable *tab= info->table->reginfo.join_tab;
4045 
4046  /* Test if we have already done a read after null key */
4047  if (*tab->ref.null_ref_key)
4048  {
4049  return -1; // All keys read
4050  }
4051 
4052  *tab->ref.null_ref_key= 1; // Set null byte
4053 
4054  return safe_index_read(tab); // then read null keys
4055 }
4056 
4057 enum_nested_loop_state end_send_group(Join *join, JoinTable *, bool end_of_records)
4058 {
4059  int idx= -1;
4060  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
4061 
4062  if (!join->first_record or end_of_records or (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
4063  {
4064  if (join->first_record or
4065  (end_of_records && !join->group && !join->group_optimized_away))
4066  {
4067  if (idx < (int) join->send_group_parts)
4068  {
4069  int error=0;
4070  {
4071  if (!join->first_record)
4072  {
4073  List<Item>::iterator it(join->fields->begin());
4074  Item *item;
4075  /* No matching rows for group function */
4076  join->clear();
4077 
4078  while ((item= it++))
4079  {
4080  item->no_rows_in_result();
4081  }
4082  }
4083  if (join->having && join->having->val_int() == 0)
4084  {
4085  error= -1; // Didn't satisfy having
4086  }
4087  else
4088  {
4089  if (join->do_send_rows)
4090  error=join->result->send_data(*join->fields) ? 1 : 0;
4091  join->send_records++;
4092  }
4093  if (join->rollup.getState() != Rollup::STATE_NONE && error <= 0)
4094  {
4095  if (join->rollup_send_data((uint32_t) (idx+1)))
4096  error= 1;
4097  }
4098  }
4099 
4100  if (error > 0)
4101  {
4102  return(NESTED_LOOP_ERROR);
4103  }
4104 
4105  if (end_of_records)
4106  {
4107  return(NESTED_LOOP_OK);
4108  }
4109 
4110  if (join->send_records >= join->unit->select_limit_cnt &&
4111  join->do_send_rows)
4112  {
4113  if (!(join->select_options & OPTION_FOUND_ROWS))
4114  {
4115  return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
4116  }
4117  join->do_send_rows=0;
4118  join->unit->select_limit_cnt = HA_POS_ERROR;
4119  }
4120  else if (join->send_records >= join->fetch_limit)
4121  {
4122  /*
4123  There is a server side cursor and all rows
4124  for this fetch request are sent.
4125  */
4126  /*
4127  Preventing code duplication. When finished with the group reset
4128  the group functions and copy_fields. We fall through. bug #11904
4129  */
4130  ok_code= NESTED_LOOP_CURSOR_LIMIT;
4131  }
4132  }
4133  }
4134  else
4135  {
4136  if (end_of_records)
4137  {
4138  return(NESTED_LOOP_OK);
4139  }
4140  join->first_record=1;
4141  test_if_item_cache_changed(join->group_fields);
4142  }
4143  if (idx < (int) join->send_group_parts)
4144  {
4145  /*
4146  This branch is executed also for cursors which have finished their
4147  fetch limit - the reason for ok_code.
4148  */
4149  copy_fields(&join->tmp_table_param);
4150  if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4151  {
4152  return(NESTED_LOOP_ERROR);
4153  }
4154  return(ok_code);
4155  }
4156  }
4157  if (update_sum_func(join->sum_funcs))
4158  return(NESTED_LOOP_ERROR);
4159  return(NESTED_LOOP_OK);
4160 }
4161 
4162 enum_nested_loop_state end_write_group(Join *join, JoinTable *, bool end_of_records)
4163 {
4164  Table *table=join->tmp_table;
4165  int idx= -1;
4166 
4167  if (join->session->getKilled())
4168  { // Aborted by user
4169  join->session->send_kill_message();
4170  return NESTED_LOOP_KILLED;
4171  }
4172 
4173  if (!join->first_record or end_of_records or (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
4174  {
4175  if (join->first_record or (end_of_records && !join->group))
4176  {
4177  int send_group_parts= join->send_group_parts;
4178  if (idx < send_group_parts)
4179  {
4180  if (!join->first_record)
4181  {
4182  /* No matching rows for group function */
4183  join->clear();
4184  }
4185 
4186  copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
4187 
4188  if (!join->having || join->having->val_int())
4189  {
4190  int error= table->cursor->insertRecord(table->getInsertRecord());
4191 
4192  if (error)
4193  {
4194  my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
4195  return NESTED_LOOP_ERROR;
4196  }
4197  }
4198 
4199  if (join->rollup.getState() != Rollup::STATE_NONE)
4200  {
4201  if (join->rollup_write_data((uint32_t) (idx+1), table))
4202  {
4203  return NESTED_LOOP_ERROR;
4204  }
4205  }
4206 
4207  if (end_of_records)
4208  {
4209  return NESTED_LOOP_OK;
4210  }
4211  }
4212  }
4213  else
4214  {
4215  if (end_of_records)
4216  {
4217  return NESTED_LOOP_OK;
4218  }
4219  join->first_record=1;
4220  test_if_item_cache_changed(join->group_fields);
4221  }
4222  if (idx < (int) join->send_group_parts)
4223  {
4224  copy_fields(&join->tmp_table_param);
4225  if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
4226  {
4227  return NESTED_LOOP_ERROR;
4228  }
4229 
4230  if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4231  {
4232  return NESTED_LOOP_ERROR;
4233  }
4234 
4235  return NESTED_LOOP_OK;
4236  }
4237  }
4238 
4239  if (update_sum_func(join->sum_funcs))
4240  {
4241  return NESTED_LOOP_ERROR;
4242  }
4243 
4244  return NESTED_LOOP_OK;
4245 }
4246 
4247 /*****************************************************************************
4248  Remove calculation with tables that aren't yet read. Remove also tests
4249  against fields that are read through key where the table is not a
4250  outer join table.
4251  We can't remove tests that are made against columns which are stored
4252  in sorted order.
4253  @return
4254  1 if right_item used is a removable reference key on left_item
4255  0 otherwise.
4256 ****************************************************************************/
4257 bool test_if_ref(Item_field *left_item,Item *right_item)
4258 {
4259  Field *field=left_item->field;
4260  // No need to change const test. We also have to keep tests on LEFT JOIN
4261  if (not field->getTable()->const_table && !field->getTable()->maybe_null)
4262  {
4263  Item *ref_item=part_of_refkey(field->getTable(),field);
4264  if (ref_item && ref_item->eq(right_item,1))
4265  {
4266  right_item= right_item->real_item();
4267  if (right_item->type() == Item::FIELD_ITEM)
4268  return (field->eq_def(((Item_field *) right_item)->field));
4269  /* remove equalities injected by IN->EXISTS transformation */
4270  else if (right_item->type() == Item::CACHE_ITEM)
4271  return ((Item_cache *)right_item)->eq_def (field);
4272  if (right_item->const_item() && !(right_item->is_null()))
4273  {
4274  /*
4275  We can remove binary fields and numerical fields except float,
4276  as float comparison isn't 100 % secure
4277  We have to keep normal strings to be able to check for end spaces
4278 
4279  sergefp: the above seems to be too restrictive. Counterexample:
4280  create table t100 (v varchar(10), key(v)) default charset=latin1;
4281  insert into t100 values ('a'),('a ');
4282  explain select * from t100 where v='a';
4283  The EXPLAIN shows 'using Where'. Running the query returns both
4284  rows, so it seems there are no problems with endspace in the most
4285  frequent case?
4286  */
4287  if (field->binary() &&
4288  field->real_type() != DRIZZLE_TYPE_VARCHAR &&
4289  field->decimals() == 0)
4290  {
4291  return ! store_val_in_field(field, right_item, CHECK_FIELD_WARN);
4292  }
4293  }
4294  }
4295  }
4296  return 0;
4297 }
4298 
4299 /*
4300  Extract a condition that can be checked after reading given table
4301 
4302  SYNOPSIS
4303  make_cond_for_table()
4304  cond Condition to analyze
4305  tables Tables for which "current field values" are available
4306  used_table Table that we're extracting the condition for (may
4307  also include PSEUDO_TABLE_BITS
4308 
4309  DESCRIPTION
4310  Extract the condition that can be checked after reading the table
4311  specified in 'used_table', given that current-field values for tables
4312  specified in 'tables' bitmap are available.
4313 
4314  The function assumes that
4315  - Constant parts of the condition has already been checked.
4316  - Condition that could be checked for tables in 'tables' has already
4317  been checked.
4318 
4319  The function takes into account that some parts of the condition are
4320  guaranteed to be true by employed 'ref' access methods (the code that
4321  does this is located at the end, search down for "EQ_FUNC").
4322 
4323 
4324  SEE ALSO
4325  make_cond_for_info_schema uses similar algorithm
4326 
4327  RETURN
4328  Extracted condition
4329 */
4330 COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
4331 {
4332  if (used_table && !(cond->used_tables() & used_table) &&
4333  /*
4334  Exclude constant conditions not checked at optimization time if
4335  the table we are pushing conditions to is the first one.
4336  As a result, such conditions are not considered as already checked
4337  and will be checked at execution time, attached to the first table.
4338  */
4339  !((used_table & 1) && cond->is_expensive()))
4340  {
4341  return (COND*) 0; // Already checked
4342  }
4343 
4344  if (cond->type() == Item::COND_ITEM)
4345  {
4346  if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
4347  {
4348  /* Create new top level AND item */
4349  Item_cond_and *new_cond=new Item_cond_and;
4350  if (new_cond == NULL)
4351  {
4352  return (COND*) 0;
4353  }
4354  List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
4355  Item *item;
4356  while ((item=li++))
4357  {
4358  Item *fix= make_cond_for_table(item,tables,used_table,
4359  exclude_expensive_cond);
4360  if (fix)
4361  {
4362  new_cond->argument_list()->push_back(fix);
4363  }
4364  }
4365  switch (new_cond->argument_list()->size())
4366  {
4367  case 0:
4368  return (COND*) 0; // Always true
4369 
4370  case 1:
4371  return &new_cond->argument_list()->front();
4372 
4373  default:
4374  /*
4375  Item_cond_and do not need fix_fields for execution, its parameters
4376  are fixed or do not need fix_fields, too
4377  */
4378  new_cond->quick_fix_field();
4379  new_cond->used_tables_cache= ((Item_cond_and*) cond)->used_tables_cache & tables;
4380  return new_cond;
4381  }
4382  }
4383  else
4384  { // Or list
4385  Item_cond_or *new_cond=new Item_cond_or;
4386  if (new_cond == NULL)
4387  {
4388  return (COND*) 0;
4389  }
4390  List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
4391  Item *item;
4392  while ((item=li++))
4393  {
4394  Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
4395  if (!fix)
4396  {
4397  return (COND*) 0; // Always true
4398  }
4399  new_cond->argument_list()->push_back(fix);
4400  }
4401  /*
4402  Item_cond_and do not need fix_fields for execution, its parameters
4403  are fixed or do not need fix_fields, too
4404  */
4405  new_cond->quick_fix_field();
4406  new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4407  new_cond->top_level_item();
4408 
4409  return new_cond;
4410  }
4411  }
4412 
4413  /*
4414  Because the following test takes a while and it can be done
4415  table_count times, we mark each item that we have examined with the result
4416  of the test
4417  */
4418 
4419  if (cond->marker == 3 || (cond->used_tables() & ~tables) ||
4420  /*
4421  When extracting constant conditions, treat expensive conditions as
4422  non-constant, so that they are not evaluated at optimization time.
4423  */
4424  (!used_table && exclude_expensive_cond && cond->is_expensive()))
4425  {
4426  return (COND*) 0; // Can't check this yet
4427  }
4428 
4429  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
4430  {
4431  return cond; // Not boolean op
4432  }
4433 
4434  /*
4435  Remove equalities that are guaranteed to be true by use of 'ref' access
4436  method
4437  */
4438  if (((Item_func*) cond)->functype() == Item_func::EQ_FUNC)
4439  {
4440  Item *left_item= ((Item_func*) cond)->arguments()[0];
4441  Item *right_item= ((Item_func*) cond)->arguments()[1];
4442  if (left_item->type() == Item::FIELD_ITEM && test_if_ref((Item_field*) left_item,right_item))
4443  {
4444  cond->marker=3; // Checked when read
4445  return (COND*) 0;
4446  }
4447  if (right_item->type() == Item::FIELD_ITEM && test_if_ref((Item_field*) right_item,left_item))
4448  {
4449  cond->marker=3; // Checked when read
4450  return (COND*) 0;
4451  }
4452  }
4453  cond->marker=2;
4454  return cond;
4455 }
4456 
4457 static Item *part_of_refkey(Table *table,Field *field)
4458 {
4459  if (!table->reginfo.join_tab)
4460  {
4461  return (Item*) 0; // field from outer non-select (UPDATE,...)
4462  }
4463 
4464  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
4465  if (ref_parts)
4466  {
4467  KeyPartInfo *key_part=
4468  table->key_info[table->reginfo.join_tab->ref.key].key_part;
4469  uint32_t part;
4470 
4471  for (part=0 ; part < ref_parts ; part++)
4472  {
4473  if (table->reginfo.join_tab->ref.cond_guards[part])
4474  {
4475  return 0;
4476  }
4477  }
4478 
4479  for (part=0 ; part < ref_parts ; part++,key_part++)
4480  {
4481  if (field->eq(key_part->field) &&
4482  !(key_part->key_part_flag & HA_PART_KEY_SEG) &&
4483  //If field can be NULL, we should not remove this predicate, as
4484  //it may lead to non-rejection of NULL values.
4485  !(field->real_maybe_null()))
4486  {
4487  return table->reginfo.join_tab->ref.items[part];
4488  }
4489  }
4490  }
4491 
4492  return (Item*) 0;
4493 }
4494 
4521 static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
4522 {
4523  KeyPartInfo *key_part= NULL;
4524  KeyPartInfo *key_part_end= NULL;
4525  key_part= table->key_info[idx].key_part;
4526  key_part_end= key_part + table->key_info[idx].key_parts;
4527  key_part_map const_key_parts=table->const_key_parts[idx];
4528  int reverse= 0;
4529  bool on_primary_key= false;
4530 
4531  for (; order ; order=order->next, const_key_parts>>=1)
4532  {
4533  Field *field=((Item_field*) (*order->item)->real_item())->field;
4534  int flag;
4535 
4536  /*
4537  Skip key parts that are constants in the WHERE clause.
4538  These are already skipped in the ORDER BY by const_expression_in_where()
4539  */
4540  for (; const_key_parts & 1 ; const_key_parts>>= 1)
4541  {
4542  key_part++;
4543  }
4544 
4545  if (key_part == key_part_end)
4546  {
4547  /*
4548  We are at the end of the key. Check if the engine has the primary
4549  key as a suffix to the secondary keys. If it has continue to check
4550  the primary key as a suffix.
4551  */
4552  if (!on_primary_key &&
4553  (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4554  table->getShare()->hasPrimaryKey())
4555  {
4556  on_primary_key= true;
4557  key_part= table->key_info[table->getShare()->getPrimaryKey()].key_part;
4558  key_part_end=key_part+table->key_info[table->getShare()->getPrimaryKey()].key_parts;
4559  const_key_parts=table->const_key_parts[table->getShare()->getPrimaryKey()];
4560 
4561  for (; const_key_parts & 1 ; const_key_parts>>= 1)
4562  {
4563  key_part++;
4564  }
4565 
4566  /*
4567  The primary and secondary key parts were all const (i.e. there's
4568  one row). The sorting doesn't matter.
4569  */
4570  if (key_part == key_part_end && reverse == 0)
4571  {
4572  return 1;
4573  }
4574  }
4575  else
4576  {
4577  return 0;
4578  }
4579  }
4580 
4581  if (key_part->field != field)
4582  {
4583  return 0;
4584  }
4585 
4586  /* set flag to 1 if we can use read-next on key, else to -1 */
4587  flag= ((order->asc == !(key_part->key_part_flag & HA_REVERSE_SORT)) ?
4588  1 : -1);
4589  if (reverse && flag != reverse)
4590  {
4591  return 0;
4592  }
4593  reverse=flag; // Remember if reverse
4594  key_part++;
4595  }
4596  if (on_primary_key)
4597  {
4598  uint32_t used_key_parts_secondary= table->key_info[idx].key_parts;
4599  uint32_t used_key_parts_pk= (uint32_t) (key_part - table->key_info[table->getShare()->getPrimaryKey()].key_part);
4600  *used_key_parts= used_key_parts_pk + used_key_parts_secondary;
4601 
4602  if (reverse == -1 &&
4603  (!(table->index_flags(idx) &
4604  HA_READ_PREV) ||
4605  !(table->index_flags(table->getShare()->getPrimaryKey()) &
4606  HA_READ_PREV)))
4607  reverse= 0; // Index can't be used
4608  }
4609  else
4610  {
4611  *used_key_parts= (uint32_t) (key_part - table->key_info[idx].key_part);
4612  if (reverse == -1 &&
4613  !(table->index_flags(idx) & HA_READ_PREV))
4614  reverse= 0; // Index can't be used
4615  }
4616  return(reverse);
4617 }
4618 
4634 inline bool is_subkey(KeyPartInfo *key_part,
4635  KeyPartInfo *ref_key_part,
4636  KeyPartInfo *ref_key_part_end)
4637 {
4638  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4639  {
4640  if (! key_part->field->eq(ref_key_part->field))
4641  {
4642  return 0;
4643  }
4644  }
4645 
4646  return 1;
4647 }
4648 
4660 static uint32_t test_if_subkey(Order *order,
4661  Table *table,
4662  uint32_t ref,
4663  uint32_t ref_key_parts,
4664  const key_map *usable_keys)
4665 {
4666  uint32_t nr;
4667  uint32_t min_length= UINT32_MAX;
4668  uint32_t best= MAX_KEY;
4669  uint32_t not_used;
4670  KeyPartInfo *ref_key_part= table->key_info[ref].key_part;
4671  KeyPartInfo *ref_key_part_end= ref_key_part + ref_key_parts;
4672 
4673  for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
4674  {
4675  if (usable_keys->test(nr) &&
4676  table->key_info[nr].key_length < min_length &&
4677  table->key_info[nr].key_parts >= ref_key_parts &&
4678  is_subkey(table->key_info[nr].key_part, ref_key_part,
4679  ref_key_part_end) &&
4680  test_if_order_by_key(order, table, nr, &not_used))
4681  {
4682  min_length= table->key_info[nr].key_length;
4683  best= nr;
4684  }
4685  }
4686  return best;
4687 }
4688 
4720 bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
4721 {
4722  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
4723  {
4724  if (keynr == table->getShare()->getPrimaryKey() ||
4725  (table->key_info[keynr].flags & HA_NOSAME))
4726  {
4727  KeyInfo *keyinfo= table->key_info + keynr;
4728  KeyPartInfo *key_part= NULL;
4729  KeyPartInfo *key_part_end= NULL;
4730 
4731  for (key_part=keyinfo->key_part,
4732  key_part_end=key_part+ keyinfo->key_parts;
4733  key_part < key_part_end;
4734  key_part++)
4735  {
4736  if (key_part->field->maybe_null() || ! find_func(key_part->field, data))
4737  {
4738  break;
4739  }
4740  }
4741 
4742  if (key_part == key_part_end)
4743  {
4744  return 1;
4745  }
4746  }
4747  }
4748  return 0;
4749 }
4750 
4764 bool find_field_in_order_list (Field *field, void *data)
4765 {
4766  Order *group= (Order *) data;
4767  bool part_found= 0;
4768  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4769  {
4770  Item *item= (*tmp_group->item)->real_item();
4771  if (item->type() == Item::FIELD_ITEM &&
4772  ((Item_field*) item)->field->eq(field))
4773  {
4774  part_found= 1;
4775  break;
4776  }
4777  }
4778  return part_found;
4779 }
4780 
4794 bool find_field_in_item_list (Field *field, void *data)
4795 {
4796  List<Item> *fields= (List<Item> *) data;
4797  bool part_found= 0;
4798  List<Item>::iterator li(fields->begin());
4799  Item *item;
4800 
4801  while ((item= li++))
4802  {
4803  if (item->type() == Item::FIELD_ITEM &&
4804  ((Item_field*) item)->field->eq(field))
4805  {
4806  part_found= 1;
4807  break;
4808  }
4809  }
4810  return part_found;
4811 }
4812 
4838 bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
4839 {
4840  int32_t ref_key;
4841  uint32_t ref_key_parts;
4842  int order_direction;
4843  uint32_t used_key_parts;
4844  Table *table=tab->table;
4845  optimizer::SqlSelect *select= tab->select;
4846  key_map usable_keys;
4847  optimizer::QuickSelectInterface *save_quick= NULL;
4848 
4849  /*
4850  Keys disabled by ALTER Table ... DISABLE KEYS should have already
4851  been taken into account.
4852  */
4853  usable_keys= *map;
4854 
4855  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4856  {
4857  Item *item= (*tmp_order->item)->real_item();
4858  if (item->type() != Item::FIELD_ITEM)
4859  {
4860  usable_keys.reset();
4861  return 0;
4862  }
4863  usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
4864  if (usable_keys.none())
4865  {
4866  return 0; // No usable keys
4867  }
4868  }
4869 
4870  ref_key= -1;
4871  /* Test if constant range in WHERE */
4872  if (tab->ref.key >= 0 && tab->ref.key_parts)
4873  {
4874  ref_key= tab->ref.key;
4875  ref_key_parts= tab->ref.key_parts;
4876  if (tab->type == AM_REF_OR_NULL)
4877  {
4878  return 0;
4879  }
4880  }
4881  else if (select && select->quick) // Range found by optimizer/range
4882  {
4883  int quick_type= select->quick->get_type();
4884  save_quick= select->quick;
4885  /*
4886  assume results are not ordered when index merge is used
4887  @todo sergeyp: Results of all index merge selects actually are ordered
4888  by clustered PK values.
4889  */
4890 
4891  if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
4892  quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
4893  quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT)
4894  {
4895  return 0;
4896  }
4897  ref_key= select->quick->index;
4898  ref_key_parts= select->quick->used_key_parts;
4899  }
4900 
4901  if (ref_key >= 0)
4902  {
4903  /*
4904  We come here when there is a REF key.
4905  */
4906  if (! usable_keys.test(ref_key))
4907  {
4908  /*
4909  We come here when ref_key is not among usable_keys
4910  */
4911  uint32_t new_ref_key;
4912  /*
4913  If using index only read, only consider other possible index only
4914  keys
4915  */
4916  if (table->covering_keys.test(ref_key))
4917  usable_keys&= table->covering_keys;
4918 
4919  if (tab->pre_idx_push_select_cond)
4920  tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
4921 
4922  if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
4923  &usable_keys)) < MAX_KEY)
4924  {
4925  /* Found key that can be used to retrieve data in sorted order */
4926  if (tab->ref.key >= 0)
4927  {
4928  /*
4929  We'll use ref access method on key new_ref_key. In general case
4930  the index search tuple for new_ref_key will be different (e.g.
4931  when one index is defined as (part1, part2, ...) and another as
4932  (part1, part2(N), ...) and the WHERE clause contains
4933  "part1 = const1 AND part2=const2".
4934  So we build tab->ref from scratch here.
4935  */
4936  optimizer::KeyUse *keyuse= tab->keyuse;
4937  while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
4938  keyuse++;
4939 
4940  if (create_ref_for_key(tab->join, tab, keyuse, tab->join->const_table_map))
4941  {
4942  return 0;
4943  }
4944  }
4945  else
4946  {
4947  /*
4948  The range optimizer constructed QuickRange for ref_key, and
4949  we want to use instead new_ref_key as the index. We can't
4950  just change the index of the quick select, because this may
4951  result in an incosistent QUICK_SELECT object. Below we
4952  create a new QUICK_SELECT from scratch so that all its
4953  parameres are set correctly by the range optimizer.
4954  */
4955  key_map new_ref_key_map;
4956  new_ref_key_map.reset(); // Force the creation of quick select
4957  new_ref_key_map.set(new_ref_key); // only for new_ref_key.
4958 
4959  if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
4960  (tab->join->select_options &
4961  OPTION_FOUND_ROWS) ?
4962  HA_POS_ERROR :
4963  tab->join->unit->select_limit_cnt,0,
4964  true) <=
4965  0)
4966  {
4967  return 0;
4968  }
4969  }
4970  ref_key= new_ref_key;
4971  }
4972  }
4973  /* Check if we get the rows in requested sorted order by using the key */
4974  if (usable_keys.test(ref_key) &&
4975  (order_direction= test_if_order_by_key(order,table,ref_key,
4976  &used_key_parts)))
4977  goto check_reverse_order;
4978  }
4979  {
4980  /*
4981  Check whether there is an index compatible with the given order
4982  usage of which is cheaper than usage of the ref_key index (ref_key>=0)
4983  or a table scan.
4984  It may be the case if order_st/GROUP BY is used with LIMIT.
4985  */
4986  uint32_t nr;
4987  key_map keys;
4988  uint32_t best_key_parts= 0;
4989  int best_key_direction= 0;
4990  ha_rows best_records= 0;
4991  double read_time;
4992  int best_key= -1;
4993  bool is_best_covering= false;
4994  double fanout= 1;
4995  Join *join= tab->join;
4996  uint32_t tablenr= tab - join->join_tab;
4997  ha_rows table_records= table->cursor->stats.records;
4998  bool group= join->group && order == join->group_list;
4999  optimizer::Position cur_pos;
5000 
5001  /*
5002  If not used with LIMIT, only use keys if the whole query can be
5003  resolved with a key; This is because filesort() is usually faster than
5004  retrieving all rows through an index.
5005  */
5006  if (select_limit >= table_records)
5007  {
5008  /*
5009  filesort() and join cache are usually faster than reading in
5010  index order and not using join cache
5011  */
5012  if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
5013  {
5014  return 0;
5015  }
5016  keys= *table->cursor->keys_to_use_for_scanning();
5017  keys|= table->covering_keys;
5018 
5019  /*
5020  We are adding here also the index specified in FORCE INDEX clause,
5021  if any.
5022  This is to allow users to use index in order_st BY.
5023  */
5024  if (table->force_index)
5025  {
5026  keys|= (group ? table->keys_in_use_for_group_by :
5027  table->keys_in_use_for_order_by);
5028  }
5029  keys&= usable_keys;
5030  }
5031  else
5032  {
5033  keys= usable_keys;
5034  }
5035 
5036  cur_pos= join->getPosFromOptimalPlan(tablenr);
5037  read_time= cur_pos.getCost();
5038 
5039  for (uint32_t i= tablenr+1; i < join->tables; i++)
5040  {
5041  cur_pos= join->getPosFromOptimalPlan(i);
5042  fanout*= cur_pos.getFanout(); // fanout is always >= 1
5043  }
5044 
5045  for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
5046  {
5047  int direction;
5048  if (keys.test(nr) &&
5049  (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
5050  {
5051  bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
5052 
5053  /*
5054  Don't use an index scan with ORDER BY without limit.
5055  For GROUP BY without limit always use index scan
5056  if there is a suitable index.
5057  Why we hold to this asymmetry hardly can be explained
5058  rationally. It's easy to demonstrate that using
5059  temporary table + filesort could be cheaper for grouping
5060  queries too.
5061  */
5062  if (is_covering ||
5063  select_limit != HA_POS_ERROR ||
5064  (ref_key < 0 && (group || table->force_index)))
5065  {
5066  double rec_per_key;
5067  double index_scan_time;
5068  KeyInfo *keyinfo= tab->table->key_info+nr;
5069  if (select_limit == HA_POS_ERROR)
5070  {
5071  select_limit= table_records;
5072  }
5073 
5074  if (group)
5075  {
5076  rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
5077  set_if_bigger(rec_per_key, 1.0);
5078  /*
5079  With a grouping query each group containing on average
5080  rec_per_key records produces only one row that will
5081  be included into the result set.
5082  */
5083  if (select_limit > table_records/rec_per_key)
5084  {
5085  select_limit= table_records;
5086  }
5087  else
5088  {
5089  select_limit= (ha_rows) (select_limit*rec_per_key);
5090  }
5091  }
5092  /*
5093  If tab=tk is not the last joined table tn then to get first
5094  L records from the result set we can expect to retrieve
5095  only L/fanout(tk,tn) where fanout(tk,tn) says how many
5096  rows in the record set on average will match each row tk.
5097  Usually our estimates for fanouts are too pessimistic.
5098  So the estimate for L/fanout(tk,tn) will be too optimistic
5099  and as result we'll choose an index scan when using ref/range
5100  access + filesort will be cheaper.
5101  */
5102  select_limit= (ha_rows) (select_limit < fanout ?
5103  1 : select_limit/fanout);
5104  /*
5105  We assume that each of the tested indexes is not correlated
5106  with ref_key. Thus, to select first N records we have to scan
5107  N/selectivity(ref_key) index entries.
5108  selectivity(ref_key) = #scanned_records/#table_records =
5109  table->quick_condition_rows/table_records.
5110  In any case we can't select more than #table_records.
5111  N/(table->quick_condition_rows/table_records) > table_records
5112  <=> N > table->quick_condition_rows.
5113  */
5114  if (select_limit > table->quick_condition_rows)
5115  {
5116  select_limit= table_records;
5117  }
5118  else
5119  {
5120  select_limit= (ha_rows) (select_limit *
5121  (double) table_records /
5122  table->quick_condition_rows);
5123  }
5124  rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
5125  set_if_bigger(rec_per_key, 1.0);
5126  /*
5127  Here we take into account the fact that rows are
5128  accessed in sequences rec_per_key records in each.
5129  Rows in such a sequence are supposed to be ordered
5130  by rowid/primary key. When reading the data
5131  in a sequence we'll touch not more pages than the
5132  table cursor contains.
5133  TODO. Use the formula for a disk sweep sequential access
5134  to calculate the cost of accessing data rows for one
5135  index entry.
5136  */
5137  index_scan_time= select_limit/rec_per_key * min(rec_per_key, table->cursor->scan_time());
5138 
5139  if (is_covering || (ref_key < 0 && (group || table->force_index)) || index_scan_time < read_time)
5140  {
5141  ha_rows quick_records= table_records;
5142  if (is_best_covering && !is_covering)
5143  {
5144  continue;
5145  }
5146 
5147  if (table->quick_keys.test(nr))
5148  {
5149  quick_records= table->quick_rows[nr];
5150  }
5151 
5152  if (best_key < 0 ||
5153  (select_limit <= min(quick_records,best_records) ?
5154  keyinfo->key_parts < best_key_parts :
5155  quick_records < best_records))
5156  {
5157  best_key= nr;
5158  best_key_parts= keyinfo->key_parts;
5159  best_records= quick_records;
5160  is_best_covering= is_covering;
5161  best_key_direction= direction;
5162  }
5163  }
5164  }
5165  }
5166  }
5167 
5168  if (best_key >= 0)
5169  {
5170  bool quick_created= false;
5171  if (table->quick_keys.test(best_key) && best_key != ref_key)
5172  {
5173  key_map test_map;
5174  test_map.reset(); // Force the creation of quick select
5175  test_map.set(best_key); // only best_key.
5176  quick_created=
5177  select->test_quick_select(join->session, test_map, 0,
5178  join->select_options & OPTION_FOUND_ROWS ?
5179  HA_POS_ERROR :
5180  join->unit->select_limit_cnt,
5181  true, false) > 0;
5182  }
5183  if (no_changes == false)
5184  {
5185  if (!quick_created)
5186  {
5187  tab->index= best_key;
5188  tab->read_first_record= best_key_direction > 0 ?
5189  join_read_first : join_read_last;
5190  tab->type= AM_NEXT; // Read with index_first(), index_next()
5191  if (select && select->quick)
5192  {
5193  safe_delete(select->quick);
5194  }
5195  if (table->covering_keys.test(best_key))
5196  {
5197  table->key_read=1;
5198  table->cursor->extra(HA_EXTRA_KEYREAD);
5199  }
5200  table->cursor->ha_index_or_rnd_end();
5201  if (join->select_options & SELECT_DESCRIBE)
5202  {
5203  tab->ref.key= -1;
5204  tab->ref.key_parts= 0;
5205  if (select_limit < table_records)
5206  tab->limit= select_limit;
5207  }
5208  }
5209  else if (tab->type != AM_ALL)
5210  {
5211  /*
5212  We're about to use a quick access to the table.
5213  We need to change the access method so as the quick access
5214  method is actually used.
5215  */
5216  assert(tab->select->quick);
5217  tab->type= AM_ALL;
5218  tab->use_quick=1;
5219  tab->ref.key= -1;
5220  tab->ref.key_parts=0; // Don't use ref key.
5221  tab->read_first_record= join_init_read_record;
5222  }
5223  }
5224  used_key_parts= best_key_parts;
5225  order_direction= best_key_direction;
5226  }
5227  else
5228  {
5229  return 0;
5230  }
5231  }
5232 
5233 check_reverse_order:
5234  if (order_direction == -1) // If ORDER BY ... DESC
5235  {
5236  if (select && select->quick)
5237  {
5238  /*
5239  Don't reverse the sort order, if it's already done.
5240  (In some cases test_if_order_by_key() can be called multiple times
5241  */
5242  if (! select->quick->reverse_sorted())
5243  {
5245  bool error= false;
5246  int quick_type= select->quick->get_type();
5247  if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
5248  quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT ||
5249  quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
5250  quick_type == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX)
5251  {
5252  tab->limit= 0;
5253  select->quick= save_quick;
5254  return 0; // Use filesort
5255  }
5256 
5257  /* ORDER BY range_key DESC */
5259  used_key_parts,
5260  &error);
5261  if (! tmp || error)
5262  {
5263  delete tmp;
5264  select->quick= save_quick;
5265  tab->limit= 0;
5266  return 0; // Reverse sort not supported
5267  }
5268  select->quick= tmp;
5269  }
5270  }
5271  else if (tab->type != AM_NEXT &&
5272  tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
5273  {
5274  /*
5275  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
5276 
5277  Use a traversal function that starts by reading the last row
5278  with key part (A) and then traverse the index backwards.
5279  */
5280  tab->read_first_record= join_read_last_key;
5281  tab->read_record.read_record= join_read_prev_same;
5282  }
5283  }
5284  else if (select && select->quick)
5285  {
5286  select->quick->sorted= 1;
5287  }
5288 
5289  return 1;
5290 }
5291 
5292 /*
5293  If not selecting by given key, create an index how records should be read
5294 
5295  SYNOPSIS
5296  create_sort_index()
5297  session Thread Cursor
5298  tab Table to sort (in join structure)
5299  order How table should be sorted
5300  filesort_limit Max number of rows that needs to be sorted
5301  select_limit Max number of rows in final output
5302  Used to decide if we should use index or not
5303  is_order_by true if we are sorting on order_st BY, false if GROUP BY
5304  Used to decide if we should use index or not
5305 
5306 
5307  IMPLEMENTATION
5308  - If there is an index that can be used, 'tab' is modified to use
5309  this index.
5310  - If no index, create with filesort() an index cursor that can be used to
5311  retrieve rows in order (should be done with 'read_record').
5312  The sorted data is stored in tab->table and will be freed when calling
5313  tab->table->free_io_cache().
5314 
5315  RETURN VALUES
5316  0 ok
5317  -1 Some fatal error
5318  1 No records
5319 */
5320 int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
5321 {
5322  uint32_t length= 0;
5323  ha_rows examined_rows;
5324  Table *table;
5325  optimizer::SqlSelect *select= NULL;
5326  JoinTable *tab;
5327 
5328  if (join->tables == join->const_tables)
5329  return 0; // One row, no need to sort
5330  tab= join->join_tab + join->const_tables;
5331  table= tab->table;
5332  select= tab->select;
5333 
5334  /*
5335  When there is SQL_BIG_RESULT do not sort using index for GROUP BY,
5336  and thus force sorting on disk unless a group min-max optimization
5337  is going to be used as it is applied now only for one table queries
5338  with covering indexes.
5339  */
5340  if ((order != join->group_list ||
5341  !(join->select_options & SELECT_BIG_RESULT) ||
5342  (select && select->quick && (select->quick->get_type() == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX))) &&
5343  test_if_skip_sort_order(tab,order,select_limit,0,
5344  is_order_by ? &table->keys_in_use_for_order_by :
5345  &table->keys_in_use_for_group_by))
5346  {
5347  return 0;
5348  }
5349 
5350  for (Order *ord= join->order; ord; ord= ord->next)
5351  {
5352  length++;
5353  }
5354 
5355  join->sortorder= make_unireg_sortorder(order, &length, join->sortorder);
5356  table->sort.io_cache= new internal::io_cache_st;
5357  table->status=0; // May be wrong if quick_select
5358 
5359  // If table has a range, move it to select
5360  if (select && !select->quick && tab->ref.key >= 0)
5361  {
5362  if (tab->quick)
5363  {
5364  select->quick=tab->quick;
5365  tab->quick=0;
5366  /*
5367  We can only use 'Only index' if quick key is same as ref_key
5368  and in index_merge 'Only index' cannot be used
5369  */
5370  if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
5371  {
5372  table->key_read=0;
5373  table->cursor->extra(HA_EXTRA_NO_KEYREAD);
5374  }
5375  }
5376  else
5377  {
5378  /*
5379  We have a ref on a const; Change this to a range that filesort
5380  can use.
5381  For impossible ranges (like when doing a lookup on NULL on a NOT NULL
5382  field, quick will contain an empty record set.
5383  */
5384  if (! (select->quick= (optimizer::get_quick_select_for_ref(session,
5385  table,
5386  &tab->ref,
5387  tab->found_records))))
5388  {
5389  return(-1);
5390  }
5391  }
5392  }
5393 
5394  if (table->getShare()->getType())
5395  table->cursor->info(HA_STATUS_VARIABLE); // Get record count
5396 
5397  FileSort filesort(*session);
5398  table->sort.found_records=filesort.run(table,join->sortorder, length,
5399  select, filesort_limit, 0,
5400  examined_rows);
5401  tab->records= table->sort.found_records; // For SQL_CALC_ROWS
5402  if (select)
5403  {
5404  select->cleanup(); // filesort did select
5405  tab->select= 0;
5406  }
5407  tab->select_cond=0;
5408  tab->last_inner= 0;
5409  tab->first_unmatched= 0;
5410  tab->type= AM_ALL; // Read with normal read_record
5411  tab->read_first_record= join_init_read_record;
5412  tab->join->examined_rows+=examined_rows;
5413  if (table->key_read) // Restore if we used indexes
5414  {
5415  table->key_read=0;
5416  table->cursor->extra(HA_EXTRA_NO_KEYREAD);
5417  }
5418 
5419  return(table->sort.found_records == HA_POS_ERROR);
5420 }
5421 
5422 int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
5423 {
5424  Cursor *cursor=table->cursor;
5425  char *org_record,*new_record;
5426  unsigned char *record;
5427  int error;
5428  uint32_t reclength= table->getShare()->getRecordLength() - offset;
5429 
5430  org_record=(char*) (record=table->getInsertRecord())+offset;
5431  new_record=(char*) table->getUpdateRecord()+offset;
5432 
5433  if ((error= cursor->startTableScan(1)))
5434  goto err;
5435 
5436  error=cursor->rnd_next(record);
5437  for (;;)
5438  {
5439  if (session->getKilled())
5440  {
5441  session->send_kill_message();
5442  error=0;
5443  goto err;
5444  }
5445  if (error)
5446  {
5447  if (error == HA_ERR_RECORD_DELETED)
5448  {
5449  continue;
5450  }
5451  if (error == HA_ERR_END_OF_FILE)
5452  {
5453  break;
5454  }
5455  goto err;
5456  }
5457  if (having && !having->val_int())
5458  {
5459  if ((error=cursor->deleteRecord(record)))
5460  {
5461  goto err;
5462  }
5463  error=cursor->rnd_next(record);
5464  continue;
5465  }
5466  copy_blobs(first_field);
5467  memcpy(new_record,org_record,reclength);
5468 
5469  /* Read through rest of cursor and mark duplicated rows deleted */
5470  bool found=0;
5471  for (;;)
5472  {
5473  if ((error=cursor->rnd_next(record)))
5474  {
5475  if (error == HA_ERR_RECORD_DELETED)
5476  {
5477  continue;
5478  }
5479  if (error == HA_ERR_END_OF_FILE)
5480  {
5481  break;
5482  }
5483 
5484  goto err;
5485  }
5486  if (table->compare_record(first_field) == 0)
5487  {
5488  if ((error=cursor->deleteRecord(record)))
5489  {
5490  goto err;
5491  }
5492  }
5493  else if (!found)
5494  {
5495  found= 1;
5496  cursor->position(record); // Remember position
5497  }
5498  }
5499  if (!found)
5500  {
5501  break; // End of cursor
5502  }
5503  /* Move current position to the next row */
5504  error= cursor->rnd_pos(record, cursor->ref);
5505  }
5506 
5507  cursor->extra(HA_EXTRA_NO_CACHE);
5508  return 0;
5509 err:
5510  cursor->extra(HA_EXTRA_NO_CACHE);
5511  if (error)
5512  {
5513  table->print_error(error,MYF(0));
5514  }
5515  return 1;
5516 }
5517 
5525  Table *table,
5526  uint32_t field_count,
5527  Field **first_field,
5528  uint32_t key_length,
5529  Item *having)
5530 {
5531  unsigned char *key_pos, *record=table->getInsertRecord();
5532  int error;
5533  Cursor &cursor= *table->cursor;
5534  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5535  uint32_t *field_length;
5536  HASH hash;
5537  std::vector<unsigned char> key_buffer((key_length + extra_length) * (long) cursor.stats.records);
5538  std::vector<uint32_t> field_lengths(field_count);
5539 
5540  {
5541  Field **ptr;
5542  uint32_t total_length= 0;
5543 
5544  for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
5545  {
5546  uint32_t length= (*ptr)->sort_length();
5547  (*field_length++)= length;
5548  total_length+= length;
5549  }
5550  assert(total_length <= key_length);
5551  key_length= total_length;
5552  extra_length= ALIGN_SIZE(key_length)-key_length;
5553  }
5554 
5555  hash_init(&hash, &my_charset_bin, (uint32_t) cursor.stats.records, 0, key_length, (hash_get_key) 0, 0, 0);
5556 
5557  if ((error= cursor.startTableScan(1)))
5558  {
5559  goto err;
5560  }
5561 
5562  key_pos= &key_buffer[0];
5563  for (;;)
5564  {
5565  if (session->getKilled())
5566  {
5567  session->send_kill_message();
5568  error=0;
5569  goto err;
5570  }
5571  if ((error=cursor.rnd_next(record)))
5572  {
5573  if (error == HA_ERR_RECORD_DELETED)
5574  {
5575  continue;
5576  }
5577 
5578  if (error == HA_ERR_END_OF_FILE)
5579  {
5580  break;
5581  }
5582 
5583  goto err;
5584  }
5585  if (having && !having->val_int())
5586  {
5587  if ((error=cursor.deleteRecord(record)))
5588  {
5589  goto err;
5590  }
5591  continue;
5592  }
5593 
5594  /* copy fields to key buffer */
5595  unsigned char* org_key_pos= key_pos;
5596  field_length= &field_lengths[0];
5597  for (Field **ptr= first_field ; *ptr ; ptr++)
5598  {
5599  (*ptr)->sort_string(key_pos,*field_length);
5600  key_pos+= *field_length++;
5601  }
5602  /* Check if it exists before */
5603  if (hash_search(&hash, org_key_pos, key_length))
5604  {
5605  /* Duplicated found ; Remove the row */
5606  if ((error=cursor.deleteRecord(record)))
5607  goto err;
5608  }
5609  else
5610  {
5611  (void) my_hash_insert(&hash, org_key_pos);
5612  }
5613  key_pos+=extra_length;
5614  }
5615  hash_free(&hash);
5616  cursor.extra(HA_EXTRA_NO_CACHE);
5617  (void) cursor.endTableScan();
5618  return 0;
5619 
5620 err:
5621  hash_free(&hash);
5622  cursor.extra(HA_EXTRA_NO_CACHE);
5623  (void) cursor.endTableScan();
5624  if (error)
5625  {
5626  table->print_error(error,MYF(0));
5627  }
5628 
5629  return 1;
5630 }
5631 
5632 SortField* make_unireg_sortorder(Order* order, uint32_t* length, SortField* sortorder)
5633 {
5634  SortField *sort,*pos;
5635 
5636  uint32_t count= 0;
5637  for (Order *tmp = order; tmp; tmp=tmp->next)
5638  count++;
5639  if (not sortorder)
5640  sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) * (max(count, *length) + 1));
5641  pos= sort= sortorder;
5642 
5643  for (; order; order= order->next,pos++)
5644  {
5645  Item *item= order->item[0]->real_item();
5646  pos->field= 0; pos->item= 0;
5647 
5648  if (item->type() == Item::FIELD_ITEM)
5649  {
5650  pos->field= ((Item_field*) item)->field;
5651  }
5652  else if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item())
5653  {
5654  pos->field= ((Item_sum*) item)->get_tmp_table_field();
5655  }
5656  else if (item->type() == Item::COPY_STR_ITEM)
5657  { // Blob patch
5658  pos->item= ((Item_copy_string*) item)->item;
5659  }
5660  else
5661  {
5662  pos->item= *order->item;
5663  }
5664 
5665  pos->reverse=! order->asc;
5666  }
5667  *length=count;
5668  return sort;
5669 }
5670 
5671 /*
5672  eq_ref: Create the lookup key and check if it is the same as saved key
5673 
5674  SYNOPSIS
5675  cmp_buffer_with_ref()
5676  tab Join tab of the accessed table
5677 
5678  DESCRIPTION
5679  Used by eq_ref access method: create the index lookup key and check if
5680  we've used this key at previous lookup (If yes, we don't need to repeat
5681  the lookup - the record has been already fetched)
5682 
5683  RETURN
5684  true No cached record for the key, or failed to create the key (due to
5685  out-of-domain error)
5686  false The created key is the same as the previous one (and the record
5687  is already in table->record)
5688 */
5689 static bool cmp_buffer_with_ref(JoinTable *tab)
5690 {
5691  bool no_prev_key;
5692  if (!tab->ref.disable_cache)
5693  {
5694  if (!(no_prev_key= tab->ref.key_err))
5695  {
5696  /* Previous access found a row. Copy its key */
5697  memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
5698  }
5699  }
5700  else
5701  {
5702  no_prev_key= true;
5703  }
5704 
5705  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) || no_prev_key)
5706  {
5707  return 1;
5708  }
5709  return (memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length) != 0);
5710 }
5711 
5712 bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
5713 {
5714  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
5715  session->count_cuted_fields= CHECK_FIELD_IGNORE;
5716  bool result= 0;
5717 
5718  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
5719  {
5720  if ((*copy)->copy() & 1)
5721  {
5722  result= 1;
5723  break;
5724  }
5725  }
5726  session->count_cuted_fields= save_count_cuted_fields;
5727  return result;
5728 }
5729 
5730 /*****************************************************************************
5731  Group and order functions
5732 *****************************************************************************/
5733 
5766 static bool find_order_in_list(Session *session,
5767  Item **ref_pointer_array,
5768  TableList *tables,
5769  Order *order,
5770  List<Item> &fields,
5771  List<Item> &all_fields,
5772  bool is_group_field)
5773 {
5774  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
5775  Item::Type order_item_type;
5776  Item **select_item; /* The corresponding item from the SELECT clause. */
5777  Field *from_field; /* The corresponding field from the FROM clause. */
5778  uint32_t counter;
5779  enum_resolution_type resolution;
5780 
5781  /*
5782  Local SP variables may be int but are expressions, not positions.
5783  (And they can't be used before fix_fields is called for them).
5784  */
5785  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
5786  { /* Order by position */
5787  uint32_t count= (uint32_t) order_item->val_int();
5788  if (!count || count > fields.size())
5789  {
5790  my_error(ER_BAD_FIELD_ERROR, MYF(0), order_item->full_name(), session->where());
5791  return true;
5792  }
5793  order->item= ref_pointer_array + count - 1;
5794  order->in_field_list= 1;
5795  order->counter= count;
5796  order->counter_used= 1;
5797  return false;
5798  }
5799  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5800  select_item= find_item_in_list(session, order_item, fields, &counter, REPORT_EXCEPT_NOT_FOUND, &resolution);
5801  if (!select_item)
5802  {
5803  return true; /* The item is not unique, or some other error occured. */
5804  }
5805 
5806 
5807  /* Check whether the resolved field is not ambiguos. */
5808  if (select_item != not_found_item)
5809  {
5810  Item *view_ref= NULL;
5811  /*
5812  If we have found field not by its alias in select list but by its
5813  original field name, we should additionaly check if we have conflict
5814  for this name (in case if we would perform lookup in all tables).
5815  */
5816  if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed && order_item->fix_fields(session, order->item))
5817  {
5818  return true;
5819  }
5820 
5821  /* Lookup the current GROUP field in the FROM clause. */
5822  order_item_type= order_item->type();
5823  from_field= (Field*) not_found_field;
5824  if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
5825  order_item_type == Item::REF_ITEM)
5826  {
5827  from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
5828  NULL, &view_ref, IGNORE_ERRORS, false);
5829  if (!from_field)
5830  {
5831  from_field= (Field*) not_found_field;
5832  }
5833  }
5834 
5835  if (from_field == not_found_field ||
5836  (from_field != view_ref_found ?
5837  /* it is field of base table => check that fields are same */
5838  ((*select_item)->type() == Item::FIELD_ITEM &&
5839  ((Item_field*) (*select_item))->field->eq(from_field)) :
5840  /*
5841  in is field of view table => check that references on translation
5842  table are same
5843  */
5844  ((*select_item)->type() == Item::REF_ITEM &&
5845  view_ref->type() == Item::REF_ITEM &&
5846  ((Item_ref *) (*select_item))->ref ==
5847  ((Item_ref *) view_ref)->ref)))
5848  {
5849  /*
5850  If there is no such field in the FROM clause, or it is the same field
5851  as the one found in the SELECT clause, then use the Item created for
5852  the SELECT field. As a result if there was a derived field that
5853  'shadowed' a table field with the same name, the table field will be
5854  chosen over the derived field.
5855  */
5856  order->item= ref_pointer_array + counter;
5857  order->in_field_list=1;
5858  return false;
5859  }
5860  else
5861  {
5862  /*
5863  There is a field with the same name in the FROM clause. This
5864  is the field that will be chosen. In this case we issue a
5865  warning so the user knows that the field from the FROM clause
5866  overshadows the column reference from the SELECT list.
5867  */
5868  push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5869  ER(ER_NON_UNIQ_ERROR),
5870  ((Item_ident*) order_item)->field_name,
5871  session->where());
5872  }
5873  }
5874 
5875  order->in_field_list=0;
5876  /*
5877  The call to order_item->fix_fields() means that here we resolve
5878  'order_item' to a column from a table in the list 'tables', or to
5879  a column in some outer query. Exactly because of the second case
5880  we come to this point even if (select_item == not_found_item),
5881  inspite of that fix_fields() calls find_item_in_list() one more
5882  time.
5883 
5884  We check order_item->fixed because Item_func_group_concat can put
5885  arguments for which fix_fields already was called.
5886  */
5887  if (!order_item->fixed &&
5888  (order_item->fix_fields(session, order->item) ||
5889  (order_item= *order->item)->check_cols(1) ||
5890  session->is_fatal_error))
5891  {
5892  return true; /* Wrong field. */
5893  }
5894 
5895  uint32_t el= all_fields.size();
5896  all_fields.push_front(order_item); /* Add new field to field list. */
5897  ref_pointer_array[el]= order_item;
5898  order->item= ref_pointer_array + el;
5899  return false;
5900 }
5901 
5908 int setup_order(Session *session,
5909  Item **ref_pointer_array,
5910  TableList *tables,
5911  List<Item> &fields,
5912  List<Item> &all_fields,
5913  Order *order)
5914 {
5915  session->setWhere("order clause");
5916  for (; order; order= order->next)
5917  {
5918  if (find_order_in_list(session, ref_pointer_array, tables, order, fields, all_fields, false))
5919  {
5920  return 1;
5921  }
5922  }
5923  return 0;
5924 }
5925 
5951 int setup_group(Session *session,
5952  Item **ref_pointer_array,
5953  TableList *tables,
5954  List<Item> &fields,
5955  List<Item> &all_fields,
5956  Order *order,
5957  bool *hidden_group_fields)
5958 {
5959  *hidden_group_fields=0;
5960 
5961  if (order == NULL)
5962  {
5963  return 0; /* Everything is ok */
5964  }
5965 
5966  uint32_t org_fields=all_fields.size();
5967 
5968  session->setWhere("group statement");
5969  for (Order *ord= order; ord; ord= ord->next)
5970  {
5971  if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
5972  all_fields, true))
5973  {
5974  return 1;
5975  }
5976  (*ord->item)->marker= UNDEF_POS; /* Mark found */
5977  if ((*ord->item)->with_sum_func)
5978  {
5979  my_error(ER_WRONG_GROUP_FIELD, MYF(0), (*ord->item)->full_name());
5980  return 1;
5981  }
5982  }
5983 
5984  /* MODE_ONLY_FULL_GROUP_BY */
5985  {
5986  /*
5987  Don't allow one to use fields that is not used in GROUP BY
5988  For each select a list of field references that aren't under an
5989  aggregate function is created. Each field in this list keeps the
5990  position of the select list expression which it belongs to.
5991 
5992  First we check an expression from the select list against the GROUP BY
5993  list. If it's found there then it's ok. It's also ok if this expression
5994  is a constant or an aggregate function. Otherwise we scan the list
5995  of non-aggregated fields and if we'll find at least one field reference
5996  that belongs to this expression and doesn't occur in the GROUP BY list
5997  we throw an error. If there are no fields in the created list for a
5998  select list expression this means that all fields in it are used under
5999  aggregate functions.
6000  */
6001  Item *item;
6002  Item_field *field;
6003  int cur_pos_in_select_list= 0;
6004  List<Item>::iterator li(fields.begin());
6005  List<Item_field>::iterator naf_it(session->lex().current_select->non_agg_fields.begin());
6006 
6007  field= naf_it++;
6008  while (field && (item=li++))
6009  {
6010  if (item->type() != Item::SUM_FUNC_ITEM && item->marker >= 0 &&
6011  !item->const_item() &&
6012  !(item->real_item()->type() == Item::FIELD_ITEM &&
6013  item->used_tables() & OUTER_REF_TABLE_BIT))
6014  {
6015  while (field)
6016  {
6017  /* Skip fields from previous expressions. */
6018  if (field->marker < cur_pos_in_select_list)
6019  {
6020  goto next_field;
6021  }
6022 
6023  /* Found a field from the next expression. */
6024  if (field->marker > cur_pos_in_select_list)
6025  {
6026  break;
6027  }
6028 
6029  /*
6030  Check whether the field occur in the GROUP BY list.
6031  Throw the error later if the field isn't found.
6032  */
6033  for (Order *ord= order; ord; ord= ord->next)
6034  {
6035  if ((*ord->item)->eq((Item*)field, 0))
6036  {
6037  goto next_field;
6038  }
6039  }
6040 
6041  /*
6042  @todo change ER_WRONG_FIELD_WITH_GROUP to more detailed ER_NON_GROUPING_FIELD_USED
6043  */
6044  my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
6045  return 1;
6046 next_field:
6047  field= naf_it++;
6048  }
6049  }
6050  cur_pos_in_select_list++;
6051  }
6052  }
6053 
6054  if (org_fields != all_fields.size())
6055  {
6056  *hidden_group_fields=1; // group fields is not used
6057  }
6058 
6059  return 0;
6060 }
6061 
6069  Item **ref_pointer_array,
6070  Order *order_list,
6071  List<Item> &fields,
6072  List<Item> &,
6073  bool *all_order_by_fields_used)
6074 {
6075  List<Item>::iterator li(fields.begin());
6076  Order *order,*group,**prev;
6077 
6078  *all_order_by_fields_used= 1;
6079  while (Item* item=li++)
6080  {
6081  item->marker=0; /* Marker that field is not used */
6082  }
6083 
6084  prev= &group; group=0;
6085  for (order=order_list ; order; order=order->next)
6086  {
6087  if (order->in_field_list)
6088  {
6089  Order *ord=(Order*) session->mem.memdup(order,sizeof(Order));
6090  *prev=ord;
6091  prev= &ord->next;
6092  (*ord->item)->marker=1;
6093  }
6094  else
6095  {
6096  *all_order_by_fields_used= 0;
6097  }
6098  }
6099 
6100  li= fields.begin();
6101  while (Item* item=li++)
6102  {
6103  if (!item->const_item() && !item->with_sum_func && !item->marker)
6104  {
6105  /*
6106  Don't put duplicate columns from the SELECT list into the
6107  GROUP BY list.
6108  */
6109  Order *ord_iter;
6110  for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
6111  {
6112  if ((*ord_iter->item)->eq(item, 1))
6113  {
6114  goto next_item;
6115  }
6116  }
6117 
6118  Order *ord=(Order*) session->mem.calloc(sizeof(Order));
6119 
6120  /*
6121  We have here only field_list (not all_field_list), so we can use
6122  simple indexing of ref_pointer_array (order in the array and in the
6123  list are same)
6124  */
6125  ord->item= ref_pointer_array;
6126  ord->asc=1;
6127  *prev=ord;
6128  prev= &ord->next;
6129  }
6130 next_item:
6131  ref_pointer_array++;
6132  }
6133  *prev=0;
6134  return group;
6135 }
6136 
6140 void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
6141 {
6142  List<Item>::iterator li(fields.begin());
6143  Item *field;
6144 
6145  param->field_count=param->sum_func_count=param->func_count= param->hidden_field_count=0;
6146  param->quick_group=1;
6147 
6148  while ((field=li++))
6149  {
6150  Item::Type real_type= field->real_item()->type();
6151  if (real_type == Item::FIELD_ITEM)
6152  {
6153  param->field_count++;
6154  }
6155  else if (real_type == Item::SUM_FUNC_ITEM)
6156  {
6157  if (! field->const_item())
6158  {
6159  Item_sum *sum_item=(Item_sum*) field->real_item();
6160  if (!sum_item->depended_from() ||
6161  sum_item->depended_from() == select_lex)
6162  {
6163  if (!sum_item->quick_group)
6164  {
6165  param->quick_group=0; // UDF SUM function
6166  }
6167  param->sum_func_count++;
6168 
6169  for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
6170  {
6171  if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
6172  param->field_count++;
6173  else
6174  param->func_count++;
6175  }
6176  }
6177  param->func_count++;
6178  }
6179  }
6180  else
6181  {
6182  param->func_count++;
6183  if (reset_with_sum_func)
6184  {
6185  field->with_sum_func= 0;
6186  }
6187  }
6188  }
6189 }
6190 
6191 /*
6192  Test if a single-row cache of items changed, and update the cache.
6193 
6194  @details Test if a list of items that typically represents a result
6195  row has changed. If the value of some item changed, update the cached
6196  value for this item.
6197 
6198  @param list list of <item, cached_value> pairs stored as Cached_item.
6199 
6200  @return -1 if no item changed
6201  @return index of the first item that changed
6202 */
6203 int test_if_item_cache_changed(List<Cached_item> &list)
6204 {
6205  List<Cached_item>::iterator li(list.begin());
6206  int idx= -1;
6207  Cached_item *buff;
6208 
6209  for (int i=(int) list.size()-1 ; (buff=li++) ; i--)
6210  {
6211  if (buff->cmp())
6212  idx= i;
6213  }
6214  return idx;
6215 }
6216 
6246  Tmp_Table_Param *param,
6247  Item **ref_pointer_array,
6248  List<Item> &res_selected_fields,
6249  List<Item> &res_all_fields,
6250  uint32_t elements,
6251  List<Item> &all_fields)
6252 {
6253  Item *pos;
6254  List<Item>::iterator li(all_fields.begin());
6255  CopyField *copy= NULL;
6256  res_selected_fields.clear();
6257  res_all_fields.clear();
6258  List<Item>::iterator itr(res_all_fields.begin());
6259  List<Item> extra_funcs;
6260  uint32_t border= all_fields.size() - elements;
6261 
6262  if (param->field_count &&
6263  !(copy= param->copy_field= new CopyField[param->field_count]))
6264  {
6265  return true;
6266  }
6267 
6268  param->copy_funcs.clear();
6269  for (uint32_t i= 0; (pos= li++); i++)
6270  {
6271  Field *field;
6272  unsigned char *tmp;
6273  Item *real_pos= pos->real_item();
6274  if (real_pos->type() == Item::FIELD_ITEM)
6275  {
6276  Item_field* item= new Item_field(session, ((Item_field*) real_pos));
6277  if (pos->type() == Item::REF_ITEM)
6278  {
6279  /* preserve the names of the ref when dereferncing */
6280  Item_ref *ref= (Item_ref *) pos;
6281  item->db_name= ref->db_name;
6282  item->table_name= ref->table_name;
6283  item->name= ref->name;
6284  }
6285  pos= item;
6286 
6287  if (item->field->flags & BLOB_FLAG)
6288  {
6289  pos= new Item_copy_string(pos);
6290  /*
6291  Item_copy_string::copy for function can call
6292  Item_copy_string::val_int for blob via Item_ref.
6293  But if Item_copy_string::copy for blob isn't called before,
6294  it's value will be wrong
6295  so let's insert Item_copy_string for blobs in the beginning of
6296  copy_funcs
6297  (to see full test case look at having.test, BUG #4358)
6298  */
6299  param->copy_funcs.push_front(pos);
6300  }
6301  else
6302  {
6303  /*
6304  set up save buffer and change result_field to point at
6305  saved value
6306  */
6307  field= item->field;
6308  item->result_field=field->new_field(session->mem_root,field->getTable(), 1);
6309  /*
6310  We need to allocate one extra byte for null handling and
6311  another extra byte to not get warnings from purify in
6312  Field_varstring::val_int
6313  */
6314  if (!(tmp= (unsigned char*) memory::sql_alloc(field->pack_length()+2)))
6315  {
6316  goto err;
6317  }
6318 
6319  if (copy)
6320  {
6321  copy->set(tmp, item->result_field);
6322  item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
6323 #ifdef HAVE_VALGRIND
6324  copy->to_ptr[copy->from_length]= 0;
6325 #endif
6326  copy++;
6327  }
6328  }
6329  }
6330  else if ((real_pos->type() == Item::FUNC_ITEM ||
6331  real_pos->type() == Item::SUBSELECT_ITEM ||
6332  real_pos->type() == Item::CACHE_ITEM ||
6333  real_pos->type() == Item::COND_ITEM) &&
6334  !real_pos->with_sum_func)
6335  { // Save for send fields
6336  pos= real_pos;
6337  /*
6338  @todo In most cases this result will be sent to the user.
6339  This should be changed to use copy_int or copy_real depending
6340  on how the value is to be used: In some cases this may be an
6341  argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
6342  */
6343  pos=new Item_copy_string(pos);
6344  if (i < border) // HAVING, order_st and GROUP BY
6345  {
6346  extra_funcs.push_back(pos);
6347  }
6348  else
6349  {
6350  param->copy_funcs.push_back(pos);
6351  }
6352  }
6353  res_all_fields.push_back(pos);
6354  ref_pointer_array[((i < border)? all_fields.size()-i-1 : i-border)]=
6355  pos;
6356  }
6357  param->copy_field_end= copy;
6358 
6359  for (uint32_t i= 0; i < border; i++)
6360  {
6361  itr++;
6362  }
6363  itr.sublist(res_selected_fields, elements);
6364  /*
6365  Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
6366  reference used in these will resolve to a item that is already calculated
6367  */
6368  param->copy_funcs.concat(&extra_funcs);
6369 
6370  return 0;
6371 
6372 err:
6373  if (copy)
6374  {
6375  delete[] param->copy_field;
6376  }
6377  param->copy_field=0;
6378  return true;
6379 }
6380 
6388 {
6389  for (CopyField *ptr= param->copy_field; ptr != param->copy_field_end; ptr++)
6390  {
6391  (*ptr->do_copy)(ptr);
6392  }
6393 
6394  List<Item>::iterator it(param->copy_funcs.begin());
6395  Item_copy_string *item;
6396  while ((item = (Item_copy_string*) it++))
6397  {
6398  item->copy();
6399  }
6400 }
6401 
6419  Item **ref_pointer_array,
6420  List<Item> &res_selected_fields,
6421  List<Item> &res_all_fields,
6422  uint32_t elements,
6423  List<Item> &all_fields)
6424 {
6425  List<Item>::iterator it(all_fields.begin());
6426  Item *item_field,*item;
6427 
6428  res_selected_fields.clear();
6429  res_all_fields.clear();
6430 
6431  uint32_t i, border= all_fields.size() - elements;
6432  for (i= 0; (item= it++); i++)
6433  {
6434  Field *field;
6435 
6436  if ((item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM) or
6437  (item->type() == Item::FUNC_ITEM and ((Item_func*)item)->functype() == Item_func::SUSERVAR_FUNC))
6438  {
6439  item_field= item;
6440  }
6441  else
6442  {
6443  if (item->type() == Item::FIELD_ITEM)
6444  {
6445  item_field= item->get_tmp_table_item(session);
6446  }
6447  else if ((field= item->get_tmp_table_field()))
6448  {
6449  if (item->type() == Item::SUM_FUNC_ITEM && field->getTable()->group)
6450  {
6451  item_field= ((Item_sum*) item)->result_item(field);
6452  }
6453  else
6454  {
6455  item_field= (Item*) new Item_field(field);
6456  }
6457 
6458  if (item_field == NULL)
6459  {
6460  return true; // Fatal error
6461  }
6462 
6463  if (item->real_item()->type() != Item::FIELD_ITEM)
6464  {
6465  field->orig_table= 0;
6466  }
6467 
6468  item_field->name= item->name;
6469 
6470  if (item->type() == Item::REF_ITEM)
6471  {
6472  Item_field *ifield= (Item_field *) item_field;
6473  Item_ref *iref= (Item_ref *) item;
6474  ifield->table_name= iref->table_name;
6475  ifield->db_name= iref->db_name;
6476  }
6477  }
6478  else
6479  {
6480  item_field= item;
6481  }
6482  }
6483  res_all_fields.push_back(item_field);
6484  ref_pointer_array[((i < border)? all_fields.size()-i-1 : i-border)]= item_field;
6485  }
6486 
6487  List<Item>::iterator itr(res_all_fields.begin());
6488  for (i= 0; i < border; i++)
6489  itr++;
6490  itr.sublist(res_selected_fields, elements);
6491  return false;
6492 }
6493 
6511  Item **ref_pointer_array,
6512  List<Item> &res_selected_fields,
6513  List<Item> &res_all_fields,
6514  uint32_t elements,
6515  List<Item> &all_fields)
6516 {
6517  List<Item>::iterator it(all_fields.begin());
6518  Item *item, *new_item;
6519  res_selected_fields.clear();
6520  res_all_fields.clear();
6521 
6522  uint32_t i, border= all_fields.size() - elements;
6523  for (i= 0; (item= it++); i++)
6524  {
6525  res_all_fields.push_back(new_item= item->get_tmp_table_item(session));
6526  ref_pointer_array[((i < border)? all_fields.size()-i-1 : i-border)]=
6527  new_item;
6528  }
6529 
6530  List<Item>::iterator itr(res_all_fields.begin());
6531  for (i= 0; i < border; i++)
6532  {
6533  itr++;
6534  }
6535  itr.sublist(res_selected_fields, elements);
6536 
6537  return session->is_fatal_error;
6538 }
6539 
6540 /******************************************************************************
6541  Code for calculating functions
6542 ******************************************************************************/
6543 
6555 bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
6556 {
6557  Item_sum *func;
6558  while ((func= *(func_ptr++)))
6559  {
6560  if (func->setup(session))
6561  {
6562  return true;
6563  }
6564  }
6565  return false;
6566 }
6567 
6568 void init_tmptable_sum_functions(Item_sum **func_ptr)
6569 {
6570  Item_sum *func;
6571  while ((func= *(func_ptr++)))
6572  {
6573  func->reset_field();
6574  }
6575 }
6576 
6579 {
6580  Item_sum *func;
6581  while ((func= *(func_ptr++)))
6582  {
6583  func->update_field();
6584  }
6585 }
6586 
6588 void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
6589 {
6590  for (; func_ptr != end_ptr ; func_ptr++)
6591  {
6592  (void) (*func_ptr)->save_in_result_field(1);
6593  }
6594 }
6595 
6596 bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6597 {
6598  for (; func_ptr != end_ptr ;func_ptr++)
6599  {
6600  if ((*func_ptr)->reset())
6601  return 1;
6602  }
6603 
6604  /* If rollup, calculate the upper sum levels */
6605  for ( ; *func_ptr ; func_ptr++)
6606  {
6607  if ((*func_ptr)->add())
6608  {
6609  return 1;
6610  }
6611  }
6612  return 0;
6613 }
6614 
6615 bool update_sum_func(Item_sum **func_ptr)
6616 {
6617  Item_sum *func;
6618  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6619  {
6620  if (func->add())
6621  {
6622  return 1;
6623  }
6624  }
6625  return 0;
6626 }
6627 
6629 bool copy_funcs(Item **func_ptr, const Session *session)
6630 {
6631  Item *func;
6632  for (; (func = *func_ptr) ; func_ptr++)
6633  {
6634  func->save_in_result_field(1);
6635  /*
6636  Need to check the THD error state because Item::val_xxx() don't
6637  return error code, but can generate errors
6638  @todo change it for a real status check when Item::val_xxx()
6639  are extended to return status code.
6640  */
6641  if (session->is_error())
6642  {
6643  return true;
6644  }
6645  }
6646  return false;
6647 }
6648 
6655 void free_underlaid_joins(Session *, Select_Lex *select)
6656 {
6657  for (Select_Lex_Unit *unit= select->first_inner_unit();
6658  unit;
6659  unit= unit->next_unit())
6660  unit->cleanup();
6661 }
6662 
6663 /****************************************************************************
6664  ROLLUP handling
6665 ****************************************************************************/
6666 
6706 bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
6707 {
6708  if (expr->arg_count)
6709  {
6710  Name_resolution_context *context= &session->lex().current_select->context;
6711  Item **arg,**arg_end;
6712  bool arg_changed= false;
6713  for (arg= expr->arguments(),
6714  arg_end= expr->arguments()+expr->arg_count;
6715  arg != arg_end; arg++)
6716  {
6717  Item *item= *arg;
6718  if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6719  {
6720  Order *group_tmp;
6721  for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6722  {
6723  if (item->eq(*group_tmp->item,0))
6724  {
6725  Item* new_item= new Item_ref(context, group_tmp->item, 0, item->name);
6726  *arg= new_item;
6727  arg_changed= true;
6728  }
6729  }
6730  }
6731  else if (item->type() == Item::FUNC_ITEM)
6732  {
6733  if (change_group_ref(session, (Item_func *) item, group_list, &arg_changed))
6734  {
6735  return 1;
6736  }
6737  }
6738  }
6739 
6740  if (arg_changed)
6741  {
6742  expr->maybe_null= 1;
6743  *changed= true;
6744  }
6745  }
6746  return 0;
6747 }
6748 
6749 
6750 static void print_table_array(Session *session, String *str, TableList **table,
6751  TableList **end)
6752 {
6753  (*table)->print(session, str);
6754 
6755  for (TableList **tbl= table + 1; tbl < end; tbl++)
6756  {
6757  TableList *curr= *tbl;
6758  if (curr->outer_join)
6759  {
6760  /* MySQL converts right to left joins */
6761  str->append(STRING_WITH_LEN(" left join "));
6762  }
6763  else if (curr->straight)
6764  {
6765  str->append(STRING_WITH_LEN(" straight_join "));
6766  }
6767  else
6768  {
6769  str->append(STRING_WITH_LEN(" join "));
6770  }
6771  curr->print(session, str);
6772  if (curr->on_expr)
6773  {
6774  str->append(STRING_WITH_LEN(" on("));
6775  curr->on_expr->print(str);
6776  str->append(')');
6777  }
6778  }
6779 }
6780 
6787 void print_join(Session *session, String *str,
6788  List<TableList> *tables)
6789 {
6790  /* List is reversed => we should reverse it before using */
6791  List<TableList>::iterator ti(tables->begin());
6792  TableList **table= new (session->mem) TableList*[tables->size()];
6793 
6794  for (TableList **t= table + (tables->size() - 1); t >= table; t--)
6795  {
6796  *t= ti++;
6797  }
6798  assert(tables->size() >= 1);
6799  print_table_array(session, str, table, table + tables->size());
6800 }
6801 
6802 void Select_Lex::print(Session *session, String *str)
6803 {
6804  /* QQ: session may not be set for sub queries, but this should be fixed */
6805  if (not session)
6806  {
6807  session= current_session;
6808  }
6809 
6810 
6811  str->append(STRING_WITH_LEN("select "));
6812 
6813  /* First add options */
6814  if (options & SELECT_STRAIGHT_JOIN)
6815  str->append(STRING_WITH_LEN("straight_join "));
6816 
6817  if (options & SELECT_DISTINCT)
6818  str->append(STRING_WITH_LEN("distinct "));
6819 
6820  if (options & SELECT_SMALL_RESULT)
6821  str->append(STRING_WITH_LEN("sql_small_result "));
6822 
6823  if (options & SELECT_BIG_RESULT)
6824  str->append(STRING_WITH_LEN("sql_big_result "));
6825 
6826  if (options & OPTION_BUFFER_RESULT)
6827  str->append(STRING_WITH_LEN("sql_buffer_result "));
6828 
6829  if (options & OPTION_FOUND_ROWS)
6830  str->append(STRING_WITH_LEN("sql_calc_found_rows "));
6831 
6832  //Item List
6833  bool first= 1;
6834  List<Item>::iterator it(item_list.begin());
6835  Item *item;
6836  while ((item= it++))
6837  {
6838  if (first)
6839  {
6840  first= 0;
6841  }
6842  else
6843  {
6844  str->append(',');
6845  }
6846  item->print_item_w_name(str);
6847  }
6848 
6849  /*
6850  from clause
6851  @todo support USING/FORCE/IGNORE index
6852  */
6853  if (table_list.size())
6854  {
6855  str->append(STRING_WITH_LEN(" from "));
6856  /* go through join tree */
6857  print_join(session, str, &top_join_list);
6858  }
6859  else if (where)
6860  {
6861  /*
6862  "SELECT 1 FROM DUAL WHERE 2" should not be printed as
6863  "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
6864  */
6865  str->append(STRING_WITH_LEN(" from DUAL "));
6866  }
6867 
6868  // Where
6869  Item *cur_where= where;
6870  if (join)
6871  cur_where= join->conds;
6872  if (cur_where || cond_value != Item::COND_UNDEF)
6873  {
6874  str->append(STRING_WITH_LEN(" where "));
6875  if (cur_where)
6876  {
6877  cur_where->print(str);
6878  }
6879  else
6880  {
6881  if (cond_value != Item::COND_FALSE)
6882  {
6883  str->append(STRING_WITH_LEN("1"));
6884  }
6885  else
6886  {
6887  str->append(STRING_WITH_LEN("0"));
6888  }
6889  }
6890  }
6891 
6892  // group by & olap
6893  if (group_list.size())
6894  {
6895  str->append(STRING_WITH_LEN(" group by "));
6896  print_order(str, (Order *) group_list.first);
6897  switch (olap)
6898  {
6899  case CUBE_TYPE:
6900  str->append(STRING_WITH_LEN(" with cube"));
6901  break;
6902  case ROLLUP_TYPE:
6903  str->append(STRING_WITH_LEN(" with rollup"));
6904  break;
6905  default:
6906  ; //satisfy compiler
6907  }
6908  }
6909 
6910  // having
6911  Item *cur_having= having;
6912  if (join)
6913  cur_having= join->having;
6914 
6915  if (cur_having || having_value != Item::COND_UNDEF)
6916  {
6917  str->append(STRING_WITH_LEN(" having "));
6918  if (cur_having)
6919  {
6920  cur_having->print(str);
6921  }
6922  else
6923  {
6924  if (having_value != Item::COND_FALSE)
6925  {
6926  str->append(STRING_WITH_LEN("1"));
6927  }
6928  else
6929  {
6930  str->append(STRING_WITH_LEN("0"));
6931  }
6932  }
6933  }
6934 
6935  if (order_list.size())
6936  {
6937  str->append(STRING_WITH_LEN(" order by "));
6938  print_order(str, (Order *) order_list.first);
6939  }
6940 
6941  // limit
6942  print_limit(session, str);
6943 
6944  // PROCEDURE unsupported here
6945 }
6946 
6951 } /* namespace drizzled */
JoinTable * first_unmatched
Definition: join_table.h:137
int join_tab_cmp_straight(const void *ptr1, const void *ptr2)
Definition: sql_select.cc:866
virtual uint32_t pack_length() const
Definition: field.cc:707
virtual bool const_item() const
Definition: item.h:495
virtual int index_read_idx_map(unsigned char *buf, uint32_t index, const unsigned char *key, key_part_map keypart_map, enum ha_rkey_function find_flag)
Positions an index cursor to the index specified in the handle. Fetches the row if available...
Definition: cursor.cc:1202
bool check_interleaving_with_nj(JoinTable *next_tab)
Definition: sql_select.cc:2763
key_map key_start
Definition: field.h:106
const char * name
Definition: item.h:110
static COND * build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
Definition: sql_select.cc:1882
uint32_t row_count
Definition: session.h:447
enum_nested_loop_state evaluate_join_record(Join *join, JoinTable *join_tab, int error)
Process one record of the nested loop join.
Definition: join.cc:2606
table_map map
ID bit of table (1,2,4,8,16...)
Definition: table.h:270
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
Definition: sql_select.cc:3060
void * memdup(const void *, size_t)
Duplicate the provided block into memory allocated from within the specified Root.
Definition: root.cc:366
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
Definition: sql_select.cc:6578
bool substitute_null_with_insert_id
Definition: session.h:560
virtual bool basic_const_item() const
Definition: item.h:474
enum access_method type
Definition: join_table.h:185
void set_proc_info(const char *info)
Definition: session.h:609
bool is_subkey(KeyPartInfo *key_part, KeyPartInfo *ref_key_part, KeyPartInfo *ref_key_part_end)
Definition: sql_select.cc:4634
Item_equal * find_item_equal(COND_EQUAL *cond_equal)
Definition: field.cc:947
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
Definition: sql_select.cc:6555
void merge(Item_equal *item)
Definition: cmpfunc.cc:4995
void calc_used_field_length(Session *, JoinTable *join_tab)
Definition: sql_select.cc:883
virtual int64_t val_int()=0
void copy_fields(Tmp_Table_Param *param)
Definition: sql_select.cc:6387
int join_read_always_key_or_null(JoinTable *tab)
Definition: sql_select.cc:4021
void * session_marker
Definition: session.h:599
bool change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array, List< Item > &res_selected_fields, List< Item > &res_all_fields, uint32_t elements, List< Item > &all_fields)
Definition: sql_select.cc:6510
uint32_t max_part_bit(key_part_map bits)
Definition: sql_select.cc:515
ha_rows cuted_fields
Definition: session.h:408
bool contains(Field *field)
Definition: cmpfunc.cc:4971
const char * in_additional_cond
Definition: drizzled.cc:312
bool fixed
Definition: item.h:120
COND_EQUAL * cond_equal
Used with outer join.
Definition: table_list.h:155
TODO: Rename this file - func.h is stupid.
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
Definition: sql_select.cc:975
bool resume_nested_loop
Definition: join.h:81
bool list_contains_unique_index(Table *table, bool(*find_func)(Field *, void *), void *data)
Definition: sql_select.cc:4720
enum_nested_loop_state end_update(Join *join, JoinTable *, bool end_of_records)
Definition: join.cc:2996
table_map used_tables() const
Definition: field.cc:380
COND * remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
Definition: sql_select.cc:2851
Table * orig_table
Definition: field.h:101
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List< Item > &fields, bool reset_with_sum_func)
Definition: sql_select.cc:6140
int do_select(Join *join, List< Item > *fields, Table *table)
Definition: sql_select.cc:3206
virtual bool eq_def(Field *field)
Definition: field.cc:1146
QuickSelectInterface * quick
Definition: range.h:277
bool change_result(select_result *result)
Definition: join.cc:2551
int prepare(Item ***rref_pointer_array, TableList *tables, uint32_t wind_num, COND *conds, uint32_t og_num, Order *order, Order *group, Item *having, Select_Lex *select, Select_Lex_Unit *unit)
Definition: join.cc:332
int join_read_const(JoinTable *tab)
Definition: sql_select.cc:3579
int report_error(int error)
Definition: table.cc:1591
static uint32_t test_if_subkey(Order *order, Table *table, uint32_t ref, uint32_t ref_key_parts, const key_map *usable_keys)
Definition: sql_select.cc:4660
Order * create_distinct_group(Session *session, Item **ref_pointer_array, Order *order_list, List< Item > &fields, List< Item > &, bool *all_order_by_fields_used)
Definition: sql_select.cc:6068
virtual bool eq(const Item *, bool binary_cmp) const
Definition: item.cc:433
Item * conds_history
Definition: join.h:198
#define STACK_MIN_SIZE
Abort if less stack during eval.
Definition: definitions.h:120
bool check_stack_overrun(Session *session, long margin, void *)
COND * substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
Definition: sql_select.cc:2354
uint64_t first_successful_insert_id_in_prev_stmt
Definition: session.h:379
static int compare_fields_by_table_order(Item_field *field1, Item_field *field2, void *table_join_idx)
Definition: sql_select.cc:2170
int join_read_last_key(JoinTable *tab)
Definition: sql_select.cc:3737
virtual bool val_bool()
Definition: item.cc:95
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
Definition: sql_select.cc:3486
static bool check_row_equality(Session *session, Item *left_row, Item_row *right_row, COND_EQUAL *cond_equal, List< Item > *eq_list)
Definition: sql_select.cc:1723
bool select_distinct
Definition: join.h:90
static Item_equal * find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
Definition: sql_select.cc:1419
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
Definition: sql_select.cc:4521
bool select_query(Session *session, Item ***rref_pointer_array, TableList *tables, uint32_t wild_num, List< Item > &fields, COND *conds, uint32_t og_num, Order *order, Order *group, Item *having, uint64_t select_options, select_result *result, Select_Lex_Unit *unit, Select_Lex *select_lex)
Definition: sql_select.cc:368
Item * having_history
Definition: join.h:173
Table * table
Definition: field.h:81
void join_free()
Definition: join.cc:1967
JoinTable * first_inner
Definition: join_table.h:132
uint64_t limit_found_rows
Definition: session.h:399
int remove_dup_with_hash_index(Session *session, Table *table, uint32_t field_count, Field **first_field, uint32_t key_length, Item *having)
Definition: sql_select.cc:5524
bool maybe_null
Definition: item.h:121
int key_cmp(KeyPartInfo *key_part, const unsigned char *key, uint32_t key_length)
Definition: key.cc:438
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
Definition: sql_select.cc:1337
void quick_fix_field()
Definition: item.h:207
enum_nested_loop_state evaluate_null_complemented_join_record(Join *join, JoinTable *join_tab)
Definition: join.cc:2730
uint32_t used_fields
Definition: join_table.h:182
int join_tab_cmp(const void *ptr1, const void *ptr2)
Definition: sql_select.cc:843
void sort(Item_field_cmpfunc cmp, void *arg)
Definition: cmpfunc.cc:5029
void update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse, JoinTable *join_tab, uint32_t tables, COND *cond, COND_EQUAL *, table_map normal_tables, Select_Lex *select_lex, vector< optimizer::SargableParam > &sargables)
Definition: sql_select.cc:566
bool handle_select(Session *session, LEX *lex, select_result *result, uint64_t setup_tables_done_option)
Definition: sql_select.cc:118
List< TableList > * join_list
Definition: join.h:142
bool find_field_in_item_list(Field *field, void *data)
Definition: sql_select.cc:4794
JoinTable * return_tab
Definition: join.h:201
int setup_group(Session *session, Item **ref_pointer_array, TableList *tables, List< Item > &fields, List< Item > &all_fields, Order *order, bool *hidden_group_fields)
Definition: sql_select.cc:5951
Select_Lex_Unit * unit
Definition: join.h:144
void exec()
Definition: join.cc:1372
table_map used_tables
Definition: session.h:430
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
Definition: sql_select.cc:6588
bool change_to_use_tmp_fields(Session *session, Item **ref_pointer_array, List< Item > &res_selected_fields, List< Item > &res_all_fields, uint32_t elements, List< Item > &all_fields)
Definition: sql_select.cc:6418
bool find_field_in_order_list(Field *field, void *data)
Definition: sql_select.cc:4764
int optimize()
Definition: join.cc:609
enum_nested_loop_state end_unique_update(Join *join, JoinTable *, bool end_of_records)
Definition: join.cc:3063
void print_join(Session *session, String *str, List< TableList > *tables)
Definition: sql_select.cc:6787
int(* qsort_cmp)(const void *, const void *)
Definition: qsort_cmp.h:26
KeyInfo * key_info
Definition: table.h:141
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
Definition: sql_select.cc:716
void update_const_equal_items(COND *cond, JoinTable *tab)
Definition: sql_select.cc:2442
static bool find_order_in_list(Session *session, Item **ref_pointer_array, TableList *tables, Order *order, List< Item > &fields, List< Item > &all_fields, bool is_group_field)
Definition: sql_select.cc:5766
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables, List< Item > &fields, List< Item > &all_fields, Order *order)
Definition: sql_select.cc:5908
std::bitset< 64 > cur_embedding_map
Definition: join.h:153
bool is_fatal_error
Definition: session.h:540
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
Definition: sql_select.cc:6706
uint32_t used_fieldlength
Definition: join_table.h:183
bool setup_copy_fields(Session *session, Tmp_Table_Param *param, Item **ref_pointer_array, List< Item > &res_selected_fields, List< Item > &res_all_fields, uint32_t elements, List< Item > &all_fields)
Definition: sql_select.cc:6245
void free_underlaid_joins(Session *session, Select_Lex *select)
Definition: sql_select.cc:6655
Session * in_use
Definition: table.h:123
Next_select_func setup_end_select_func(Join *join)
Definition: sql_select.cc:3137
std::bitset< 64 > embedding_map
Definition: join_table.h:229
uint32_t tables
Definition: join.h:66
Item * pre_idx_push_select_cond
Definition: join_table.h:129
List< Item > & fields_list
Definition: join.h:141
uint64_t options
Definition: session.h:400
Cursor * cursor
Definition: table.h:68
ha_rows quick_condition_rows
Definition: table.h:255
virtual table_map used_tables() const
Definition: item.h:451
JoinTable * last_inner
Definition: join_table.h:135
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
Definition: sql_select.cc:4838
bool copy_funcs(Item **func_ptr, const Session *session)
Definition: sql_select.cc:6629
enum_resolution_type
Definition: sql_base.h:82
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
Definition: sql_select.cc:772
static COND * build_equal_items(Session *session, COND *cond, COND_EQUAL *inherited, List< TableList > *join_list, COND_EQUAL **cond_equal_ref)
Definition: sql_select.cc:2096
virtual int index_read_last_map(unsigned char *buf, const unsigned char *key, key_part_map keypart_map)
The following functions works like index_read, but it find the last row with the current key value or...
Definition: cursor.h:340
Item * remove_additional_cond(Item *conds)
Definition: sql_select.cc:2584
optimizer::Position & getPosFromOptimalPlan(uint32_t index)
Definition: join.h:321
memory::Root * mem_root
Definition: session.h:118
optimizer::KeyUse * keyuse
Definition: join_table.h:118
Item * tmp_having
Definition: join.h:172
static bool check_simple_equality(Item *left_item, Item *right_item, Item *item, COND_EQUAL *cond_equal)
Definition: sql_select.cc:1524
bool key_cmp_if_same(Table *table, const unsigned char *key, uint32_t idx, uint32_t key_length)
Definition: key.cc:266
bool is_error() const
Definition: session.cc:1871
Item * on_expr
Used with outer join.
Definition: table_list.h:144
static Item * eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
Definition: sql_select.cc:2238
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List< Item > *eq_list)
Definition: sql_select.cc:1794
Order * group_list
Definition: join.h:196
virtual Item * compile(Item_analyzer analyzer, unsigned char **arg_p, Item_transformer transformer, unsigned char *arg_t)
Definition: item.cc:585