~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join.cc

  • Committer: Brian Aker
  • Date: 2010-12-30 04:59:36 UTC
  • mto: (2040.1.2 clean) (2041.2.1 clean)
  • mto: This revision was merged to the branch mainline in revision 2041.
  • Revision ID: brian@tangent.org-20101230045936-r1i4sek9qotl8abq
result_type() to display the internal type.

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
#include "drizzled/internal/my_bit.h"
59
59
#include "drizzled/internal/my_sys.h"
60
60
#include "drizzled/internal/iocache.h"
61
 
#include "drizzled/plugin/storage_engine.h"
62
 
 
63
 
#include <drizzled/debug.h>
64
61
 
65
62
#include <algorithm>
66
63
 
68
65
 
69
66
namespace drizzled
70
67
{
 
68
 
71
69
extern plugin::StorageEngine *heap_engine;
 
70
extern std::bitset<12> test_flags;
72
71
 
73
72
/** Declarations of static functions used in this source file. */
74
73
static bool make_group_fields(Join *main_join, Join *curr_join);
134
133
static bool add_ref_to_table_cond(Session *session, JoinTable *join_tab);
135
134
static void free_blobs(Field **ptr); /* Rename this method...conflicts with another in global namespace... */
136
135
 
137
 
Join::Join(Session *session_arg, 
138
 
           List<Item> &fields_arg, 
139
 
           uint64_t select_options_arg,
140
 
           select_result *result_arg) :
141
 
  join_tab(NULL),
142
 
  best_ref(NULL),
143
 
  map2table(NULL),
144
 
  join_tab_save(NULL),
145
 
  table(NULL),
146
 
  all_tables(NULL),
147
 
  sort_by_table(NULL),
148
 
  tables(0),
149
 
  outer_tables(0),
150
 
  const_tables(0),
151
 
  send_group_parts(0),
152
 
  sort_and_group(false),
153
 
  first_record(false),
154
 
  full_join(false),
155
 
  group(false),
156
 
  no_field_update(false),
157
 
  do_send_rows(true),
158
 
  resume_nested_loop(false),
159
 
  no_const_tables(false),
160
 
  select_distinct(false),
161
 
  group_optimized_away(false),
162
 
  simple_order(false),
163
 
  simple_group(false),
164
 
  no_order(false),
165
 
  skip_sort_order(false),
166
 
  union_part(false),
167
 
  optimized(false),
168
 
  need_tmp(false),
169
 
  hidden_group_fields(false),
170
 
  const_table_map(0),
171
 
  found_const_table_map(0),
172
 
  outer_join(0),
173
 
  send_records(0),
174
 
  found_records(0),
175
 
  examined_rows(0),
176
 
  row_limit(0),
177
 
  select_limit(0),
178
 
  fetch_limit(HA_POS_ERROR),
179
 
  session(session_arg),
180
 
  fields_list(fields_arg), 
181
 
  join_list(NULL),
182
 
  unit(NULL),
183
 
  select_lex(NULL),
184
 
  select(NULL),
185
 
  exec_tmp_table1(NULL),
186
 
  exec_tmp_table2(NULL),
187
 
  sum_funcs(NULL),
188
 
  sum_funcs2(NULL),
189
 
  having(NULL),
190
 
  tmp_having(NULL),
191
 
  having_history(NULL),
192
 
  select_options(select_options_arg),
193
 
  result(result_arg),
194
 
  lock(session_arg->lock),
195
 
  tmp_join(NULL),
196
 
  all_fields(fields_arg),
197
 
  error(0),
198
 
  cond_equal(NULL),
199
 
  return_tab(NULL),
200
 
  ref_pointer_array(NULL),
201
 
  items0(NULL),
202
 
  items1(NULL),
203
 
  items2(NULL),
204
 
  items3(NULL),
205
 
  ref_pointer_array_size(0),
206
 
  zero_result_cause(NULL),
207
 
  sortorder(NULL),
208
 
  table_reexec(NULL),
209
 
  join_tab_reexec(NULL)
210
 
{
211
 
  select_distinct= test(select_options & SELECT_DISTINCT);
212
 
  if (&fields_list != &fields_arg) /* only copy if not same*/
213
 
    fields_list= fields_arg;
214
 
  memset(&keyuse, 0, sizeof(keyuse));
215
 
  tmp_table_param.init();
216
 
  tmp_table_param.end_write_records= HA_POS_ERROR;
217
 
  rollup.setState(Rollup::STATE_NONE);
218
 
}
219
 
 
220
 
  /** 
221
 
   * This method is currently only used when a subselect EXPLAIN is performed.
222
 
   * I pulled out the init() method and have simply reset the values to what
223
 
   * was previously in the init() method.  See the note about the hack in 
224
 
   * sql_union.cc...
225
 
   */
226
 
void Join::reset(Session *session_arg, 
227
 
                 List<Item> &fields_arg, 
228
 
                 uint64_t select_options_arg,
229
 
                 select_result *result_arg)
230
 
{
231
 
  join_tab= NULL;
232
 
  best_ref= NULL;
233
 
  map2table= NULL;
234
 
  join_tab_save= NULL;
235
 
  table= NULL;
236
 
  all_tables= NULL;
237
 
  sort_by_table= NULL;
238
 
  tables= 0;
239
 
  outer_tables= 0;
240
 
  const_tables= 0;
241
 
  send_group_parts= 0;
242
 
  sort_and_group= false;
243
 
  first_record= false;
244
 
  full_join= false;
245
 
  group= false;
246
 
  no_field_update= false;
247
 
  do_send_rows= true;
248
 
  resume_nested_loop= false;
249
 
  no_const_tables= false;
250
 
  select_distinct= false;
251
 
  group_optimized_away= false;
252
 
  simple_order= false;
253
 
  simple_group= false;
254
 
  no_order= false;
255
 
  skip_sort_order= false;
256
 
  union_part= false;
257
 
  optimized= false;
258
 
  need_tmp= false;
259
 
  hidden_group_fields= false;
260
 
  const_table_map= 0;
261
 
  found_const_table_map= 0;
262
 
  outer_join= 0;
263
 
  send_records= 0;
264
 
  found_records= 0;
265
 
  examined_rows= 0;
266
 
  row_limit= 0;
267
 
  select_limit= 0;
268
 
  fetch_limit= HA_POS_ERROR;
269
 
  session= session_arg;
270
 
  fields_list= fields_arg; 
271
 
  join_list= NULL;
272
 
  unit= NULL;
273
 
  select_lex= NULL;
274
 
  select= NULL;
275
 
  exec_tmp_table1= NULL;
276
 
  exec_tmp_table2= NULL;
277
 
  sum_funcs= NULL;
278
 
  sum_funcs2= NULL;
279
 
  having= NULL;
280
 
  tmp_having= NULL;
281
 
  having_history= NULL;
282
 
  select_options= select_options_arg;
283
 
  result= result_arg;
284
 
  lock= session_arg->lock;
285
 
  tmp_join= NULL;
286
 
  all_fields= fields_arg;
287
 
  error= 0;
288
 
  cond_equal= NULL;
289
 
  return_tab= NULL;
290
 
  ref_pointer_array= NULL;
291
 
  items0= NULL;
292
 
  items1= NULL;
293
 
  items2= NULL;
294
 
  items3= NULL;
295
 
  ref_pointer_array_size= 0;
296
 
  zero_result_cause= NULL;
297
 
  sortorder= NULL;
298
 
  table_reexec= NULL;
299
 
  join_tab_reexec= NULL;
300
 
  select_distinct= test(select_options & SELECT_DISTINCT);
301
 
  if (&fields_list != &fields_arg) /* only copy if not same*/
302
 
    fields_list= fields_arg;
303
 
  memset(&keyuse, 0, sizeof(keyuse));
304
 
  tmp_table_param.init();
305
 
  tmp_table_param.end_write_records= HA_POS_ERROR;
306
 
  rollup.setState(Rollup::STATE_NONE);
307
 
}
308
 
 
309
 
bool Join::is_top_level_join() const
310
 
{
311
 
  return (unit == &session->lex->unit && (unit->fake_select_lex == 0 ||
312
 
                                          select_lex == unit->fake_select_lex));
313
 
}
314
 
 
315
136
/**
316
137
  Prepare of whole select (including sub queries in future).
317
138
 
391
212
  if (having)
392
213
  {
393
214
    nesting_map save_allow_sum_func= session->lex->allow_sum_func;
394
 
    session->setWhere("having clause");
 
215
    session->where="having clause";
395
216
    session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
396
217
    select_lex->having_fix_field= 1;
397
218
    bool having_fix_rc= (!having->fixed &&
907
728
  }
908
729
  if (group_list || tmp_table_param.sum_func_count)
909
730
  {
910
 
    if (! hidden_group_fields && rollup.getState() == Rollup::STATE_NONE)
 
731
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
911
732
      select_distinct=0;
912
733
  }
913
734
  else if (select_distinct && tables - const_tables == 1)
971
792
  {
972
793
    Order *old_group_list;
973
794
    group_list= remove_constants(this, (old_group_list= group_list), conds,
974
 
                                 rollup.getState() == Rollup::STATE_NONE,
 
795
                                 rollup.state == ROLLUP::STATE_NONE,
975
796
                                 &simple_group);
976
797
    if (session->is_error())
977
798
    {
1163
984
 
1164
985
    tmp_table_param.hidden_field_count= (all_fields.elements -
1165
986
           fields_list.elements);
1166
 
    Order *tmp_group= (((not simple_group) or not (getDebug().test(debug::NO_KEY_GROUP))) ? group_list : (Order*) 0);
1167
 
 
 
987
    Order *tmp_group= ((!simple_group &&
 
988
                           ! (test_flags.test(TEST_NO_KEY_GROUP))) ? group_list :
 
989
                                                                     (Order*) 0);
1168
990
    /*
1169
991
      Pushing LIMIT to the temporary table creation is not applicable
1170
992
      when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1350
1172
*/
1351
1173
bool Join::init_save_join_tab()
1352
1174
{
1353
 
  if (!(tmp_join= (Join*)session->getMemRoot()->allocate(sizeof(Join))))
 
1175
  if (!(tmp_join= (Join*)session->alloc(sizeof(Join))))
1354
1176
    return 1;
1355
1177
 
1356
1178
  error= 0;              // Ensure that tmp_join.error= 0
1363
1185
{
1364
1186
  if (! join_tab_save && select_lex->master_unit()->uncacheable.any())
1365
1187
  {
1366
 
    if (!(join_tab_save= (JoinTable*)session->getMemRoot()->duplicate((unsigned char*) join_tab,
 
1188
    if (!(join_tab_save= (JoinTable*)session->memdup((unsigned char*) join_tab,
1367
1189
            sizeof(JoinTable) * tables)))
1368
1190
      return 1;
1369
1191
  }
1765
1587
      if (sort_table_cond)
1766
1588
      {
1767
1589
        if (!curr_table->select)
1768
 
          if (!(curr_table->select= new optimizer::SqlSelect()))
 
1590
          if (!(curr_table->select= new optimizer::SqlSelect))
1769
1591
            return;
1770
1592
        if (!curr_table->select->cond)
1771
1593
          curr_table->select->cond= sort_table_cond;
2012
1834
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
2013
1835
    {
2014
1836
      Item_subselect *subselect= sl->master_unit()->item;
2015
 
      bool full_local= full && (!subselect || 
2016
 
                                (subselect->is_evaluated() &&
2017
 
                                !subselect->is_uncacheable()));
 
1837
      bool full_local= full && (!subselect || subselect->is_evaluated());
2018
1838
      /*
2019
1839
        If this join is evaluated, we can fully clean it up and clean up all
2020
1840
        its underlying joins even if they are correlated -- they will not be
2066
1886
{
2067
1887
  if (table)
2068
1888
  {
 
1889
    JoinTable *tab,*end;
2069
1890
    /*
2070
1891
      Only a sorted table may be cached.  This sorted table is always the
2071
1892
      first non const table in join->table
2075
1896
      table[const_tables]->free_io_cache();
2076
1897
      table[const_tables]->filesort_free_buffers(full);
2077
1898
    }
2078
 
  }
2079
 
 
2080
 
  if (join_tab)
2081
 
  {
2082
 
    JoinTable *tab,*end;
2083
1899
 
2084
1900
    if (full)
2085
1901
    {
2096
1912
      }
2097
1913
    }
2098
1914
  }
2099
 
 
2100
1915
  /*
2101
1916
    We are not using tables anymore
2102
1917
    Unlock all tables. We may be in an INSERT .... SELECT statement.
2140
1955
    are not re-calculated.
2141
1956
  */
2142
1957
  for (uint32_t i= join->const_tables; i < join->tables; i++)
2143
 
  {
2144
1958
    join->table[i]->mark_as_null_row();   // All fields are NULL
2145
 
  }
2146
1959
}
2147
1960
 
2148
1961
/**
2163
1976
    If we are using rollup, we need a copy of the summary functions for
2164
1977
    each level
2165
1978
  */
2166
 
  if (rollup.getState() != Rollup::STATE_NONE)
 
1979
  if (rollup.state != ROLLUP::STATE_NONE)
2167
1980
    func_count*= (send_group_parts+1);
2168
1981
 
2169
1982
  group_parts= send_group_parts;
2226
2039
         ((Item_sum *)item)->depended_from() == select_lex))
2227
2040
      *func++= (Item_sum*) item;
2228
2041
  }
2229
 
  if (before_group_by && rollup.getState() == Rollup::STATE_INITED)
 
2042
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
2230
2043
  {
2231
 
    rollup.setState(Rollup::STATE_READY);
 
2044
    rollup.state= ROLLUP::STATE_READY;
2232
2045
    if (rollup_make_fields(field_list, send_fields, &func))
2233
2046
      return true;     // Should never happen
2234
2047
  }
2235
 
  else if (rollup.getState() == Rollup::STATE_NONE)
 
2048
  else if (rollup.state == ROLLUP::STATE_NONE)
2236
2049
  {
2237
2050
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
2238
2051
      sum_funcs_end[i]= func;
2239
2052
  }
2240
 
  else if (rollup.getState() == Rollup::STATE_READY)
 
2053
  else if (rollup.state == ROLLUP::STATE_READY)
2241
2054
    return(false);                         // Don't put end marker
2242
2055
  *func=0;          // End marker
2243
2056
  return(false);
2246
2059
/** Allocate memory needed for other rollup functions. */
2247
2060
bool Join::rollup_init()
2248
2061
{
 
2062
  uint32_t i,j;
2249
2063
  Item **ref_array;
2250
2064
 
2251
2065
  tmp_table_param.quick_group= 0; // Can't create groups in tmp table
2252
 
  rollup.setState(Rollup::STATE_INITED);
 
2066
  rollup.state= ROLLUP::STATE_INITED;
2253
2067
 
2254
2068
  /*
2255
2069
    Create pointers to the different sum function groups
2257
2071
  */
2258
2072
  tmp_table_param.group_parts= send_group_parts;
2259
2073
 
2260
 
  rollup.setNullItems((Item_null_result**) session->getMemRoot()->allocate((sizeof(Item*) +
 
2074
  if (!(rollup.null_items= (Item_null_result**) session->alloc((sizeof(Item*) +
2261
2075
                                                                sizeof(Item**) +
2262
2076
                                                                sizeof(List<Item>) +
2263
2077
                                                                ref_pointer_array_size)
2264
 
                                                               * send_group_parts ));
2265
 
  if (! rollup.getNullItems())
 
2078
                                                               * send_group_parts )))
2266
2079
  {
2267
2080
    return 1;
2268
2081
  }
2269
2082
 
2270
 
  rollup.setFields((List<Item>*) (rollup.getNullItems() + send_group_parts));
2271
 
  rollup.setRefPointerArrays((Item***) (rollup.getFields() + send_group_parts));
2272
 
  ref_array= (Item**) (rollup.getRefPointerArrays()+send_group_parts);
 
2083
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
2084
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
2085
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
2273
2086
 
2274
2087
  /*
2275
2088
    Prepare space for field list for the different levels
2276
2089
    These will be filled up in rollup_make_fields()
2277
2090
  */
2278
 
  for (uint32_t i= 0 ; i < send_group_parts ; i++)
 
2091
  for (i= 0 ; i < send_group_parts ; i++)
2279
2092
  {
2280
 
    rollup.getNullItems()[i]= new (session->mem_root) Item_null_result();
2281
 
    List<Item> *rollup_fields= &rollup.getFields()[i];
 
2093
    rollup.null_items[i]= new (session->mem_root) Item_null_result();
 
2094
    List<Item> *rollup_fields= &rollup.fields[i];
2282
2095
    rollup_fields->empty();
2283
 
    rollup.getRefPointerArrays()[i]= ref_array;
 
2096
    rollup.ref_pointer_arrays[i]= ref_array;
2284
2097
    ref_array+= all_fields.elements;
2285
2098
  }
2286
 
 
2287
 
  for (uint32_t i= 0 ; i < send_group_parts; i++)
 
2099
  for (i= 0 ; i < send_group_parts; i++)
2288
2100
  {
2289
 
    for (uint32_t j= 0 ; j < fields_list.elements ; j++)
2290
 
    {
2291
 
      rollup.getFields()[i].push_back(rollup.getNullItems()[i]);
2292
 
    }
 
2101
    for (j=0 ; j < fields_list.elements ; j++)
 
2102
      rollup.fields[i].push_back(rollup.null_items[i]);
2293
2103
  }
2294
 
 
2295
2104
  List_iterator<Item> it(all_fields);
2296
2105
  Item *item;
2297
2106
  while ((item= it++))
2398
2207
    uint32_t pos= send_group_parts - level -1;
2399
2208
    bool real_fields= 0;
2400
2209
    Item *item;
2401
 
    List_iterator<Item> new_it(rollup.getFields()[pos]);
2402
 
    Item **ref_array_start= rollup.getRefPointerArrays()[pos];
 
2210
    List_iterator<Item> new_it(rollup.fields[pos]);
 
2211
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
2403
2212
    Order *start_group;
2404
2213
 
2405
2214
    /* Point to first hidden field */
2497
2306
*/
2498
2307
int Join::rollup_send_data(uint32_t idx)
2499
2308
{
2500
 
  for (uint32_t i= send_group_parts ; i-- > idx ; )
 
2309
  uint32_t i;
 
2310
  for (i= send_group_parts ; i-- > idx ; )
2501
2311
  {
2502
2312
    /* Get reference pointers to sum functions in place */
2503
 
    memcpy(ref_pointer_array, rollup.getRefPointerArrays()[i], ref_pointer_array_size);
2504
 
 
 
2313
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
2314
     ref_pointer_array_size);
2505
2315
    if ((!having || having->val_int()))
2506
2316
    {
2507
 
      if (send_records < unit->select_limit_cnt && do_send_rows && result->send_data(rollup.getFields()[i]))
2508
 
      {
2509
 
        return 1;
2510
 
      }
 
2317
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
2318
    result->send_data(rollup.fields[i]))
 
2319
  return 1;
2511
2320
      send_records++;
2512
2321
    }
2513
2322
  }
2514
2323
  /* Restore ref_pointer_array */
2515
2324
  set_items_ref_array(current_ref_pointer_array);
2516
 
 
2517
2325
  return 0;
2518
2326
}
2519
2327
 
2538
2346
*/
2539
2347
int Join::rollup_write_data(uint32_t idx, Table *table_arg)
2540
2348
{
2541
 
  for (uint32_t i= send_group_parts ; i-- > idx ; )
 
2349
  uint32_t i;
 
2350
  for (i= send_group_parts ; i-- > idx ; )
2542
2351
  {
2543
2352
    /* Get reference pointers to sum functions in place */
2544
 
    memcpy(ref_pointer_array, rollup.getRefPointerArrays()[i],
 
2353
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
2545
2354
           ref_pointer_array_size);
2546
2355
    if ((!having || having->val_int()))
2547
2356
    {
2548
2357
      int write_error;
2549
2358
      Item *item;
2550
 
      List_iterator_fast<Item> it(rollup.getFields()[i]);
 
2359
      List_iterator_fast<Item> it(rollup.fields[i]);
2551
2360
      while ((item= it++))
2552
2361
      {
2553
2362
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
2563
2372
  }
2564
2373
  /* Restore ref_pointer_array */
2565
2374
  set_items_ref_array(current_ref_pointer_array);
2566
 
 
2567
2375
  return 0;
2568
2376
}
2569
2377
 
3232
3040
          if (type == DRIZZLE_TYPE_DATE ||
3233
3041
              type == DRIZZLE_TYPE_TIME ||
3234
3042
              type == DRIZZLE_TYPE_DATETIME ||
3235
 
              type == DRIZZLE_TYPE_MICROTIME ||
3236
3043
              type == DRIZZLE_TYPE_TIMESTAMP)
3237
3044
          {
3238
3045
            key_length+= 8;
3405
3212
 
3406
3213
  table_count=join->tables;
3407
3214
  if (!(join->join_tab=join_tab=
3408
 
  (JoinTable*) session->getMemRoot()->allocate(sizeof(JoinTable)*table_count)))
 
3215
  (JoinTable*) session->alloc(sizeof(JoinTable)*table_count)))
3409
3216
    return(true);
3410
3217
 
3411
 
  for (i= 0; i < table_count; i++)
3412
 
    new (join_tab+i) JoinTable();
3413
 
 
3414
3218
  join->full_join=0;
3415
3219
 
3416
3220
  used_tables= OUTER_REF_TABLE_BIT;   // Outer row is already read
4563
4367
  */
4564
4368
  if (!join->table_reexec)
4565
4369
  {
4566
 
    if (!(join->table_reexec= (Table**) join->session->getMemRoot()->allocate(sizeof(Table*))))
 
4370
    if (!(join->table_reexec= (Table**) join->session->alloc(sizeof(Table*))))
4567
4371
      return(true);
4568
4372
    if (join->tmp_join)
4569
4373
      join->tmp_join->table_reexec= join->table_reexec;
4571
4375
  if (!join->join_tab_reexec)
4572
4376
  {
4573
4377
    if (!(join->join_tab_reexec=
4574
 
          (JoinTable*) join->session->getMemRoot()->allocate(sizeof(JoinTable))))
 
4378
          (JoinTable*) join->session->alloc(sizeof(JoinTable))))
4575
4379
      return(true);
4576
 
    new (join->join_tab_reexec) JoinTable();
4577
4380
    if (join->tmp_join)
4578
4381
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
4579
4382
  }
4594
4397
  join->row_limit=join->unit->select_limit_cnt;
4595
4398
  join->do_send_rows = (join->row_limit) ? 1 : 0;
4596
4399
 
 
4400
  join_tab->cache.buff=0;                       /* No caching */
4597
4401
  join_tab->table=tmp_table;
4598
4402
  join_tab->select=0;
4599
4403
  join_tab->select_cond=0;
4684
4488
      /* Ignore sj-nests: */
4685
4489
      if (!embedding->on_expr)
4686
4490
        continue;
4687
 
      NestedJoin *nested_join= embedding->getNestedJoin();
 
4491
      nested_join_st *nested_join= embedding->getNestedJoin();
4688
4492
      if (!nested_join->counter_)
4689
4493
      {
4690
4494
        /*
4802
4606
          join->full_join= 1;
4803
4607
      }
4804
4608
 
4805
 
      if (join->full_join and not session->lex->current_select->is_cross and not cond)
4806
 
      {
4807
 
        my_error(ER_CARTESIAN_JOIN_ATTEMPTED, MYF(0));
4808
 
        return 1;
4809
 
      }
4810
 
 
4811
4609
      tmp= NULL;
4812
4610
      if (cond)
4813
4611
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
4838
4636
          tab->type == AM_EQ_REF)
4839
4637
      {
4840
4638
        optimizer::SqlSelect *sel= tab->select= ((optimizer::SqlSelect*)
4841
 
            session->getMemRoot()->duplicate((unsigned char*) select,
 
4639
            session->memdup((unsigned char*) select,
4842
4640
              sizeof(*select)));
4843
4641
        if (! sel)
4844
4642
          return 1;                     // End of memory
4976
4774
                                         current_map, 0)))
4977
4775
            {
4978
4776
              tab->cache.select= (optimizer::SqlSelect*)
4979
 
                session->getMemRoot()->duplicate((unsigned char*) sel, sizeof(optimizer::SqlSelect));
 
4777
                session->memdup((unsigned char*) sel, sizeof(optimizer::SqlSelect));
4980
4778
              tab->cache.select->cond= tmp;
4981
4779
              tab->cache.select->read_tables= join->const_table_map;
4982
4780
            }
5112
4910
 
5113
4911
    if (tab->insideout_match_tab)
5114
4912
    {
5115
 
      if (! (tab->insideout_buf= (unsigned char*) join->session->getMemRoot()->allocate(tab->table->key_info
 
4913
      if (! (tab->insideout_buf= (unsigned char*) join->session->alloc(tab->table->key_info
5116
4914
                                                                       [tab->index].
5117
4915
                                                                       key_length)))
5118
4916
        return true;
5430
5228
static COND *simplify_joins(Join *join, List<TableList> *join_list, COND *conds, bool top)
5431
5229
{
5432
5230
  TableList *table;
5433
 
  NestedJoin *nested_join;
 
5231
  nested_join_st *nested_join;
5434
5232
  TableList *prev_table= 0;
5435
5233
  List_iterator<TableList> li(*join_list);
5436
5234
 
5703
5501
 
5704
5502
  table_count= join->tables;
5705
5503
  stat= (JoinTable*) join->session->calloc(sizeof(JoinTable)*table_count);
5706
 
  stat_ref= (JoinTable**) join->session->getMemRoot()->allocate(sizeof(JoinTable*)*MAX_TABLES);
5707
 
  table_vector= (Table**) join->session->getMemRoot()->allocate(sizeof(Table*)*(table_count*2));
 
5504
  stat_ref= (JoinTable**) join->session->alloc(sizeof(JoinTable*)*MAX_TABLES);
 
5505
  table_vector= (Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
5708
5506
  if (! stat || ! stat_ref || ! table_vector)
5709
5507
    return 1;
5710
5508
 
5768
5566
      s->embedding_map.reset();
5769
5567
      do
5770
5568
      {
5771
 
        NestedJoin *nested_join= embedding->getNestedJoin();
 
5569
        nested_join_st *nested_join= embedding->getNestedJoin();
5772
5570
        s->embedding_map|= nested_join->nj_map;
5773
5571
        s->dependent|= embedding->getDepTables();
5774
5572
        embedding= embedding->getEmbedding();
5797
5595
       As we use bitmaps to represent the relation the complexity
5798
5596
       of the algorithm is O((number of tables)^2).
5799
5597
    */
5800
 
    for (i= 0; i < table_count; i++)
 
5598
    for (i= 0, s= stat ; i < table_count ; i++, s++)
5801
5599
    {
5802
 
      uint32_t j;
5803
 
      table= stat[i].table;
5804
 
 
5805
 
      if (!table->reginfo.join_tab->dependent)
5806
 
        continue;
5807
 
 
5808
 
      for (j= 0, s= stat; j < table_count; j++, s++)
 
5600
      for (uint32_t j= 0 ; j < table_count ; j++)
5809
5601
      {
 
5602
        table= stat[j].table;
5810
5603
        if (s->dependent & table->map)
5811
 
        {
5812
 
          table_map was_dependent= s->dependent;
5813
5604
          s->dependent |= table->reginfo.join_tab->dependent;
5814
 
          if (i > j && s->dependent != was_dependent)
5815
 
          {
5816
 
            i= j= 1;
5817
 
            break;
5818
 
          }
5819
 
        }
5820
5605
      }
 
5606
      if (s->dependent)
 
5607
        s->table->maybe_null= 1;
5821
5608
    }
5822
5609
    /* Catch illegal cross references for outer joins */
5823
5610
    for (i= 0, s= stat ; i < table_count ; i++, s++)
5828
5615
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
5829
5616
        return 1;
5830
5617
      }
5831
 
      if (outer_join & s->table->map)
5832
 
        s->table->maybe_null= 1;
5833
 
 
5834
5618
      s->key_dependent= s->dependent;
5835
5619
    }
5836
5620
  }
5852
5636
    s= p_pos->getJoinTable();
5853
5637
    s->type= AM_SYSTEM;
5854
5638
    join->const_table_map|=s->table->map;
5855
 
    if ((tmp= s->joinReadConstTable(p_pos)))
 
5639
    if ((tmp= join_read_const_table(s, p_pos)))
5856
5640
    {
5857
5641
      if (tmp > 0)
5858
5642
        return 1;                       // Fatal error
5926
5710
          join->const_table_map|=table->map;
5927
5711
          set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
5928
5712
          partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5929
 
          if ((tmp= s->joinReadConstTable(partial_pos)))
 
5713
          if ((tmp= join_read_const_table(s, partial_pos)))
5930
5714
          {
5931
5715
            if (tmp > 0)
5932
5716
              return 1;                 // Fatal error
5978
5762
                if (create_ref_for_key(join, s, start_keyuse, found_const_table_map))
5979
5763
                  return 1;
5980
5764
                partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5981
 
                if ((tmp=s->joinReadConstTable(partial_pos)))
 
5765
                if ((tmp=join_read_const_table(s, partial_pos)))
5982
5766
                {
5983
5767
                  if (tmp > 0)
5984
5768
                    return 1;                   // Fatal error
6062
5846
      s->quick=select->quick;
6063
5847
      s->needed_reg=select->needed_reg;
6064
5848
      select->quick=0;
6065
 
 
6066
5849
      if (records == 0 && s->table->reginfo.impossible_range)
6067
5850
      {
6068
5851
        /*
6144
5927
  TableList *table;
6145
5928
  while ((table= li++))
6146
5929
  {
6147
 
    NestedJoin *nested_join;
 
5930
    nested_join_st *nested_join;
6148
5931
    if ((nested_join= table->getNestedJoin()))
6149
5932
    {
6150
5933
      /*
6201
5984
}
6202
5985
 
6203
5986
/**
6204
 
  Set NestedJoin::counter=0 in all nested joins in passed list.
 
5987
  Set nested_join_st::counter=0 in all nested joins in passed list.
6205
5988
 
6206
 
    Recursively set NestedJoin::counter=0 for all nested joins contained in
 
5989
    Recursively set nested_join_st::counter=0 for all nested joins contained in
6207
5990
    the passed join_list.
6208
5991
 
6209
5992
  @param join_list  List of nested joins to process. It may also contain base
6215
5998
  TableList *table;
6216
5999
  while ((table= li++))
6217
6000
  {
6218
 
    NestedJoin *nested_join;
 
6001
    nested_join_st *nested_join;
6219
6002
    if ((nested_join= table->getNestedJoin()))
6220
6003
    {
6221
6004
      nested_join->counter_= 0;
6301
6084
  Join *join= last->join;
6302
6085
  for (;last_emb != NULL; last_emb= last_emb->getEmbedding())
6303
6086
  {
6304
 
    NestedJoin *nest= last_emb->getNestedJoin();
 
6087
    nested_join_st *nest= last_emb->getNestedJoin();
6305
6088
    
6306
6089
    bool was_fully_covered= nest->is_fully_covered();
6307
6090