~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/item_subselect.cc

Removed DBUG symbols and fixed TRUE/FALSE

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
    - add function from mysql_select that use JOIN* as parameter to JOIN
24
24
    methods (sql_select.h/sql_select.cc)
25
25
*/
26
 
#include <drizzled/server_includes.h>
27
 
#include <drizzled/sql_select.h>
28
 
#include <drizzled/drizzled_error_messages.h>
 
26
 
 
27
#ifdef USE_PRAGMA_IMPLEMENTATION
 
28
#pragma implementation                          // gcc: Class implementation
 
29
#endif
 
30
 
 
31
#include "mysql_priv.h"
 
32
#include "sql_select.h"
29
33
 
30
34
inline Item * and_items(Item* cond, Item *item)
31
35
{
56
60
    which depends on alterations to the parse tree implemented here.
57
61
  */
58
62
 
 
63
  DBUG_ENTER("Item_subselect::init");
 
64
  DBUG_PRINT("enter", ("select_lex: 0x%lx", (long) select_lex));
59
65
  unit= select_lex->master_unit();
60
66
 
61
67
  if (unit->item)
90
96
    if (upper->parsing_place == IN_HAVING)
91
97
      upper->subquery_in_having= 1;
92
98
  }
93
 
  return;
 
99
  DBUG_VOID_RETURN;
94
100
}
95
101
 
96
102
st_select_lex *
101
107
 
102
108
void Item_subselect::cleanup()
103
109
{
 
110
  DBUG_ENTER("Item_subselect::cleanup");
104
111
  Item_result_field::cleanup();
105
112
  if (old_engine)
106
113
  {
113
120
    engine->cleanup();
114
121
  reset();
115
122
  value_assigned= 0;
116
 
  return;
 
123
  DBUG_VOID_RETURN;
117
124
}
118
125
 
119
126
void Item_singlerow_subselect::cleanup()
120
127
{
 
128
  DBUG_ENTER("Item_singlerow_subselect::cleanup");
121
129
  value= 0; row= 0;
122
130
  Item_subselect::cleanup();
123
 
  return;
 
131
  DBUG_VOID_RETURN;
124
132
}
125
133
 
126
134
 
127
135
void Item_in_subselect::cleanup()
128
136
{
 
137
  DBUG_ENTER("Item_in_subselect::cleanup");
129
138
  if (left_expr_cache)
130
139
  {
131
140
    left_expr_cache->delete_elements();
134
143
  }
135
144
  first_execution= true;
136
145
  Item_subselect::cleanup();
137
 
  return;
 
146
  DBUG_VOID_RETURN;
138
147
}
139
148
 
140
149
Item_subselect::~Item_subselect()
143
152
}
144
153
 
145
154
Item_subselect::trans_res
146
 
Item_subselect::select_transformer(JOIN *join __attribute__((unused)))
 
155
Item_subselect::select_transformer(JOIN *join __attribute__((__unused__)))
147
156
{
148
 
  return(RES_OK);
 
157
  DBUG_ENTER("Item_subselect::select_transformer");
 
158
  DBUG_RETURN(RES_OK);
149
159
}
150
160
 
151
161
 
152
162
bool Item_subselect::fix_fields(THD *thd_param, Item **ref)
153
163
{
154
164
  char const *save_where= thd_param->where;
155
 
  uint8_t uncacheable;
 
165
  uint8 uncacheable;
156
166
  bool res;
157
167
 
158
 
  assert(fixed == 0);
 
168
  DBUG_ASSERT(fixed == 0);
159
169
  engine->set_thd((thd= thd_param));
160
170
 
161
 
  if (check_stack_overrun(thd, STACK_MIN_SIZE, (unsigned char*)&res))
 
171
  if (check_stack_overrun(thd, STACK_MIN_SIZE, (uchar*)&res))
162
172
    return true;
163
173
 
164
174
  res= engine->prepare();
221
231
 
222
232
 
223
233
bool Item_subselect::walk(Item_processor processor, bool walk_subquery,
224
 
                          unsigned char *argument)
 
234
                          uchar *argument)
225
235
{
226
236
 
227
237
  if (walk_subquery)
230
240
    {
231
241
      List_iterator<Item> li(lex->item_list);
232
242
      Item *item;
233
 
      order_st *order;
 
243
      ORDER *order;
234
244
 
235
245
      if (lex->where && (lex->where)->walk(processor, walk_subquery, argument))
236
246
        return 1;
243
253
        if (item->walk(processor, walk_subquery, argument))
244
254
          return 1;
245
255
      }
246
 
      for (order= (order_st*) lex->order_list.first ; order; order= order->next)
 
256
      for (order= (ORDER*) lex->order_list.first ; order; order= order->next)
247
257
      {
248
258
        if ((*order->item)->walk(processor, walk_subquery, argument))
249
259
          return 1;
250
260
      }
251
 
      for (order= (order_st*) lex->group_list.first ; order; order= order->next)
 
261
      for (order= (ORDER*) lex->group_list.first ; order; order= order->next)
252
262
      {
253
263
        if ((*order->item)->walk(processor, walk_subquery, argument))
254
264
          return 1;
284
294
 
285
295
bool Item_in_subselect::exec()
286
296
{
287
 
  assert(exec_method != MATERIALIZATION ||
 
297
  DBUG_ENTER("Item_in_subselect::exec");
 
298
  DBUG_ASSERT(exec_method != MATERIALIZATION ||
288
299
              (exec_method == MATERIALIZATION &&
289
300
               engine->engine_type() == subselect_engine::HASH_SJ_ENGINE));
290
301
  /*
307
318
  {
308
319
    /* Always compute IN for the first row as the cache is not valid for it. */
309
320
    if (!first_execution)
310
 
      return(false);
 
321
      DBUG_RETURN(false);
311
322
    first_execution= false;
312
323
  }
313
324
 
316
327
    we don't call it, the next call to item::val_int() will return whatever
317
328
    result was computed by its previous call.
318
329
  */
319
 
  return(Item_subselect::exec());
 
330
  DBUG_RETURN(Item_subselect::exec());
320
331
}
321
332
 
322
333
 
372
383
Item_singlerow_subselect::Item_singlerow_subselect(st_select_lex *select_lex)
373
384
  :Item_subselect(), value(0)
374
385
{
 
386
  DBUG_ENTER("Item_singlerow_subselect::Item_singlerow_subselect");
375
387
  init(select_lex, new select_singlerow_subselect(this));
376
388
  maybe_null= 1;
377
389
  max_columns= UINT_MAX;
378
 
  return;
 
390
  DBUG_VOID_RETURN;
379
391
}
380
392
 
381
393
st_select_lex *
382
394
Item_singlerow_subselect::invalidate_and_restore_select_lex()
383
395
{
 
396
  DBUG_ENTER("Item_singlerow_subselect::invalidate_and_restore_select_lex");
384
397
  st_select_lex *result= get_select_lex();
385
398
 
386
 
  assert(result);
 
399
  DBUG_ASSERT(result);
387
400
 
388
401
  /*
389
402
    This code restore the parse tree in it's state before the execution of
394
407
  */
395
408
  unit->item= NULL;
396
409
 
397
 
  return(result);
 
410
  DBUG_RETURN(result);
398
411
}
399
412
 
400
413
Item_maxmin_subselect::Item_maxmin_subselect(THD *thd_param,
403
416
                                             bool max_arg)
404
417
  :Item_singlerow_subselect(), was_values(true)
405
418
{
 
419
  DBUG_ENTER("Item_maxmin_subselect::Item_maxmin_subselect");
406
420
  max= max_arg;
407
421
  init(select_lex, new select_max_min_finder_subselect(this, max_arg));
408
422
  max_columns= 1;
422
436
  */
423
437
  thd= thd_param;
424
438
 
425
 
  return;
 
439
  DBUG_VOID_RETURN;
426
440
}
427
441
 
428
442
void Item_maxmin_subselect::cleanup()
429
443
{
 
444
  DBUG_ENTER("Item_maxmin_subselect::cleanup");
430
445
  Item_singlerow_subselect::cleanup();
431
446
 
432
447
  /*
438
453
    value will be found.
439
454
  */
440
455
  was_values= true;
441
 
  return;
 
456
  DBUG_VOID_RETURN;
442
457
}
443
458
 
444
459
 
470
485
Item_subselect::trans_res
471
486
Item_singlerow_subselect::select_transformer(JOIN *join)
472
487
{
 
488
  DBUG_ENTER("Item_singlerow_subselect::select_transformer");
473
489
  if (changed)
474
 
    return(RES_OK);
 
490
    DBUG_RETURN(RES_OK);
475
491
 
476
492
  SELECT_LEX *select_lex= join->select_lex;
477
493
 
495
511
    have_to_be_excluded= 1;
496
512
    if (thd->lex->describe)
497
513
    {
498
 
      char warn_buff[DRIZZLE_ERRMSG_SIZE];
 
514
      char warn_buff[MYSQL_ERRMSG_SIZE];
499
515
      sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number);
500
 
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
516
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
501
517
                   ER_SELECT_REDUCED, warn_buff);
502
518
    }
503
519
    substitution= select_lex->item_list.head();
506
522
      'upper' select is not really dependent => we remove this dependence
507
523
    */
508
524
    substitution->walk(&Item::remove_dependence_processor, 0,
509
 
                       (unsigned char *) select_lex->outer_select());
510
 
    return(RES_REDUCE);
 
525
                       (uchar *) select_lex->outer_select());
 
