~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_subselect.cc

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

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