~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join.cc

  • Committer: patrick crews
  • Date: 2011-06-08 03:02:27 UTC
  • mto: This revision was merged to the branch mainline in revision 2329.
  • Revision ID: gleebix@gmail.com-20110608030227-updkyv2652zvfajc
Initial voodoo worked to give us a crashme mode.  Need docs still

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008-2009 Sun Microsystems
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
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
 
 
 
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/open_tables_state.h>
 
49
#include <drizzled/optimizer/position.h>
 
50
#include <drizzled/optimizer/sargable_param.h>
 
51
#include <drizzled/optimizer/key_use.h>
 
52
#include <drizzled/optimizer/range.h>
 
53
#include <drizzled/optimizer/sum.h>
 
54
#include <drizzled/optimizer/explain_plan.h>
 
55
#include <drizzled/optimizer/access_method_factory.h>
 
56
#include <drizzled/optimizer/access_method.h>
 
57
#include <drizzled/records.h>
 
58
#include <drizzled/probes.h>
 
59
#include <drizzled/internal/my_bit.h>
 
60
#include <drizzled/internal/my_sys.h>
 
61
#include <drizzled/internal/iocache.h>
 
62
#include <drizzled/plugin/storage_engine.h>
 
63
#include <drizzled/session.h>
 
64
#include <drizzled/select_result.h>
 
65
#include <drizzled/debug.h>
 
66
#include <drizzled/item/subselect.h>
 
67
#include <drizzled/my_hash.h>
 
68
#include <drizzled/sql_lex.h>
 
69
#include <drizzled/statistics_variables.h>
 
70
#include <drizzled/system_variables.h>
62
71
#include <algorithm>
63
72
 
64
73
using namespace std;
65
74
 
66
 
namespace drizzled
67
 
