~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join.cc

  • Committer: Lee Bieber
  • Date: 2011-03-29 22:31:41 UTC
  • mfrom: (2257.1.3 build)
  • Revision ID: kalebral@gmail.com-20110329223141-yxc22h3l2he58sk0
Merge Andrew - 743842: Build failure using GCC 4.6
Merge Stewart - 738022: CachedDirectory silently fails to add entries if stat() fails
Merge Olaf - Common fwd: add copyright, add more declaration

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