Drizzled Public API Documentation

subselect.cc
Go to the documentation of this file.
1 /* Copyright (C) 2000 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 
26 #include <config.h>
27 
28 #include <cstdio>
29 #include <limits.h>
30 
31 #include <drizzled/session.h>
32 #include <drizzled/sql_select.h>
33 #include <drizzled/error.h>
34 #include <drizzled/item/cache.h>
35 #include <drizzled/item/subselect.h>
36 #include <drizzled/item/cmpfunc.h>
37 #include <drizzled/item/ref_null_helper.h>
38 #include <drizzled/cached_item.h>
39 #include <drizzled/check_stack_overrun.h>
40 #include <drizzled/item/ref_null_helper.h>
41 #include <drizzled/item/direct_ref.h>
42 #include <drizzled/join.h>
43 #include <drizzled/plugin/storage_engine.h>
44 #include <drizzled/select_singlerow_subselect.h>
45 #include <drizzled/select_max_min_finder_subselect.h>
46 #include <drizzled/select_exists_subselect.h>
47 #include <drizzled/select_union.h>
48 #include <drizzled/sql_lex.h>
49 #include <drizzled/system_variables.h>
50 
51 namespace drizzled {
52 
53 extern plugin::StorageEngine *myisam_engine;
54 
55 inline Item* and_items(Item* cond, Item *item)
56 {
57  return cond ? new Item_cond_and(cond, item) : item;
58 }
59 
60 Item_subselect::Item_subselect() :
61  Item_result_field(),
62  value_assigned(false),
63  session(NULL),
64  substitution(NULL),
65  unit(NULL),
66  engine(NULL),
67  old_engine(NULL),
68  used_tables_cache(0),
69  max_columns(0),
70  parsing_place(NO_MATTER),
71  have_to_be_excluded(false),
72  const_item_cache(true),
73  engine_changed(false),
74  changed(false),
75  is_correlated(false)
76 {
77  with_subselect= 1;
78  reset();
79  /*
80  Item value is NULL if select_result_interceptor didn't change this value
81  (i.e. some rows will be found returned)
82  */
83  null_value= 1;
84 }
85 
86 
87 void Item_subselect::init(Select_Lex *select_lex,
88  select_result_interceptor *result)
89 {
90  /*
91  Please see Item_singlerow_subselect::invalidate_and_restore_select_lex(),
92  which depends on alterations to the parse tree implemented here.
93  */
94 
95  unit= select_lex->master_unit();
96 
97  if (unit->item)
98  {
99  /*
100  Item can be changed in JOIN::prepare while engine in Join::optimize
101  => we do not copy old_engine here
102  */
103  engine= unit->item->engine;
104  parsing_place= unit->item->parsing_place;
105  unit->item->engine= 0;
106  unit->item= this;
107  engine->change_result(this, result);
108  }
109  else
110  {
111  Select_Lex *outer_select= unit->outer_select();
112  /*
113  do not take into account expression inside aggregate functions because
114  they can access original table fields
115  */
116  parsing_place= (outer_select->in_sum_expr ?
117  NO_MATTER :
118  outer_select->parsing_place);
119  if (unit->is_union())
120  engine= new subselect_union_engine(unit, result, this);
121  else
122  engine= new subselect_single_select_engine(select_lex, result, this);
123  }
124  {
125  Select_Lex *upper= unit->outer_select();
126  if (upper->parsing_place == IN_HAVING)
127  upper->subquery_in_having= 1;
128  }
129  return;
130 }
131 
132 Select_Lex *
134 {
135  return unit->first_select();
136 }
137 
138 void Item_subselect::cleanup()
139 {
140  Item_result_field::cleanup();
141  if (old_engine)
142  {
143  if (engine)
144  engine->cleanup();
145  engine= old_engine;
146  old_engine= 0;
147  }
148  if (engine)
149  engine->cleanup();
150  reset();
151  value_assigned= 0;
152 }
153 
154 void Item_singlerow_subselect::cleanup()
155 {
156  value= 0; row= 0;
157  Item_subselect::cleanup();
158 }
159 
160 
161 void Item_in_subselect::cleanup()
162 {
163  if (left_expr_cache)
164  {
165  left_expr_cache->delete_elements();
166  delete left_expr_cache;
167  left_expr_cache= NULL;
168  }
169  first_execution= true;
170  Item_subselect::cleanup();
171 }
172 
173 Item_subselect::~Item_subselect()
174 {
175  delete engine;
176 }
177 
178 Item_subselect::trans_res
179 Item_subselect::select_transformer(Join *)
180 {
181  return RES_OK;
182 }
183 
184 
185 bool Item_subselect::fix_fields(Session *session_param, Item **ref)
186 {
187  char const *save_where= session_param->where();
188  bool res;
189 
190  assert(fixed == 0);
191  engine->set_session((session= session_param));
192 
193  if (check_stack_overrun(session, STACK_MIN_SIZE, (unsigned char*)&res))
194  return true;
195 
196  res= engine->prepare();
197 
198  // all transformation is done (used by prepared statements)
199  changed= 1;
200 
201  if (!res)
202  {
203  /*
204  Substitute the current item with an Item_in_optimizer that was
205  created by Item_in_subselect::select_in_like_transformer and
206  call fix_fields for the substituted item which in turn calls
207  engine->prepare for the subquery predicate.
208  */
209  if (substitution)
210  {
211  int ret= 0;
212 
213  // did we changed top item of WHERE condition
214  if (unit->outer_select()->where == (*ref))
215  {
216  unit->outer_select()->where= substitution; // correct WHERE for PS
217  }
218  else if (unit->outer_select()->having == (*ref))
219  {
220  unit->outer_select()->having= substitution; // correct HAVING for PS
221  }
222 
223  (*ref)= substitution;
224  substitution->name= name;
225  if (have_to_be_excluded)
226  {
227  engine->exclude();
228  }
229  substitution= 0;
230  session->setWhere("checking transformed subquery");
231  if (! (*ref)->fixed)
232  {
233  ret= (*ref)->fix_fields(session, ref);
234  }
235  session->setWhere(save_where);
236 
237  return ret;
238  }
239  // Is it one field subselect?
240  if (engine->cols() > max_columns)
241  {
242  my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
243  return true;
244  }
245  fix_length_and_dec();
246  }
247  else
248  goto err;
249 
250  if (engine->uncacheable())
251  {
252  const_item_cache= false;
253  if (engine->uncacheable(UNCACHEABLE_RAND))
254  {
255  used_tables_cache|= RAND_TABLE_BIT;
256  }
257  }
258  fixed= 1;
259 
260 err:
261  session->setWhere(save_where);
262  return res;
263 }
264 
265 
266 bool Item_subselect::walk(Item_processor processor, bool walk_subquery,
267  unsigned char *argument)
268 {
269 
270  if (walk_subquery)
271  {
272  for (Select_Lex *lex= unit->first_select(); lex; lex= lex->next_select())
273  {
274  List<Item>::iterator li(lex->item_list.begin());
275  Item *item;
276  Order *order;
277 
278  if (lex->where && (lex->where)->walk(processor, walk_subquery, argument))
279  return 1;
280  if (lex->having && (lex->having)->walk(processor, walk_subquery,
281  argument))
282  return 1;
283 
284  while ((item=li++))
285  {
286  if (item->walk(processor, walk_subquery, argument))
287  return 1;
288  }
289  for (order= (Order*) lex->order_list.first ; order; order= order->next)
290  {
291  if ((*order->item)->walk(processor, walk_subquery, argument))
292  return 1;
293  }
294  for (order= (Order*) lex->group_list.first ; order; order= order->next)
295  {
296  if ((*order->item)->walk(processor, walk_subquery, argument))
297  return 1;
298  }
299  }
300  }
301  return (this->*processor)(argument);
302 }
303 
304 
305 bool Item_subselect::exec()
306 {
307  int res;
308 
309  if (session->is_error())
310  /* Do not execute subselect in case of a fatal error */
311  return 1;
312 
313  res= engine->exec();
314 
315  if (engine_changed)
316  {
317  engine_changed= 0;
318  return exec();
319  }
320  return (res);
321 }
322 
323 
324 /*
325  Compute the IN predicate if the left operand's cache changed.
326 */
327 
328 bool Item_in_subselect::exec()
329 {
330  assert(exec_method != MATERIALIZATION ||
331  (exec_method == MATERIALIZATION &&
332  engine->engine_type() == subselect_engine::HASH_SJ_ENGINE));
333  /*
334  Initialize the cache of the left predicate operand. This has to be done as
335  late as now, because Cached_item directly contains a resolved field (not
336  an item, and in some cases (when temp tables are created), these fields
337  end up pointing to the wrong field. One solution is to change Cached_item
338  to not resolve its field upon creation, but to resolve it dynamically
339  from a given Item_ref object.
340  TODO: the cache should be applied conditionally based on:
341  - rules - e.g. only if the left operand is known to be ordered, and/or
342  - on a cost-based basis, that takes into account the cost of a cache
343  lookup, the cache hit rate, and the savings per cache hit.
344  */
345  if (!left_expr_cache && exec_method == MATERIALIZATION)
346  init_left_expr_cache();
347 
348  /* If the new left operand is already in the cache, reuse the old result. */
349  if (left_expr_cache && test_if_item_cache_changed(*left_expr_cache) < 0)
350  {
351  /* Always compute IN for the first row as the cache is not valid for it. */
352  if (!first_execution)
353  return false;
354  first_execution= false;
355  }
356 
357  /*
358  The exec() method below updates item::value, and item::null_value, thus if
359  we don't call it, the next call to item::val_int() will return whatever
360  result was computed by its previous call.
361  */
362  return(Item_subselect::exec());
363 }
364 
365 
366 Item::Type Item_subselect::type() const
367 {
368  return SUBSELECT_ITEM;
369 }
370 
371 
372 void Item_subselect::fix_length_and_dec()
373 {
374  engine->fix_length_and_dec(0);
375 }
376 
377 
379 {
380  return (table_map) (engine->uncacheable() ? used_tables_cache : 0L);
381 }
382 
383 
385 {
386  return const_item_cache;
387 }
388 
389 Item *Item_subselect::get_tmp_table_item(Session *session_arg)
390 {
391  if (!with_sum_func && !const_item())
392  return new Item_field(result_field);
393  return copy_or_same(session_arg);
394 }
395 
396 void Item_subselect::update_used_tables()
397 {
398  if (! engine->uncacheable())
399  {
400  // did all used tables become static?
401  if (!(used_tables_cache & ~engine->upper_select_const_tables()))
402  const_item_cache= true;
403  }
404 }
405 
406 
408 {
409  str->append('(');
410  engine->print(str);
411  str->append(')');
412 }
413 
414 
415 Item_singlerow_subselect::Item_singlerow_subselect(Select_Lex *select_lex)
416  :Item_subselect(), value(0)
417 {
418  init(select_lex, new select_singlerow_subselect(this));
419  maybe_null= 1;
420  max_columns= UINT_MAX;
421 }
422 
423 Select_Lex *
425 {
426  Select_Lex *result= get_select_lex();
427 
428  assert(result);
429 
430  /*
431  This code restore the parse tree in it's state before the execution of
432  Item_singlerow_subselect::Item_singlerow_subselect(),
433  and in particular decouples this object from the Select_Lex,
434  so that the Select_Lex can be used with a different flavor
435  or Item_subselect instead, as part of query rewriting.
436  */
437  unit->item= NULL;
438 
439  return(result);
440 }
441 
442 Item_maxmin_subselect::Item_maxmin_subselect(Session *session_param,
443  Item_subselect *parent,
444  Select_Lex *select_lex,
445  bool max_arg)
446  :Item_singlerow_subselect(), was_values(true)
447 {
448  max= max_arg;
449  init(select_lex, new select_max_min_finder_subselect(this, max_arg));
450  max_columns= 1;
451  maybe_null= 1;
452  max_columns= 1;
453 
454  /*
455  Following information was collected during performing fix_fields()
456  of Items belonged to subquery, which will be not repeated
457  */
458  used_tables_cache= parent->get_used_tables_cache();
459  const_item_cache= parent->get_const_item_cache();
460 
461  /*
462  this subquery always creates during preparation, so we can assign
463  session here
464  */
465  session= session_param;
466 }
467 
468 void Item_maxmin_subselect::cleanup()
469 {
470  Item_singlerow_subselect::cleanup();
471 
472  /*
473  By default it is true to avoid true reporting by
474  Item_func_not_all/Item_func_nop_all if this item was never called.
475 
476  Engine exec() set it to false by reset_value_registration() call.
477  select_max_min_finder_subselect::send_data() set it back to true if some
478  value will be found.
479  */
480  was_values= true;
481 }
482 
483 
485 {
486  str->append(max?"<max>":"<min>", 5);
488 }
489 
490 
491 void Item_singlerow_subselect::reset()
492 {
493  null_value= 1;
494  if (value)
495  value->null_value= 1;
496 }
497 
498 
509 Item_subselect::trans_res
511 {
512  if (changed)
513  return(RES_OK);
514 
515  Select_Lex *select_lex= join->select_lex;
516 
517  if (!select_lex->master_unit()->is_union() &&
518  !select_lex->table_list.elements &&
519  select_lex->item_list.size() == 1 &&
520  !select_lex->item_list.front().with_sum_func &&
521  /*
522  We cant change name of Item_field or Item_ref, because it will
523  prevent it's correct resolving, but we should save name of
524  removed item => we do not make optimization if top item of
525  list is field or reference.
526  TODO: solve above problem
527  */
528  !(select_lex->item_list.front().type() == FIELD_ITEM ||
529  select_lex->item_list.front().type() == REF_ITEM) &&
530  !join->conds && !join->having
531  )
532  {
533 
534  have_to_be_excluded= 1;
535  if (session->lex().describe)
536  {
537  char warn_buff[DRIZZLE_ERRMSG_SIZE];
538  snprintf(warn_buff, sizeof(warn_buff), ER(ER_SELECT_REDUCED), select_lex->select_number);
539  push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
540  ER_SELECT_REDUCED, warn_buff);
541  }
542  substitution= &select_lex->item_list.front();
543  /*
544  as far as we moved content to upper level, field which depend of
545  'upper' select is not really dependent => we remove this dependence
546  */
547  substitution->walk(&Item::remove_dependence_processor, 0,
548  (unsigned char *) select_lex->outer_select());
549  return(RES_REDUCE);
550  }
551  return(RES_OK);
552 }
553 
554 
555 void Item_singlerow_subselect::store(uint32_t i, Item *item)
556 {
557  row[i]->store(item);
558 }
559 
560 enum Item_result Item_singlerow_subselect::result_type() const
561 {
562  return engine->type();
563 }
564 
565 /*
566  Don't rely on the result type to calculate field type.
567  Ask the engine instead.
568 */
569 enum_field_types Item_singlerow_subselect::field_type() const
570 {
571  return engine->field_type();
572 }
573 
574 void Item_singlerow_subselect::fix_length_and_dec()
575 {
576  if ((max_columns= engine->cols()) == 1)
577  {
578  engine->fix_length_and_dec(row= &value);
579  }
580  else
581  {
582  if (!(row= (Item_cache**) memory::sql_alloc(sizeof(Item_cache*)*max_columns)))
583  return;
584  engine->fix_length_and_dec(row);
585  value= *row;
586  }
587  unsigned_flag= value->unsigned_flag;
588  /*
589  If there are not tables in subquery then ability to have NULL value
590  depends on SELECT list (if single row subquery have tables then it
591  always can be NULL if there are not records fetched).
592  */
593  if (engine->no_tables())
594  maybe_null= engine->may_be_null();
595 }
596 
597 uint32_t Item_singlerow_subselect::cols()
598 {
599  return engine->cols();
600 }
601 
602 bool Item_singlerow_subselect::check_cols(uint32_t c)
603 {
604  if (c != engine->cols())
605  {
606  my_error(ER_OPERAND_COLUMNS, MYF(0), c);
607  return 1;
608  }
609  return 0;
610 }
611 
612 bool Item_singlerow_subselect::null_inside()
613 {
614  for (uint32_t i= 0; i < max_columns ; i++)
615  {
616  if (row[i]->null_value)
617  return 1;
618  }
619  return 0;
620 }
621 
622 void Item_singlerow_subselect::bring_value()
623 {
624  exec();
625 }
626 
628 {
629  assert(fixed == 1);
630  if (!exec() && !value->null_value)
631  {
632  null_value= 0;
633  return value->val_real();
634  }
635  else
636  {
637  reset();
638  return 0;
639  }
640 }
641 
643 {
644  assert(fixed == 1);
645  if (!exec() && !value->null_value)
646  {
647  null_value= 0;
648  return value->val_int();
649  }
650  else
651  {
652  reset();
653  return 0;
654  }
655 }
656 
658 {
659  if (!exec() && !value->null_value)
660  {
661  null_value= 0;
662  return value->val_str(str);
663  }
664  else
665  {
666  reset();
667  return 0;
668  }
669 }
670 
671 
673 {
674  if (!exec() && !value->null_value)
675  {
676  null_value= 0;
677  return value->val_decimal(decimal_value);
678  }
679  else
680  {
681  reset();
682  return 0;
683  }
684 }
685 
686 
688 {
689  if (!exec() && !value->null_value)
690  {
691  null_value= 0;
692  return value->val_bool();
693  }
694  else
695  {
696  reset();
697  return 0;
698  }
699 }
700 
701 
702 Item_exists_subselect::Item_exists_subselect(Select_Lex *select_lex):
704 {
705  bool val_bool();
706  init(select_lex, new select_exists_subselect(this));
707  max_columns= UINT_MAX;
708  null_value= 0; //can't be NULL
709  maybe_null= 0; //can't be NULL
710  value= 0;
711  return;
712 }
713 
714 
716 {
717  str->append(STRING_WITH_LEN("exists"));
719 }
720 
721 
722 bool Item_in_subselect::test_limit(Select_Lex_Unit *unit_arg)
723 {
724  if (unit_arg->fake_select_lex &&
725  unit_arg->fake_select_lex->test_limit())
726  return 1;
727 
728  Select_Lex *sl= unit_arg->first_select();
729  for (; sl; sl= sl->next_select())
730  {
731  if (sl->test_limit())
732  return 1;
733  }
734  return 0;
735 }
736 
737 Item_in_subselect::Item_in_subselect(Item * left_exp,
738  Select_Lex *select_lex) :
739  Item_exists_subselect(),
740  left_expr(left_exp),
741  left_expr_cache(NULL),
742  first_execution(true),
743  optimizer(NULL),
744  pushed_cond_guards(NULL),
745  sj_convert_priority(0),
746  expr_join_nest(NULL),
747  exec_method(NOT_TRANSFORMED),
748  upper_item(NULL)
749 {
750  init(select_lex, new select_exists_subselect(this));
751  max_columns= UINT_MAX;
752  maybe_null= 1;
753  abort_on_null= 0;
754  reset();
755  //if test_limit will fail then error will be reported to client
756  test_limit(select_lex->master_unit());
757  return;
758 }
759 
760 Item_allany_subselect::Item_allany_subselect(Item * left_exp,
761  chooser_compare_func_creator fc,
762  Select_Lex *select_lex,
763  bool all_arg)
764  :Item_in_subselect(), func_creator(fc), all(all_arg)
765 {
766  left_expr= left_exp;
767  func= func_creator(all_arg);
768  init(select_lex, new select_exists_subselect(this));
769  max_columns= 1;
770  abort_on_null= 0;
771  reset();
772  //if test_limit will fail then error will be reported to client
773  test_limit(select_lex->master_unit());
774  return;
775 }
776 
777 
778 void Item_exists_subselect::fix_length_and_dec()
779 {
780  decimals= 0;
781  max_length= 1;
782  max_columns= engine->cols();
783  /* We need only 1 row to determine existence */
784  unit->global_parameters->select_limit= new Item_int((int32_t) 1);
785 }
786 
788 {
789  assert(fixed == 1);
790  if (exec())
791  {
792  reset();
793  return 0;
794  }
795  return (double) value;
796 }
797 
799 {
800  assert(fixed == 1);
801  if (exec())
802  {
803  reset();
804  return 0;
805  }
806  return value;
807 }
808 
810 {
811  assert(fixed == 1);
812  if (exec())
813  {
814  reset();
815  return 0;
816  }
817  str->set((uint64_t)value,&my_charset_bin);
818  return str;
819 }
820 
821 
823 {
824  assert(fixed == 1);
825  if (exec())
826  {
827  reset();
828  return 0;
829  }
830  int2_class_decimal(E_DEC_FATAL_ERROR, value, 0, decimal_value);
831  return decimal_value;
832 }
833 
834 
836 {
837  assert(fixed == 1);
838  if (exec())
839  {
840  reset();
841  return 0;
842  }
843  return value != 0;
844 }
845 
846 
848 {
849  /*
850  As far as Item_in_subselect called only from Item_in_optimizer this
851  method should not be used
852  */
853  assert(0);
854  assert(fixed == 1);
855  null_value= 0;
856  if (exec())
857  {
858  reset();
859  null_value= 1;
860  return 0;
861  }
862  if (was_null && !value)
863  null_value= 1;
864  return (double) value;
865 }
866 
867 
869 {
870  /*
871  As far as Item_in_subselect called only from Item_in_optimizer this
872  method should not be used
873  */
874  assert(fixed == 1);
875  null_value= 0;
876  if (exec())
877  {
878  reset();
879  null_value= 1;
880  return 0;
881  }
882  if (was_null && !value)
883  null_value= 1;
884  return value;
885 }
886 
887 
889 {
890  /*
891  As far as Item_in_subselect called only from Item_in_optimizer this
892  method should not be used
893  */
894  assert(0);
895  assert(fixed == 1);
896  null_value= 0;
897  if (exec())
898  {
899  reset();
900  null_value= 1;
901  return 0;
902  }
903  if (was_null && !value)
904  {
905  null_value= 1;
906  return 0;
907  }
908  str->set((uint64_t)value, &my_charset_bin);
909  return str;
910 }
911 
912 
914 {
915  assert(fixed == 1);
916  null_value= 0;
917  if (exec())
918  {
919  reset();
920  /*
921  Must mark the IN predicate as NULL so as to make sure an enclosing NOT
922  predicate will return false. See the comments in
923  subselect_uniquesubquery_engine::copy_ref_key for further details.
924  */
925  null_value= 1;
926  return 0;
927  }
928  if (was_null && !value)
929  null_value= 1;
930  return value;
931 }
932 
934 {
935  /*
936  As far as Item_in_subselect called only from Item_in_optimizer this
937  method should not be used
938  */
939  assert(0);
940  null_value= 0;
941  assert(fixed == 1);
942  if (exec())
943  {
944  reset();
945  null_value= 1;
946  return 0;
947  }
948  if (was_null && !value)
949  null_value= 1;
950  int2_class_decimal(E_DEC_FATAL_ERROR, value, 0, decimal_value);
951  return decimal_value;
952 }
953 
954 
955 /*
956  Rewrite a single-column IN/ALL/ANY subselect
957 
958  SYNOPSIS
959  Item_in_subselect::single_value_transformer()
960  join Join object of the subquery (i.e. 'child' join).
961  func Subquery comparison creator
962 
963  DESCRIPTION
964  Rewrite a single-column subquery using rule-based approach. The subquery
965 
966  oe $cmp$ (SELECT ie FROM ... WHERE subq_where ... HAVING subq_having)
967 
968  First, try to convert the subquery to scalar-result subquery in one of
969  the forms:
970 
971  - oe $cmp$ (SELECT MAX(...) ) // handled by Item_singlerow_subselect
972  - oe $cmp$ <max>(SELECT ...) // handled by Item_maxmin_subselect
973 
974  If that fails, the subquery will be handled with class Item_in_optimizer.
975  There are two possibilites:
976  - If the subquery execution method is materialization, then the subquery is
977  not transformed any further.
978  - Otherwise the IN predicates is transformed into EXISTS by injecting
979  equi-join predicates and possibly other helper predicates. For details
980  see method single_value_in_like_transformer().
981 
982  RETURN
983  RES_OK Either subquery was transformed, or appopriate
984  predicates where injected into it.
985  RES_REDUCE The subquery was reduced to non-subquery
986  RES_ERROR Error
987 */
988 
989 Item_subselect::trans_res
990 Item_in_subselect::single_value_transformer(Join *join,
991  const Comp_creator *func)
992 {
993  Select_Lex *select_lex= join->select_lex;
994 
995  /*
996  Check that the right part of the subselect contains no more than one
997  column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
998  */
999  if (select_lex->item_list.size() > 1)
1000  {
1001  my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
1002  return(RES_ERROR);
1003  }
1004 
1005  /*
1006  If this is an ALL/ANY single-value subselect, try to rewrite it with
1007  a MIN/MAX subselect. We can do that if a possible NULL result of the
1008  subselect can be ignored.
1009  E.g. SELECT * FROM t1 WHERE b > ANY (SELECT a FROM t2) can be rewritten
1010  with SELECT * FROM t1 WHERE b > (SELECT MAX(a) FROM t2).
1011  We can't check that this optimization is safe if it's not a top-level
1012  item of the WHERE clause (e.g. because the WHERE clause can contain IS
1013  NULL/IS NOT NULL functions). If so, we rewrite ALL/ANY with NOT EXISTS
1014  later in this method.
1015  */
1016  if ((abort_on_null || (upper_item && upper_item->top_level())) &&
1017  select_lex->master_unit()->uncacheable.none() && !func->eqne_op())
1018  {
1019  if (substitution)
1020  {
1021  // It is second (third, ...) SELECT of UNION => All is done
1022  return(RES_OK);
1023  }
1024 
1025  Item *subs;
1026  if (!select_lex->group_list.elements &&
1027  !select_lex->having &&
1028  !select_lex->with_sum_func &&
1029  !(select_lex->next_select()) &&
1030  select_lex->table_list.elements)
1031  {
1032  Item_sum_hybrid *item;
1033  nesting_map save_allow_sum_func;
1034  if (func->l_op())
1035  {
1036  /*
1037  (ALL && (> || =>)) || (ANY && (< || =<))
1038  for ALL condition is inverted
1039  */
1040  item= new Item_sum_max(*select_lex->ref_pointer_array);
1041  }
1042  else
1043  {
1044  /*
1045  (ALL && (< || =<)) || (ANY && (> || =>))
1046  for ALL condition is inverted
1047  */
1048  item= new Item_sum_min(*select_lex->ref_pointer_array);
1049  }
1050  if (upper_item)
1051  upper_item->set_sum_test(item);
1052  *select_lex->ref_pointer_array= item;
1053  {
1054  List<Item>::iterator it(select_lex->item_list.begin());
1055  it++;
1056  it.replace(item);
1057  }
1058 
1059  save_allow_sum_func= session->lex().allow_sum_func;
1060  session->lex().allow_sum_func|= 1 << session->lex().current_select->nest_level;
1061  /*
1062  Item_sum_(max|min) can't substitute other item => we can use 0 as
1063  reference, also Item_sum_(max|min) can't be fixed after creation, so
1064  we do not check item->fixed
1065  */
1066  if (item->fix_fields(session, 0))
1067  return(RES_ERROR);
1068  session->lex().allow_sum_func= save_allow_sum_func;
1069  /* we added aggregate function => we have to change statistic */
1070  count_field_types(select_lex, &join->tmp_table_param, join->all_fields,
1071  0);
1072 
1073  subs= new Item_singlerow_subselect(select_lex);
1074  }
1075  else
1076  {
1077  Item_maxmin_subselect *item;
1078  subs= item= new Item_maxmin_subselect(session, this, select_lex, func->l_op());
1079  if (upper_item)
1080  upper_item->set_sub_test(item);
1081  }
1082  /* fix fields is already called for left expression */
1083  substitution= func->create(left_expr, subs);
1084  return(RES_OK);
1085  }
1086 
1087  if (!substitution)
1088  {
1089  /* We're invoked for the 1st (or the only) SELECT in the subquery UNION */
1090  Select_Lex_Unit *master_unit= select_lex->master_unit();
1091  substitution= optimizer;
1092 
1093  Select_Lex *current= session->lex().current_select, *up;
1094 
1095  session->lex().current_select= up= current->return_after_parsing();
1096  //optimizer never use Item **ref => we can pass 0 as parameter
1097  if (!optimizer || optimizer->fix_left(session, 0))
1098  {
1099  session->lex().current_select= current;
1100  return(RES_ERROR);
1101  }
1102  session->lex().current_select= current;
1103 
1104  /*
1105  As far as Item_ref_in_optimizer do not substitute itself on fix_fields
1106  we can use same item for all selects.
1107  */
1108  expr= new Item_direct_ref(&select_lex->context, (Item**)optimizer->get_cache(), "<no matter>", in_left_expr_name);
1109 
1110  master_unit->uncacheable.set(UNCACHEABLE_DEPENDENT);
1111  }
1112 
1113  if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards)
1114  {
1115  pushed_cond_guards= new (join->session->mem) bool;
1116  pushed_cond_guards[0]= true;
1117  }
1118 
1119  /*
1120  If this IN predicate can be computed via materialization, do not
1121  perform the IN -> EXISTS transformation.
1122  */
1123  if (exec_method == MATERIALIZATION)
1124  return(RES_OK);
1125 
1126  /* Perform the IN=>EXISTS transformation. */
1127  return(single_value_in_to_exists_transformer(join, func));
1128 }
1129 
1130 
1167 Item_subselect::trans_res
1169 {
1170  Select_Lex *select_lex= join->select_lex;
1171 
1172  select_lex->uncacheable.set(UNCACHEABLE_DEPENDENT);
1173  if (join->having || select_lex->with_sum_func ||
1174  select_lex->group_list.elements)
1175  {
1176  bool tmp;
1177  Item *item= func->create(expr, new Item_ref_null_helper(&select_lex->context, this, select_lex->ref_pointer_array, "<ref>", this->full_name()));
1178  if (!abort_on_null && left_expr->maybe_null)
1179  {
1180  /*
1181  We can encounter "NULL IN (SELECT ...)". Wrap the added condition
1182  within a trig_cond.
1183  */
1184  item= new Item_func_trig_cond(item, get_cond_guard(0));
1185  }
1186 
1187  /*
1188  AND and comparison functions can't be changed during fix_fields()
1189  we can assign select_lex->having here, and pass 0 as last
1190  argument (reference) to fix_fields()
1191  */
1192  select_lex->having= join->having= and_items(join->having, item);
1193  if (join->having == item)
1194  item->name= (char*)in_having_cond;
1195  select_lex->having->top_level_item();
1196  select_lex->having_fix_field= 1;
1197  /*
1198  we do not check join->having->fixed, because Item_and (from and_items)
1199  or comparison function (from func->create) can't be fixed after creation
1200  */
1201  tmp= join->having->fix_fields(session, 0);
1202  select_lex->having_fix_field= 0;
1203  if (tmp)
1204  return(RES_ERROR);
1205  }
1206  else
1207  {
1208  Item *item= &select_lex->item_list.front();
1209 
1210  if (select_lex->table_list.elements)
1211  {
1212  bool tmp;
1213  Item *having= item, *orig_item= item;
1214  select_lex->item_list.clear();
1215  select_lex->item_list.push_back(new Item_int("Not_used", 1, MY_INT64_NUM_DECIMAL_DIGITS));
1216  select_lex->ref_pointer_array[0]= &select_lex->item_list.front();
1217 
1218  item= func->create(expr, item);
1219  if (!abort_on_null && orig_item->maybe_null)
1220  {
1221  having= new Item_is_not_null_test(this, having);
1222  if (left_expr->maybe_null)
1223  having= new Item_func_trig_cond(having, get_cond_guard(0));
1224  /*
1225  Item_is_not_null_test can't be changed during fix_fields()
1226  we can assign select_lex->having here, and pass 0 as last
1227  argument (reference) to fix_fields()
1228  */
1229  having->name= (char*)in_having_cond;
1230  select_lex->having= join->having= having;
1231  select_lex->having_fix_field= 1;
1232  /*
1233  we do not check join->having->fixed, because Item_and (from
1234  and_items) or comparison function (from func->create) can't be
1235  fixed after creation
1236  */
1237  tmp= join->having->fix_fields(session, 0);
1238  select_lex->having_fix_field= 0;
1239  if (tmp)
1240  return(RES_ERROR);
1241  item= new Item_cond_or(item, new Item_func_isnull(orig_item));
1242  }
1243  /*
1244  If we may encounter NULL IN (SELECT ...) and care whether subquery
1245  result is NULL or false, wrap condition in a trig_cond.
1246  */
1247  if (!abort_on_null && left_expr->maybe_null)
1248  item= new Item_func_trig_cond(item, get_cond_guard(0));
1249  /*
1250  TODO: figure out why the following is done here in
1251  single_value_transformer but there is no corresponding action in
1252  row_value_transformer?
1253  */
1254  item->name= (char *)in_additional_cond;
1255 
1256  /*
1257  AND can't be changed during fix_fields()
1258  we can assign select_lex->having here, and pass 0 as last
1259  argument (reference) to fix_fields()
1260  */
1261  select_lex->where= join->conds= and_items(join->conds, item);
1262  select_lex->where->top_level_item();
1263  /*
1264  we do not check join->conds->fixed, because Item_and can't be fixed
1265  after creation
1266  */
1267  if (join->conds->fix_fields(session, 0))
1268  return(RES_ERROR);
1269  }
1270  else
1271  {
1272  bool tmp;
1273  if (select_lex->master_unit()->is_union())
1274  {
1275  /*
1276  comparison functions can't be changed during fix_fields()
1277  we can assign select_lex->having here, and pass 0 as last
1278  argument (reference) to fix_fields()
1279  */
1280  Item *new_having= func->create(expr, new Item_ref_null_helper(&select_lex->context, this, select_lex->ref_pointer_array, "<no matter>", "<result>"));
1281  if (!abort_on_null && left_expr->maybe_null)
1282  new_having= new Item_func_trig_cond(new_having, get_cond_guard(0));
1283  new_having->name= in_having_cond;
1284  select_lex->having= join->having= new_having;
1285  select_lex->having_fix_field= 1;
1286 
1287  /*
1288  we do not check join->having->fixed, because comparison function
1289  (from func->create) can't be fixed after creation
1290  */
1291  tmp= join->having->fix_fields(session, 0);
1292  select_lex->having_fix_field= 0;
1293  if (tmp)
1294  return(RES_ERROR);
1295  }
1296  else
1297  {
1298  // it is single select without tables => possible optimization
1299  item= func->create(left_expr, item);
1300  // fix_field of item will be done in time of substituting
1301  substitution= item;
1302  have_to_be_excluded= 1;
1303  if (session->lex().describe)
1304  {
1305  char warn_buff[DRIZZLE_ERRMSG_SIZE];
1306  snprintf(warn_buff, sizeof(warn_buff), ER(ER_SELECT_REDUCED), select_lex->select_number);
1307  push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1308  ER_SELECT_REDUCED, warn_buff);
1309  }
1310  return(RES_REDUCE);
1311  }
1312  }
1313  }
1314 
1315  return(RES_OK);
1316 }
1317 
1318 
1319 Item_subselect::trans_res
1320 Item_in_subselect::row_value_transformer(Join *join)
1321 {
1322  Select_Lex *select_lex= join->select_lex;
1323  uint32_t cols_num= left_expr->cols();
1324 
1325  if (select_lex->item_list.size() != left_expr->cols())
1326  {
1327  my_error(ER_OPERAND_COLUMNS, MYF(0), left_expr->cols());
1328  return(RES_ERROR);
1329  }
1330 
1331  /*
1332  Wrap the current IN predicate in an Item_in_optimizer. The actual
1333  substitution in the Item tree takes place in Item_subselect::fix_fields.
1334  */
1335  if (!substitution)
1336  {
1337  //first call for this unit
1338  Select_Lex_Unit *master_unit= select_lex->master_unit();
1339  substitution= optimizer;
1340 
1341  Select_Lex *current= session->lex().current_select, *up;
1342  session->lex().current_select= up= current->return_after_parsing();
1343  //optimizer never use Item **ref => we can pass 0 as parameter
1344  if (!optimizer || optimizer->fix_left(session, 0))
1345  {
1346  session->lex().current_select= current;
1347  return(RES_ERROR);
1348  }
1349 
1350  // we will refer to upper level cache array => we have to save it in PS
1351  optimizer->keep_top_level_cache();
1352 
1353  session->lex().current_select= current;
1354  master_unit->uncacheable.set(UNCACHEABLE_DEPENDENT);
1355 
1356  if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards)
1357  {
1358  pushed_cond_guards= new (join->session->mem) bool[left_expr->cols()];
1359  for (uint32_t i= 0; i < cols_num; i++)
1360  pushed_cond_guards[i]= true;
1361  }
1362  }
1363 
1364  /*
1365  If this IN predicate can be computed via materialization, do not
1366  perform the IN -> EXISTS transformation.
1367  */
1368  if (exec_method == MATERIALIZATION)
1369  return(RES_OK);
1370 
1371  /* Perform the IN=>EXISTS transformation. */
1372  return(row_value_in_to_exists_transformer(join));
1373 }
1374 
1375 
1394 Item_subselect::trans_res
1396 {
1397  Select_Lex *select_lex= join->select_lex;
1398  Item *having_item= 0;
1399  uint32_t cols_num= left_expr->cols();
1400  bool is_having_used= (join->having || select_lex->with_sum_func ||
1401  select_lex->group_list.first ||
1402  !select_lex->table_list.elements);
1403 
1404  select_lex->uncacheable.set(UNCACHEABLE_DEPENDENT);
1405  if (is_having_used)
1406  {
1407  /*
1408  (l1, l2, l3) IN (SELECT v1, v2, v3 ... HAVING having) =>
1409  EXISTS (SELECT ... HAVING having and
1410  (l1 = v1 or is null v1) and
1411  (l2 = v2 or is null v2) and
1412  (l3 = v3 or is null v3) and
1413  is_not_null_test(v1) and
1414  is_not_null_test(v2) and
1415  is_not_null_test(v3))
1416  where is_not_null_test used to register nulls in case if we have
1417  not found matching to return correct NULL value
1418  TODO: say here explicitly if the order of AND parts matters or not.
1419  */
1420  Item *item_having_part2= 0;
1421  for (uint32_t i= 0; i < cols_num; i++)
1422  {
1423  assert((left_expr->fixed && select_lex->ref_pointer_array[i]->fixed) ||
1424  (select_lex->ref_pointer_array[i]->type() == REF_ITEM &&
1425  ((Item_ref*)(select_lex->ref_pointer_array[i]))->ref_type() ==
1426  Item_ref::OUTER_REF));
1427  if (select_lex->ref_pointer_array[i]->
1428  check_cols(left_expr->element_index(i)->cols()))
1429  return(RES_ERROR);
1430  Item *item_eq=
1431  new Item_func_eq(new
1432  Item_ref(&select_lex->context,
1433  (*optimizer->get_cache())->
1434  addr(i),
1435  "<no matter>",
1437  new
1438  Item_ref(&select_lex->context,
1439  select_lex->ref_pointer_array + i,
1440  "<no matter>",
1441  "<list ref>")
1442  );
1443  Item *item_isnull=
1444  new Item_func_isnull(new
1445  Item_ref(&select_lex->context,
1446  select_lex->ref_pointer_array+i,
1447  "<no matter>",
1448  "<list ref>")
1449  );
1450  Item *col_item= new Item_cond_or(item_eq, item_isnull);
1451  if (!abort_on_null && left_expr->element_index(i)->maybe_null)
1452  col_item= new Item_func_trig_cond(col_item, get_cond_guard(i));
1453  having_item= and_items(having_item, col_item);
1454 
1455  Item *item_nnull_test=
1456  new Item_is_not_null_test(this,
1457  new Item_ref(&select_lex->context,
1458  select_lex->
1459  ref_pointer_array + i,
1460  "<no matter>",
1461  "<list ref>"));
1462  if (!abort_on_null && left_expr->element_index(i)->maybe_null)
1463  {
1464  item_nnull_test= new Item_func_trig_cond(item_nnull_test, get_cond_guard(i));
1465  }
1466  item_having_part2= and_items(item_having_part2, item_nnull_test);
1467  item_having_part2->top_level_item();
1468  }
1469  having_item= and_items(having_item, item_having_part2);
1470  having_item->top_level_item();
1471  }
1472  else
1473  {
1474  /*
1475  (l1, l2, l3) IN (SELECT v1, v2, v3 ... WHERE where) =>
1476  EXISTS (SELECT ... WHERE where and
1477  (l1 = v1 or is null v1) and
1478  (l2 = v2 or is null v2) and
1479  (l3 = v3 or is null v3)
1480  HAVING is_not_null_test(v1) and
1481  is_not_null_test(v2) and
1482  is_not_null_test(v3))
1483  where is_not_null_test register NULLs values but reject rows
1484 
1485  in case when we do not need correct NULL, we have simplier construction:
1486  EXISTS (SELECT ... WHERE where and
1487  (l1 = v1) and
1488  (l2 = v2) and
1489  (l3 = v3)
1490  */
1491  Item *where_item= 0;
1492  for (uint32_t i= 0; i < cols_num; i++)
1493  {
1494  Item *item, *item_isnull;
1495  assert((left_expr->fixed && select_lex->ref_pointer_array[i]->fixed) ||
1496  (select_lex->ref_pointer_array[i]->type() == REF_ITEM &&
1497  ((Item_ref*)(select_lex->ref_pointer_array[i]))->ref_type() ==
1498  Item_ref::OUTER_REF));
1499  if (select_lex->ref_pointer_array[i]->
1500  check_cols(left_expr->element_index(i)->cols()))
1501  return(RES_ERROR);
1502  item=
1503  new Item_func_eq(new
1504  Item_direct_ref(&select_lex->context,
1505  (*optimizer->get_cache())->
1506  addr(i),
1507  "<no matter>",
1509  new
1510  Item_direct_ref(&select_lex->context,
1511  select_lex->
1512  ref_pointer_array+i,
1513  "<no matter>",
1514  "<list ref>")
1515  );
1516  if (!abort_on_null)
1517  {
1518  Item *having_col_item=
1519  new Item_is_not_null_test(this,
1520  new
1521  Item_ref(&select_lex->context,
1522  select_lex->ref_pointer_array + i,
1523  "<no matter>",
1524  "<list ref>"));
1525 
1526 
1527  item_isnull= new
1528  Item_func_isnull(new
1529  Item_direct_ref(&select_lex->context,
1530  select_lex->
1531  ref_pointer_array+i,
1532  "<no matter>",
1533  "<list ref>")
1534  );
1535  item= new Item_cond_or(item, item_isnull);
1536  /*
1537  TODO: why we create the above for cases where the right part
1538  cant be NULL?
1539  */
1540  if (left_expr->element_index(i)->maybe_null)
1541  {
1542  item= new Item_func_trig_cond(item, get_cond_guard(i));
1543  having_col_item= new Item_func_trig_cond(having_col_item, get_cond_guard(i));
1544  }
1545  having_item= and_items(having_item, having_col_item);
1546  }
1547  where_item= and_items(where_item, item);
1548  }
1549  /*
1550  AND can't be changed during fix_fields()
1551  we can assign select_lex->where here, and pass 0 as last
1552  argument (reference) to fix_fields()
1553  */
1554  select_lex->where= join->conds= and_items(join->conds, where_item);
1555  select_lex->where->top_level_item();
1556  if (join->conds->fix_fields(session, 0))
1557  return(RES_ERROR);
1558  }
1559  if (having_item)
1560  {
1561  bool res;
1562  select_lex->having= join->having= and_items(join->having, having_item);
1563  if (having_item == select_lex->having)
1564  having_item->name= (char*)in_having_cond;
1565  select_lex->having->top_level_item();
1566  /*
1567  AND can't be changed during fix_fields()
1568  we can assign select_lex->having here, and pass 0 as last
1569  argument (reference) to fix_fields()
1570  */
1571  select_lex->having_fix_field= 1;
1572  res= join->having->fix_fields(session, 0);
1573  select_lex->having_fix_field= 0;
1574  if (res)
1575  {
1576  return(RES_ERROR);
1577  }
1578  }
1579 
1580  return(RES_OK);
1581 }
1582 
1583 
1584 Item_subselect::trans_res
1585 Item_in_subselect::select_transformer(Join *join)
1586 {
1587  return select_in_like_transformer(join, Eq_creator::instance());
1588 }
1589 
1590 
1612 Item_subselect::trans_res
1614 {
1615  Select_Lex *current= session->lex().current_select, *up;
1616  const char *save_where= session->where();
1617  Item_subselect::trans_res res= RES_ERROR;
1618  bool result;
1619 
1620  {
1621  /*
1622  IN/SOME/ALL/ANY subqueries aren't support LIMIT clause. Without it
1623  ORDER BY clause becomes meaningless thus we drop it here.
1624  */
1625  Select_Lex *sl= current->master_unit()->first_select();
1626  for (; sl; sl= sl->next_select())
1627  {
1628  if (sl->join)
1629  sl->join->order= 0;
1630  }
1631  }
1632 
1633  if (changed)
1634  return(RES_OK);
1635 
1636  session->setWhere("IN/ALL/ANY subquery");
1637 
1638  /*
1639  In some optimisation cases we will not need this Item_in_optimizer
1640  object, but we can't know it here, but here we need address correct
1641  reference on left expresion.
1642  */
1643  if (!optimizer)
1644  {
1645  result= (!(optimizer= new Item_in_optimizer(left_expr, this)));
1646  if (result)
1647  goto err;
1648  }
1649 
1650  session->lex().current_select= up= current->return_after_parsing();
1651  result= (!left_expr->fixed &&
1652  left_expr->fix_fields(session, optimizer->arguments()));
1653  /* fix_fields can change reference to left_expr, we need reassign it */
1654  left_expr= optimizer->arguments()[0];
1655 
1656  session->lex().current_select= current;
1657  if (result)
1658  goto err;
1659 
1660  /*
1661  If we didn't choose an execution method up to this point, we choose
1662  the IN=>EXISTS transformation.
1663  */
1664  if (exec_method == NOT_TRANSFORMED)
1665  exec_method= IN_TO_EXISTS;
1666 
1667  /*
1668  Both transformers call fix_fields() only for Items created inside them,
1669  and all those items do not make permanent changes in the current item arena
1670  which allows us to call them with changed arena (if we do not know the
1671  nature of Item, we have to call fix_fields() for it only with the original
1672  arena to avoid memory leak).
1673  */
1674  if (left_expr->cols() == 1)
1675  res= single_value_transformer(join, func);
1676  else
1677  {
1678  /* we do not support row operation for ALL/ANY/SOME */
1679  if (func != Eq_creator::instance())
1680  {
1681  my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
1682  return(RES_ERROR);
1683  }
1684  res= row_value_transformer(join);
1685  }
1686 err:
1687  session->setWhere(save_where);
1688  return res;
1689 }
1690 
1691 
1693 {
1694  if (exec_method == IN_TO_EXISTS)
1695  str->append(STRING_WITH_LEN("<exists>"));
1696  else
1697  {
1698  left_expr->print(str);
1699  str->append(STRING_WITH_LEN(" in "));
1700  }
1701  Item_subselect::print(str);
1702 }
1703 
1704 
1705 bool Item_in_subselect::fix_fields(Session *session_arg, Item **ref)
1706 {
1707  bool result = 0;
1708 
1709  if (exec_method == SEMI_JOIN)
1710  return !( (*ref)= new Item_int(1));
1711 
1712  return result || Item_subselect::fix_fields(session_arg, ref);
1713 }
1714 
1715 
1741 {
1742  subselect_hash_sj_engine *new_engine= NULL;
1743  bool res= false;
1744 
1745  if (engine->engine_type() == subselect_engine::SINGLE_SELECT_ENGINE)
1746  {
1747  /* Create/initialize objects in permanent memory. */
1748  subselect_single_select_engine *old_engine_ptr;
1749 
1750  old_engine_ptr= static_cast<subselect_single_select_engine *>(engine);
1751  new_engine= new subselect_hash_sj_engine(session, this, old_engine_ptr);
1752  if (new_engine->init_permanent(unit->get_unit_column_types()))
1753  {
1754  Item_subselect::trans_res new_trans_res;
1755  /*
1756  If for some reason we cannot use materialization for this IN predicate,
1757  delete all materialization-related objects, and apply the IN=>EXISTS
1758  transformation.
1759  */
1760  delete new_engine;
1761  new_engine= NULL;
1762  exec_method= NOT_TRANSFORMED;
1763  if (left_expr->cols() == 1)
1764  new_trans_res= single_value_in_to_exists_transformer(old_engine_ptr->join, Eq_creator::instance());
1765  else
1766  new_trans_res= row_value_in_to_exists_transformer(old_engine_ptr->join);
1767  res= (new_trans_res != Item_subselect::RES_OK);
1768  }
1769  if (new_engine)
1770  engine= new_engine;
1771  }
1772  else
1773  {
1774  assert(engine->engine_type() == subselect_engine::HASH_SJ_ENGINE);
1775  new_engine= static_cast<subselect_hash_sj_engine *>(engine);
1776  }
1777 
1778  /* Initilizations done in runtime memory, repeated for each execution. */
1779  if (new_engine)
1780  {
1781  /*
1782  Reset the LIMIT 1 set in Item_exists_subselect::fix_length_and_dec.
1783  TODO:
1784  Currently we set the subquery LIMIT to infinity, and this is correct
1785  because we forbid at parse time LIMIT inside IN subqueries (see
1786  Item_in_subselect::test_limit). However, once we allow this, here
1787  we should set the correct limit if given in the query.
1788  */
1789  unit->global_parameters->select_limit= NULL;
1790  new_engine->init_runtime();
1791  }
1792 
1793  return res;
1794 }
1795 
1796 
1810 {
1811  Join *outer_join= NULL;
1812 
1813  outer_join= unit->outer_select()->join;
1814  if (! outer_join || ! outer_join->tables || ! outer_join->join_tab)
1815  return true;
1816 
1817  left_expr_cache= new List<Cached_item>;
1818 
1819  for (uint32_t i= 0; i < left_expr->cols(); i++)
1820  {
1821  Cached_item *cur_item_cache= new_Cached_item(session, left_expr->element_index(i));
1822  if (!cur_item_cache)
1823  return true;
1824  left_expr_cache->push_front(cur_item_cache);
1825  }
1826  return false;
1827 }
1828 
1829 
1830 /*
1831  Callback to test if an IN predicate is expensive.
1832 
1833  @details
1834  IN predicates are considered expensive only if they will be executed via
1835  materialization. The return value affects the behavior of
1836  make_cond_for_table() in such a way that it is unchanged when we use
1837  the IN=>EXISTS transformation to compute IN.
1838 
1839  @retval true if the predicate is expensive
1840  @retval false otherwise
1841 */
1842 
1843 bool Item_in_subselect::is_expensive_processor(unsigned char *)
1844 {
1845  return exec_method == MATERIALIZATION;
1846 }
1847 
1848 
1849 Item_subselect::trans_res
1850 Item_allany_subselect::select_transformer(Join *join)
1851 {
1852  exec_method= IN_TO_EXISTS;
1853  if (upper_item)
1854  upper_item->show= 1;
1855  return(select_in_like_transformer(join, func));
1856 }
1857 
1858 
1860 {
1861  if (exec_method == IN_TO_EXISTS)
1862  {
1863  str->append(STRING_WITH_LEN("<exists>"));
1864  }
1865  else
1866  {
1867  left_expr->print(str);
1868  str->append(' ');
1869  str->append(func->symbol(all), strlen(func->symbol(all)));
1870  str->append(all ? " all " : " any ", 5);
1871  }
1872  Item_subselect::print(str);
1873 }
1874 
1875 
1876 void subselect_engine::set_session(Session *session_arg)
1877 {
1878  session= session_arg;
1879  if (result)
1880  {
1881  result->set_session(session_arg);
1882  }
1883 }
1884 
1885 
1886 subselect_single_select_engine::
1887 subselect_single_select_engine(Select_Lex *select,
1888  select_result_interceptor *result_arg,
1889  Item_subselect *item_arg)
1890  :subselect_engine(item_arg, result_arg),
1891  prepared(0), executed(0), select_lex(select), join(0)
1892 {
1893  select_lex->master_unit()->item= item_arg;
1894 }
1895 
1896 
1897 void subselect_single_select_engine::cleanup()
1898 {
1899  prepared= executed= 0;
1900  join= 0;
1901  result->cleanup();
1902  return;
1903 }
1904 
1905 
1906 void subselect_union_engine::cleanup()
1907 {
1908  unit->reinit_exec_mechanism();
1909  result->cleanup();
1910  return;
1911 }
1912 
1913 
1914 bool subselect_union_engine::is_executed() const
1915 {
1916  return unit->executed;
1917 }
1918 
1919 
1920 /*
1921  Check if last execution of the subquery engine produced any rows
1922 
1923  SYNOPSIS
1924  subselect_union_engine::no_rows()
1925 
1926  DESCRIPTION
1927  Check if last execution of the subquery engine produced any rows. The
1928  return value is undefined if last execution ended in an error.
1929 
1930  RETURN
1931  true - Last subselect execution has produced no rows
1932  false - Otherwise
1933 */
1934 
1935 bool subselect_union_engine::no_rows()
1936 {
1937  /* Check if we got any rows when reading UNION result from temp. table: */
1938  return test(!unit->fake_select_lex->join->send_records);
1939 }
1940 
1941 
1942 void subselect_uniquesubquery_engine::cleanup()
1943 {
1944  /* Tell handler we don't need the index anymore */
1945  if (tab->table->cursor->inited)
1946  tab->table->cursor->endIndexScan();
1947  return;
1948 }
1949 
1950 
1951 subselect_union_engine::subselect_union_engine(Select_Lex_Unit *u,
1952  select_result_interceptor *result_arg,
1953  Item_subselect *item_arg)
1954  :subselect_engine(item_arg, result_arg)
1955 {
1956  unit= u;
1957  unit->item= item_arg;
1958 }
1959 
1960 
1988 {
1989  if (prepared)
1990  return 0;
1991  join= new Join(session, select_lex->item_list, select_lex->options | SELECT_NO_UNLOCK, result);
1992  if (not result)
1993  return 1; /* Fatal error is set already. */
1994  prepared= 1;
1995  Select_Lex *save_select= session->lex().current_select;
1996  session->lex().current_select= select_lex;
1997  if (join->prepare(&select_lex->ref_pointer_array,
1998  (TableList*) select_lex->table_list.first,
1999  select_lex->with_wild,
2000  select_lex->where,
2001  select_lex->order_list.elements +
2002  select_lex->group_list.elements,
2003  (Order*) select_lex->order_list.first,
2004  (Order*) select_lex->group_list.first,
2005  select_lex->having,
2006  select_lex, select_lex->master_unit()))
2007  return 1;
2008  session->lex().current_select= save_select;
2009  return 0;
2010 }
2011 
2012 int subselect_union_engine::prepare()
2013 {
2014  return unit->prepare(session, result, (uint32_t)SELECT_NO_UNLOCK);
2015 }
2016 
2017 int subselect_uniquesubquery_engine::prepare()
2018 {
2019  /* Should never be called. */
2020  assert(false);
2021  return 1;
2022 }
2023 
2024 
2025 /*
2026  Check if last execution of the subquery engine produced any rows
2027 
2028  SYNOPSIS
2029  subselect_single_select_engine::no_rows()
2030 
2031  DESCRIPTION
2032  Check if last execution of the subquery engine produced any rows. The
2033  return value is undefined if last execution ended in an error.
2034 
2035  RETURN
2036  true - Last subselect execution has produced no rows
2037  false - Otherwise
2038 */
2039 
2040 bool subselect_single_select_engine::no_rows()
2041 {
2042  return !item->assigned();
2043 }
2044 
2045 
2046 /*
2047  makes storage for the output values for the subquery and calcuates
2048  their data and column types and their nullability.
2049 */
2050 void subselect_engine::set_row(List<Item> &item_list, Item_cache **row)
2051 {
2052  Item *sel_item;
2053  List<Item>::iterator li(item_list.begin());
2054  res_type= STRING_RESULT;
2055  res_field_type= DRIZZLE_TYPE_VARCHAR;
2056  for (uint32_t i= 0; (sel_item= li++); i++)
2057  {
2058  item->max_length= sel_item->max_length;
2059  res_type= sel_item->result_type();
2060  res_field_type= sel_item->field_type();
2061  item->decimals= sel_item->decimals;
2062  item->unsigned_flag= sel_item->unsigned_flag;
2063  maybe_null= sel_item->maybe_null;
2064  if (!(row[i]= Item_cache::get_cache(sel_item)))
2065  return;
2066  row[i]->setup(sel_item);
2067  }
2068  if (item_list.size() > 1)
2069  res_type= ROW_RESULT;
2070 }
2071 
2072 void subselect_single_select_engine::fix_length_and_dec(Item_cache **row)
2073 {
2074  assert(row || select_lex->item_list.size() == 1);
2075  set_row(select_lex->item_list, row);
2076  item->collation.set(row[0]->collation);
2077  if (cols() != 1)
2078  maybe_null= 0;
2079 }
2080 
2081 void subselect_union_engine::fix_length_and_dec(Item_cache **row)
2082 {
2083  assert(row || unit->first_select()->item_list.size() == 1);
2084 
2085  if (unit->first_select()->item_list.size() == 1)
2086  {
2087  set_row(unit->types, row);
2088  item->collation.set(row[0]->collation);
2089  }
2090  else
2091  {
2092  bool maybe_null_saved= maybe_null;
2093  set_row(unit->types, row);
2094  maybe_null= maybe_null_saved;
2095  }
2096 }
2097 
2098 void subselect_uniquesubquery_engine::fix_length_and_dec(Item_cache **)
2099 {
2100  //this never should be called
2101  assert(0);
2102 }
2103 
2104 int subselect_single_select_engine::exec()
2105 {
2106  char const *save_where= session->where();
2107  Select_Lex *save_select= session->lex().current_select;
2108  session->lex().current_select= select_lex;
2109  if (!join->optimized)
2110  {
2111  Select_Lex_Unit *unit= select_lex->master_unit();
2112 
2113  unit->set_limit(unit->global_parameters);
2114  if (join->optimize())
2115  {
2116  session->setWhere(save_where);
2117  executed= 1;
2118  session->lex().current_select= save_select;
2119  return(join->error ? join->error : 1);
2120  }
2121  save_join_if_explain();
2122  if (item->engine_changed)
2123  {
2124  return 1;
2125  }
2126  }
2127  if (select_lex->uncacheable.any() &&
2128  ! select_lex->uncacheable.test(UNCACHEABLE_EXPLAIN) &&
2129  executed)
2130  {
2131  if (join->reinit())
2132  {
2133  session->setWhere(save_where);
2134  session->lex().current_select= save_select;
2135  return 1;
2136  }
2137  item->reset();
2138  item->assigned((executed= 0));
2139  }
2140  if (!executed)
2141  {
2142  item->reset_value_registration();
2143  JoinTable *changed_tabs[MAX_TABLES];
2144  JoinTable **last_changed_tab= changed_tabs;
2145  if (item->have_guarded_conds())
2146  {
2147  /*
2148  For at least one of the pushed predicates the following is true:
2149  We should not apply optimizations based on the condition that was
2150  pushed down into the subquery. Those optimizations are ref[_or_null]
2151  acceses. Change them to be full table scans.
2152  */
2153  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
2154  {
2155  JoinTable *tab=join->join_tab+i;
2156  if (tab && tab->keyuse)
2157  {
2158  for (uint32_t key_part= 0;
2159  key_part < tab->ref.key_parts;
2160  key_part++)
2161  {
2162  bool *cond_guard= tab->ref.cond_guards[key_part];
2163  if (cond_guard && !*cond_guard)
2164  {
2165  /* Change the access method to full table scan */
2166  tab->save_read_first_record= tab->read_first_record;
2167  tab->save_read_record= tab->read_record.read_record;
2168  tab->read_first_record= init_read_record_seq;
2169  tab->read_record.record= tab->table->record[0];
2170  tab->read_record.session= join->session;
2171  tab->read_record.ref_length= tab->table->cursor->ref_length;
2172  *(last_changed_tab++)= tab;
2173  break;
2174  }
2175  }
2176  }
2177  }
2178  }
2179 
2180  join->exec();
2181 
2182  /* Enable the optimizations back */
2183  for (JoinTable **ptab= changed_tabs; ptab != last_changed_tab; ptab++)
2184  {
2185  JoinTable *tab= *ptab;
2186  tab->read_record.record= 0;
2187  tab->read_record.ref_length= 0;
2188  tab->read_first_record= tab->save_read_first_record;
2189  tab->read_record.read_record= tab->save_read_record;
2190  }
2191  executed= 1;
2192  session->setWhere(save_where);
2193  session->lex().current_select= save_select;
2194  return(join->error||session->is_fatal_error);
2195  }
2196  session->setWhere(save_where);
2197  session->lex().current_select= save_select;
2198  return 0;
2199 }
2200 
2201 void subselect_single_select_engine::save_join_if_explain()
2202 {
2203  /*
2204  Save this JOIN to join->tmp_join since the original layout will be
2205  replaced when JOIN::exec() calls make_simple_join() if:
2206  1) We are executing an EXPLAIN query
2207  2) An uncacheable flag has not been set for the select_lex. If
2208  set, JOIN::optimize() has already saved the JOIN
2209  3) Call does not come from select_describe()). If it does,
2210  JOIN::exec() will not call make_simple_join() and the JOIN we
2211  plan to save will not be replaced anyway.
2212  4) A temp table is needed. This is what triggers JOIN::exec() to
2213  make a replacement JOIN by calling make_simple_join().
2214  5) The Item_subselect is cacheable
2215  */
2216  if (session->lex().describe && // 1
2217  select_lex->uncacheable.none() && // 2
2218  !(join->select_options & SELECT_DESCRIBE) && // 3
2219  join->need_tmp && // 4
2220  item->const_item()) // 5
2221  {
2222  /*
2223  Save this JOIN to join->tmp_join since the original layout will
2224  be replaced when JOIN::exec() calls make_simple_join() due to
2225  need_tmp==TRUE. The original layout is needed so we can describe
2226  the query. No need to do this if uncacheable != 0 since in this
2227  case the JOIN has already been saved during JOIN::optimize()
2228  */
2229  select_lex->uncacheable.set(UNCACHEABLE_EXPLAIN);
2230  select_lex->master_unit()->uncacheable.set(UNCACHEABLE_EXPLAIN);
2231  join->init_save_join_tab();
2232  }
2233 }
2234 
2235 
2236 int subselect_union_engine::exec()
2237 {
2238  char const *save_where= session->where();
2239  int res= unit->exec();
2240  session->setWhere(save_where);
2241 
2242  return res;
2243 }
2244 
2245 
2246 /*
2247  Search for at least one row satisfying select condition
2248 
2249  SYNOPSIS
2250  subselect_uniquesubquery_engine::scan_table()
2251 
2252  DESCRIPTION
2253  Scan the table using sequential access until we find at least one row
2254  satisfying select condition.
2255 
2256  The caller must set this->empty_result_set=false before calling this
2257  function. This function will set it to true if it finds a matching row.
2258 
2259  RETURN
2260  false - OK
2261  true - Error
2262 */
2263 
2264 int subselect_uniquesubquery_engine::scan_table()
2265 {
2266  int error;
2267  Table *table= tab->table;
2268 
2269  if (table->cursor->inited)
2270  table->cursor->endIndexScan();
2271 
2272  if ((error= table->cursor->startTableScan(1)))
2273  {
2274  table->print_error(error, MYF(0));
2275  return 1;
2276  }
2277 
2278  assert(table->getSession());
2279  table->cursor->extra_opt(HA_EXTRA_CACHE,
2280  table->getSession()->variables.read_buff_size);
2281  table->null_row= 0;
2282  for (;;)
2283  {
2284  error=table->cursor->rnd_next(table->record[0]);
2285  if (error && error != HA_ERR_END_OF_FILE)
2286  {
2287  error= table->report_error(error);
2288  break;
2289  }
2290  /* No more rows */
2291  if (table->status)
2292  break;
2293 
2294  if (!cond || cond->val_int())
2295  {
2296  empty_result_set= false;
2297  break;
2298  }
2299  }
2300 
2301  table->cursor->endTableScan();
2302  return(error != 0);
2303 }
2304 
2305 
2306 /*
2307  Copy ref key and check for null parts in it
2308 
2309  SYNOPSIS
2310  subselect_uniquesubquery_engine::copy_ref_key()
2311 
2312  DESCRIPTION
2313  Copy ref key and check for null parts in it.
2314  Depending on the nullability and conversion problems this function
2315  recognizes and processes the following states :
2316  1. Partial match on top level. This means IN has a value of false
2317  regardless of the data in the subquery table.
2318  Detected by finding a NULL in the left IN operand of a top level
2319  expression.
2320  We may actually skip reading the subquery, so return true to skip
2321  the table scan in subselect_uniquesubquery_engine::exec and make
2322  the value of the IN predicate a NULL (that is equal to false on
2323  top level).
2324  2. No exact match when IN is nested inside another predicate.
2325  Detected by finding a NULL in the left IN operand when IN is not
2326  a top level predicate.
2327  We cannot have an exact match. But we must proceed further with a
2328  table scan to find out if it's a partial match (and IN has a value
2329  of NULL) or no match (and IN has a value of false).
2330  So we return false to continue with the scan and see if there are
2331  any record that would constitute a partial match (as we cannot
2332  determine that from the index).
2333  3. Error converting the left IN operand to the column type of the
2334  right IN operand. This counts as no match (and IN has the value of
2335  false). We mark the subquery table cursor as having no more rows
2336  (to ensure that the processing that follows will not find a match)
2337  and return false, so IN is not treated as returning NULL.
2338 
2339 
2340  RETURN
2341  false - The value of the IN predicate is not known. Proceed to find the
2342  value of the IN predicate using the determined values of
2343  null_keypart and table->status.
2344  true - IN predicate has a value of NULL. Stop the processing right there
2345  and return NULL to the outer predicates.
2346 */
2347 
2348 bool subselect_uniquesubquery_engine::copy_ref_key()
2349 {
2350  for (StoredKey **copy= tab->ref.key_copy ; *copy ; copy++)
2351  {
2352  StoredKey::store_key_result store_res= (*copy)->copy();
2353  tab->ref.key_err= store_res;
2354 
2355  /*
2356  When there is a NULL part in the key we don't need to make index
2357  lookup for such key thus we don't need to copy whole key.
2358  If we later should do a sequential scan return OK. Fail otherwise.
2359 
2360  See also the comment for the subselect_uniquesubquery_engine::exec()
2361  function.
2362  */
2363  null_keypart= (*copy)->null_key;
2364  if (null_keypart)
2365  {
2366  bool top_level= ((Item_in_subselect *) item)->is_top_level_item();
2367  if (top_level)
2368  {
2369  /* Partial match on top level */
2370  return 1;
2371  }
2372  else
2373  {
2374  /* No exact match when IN is nested inside another predicate */
2375  break;
2376  }
2377  }
2378 
2379  /*
2380  Check if the error is equal to STORE_KEY_FATAL. This is not expressed
2381  using the StoredKey::store_key_result enum because ref.key_err is a
2382  boolean and we want to detect both true and STORE_KEY_FATAL from the
2383  space of the union of the values of [true, false] and
2384  StoredKey::store_key_result.
2385  TODO: fix the variable an return types.
2386  */
2387  if (store_res == StoredKey::STORE_KEY_FATAL)
2388  {
2389  /*
2390  Error converting the left IN operand to the column type of the right
2391  IN operand.
2392  */
2393  tab->table->status= STATUS_NOT_FOUND;
2394  break;
2395  }
2396  }
2397  return 0;
2398 }
2399 
2400 
2401 /*
2402  Execute subselect
2403 
2404  SYNOPSIS
2405  subselect_uniquesubquery_engine::exec()
2406 
2407  DESCRIPTION
2408  Find rows corresponding to the ref key using index access.
2409  If some part of the lookup key is NULL, then we're evaluating
2410  NULL IN (SELECT ... )
2411  This is a special case, we don't need to search for NULL in the table,
2412  instead, the result value is
2413  - NULL if select produces empty row set
2414  - false otherwise.
2415 
2416  In some cases (IN subselect is a top level item, i.e. abort_on_null==true)
2417  the caller doesn't distinguish between NULL and false result and we just
2418  return false.
2419  Otherwise we make a full table scan to see if there is at least one
2420  matching row.
2421 
2422  The result of this function (info about whether a row was found) is
2423  stored in this->empty_result_set.
2424  NOTE
2425 
2426  RETURN
2427  false - ok
2428  true - an error occured while scanning
2429 */
2430 
2431 int subselect_uniquesubquery_engine::exec()
2432 {
2433  int error;
2434  Table *table= tab->table;
2435  empty_result_set= true;
2436  table->status= 0;
2437 
2438  /* TODO: change to use of 'full_scan' here? */
2439  if (copy_ref_key())
2440  return 1;
2441  if (table->status)
2442  {
2443  /*
2444  We know that there will be no rows even if we scan.
2445  Can be set in copy_ref_key.
2446  */
2447  ((Item_in_subselect *) item)->value= 0;
2448  return 0;
2449  }
2450 
2451  if (null_keypart)
2452  return(scan_table());
2453 
2454  if (!table->cursor->inited)
2455  {
2456  error= table->cursor->startIndexScan(tab->ref.key, 0);
2457 
2458  if (error != 0)
2459  {
2460  error= table->report_error(error);
2461  return (error != 0);
2462  }
2463  }
2464 
2465  error= table->cursor->index_read_map(table->record[0],
2466  tab->ref.key_buff,
2467  make_prev_keypart_map(tab->ref.key_parts),
2468  HA_READ_KEY_EXACT);
2469  if (error &&
2470  error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
2471  error= table->report_error(error);
2472  else
2473  {
2474  error= 0;
2475  table->null_row= 0;
2476  if (!table->status && (!cond || cond->val_int()))
2477  {
2478  ((Item_in_subselect *) item)->value= 1;
2479  empty_result_set= false;
2480  }
2481  else
2482  ((Item_in_subselect *) item)->value= 0;
2483  }
2484 
2485  return(error != 0);
2486 }
2487 
2488 
2489 /*
2490  Index-lookup subselect 'engine' - run the subquery
2491 
2492  SYNOPSIS
2493  subselect_indexsubquery_engine:exec()
2494  full_scan
2495 
2496  DESCRIPTION
2497  The engine is used to resolve subqueries in form
2498 
2499  oe IN (SELECT key FROM tbl WHERE subq_where)
2500 
2501  The value of the predicate is calculated as follows:
2502  1. If oe IS NULL, this is a special case, do a full table scan on
2503  table tbl and search for row that satisfies subq_where. If such
2504  row is found, return NULL, otherwise return false.
2505  2. Make an index lookup via key=oe, search for a row that satisfies
2506  subq_where. If found, return true.
2507  3. If check_null==true, make another lookup via key=NULL, search for a
2508  row that satisfies subq_where. If found, return NULL, otherwise
2509  return false.
2510 
2511  TODO
2512  The step #1 can be optimized further when the index has several key
2513  parts. Consider a subquery:
2514 
2515  (oe1, oe2) IN (SELECT keypart1, keypart2 FROM tbl WHERE subq_where)
2516 
2517  and suppose we need to evaluate it for {oe1, oe2}=={const1, NULL}.
2518  Current code will do a full table scan and obtain correct result. There
2519  is a better option: instead of evaluating
2520 
2521  SELECT keypart1, keypart2 FROM tbl WHERE subq_where (1)
2522 
2523  and checking if it has produced any matching rows, evaluate
2524 
2525  SELECT keypart2 FROM tbl WHERE subq_where AND keypart1=const1 (2)
2526 
2527  If this query produces a row, the result is NULL (as we're evaluating
2528  "(const1, NULL) IN { (const1, X), ... }", which has a value of UNKNOWN,
2529  i.e. NULL). If the query produces no rows, the result is false.
2530 
2531  We currently evaluate (1) by doing a full table scan. (2) can be
2532  evaluated by doing a "ref" scan on "keypart1=const1", which can be much
2533  cheaper. We can use index statistics to quickly check whether "ref" scan
2534  will be cheaper than full table scan.
2535 
2536  RETURN
2537  0
2538  1
2539 */
2540 
2541 int subselect_indexsubquery_engine::exec()
2542 {
2543  int error;
2544  bool null_finding= 0;
2545  Table *table= tab->table;
2546 
2547  ((Item_in_subselect *) item)->value= 0;
2548  empty_result_set= true;
2549  null_keypart= 0;
2550  table->status= 0;
2551 
2552  if (check_null)
2553  {
2554  /* We need to check for NULL if there wasn't a matching value */
2555  *tab->ref.null_ref_key= 0; // Search first for not null
2556  ((Item_in_subselect *) item)->was_null= 0;
2557  }
2558 
2559  /* Copy the ref key and check for nulls... */
2560  if (copy_ref_key())
2561  return 1;
2562 
2563  if (table->status)
2564  {
2565  /*
2566  We know that there will be no rows even if we scan.
2567  Can be set in copy_ref_key.
2568  */
2569  ((Item_in_subselect *) item)->value= 0;
2570  return 0;
2571  }
2572 
2573  if (null_keypart)
2574  return(scan_table());
2575 
2576  if (!table->cursor->inited)
2577  {
2578  error= table->cursor->startIndexScan(tab->ref.key, 1);
2579 
2580  if (error != 0)
2581  {
2582  error= table->report_error(error);
2583  return(error != 0);
2584  }
2585  }
2586  error= table->cursor->index_read_map(table->record[0],
2587  tab->ref.key_buff,
2588  make_prev_keypart_map(tab->ref.key_parts),
2589  HA_READ_KEY_EXACT);
2590  if (error &&
2591  error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
2592  error= table->report_error(error);
2593  else
2594  {
2595  for (;;)
2596  {
2597  error= 0;
2598  table->null_row= 0;
2599  if (!table->status)
2600  {
2601  if ((!cond || cond->val_int()) && (!having || having->val_int()))
2602  {
2603  empty_result_set= false;
2604  if (null_finding)
2605  ((Item_in_subselect *) item)->was_null= 1;
2606  else
2607  ((Item_in_subselect *) item)->value= 1;
2608  break;
2609  }
2610  error= table->cursor->index_next_same(table->record[0],
2611  tab->ref.key_buff,
2612  tab->ref.key_length);
2613  if (error && error != HA_ERR_END_OF_FILE)
2614  {
2615  error= table->report_error(error);
2616  break;
2617  }
2618  }
2619  else
2620  {
2621  if (!check_null || null_finding)
2622  break; /* We don't need to check nulls */
2623  *tab->ref.null_ref_key= 1;
2624  null_finding= 1;
2625  /* Check if there exists a row with a null value in the index */
2626  if ((error= (safe_index_read(tab) == 1)))
2627  break;
2628  }
2629  }
2630  }
2631  return(error != 0);
2632 }
2633 
2634 
2635 uint32_t subselect_single_select_engine::cols()
2636 {
2637  return select_lex->item_list.size();
2638 }
2639 
2640 
2641 uint32_t subselect_union_engine::cols()
2642 {
2643  return unit->types.size();
2644 }
2645 
2646 
2647 bool subselect_single_select_engine::uncacheable()
2648 {
2649  return select_lex->uncacheable.any();
2650 }
2651 
2652 
2653 bool subselect_single_select_engine::uncacheable(uint32_t bit_pos)
2654 {
2655  return select_lex->uncacheable.test(bit_pos);
2656 }
2657 
2658 
2659 bool subselect_union_engine::uncacheable()
2660 {
2661  return unit->uncacheable.any();
2662 }
2663 
2664 
2665 bool subselect_union_engine::uncacheable(uint32_t bit_pos)
2666 {
2667  return unit->uncacheable.test(bit_pos);
2668 }
2669 
2670 
2671 void subselect_single_select_engine::exclude()
2672 {
2673  select_lex->master_unit()->exclude_level();
2674 }
2675 
2676 void subselect_union_engine::exclude()
2677 {
2678  unit->exclude_level();
2679 }
2680 
2681 
2682 void subselect_uniquesubquery_engine::exclude()
2683 {
2684  //this never should be called
2685  assert(0);
2686 }
2687 
2688 
2689 table_map subselect_engine::calc_const_tables(TableList *table)
2690 {
2691  table_map map= 0;
2692  for (; table; table= table->next_leaf)
2693  {
2694  Table *tbl= table->table;
2695  if (tbl && tbl->const_table)
2696  map|= tbl->map;
2697  }
2698  return map;
2699 }
2700 
2701 
2702 table_map subselect_single_select_engine::upper_select_const_tables()
2703 {
2704  return calc_const_tables((TableList *) select_lex->outer_select()->
2705  leaf_tables);
2706 }
2707 
2708 
2709 table_map subselect_union_engine::upper_select_const_tables()
2710 {
2711  return calc_const_tables((TableList *) unit->outer_select()->leaf_tables);
2712 }
2713 
2714 
2715 void subselect_single_select_engine::print(String *str)
2716 {
2717  select_lex->print(session, str);
2718 }
2719 
2720 
2721 void subselect_union_engine::print(String *str)
2722 {
2723  unit->print(str);
2724 }
2725 
2726 
2727 void subselect_uniquesubquery_engine::print(String *str)
2728 {
2729  str->append(STRING_WITH_LEN("<primary_index_lookup>("));
2730  tab->ref.items[0]->print(str);
2731  str->append(STRING_WITH_LEN(" in "));
2732  if (tab->table->getShare()->isTemporaryCategory())
2733  {
2734  /*
2735  Temporary tables' names change across runs, so they can't be used for
2736  EXPLAIN EXTENDED.
2737  */
2738  str->append(STRING_WITH_LEN("<temporary table>"));
2739  }
2740  else
2741  str->append(tab->table->getShare()->getTableNameRef());
2742  str->append(STRING_WITH_LEN(" on "));
2743  str->append(tab->table->key_info[tab->ref.key].name, strlen(tab->table->key_info[tab->ref.key].name));
2744  if (cond)
2745  {
2746  str->append(STRING_WITH_LEN(" where "));
2747  cond->print(str);
2748  }
2749  str->append(')');
2750 }
2751 
2752 /*
2753 TODO:
2754 The above ::print method should be changed as below. Do it after
2755 all other tests pass.
2756 
2757 void subselect_uniquesubquery_engine::print(String *str)
2758 {
2759  KEY *key_info= tab->table->key_info + tab->ref.key;
2760  str->append(STRING_WITH_LEN("<primary_index_lookup>("));
2761  for (uint32_t i= 0; i < key_info->key_parts; i++)
2762  tab->ref.items[i]->print(str);
2763  str->append(STRING_WITH_LEN(" in "));
2764  str->append(tab->table->getShare()->getTableName(), tab->table->getShare()->getTableNameSize());
2765  str->append(STRING_WITH_LEN(" on "));
2766  str->append(key_info->name);
2767  if (cond)
2768  {
2769  str->append(STRING_WITH_LEN(" where "));
2770  cond->print(str);
2771  }
2772  str->append(')');
2773 }
2774 */
2775 
2776 void subselect_indexsubquery_engine::print(String *str)
2777 {
2778  str->append(STRING_WITH_LEN("<index_lookup>("));
2779  tab->ref.items[0]->print(str);
2780  str->append(STRING_WITH_LEN(" in "));
2781  str->append(tab->table->getShare()->getTableNameRef());
2782  KeyInfo *key_info= tab->table->key_info+ tab->ref.key;
2783  str->append(STRING_WITH_LEN(" on "));
2784  str->append(key_info->name, strlen(key_info->name));
2785  if (check_null)
2786  {
2787  str->append(STRING_WITH_LEN(" checking NULL"));
2788  }
2789 
2790  if (cond)
2791  {
2792  str->append(STRING_WITH_LEN(" where "));
2793  cond->print(str);
2794  }
2795 
2796  if (having)
2797  {
2798  str->append(STRING_WITH_LEN(" having "));
2799  having->print(str);
2800  }
2801 
2802  str->append(')');
2803 }
2804 
2819 {
2820  item= si;
2821  result= res;
2822  return select_lex->join->change_result(result);
2823 }
2824 
2825 
2840 {
2841  item= si;
2842  int rc= unit->change_result(res, result);
2843  result= res;
2844  return rc;
2845 }
2846 
2847 
2862 {
2863  assert(0);
2864  return true;
2865 }
2866 
2867 
2877 {
2878  return(select_lex->table_list.elements == 0);
2879 }
2880 
2881 
2882 /*
2883  Check statically whether the subquery can return NULL
2884 
2885  SINOPSYS
2886  subselect_single_select_engine::may_be_null()
2887 
2888  RETURN
2889  false can guarantee that the subquery never return NULL
2890  true otherwise
2891 */
2892 bool subselect_single_select_engine::may_be_null()
2893 {
2894  return ((no_tables() && !join->conds && !join->having) ? maybe_null : 1);
2895 }
2896 
2897 
2907 {
2908  for (Select_Lex *sl= unit->first_select(); sl; sl= sl->next_select())
2909  {
2910  if (sl->table_list.elements)
2911  return false;
2912  }
2913  return true;
2914 }
2915 
2916 
2927 {
2928  /* returning value is correct, but this method should never be called */
2929  return 0;
2930 }
2931 
2932 
2933 /******************************************************************************
2934  WL#1110 - Implementation of class subselect_hash_sj_engine
2935 ******************************************************************************/
2936 
2937 
2959 {
2960  /* The result sink where we will materialize the subquery result. */
2961  select_union *tmp_result_sink;
2962  /* The table into which the subquery is materialized. */
2963  Table *tmp_table;
2964  KeyInfo *tmp_key; /* The only index on the temporary table. */
2965  uint32_t tmp_key_parts; /* Number of keyparts in tmp_key. */
2966  Item_in_subselect *item_in= (Item_in_subselect *) item;
2967 
2968  /* 1. Create/initialize materialization related objects. */
2969 
2970  /*
2971  Create and initialize a select result interceptor that stores the
2972  result stream in a temporary table. The temporary table itself is
2973  managed (created/filled/etc) internally by the interceptor.
2974  */
2975  tmp_result_sink= new select_union;
2976 
2977  if (tmp_result_sink->create_result_table(session, tmp_columns, true,
2978  session->options | TMP_TABLE_ALL_COLUMNS, "materialized subselect"))
2979  return true;
2980 
2981  tmp_table= tmp_result_sink->table;
2982  tmp_key= tmp_table->key_info;
2983  tmp_key_parts= tmp_key->key_parts;
2984 
2985  /*
2986  If the subquery has blobs, or the total key lenght is bigger than some
2987  length, then the created index cannot be used for lookups and we
2988  can't use hash semi join. If this is the case, delete the temporary
2989  table since it will not be used, and tell the caller we failed to
2990  initialize the engine.
2991  */
2992  if (tmp_table->getShare()->sizeKeys() == 0)
2993  {
2994  assert(tmp_table->getShare()->db_type() == myisam_engine);
2995  assert(
2996  tmp_table->getShare()->uniques ||
2997  tmp_table->key_info->key_length >= tmp_table->cursor->getEngine()->max_key_length() ||
2998  tmp_table->key_info->key_parts > tmp_table->cursor->getEngine()->max_key_parts());
2999  tmp_table= NULL;
3000  delete result;
3001  result= NULL;
3002  return true;
3003  }
3004  result= tmp_result_sink;
3005 
3006  /*
3007  Make sure there is only one index on the temp table, and it doesn't have
3008  the extra key part created when s->uniques > 0.
3009  */
3010  assert(tmp_table->getShare()->sizeKeys() == 1 && tmp_columns->size() == tmp_key_parts);
3011 
3012 
3013  /* 2. Create/initialize execution related objects. */
3014 
3015  /*
3016  Create and initialize the JoinTable that represents an index lookup
3017  plan operator into the materialized subquery result. Notice that:
3018  - this JoinTable has no corresponding JOIN (and doesn't need one), and
3019  - here we initialize only those members that are used by
3020  subselect_uniquesubquery_engine, so these objects are incomplete.
3021  */
3022  tab= new (session->mem) JoinTable;
3023  tab->table= tmp_table;
3024  tab->ref.key= 0; /* The only temp table index. */
3025  tab->ref.key_length= tmp_key->key_length;
3026  tab->ref.key_buff= (unsigned char*) session->mem.calloc(ALIGN_SIZE(tmp_key->key_length) * 2);
3027  tab->ref.key_copy= new (session->mem) StoredKey*[tmp_key_parts + 1];
3028  tab->ref.items= new (session->mem) Item*[tmp_key_parts];
3029 
3030  KeyPartInfo *cur_key_part= tmp_key->key_part;
3031  StoredKey **ref_key= tab->ref.key_copy;
3032  unsigned char *cur_ref_buff= tab->ref.key_buff;
3033 
3034  for (uint32_t i= 0; i < tmp_key_parts; i++, cur_key_part++, ref_key++)
3035  {
3036  tab->ref.items[i]= item_in->left_expr->element_index(i);
3037  int null_count= test(cur_key_part->field->real_maybe_null());
3038  *ref_key= new store_key_item(session, cur_key_part->field,
3039  /* TODO:
3040  the NULL byte is taken into account in
3041  cur_key_part->store_length, so instead of
3042  cur_ref_buff + test(maybe_null), we could
3043  use that information instead.
3044  */
3045  cur_ref_buff + null_count,
3046  null_count ? tab->ref.key_buff : 0,
3047  cur_key_part->length, tab->ref.items[i]);
3048  cur_ref_buff+= cur_key_part->store_length;
3049  }
3050  *ref_key= NULL; /* End marker. */
3051  tab->ref.key_err= 1;
3052  tab->ref.key_parts= tmp_key_parts;
3053 
3054  return false;
3055 }
3056 
3057 
3067 {
3068  /*
3069  Create and optimize the JOIN that will be used to materialize
3070  the subquery if not yet created.
3071  */
3072  materialize_engine->prepare();
3073  /* Let our engine reuse this query plan for materialization. */
3074  materialize_join= materialize_engine->join;
3075  materialize_join->change_result(result);
3076 }
3077 
3078 
3079 subselect_hash_sj_engine::~subselect_hash_sj_engine()
3080 {
3081  delete result;
3082  if (tab)
3083  {
3084  tab->table= NULL;
3085  }
3086 }
3087 
3088 
3097 {
3098  is_materialized= false;
3099  result->cleanup(); /* Resets the temp table as well. */
3100  materialize_engine->cleanup();
3101  subselect_uniquesubquery_engine::cleanup();
3102 }
3103 
3104 
3117 {
3118  Item_in_subselect *item_in= (Item_in_subselect *) item;
3119 
3120  /*
3121  Optimize and materialize the subquery during the first execution of
3122  the subquery predicate.
3123  */
3124  if (!is_materialized)
3125  {
3126  int res= 0;
3127  Select_Lex *save_select= session->lex().current_select;
3128  session->lex().current_select= materialize_engine->select_lex;
3129  if ((res= materialize_join->optimize()))
3130  goto err;
3131 
3132  materialize_engine->save_join_if_explain();
3133 
3134  materialize_join->exec();
3135  if ((res= test(materialize_join->error || session->is_fatal_error)))
3136  goto err;
3137 
3138  /*
3139  TODO:
3140  - Unlock all subquery tables as we don't need them. To implement this
3141  we need to add new functionality to Join::join_free that can unlock
3142  all tables in a subquery (and all its subqueries).
3143  - The temp table used for grouping in the subquery can be freed
3144  immediately after materialization (yet it's done together with
3145  unlocking).
3146  */
3147  is_materialized= true;
3148  /*
3149  If the subquery returned no rows, the temporary table is empty, so we know
3150  directly that the result of IN is false. We first update the table
3151  statistics, then we test if the temporary table for the query result is
3152  empty.
3153  */
3154  tab->table->cursor->info(HA_STATUS_VARIABLE);
3155  if (!tab->table->cursor->stats.records)
3156  {
3157  empty_result_set= true;
3158  item_in->value= false;
3159  /* TODO: check we need this: item_in->null_value= false; */
3160  return false;
3161  }
3162  /* Set tmp_param only if its usable, i.e. tmp_param->copy_field != NULL. */
3163  tmp_param= &(item_in->unit->outer_select()->join->tmp_table_param);
3164  if (tmp_param && !tmp_param->copy_field)
3165  tmp_param= NULL;
3166 
3167 err:
3168  session->lex().current_select= save_select;
3169  if (res)
3170  return res;
3171  }
3172 
3173  /*
3174  Lookup the left IN operand in the hash index of the materialized subquery.
3175  */
3176  return(subselect_uniquesubquery_engine::exec());
3177 }
3178 
3179 
3185 {
3186  str->append(STRING_WITH_LEN(" <materialize> ("));
3187  materialize_engine->print(str);
3188  str->append(STRING_WITH_LEN(" ), "));
3189  if (tab)
3190  subselect_uniquesubquery_engine::print(str);
3191  else
3192  str->append(STRING_WITH_LEN(
3193  "<the access method for lookups is not yet created>"
3194  ));
3195 }
3196 
3197 } /* namespace drizzled */
Cached_item * new_Cached_item(Session *session, Item *item)
Definition: cached_item.cc:44
const char * name
Definition: item.h:110
virtual void print(String *str)
Definition: subselect.cc:1692
bool change_result(Item_subselect *si, select_result_interceptor *result)
Definition: subselect.cc:2817
Select_Lex * select_lex
Definition: join.h:146
virtual int64_t val_int()=0
bool change_result(Item_subselect *si, select_result_interceptor *result)
Definition: subselect.cc:2860
const char * in_additional_cond
Definition: drizzled.cc:312
bool fixed
Definition: item.h:120
TODO: Rename this file - func.h is stupid.
String * val_str(String *)
Definition: subselect.cc:809
bool init_permanent(List< Item > *tmp_columns)
Definition: subselect.cc:2958
type::Decimal * val_decimal(type::Decimal *)
Definition: subselect.cc:672
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
virtual void print(String *str)
Definition: subselect.cc:1859
bool change_result(select_result *result)
Definition: join.cc:2551
table_map used_tables() const
Definition: subselect.cc:378
bool null_value
Definition: item.h:122
virtual void print(String *str)
Definition: subselect.cc:3184
#define STACK_MIN_SIZE
Abort if less stack during eval.
Definition: definitions.h:120
bool check_stack_overrun(Session *session, long margin, void *)
virtual bool val_bool()
Definition: item.cc:95
virtual void top_level_item(void)
Definition: item.cc:523
type::Decimal * val_decimal(type::Decimal *)
Definition: subselect.cc:933
bool maybe_null
Definition: item.h:121
virtual double val_real()=0
void exec()
Definition: join.cc:1372
trans_res select_transformer(Join *join)
Definition: subselect.cc:510
int optimize()
Definition: join.cc:609
virtual void print(String *str)
Definition: subselect.cc:484
KeyInfo * key_info
Definition: table.h:141
virtual String * val_str(String *str)=0
String * val_str(String *)
Definition: subselect.cc:888
bool is_fatal_error
Definition: session.h:540
Select_Lex * get_select_lex()
Definition: subselect.cc:133
type::Decimal * val_decimal(type::Decimal *)
Definition: subselect.cc:822
Select_Lex * invalidate_and_restore_select_lex()
Definition: subselect.cc:424
trans_res select_in_like_transformer(Join *join, const Comp_creator *func)
Definition: subselect.cc:1613
uint32_t tables
Definition: join.h:66
uint64_t options
Definition: session.h:400
Cursor * cursor
Definition: table.h:68
virtual void print(String *str)
Definition: subselect.cc:715
const char * in_left_expr_name
Definition: drizzled.cc:310
virtual void print(String *str)
Definition: item.cc:362
List< Item > all_fields
Definition: join.h:184
trans_res row_value_in_to_exists_transformer(Join *join)
Definition: subselect.cc:1395
virtual type::Decimal * val_decimal(type::Decimal *decimal_buffer)=0
static const uint32_t UNCACHEABLE_EXPLAIN
forcing to save JOIN for explain
Definition: definitions.h:190
virtual void print(String *str)
Definition: subselect.cc:407
bool const_item() const
Definition: subselect.cc:384
bool change_result(Item_subselect *si, select_result_interceptor *result)
Definition: subselect.cc:2838
trans_res single_value_in_to_exists_transformer(Join *join, const Comp_creator *func)
Definition: subselect.cc:1168
bool is_error() const
Definition: session.cc:1871