{
 
75
namespace drizzled {
68
76
 
69
77
extern plugin::StorageEngine *heap_engine;
70
 
extern std::bitset<12> test_flags;
71
78
 
72
79
/** Declarations of static functions used in this source file. */
73
80
static bool make_group_fields(Join *main_join, Join *curr_join);
133
140
static bool add_ref_to_table_cond(Session *session, JoinTable *join_tab);
134
141
static void free_blobs(Field **ptr); /* Rename this method...conflicts with another in global namespace... */
135
142
 
 
143
Join::Join(Session *session_arg, 
 
144
           List<Item> &fields_arg, 
 
145
           uint64_t select_options_arg,
 
146
           select_result *result_arg) :
 
147
  join_tab(NULL),
 
148
  best_ref(NULL),
 
149
  map2table(NULL),
 
150
  join_tab_save(NULL),
 
151
  table(NULL),
 
152
  all_tables(NULL),
 
153
  sort_by_table(NULL),
 
154
  tables(0),
 
155
  outer_tables(0),
 
156
  const_tables(0),
 
157
  send_group_parts(0),
 
158
  sort_and_group(false),
 
159
  first_record(false),
 
160
  full_join(false),
 
161
  group(false),
 
162
  no_field_update(false),
 
163
  do_send_rows(true),
 
164
  resume_nested_loop(false),
 
165
  no_const_tables(false),
 
166
  select_distinct(false),
 
167
  group_optimized_away(false),
 
168
  simple_order(false),
 
169
  simple_group(false),
 
170
  no_order(false),
 
171
  skip_sort_order(false),
 
172
  union_part(false),
 
173
  optimized(false),
 
174
  need_tmp(false),
 
175
  hidden_group_fields(false),
 
176
  const_table_map(0),
 
177
  found_const_table_map(0),
 
178
  outer_join(0),
 
179
  send_records(0),
 
180
  found_records(0),
 
181
  examined_rows(0),
 
182
  row_limit(0),
 
183
  select_limit(0),
 
184
  fetch_limit(HA_POS_ERROR),
 
185
  session(session_arg),
 
186
  fields_list(fields_arg), 
 
187
  join_list(NULL),
 
188
  unit(NULL),
 
189
  select_lex(NULL),
 
190
  select(NULL),
 
191
  exec_tmp_table1(NULL),
 
192
  exec_tmp_table2(NULL),
 
193
  sum_funcs(NULL),
 
194
  sum_funcs2(NULL),
 
195
  having(NULL),
 
196
  tmp_having(NULL),
 
197
  having_history(NULL),
 
198
  select_options(select_options_arg),
 
199
  result(result_arg),
 
200
  lock(session_arg->open_tables.lock),
 
201
  tmp_join(NULL),
 
202
  all_fields(fields_arg),
 
203
  error(0),
 
204
  cond_equal(NULL),
 
205
  return_tab(NULL),
 
206
  ref_pointer_array(NULL),
 
207
  items0(NULL),
 
208
  items1(NULL),
 
209
  items2(NULL),
 
210
  items3(NULL),
 
211
  ref_pointer_array_size(0),
 
212
  zero_result_cause(NULL),
 
213
  sortorder(NULL),
 
214
  table_reexec(NULL),
 
215
  join_tab_reexec(NULL)
 
216
{
 
217
  select_distinct= test(select_options & SELECT_DISTINCT);
 
218
  if (&fields_list != &fields_arg) /* only copy if not same*/
 
219
    fields_list= fields_arg;
 
220
  memset(&keyuse, 0, sizeof(keyuse));
 
221
  tmp_table_param.init();
 
222
  tmp_table_param.end_write_records= HA_POS_ERROR;
 
223
  rollup.setState(Rollup::STATE_NONE);
 
224
}
 
225
 
 
226
  /** 
 
227
   * This method is currently only used when a subselect EXPLAIN is performed.
 
228
   * I pulled out the init() method and have simply reset the values to what
 
229
   * was previously in the init() method.  See the note about the hack in 
 
230
   * sql_union.cc...
 
231
   */
 
232
void Join::reset(Session *session_arg, 
 
233
                 List<Item> &fields_arg, 
 
234
                 uint64_t select_options_arg,
 
235
                 select_result *result_arg)
 
236
{
 
237
  join_tab= NULL;
 
238
  best_ref= NULL;
 
239
  map2table= NULL;
 
240
  join_tab_save= NULL;
 
241
  table= NULL;
 
242
  all_tables= NULL;
 
243
  sort_by_table= NULL;
 
244
  tables= 0;
 
245
  outer_tables= 0;
 
246
  const_tables= 0;
 
247
  send_group_parts= 0;
 
248
  sort_and_group= false;
 
249
  first_record= false;
 
250
  full_join= false;
 
251
  group= false;
 
252
  no_field_update= false;
 
253
  do_send_rows= true;
 
254
  resume_nested_loop= false;
 
255
  no_const_tables= false;
 
256
  select_distinct= false;
 
257
  group_optimized_away= false;
 
258
  simple_order= false;
 
259
  simple_group= false;
 
260
  no_order= false;
 
261
  skip_sort_order= false;
 
262
  union_part= false;
 
263
  optimized= false;
 
264
  need_tmp= false;
 
265
  hidden_group_fields= false;
 
266
  const_table_map= 0;
 
267
  found_const_table_map= 0;
 
268
  outer_join= 0;
 
269
  send_records= 0;
 
270
  found_records= 0;
 
271
  examined_rows= 0;
 
272
  row_limit= 0;
 
273
  select_limit= 0;
 
274
  fetch_limit= HA_POS_ERROR;
 
275
  session= session_arg;
 
276
  fields_list= fields_arg; 
 
277
  join_list= NULL;
 
278
  unit= NULL;
 
279
  select_lex= NULL;
 
280
  select= NULL;
 
281
  exec_tmp_table1= NULL;
 
282
  exec_tmp_table2= NULL;
 
283
  sum_funcs= NULL;
 
284
  sum_funcs2= NULL;
 
285
  having= NULL;
 
286
  tmp_having= NULL;
 
287
  having_history= NULL;
 
288
  select_options= select_options_arg;
 
289
  result= result_arg;
 
290
  lock= session_arg->open_tables.lock;
 
291
  tmp_join= NULL;
 
292
  all_fields= fields_arg;
 
293
  error= 0;
 
294
  cond_equal= NULL;
 
295
  return_tab= NULL;
 
296
  ref_pointer_array= NULL;
 
297
  items0= NULL;
 
298
  items1= NULL;
 
299
  items2= NULL;
 
300
  items3= NULL;
 
301
  ref_pointer_array_size= 0;
 
302
  zero_result_cause= NULL;
 
303
  sortorder= NULL;
 
304
  table_reexec= NULL;
 
305
  join_tab_reexec= NULL;
 
306
  select_distinct= test(select_options & SELECT_DISTINCT);
 
307
  if (&fields_list != &fields_arg) /* only copy if not same*/
 
308
    fields_list= fields_arg;
 
309
  memset(&keyuse, 0, sizeof(keyuse));
 
310
  tmp_table_param.init();
 
311
  tmp_table_param.end_write_records= HA_POS_ERROR;
 
312
  rollup.setState(Rollup::STATE_NONE);
 
313
}
 
314
 
 
315
bool Join::is_top_level_join() const
 
316
{
 
317
  return (unit == &session->lex().unit && (unit->fake_select_lex == 0 ||
 
318
                                          select_lex == unit->fake_select_lex));
 
319
}
 
320
 
136
321
/**
137
322
  Prepare of whole select (including sub queries in future).
138
323
 
170
355
  join_list= &select_lex->top_join_list;
171
356
  union_part= unit_arg->is_union();
172
357
 
173
 
  session->lex->current_select->is_item_list_lookup= 1;
 
358
  session->lex().current_select->is_item_list_lookup= 1;
174
359
  /*
175
360
    If we have already executed SELECT, then it have not sense to prevent
176
361
    its table from update (see unique_table())
211
396
 
212
397
  if (having)
213
398
  {
214
 
    nesting_map save_allow_sum_func= session->lex->allow_sum_func;
215
 
    session->where="having clause";
216
 
    session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
399
    nesting_map save_allow_sum_func= session->lex().allow_sum_func;
 
400
    session->setWhere("having clause");
 
401
    session->lex().allow_sum_func|= 1 << select_lex_arg->nest_level;
217
402
    select_lex->having_fix_field= 1;
218
403
    bool having_fix_rc= (!having->fixed &&
219
404
       (having->fix_fields(session, &having) ||
221
406
    select_lex->having_fix_field= 0;
222
407
    if (having_fix_rc || session->is_error())
223
408
      return(-1);
224
 
    session->lex->allow_sum_func= save_allow_sum_func;
 
409
    session->lex().allow_sum_func= save_allow_sum_func;
225
410
  }
226
411
 
227
412
  {
271
456
            in_subs  &&                                                   // 1
272
457
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
273
458
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
274
 
            session->lex->sql_command == SQLCOM_SELECT)                       // *
 
459
            session->lex().sql_command == SQLCOM_SELECT)                       // *
275
460
        {
276
461
          if (in_subs->is_top_level_item() &&                             // 4
277
462
              !in_subs->is_correlated &&                                  // 5
315
500
    } while (item_sum != end);
316
501
  }
317
502
 
318
 
  if (select_lex->inner_refs_list.elements &&
 
503
  if (select_lex->inner_refs_list.size() &&
319
504
      fix_inner_refs(session, all_fields, select_lex, ref_pointer_array))
320
505
    return(-1);
321
506
 
354
539
 
355
540
  /* Init join struct */
356
541
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
357
 
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
542
  ref_pointer_array_size= all_fields.size() * sizeof(Item*);
358
543
  this->group= group_list != 0;
359
544
  unit= unit_arg;
360
545
 
444
629
    select_limit= HA_POS_ERROR;
445
630
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
446
631
  // Ignore errors of execution if option IGNORE present
447
 
  if (session->lex->ignore)
448
 
    session->lex->current_select->no_error= 1;
 
632
  if (session->lex().ignore)
 
633
    session->lex().current_select->no_error= 1;
449
634
 
450
635
#ifdef HAVE_REF_TO_FIELDS     // Not done yet
451
636
  /* Add HAVING to WHERE if possible */
545
730
        conjunctions.
546
731
        Preserve conditions for EXPLAIN.
547
732
      */
548
 
      if (conds && !(session->lex->describe & DESCRIBE_EXTENDED))
 
733
      if (conds && !(session->lex().describe & DESCRIBE_EXTENDED))
549
734
      {
550
735
        COND *table_independent_conds= make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
551
736
        conds= table_independent_conds;
580
765
      !(select_options & SELECT_DESCRIBE) &&
581
766
      (!conds ||
582
767
       !(conds->used_tables() & RAND_TABLE_BIT) ||
583
 
       select_lex->master_unit() == &session->lex->unit)) // upper level SELECT
 
768
       select_lex->master_unit() == &session->lex().unit)) // upper level SELECT
584
769
  {
585
770
    zero_result_cause= "no matching row in const table";
586
771
    goto setup_subq_exit;
640
825
 
641
826
  if (conds &&!outer_join && const_table_map != found_const_table_map &&
642
827
      (select_options & SELECT_DESCRIBE) &&
643
 
      select_lex->master_unit() == &session->lex->unit) // upper level SELECT
 
828
      select_lex->master_unit() == &session->lex().unit) // upper level SELECT
644
829
  {
645
 
    conds=new Item_int((int64_t) 0,1);  // Always false
 
830
    conds=new Item_int(0, 1);  // Always false
646
831
  }
647
832
 
648
833
  if (make_join_select(this, select, conds))
728
913
  }
729
914
  if (group_list || tmp_table_param.sum_func_count)
730
915
  {
731
 
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
916
    if (! hidden_group_fields && rollup.getState() == Rollup::STATE_NONE)
732
917
      select_distinct=0;
733
918
  }
734
919
  else if (select_distinct && tables - const_tables == 1)
