~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join.cc

Merge in additional test case fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 * @{
28
28
 */
29
29
 
30
 
#include <config.h>
 
30
#include "config.h"
31
31
 
32
32
#include <float.h>
33
33
#include <math.h>
34
34
 
35
 
#include <drizzled/item/cache.h>
36
 
#include <drizzled/item/cmpfunc.h>
37
 
#include <drizzled/item/copy_string.h>
38
 
#include <drizzled/item/uint.h>
39
 
#include <drizzled/cached_item.h>
40
 
#include <drizzled/sql_base.h>
41
 
#include <drizzled/sql_select.h> /* include join.h */
42
 
#include <drizzled/lock.h>
43
 
#include <drizzled/nested_join.h>
44
 
#include <drizzled/join.h>
45
 
#include <drizzled/join_cache.h>
46
 
#include <drizzled/show.h>
47
 
#include <drizzled/field/blob.h>
48
 
#include <drizzled/optimizer/position.h>
49
 
#include <drizzled/optimizer/sargable_param.h>
50
 
#include <drizzled/optimizer/key_use.h>
51
 
#include <drizzled/optimizer/range.h>
52
 
#include <drizzled/optimizer/sum.h>
53
 
#include <drizzled/optimizer/explain_plan.h>
54
 
#include <drizzled/optimizer/access_method_factory.h>
55
 
#include <drizzled/optimizer/access_method.h>
56
 
#include <drizzled/records.h>
57
 
#include <drizzled/probes.h>
58
 
#include <drizzled/internal/my_bit.h>
59
 
#include <drizzled/internal/my_sys.h>
60
 
#include <drizzled/internal/iocache.h>
61
 
#include <drizzled/plugin/storage_engine.h>
62
 
#include <drizzled/session.h>
63
 
#include <drizzled/select_result.h>
 
35
#include "drizzled/item/cache.h"
 
36
#include "drizzled/item/cmpfunc.h"
 
37
#include "drizzled/item/copy_string.h"
 
38
#include "drizzled/item/uint.h"
 
39
#include "drizzled/cached_item.h"
 
40
#include "drizzled/sql_base.h"
 
41
#include "drizzled/sql_select.h" /* include join.h */
 
42
#include "drizzled/lock.h"
 
43
#include "drizzled/nested_join.h"
 
44
#include "drizzled/join.h"
 
45
#include "drizzled/join_cache.h"
 
46
#include "drizzled/show.h"
 
47
#include "drizzled/field/blob.h"
 
48
#include "drizzled/optimizer/position.h"
 
49
#include "drizzled/optimizer/sargable_param.h"
 
50
#include "drizzled/optimizer/key_use.h"
 
51
#include "drizzled/optimizer/range.h"
 
52
#include "drizzled/optimizer/sum.h"
 
53
#include "drizzled/optimizer/explain_plan.h"
 
54
#include "drizzled/optimizer/access_method_factory.h"
 
55
#include "drizzled/optimizer/access_method.h"
 
56
#include "drizzled/records.h"
 
57
#include "drizzled/probes.h"
 
58
#include "drizzled/internal/my_bit.h"
 
59
#include "drizzled/internal/my_sys.h"
 
60
#include "drizzled/internal/iocache.h"
64
61
 
65
62
#include <drizzled/debug.h>
66
63
 
70
67
 
71
68
namespace drizzled
72
69
{
 
70
 
73
71
extern plugin::StorageEngine *heap_engine;
74
72
 
75
73
/** Declarations of static functions used in this source file. */
136
134
static bool add_ref_to_table_cond(Session *session, JoinTable *join_tab);
137
135
static void free_blobs(Field **ptr); /* Rename this method...conflicts with another in global namespace... */
138
136
 
139
 
Join::Join(Session *session_arg, 
140
 
           List<Item> &fields_arg, 
141
 
           uint64_t select_options_arg,
142
 
           select_result *result_arg) :
143
 
  join_tab(NULL),
144
 
  best_ref(NULL),
145
 
  map2table(NULL),
146
 
  join_tab_save(NULL),
147
 
  table(NULL),
148
 
  all_tables(NULL),
149
 
  sort_by_table(NULL),
150
 
  tables(0),
151
 
  outer_tables(0),
152
 
  const_tables(0),
153
 
  send_group_parts(0),
154
 
  sort_and_group(false),
155
 
  first_record(false),
156
 
  full_join(false),
157
 
  group(false),
158
 
  no_field_update(false),
159
 
  do_send_rows(true),
160
 
  resume_nested_loop(false),
161
 
  no_const_tables(false),
162
 
  select_distinct(false),
163
 
  group_optimized_away(false),
164
 
  simple_order(false),
165
 
  simple_group(false),
166
 
  no_order(false),
167
 
  skip_sort_order(false),
168
 
  union_part(false),
169
 
  optimized(false),
170
 
  need_tmp(false),
171
 
  hidden_group_fields(false),
172
 
  const_table_map(0),
173
 
  found_const_table_map(0),
174
 
  outer_join(0),
175
 
  send_records(0),
176
 
  found_records(0),
177
 
  examined_rows(0),
178
 
  row_limit(0),
179
 
  select_limit(0),
180
 
  fetch_limit(HA_POS_ERROR),
181
 
  session(session_arg),
182
 
  fields_list(fields_arg), 
183
 
  join_list(NULL),
184
 
  unit(NULL),
185
 
  select_lex(NULL),
186
 
  select(NULL),
187
 
  exec_tmp_table1(NULL),
188
 
  exec_tmp_table2(NULL),
189
 
  sum_funcs(NULL),
190
 
  sum_funcs2(NULL),
191
 
  having(NULL),
192
 
  tmp_having(NULL),
193
 
  having_history(NULL),
194
 
  select_options(select_options_arg),
195
 
  result(result_arg),
196
 
  lock(session_arg->lock),
197
 
  tmp_join(NULL),
198
 
  all_fields(fields_arg),
199
 
  error(0),
200
 
  cond_equal(NULL),
201
 
  return_tab(NULL),
202
 
  ref_pointer_array(NULL),
203
 
  items0(NULL),
204
 
  items1(NULL),
205
 
  items2(NULL),
206
 
  items3(NULL),
207
 
  ref_pointer_array_size(0),
208
 
  zero_result_cause(NULL),
209
 
  sortorder(NULL),
210
 
  table_reexec(NULL),
211
 
  join_tab_reexec(NULL)
212
 
{
213
 
  select_distinct= test(select_options & SELECT_DISTINCT);
214
 
  if (&fields_list != &fields_arg) /* only copy if not same*/
215
 
    fields_list= fields_arg;
216
 
  memset(&keyuse, 0, sizeof(keyuse));
217
 
  tmp_table_param.init();
218
 
  tmp_table_param.end_write_records= HA_POS_ERROR;
219
 
  rollup.setState(Rollup::STATE_NONE);
220
 
}
221
 
 
222
 
  /** 
223
 
   * This method is currently only used when a subselect EXPLAIN is performed.
224
 
   * I pulled out the init() method and have simply reset the values to what
225
 
   * was previously in the init() method.  See the note about the hack in 
226
 
   * sql_union.cc...
227
 
   */
228
 
void Join::reset(Session *session_arg, 
229
 
                 List<Item> &fields_arg, 
230
 
                 uint64_t select_options_arg,
231
 
                 select_result *result_arg)
232
 
{
233
 
  join_tab= NULL;
234
 
  best_ref= NULL;
235
 
  map2table= NULL;
236
 
  join_tab_save= NULL;
237
 
  table= NULL;
238
 
  all_tables= NULL;
239
 
  sort_by_table= NULL;
240
 
  tables= 0;
241
 
  outer_tables= 0;
242
 
  const_tables= 0;
243
 
  send_group_parts= 0;
244
 
  sort_and_group= false;
245
 
  first_record= false;
246
 
  full_join= false;
247
 
  group= false;
248
 
  no_field_update= false;
249
 
  do_send_rows= true;
250
 
  resume_nested_loop= false;
251
 
  no_const_tables= false;
252
 
  select_distinct= false;
253
 
  group_optimized_away= false;
254
 
  simple_order= false;
255
 
  simple_group= false;
256
 
  no_order= false;
257
 
  skip_sort_order= false;
258
 
  union_part= false;
259
 
  optimized= false;
260
 
  need_tmp= false;
261
 
  hidden_group_fields= false;
262
 
  const_table_map= 0;
263
 
  found_const_table_map= 0;
264
 
  outer_join= 0;
265
 
  send_records= 0;
266
 
  found_records= 0;
267
 
  examined_rows= 0;
268
 
  row_limit= 0;
269
 
  select_limit= 0;
270
 
  fetch_limit= HA_POS_ERROR;
271
 
  session= session_arg;
272
 
  fields_list= fields_arg; 
273
 
  join_list= NULL;
274
 
  unit= NULL;
275
 
  select_lex= NULL;
276
 
  select= NULL;
277
 
  exec_tmp_table1= NULL;
278
 
  exec_tmp_table2= NULL;
279
 
  sum_funcs= NULL;
280
 
  sum_funcs2= NULL;
281
 
  having= NULL;
282
 
  tmp_having= NULL;
283
 
  having_history= NULL;
284
 
  select_options= select_options_arg;
285
 
  result= result_arg;
286
 
  lock= session_arg->lock;
287
 
  tmp_join= NULL;
288
 
  all_fields= fields_arg;
289
 
  error= 0;
290
 
  cond_equal= NULL;
291
 
  return_tab= NULL;
292
 
  ref_pointer_array= NULL;
293
 
  items0= NULL;
294
 
  items1= NULL;
295
 
  items2= NULL;
296
 
  items3= NULL;
297
 
  ref_pointer_array_size= 0;
298
 
  zero_result_cause= NULL;
299
 
  sortorder= NULL;
300
 
  table_reexec= NULL;
301
 
  join_tab_reexec= NULL;
302
 
  select_distinct= test(select_options & SELECT_DISTINCT);
303
 
  if (&fields_list != &fields_arg) /* only copy if not same*/
304
 
    fields_list= fields_arg;
305
 
  memset(&keyuse, 0, sizeof(keyuse));
306
 
  tmp_table_param.init();
307
 
  tmp_table_param.end_write_records= HA_POS_ERROR;
308
 
  rollup.setState(Rollup::STATE_NONE);
309
 
}
310
 
 
311
 
bool Join::is_top_level_join() const
312
 
{
313
 
  return (unit == &session->getLex()->unit && (unit->fake_select_lex == 0 ||
314
 
                                          select_lex == unit->fake_select_lex));
315
 
}
316
 
 
317
137
/**
318
138
  Prepare of whole select (including sub queries in future).
319
139
 
351
171
  join_list= &select_lex->top_join_list;
352
172
  union_part= unit_arg->is_union();
353
173
 
354
 
  session->getLex()->current_select->is_item_list_lookup= 1;
 
174
  session->lex->current_select->is_item_list_lookup= 1;
355
175
  /*
356
176
    If we have already executed SELECT, then it have not sense to prevent
357
177
    its table from update (see unique_table())
392
212
 
393
213
  if (having)
394
214
  {
395
 
    nesting_map save_allow_sum_func= session->getLex()->allow_sum_func;
 
215
    nesting_map save_allow_sum_func= session->lex->allow_sum_func;
396
216
    session->setWhere("having clause");
397
 
    session->getLex()->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
217
    session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
398
218
    select_lex->having_fix_field= 1;
399
219
    bool having_fix_rc= (!having->fixed &&
400
220
       (having->fix_fields(session, &having) ||
402
222
    select_lex->having_fix_field= 0;
403
223
    if (having_fix_rc || session->is_error())
404
224
      return(-1);
405
 
    session->getLex()->allow_sum_func= save_allow_sum_func;
 
225
    session->lex->allow_sum_func= save_allow_sum_func;
406
226
  }
407
227
 
408
228
  {
452
272
            in_subs  &&                                                   // 1
453
273
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
454
274
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
455
 
            session->getLex()->sql_command == SQLCOM_SELECT)                       // *
 
275
            session->lex->sql_command == SQLCOM_SELECT)                       // *
456
276
        {
457
277
          if (in_subs->is_top_level_item() &&                             // 4
458
278
              !in_subs->is_correlated &&                                  // 5
625
445
    select_limit= HA_POS_ERROR;
626
446
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
627
447
  // Ignore errors of execution if option IGNORE present
628
 
  if (session->getLex()->ignore)
629
 
    session->getLex()->current_select->no_error= 1;
 
448
  if (session->lex->ignore)
 
449
    session->lex->current_select->no_error= 1;
630
450
 
631
451
#ifdef HAVE_REF_TO_FIELDS     // Not done yet
632
452
  /* Add HAVING to WHERE if possible */
726
546
        conjunctions.
727
547
        Preserve conditions for EXPLAIN.
728
548
      */
729
 
      if (conds && !(session->getLex()->describe & DESCRIBE_EXTENDED))
 
549
      if (conds && !(session->lex->describe & DESCRIBE_EXTENDED))
730
550
      {
731
551
        COND *table_independent_conds= make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
732
552
        conds= table_independent_conds;
761
581
      !(select_options & SELECT_DESCRIBE) &&
762
582
      (!conds ||
763
583
       !(conds->used_tables() & RAND_TABLE_BIT) ||
764
 
       select_lex->master_unit() == &session->getLex()->unit)) // upper level SELECT
 
584
       select_lex->master_unit() == &session->lex->unit)) // upper level SELECT