526
    DBUG_RETURN(RES_REDUCE);
511
527
  }
512
 
  return(RES_OK);
 
528
  DBUG_RETURN(RES_OK);
513
529
}
514
530
 
515
531
 
516
 
void Item_singlerow_subselect::store(uint32_t i, Item *item)
 
532
void Item_singlerow_subselect::store(uint i, Item *item)
517
533
{
518
534
  row[i]->store(item);
519
535
}
555
571
    maybe_null= engine->may_be_null();
556
572
}
557
573
 
558
 
uint32_t Item_singlerow_subselect::cols()
 
574
uint Item_singlerow_subselect::cols()
559
575
{
560
576
  return engine->cols();
561
577
}
562
578
 
563
 
bool Item_singlerow_subselect::check_cols(uint32_t c)
 
579
bool Item_singlerow_subselect::check_cols(uint c)
564
580
{
565
581
  if (c != engine->cols())
566
582
  {
572
588
 
573
589
bool Item_singlerow_subselect::null_inside()
574
590
{
575
 
  for (uint32_t i= 0; i < max_columns ; i++)
 
591
  for (uint i= 0; i < max_columns ; i++)
576
592
  {
577
593
    if (row[i]->null_value)
578
594
      return 1;
587
603
 
588
604
double Item_singlerow_subselect::val_real()
589
605
{
590
 
  assert(fixed == 1);
 
606
  DBUG_ASSERT(fixed == 1);
591
607
  if (!exec() && !value->null_value)
592
608
  {
593
609
    null_value= 0;
600
616
  }
601
617
}
602
618
 
603
 
int64_t Item_singlerow_subselect::val_int()
 
619
longlong Item_singlerow_subselect::val_int()
604
620
{
605
 
  assert(fixed == 1);
 
621
  DBUG_ASSERT(fixed == 1);
606
622
  if (!exec() && !value->null_value)
607
623
  {
608
624
    null_value= 0;
663
679
Item_exists_subselect::Item_exists_subselect(st_select_lex *select_lex):
664
680
  Item_subselect()
665
681
{
 
682
  DBUG_ENTER("Item_exists_subselect::Item_exists_subselect");
666
683
  bool val_bool();
667
684
  init(select_lex, new select_exists_subselect(this));
668
685
  max_columns= UINT_MAX;
669
686
  null_value= 0; //can't be NULL
670
687
  maybe_null= 0; //can't be NULL
671
688
  value= 0;
672
 
  return;
 
689
  DBUG_VOID_RETURN;
673
690
}
674
691
 
675
692
 
701
718
  optimizer(0), pushed_cond_guards(NULL), exec_method(NOT_TRANSFORMED),
702
719
  upper_item(0)
703
720
{
 
721
  DBUG_ENTER("Item_in_subselect::Item_in_subselect");
704
722
  left_expr= left_exp;
705
723
  init(select_lex, new select_exists_subselect(this));
706
724
  max_columns= UINT_MAX;
709
727
  reset();
710
728
  //if test_limit will fail then error will be reported to client
711
729
  test_limit(select_lex->master_unit());
712
 
  return;
 
730
  DBUG_VOID_RETURN;
713
731
}
714
732
 
715
733
Item_allany_subselect::Item_allany_subselect(Item * left_exp,
718
736
                                             bool all_arg)
719
737
  :Item_in_subselect(), func_creator(fc), all(all_arg)
720
738
{
 
739
  DBUG_ENTER("Item_allany_subselect::Item_allany_subselect");
721
740
  left_expr= left_exp;
722
741
  func= func_creator(all_arg);
723
742
  init(select_lex, new select_exists_subselect(this));
726
745
  reset();
727
746
  //if test_limit will fail then error will be reported to client
728
747
  test_limit(select_lex->master_unit());
729
 
  return;
 
748
  DBUG_VOID_RETURN;
730
749
}
731
750
 
732
751
 
736
755
   max_length= 1;
737
756
   max_columns= engine->cols();
738
757
  /* We need only 1 row to determine existence */
739
 
  unit->global_parameters->select_limit= new Item_int((int32_t) 1);
 
758
  unit->global_parameters->select_limit= new Item_int((int32) 1);
740
759
}
741
760
 
742
761
double Item_exists_subselect::val_real()
743
762
{
744
 
  assert(fixed == 1);
 
763
  DBUG_ASSERT(fixed == 1);
745
764
  if (exec())
746
765
  {
747
766
    reset();
750
769
  return (double) value;
751
770
}
752
771
 
753
 
int64_t Item_exists_subselect::val_int()
 
772
longlong Item_exists_subselect::val_int()
754
773
{
755
 
  assert(fixed == 1);
 
774
  DBUG_ASSERT(fixed == 1);
756
775
  if (exec())
757
776
  {
758
777
    reset();
763
782
 
764
783
String *Item_exists_subselect::val_str(String *str)
765
784
{
766
 
  assert(fixed == 1);
 
785
  DBUG_ASSERT(fixed == 1);
767
786
  if (exec())
768
787
  {
769
788
    reset();
770
789
    return 0;
771
790
  }
772
 
  str->set((uint64_t)value,&my_charset_bin);
 
791
  str->set((ulonglong)value,&my_charset_bin);
773
792
  return str;
774
793
}
775
794
 
776
795
 
777
796
my_decimal *Item_exists_subselect::val_decimal(my_decimal *decimal_value)
778
797
{
779
 
  assert(fixed == 1);
 
798
  DBUG_ASSERT(fixed == 1);
780
799
  if (exec())
781
800
  {
782
801
    reset();
789
808
 
790
809
bool Item_exists_subselect::val_bool()
791
810
{
792
 
  assert(fixed == 1);
 
811
  DBUG_ASSERT(fixed == 1);
793
812
  if (exec())
794
813
  {
795
814
    reset();
805
824
    As far as Item_in_subselect called only from Item_in_optimizer this
806
825
    method should not be used
807
826
  */
808
 
  assert(0);
809
 
  assert(fixed == 1);
 
827
  DBUG_ASSERT(0);
 
828
  DBUG_ASSERT(fixed == 1);
810
829
  null_value= 0;
811
830
  if (exec())
812
831
  {
820
839
}
821
840
 
822
841
 
823
 
int64_t Item_in_subselect::val_int()
 
842
longlong Item_in_subselect::val_int()
824
843
{
825
844
  /*
826
845
    As far as Item_in_subselect called only from Item_in_optimizer this
827
846
    method should not be used
828
847
  */
829
 
  assert(fixed == 1);
 
848
  DBUG_ASSERT(0);
 
849
  DBUG_ASSERT(fixed == 1);
830
850
  null_value= 0;
831
851
  if (exec())
832
852
  {
846
866
    As far as Item_in_subselect called only from Item_in_optimizer this
847
867
    method should not be used
848
868
  */
849
 
  assert(0);
850
 
  assert(fixed == 1);
 
869
  DBUG_ASSERT(0);
 
870
  DBUG_ASSERT(fixed == 1);
851
871
  null_value= 0;
852
872
  if (exec())
853
873
  {
860
880
    null_value= 1;
861
881
    return 0;
862
882
  }
863
 
  str->set((uint64_t)value, &my_charset_bin);
 
883
  str->set((ulonglong)value, &my_charset_bin);
864
884
  return str;
865
885
}
866
886
 
867
887
 
868
888
bool Item_in_subselect::val_bool()
869
889
{
870
 
  assert(fixed == 1);
 
890
  DBUG_ASSERT(fixed == 1);
871
891
  null_value= 0;
872
892
  if (exec())
873
893
  {
891
911
    As far as Item_in_subselect called only from Item_in_optimizer this
892
912
    method should not be used
893
913
  */
894
 
  assert(0);
 
914
  DBUG_ASSERT(0);
895
915
  null_value= 0;
896
 
  assert(fixed == 1);
 
916
  DBUG_ASSERT(fixed == 1);
897
917
  if (exec())
898
918
  {
899
919
    reset();
946
966
                                            Comp_creator *func)
947
967
{
948
968
  SELECT_LEX *select_lex= join->select_lex;
 
969
  DBUG_ENTER("Item_in_subselect::single_value_transformer");
949
970
 
950
971
  /*
951
972
    Check that the right part of the subselect contains no more than one
954
975
  if (select_lex->item_list.elements > 1)
955
976
  {
956
977
    my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
957
 
    return(RES_ERROR);
 
978
    DBUG_RETURN(RES_ERROR);
958
979
  }
959
980
 
960
981
  /*
974
995
    if (substitution)
975
996
    {
976
997
      // It is second (third, ...) SELECT of UNION => All is done
977
 
      return(RES_OK);
 
998
      DBUG_RETURN(RES_OK);
978
999
    }
979
1000
 
980
1001
    Item *subs;
1019
1040
        we do not check item->fixed
1020
1041
      */
1021
1042
      if (item->fix_fields(thd, 0))
1022
 
        return(RES_ERROR);
 
1043
        DBUG_RETURN(RES_ERROR);
1023
1044
      thd->lex->allow_sum_func= save_allow_sum_func; 
1024
1045
      /* we added aggregate function => we have to change statistic */
1025
1046
      count_field_types(select_lex, &join->tmp_table_param, join->all_fields, 
1036
1057
    }
1037
1058
    /* fix fields is already called for  left expression */
1038
1059
    substitution= func->create(left_expr, subs);
1039
 
    return(RES_OK);
 
1060
    DBUG_RETURN(RES_OK);
1040
1061
  }
1041
1062
 
1042
1063
  if (!substitution)
1052
1073
    if (!optimizer || optimizer->fix_left(thd, 0))
1053
1074
    {
1054
1075
      thd->lex->current_select= current;
1055
 
      return(RES_ERROR);
 
1076
      DBUG_RETURN(RES_ERROR);
1056
1077
    }
1057
1078
    thd->lex->current_select= current;
1058
1079
 
1071
1092
  if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards)
1072
1093
  {
1073
1094
    if (!(pushed_cond_guards= (bool*)join->thd->alloc(sizeof(bool))))
1074
 
      return(RES_ERROR);
 
1095
      DBUG_RETURN(RES_ERROR);
1075
1096
    pushed_cond_guards[0]= true;
1076
1097
  }
1077
1098
 
1080
1101
    perform the IN -> EXISTS transformation.
1081
1102
  */
1082
1103
  if (exec_method == MATERIALIZATION)
1083
 
    return(RES_OK);
 
1104
    DBUG_RETURN(RES_OK);
1084
1105
 
1085
1106
  /* Perform the IN=>EXISTS transformation. */
1086
 
  return(single_value_in_to_exists_transformer(join, func));
 
1107
  DBUG_RETURN(single_value_in_to_exists_transformer(join, func));
1087
1108
}
1088
1109
 
1089
1110
 
1127
1148
Item_in_subselect::single_value_in_to_exists_transformer(JOIN * join, Comp_creator *func)
1128
1149
{
1129
1150
  SELECT_LEX *select_lex= join->select_lex;
 
1151
  DBUG_ENTER("Item_in_subselect::single_value_in_to_exists_transformer");
1130
1152
 
1131
1153
  select_lex->uncacheable|= UNCACHEABLE_DEPENDENT;
1132
1154
  if (join->having || select_lex->with_sum_func ||
1165
1187
    tmp= join->having->fix_fields(thd, 0);
1166
1188
    select_lex->having_fix_field= 0;
1167
1189
    if (tmp)
1168
 
      return(RES_ERROR);
 
1190
      DBUG_RETURN(RES_ERROR);
1169
1191
  }
1170
1192
  else
1171
1193
  {
1177
1199
      Item *having= item, *orig_item= item;
1178
1200
      select_lex->item_list.empty();
1179
1201
      select_lex->item_list.push_back(new Item_int("Not_used",
1180
 
                                                   (int64_t) 1,
 
1202
                                                   (longlong) 1,
1181
1203
                                                   MY_INT64_NUM_DECIMAL_DIGITS));
1182
1204
      select_lex->ref_pointer_array[0]= select_lex->item_list.head();
1183
1205
       
1189
1211
        {
1190
1212
          if (!(having= new Item_func_trig_cond(having,
1191
1213
                                                get_cond_guard(0))))
1192
 
            return(RES_ERROR);
 
1214
            DBUG_RETURN(RES_ERROR);
1193
1215
        }
1194
1216
        /*
1195
1217
          Item_is_not_null_test can't be changed during fix_fields()
1207
1229
        tmp= join->having->fix_fields(thd, 0);
1208
1230
        select_lex->having_fix_field= 0;
1209
1231
        if (tmp)
1210
 
          return(RES_ERROR);
 
1232
          DBUG_RETURN(RES_ERROR);
1211
1233
        item= new Item_cond_or(item,
1212
1234
                               new Item_func_isnull(orig_item));
1213
1235
      }
1218
1240
      if (!abort_on_null && left_expr->maybe_null)
1219
1241
      {
1220
1242
        if (!(item= new Item_func_trig_cond(item, get_cond_guard(0))))
1221
 
          return(RES_ERROR);
 
1243
          DBUG_RETURN(RES_ERROR);
1222
1244
      }
1223
1245
      /*
1224
1246
        TODO: figure out why the following is done here in 
1239
1261
        after creation
1240
1262
      */
1241
1263
      if (join->conds->fix_fields(thd, 0))
1242
 
        return(RES_ERROR);
 
1264
        DBUG_RETURN(RES_ERROR);
1243
1265
    }
1244
1266
    else
1245
1267
    {
1261
1283
        {
1262
1284
          if (!(new_having= new Item_func_trig_cond(new_having,
1263
1285
                                                    get_cond_guard(0))))
1264
 
            return(RES_ERROR);
 
1286
            DBUG_RETURN(RES_ERROR);
1265
1287
        }
1266
1288
        new_having->name= (char*)in_having_cond;
1267
1289
        select_lex->having= join->having= new_having;
1274
1296
        tmp= join->having->fix_fields(thd, 0);
1275
1297
        select_lex->having_fix_field= 0;
1276
1298
        if (tmp)
1277
 
          return(RES_ERROR);
 
1299
          DBUG_RETURN(RES_ERROR);
1278
1300
      }
1279
1301
      else
1280
1302
      {
1285
1307
        have_to_be_excluded= 1;
1286
1308
        if (thd->lex->describe)
1287
1309
        {
1288
 
          char warn_buff[DRIZZLE_ERRMSG_SIZE];
 
1310
          char warn_buff[MYSQL_ERRMSG_SIZE];
1289
1311
          sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number);
1290
 
          push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
1312
          push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
1291
1313
                       ER_SELECT_REDUCED, warn_buff);
1292
1314
        }
1293
 
        return(RES_REDUCE);
 
1315
        DBUG_RETURN(RES_REDUCE);
1294
1316
      }
1295
1317
    }
1296
1318
  }
1297
1319
 
1298
 
  return(RES_OK);
 
1320
  DBUG_RETURN(RES_OK);
1299
1321
}
1300
1322
 
1301
1323
 
1303
1325
Item_in_subselect::row_value_transformer(JOIN *join)
1304
1326
{
1305
1327
  SELECT_LEX *select_lex= join->select_lex;
1306
 
  uint32_t cols_num= left_expr->cols();
 
1328
  uint cols_num= left_expr->cols();
 
1329
 
 
1330
  DBUG_ENTER("Item_in_subselect::row_value_transformer");
1307
1331
 
1308
1332
  if (select_lex->item_list.elements != left_expr->cols())
1309
1333
  {
1310
1334
    my_error(ER_OPERAND_COLUMNS, MYF(0), left_expr->cols());
1311
 
    return(RES_ERROR);
 
1335
    DBUG_RETURN(RES_ERROR);
1312
1336
  }
1313
1337
 
1314
1338
  /*
1327
1351
    if (!optimizer || optimizer->fix_left(thd, 0))
1328
1352
    {
1329
1353
      thd->lex->current_select= current;
1330
 
      return(RES_ERROR);
 
1354
      DBUG_RETURN(RES_ERROR);
1331
1355
    }
1332
1356
 
1333
1357
    // we will refer to upper level cache array => we have to save it in PS
1340
1364
    {
1341
1365
      if (!(pushed_cond_guards= (bool*)join->thd->alloc(sizeof(bool) *
1342
1366
                                                        left_expr->cols())))
1343
 
        return(RES_ERROR);
1344
 
      for (uint32_t i= 0; i < cols_num; i++)
 
1367
        DBUG_RETURN(RES_ERROR);
 
1368
      for (uint i= 0; i < cols_num; i++)
1345
1369
        pushed_cond_guards[i]= true;
1346
1370
    }
1347
1371
  }
1351
1375
    perform the IN -> EXISTS transformation.
1352
1376
  */
1353
1377
  if (exec_method == MATERIALIZATION)
1354
 
    return(RES_OK);
 
1378
    DBUG_RETURN(RES_OK);
1355
1379
 
1356
1380
  /* Perform the IN=>EXISTS transformation. */
1357
 
  return(row_value_in_to_exists_transformer(join));
 
1381
  DBUG_RETURN(row_value_in_to_exists_transformer(join));
1358
1382
}
1359
1383
 
1360
1384
 
1381
1405
{
1382
1406
  SELECT_LEX *select_lex= join->select_lex;
1383
1407
  Item *having_item= 0;
1384
 
  uint32_t cols_num= left_expr->cols();
 
1408
  uint cols_num= left_expr->cols();
1385
1409
  bool is_having_used= (join->having || select_lex->with_sum_func ||
1386
1410
                        select_lex->group_list.first ||
1387
1411
                        !select_lex->table_list.elements);
1388
1412
 
 
1413
  DBUG_ENTER("Item_in_subselect::row_value_in_to_exists_transformer");
 
1414
 
1389
1415
  select_lex->uncacheable|= UNCACHEABLE_DEPENDENT;
1390
1416
  if (is_having_used)
1391
1417
  {
1403
1429
      TODO: say here explicitly if the order of AND parts matters or not.
1404
1430
    */
1405
1431
    Item *item_having_part2= 0;
1406
 
    for (uint32_t i= 0; i < cols_num; i++)
 
1432
    for (uint i= 0; i < cols_num; i++)
1407
1433
    {
1408
 
      assert((left_expr->fixed && select_lex->ref_pointer_array[i]->fixed) ||
 
1434
      DBUG_ASSERT(left_expr->fixed &&
 
1435
                  select_lex->ref_pointer_array[i]->fixed ||
1409
1436
                  (select_lex->ref_pointer_array[i]->type() == REF_ITEM &&
1410
1437
                   ((Item_ref*)(select_lex->ref_pointer_array[i]))->ref_type() ==
1411
1438
                    Item_ref::OUTER_REF));
1412
1439
      if (select_lex->ref_pointer_array[i]->
1413
1440
          check_cols(left_expr->element_index(i)->cols()))
1414
 
        return(RES_ERROR);
 
1441
        DBUG_RETURN(RES_ERROR);
1415
1442
      Item *item_eq=
1416
1443
        new Item_func_eq(new
1417
1444
                         Item_ref(&select_lex->context,
1436
1463
      if (!abort_on_null && left_expr->element_index(i)->maybe_null)
1437
1464
      {
1438
1465
        if (!(col_item= new Item_func_trig_cond(col_item, get_cond_guard(i))))
1439
 
          return(RES_ERROR);
 
1466
          DBUG_RETURN(RES_ERROR);
1440
1467
      }
1441
1468
      having_item= and_items(having_item, col_item);
1442
1469
      
1451
1478
      {
1452
1479
        if (!(item_nnull_test= 
1453
1480
              new Item_func_trig_cond(item_nnull_test, get_cond_guard(i))))
1454
 
          return(RES_ERROR);
 
1481
          DBUG_RETURN(RES_ERROR);
1455
1482
      }
1456
1483
      item_having_part2= and_items(item_having_part2, item_nnull_test);
1457
1484
      item_having_part2->top_level_item();
1479
1506
                               (l3 = v3)
1480
1507
    */
1481
1508
    Item *where_item= 0;
1482
 
    for (uint32_t i= 0; i < cols_num; i++)
 
1509
    for (uint i= 0; i < cols_num; i++)
1483
1510
    {
1484
1511
      Item *item, *item_isnull;
1485
 
      assert((left_expr->fixed && select_lex->ref_pointer_array[i]->fixed) ||
 
1512
      DBUG_ASSERT(left_expr->fixed &&
 
1513
                  select_lex->ref_pointer_array[i]->fixed ||
1486
1514
                  (select_lex->ref_pointer_array[i]->type() == REF_ITEM &&
1487
1515
                   ((Item_ref*)(select_lex->ref_pointer_array[i]))->ref_type() ==
1488
1516
                    Item_ref::OUTER_REF));
1489
1517
      if (select_lex->ref_pointer_array[i]->
1490
1518
          check_cols(left_expr->element_index(i)->cols()))
1491
 
        return(RES_ERROR);
 
1519
        DBUG_RETURN(RES_ERROR);
1492
1520
      item=
1493
1521
        new Item_func_eq(new
1494
1522
                         Item_direct_ref(&select_lex->context,
1530
1558
        if (left_expr->element_index(i)->maybe_null)
1531
1559
        {
1532
1560
          if (!(item= new Item_func_trig_cond(item, get_cond_guard(i))))
1533
 
            return(RES_ERROR);
 
1561
            DBUG_RETURN(RES_ERROR);
1534
1562
          if (!(having_col_item= 
1535
1563
                  new Item_func_trig_cond(having_col_item, get_cond_guard(i))))
1536
 
            return(RES_ERROR);
 
1564
            DBUG_RETURN(RES_ERROR);
1537
1565
        }
1538
1566
        having_item= and_items(having_item, having_col_item);
1539
1567
      }
1547
1575
    select_lex->where= join->conds= and_items(join->conds, where_item);
1548
1576
    select_lex->where->top_level_item();
1549
1577
    if (join->conds->fix_fields(thd, 0))
1550
 
      return(RES_ERROR);
 
1578
      DBUG_RETURN(RES_ERROR);
1551
1579
  }
1552
1580
  if (having_item)
1553
1581
  {
1566
1594
    select_lex->having_fix_field= 0;
1567
1595
    if (res)
1568
1596
    {
1569
 
      return(RES_ERROR);
 
1597
      DBUG_RETURN(RES_ERROR);
1570
1598
    }
1571
1599
  }
1572
1600
 
1573
 
  return(RES_OK);
 
1601
  DBUG_RETURN(RES_OK);
1574
1602
}
1575
1603
 
1576
1604
 
1610
1638
  Item_subselect::trans_res res= RES_ERROR;
1611
1639
  bool result;
1612
1640
 
 
1641
  DBUG_ENTER("Item_in_subselect::select_in_like_transformer");
 
1642
 
1613
1643
  {
1614
1644
    /*
1615
1645
      IN/SOME/ALL/ANY subqueries aren't support LIMIT clause. Without it
1616
 
      order_st BY clause becomes meaningless thus we drop it here.
 
1646
      ORDER BY clause becomes meaningless thus we drop it here.
1617
1647
    */
1618
1648
    SELECT_LEX *sl= current->master_unit()->first_select();
1619
1649
    for (; sl; sl= sl->next_select())
1624
1654
  }
1625
1655
 
1626
1656
  if (changed)
1627
 
    return(RES_OK);
 
1657
    DBUG_RETURN(RES_OK);
1628
1658
 
1629
1659
  thd->where= "IN/ALL/ANY subquery";
1630
1660
 
1672
1702
    if (func != &eq_creator)
1673
1703
    {
1674
1704
      my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
1675
 
      return(RES_ERROR);
 
1705
      DBUG_RETURN(RES_ERROR);
1676
1706
    }
1677
1707
    res= row_value_transformer(join);
1678
1708
  }
1679
1709
err:
1680
1710
  thd->where= save_where;
1681
 
  return(res);
 
1711
  DBUG_RETURN(res);
1682
1712
}
1683
1713
 
1684
1714
 
1702
1732
  if (exec_method == SEMI_JOIN)
1703
1733
    return !( (*ref)= new Item_int(1));
1704
1734
 
 
1735
  if (thd_arg->lex->view_prepare_mode && left_expr && !left_expr->fixed)
 
1736
    result = left_expr->fix_fields(thd_arg, &left_expr);
 
1737
 
1705
1738
  return result || Item_subselect::fix_fields(thd_arg, ref);
1706
1739
}
1707
1740
 
1735
1768
  subselect_hash_sj_engine *new_engine= NULL;
1736
1769
  bool res= false;
1737
1770
 
 
1771
  DBUG_ENTER("Item_in_subselect::setup_engine");
 
1772
 
1738
1773
  if (engine->engine_type() == subselect_engine::SINGLE_SELECT_ENGINE)
1739
1774
  {
1740
1775
    /* Create/initialize objects in permanent memory. */
1767
1802
  }
1768
1803
  else
1769
1804
  {
1770
 
    assert(engine->engine_type() == subselect_engine::HASH_SJ_ENGINE);
 
1805
    DBUG_ASSERT(engine->engine_type() == subselect_engine::HASH_SJ_ENGINE);
1771
1806
    new_engine= (subselect_hash_sj_engine*) engine;
1772
1807
  }
1773
1808
 
1784
1819
    */
1785
1820
    unit->global_parameters->select_limit= NULL;
1786
1821
    if ((res= new_engine->init_runtime()))
1787
 
      return(res);
 
1822
      DBUG_RETURN(res);
1788
1823
  }
1789
1824
 
1790
 
  return(res);
 
1825
  DBUG_RETURN(res);
1791
1826
}
1792
1827
 
1793
1828
 
1828
1863
  if (!(left_expr_cache= new List<Cached_item>))
1829
1864
    return true;
1830
1865
 
1831
 
  for (uint32_t i= 0; i < left_expr->cols(); i++)
 
1866
  for (uint i= 0; i < left_expr->cols(); i++)
1832
1867
  {
1833
1868
    Cached_item *cur_item_cache= new_Cached_item(thd,
1834
1869
                                                 left_expr->element_index(i),
1853
1888
  @retval false otherwise
1854
1889
*/
1855
1890
 
1856
 
bool Item_in_subselect::is_expensive_processor(unsigned char *arg __attribute__((unused)))
 
1891
bool Item_in_subselect::is_expensive_processor(uchar *arg __attribute__((__unused__)))
1857
1892
{
1858
1893
  return exec_method == MATERIALIZATION;
1859
1894
}
1862
1897
Item_subselect::trans_res
1863
1898
Item_allany_subselect::select_transformer(JOIN *join)
1864
1899
{
 
1900
  DBUG_ENTER("Item_allany_subselect::select_transformer");
1865
1901
  exec_method= IN_TO_EXISTS;
1866
1902
  if (upper_item)
1867
1903
    upper_item->show= 1;
1868
 
  return(select_in_like_transformer(join, func));
 
1904
  DBUG_RETURN(select_in_like_transformer(join, func));
1869
1905
}
1870
1906
 
1871
1907
 
1905
1941
 
1906
1942
void subselect_single_select_engine::cleanup()
1907
1943
{
 
1944
  DBUG_ENTER("subselect_single_select_engine::cleanup");
1908
1945
  prepared= executed= 0;
1909
1946
  join= 0;
1910
1947
  result->cleanup();
1911
 
  return;
 
1948
  DBUG_VOID_RETURN;
1912
1949
}
1913
1950
 
1914
1951
 
1915
1952
void subselect_union_engine::cleanup()
1916
1953
{
 
1954
  DBUG_ENTER("subselect_union_engine::cleanup");
1917
1955
  unit->reinit_exec_mechanism();
1918
1956
  result->cleanup();
1919
 
  return;
 
1957
  DBUG_VOID_RETURN;
1920
1958
}
1921
1959
 
1922
1960
 
1950
1988
 
1951
1989
void subselect_uniquesubquery_engine::cleanup()
1952
1990
{
 
1991
  DBUG_ENTER("subselect_uniquesubquery_engine::cleanup");
1953
1992
  /* Tell handler we don't need the index anymore */
1954
1993
  if (tab->table->file->inited)
1955
1994
    tab->table->file->ha_index_end();
1956
 
  return;
 
1995
  DBUG_VOID_RETURN;
1957
1996
}
1958
1997
 
1959
1998
 
2005
2044
  SELECT_LEX *save_select= thd->lex->current_select;
2006
2045
  thd->lex->current_select= select_lex;
2007
2046
  if (join->prepare(&select_lex->ref_pointer_array,
2008
 
                    (TableList*) select_lex->table_list.first,
 
2047
                    (TABLE_LIST*) select_lex->table_list.first,
2009
2048
                    select_lex->with_wild,
2010
2049
                    select_lex->where,
2011
2050
                    select_lex->order_list.elements +
2012
2051
                    select_lex->group_list.elements,
2013
 
                    (order_st*) select_lex->order_list.first,
2014
 
                    (order_st*) select_lex->group_list.first,
 
2052
                    (ORDER*) select_lex->order_list.first,
 
2053
                    (ORDER*) select_lex->group_list.first,
2015
2054
                    select_lex->having,
2016
 
                    (order_st*) 0, select_lex,
 
2055
                    (ORDER*) 0, select_lex,
2017
2056
                    select_lex->master_unit()))
2018
2057
    return 1;
2019
2058
  thd->lex->current_select= save_select;
2028
2067
int subselect_uniquesubquery_engine::prepare()
2029
2068
{
2030
2069
  /* Should never be called. */
2031
 
  assert(false);
 
2070
  DBUG_ASSERT(false);
2032
2071
  return 1;
2033
2072
}
2034
2073
 
2063
2102
  Item *sel_item;
2064
2103
  List_iterator_fast<Item> li(item_list);
2065
2104
  res_type= STRING_RESULT;
2066
 
  res_field_type= DRIZZLE_TYPE_VARCHAR;
2067
 
  for (uint32_t i= 0; (sel_item= li++); i++)
 
2105
  res_field_type= MYSQL_TYPE_STRING;
 
2106
  for (uint i= 0; (sel_item= li++); i++)
2068
2107
  {
2069
2108
    item->max_length= sel_item->max_length;
2070
2109
    res_type= sel_item->result_type();
2082
2121
 
2083
2122
void subselect_single_select_engine::fix_length_and_dec(Item_cache **row)
2084
2123
{
2085
 
  assert(row || select_lex->item_list.elements==1);
 
2124
  DBUG_ASSERT(row || select_lex->item_list.elements==1);
2086
2125
  set_row(select_lex->item_list, row);
2087
2126
  item->collation.set(row[0]->collation);
2088
2127
  if (cols() != 1)
2091
2130
 
2092
2131
void subselect_union_engine::fix_length_and_dec(Item_cache **row)
2093
2132
{
2094
 
  assert(row || unit->first_select()->item_list.elements==1);
 
2133
  DBUG_ASSERT(row || unit->first_select()->item_list.elements==1);
2095
2134
 
2096
2135
  if (unit->first_select()->item_list.elements == 1)
2097
2136
  {
2106
2145
  }
2107
2146
}
2108
2147
 
2109
 
void subselect_uniquesubquery_engine::fix_length_and_dec(Item_cache **row __attribute__((unused)))
 
2148
void subselect_uniquesubquery_engine::fix_length_and_dec(Item_cache **row __attribute__((__unused__)))
2110
2149
{
2111
2150
  //this never should be called
2112
 
  assert(0);
 
2151
  DBUG_ASSERT(0);
2113
2152
}
2114
2153
 
2115
2154
int  init_read_record_seq(JOIN_TAB *tab);
2118
2157
 
2119
2158
int subselect_single_select_engine::exec()
2120
2159
{
 
2160
  DBUG_ENTER("subselect_single_select_engine::exec");
2121
2161
  char const *save_where= thd->where;
2122
2162
  SELECT_LEX *save_select= thd->lex->current_select;
2123
2163
  thd->lex->current_select= select_lex;
2129
2169
    if (join->flatten_subqueries())
2130
2170
    {
2131
2171
      thd->is_fatal_error= true;
2132
 
      return(1);
 
2172
      DBUG_RETURN(1);
2133
2173
    }
2134
2174
    if (join->optimize())
2135
2175
    {
2136
2176
      thd->where= save_where;
2137
2177
      executed= 1;
2138
2178
      thd->lex->current_select= save_select;
2139
 
      return(join->error ? join->error : 1);
 
2179
      DBUG_RETURN(join->error ? join->error : 1);
2140
2180
    }
2141
2181
    if (!select_lex->uncacheable && thd->lex->describe && 
2142
2182
        !(join->select_options & SELECT_DESCRIBE) && 
2151
2191
      select_lex->uncacheable|= UNCACHEABLE_EXPLAIN;
2152
2192
      select_lex->master_unit()->uncacheable|= UNCACHEABLE_EXPLAIN;
2153
2193
      if (join->init_save_join_tab())
2154
 
        return(1);                        /* purecov: inspected */
 
2194
        DBUG_RETURN(1);                        /* purecov: inspected */
2155
2195
    }
2156
2196
    if (item->engine_changed)
2157
2197
    {
2158
 
      return(1);
 
2198
      DBUG_RETURN(1);
2159
2199
    }
2160
2200
  }
2161
2201
  if (select_lex->uncacheable &&
2166
2206
    {
2167
2207
      thd->where= save_where;
2168
2208
      thd->lex->current_select= save_select;
2169
 
      return(1);
 
2209
      DBUG_RETURN(1);
2170
2210
    }
2171
2211
    item->reset();
2172
2212
    item->assigned((executed= 0));
2184
2224
        pushed down into the subquery. Those optimizations are ref[_or_null]
2185
2225
        acceses. Change them to be full table scans.
2186
2226
      */
2187
 
      for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
2227
      for (uint i=join->const_tables ; i < join->tables ; i++)
2188
2228
      {
2189
2229
        JOIN_TAB *tab=join->join_tab+i;
2190
2230
        if (tab && tab->keyuse)
2191
2231
        {
2192
 
          for (uint32_t i= 0; i < tab->ref.key_parts; i++)
 
2232
          for (uint i= 0; i < tab->ref.key_parts; i++)
2193
2233
          {
2194
2234
            bool *cond_guard= tab->ref.cond_guards[i];
2195
2235
            if (cond_guard && !*cond_guard)
2223
2263
    executed= 1;
2224
2264
    thd->where= save_where;
2225
2265
    thd->lex->current_select= save_select;
2226
 
    return(join->error||thd->is_fatal_error);
 
2266
    DBUG_RETURN(join->error||thd->is_fatal_error);
2227
2267
  }
2228
2268
  thd->where= save_where;
2229
2269
  thd->lex->current_select= save_select;
2230
 
  return(0);
 
2270
  DBUG_RETURN(0);
2231
2271
}
2232
2272
 
2233
2273
int subselect_union_engine::exec()
2260
2300
int subselect_uniquesubquery_engine::scan_table()
2261
2301
{
2262
2302
  int error;
2263
 
  Table *table= tab->table;
 
2303
  TABLE *table= tab->table;
 
2304
  DBUG_ENTER("subselect_uniquesubquery_engine::scan_table");
2264
2305
 
2265
2306
  if (table->file->inited)
2266
2307
    table->file->ha_index_end();
2274
2315
    error=table->file->rnd_next(table->record[0]);
2275
2316
    if (error && error != HA_ERR_END_OF_FILE)
2276
2317
    {
2277
 
      error= table->report_error(error);
 
2318
      error= report_error(table, error);
2278
2319
      break;
2279
2320
    }
2280
2321
    /* No more rows */
2289
2330
  }
2290
2331
 
2291
2332
  table->file->ha_rnd_end();
2292
 
  return(error != 0);
 
2333
  DBUG_RETURN(error != 0);
2293
2334
}
2294
2335
 
2295
2336
 
2337
2378
 
2338
2379
bool subselect_uniquesubquery_engine::copy_ref_key()
2339
2380
{
 
2381
  DBUG_ENTER("subselect_uniquesubquery_engine::copy_ref_key");
 
2382
 
2340
2383
  for (store_key **copy= tab->ref.key_copy ; *copy ; copy++)
2341
2384
  {
2342
2385
    enum store_key::store_key_result store_res;
2358
2401
      if (top_level)
2359
2402
      {
2360
2403
        /* Partial match on top level */
2361
 
        return(1);
 
2404
        DBUG_RETURN(1);
2362
2405
      }
2363
2406
      else
2364
2407
      {
2385
2428
      break;
2386
2429
    }
2387
2430
  }
2388
 
  return(0);
 
2431
  DBUG_RETURN(0);
2389
2432
}
2390
2433
 
2391
2434
 
2421
2464
 
2422
2465
int subselect_uniquesubquery_engine::exec()
2423
2466
{
 
2467
  DBUG_ENTER("subselect_uniquesubquery_engine::exec");
2424
2468
  int error;
2425
 
  Table *table= tab->table;
 
2469
  TABLE *table= tab->table;
2426
2470
  empty_result_set= true;
2427
2471
  table->status= 0;
2428
2472
 
2429
2473
  /* TODO: change to use of 'full_scan' here? */
2430
2474
  if (copy_ref_key())
2431
 
    return(1);
 
2475
    DBUG_RETURN(1);
2432
2476
  if (table->status)
2433
2477
  {
2434
2478
    /* 
2436
2480
      Can be set in copy_ref_key.
2437
2481
    */
2438
2482
    ((Item_in_subselect *) item)->value= 0;
2439
 
    return(0);
 
2483
    DBUG_RETURN(0);
2440
2484
  }
2441
2485
 
2442
2486
  if (null_keypart)
2443
 
    return(scan_table());
 
2487
    DBUG_RETURN(scan_table());
2444
2488
 
2445
2489
  if (!table->file->inited)
2446
2490
    table->file->ha_index_init(tab->ref.key, 0);
2448
2492
                                     tab->ref.key_buff,
2449
2493
                                     make_prev_keypart_map(tab->ref.key_parts),
2450
2494
                                     HA_READ_KEY_EXACT);
 
2495
  DBUG_PRINT("info", ("lookup result: %i", error));
2451
2496
  if (error &&
2452
2497
      error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
2453
 
    error= table->report_error(error);
 
2498
    error= report_error(table, error);
2454
2499
  else
2455
2500
  {
2456
2501
    error= 0;
2464
2509
      ((Item_in_subselect *) item)->value= 0;
2465
2510
  }
2466
2511
 
2467
 
  return(error != 0);
 
2512
  DBUG_RETURN(error != 0);
2468
2513
}
2469
2514
 
2470
2515
 
2522
2567
 
2523
2568
int subselect_indexsubquery_engine::exec()
2524
2569
{
 
2570
  DBUG_ENTER("subselect_indexsubquery_engine::exec");
2525
2571
  int error;
2526
2572
  bool null_finding= 0;
2527
 
  Table *table= tab->table;
 
2573
  TABLE *table= tab->table;
2528
2574
 
2529
2575
  ((Item_in_subselect *) item)->value= 0;
2530
2576
  empty_result_set= true;
2540
2586
 
2541
2587
  /* Copy the ref key and check for nulls... */
2542
2588
  if (copy_ref_key())
2543
 
    return(1);
 
2589
    DBUG_RETURN(1);
2544
2590
 
2545
2591
  if (table->status)
2546
2592
  {
2549
2595
      Can be set in copy_ref_key.
2550
2596
    */
2551
2597
    ((Item_in_subselect *) item)->value= 0;
2552
 
    return(0);
 
2598
    DBUG_RETURN(0);
2553
2599
  }
2554
2600
 
2555
2601
  if (null_keypart)
2556
 
    return(scan_table());
 
2602
    DBUG_RETURN(scan_table());
2557
2603
 
2558
2604
  if (!table->file->inited)
2559
2605
    table->file->ha_index_init(tab->ref.key, 1);
2563
2609
                                     HA_READ_KEY_EXACT);
2564
2610
  if (error &&
2565
2611
      error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
2566
 
    error= table->report_error(error);
 
2612
    error= report_error(table, error);
2567
2613
  else
2568
2614
  {
2569
2615
    for (;;)
2586
2632
                                            tab->ref.key_length);
2587
2633
        if (error && error != HA_ERR_END_OF_FILE)
2588
2634
        {
2589
 
          error= table->report_error(error);
 
2635
          error= report_error(table, error);
2590
2636
          break;
2591
2637
        }
2592
2638
      }
2602
2648
      }
2603
2649
    }
2604
2650
  }
2605
 
  return(error != 0);
 
2651
  DBUG_RETURN(error != 0);
2606
2652
}
2607
2653
 
2608
2654
 
2609
 
uint32_t subselect_single_select_engine::cols()
 
2655
uint subselect_single_select_engine::cols()
2610
2656
{
2611
2657
  return select_lex->item_list.elements;
2612
2658
}
2613
2659
 
2614
2660
 
2615
 
uint32_t subselect_union_engine::cols()
 
2661
uint subselect_union_engine::cols()
2616
2662
{
2617
2663
  return unit->types.elements;
2618
2664
}
2619
2665
 
2620
2666
 
2621
 
uint8_t subselect_single_select_engine::uncacheable()
 
2667
uint8 subselect_single_select_engine::uncacheable()
2622
2668
{
2623
2669
  return select_lex->uncacheable;
2624
2670
}
2625
2671
 
2626
2672
 
2627
 
uint8_t subselect_union_engine::uncacheable()
 
2673
uint8 subselect_union_engine::uncacheable()
2628
2674
{
2629
2675
  return unit->uncacheable;
2630
2676
}
2644
2690
void subselect_uniquesubquery_engine::exclude()
2645
2691
{
2646
2692
  //this never should be called
2647
 
  assert(0);
 
2693
  DBUG_ASSERT(0);
2648
2694
}
2649
2695
 
2650
2696
 
2651
 
table_map subselect_engine::calc_const_tables(TableList *table)
 
2697
table_map subselect_engine::calc_const_tables(TABLE_LIST *table)
2652
2698
{
2653
2699
  table_map map= 0;
2654
2700
  for (; table; table= table->next_leaf)
2655
2701
  {
2656
 
    Table *tbl= table->table;
 
2702
    TABLE *tbl= table->table;
2657
2703
    if (tbl && tbl->const_table)
2658
2704
      map|= tbl->map;
2659
2705
  }
2663
2709
 
2664
2710
table_map subselect_single_select_engine::upper_select_const_tables()
2665
2711
{
2666
 
  return calc_const_tables((TableList *) select_lex->outer_select()->
 
2712
  return calc_const_tables((TABLE_LIST *) select_lex->outer_select()->
2667
2713
                           leaf_tables);
2668
2714
}
2669
2715
 
2670
2716
 
2671
2717
table_map subselect_union_engine::upper_select_const_tables()
2672
2718
{
2673
 
  return calc_const_tables((TableList *) unit->outer_select()->leaf_tables);
 
2719
  return calc_const_tables((TABLE_LIST *) unit->outer_select()->leaf_tables);
2674
2720
}
2675
2721
 
2676
2722
 
2724
2770
{
2725
2771
  KEY *key_info= tab->table->key_info + tab->ref.key;
2726
2772
  str->append(STRING_WITH_LEN("<primary_index_lookup>("));
2727
 
  for (uint32_t i= 0; i < key_info->key_parts; i++)
 
2773
  for (uint i= 0; i < key_info->key_parts; i++)
2728
2774
    tab->ref.items[i]->print(str);
2729
2775
  str->append(STRING_WITH_LEN(" in "));
2730
2776
  str->append(tab->table->s->table_name.str, tab->table->s->table_name.length);
2819
2865
    true  error
2820
2866
*/
2821
2867
 
2822
 
bool subselect_uniquesubquery_engine::change_result(Item_subselect *si __attribute__((unused)),
2823
 
                                                    select_result_interceptor *res __attribute__((unused)))
 
2868
bool subselect_uniquesubquery_engine::change_result(Item_subselect *si __attribute__((__unused__)),
 
2869
                                                    select_result_interceptor *res __attribute__((__unused__)))
2824
2870
{
2825
 
  assert(0);
 
2871
  DBUG_ASSERT(0);
2826
2872
  return true;
2827
2873
}
2828
2874
 
2922
2968
  /* The result sink where we will materialize the subquery result. */
2923
2969
  select_union  *tmp_result_sink;
2924
2970
  /* The table into which the subquery is materialized. */
2925
 
  Table         *tmp_table;
 
2971
  TABLE         *tmp_table;
2926
2972
  KEY           *tmp_key; /* The only index on the temporary table. */
2927
 
  uint32_t          tmp_key_parts; /* Number of keyparts in tmp_key. */
 
2973
  uint          tmp_key_parts; /* Number of keyparts in tmp_key. */
2928
2974
  Item_in_subselect *item_in= (Item_in_subselect *) item;
2929
2975
 
 
2976
  DBUG_ENTER("subselect_hash_sj_engine::init_permanent");
 
2977
 
2930
2978
  /* 1. Create/initialize materialization related objects. */
2931
2979
 
2932
2980
  /*
2935
2983
    managed (created/filled/etc) internally by the interceptor.
2936
2984
  */
2937
2985
  if (!(tmp_result_sink= new select_union))
2938
 
    return(true);
 
2986
    DBUG_RETURN(true);
2939
2987
  if (tmp_result_sink->create_result_table(
2940
2988
                         thd, tmp_columns, true,
2941
2989
                         thd->options | TMP_TABLE_ALL_COLUMNS,
2942
2990
                         "materialized subselect", true))
2943
 
    return(true);
 
2991
    DBUG_RETURN(true);
2944
2992
 
2945
2993
  tmp_table= tmp_result_sink->table;
2946
2994
  tmp_key= tmp_table->key_info;
2955
3003
  */
2956
3004
  if (tmp_table->s->keys == 0)
2957
3005
  {
2958
 
    assert(tmp_table->s->db_type() == myisam_hton);
2959
 
    assert(
 
3006
    DBUG_ASSERT(tmp_table->s->db_type() == myisam_hton);
 
3007
    DBUG_ASSERT(
2960
3008
      tmp_table->s->uniques ||
2961
3009
      tmp_table->key_info->key_length >= tmp_table->file->max_key_length() ||
2962
3010
      tmp_table->key_info->key_parts > tmp_table->file->max_key_parts());
2963
 
    tmp_table->free_tmp_table(thd);
 
3011
    free_tmp_table(thd, tmp_table);
2964
3012
    delete result;
2965
3013
    result= NULL;
2966
 
    return(true);
 
3014
    DBUG_RETURN(true);
2967
3015
  }
2968
3016
  result= tmp_result_sink;
2969
3017
 
2971
3019
    Make sure there is only one index on the temp table, and it doesn't have
2972
3020
    the extra key part created when s->uniques > 0.
2973
3021
  */
2974
 
  assert(tmp_table->s->keys == 1 && tmp_columns->elements == tmp_key_parts);
 
3022
  DBUG_ASSERT(tmp_table->s->keys == 1 && tmp_columns->elements == tmp_key_parts);
2975
3023
 
2976
3024
 
2977
3025
  /* 2. Create/initialize execution related objects. */
2984
3032
      subselect_uniquesubquery_engine, so these objects are incomplete.
2985
3033
  */ 
2986
3034
  if (!(tab= (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB))))
2987
 
    return(true);
 
3035
    DBUG_RETURN(true);
2988
3036
  tab->table= tmp_table;
2989
3037
  tab->ref.key= 0; /* The only temp table index. */
2990
3038
  tab->ref.key_length= tmp_key->key_length;
2991
3039
  if (!(tab->ref.key_buff=
2992
 
        (unsigned char*) thd->calloc(ALIGN_SIZE(tmp_key->key_length) * 2)) ||
 
3040
        (uchar*) thd->calloc(ALIGN_SIZE(tmp_key->key_length) * 2)) ||
2993
3041
      !(tab->ref.key_copy=
2994
3042
        (store_key**) thd->alloc((sizeof(store_key*) *
2995
3043
                                  (tmp_key_parts + 1)))) ||
2996
3044
      !(tab->ref.items=
2997
3045
        (Item**) thd->alloc(sizeof(Item*) * tmp_key_parts)))
2998
 
    return(true);
 
3046
    DBUG_RETURN(true);
2999
3047
 
3000
3048
  KEY_PART_INFO *cur_key_part= tmp_key->key_part;
3001
3049
  store_key **ref_key= tab->ref.key_copy;
3002
 
  unsigned char *cur_ref_buff= tab->ref.key_buff;
 
3050
  uchar *cur_ref_buff= tab->ref.key_buff;
3003
3051
  
3004
 
  for (uint32_t i= 0; i < tmp_key_parts; i++, cur_key_part++, ref_key++)
 
3052
  for (uint i= 0; i < tmp_key_parts; i++, cur_key_part++, ref_key++)
3005
3053
  {
3006
3054
    tab->ref.items[i]= item_in->left_expr->element_index(i);
3007
3055
    int null_count= test(cur_key_part->field->real_maybe_null());
3021
3069
  tab->ref.key_err= 1;
3022
3070
  tab->ref.key_parts= tmp_key_parts;
3023
3071
 
3024
 
  return(false);
 
3072
  DBUG_RETURN(false);
3025
3073
}
3026
3074
 
3027
3075
 
3051
3099
{
3052
3100
  delete result;
3053
3101
  if (tab)
3054
 
    tab->table->free_tmp_table(thd);
 
3102
    free_tmp_table(thd, tab->table);
3055
3103
}
3056
3104
 
3057
3105
 
3086
3134
{
3087
3135
  Item_in_subselect *item_in= (Item_in_subselect *) item;
3088
3136
 
 
3137
  DBUG_ENTER("subselect_hash_sj_engine::exec");
 
3138
 
3089
3139
  /*
3090
3140
    Optimize and materialize the subquery during the first execution of
3091
3141
    the subquery predicate.
3123
3173
      empty_result_set= true;
3124
3174
      item_in->value= false;
3125
3175
      /* TODO: check we need this: item_in->null_value= false; */
3126
 
      return(false);
 
3176
      DBUG_RETURN(false);
3127
3177
    }
3128
3178
    /* Set tmp_param only if its usable, i.e. tmp_param->copy_field != NULL. */
3129
3179
    tmp_param= &(item_in->unit->outer_select()->join->tmp_table_param);
3133
3183
err:
3134
3184
    thd->lex->current_select= save_select;
3135
3185
    if (res)
3136
 
      return(res);
 
3186
      DBUG_RETURN(res);
3137
3187
  }
3138
3188
 
3139
3189
  /*
3140
3190
    Lookup the left IN operand in the hash index of the materialized subquery.
3141
3191
  */
3142
 
  return(subselect_uniquesubquery_engine::exec());
 
3192
  DBUG_RETURN(subselect_uniquesubquery_engine::exec());
3143
3193
}
3144
3194
 
3145
3195