792
977
  {
793
978
    Order *old_group_list;
794
979
    group_list= remove_constants(this, (old_group_list= group_list), conds,
795
 
                                 rollup.state == ROLLUP::STATE_NONE,
 
980
                                 rollup.getState() == Rollup::STATE_NONE,
796
981
                                 &simple_group);
797
982
    if (session->is_error())
798
983
    {
982
1167
 
983
1168
    init_items_ref_array();
984
1169
 
985
 
    tmp_table_param.hidden_field_count= (all_fields.elements -
986
 
           fields_list.elements);
987
 
    Order *tmp_group= ((!simple_group &&
988
 
                           ! (test_flags.test(TEST_NO_KEY_GROUP))) ? group_list :
989
 
                                                                     (Order*) 0);
 
1170
    tmp_table_param.hidden_field_count= all_fields.size() - fields_list.size();
 
1171
    Order *tmp_group= (((not simple_group) or not (getDebug().test(debug::NO_KEY_GROUP))) ? group_list : NULL);
 
1172
 
990
1173
    /*
991
1174
      Pushing LIMIT to the temporary table creation is not applicable
992
1175
      when there is ORDER BY or GROUP BY or there is no GROUP BY, but
995
1178
    */
996
1179
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
997
1180
                             !tmp_group &&
998
 
                             !session->lex->current_select->with_sum_func) ?
 
1181
                             !session->lex().current_select->with_sum_func) ?
999
1182
                            select_limit : HA_POS_ERROR;
1000
1183
 