765
585
  {
766
586
    zero_result_cause= "no matching row in const table";
767
587
    goto setup_subq_exit;
821
641
 
822
642
  if (conds &&!outer_join && const_table_map != found_const_table_map &&
823
643
      (select_options & SELECT_DESCRIBE) &&
824
 
      select_lex->master_unit() == &session->getLex()->unit) // upper level SELECT
 
644
      select_lex->master_unit() == &session->lex->unit) // upper level SELECT
825
645
  {
826
646
    conds=new Item_int((int64_t) 0,1);  // Always false
827
647
  }
1175
995
    */
1176
996
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1177
997
                             !tmp_group &&
1178
 
                             !session->getLex()->current_select->with_sum_func) ?
 
998
                             !session->lex->current_select->with_sum_func) ?
1179
999
                            select_limit : HA_POS_ERROR;
1180
1000
 
1181
1001
    if (!(exec_tmp_table1=
1352
1172
*/
1353
1173
bool Join::init_save_join_tab()
1354
1174
{
1355
 
  if (!(tmp_join= (Join*)session->getMemRoot()->allocate(sizeof(Join))))
 
1175
  if (!(tmp_join= (Join*)session->alloc(sizeof(Join))))
1356
1176
    return 1;
1357
1177
 
1358
1178
  error= 0;              // Ensure that tmp_join.error= 0
1365
1185
{
1366
1186
  if (! join_tab_save && select_lex->master_unit()->uncacheable.any())
1367
1187
  {
1368
 
    if (!(join_tab_save= (JoinTable*)session->getMemRoot()->duplicate((unsigned char*) join_tab,
 
1188
    if (!(join_tab_save= (JoinTable*)session->memdup((unsigned char*) join_tab,
1369
1189
            sizeof(JoinTable) * tables)))
1370
1190
      return 1;
1371
1191
  }
1871
1691
    for a derived table which is always materialized.
1872
1692
    Otherwise we would not be able to print the query  correctly.
1873
1693
  */
1874
 
  if (items0 && (session->getLex()->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
 
1694
  if (items0 && (session->lex->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
1875
1695
    set_items_ref_array(items0);
1876
1696
 
1877
1697
  return;
2003
1823
    Optimization: if not EXPLAIN and we are done with the Join,
2004
1824
    free all tables.
2005
1825
  */
2006
 
  bool full= (select_lex->uncacheable.none() && ! session->getLex()->describe);
 
1826
  bool full= (select_lex->uncacheable.none() && ! session->lex->describe);
2007
1827
  bool can_unlock= full;
2008
1828
 
2009
1829
  cleanup(full);
2038
1858
  if (can_unlock && lock && session->lock &&
2039
1859
      !(select_options & SELECT_NO_UNLOCK) &&
2040
1860
      !select_lex->subquery_in_having &&
2041
 
      (select_lex == (session->getLex()->unit.fake_select_lex ?
2042
 
                      session->getLex()->unit.fake_select_lex : &session->getLex()->select_lex)))
 
1861
      (select_lex == (session->lex->unit.fake_select_lex ?
 
1862
                      session->lex->unit.fake_select_lex : &session->lex->select_lex)))
2043
1863
  {
2044
1864
    /*
2045
1865
      TODO: unlock tables even if the join isn't top level select in the
2112
1932
      We can't call delete_elements() on copy_funcs as this will cause
2113
1933
      problems in free_elements() as some of the elements are then deleted.
2114
1934
    */
2115
 
    tmp_table_param.copy_funcs.clear();
 
1935
    tmp_table_param.copy_funcs.empty();
2116
1936
    /*
2117
1937
      If we have tmp_join and 'this' Join is not tmp_join and
2118
1938
      tmp_table_param.copy_field's  of them are equal then we have to remove
2213
2033
                              bool before_group_by, 
2214
2034
                              bool recompute)
2215
2035
{
2216
 
  List<Item>::iterator it(field_list.begin());
 
2036
  List_iterator_fast<Item> it(field_list);
2217
2037
  Item_sum **func;
2218
2038
  Item *item;
2219
2039
 
2259
2079
  */
2260
2080
  tmp_table_param.group_parts= send_group_parts;
2261
2081
 
2262
 
  rollup.setNullItems((Item_null_result**) session->getMemRoot()->allocate((sizeof(Item*) +
 
2082
  rollup.setNullItems((Item_null_result**) session->alloc((sizeof(Item*) +
2263
2083
                                                                sizeof(Item**) +
2264
2084
                                                                sizeof(List<Item>) +
2265
2085
                                                                ref_pointer_array_size)
2281
2101
  {
2282
2102
    rollup.getNullItems()[i]= new (session->mem_root) Item_null_result();
2283
2103
    List<Item> *rollup_fields= &rollup.getFields()[i];
2284
 
    rollup_fields->clear();
 
2104
    rollup_fields->empty();
2285
2105
    rollup.getRefPointerArrays()[i]= ref_array;
2286
2106
    ref_array+= all_fields.elements;
2287
2107
  }
2294
2114
    }
2295
2115
  }
2296
2116
 
2297
 
  List<Item>::iterator it(all_fields.begin());
 
2117
  List_iterator<Item> it(all_fields);
2298
2118
  Item *item;
2299
2119
  while ((item= it++))
2300
2120
  {
2369
2189
*/
2370
2190
bool Join::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields, Item_sum ***func)
2371
2191
{
2372
 
  List<Item>::iterator it(fields_arg.begin());
 
2192
  List_iterator_fast<Item> it(fields_arg);
2373
2193
  Item *first_field= sel_fields.head();
2374
2194
  uint32_t level;
2375
2195
 
2400
2220
    uint32_t pos= send_group_parts - level -1;
2401
2221
    bool real_fields= 0;
2402
2222
    Item *item;
2403
 
    List<Item>::iterator new_it(rollup.getFields()[pos].begin());
 
2223
    List_iterator<Item> new_it(rollup.getFields()[pos]);
2404
2224
    Item **ref_array_start= rollup.getRefPointerArrays()[pos];
2405
2225
    Order *start_group;
2406
2226
 
2414
2234
    for (i= 0, start_group= group_list ;i++ < pos ;start_group= start_group->next)
2415
2235
    {}
2416
2236
 
2417
 
    it= fields_arg.begin();
 
2237
    it.rewind();
2418
2238
    while ((item= it++))
2419
2239
    {
2420
2240
      if (item == first_field)
2549
2369
    {
2550
2370
      int write_error;
2551
2371
      Item *item;
2552
 
      List<Item>::iterator it(rollup.getFields()[i].begin());
 
2372
      List_iterator_fast<Item> it(rollup.getFields()[i]);
2553
2373
      while ((item= it++))
2554
2374
      {
2555
2375
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
3231
3051
            have STRING_RESULT result type, we increase the length
3232
3052
            by 8 as maximum pack length of such fields.
3233
3053
          */
3234
 
          if (field::isDateTime(type))
 
3054
          if (type == DRIZZLE_TYPE_DATE ||
 
3055
              type == DRIZZLE_TYPE_TIME ||
 
3056
              type == DRIZZLE_TYPE_DATETIME ||
 
3057
              type == DRIZZLE_TYPE_MICROTIME ||
 
3058
              type == DRIZZLE_TYPE_TIMESTAMP)
3235
3059
          {
3236
3060
            key_length+= 8;
3237
3061
          }
3403
3227
 
3404
3228
  table_count=join->tables;
3405
3229
  if (!(join->join_tab=join_tab=
3406
 
  (JoinTable*) session->getMemRoot()->allocate(sizeof(JoinTable)*table_count)))
 
3230
  (JoinTable*) session->alloc(sizeof(JoinTable)*table_count)))
3407
3231
    return(true);
3408
3232
 
3409
3233
  for (i= 0; i < table_count; i++)
3527
3351
    i.e. they have subqueries, unions or call stored procedures.
3528
3352
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
3529
3353
  */
3530
 
  if (join->session->getLex()->is_single_level_stmt())
 
3354
  if (join->session->lex->is_single_level_stmt())
3531
3355
    join->session->status_var.last_query_cost= join->best_read;
3532
3356
  return(false);
3533
3357
}
4040
3864
        will ensure that this will be used
4041
3865
      */
4042
3866
      best= tmp;
4043
 
      records= rnd_records;
 
3867
      records= rows2double(rnd_records);
4044
3868
      best_key= 0;
4045
3869
      /* range/index_merge/ALL/index access method are "independent", so: */
4046
3870
      best_ref_depends_map= 0;
4561
4385
  */
4562
4386
  if (!join->table_reexec)
4563
4387
  {
4564
 
    if (!(join->table_reexec= (Table**) join->session->getMemRoot()->allocate(sizeof(Table*))))
 
4388
    if (!(join->table_reexec= (Table**) join->session->alloc(sizeof(Table*))))
4565
4389
      return(true);
4566
4390
    if (join->tmp_join)
4567
4391
      join->tmp_join->table_reexec= join->table_reexec;
4569
4393
  if (!join->join_tab_reexec)
4570
4394
  {
4571
4395
    if (!(join->join_tab_reexec=
4572
 
          (JoinTable*) join->session->getMemRoot()->allocate(sizeof(JoinTable))))
 
4396
          (JoinTable*) join->session->alloc(sizeof(JoinTable))))
4573
4397
      return(true);
4574
4398
    new (join->join_tab_reexec) JoinTable();
4575
4399
    if (join->tmp_join)
4682
4506
      /* Ignore sj-nests: */
4683
4507
      if (!embedding->on_expr)
4684
4508
        continue;
4685
 
      NestedJoin *nested_join= embedding->getNestedJoin();
 
4509
      nested_join_st *nested_join= embedding->getNestedJoin();
4686
4510
      if (!nested_join->counter_)
4687
4511
      {
4688
4512
        /*
4721
4545
      if (join->tables > 1)
4722
4546
        cond->update_used_tables();             // Tablenr may have changed
4723
4547
      if (join->const_tables == join->tables &&
4724
 
          session->getLex()->current_select->master_unit() ==
4725
 
          &session->getLex()->unit)             // not upper level SELECT
 
4548
          session->lex->current_select->master_unit() ==
 
4549
          &session->lex->unit)          // not upper level SELECT
4726
4550
        join->const_table_map|=RAND_TABLE_BIT;
4727
4551
      {                                         // Check const tables
4728
4552
        COND *const_cond=
4791
4615
        tab->ref.key= -1;
4792
4616
        tab->ref.key_parts= 0;          // Don't use ref key.
4793
4617
        cur_pos= join->getPosFromOptimalPlan(i);
4794
 
        cur_pos.setFanout(tab->quick->records);
 
4618
        cur_pos.setFanout(rows2double(tab->quick->records));
4795
4619
        /*
4796
4620
           We will use join cache here : prevent sorting of the first
4797
4621
           table only and sort at the end.
4800
4624
          join->full_join= 1;
4801
4625
      }
4802
4626
 
4803
 
      if (join->full_join and not session->getLex()->current_select->is_cross and not cond)
4804
 
      {
4805
 
        my_error(ER_CARTESIAN_JOIN_ATTEMPTED, MYF(0));
4806
 
        return 1;
4807
 
      }
4808
 
 
4809
4627
      tmp= NULL;
4810
4628
      if (cond)
4811
4629
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
4836
4654
          tab->type == AM_EQ_REF)
4837
4655
      {
4838
4656
        optimizer::SqlSelect *sel= tab->select= ((optimizer::SqlSelect*)
4839
 
            session->getMemRoot()->duplicate((unsigned char*) select,
 
4657
            session->memdup((unsigned char*) select,
4840
4658
              sizeof(*select)));
4841
4659
        if (! sel)
4842
4660
          return 1;                     // End of memory
4974
4792
                                         current_map, 0)))
4975
4793
            {
4976
4794
              tab->cache.select= (optimizer::SqlSelect*)
4977
 
                session->getMemRoot()->duplicate((unsigned char*) sel, sizeof(optimizer::SqlSelect));
 
4795
                session->memdup((unsigned char*) sel, sizeof(optimizer::SqlSelect));
4978
4796
              tab->cache.select->cond= tmp;
4979
4797
              tab->cache.select->read_tables= join->const_table_map;
4980
4798
            }
5110
4928
 
5111
4929
    if (tab->insideout_match_tab)
5112
4930
    {
5113
 
      if (! (tab->insideout_buf= (unsigned char*) join->session->getMemRoot()->allocate(tab->table->key_info
 
4931
      if (! (tab->insideout_buf= (unsigned char*) join->session->alloc(tab->table->key_info
5114
4932
                                                                       [tab->index].
5115
4933
                                                                       key_length)))
5116
4934
        return true;
5292
5110
  {
5293
5111
    if (send_row)
5294
5112
    {
5295
 
      List<Item>::iterator it(fields.begin());
 
5113
      List_iterator_fast<Item> it(fields);
5296
5114
      Item *item;
5297
5115
      while ((item= it++))
5298
5116
        item->no_rows_in_result();
5428
5246
static COND *simplify_joins(Join *join, List<TableList> *join_list, COND *conds, bool top)
5429
5247
{
5430
5248
  TableList *table;
5431
 
  NestedJoin *nested_join;
 
5249
  nested_join_st *nested_join;
5432
5250
  TableList *prev_table= 0;
5433
 
  List<TableList>::iterator li(join_list->begin());
 
5251
  List_iterator<TableList> li(*join_list);
5434
5252
 
5435
5253
  /*
5436
5254
    Try to simplify join operations from join_list.
5566
5384
    Flatten nested joins that can be flattened.
5567
5385
    no ON expression and not a semi-join => can be flattened.
5568
5386
  */
5569
 
  li= join_list->begin();
 
5387
  li.rewind();
5570
5388
  while ((table= li++))
5571
5389
  {
5572
5390
    nested_join= table->getNestedJoin();
5573
5391
    if (nested_join && !table->on_expr)
5574
5392
    {
5575
5393
      TableList *tbl;
5576
 
      List<TableList>::iterator it(nested_join->join_list.begin());
 
5394
      List_iterator<TableList> it(nested_join->join_list);
5577
5395
      while ((tbl= it++))
5578
5396
      {
5579
5397
        tbl->setEmbedding(table->getEmbedding());
5596
5414
 
5597
5415
  /* Calculate how many saved fields there is in list */
5598
5416
  field_count=0;
5599
 
  List<Item>::iterator it(fields.begin());
 
5417
  List_iterator<Item> it(fields);
5600
5418
  Item *item;
5601
5419
  while ((item=it++))
5602
5420
  {
5650
5468
                               bool *hidden_group_fields)
5651
5469
{
5652
5470
  int res;
5653
 
  nesting_map save_allow_sum_func=session->getLex()->allow_sum_func ;
 
5471
  nesting_map save_allow_sum_func=session->lex->allow_sum_func ;
5654
5472
 
5655
 
  session->getLex()->allow_sum_func&= ~(1 << session->getLex()->current_select->nest_level);
 
5473
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
5656
5474
  res= session->setup_conds(tables, conds);
5657
5475
 
5658
 
  session->getLex()->allow_sum_func|= 1 << session->getLex()->current_select->nest_level;
 
5476
  session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
5659
5477
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
5660
5478
                          order);
5661
 
  session->getLex()->allow_sum_func&= ~(1 << session->getLex()->current_select->nest_level);
 
5479
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
5662
5480
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
5663
5481
                          group, hidden_group_fields);
5664
 
  session->getLex()->allow_sum_func= save_allow_sum_func;
 
5482
  session->lex->allow_sum_func= save_allow_sum_func;
5665
5483
  return(res);
5666
5484
}
5667
5485
 
5701
5519
 
5702
5520
  table_count= join->tables;
5703
5521
  stat= (JoinTable*) join->session->calloc(sizeof(JoinTable)*table_count);
5704
 
  stat_ref= (JoinTable**) join->session->getMemRoot()->allocate(sizeof(JoinTable*)*MAX_TABLES);
5705
 
  table_vector= (Table**) join->session->getMemRoot()->allocate(sizeof(Table*)*(table_count*2));
 
5522
  stat_ref= (JoinTable**) join->session->alloc(sizeof(JoinTable*)*MAX_TABLES);
 
5523
  table_vector= (Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
5706
5524
  if (! stat || ! stat_ref || ! table_vector)
5707
5525
    return 1;
5708
5526
 
5766
5584
      s->embedding_map.reset();
5767
5585
      do
5768
5586
      {
5769
 
        NestedJoin *nested_join= embedding->getNestedJoin();
 
5587
        nested_join_st *nested_join= embedding->getNestedJoin();
5770
5588
        s->embedding_map|= nested_join->nj_map;
5771
5589
        s->dependent|= embedding->getDepTables();
5772
5590
        embedding= embedding->getEmbedding();
5850
5668
    s= p_pos->getJoinTable();
5851
5669
    s->type= AM_SYSTEM;
5852
5670
    join->const_table_map|=s->table->map;
5853
 
    if ((tmp= s->joinReadConstTable(p_pos)))
 
5671
    if ((tmp= join_read_const_table(s, p_pos)))
5854
5672
    {
5855
5673
      if (tmp > 0)
5856
5674
        return 1;                       // Fatal error
5924
5742
          join->const_table_map|=table->map;
5925
5743
          set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
5926
5744
          partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5927
 
          if ((tmp= s->joinReadConstTable(partial_pos)))
 
5745
          if ((tmp= join_read_const_table(s, partial_pos)))
5928
5746
          {
5929
5747
            if (tmp > 0)
5930
5748
              return 1;                 // Fatal error
5976
5794
                if (create_ref_for_key(join, s, start_keyuse, found_const_table_map))
5977
5795
                  return 1;
5978
5796
                partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5979
 
                if ((tmp=s->joinReadConstTable(partial_pos)))
 
5797
                if ((tmp=join_read_const_table(s, partial_pos)))
5980
5798
                {
5981
5799
                  if (tmp > 0)
5982
5800
                    return 1;                   // Fatal error
6060
5878
      s->quick=select->quick;
6061
5879
      s->needed_reg=select->needed_reg;
6062
5880
      select->quick=0;
6063
 
 
6064
5881
      if (records == 0 && s->table->reginfo.impossible_range)
6065
5882
      {
6066
5883
        /*
6138
5955
*/
6139
5956
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused)
6140
5957
{
6141
 
  List<TableList>::iterator li(join_list->begin());
 
5958
  List_iterator<TableList> li(*join_list);
6142
5959
  TableList *table;
6143
5960
  while ((table= li++))
6144
5961
  {
6145
 
    NestedJoin *nested_join;
 
5962
    nested_join_st *nested_join;
6146
5963
    if ((nested_join= table->getNestedJoin()))
6147
5964
    {
6148
5965
      /*
6199
6016
}
6200
6017
 
6201
6018
/**
6202
 
  Set NestedJoin::counter=0 in all nested joins in passed list.
 
6019
  Set nested_join_st::counter=0 in all nested joins in passed list.
6203
6020
 
6204
 
    Recursively set NestedJoin::counter=0 for all nested joins contained in
 
6021
    Recursively set nested_join_st::counter=0 for all nested joins contained in
6205
6022
    the passed join_list.
6206
6023
 
6207
6024
  @param join_list  List of nested joins to process. It may also contain base
6209
6026
*/
6210
6027
static void reset_nj_counters(List<TableList> *join_list)
6211
6028
{
6212
 
  List<TableList>::iterator li(join_list->begin());
 
6029
  List_iterator<TableList> li(*join_list);
6213
6030
  TableList *table;
6214
6031
  while ((table= li++))
6215
6032
  {
6216
 
    NestedJoin *nested_join;
 
6033
    nested_join_st *nested_join;
6217
6034
    if ((nested_join= table->getNestedJoin()))
6218
6035
    {
6219
6036
      nested_join->counter_= 0;
6299
6116
  Join *join= last->join;
6300
6117
  for (;last_emb != NULL; last_emb= last_emb->getEmbedding())
6301
6118
  {
6302
 
    NestedJoin *nest= last_emb->getNestedJoin();
 
6119
    nested_join_st *nest= last_emb->getNestedJoin();
6303
6120
    
6304
6121
    bool was_fully_covered= nest->is_fully_covered();
6305
6122