1001
1184
    if (!(exec_tmp_table1=
1172
1355
*/
1173
1356
bool Join::init_save_join_tab()
1174
1357
{
1175
 
  if (!(tmp_join= (Join*)session->alloc(sizeof(Join))))
 
1358
  if (!(tmp_join= (Join*)session->getMemRoot()->allocate(sizeof(Join))))
1176
1359
    return 1;
1177
1360
 
1178
1361
  error= 0;              // Ensure that tmp_join.error= 0
1185
1368
{
1186
1369
  if (! join_tab_save && select_lex->master_unit()->uncacheable.any())
1187
1370
  {
1188
 
    if (!(join_tab_save= (JoinTable*)session->memdup((unsigned char*) join_tab,
 
1371
    if (!(join_tab_save= (JoinTable*)session->getMemRoot()->duplicate((unsigned char*) join_tab,
1189
1372
            sizeof(JoinTable) * tables)))
1190
1373
      return 1;
1191
1374
  }
1334
1517
    session->set_proc_info("Copying to tmp table");
1335
1518
    if (! curr_join->sort_and_group && curr_join->const_tables != curr_join->tables)
1336
1519
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
1337
 
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
1520
    if ((tmp_error= do_select(curr_join, NULL, curr_tmp_table)))
1338
1521
    {
1339
1522
      error= tmp_error;
1340
1523
      return;
1348
1531
    curr_join->all_fields= *curr_all_fields;
1349
1532
    if (!items1)
1350
1533
    {
1351
 
      items1= items0 + all_fields.elements;
 
1534
      items1= items0 + all_fields.size();
1352
1535
      if (sort_and_group || curr_tmp_table->group)
1353
1536
      {
1354
1537
        if (change_to_use_tmp_fields(session, items1,
1355
1538
                  tmp_fields_list1, tmp_all_fields1,
1356
 
                  fields_list.elements, all_fields))
 
1539
                  fields_list.size(), all_fields))
1357
1540
          return;
1358
1541
      }
1359
1542
      else
1360
1543
      {
1361
1544
        if (change_refs_to_tmp_fields(session, items1,
1362
1545
                    tmp_fields_list1, tmp_all_fields1,
1363
 
                    fields_list.elements, all_fields))
 
1546
                    fields_list.size(), all_fields))
1364
1547
          return;
1365
1548
      }
1366
1549
      curr_join->tmp_all_fields1= tmp_all_fields1;
1410
1593
      count_field_types(select_lex, &curr_join->tmp_table_param,
1411
1594
      curr_join->tmp_all_fields1,
1412
1595
      curr_join->select_distinct && !curr_join->group_list);
1413
 
      curr_join->tmp_table_param.hidden_field_count= curr_join->tmp_all_fields1.elements
1414
 
                                                   - curr_join->tmp_fields_list1.elements;
 
1596
      curr_join->tmp_table_param.hidden_field_count= curr_join->tmp_all_fields1.size()
 
1597
                                                   - curr_join->tmp_fields_list1.size();
1415
1598
 
1416
1599
      if (exec_tmp_table2)
1417
1600
      {
1433
1616
              exec_tmp_table2= create_tmp_table(session,
1434
1617
                                                &curr_join->tmp_table_param,
1435
1618
                                                *curr_all_fields,
1436
 
                                                (Order*) 0,
 
1619
                                                NULL,
1437
1620
                                                curr_join->select_distinct &&
1438
1621
                                                !curr_join->group_list,
1439
1622
                                                1, curr_join->select_options,
1485
1668
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
1486
1669
      
1487
1670
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) 
1488
 
        || (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
1671
        || (tmp_error= do_select(curr_join, NULL, curr_tmp_table)))
1489
1672
      {
1490
1673
        error= tmp_error;
1491
1674
        return;
1497
1680
      // No sum funcs anymore
1498
1681
      if (!items2)
1499
1682
      {
1500
 
        items2= items1 + all_fields.elements;
 
1683
        items2= items1 + all_fields.size();
1501
1684
        if (change_to_use_tmp_fields(session, items2,
1502
1685
                  tmp_fields_list2, tmp_all_fields2,
1503
 
                  fields_list.elements, tmp_all_fields1))
 
1686
                  fields_list.size(), tmp_all_fields1))
1504
1687
          return;
1505
1688
        curr_join->tmp_fields_list2= tmp_fields_list2;
1506
1689
        curr_join->tmp_all_fields2= tmp_all_fields2;
1545
1728
    {
1546
1729
      if (! items0)
1547
1730
        init_items_ref_array();
1548
 
      items3= ref_pointer_array + (all_fields.elements*4);
 
1731
      items3= ref_pointer_array + (all_fields.size() * 4);
1549
1732
      setup_copy_fields(session, &curr_join->tmp_table_param,
1550
1733
      items3, tmp_fields_list3, tmp_all_fields3,
1551
 
      curr_fields_list->elements, *curr_all_fields);
 
1734
      curr_fields_list->size(), *curr_all_fields);
1552
1735
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
1553
1736
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
1554
1737
      tmp_table_param.save_copy_field_end= curr_join->tmp_table_param.copy_field_end;
1587
1770
      if (sort_table_cond)
1588
1771
      {
1589
1772
        if (!curr_table->select)
1590
 
          if (!(curr_table->select= new optimizer::SqlSelect))
 
1773
          if (!(curr_table->select= new optimizer::SqlSelect()))
1591
1774
            return;
1592
1775
        if (!curr_table->select->cond)
1593
1776
          curr_table->select->cond= sort_table_cond;
1691
1874
    for a derived table which is always materialized.
1692
1875
    Otherwise we would not be able to print the query  correctly.
1693
1876
  */
1694
 
  if (items0 && (session->lex->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
 
1877
  if (items0 && (session->lex().describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
1695
1878
    set_items_ref_array(items0);
1696
1879
 
1697
1880
  return;
1823
2006
    Optimization: if not EXPLAIN and we are done with the Join,
1824
2007
    free all tables.
1825
2008
  */
1826
 
  bool full= (select_lex->uncacheable.none() && ! session->lex->describe);
 
2009
  bool full= (select_lex->uncacheable.none() && ! session->lex().describe);
1827
2010
  bool can_unlock= full;
1828
2011
 
1829
2012
  cleanup(full);
1834
2017
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
1835
2018
    {
1836
2019
      Item_subselect *subselect= sl->master_unit()->item;
1837
 
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
2020
      bool full_local= full && (!subselect || 
 
2021
                                (subselect->is_evaluated() &&
 
2022
                                !subselect->is_uncacheable()));
1838
2023
      /*
1839
2024
        If this join is evaluated, we can fully clean it up and clean up all
1840
2025
        its underlying joins even if they are correlated -- they will not be
1853
2038
    We are not using tables anymore
1854
2039
    Unlock all tables. We may be in an INSERT .... SELECT statement.
1855
2040
  */
1856
 
  if (can_unlock && lock && session->lock &&
 
2041
  if (can_unlock && lock && session->open_tables.lock &&
1857
2042
      !(select_options & SELECT_NO_UNLOCK) &&
1858
2043
      !select_lex->subquery_in_having &&
1859
 
      (select_lex == (session->lex->unit.fake_select_lex ?
1860
 
                      session->lex->unit.fake_select_lex : &session->lex->select_lex)))
 
2044
      (select_lex == (session->lex().unit.fake_select_lex ?
 
2045
                      session->lex().unit.fake_select_lex : &session->lex().select_lex)))
1861
2046
  {
1862
2047
    /*
1863
2048
      TODO: unlock tables even if the join isn't top level select in the
1886
2071
{
1887
2072
  if (table)
1888
2073
  {
1889
 
    JoinTable *tab,*end;
1890
2074
    /*
1891
2075
      Only a sorted table may be cached.  This sorted table is always the
1892
2076
      first non const table in join->table
1896
2080
      table[const_tables]->free_io_cache();
1897
2081
      table[const_tables]->filesort_free_buffers(full);
1898
2082
    }
 
2083
  }
 
2084
 
 
2085
  if (join_tab)
 
2086
  {
 
2087
    JoinTable *tab,*end;
1899
2088
 
1900
2089
    if (full)
1901
2090
    {
1912
2101
      }
1913
2102
    }
1914
2103
  }
 
2104
 
1915
2105
  /*
1916
2106
    We are not using tables anymore
1917
2107
    Unlock all tables. We may be in an INSERT .... SELECT statement.
1925
2115
      We can't call delete_elements() on copy_funcs as this will cause
1926
2116
      problems in free_elements() as some of the elements are then deleted.
1927
2117
    */
1928
 
    tmp_table_param.copy_funcs.empty();
 
2118
    tmp_table_param.copy_funcs.clear();
1929
2119
    /*
1930
2120
      If we have tmp_join and 'this' Join is not tmp_join and
1931
2121
      tmp_table_param.copy_field's  of them are equal then we have to remove
1955
2145
    are not re-calculated.
1956
2146
  */
1957
2147
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
2148
  {
1958
2149
    join->table[i]->mark_as_null_row();   // All fields are NULL
 
2150
  }
1959
2151
}
1960
2152
 
1961
2153
/**
1976
2168
    If we are using rollup, we need a copy of the summary functions for
1977
2169
    each level
1978
2170
  */
1979
 
  if (rollup.state != ROLLUP::STATE_NONE)
 
2171
  if (rollup.getState() != Rollup::STATE_NONE)
1980
2172
    func_count*= (send_group_parts+1);
1981
2173
 
1982
2174
  group_parts= send_group_parts;
1986
2178
  */
1987
2179
  if (select_distinct)
1988
2180
  {
1989
 
    group_parts+= fields_list.elements;
 
2181
    group_parts+= fields_list.size();
1990
2182
    /*
1991
2183
      If the order_st clause is specified then it's possible that
1992
2184
      it also will be optimized, so reserve space for it too
2024
2216
                              bool before_group_by, 
2025
2217
                              bool recompute)
2026
2218
{
2027
 
  List_iterator_fast<Item> it(field_list);
 
2219
  List<Item>::iterator it(field_list.begin());
2028
2220
  Item_sum **func;
2029
2221
  Item *item;
2030
2222
 
2039
2231
         ((Item_sum *)item)->depended_from() == select_lex))
2040
2232
      *func++= (Item_sum*) item;
2041
2233
  }
2042
 
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
2234
  if (before_group_by && rollup.getState() == Rollup::STATE_INITED)
2043
2235
  {
2044
 
    rollup.state= ROLLUP::STATE_READY;
 
2236
    rollup.setState(Rollup::STATE_READY);
2045
2237
    if (rollup_make_fields(field_list, send_fields, &func))
2046
 
      return(true);     // Should never happen
 
2238
      return true;     // Should never happen
2047
2239
  }
2048
 
  else if (rollup.state == ROLLUP::STATE_NONE)
 
2240
  else if (rollup.getState() == Rollup::STATE_NONE)
2049
2241
  {
2050
2242
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
2051
2243
      sum_funcs_end[i]= func;
2052
2244
  }
2053
 
  else if (rollup.state == ROLLUP::STATE_READY)
 
2245
  else if (rollup.getState() == Rollup::STATE_READY)
2054
2246
    return(false);                         // Don't put end marker
2055
2247
  *func=0;          // End marker
2056
2248
  return(false);
2059
2251
/** Allocate memory needed for other rollup functions. */
2060
2252
bool Join::rollup_init()
2061
2253
{
2062
 
  uint32_t i,j;
2063
2254
  Item **ref_array;
2064
2255
 
2065
2256
  tmp_table_param.quick_group= 0; // Can't create groups in tmp table
2066
 
  rollup.state= ROLLUP::STATE_INITED;
 
2257
  rollup.setState(Rollup::STATE_INITED);
2067
2258
 
2068
2259
  /*
2069
2260
    Create pointers to the different sum function groups
2071
2262
  */
2072
2263
  tmp_table_param.group_parts= send_group_parts;
2073
2264
 
2074
 
  if (!(rollup.null_items= (Item_null_result**) session->alloc((sizeof(Item*) +
2075
 
                                                sizeof(Item**) +
2076
 
                                                sizeof(List<Item>) +
2077
 
                        ref_pointer_array_size)
2078
 
                        * send_group_parts )))
 
2265
  rollup.setNullItems((Item_null_result**) session->getMemRoot()->allocate((sizeof(Item*) +
 
2266
                                                                sizeof(Item**) +
 
2267
                                                                sizeof(List<Item>) +
 
2268
                                                                ref_pointer_array_size)
 
2269
                                                               * send_group_parts ));
 
2270
  if (! rollup.getNullItems())
 
2271
  {
2079
2272
    return 1;
 
2273
  }
2080
2274
 
2081
 
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
2082
 
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
2083
 
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
2275
  rollup.setFields((List<Item>*) (rollup.getNullItems() + send_group_parts));
 
2276
  rollup.setRefPointerArrays((Item***) (rollup.getFields() + send_group_parts));
 
2277
  ref_array= (Item**) (rollup.getRefPointerArrays()+send_group_parts);
2084
2278
 
2085
2279
  /*
2086
2280
    Prepare space for field list for the different levels
2087
2281
    These will be filled up in rollup_make_fields()
2088
2282
  */
2089
 
  for (i= 0 ; i < send_group_parts ; i++)
2090
 
  {
2091
 
    rollup.null_items[i]= new (session->mem_root) Item_null_result();
2092
 
    List<Item> *rollup_fields= &rollup.fields[i];
2093
 
    rollup_fields->empty();
2094
 
    rollup.ref_pointer_arrays[i]= ref_array;
2095
 
    ref_array+= all_fields.elements;
2096
 
  }
2097
 
  for (i= 0 ; i < send_group_parts; i++)
2098
 
  {
2099
 
    for (j=0 ; j < fields_list.elements ; j++)
2100
 
      rollup.fields[i].push_back(rollup.null_items[i]);
2101
 
  }
2102
 
  List_iterator<Item> it(all_fields);
 
2283
  for (uint32_t i= 0 ; i < send_group_parts ; i++)
 
2284
  {
 
2285
    rollup.getNullItems()[i]= new (session->mem_root) Item_null_result();
 
2286
    List<Item> *rollup_fields= &rollup.getFields()[i];
 
2287
    rollup_fields->clear();
 
2288
    rollup.getRefPointerArrays()[i]= ref_array;
 
2289
    ref_array+= all_fields.size();
 
2290
  }
 
2291
 
 
2292
  for (uint32_t i= 0 ; i < send_group_parts; i++)
 
2293
  {
 
2294
    for (uint32_t j= 0 ; j < fields_list.size() ; j++)
 
2295
    {
 
2296
      rollup.getFields()[i].push_back(rollup.getNullItems()[i]);
 
2297
    }
 
2298
  }
 
2299
 
 
2300
  List<Item>::iterator it(all_fields.begin());
2103
2301
  Item *item;
2104
2302
  while ((item= it++))
2105
2303
  {
2130
2328
          Item* new_item= new Item_func_rollup_const(item);
2131
2329
          if (!new_item)
2132
2330
            return 1;
2133
 
          new_item->fix_fields(session, (Item **) 0);
2134
 
          session->change_item_tree(it.ref(), new_item);
 
2331
          new_item->fix_fields(session, NULL);
 
2332
          *it.ref()= new_item;
2135
2333
          for (Order *tmp= group_tmp; tmp; tmp= tmp->next)
2136
2334
          {
2137
2335
            if (*tmp->item == item)
2138
 
              session->change_item_tree(tmp->item, new_item);
 
2336
              *tmp->item= new_item;
2139
2337
          }
2140
2338
        }
2141
2339
      }
2174
2372
*/
2175
2373
bool Join::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields, Item_sum ***func)
2176
2374
{
2177
 
  List_iterator_fast<Item> it(fields_arg);
2178
 
  Item *first_field= sel_fields.head();
 
2375
  List<Item>::iterator it(fields_arg.begin());
 
2376
  Item *first_field= &sel_fields.front();
2179
2377
  uint32_t level;
2180
2378
 
2181
2379
  /*
2205
2403
    uint32_t pos= send_group_parts - level -1;
2206
2404
    bool real_fields= 0;
2207
2405
    Item *item;
2208
 
    List_iterator<Item> new_it(rollup.fields[pos]);
2209
 
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
2406
    List<Item>::iterator new_it(rollup.getFields()[pos].begin());
 
2407
    Item **ref_array_start= rollup.getRefPointerArrays()[pos];
2210
2408
    Order *start_group;
2211
2409
 
2212
2410
    /* Point to first hidden field */
2213
 
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
2411
    Item **ref_array= ref_array_start + fields_arg.size()-1;
2214
2412
 
2215
2413
    /* Remember where the sum functions ends for the previous level */
2216
2414
    sum_funcs_end[pos+1]= *func;
2219
2417
    for (i= 0, start_group= group_list ;i++ < pos ;start_group= start_group->next)
2220
2418
    {}
2221
2419
 
2222
 
    it.rewind();
 
2420
    it= fields_arg.begin();
2223
2421
    while ((item= it++))
2224
2422
    {
2225
2423
      if (item == first_field)
2304
2502
*/
2305
2503
int Join::rollup_send_data(uint32_t idx)
2306
2504
{
2307
 
  uint32_t i;
2308
 
  for (i= send_group_parts ; i-- > idx ; )
 
2505
  for (uint32_t i= send_group_parts ; i-- > idx ; )
2309
2506
  {
2310
2507
    /* Get reference pointers to sum functions in place */
2311
 
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
2312
 
     ref_pointer_array_size);
 
2508
    memcpy(ref_pointer_array, rollup.getRefPointerArrays()[i], ref_pointer_array_size);
 
2509
 
2313
2510
    if ((!having || having->val_int()))
2314
2511
    {
2315
 
      if (send_records < unit->select_limit_cnt && do_send_rows &&
2316
 
    result->send_data(rollup.fields[i]))
2317
 
  return 1;
 
2512
      if (send_records < unit->select_limit_cnt && do_send_rows && result->send_data(rollup.getFields()[i]))
 
2513
      {
 
2514
        return 1;
 
2515
      }
2318
2516
      send_records++;
2319
2517
    }
2320
2518
  }
2321
2519
  /* Restore ref_pointer_array */
2322
2520
  set_items_ref_array(current_ref_pointer_array);
 
2521
 
2323
2522
  return 0;
2324
2523
}
2325
2524
 
2344
2543
*/
2345
2544
int Join::rollup_write_data(uint32_t idx, Table *table_arg)
2346
2545
{
2347
 
  uint32_t i;
2348
 
  for (i= send_group_parts ; i-- > idx ; )
 
2546
  for (uint32_t i= send_group_parts ; i-- > idx ; )
2349
2547
  {
2350
2548
    /* Get reference pointers to sum functions in place */
2351
 
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
2549
    memcpy(ref_pointer_array, rollup.getRefPointerArrays()[i],
2352
2550
           ref_pointer_array_size);
2353
2551
    if ((!having || having->val_int()))
2354
2552
    {
2355
2553
      int write_error;
2356
2554
      Item *item;
2357
 
      List_iterator_fast<Item> it(rollup.fields[i]);
 
2555
      List<Item>::iterator it(rollup.getFields()[i].begin());
2358
2556
      while ((item= it++))
2359
2557
      {
2360
2558
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
2370
2568
  }
2371
2569
  /* Restore ref_pointer_array */
2372
2570
  set_items_ref_array(current_ref_pointer_array);
 
2571
 
2373
2572
  return 0;
2374
2573
}
2375
2574
 
2973
3172
*/
2974
3173
static bool make_group_fields(Join *main_join, Join *curr_join)
2975
3174
{
2976
 
  if (main_join->group_fields_cache.elements)
 
3175
  if (main_join->group_fields_cache.size())
2977
3176
  {
2978
3177
    curr_join->group_fields= main_join->group_fields_cache;
2979
3178
    curr_join->sort_and_group= 1;
3022
3221
        break;
3023
3222
 
3024
3223
      case DECIMAL_RESULT:
3025
 
        key_length+= my_decimal_get_binary_size(group_item->max_length -
 
3224
        key_length+= class_decimal_get_binary_size(group_item->max_length -
3026
3225
                                                (group_item->decimals ? 1 : 0),
3027
3226
                                                group_item->decimals);
3028
3227
        break;
3035
3234
            have STRING_RESULT result type, we increase the length
3036
3235
            by 8 as maximum pack length of such fields.
3037
3236
          */
3038
 
          if (type == DRIZZLE_TYPE_DATE ||
3039
 
              type == DRIZZLE_TYPE_DATETIME ||
3040
 
              type == DRIZZLE_TYPE_TIMESTAMP)
 
3237
          if (field::isDateTime(type))
3041
3238
          {
3042
3239
            key_length+= 8;
3043
3240
          }
3050
3247
            */
3051
3248
            key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
3052
3249
          }
 
3250
 
3053
3251
          break;
3054
3252
        }
3055
3253
 
3083
3281
    for (; group ; group=group->next)
3084
3282
    {
3085
3283
      Cached_item *tmp= new_Cached_item(join->session, *group->item);
3086
 
      if (!tmp || join->group_fields.push_front(tmp))
 
3284
      if (!tmp)
3087
3285
        return true;
 
3286
                        join->group_fields.push_front(tmp);
3088
3287
    }
3089
3288
  }
3090
3289
  join->sort_and_group=1;     /* Mark for do_select */
3208
3407
 
3209
3408
  table_count=join->tables;
3210
3409
  if (!(join->join_tab=join_tab=
3211
 
  (JoinTable*) session->alloc(sizeof(JoinTable)*table_count)))
 
3410
  (JoinTable*) session->getMemRoot()->allocate(sizeof(JoinTable)*table_count)))
3212
3411
    return(true);
3213
3412
 
 
3413
  for (i= 0; i < table_count; i++)
 
3414
    new (join_tab+i) JoinTable();
 
3415
 
3214
3416
  join->full_join=0;
3215
3417
 
3216
3418
  used_tables= OUTER_REF_TABLE_BIT;   // Outer row is already read
3329
3531
    i.e. they have subqueries, unions or call stored procedures.
3330
3532
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
3331
3533
  */
3332
 
  if (join->session->lex->is_single_level_stmt())
 
3534
  if (join->session->lex().is_single_level_stmt())
3333
3535
    join->session->status_var.last_query_cost= join->best_read;
3334
3536
  return(false);
3335
3537
}
3842
4044
        will ensure that this will be used
3843
4045
      */
3844
4046
      best= tmp;
3845
 
      records= rows2double(rnd_records);
 
4047
      records= rnd_records;
3846
4048
      best_key= 0;
3847
4049
      /* range/index_merge/ALL/index access method are "independent", so: */
3848
4050
      best_ref_depends_map= 0;
4363
4565
  */
4364
4566
  if (!join->table_reexec)
4365
4567
  {
4366
 
    if (!(join->table_reexec= (Table**) join->session->alloc(sizeof(Table*))))
 
4568
    if (!(join->table_reexec= (Table**) join->session->getMemRoot()->allocate(sizeof(Table*))))
4367
4569
      return(true);
4368
4570
    if (join->tmp_join)
4369
4571
      join->tmp_join->table_reexec= join->table_reexec;
4371
4573
  if (!join->join_tab_reexec)
4372
4574
  {
4373
4575
    if (!(join->join_tab_reexec=
4374
 
          (JoinTable*) join->session->alloc(sizeof(JoinTable))))
 
4576
          (JoinTable*) join->session->getMemRoot()->allocate(sizeof(JoinTable))))
4375
4577
      return(true);
 
4578
    new (join->join_tab_reexec) JoinTable();
4376
4579
    if (join->tmp_join)
4377
4580
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
4378
4581
  }
4393
4596
  join->row_limit=join->unit->select_limit_cnt;
4394
4597
  join->do_send_rows = (join->row_limit) ? 1 : 0;
4395
4598
 
4396
 
  join_tab->cache.buff=0;                       /* No caching */
4397
4599
  join_tab->table=tmp_table;
4398
4600
  join_tab->select=0;
4399
4601
  join_tab->select_cond=0;
4484
4686
      /* Ignore sj-nests: */
4485
4687
      if (!embedding->on_expr)
4486
4688
        continue;
4487
 
      nested_join_st *nested_join= embedding->getNestedJoin();
 
4689
      NestedJoin *nested_join= embedding->getNestedJoin();
4488
4690
      if (!nested_join->counter_)
4489
4691
      {
4490
4692
        /*
4499
4701
      }
4500
4702
      if (!tab->first_inner)
4501
4703
        tab->first_inner= nested_join->first_nested;
4502
 
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
4704
      if (++nested_join->counter_ < nested_join->join_list.size())
4503
4705
        break;
4504
4706
      /* Table tab is the last inner table for nested join. */
4505
4707
      nested_join->first_nested->last_inner= tab;
4523
4725
      if (join->tables > 1)
4524
4726
        cond->update_used_tables();             // Tablenr may have changed
4525
4727
      if (join->const_tables == join->tables &&
4526
 
          session->lex->current_select->master_unit() ==
4527
 
          &session->lex->unit)          // not upper level SELECT
 
4728
          session->lex().current_select->master_unit() ==
 
4729
          &session->lex().unit)         // not upper level SELECT
4528
4730
        join->const_table_map|=RAND_TABLE_BIT;
4529
4731
      {                                         // Check const tables
4530
4732
        COND *const_cond=
4531
4733
          make_cond_for_table(cond,
4532
4734
              join->const_table_map,
4533
 
              (table_map) 0, 1);
 
4735
              0, 1);
4534
4736
        for (JoinTable *tab= join->join_tab+join->const_tables;
4535
4737
            tab < join->join_tab+join->tables ; tab++)
4536
4738
        {
4593
4795
        tab->ref.key= -1;
4594
4796
        tab->ref.key_parts= 0;          // Don't use ref key.
4595
4797
        cur_pos= join->getPosFromOptimalPlan(i);
4596
 
        cur_pos.setFanout(rows2double(tab->quick->records));
 
4798
        cur_pos.setFanout(tab->quick->records);
4597
4799
        /*
4598
4800
           We will use join cache here : prevent sorting of the first
4599
4801
           table only and sort at the end.
4602
4804
          join->full_join= 1;
4603
4805
      }
4604
4806
 
 
4807
      if (join->full_join and not session->lex().current_select->is_cross and not cond)
 
4808
      {
 
4809
        my_error(ER_CARTESIAN_JOIN_ATTEMPTED, MYF(0));
 
4810
        return 1;
 
4811
      }
 
4812
 
4605
4813
      tmp= NULL;
4606
4814
      if (cond)
4607
4815
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
4632
4840
          tab->type == AM_EQ_REF)
4633
4841
      {
4634
4842
        optimizer::SqlSelect *sel= tab->select= ((optimizer::SqlSelect*)
4635
 
            session->memdup((unsigned char*) select,
 
4843
            session->getMemRoot()->duplicate((unsigned char*) select,
4636
4844
              sizeof(*select)));
4637
4845
        if (! sel)
4638
4846
          return 1;                     // End of memory
4770
4978
                                         current_map, 0)))
4771
4979
            {
4772
4980
              tab->cache.select= (optimizer::SqlSelect*)
4773
 
                session->memdup((unsigned char*) sel, sizeof(optimizer::SqlSelect));
 
4981
                session->getMemRoot()->duplicate((unsigned char*) sel, sizeof(optimizer::SqlSelect));
4774
4982
              tab->cache.select->cond= tmp;
4775
4983
              tab->cache.select->read_tables= join->const_table_map;
4776
4984
            }
4906
5114
 
4907
5115
    if (tab->insideout_match_tab)
4908
5116
    {
4909
 
      if (! (tab->insideout_buf= (unsigned char*) join->session->alloc(tab->table->key_info
 
5117
      if (! (tab->insideout_buf= (unsigned char*) join->session->getMemRoot()->allocate(tab->table->key_info
4910
5118
                                                                       [tab->index].
4911
5119
                                                                       key_length)))
4912
5120
        return true;
5088
5296
  {
5089
5297
    if (send_row)
5090
5298
    {
5091
 
      List_iterator_fast<Item> it(fields);
 
5299
      List<Item>::iterator it(fields.begin());
5092
5300
      Item *item;
5093
5301
      while ((item= it++))
5094
5302
        item->no_rows_in_result();
5224
5432
static COND *simplify_joins(Join *join, List<TableList> *join_list, COND *conds, bool top)
5225
5433
{
5226
5434
  TableList *table;
5227
 
  nested_join_st *nested_join;
 
5435
  NestedJoin *nested_join;
5228
5436
  TableList *prev_table= 0;
5229
 
  List_iterator<TableList> li(*join_list);
 
5437
  List<TableList>::iterator li(join_list->begin());
5230
5438
 
5231
5439
  /*
5232
5440
    Try to simplify join operations from join_list.
5362
5570
    Flatten nested joins that can be flattened.
5363
5571
    no ON expression and not a semi-join => can be flattened.
5364
5572
  */
5365
 
  li.rewind();
 
5573
  li= join_list->begin();
5366
5574
  while ((table= li++))
5367
5575
  {
5368
5576
    nested_join= table->getNestedJoin();
5369
5577
    if (nested_join && !table->on_expr)
5370
5578
    {
5371
5579
      TableList *tbl;
5372
 
      List_iterator<TableList> it(nested_join->join_list);
 
5580
      List<TableList>::iterator it(nested_join->join_list.begin());
5373
5581
      while ((tbl= it++))
5374
5582
      {
5375
5583
        tbl->setEmbedding(table->getEmbedding());
5392
5600
 
5393
5601
  /* Calculate how many saved fields there is in list */
5394
5602
  field_count=0;
5395
 
  List_iterator<Item> it(fields);
 
5603
  List<Item>::iterator it(fields.begin());
5396
5604
  Item *item;
5397
5605
  while ((item=it++))
5398
5606
  {
5446
5654
                               bool *hidden_group_fields)
5447
5655
{
5448
5656
  int res;
5449
 
  nesting_map save_allow_sum_func=session->lex->allow_sum_func ;
 
5657
  nesting_map save_allow_sum_func=session->lex().allow_sum_func ;
5450
5658
 
5451
 
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
 
5659
  session->lex().allow_sum_func&= ~(1 << session->lex().current_select->nest_level);
5452
5660
  res= session->setup_conds(tables, conds);
5453
5661
 
5454
 
  session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
 
5662
  session->lex().allow_sum_func|= 1 << session->lex().current_select->nest_level;
5455
5663
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
5456
5664
                          order);
5457
 
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
 
5665
  session->lex().allow_sum_func&= ~(1 << session->lex().current_select->nest_level);
5458
5666
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
5459
5667
                          group, hidden_group_fields);
5460
 
  session->lex->allow_sum_func= save_allow_sum_func;
 
5668
  session->lex().allow_sum_func= save_allow_sum_func;
5461
5669
  return(res);
5462
5670
}
5463
5671
 
5497
5705
 
5498
5706
  table_count= join->tables;
5499
5707
  stat= (JoinTable*) join->session->calloc(sizeof(JoinTable)*table_count);
5500
 
  stat_ref= (JoinTable**) join->session->alloc(sizeof(JoinTable*)*MAX_TABLES);
5501
 
  table_vector= (Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
 
5708
  stat_ref= (JoinTable**) join->session->getMemRoot()->allocate(sizeof(JoinTable*)*MAX_TABLES);
 
5709
  table_vector= (Table**) join->session->getMemRoot()->allocate(sizeof(Table*)*(table_count*2));
5502
5710
  if (! stat || ! stat_ref || ! table_vector)
5503
5711
    return 1;
5504
5712
 
5547
5755
      if (!table->cursor->stats.records && !embedding)
5548
5756
      {                                         // Empty table
5549
5757
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
5550
 
        set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5758
        set_position(join, const_count++, s, NULL);
5551
5759
        continue;
5552
5760
      }
5553
5761
      outer_join|= table->map;
5562
5770
      s->embedding_map.reset();
5563
5771
      do
5564
5772
      {
5565
 
        nested_join_st *nested_join= embedding->getNestedJoin();
 
5773
        NestedJoin *nested_join= embedding->getNestedJoin();
5566
5774
        s->embedding_map|= nested_join->nj_map;
5567
5775
        s->dependent|= embedding->getDepTables();
5568
5776
        embedding= embedding->getEmbedding();
5575
5783
              (table->cursor->getEngine()->check_flag(HTON_BIT_STATS_RECORDS_IS_EXACT)) &&
5576
5784
        !join->no_const_tables)
5577
5785
    {
5578
 
      set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5786
      set_position(join, const_count++, s, NULL);
5579
5787
    }
5580
5788
  }
5581
5789
  stat_vector[i]=0;
5591
5799
       As we use bitmaps to represent the relation the complexity
5592
5800
       of the algorithm is O((number of tables)^2).
5593
5801
    */
5594
 
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
5802
    for (i= 0; i < table_count; i++)
5595
5803
    {
5596
 
      for (uint32_t j= 0 ; j < table_count ; j++)
 
5804
      uint32_t j;
 
5805
      table= stat[i].table;
 
5806
 
 
5807
      if (!table->reginfo.join_tab->dependent)
 
5808
        continue;
 
5809
 
 
5810
      for (j= 0, s= stat; j < table_count; j++, s++)
5597
5811
      {
5598
 
        table= stat[j].table;
5599
5812
        if (s->dependent & table->map)
 
5813
        {
 
5814
          table_map was_dependent= s->dependent;
5600
5815
          s->dependent |= table->reginfo.join_tab->dependent;
 
5816
          if (i > j && s->dependent != was_dependent)
 
5817
          {
 
5818
            i= j= 1;
 
5819
            break;
 
5820
          }
 
5821
        }
5601
5822
      }
5602
 
      if (s->dependent)
5603
 
        s->table->maybe_null= 1;
5604
5823
    }
5605
5824
    /* Catch illegal cross references for outer joins */
5606
5825
    for (i= 0, s= stat ; i < table_count ; i++, s++)
5611
5830
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
5612
5831
        return 1;
5613
5832
      }
 
5833
      if (outer_join & s->table->map)
 
5834
        s->table->maybe_null= 1;
 
5835
 
5614
5836
      s->key_dependent= s->dependent;
5615
5837
    }
5616
5838
  }
5632
5854
    s= p_pos->getJoinTable();
5633
5855
    s->type= AM_SYSTEM;
5634
5856
    join->const_table_map|=s->table->map;
5635
 
    if ((tmp= join_read_const_table(s, p_pos)))
 
5857
    if ((tmp= s->joinReadConstTable(p_pos)))
5636
5858
    {
5637
5859
      if (tmp > 0)
5638
5860
        return 1;                       // Fatal error
5685
5907
            table->mark_as_null_row();
5686
5908
            found_const_table_map|= table->map;
5687
5909
            join->const_table_map|= table->map;
5688
 
            set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5910
            set_position(join, const_count++, s, NULL);
5689
5911
            goto more_const_tables_found;
5690
5912
           }
5691
5913
          keyuse++;
5704
5926
          int tmp= 0;
5705
5927
          s->type= AM_SYSTEM;
5706
5928
          join->const_table_map|=table->map;
5707
 
          set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5929
          set_position(join, const_count++, s, NULL);
5708
5930
          partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5709
 
          if ((tmp= join_read_const_table(s, partial_pos)))
 
5931
          if ((tmp= s->joinReadConstTable(partial_pos)))
5710
5932
          {
5711
5933
            if (tmp > 0)
5712
5934
              return 1;                 // Fatal error
5758
5980
                if (create_ref_for_key(join, s, start_keyuse, found_const_table_map))
5759
5981
                  return 1;
5760
5982
                partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5761
 
                if ((tmp=join_read_const_table(s, partial_pos)))
 
5983
                if ((tmp=s->joinReadConstTable(partial_pos)))
5762
5984
                {
5763
5985
                  if (tmp > 0)
5764
5986
                    return 1;                   // Fatal error
5784
6006
  */
5785
6007
  if (const_count && ! sargables.empty())
5786
6008
  {
5787
 
    vector<optimizer::SargableParam>::iterator iter= sargables.begin();
5788
 
    while (iter != sargables.end())
 
6009
    BOOST_FOREACH(vector<optimizer::SargableParam>::reference iter, sargables)
5789
6010
    {
5790
 
      Field *field= (*iter).getField();
5791
 
      JoinTable *join_tab= field->getTable()->reginfo.join_tab;
5792
 
      key_map possible_keys= field->key_start;
5793
 
      possible_keys&= field->getTable()->keys_in_use_for_query;
 
6011
      Field& field= *iter.getField();
 
6012
      JoinTable *join_tab= field.getTable()->reginfo.join_tab;
 
6013
      key_map possible_keys= field.key_start & field.getTable()->keys_in_use_for_query;
5794
6014
      bool is_const= true;
5795
 
      for (uint32_t j= 0; j < (*iter).getNumValues(); j++)
5796
 
        is_const&= (*iter).isConstItem(j);
 
6015
      for (uint32_t j= 0; j < iter.getNumValues(); j++)
 
6016
        is_const&= iter.isConstItem(j);
5797
6017
      if (is_const)
5798
6018
        join_tab[0].const_keys|= possible_keys;
5799
 
      ++iter;
5800
6019
    }
5801
6020
  }
5802
6021
 
5842
6061
      s->quick=select->quick;
5843
6062
      s->needed_reg=select->needed_reg;
5844
6063
      select->quick=0;
 
6064
 
5845
6065
      if (records == 0 && s->table->reginfo.impossible_range)
5846
6066
      {
5847
6067
        /*
5851
6071
          caller to abort with a zero row result.
5852
6072
        */
5853
6073
        join->const_table_map|= s->table->map;
5854
 
        set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
6074
        set_position(join, const_count++, s, NULL);
5855
6075
        s->type= AM_CONST;
5856
6076
        if (*s->on_expr_ref)
5857
6077
        {
5919
6139
*/
5920
6140
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused)
5921
6141
{
5922
 
  List_iterator<TableList> li(*join_list);
 
6142
  List<TableList>::iterator li(join_list->begin());
5923
6143
  TableList *table;
5924
6144
  while ((table= li++))
5925
6145
  {
5926
 
    nested_join_st *nested_join;
 
6146
    NestedJoin *nested_join;
5927
6147
    if ((nested_join= table->getNestedJoin()))
5928
6148
    {
5929
6149
      /*
5937
6157
            with anything)
5938
6158
        2. we could run out of bits in the nested join bitset otherwise.
5939
6159
      */
5940
 
      if (nested_join->join_list.elements != 1)
 
6160
      if (nested_join->join_list.size() != 1)
5941
6161
      {
5942
6162
        /* Don't assign bits to sj-nests */
5943
6163
        if (table->on_expr)
5957
6177
*/
5958
6178
static Table *get_sort_by_table(Order *a, Order *b,TableList *tables)
5959
6179
{
5960
 
  table_map map= (table_map) 0;
 
6180
  table_map map= 0;
5961
6181
 
5962
6182
  if (!a)
5963
6183
    a= b;                                       // Only one need to be given
5980
6200
}
5981
6201
 
5982
6202
/**
5983
 
  Set nested_join_st::counter=0 in all nested joins in passed list.
 
6203
  Set NestedJoin::counter=0 in all nested joins in passed list.
5984
6204
 
5985
 
    Recursively set nested_join_st::counter=0 for all nested joins contained in
 
6205
    Recursively set NestedJoin::counter=0 for all nested joins contained in
5986
6206
    the passed join_list.
5987
6207
 
5988
6208
  @param join_list  List of nested joins to process. It may also contain base
5990
6210
*/
5991
6211
static void reset_nj_counters(List<TableList> *join_list)
5992
6212
{
5993
 
  List_iterator<TableList> li(*join_list);
 
6213
  List<TableList>::iterator li(join_list->begin());
5994
6214
  TableList *table;
5995
6215
  while ((table= li++))
5996
6216
  {
5997
 
    nested_join_st *nested_join;
 
6217
    NestedJoin *nested_join;
5998
6218
    if ((nested_join= table->getNestedJoin()))
5999
6219
    {
6000
6220
      nested_join->counter_= 0;
6080
6300
  Join *join= last->join;
6081
6301
  for (;last_emb != NULL; last_emb= last_emb->getEmbedding())
6082
6302
  {
6083
 
    nested_join_st *nest= last_emb->getNestedJoin();
 
6303
    NestedJoin *nest= last_emb->getNestedJoin();
6084
6304
    
6085
6305
    bool was_fully_covered= nest->is_fully_covered();
6086
6306
    
6105
6325
 
6106
6326
  Item_cond_and *cond=new Item_cond_and();
6107
6327
  Table *table=join_tab->table;
6108
 
  int error;
6109
6328
  if (!cond)
6110
6329
    return(true);
6111
6330
 
6120
6339
 
6121
6340
  if (!cond->fixed)
6122
6341
    cond->fix_fields(session, (Item**)&cond);
 
6342
  int error = 0;
6123
6343
  if (join_tab->select)
6124
6344
  {
6125
 
    error=(int) cond->add(join_tab->select->cond);
 
6345
    cond->add(join_tab->select->cond);
6126
6346
    join_tab->select_cond=join_tab->select->cond=cond;
6127
6347
  }
6128
 
  else if ((join_tab->select= optimizer::make_select(join_tab->table, 0, 0, cond, 0,
6129
 
                                                     &error)))
 
6348
  else if ((join_tab->select= optimizer::make_select(join_tab->table, 0, 0, cond, 0, &error)))
6130
6349
    join_tab->select_cond=cond;
6131
6350
 
6132
 
  return(error ? true : false);
 
6351
  return error;
6133
6352
}
6134
6353
 
6135
6354
static void free_blobs(Field **ptr)