~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join.cc

  • Committer: Monty Taylor
  • Date: 2010-08-12 20:27:32 UTC
  • mto: (1720.1.5 build)
  • mto: This revision was merged to the branch mainline in revision 1722.
  • Revision ID: mordred@inaugust.com-20100812202732-9kzchbkvkyki4n3u
Merged libdrizzle directly into tree.

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, Inc.
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems
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
 
#include <drizzled/plugin/storage_engine.h>
62
 
#include <drizzled/session.h>
63
 
#include <drizzled/select_result.h>
64
 
 
65
 
#include <drizzled/debug.h>
 
35
#include "drizzled/item/cache.h"
 
36
#include "drizzled/item/cmpfunc.h"
 
37
#include "drizzled/item/copy_string.h"
 
38
#include "drizzled/item/uint.h"
 
39
#include "drizzled/cached_item.h"
 
40
#include "drizzled/sql_base.h"
 
41
#include "drizzled/sql_select.h" /* include join.h */
 
42
#include "drizzled/lock.h"
 
43
#include "drizzled/nested_join.h"
 
44
#include "drizzled/join.h"
 
45
#include "drizzled/join_cache.h"
 
46
#include "drizzled/show.h"
 
47
#include "drizzled/field/blob.h"
 
48
#include "drizzled/optimizer/position.h"
 
49
#include "drizzled/optimizer/sargable_param.h"
 
50
#include "drizzled/optimizer/key_use.h"
 
51
#include "drizzled/optimizer/range.h"
 
52
#include "drizzled/optimizer/sum.h"
 
53
#include "drizzled/optimizer/explain_plan.h"
 
54
#include "drizzled/optimizer/access_method_factory.h"
 
55
#include "drizzled/optimizer/access_method.h"
 
56
#include "drizzled/records.h"
 
57
#include "drizzled/probes.h"
 
58
#include "drizzled/internal/my_bit.h"
 
59
#include "drizzled/internal/my_sys.h"
 
60
#include "drizzled/internal/iocache.h"
66
61
 
67
62
#include <algorithm>
68
63
 
70
65
 
71
66
namespace drizzled
72
67
{
 
68
 
73
69
extern plugin::StorageEngine *heap_engine;
 
70
extern std::bitset<12> test_flags;
74
71
 
75
72
/** Declarations of static functions used in this source file. */
76
73
static bool make_group_fields(Join *main_join, Join *curr_join);
77
 
static void calc_group_buffer(Join *join, Order *group);
78
 
static bool alloc_group_fields(Join *join, Order *group);
 
74
static void calc_group_buffer(Join *join,order_st *group);
 
75
static bool alloc_group_fields(Join *join,order_st *group);
79
76
static uint32_t cache_record_length(Join *join, uint32_t index);
80
77
static double prev_record_reads(Join *join, uint32_t idx, table_map found_ref);
81
78
static bool get_best_combination(Join *join);
105
102
static bool make_join_select(Join *join, optimizer::SqlSelect *select,COND *item);
106
103
static bool make_join_readinfo(Join *join);
107
104
static void update_depend_map(Join *join);
108
 
static void update_depend_map(Join *join, Order *order);
109
 
static Order *remove_constants(Join *join,Order *first_order,COND *cond, bool change_list, bool *simple_order);
 
105
static void update_depend_map(Join *join, order_st *order);
 
106
static order_st *remove_constants(Join *join,order_st *first_order,COND *cond, bool change_list, bool *simple_order);
110
107
static int return_zero_rows(Join *join,
111
108
                            select_result *res,
112
109
                            TableList *tables,
124
121
                               List<Item> &fields,
125
122
                               List<Item> &all_fields,
126
123
                               COND **conds,
127
 
                               Order *order,
128
 
                               Order *group,
 
124
                               order_st *order,
 
125
                               order_st *group,
129
126
                               bool *hidden_group_fields);
130
127
static bool make_join_statistics(Join *join, TableList *leaves, COND *conds, DYNAMIC_ARRAY *keyuse);
131
128
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused);
132
 
static Table *get_sort_by_table(Order *a, Order *b,TableList *tables);
 
129
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
133
130
static void reset_nj_counters(List<TableList> *join_list);
134
 
static bool test_if_subpart(Order *a,Order *b);
 
131
static bool test_if_subpart(order_st *a,order_st *b);
135
132
static void restore_prev_nj_state(JoinTable *last);
136
133
static bool add_ref_to_table_cond(Session *session, JoinTable *join_tab);
137
134
static void free_blobs(Field **ptr); /* Rename this method...conflicts with another in global namespace... */
138
135
 
139
 
Join::Join(Session *session_arg, 
140
 
           List<Item> &fields_arg, 
141
 
           uint64_t select_options_arg,
142
 
           select_result *result_arg) :
143
 
  join_tab(NULL),
144
 
  best_ref(NULL),
145
 
  map2table(NULL),
146
 
  join_tab_save(NULL),
147
 
  table(NULL),
148
 
  all_tables(NULL),
149
 
  sort_by_table(NULL),
150
 
  tables(0),
151
 
  outer_tables(0),
152
 
  const_tables(0),
153
 
  send_group_parts(0),
154
 
  sort_and_group(false),
155
 
  first_record(false),
156
 
  full_join(false),
157
 
  group(false),
158
 
  no_field_update(false),
159
 
  do_send_rows(true),
160
 
  resume_nested_loop(false),
161
 
  no_const_tables(false),
162
 
  select_distinct(false),
163
 
  group_optimized_away(false),
164
 
  simple_order(false),
165
 
  simple_group(false),
166
 
  no_order(false),
167
 
  skip_sort_order(false),
168
 
  union_part(false),
169
 
  optimized(false),
170
 
  need_tmp(false),
171
 
  hidden_group_fields(false),
172
 
  const_table_map(0),
173
 
  found_const_table_map(0),
174
 
  outer_join(0),
175
 
  send_records(0),
176
 
  found_records(0),
177
 
  examined_rows(0),
178
 
  row_limit(0),
179
 
  select_limit(0),
180
 
  fetch_limit(HA_POS_ERROR),
181
 
  session(session_arg),
182
 
  fields_list(fields_arg), 
183
 
  join_list(NULL),
184
 
  unit(NULL),
185
 
  select_lex(NULL),
186
 
  select(NULL),
187
 
  exec_tmp_table1(NULL),
188
 
  exec_tmp_table2(NULL),
189
 
  sum_funcs(NULL),
190
 
  sum_funcs2(NULL),
191
 
  having(NULL),
192
 
  tmp_having(NULL),
193
 
  having_history(NULL),
194
 
  select_options(select_options_arg),
195
 
  result(result_arg),
196
 
  lock(session_arg->lock),
197
 
  tmp_join(NULL),
198
 
  all_fields(fields_arg),
199
 
  error(0),
200
 
  cond_equal(NULL),
201
 
  return_tab(NULL),
202
 
  ref_pointer_array(NULL),
203
 
  items0(NULL),
204
 
  items1(NULL),
205
 
  items2(NULL),
206
 
  items3(NULL),
207
 
  ref_pointer_array_size(0),
208
 
  zero_result_cause(NULL),
209
 
  sortorder(NULL),
210
 
  table_reexec(NULL),
211
 
  join_tab_reexec(NULL)
212
 
{
213
 
  select_distinct= test(select_options & SELECT_DISTINCT);
214
 
  if (&fields_list != &fields_arg) /* only copy if not same*/
215
 
    fields_list= fields_arg;
216
 
  memset(&keyuse, 0, sizeof(keyuse));
217
 
  tmp_table_param.init();
218
 
  tmp_table_param.end_write_records= HA_POS_ERROR;
219
 
  rollup.setState(Rollup::STATE_NONE);
220
 
}
221
 
 
222
 
  /** 
223
 
   * This method is currently only used when a subselect EXPLAIN is performed.
224
 
   * I pulled out the init() method and have simply reset the values to what
225
 
   * was previously in the init() method.  See the note about the hack in 
226
 
   * sql_union.cc...
227
 
   */
228
 
void Join::reset(Session *session_arg, 
229
 
                 List<Item> &fields_arg, 
230
 
                 uint64_t select_options_arg,
231
 
                 select_result *result_arg)
232
 
{
233
 
  join_tab= NULL;
234
 
  best_ref= NULL;
235
 
  map2table= NULL;
236
 
  join_tab_save= NULL;
237
 
  table= NULL;
238
 
  all_tables= NULL;
239
 
  sort_by_table= NULL;
240
 
  tables= 0;
241
 
  outer_tables= 0;
242
 
  const_tables= 0;
243
 
  send_group_parts= 0;
244
 
  sort_and_group= false;
245
 
  first_record= false;
246
 
  full_join= false;
247
 
  group= false;
248
 
  no_field_update= false;
249
 
  do_send_rows= true;
250
 
  resume_nested_loop= false;
251
 
  no_const_tables= false;
252
 
  select_distinct= false;
253
 
  group_optimized_away= false;
254
 
  simple_order= false;
255
 
  simple_group= false;
256
 
  no_order= false;
257
 
  skip_sort_order= false;
258
 
  union_part= false;
259
 
  optimized= false;
260
 
  need_tmp= false;
261
 
  hidden_group_fields= false;
262
 
  const_table_map= 0;
263
 
  found_const_table_map= 0;
264
 
  outer_join= 0;
265
 
  send_records= 0;
266
 
  found_records= 0;
267
 
  examined_rows= 0;
268
 
  row_limit= 0;
269
 
  select_limit= 0;
270
 
  fetch_limit= HA_POS_ERROR;
271
 
  session= session_arg;
272
 
  fields_list= fields_arg; 
273
 
  join_list= NULL;
274
 
  unit= NULL;
275
 
  select_lex= NULL;
276
 
  select= NULL;
277
 
  exec_tmp_table1= NULL;
278
 
  exec_tmp_table2= NULL;
279
 
  sum_funcs= NULL;
280
 
  sum_funcs2= NULL;
281
 
  having= NULL;
282
 
  tmp_having= NULL;
283
 
  having_history= NULL;
284
 
  select_options= select_options_arg;
285
 
  result= result_arg;
286
 
  lock= session_arg->lock;
287
 
  tmp_join= NULL;
288
 
  all_fields= fields_arg;
289
 
  error= 0;
290
 
  cond_equal= NULL;
291
 
  return_tab= NULL;
292
 
  ref_pointer_array= NULL;
293
 
  items0= NULL;
294
 
  items1= NULL;
295
 
  items2= NULL;
296
 
  items3= NULL;
297
 
  ref_pointer_array_size= 0;
298
 
  zero_result_cause= NULL;
299
 
  sortorder= NULL;
300
 
  table_reexec= NULL;
301
 
  join_tab_reexec= NULL;
302
 
  select_distinct= test(select_options & SELECT_DISTINCT);
303
 
  if (&fields_list != &fields_arg) /* only copy if not same*/
304
 
    fields_list= fields_arg;
305
 
  memset(&keyuse, 0, sizeof(keyuse));
306
 
  tmp_table_param.init();
307
 
  tmp_table_param.end_write_records= HA_POS_ERROR;
308
 
  rollup.setState(Rollup::STATE_NONE);
309
 
}
310
 
 
311
 
bool Join::is_top_level_join() const
312
 
{
313
 
  return (unit == &session->getLex()->unit && (unit->fake_select_lex == 0 ||
314
 
                                          select_lex == unit->fake_select_lex));
315
 
}
316
 
 
317
136
/**
318
137
  Prepare of whole select (including sub queries in future).
319
138
 
331
150
                  uint32_t wild_num,
332
151
                  COND *conds_init,
333
152
                  uint32_t og_num,
334
 
                  Order *order_init,
335
 
                  Order *group_init,
 
153
                  order_st *order_init,
 
154
                  order_st *group_init,
336
155
                  Item *having_init,
337
156
                  Select_Lex *select_lex_arg,
338
157
                  Select_Lex_Unit *unit_arg)
351
170
  join_list= &select_lex->top_join_list;
352
171
  union_part= unit_arg->is_union();
353
172
 
354
 
  session->getLex()->current_select->is_item_list_lookup= 1;
 
173
  session->lex->current_select->is_item_list_lookup= 1;
355
174
  /*
356
175
    If we have already executed SELECT, then it have not sense to prevent
357
176
    its table from update (see unique_table())
373
192
  for (table_ptr= select_lex->leaf_tables;
374
193
       table_ptr;
375
194
       table_ptr= table_ptr->next_leaf)
376
 
  {
377
195
    tables++;
378
 
  }
379
 
 
380
196
 
381
197
  if (setup_wild(session, fields_list, &all_fields, wild_num) ||
382
198
      select_lex->setup_ref_array(session, og_num) ||
392
208
 
393
209
  if (having)
394
210
  {
395
 
    nesting_map save_allow_sum_func= session->getLex()->allow_sum_func;
396
 
    session->setWhere("having clause");
397
 
    session->getLex()->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
211
    nesting_map save_allow_sum_func= session->lex->allow_sum_func;
 
212
    session->where="having clause";
 
213
    session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
398
214
    select_lex->having_fix_field= 1;
399
215
    bool having_fix_rc= (!having->fixed &&
400
216
       (having->fix_fields(session, &having) ||
402
218
    select_lex->having_fix_field= 0;
403
219
    if (having_fix_rc || session->is_error())
404
220
      return(-1);
405
 
    session->getLex()->allow_sum_func= save_allow_sum_func;
 
221
    session->lex->allow_sum_func= save_allow_sum_func;
406
222
  }
407
223
 
408
224
  {
452
268
            in_subs  &&                                                   // 1
453
269
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
454
270
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
455
 
            session->getLex()->sql_command == SQLCOM_SELECT)                       // *
 
271
            session->lex->sql_command == SQLCOM_SELECT)                       // *
456
272
        {
457
273
          if (in_subs->is_top_level_item() &&                             // 4
458
274
              !in_subs->is_correlated &&                                  // 5
472
288
 
473
289
  if (order)
474
290
  {
475
 
    Order *ord;
 
291
    order_st *ord;
476
292
    for (ord= order; ord; ord= ord->next)
477
293
    {
478
294
      Item *item= *ord->item;
517
333
  {
518
334
    /* Caclulate the number of groups */
519
335
    send_group_parts= 0;
520
 
    for (Order *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
336
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
521
337
      send_group_parts++;
522
338
  }
523
339
 
524
340
  if (error)
525
 
    return(-1);
 
341
    goto err;
526
342
 
527
343
  /* 
528
344
   * The below will create the new table for
531
347
   * @see create_table_from_items() in drizzled/sql_insert.cc
532
348
   */
533
349
  if (result && result->prepare(fields_list, unit_arg))
534
 
    return(-1);
 
350
    goto err;
535
351
 
536
352
  /* Init join struct */
537
353
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
543
359
  if (sum_func_count && !group_list && (func_count || field_count))
544
360
  {
545
361
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
546
 
    return(-1);
 
362
    goto err;
547
363
  }
548
364
#endif
549
365
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
550
 
    return(-1);
551
 
 
 
366
    goto err;
552
367
  if (alloc_func_list())
553
 
    return(-1);
554
 
 
555
 
  return 0; // All OK
 
368
    goto err;
 
369
 
 
370
  return(0); // All OK
 
371
 
 
372
err:
 
373
  return(-1);
556
374
}
557
375
 
558
376
/*
625
443
    select_limit= HA_POS_ERROR;
626
444
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
627
445
  // Ignore errors of execution if option IGNORE present
628
 
  if (session->getLex()->ignore)
629
 
    session->getLex()->current_select->no_error= 1;
 
446
  if (session->lex->ignore)
 
447
    session->lex->current_select->no_error= 1;
630
448
 
631
449
#ifdef HAVE_REF_TO_FIELDS     // Not done yet
632
450
  /* Add HAVING to WHERE if possible */
726
544
        conjunctions.
727
545
        Preserve conditions for EXPLAIN.
728
546
      */
729
 
      if (conds && !(session->getLex()->describe & DESCRIBE_EXTENDED))
 
547
      if (conds && !(session->lex->describe & DESCRIBE_EXTENDED))
730
548
      {
731
549
        COND *table_independent_conds= make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
732
550
        conds= table_independent_conds;
761
579
      !(select_options & SELECT_DESCRIBE) &&
762
580
      (!conds ||
763
581
       !(conds->used_tables() & RAND_TABLE_BIT) ||
764
 
       select_lex->master_unit() == &session->getLex()->unit)) // upper level SELECT
 
582
       select_lex->master_unit() == &session->lex->unit)) // upper level SELECT
765
583
  {
766
584
    zero_result_cause= "no matching row in const table";
767
585
    goto setup_subq_exit;
775
593
    return 1;
776
594
  }
777
595
  if (const_tables && !(select_options & SELECT_NO_UNLOCK))
778
 
    session->unlockSomeTables(table, const_tables);
 
596
    mysql_unlock_some_tables(session, table, const_tables);
779
597
  if (!conds && outer_join)
780
598
  {
781
599
    /* Handle the case where we have an OUTER JOIN without a WHERE */
821
639
 
822
640
  if (conds &&!outer_join && const_table_map != found_const_table_map &&
823
641
      (select_options & SELECT_DESCRIBE) &&
824
 
      select_lex->master_unit() == &session->getLex()->unit) // upper level SELECT
 
642
      select_lex->master_unit() == &session->lex->unit) // upper level SELECT
825
643
  {
826
644
    conds=new Item_int((int64_t) 0,1);  // Always false
827
645
  }
837
655
 
838
656
  /* Optimize distinct away if possible */
839
657
  {
840
 
    Order *org_order= order;
 
658
    order_st *org_order= order;
841
659
    order= remove_constants(this, order,conds,1, &simple_order);
842
660
    if (session->is_error())
843
661
    {
909
727
  }
910
728
  if (group_list || tmp_table_param.sum_func_count)
911
729
  {
912
 
    if (! hidden_group_fields && rollup.getState() == Rollup::STATE_NONE)
 
730
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
913
731
      select_distinct=0;
914
732
  }
915
733
  else if (select_distinct && tables - const_tables == 1)
971
789
  }
972
790
  simple_group= 0;
973
791
  {
974
 
    Order *old_group_list;
 
792
    order_st *old_group_list;
975
793
    group_list= remove_constants(this, (old_group_list= group_list), conds,
976
 
                                 rollup.getState() == Rollup::STATE_NONE,
 
794
                                 rollup.state == ROLLUP::STATE_NONE,
977
795
                                 &simple_group);
978
796
    if (session->is_error())
979
797
    {
1049
867
        save_index_subquery_explain_info(join_tab, where);
1050
868
        join_tab[0].type= AM_UNIQUE_SUBQUERY;
1051
869
        error= 0;
1052
 
        return(unit->item->change_engine(new subselect_uniquesubquery_engine(session, join_tab, unit->item, where)));
 
870
        return(unit->item->
 
871
                    change_engine(new
 
872
                                  subselect_uniquesubquery_engine(session,
 
873
                                                                  join_tab,
 
874
                                                                  unit->item,
 
875
                                                                  where)));
1053
876
      }
1054
877
      else if (join_tab[0].type == AM_REF &&
1055
878
         join_tab[0].ref.items[0]->name == in_left_expr_name)
1058
881
        save_index_subquery_explain_info(join_tab, where);
1059
882
        join_tab[0].type= AM_INDEX_SUBQUERY;
1060
883
        error= 0;
1061
 
        return(unit->item->change_engine(new subselect_indexsubquery_engine(session, join_tab, unit->item, where, NULL, 0)));
 
884
        return(unit->item->
 
885
                    change_engine(new
 
886
                                  subselect_indexsubquery_engine(session,
 
887
                                                                 join_tab,
 
888
                                                                 unit->item,
 
889
                                                                 where,
 
890
                                                                 NULL,
 
891
                                                                 0)));
1062
892
      }
1063
893
    } 
1064
894
    else if (join_tab[0].type == AM_REF_OR_NULL &&
1069
899
      error= 0;
1070
900
      conds= remove_additional_cond(conds);
1071
901
      save_index_subquery_explain_info(join_tab, conds);
1072
 
      return(unit->item->change_engine(new subselect_indexsubquery_engine(session, join_tab, unit->item, conds, having, 1)));
 
902
      return(unit->item->
 
903
      change_engine(new subselect_indexsubquery_engine(session,
 
904
                   join_tab,
 
905
                   unit->item,
 
906
                   conds,
 
907
                                                                   having,
 
908
                   1)));
1073
909
    }
1074
910
 
1075
911
  }
1121
957
        Force using of tmp table if sorting by a SP or UDF function due to
1122
958
        their expensive and probably non-deterministic nature.
1123
959
      */
1124
 
      for (Order *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
960
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1125
961
      {
1126
962
        Item *item= *tmp_order->item;
1127
963
        if (item->is_expensive())
1165
1001
 
1166
1002
    tmp_table_param.hidden_field_count= (all_fields.elements -
1167
1003
           fields_list.elements);
1168
 
    Order *tmp_group= (((not simple_group) or not (getDebug().test(debug::NO_KEY_GROUP))) ? group_list : (Order*) 0);
1169
 
 
 
1004
    order_st *tmp_group= ((!simple_group && 
 
1005
                           ! (test_flags.test(TEST_NO_KEY_GROUP))) ? group_list :
 
1006
                                                                     (order_st*) 0);
1170
1007
    /*
1171
1008
      Pushing LIMIT to the temporary table creation is not applicable
1172
1009
      when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1175
1012
    */
1176
1013
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1177
1014
                             !tmp_group &&
1178
 
                             !session->getLex()->current_select->with_sum_func) ?
 
1015
                             !session->lex->current_select->with_sum_func) ?
1179
1016
                            select_limit : HA_POS_ERROR;
1180
1017
 
1181
1018
    if (!(exec_tmp_table1=
1269
1106
      If this join belongs to an uncacheable subquery save
1270
1107
      the original join
1271
1108
    */
1272
 
    if (select_lex->uncacheable.any() && 
1273
 
        ! is_top_level_join() &&
 
1109
    if (select_lex->uncacheable && !is_top_level_join() &&
1274
1110
        init_save_join_tab())
1275
 
    {
1276
 
      return -1;
1277
 
    }
 
1111
      return(-1);
1278
1112
  }
1279
1113
 
1280
1114
  error= 0;
1281
 
  return 0;
 
1115
  return(0);
1282
1116
 
1283
1117
setup_subq_exit:
1284
1118
  /* Even with zero matching rows, subqueries in the HAVING clause
1352
1186
*/
1353
1187
bool Join::init_save_join_tab()
1354
1188
{
1355
 
  if (!(tmp_join= (Join*)session->getMemRoot()->allocate(sizeof(Join))))
 
1189
  if (!(tmp_join= (Join*)session->alloc(sizeof(Join))))
1356
1190
    return 1;
1357
 
 
1358
1191
  error= 0;              // Ensure that tmp_join.error= 0
1359
1192
  restore_tmp();
1360
 
 
1361
1193
  return 0;
1362
1194
}
1363
1195
 
1364
1196
bool Join::save_join_tab()
1365
1197
{
1366
 
  if (! join_tab_save && select_lex->master_unit()->uncacheable.any())
 
1198
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
1367
1199
  {
1368
 
    if (!(join_tab_save= (JoinTable*)session->getMemRoot()->duplicate((unsigned char*) join_tab,
 
1200
    if (!(join_tab_save= (JoinTable*)session->memdup((unsigned char*) join_tab,
1369
1201
            sizeof(JoinTable) * tables)))
1370
1202
      return 1;
1371
1203
  }
1613
1445
              exec_tmp_table2= create_tmp_table(session,
1614
1446
                                                &curr_join->tmp_table_param,
1615
1447
                                                *curr_all_fields,
1616
 
                                                (Order*) 0,
 
1448
                                                (order_st*) 0,
1617
1449
                                                curr_join->select_distinct &&
1618
1450
                                                !curr_join->group_list,
1619
1451
                                                1, curr_join->select_options,
1767
1599
      if (sort_table_cond)
1768
1600
      {
1769
1601
        if (!curr_table->select)
1770
 
          if (!(curr_table->select= new optimizer::SqlSelect()))
 
1602
          if (!(curr_table->select= new optimizer::SqlSelect))
1771
1603
            return;
1772
1604
        if (!curr_table->select->cond)
1773
1605
          curr_table->select->cond= sort_table_cond;
1871
1703
    for a derived table which is always materialized.
1872
1704
    Otherwise we would not be able to print the query  correctly.
1873
1705
  */
1874
 
  if (items0 && (session->getLex()->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
 
1706
  if (items0 && (session->lex->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
1875
1707
    set_items_ref_array(items0);
1876
1708
 
1877
1709
  return;
1971
1803
    is called after all rows are sent, but before EOF packet is sent.
1972
1804
 
1973
1805
    For a simple SELECT with no subqueries this function performs a full
1974
 
    cleanup of the Join and calls unlockReadTables to free used base
 
1806
    cleanup of the Join and calls mysql_unlock_read_tables to free used base
1975
1807
    tables.
1976
1808
 
1977
1809
    If a Join is executed for a subquery or if it has a subquery, we can't
2003
1835
    Optimization: if not EXPLAIN and we are done with the Join,
2004
1836
    free all tables.
2005
1837
  */
2006
 
  bool full= (select_lex->uncacheable.none() && ! session->getLex()->describe);
 
1838
  bool full= (!select_lex->uncacheable && !session->lex->describe);
2007
1839
  bool can_unlock= full;
2008
1840
 
2009
1841
  cleanup(full);
2014
1846
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
2015
1847
    {
2016
1848
      Item_subselect *subselect= sl->master_unit()->item;
2017
 
      bool full_local= full && (!subselect || 
2018
 
                                (subselect->is_evaluated() &&
2019
 
                                !subselect->is_uncacheable()));
 
1849
      bool full_local= full && (!subselect || subselect->is_evaluated());
2020
1850
      /*
2021
1851
        If this join is evaluated, we can fully clean it up and clean up all
2022
1852
        its underlying joins even if they are correlated -- they will not be
2038
1868
  if (can_unlock && lock && session->lock &&
2039
1869
      !(select_options & SELECT_NO_UNLOCK) &&
2040
1870
      !select_lex->subquery_in_having &&
2041
 
      (select_lex == (session->getLex()->unit.fake_select_lex ?
2042
 
                      session->getLex()->unit.fake_select_lex : &session->getLex()->select_lex)))
 
1871
      (select_lex == (session->lex->unit.fake_select_lex ?
 
1872
                      session->lex->unit.fake_select_lex : &session->lex->select_lex)))
2043
1873
  {
2044
1874
    /*
2045
1875
      TODO: unlock tables even if the join isn't top level select in the
2046
1876
      tree.
2047
1877
    */
2048
 
    session->unlockReadTables(lock);           // Don't free join->lock
 
1878
    mysql_unlock_read_tables(session, lock);           // Don't free join->lock
2049
1879
    lock= 0;
2050
1880
  }
2051
1881
 
2068
1898
{
2069
1899
  if (table)
2070
1900
  {
 
1901
    JoinTable *tab,*end;
2071
1902
    /*
2072
1903
      Only a sorted table may be cached.  This sorted table is always the
2073
1904
      first non const table in join->table
2077
1908
      table[const_tables]->free_io_cache();
2078
1909
      table[const_tables]->filesort_free_buffers(full);
2079
1910
    }
2080
 
  }
2081
 
 
2082
 
  if (join_tab)
2083
 
  {
2084
 
    JoinTable *tab,*end;
2085
1911
 
2086
1912
    if (full)
2087
1913
    {
2098
1924
      }
2099
1925
    }
2100
1926
  }
2101
 
 
2102
1927
  /*
2103
1928
    We are not using tables anymore
2104
1929
    Unlock all tables. We may be in an INSERT .... SELECT statement.
2112
1937
      We can't call delete_elements() on copy_funcs as this will cause
2113
1938
      problems in free_elements() as some of the elements are then deleted.
2114
1939
    */
2115
 
    tmp_table_param.copy_funcs.clear();
 
1940
    tmp_table_param.copy_funcs.empty();
2116
1941
    /*
2117
1942
      If we have tmp_join and 'this' Join is not tmp_join and
2118
1943
      tmp_table_param.copy_field's  of them are equal then we have to remove
2142
1967
    are not re-calculated.
2143
1968
  */
2144
1969
  for (uint32_t i= join->const_tables; i < join->tables; i++)
2145
 
  {
2146
1970
    join->table[i]->mark_as_null_row();   // All fields are NULL
2147
 
  }
2148
1971
}
2149
1972
 
2150
1973
/**
2165
1988
    If we are using rollup, we need a copy of the summary functions for
2166
1989
    each level
2167
1990
  */
2168
 
  if (rollup.getState() != Rollup::STATE_NONE)
 
1991
  if (rollup.state != ROLLUP::STATE_NONE)
2169
1992
    func_count*= (send_group_parts+1);
2170
1993
 
2171
1994
  group_parts= send_group_parts;
2182
2005
    */
2183
2006
    if (order)
2184
2007
    {
2185
 
      Order *ord;
 
2008
      order_st *ord;
2186
2009
      for (ord= order; ord; ord= ord->next)
2187
2010
        group_parts++;
2188
2011
    }
2213
2036
                              bool before_group_by, 
2214
2037
                              bool recompute)
2215
2038
{
2216
 
  List<Item>::iterator it(field_list.begin());
 
2039
  List_iterator_fast<Item> it(field_list);
2217
2040
  Item_sum **func;
2218
2041
  Item *item;
2219
2042
 
2228
2051
         ((Item_sum *)item)->depended_from() == select_lex))
2229
2052
      *func++= (Item_sum*) item;
2230
2053
  }
2231
 
  if (before_group_by && rollup.getState() == Rollup::STATE_INITED)
 
2054
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
2232
2055
  {
2233
 
    rollup.setState(Rollup::STATE_READY);
 
2056
    rollup.state= ROLLUP::STATE_READY;
2234
2057
    if (rollup_make_fields(field_list, send_fields, &func))
2235
 
      return true;     // Should never happen
 
2058
      return(true);     // Should never happen
2236
2059
  }
2237
 
  else if (rollup.getState() == Rollup::STATE_NONE)
 
2060
  else if (rollup.state == ROLLUP::STATE_NONE)
2238
2061
  {
2239
2062
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
2240
2063
      sum_funcs_end[i]= func;
2241
2064
  }
2242
 
  else if (rollup.getState() == Rollup::STATE_READY)
 
2065
  else if (rollup.state == ROLLUP::STATE_READY)
2243
2066
    return(false);                         // Don't put end marker
2244
2067
  *func=0;          // End marker
2245
2068
  return(false);
2248
2071
/** Allocate memory needed for other rollup functions. */
2249
2072
bool Join::rollup_init()
2250
2073
{
 
2074
  uint32_t i,j;
2251
2075
  Item **ref_array;
2252
2076
 
2253
2077
  tmp_table_param.quick_group= 0; // Can't create groups in tmp table
2254
 
  rollup.setState(Rollup::STATE_INITED);
 
2078
  rollup.state= ROLLUP::STATE_INITED;
2255
2079
 
2256
2080
  /*
2257
2081
    Create pointers to the different sum function groups
2259
2083
  */
2260
2084
  tmp_table_param.group_parts= send_group_parts;
2261
2085
 
2262
 
  rollup.setNullItems((Item_null_result**) session->getMemRoot()->allocate((sizeof(Item*) +
2263
 
                                                                sizeof(Item**) +
2264
 
                                                                sizeof(List<Item>) +
2265
 
                                                                ref_pointer_array_size)
2266
 
                                                               * send_group_parts ));
2267
 
  if (! rollup.getNullItems())
2268
 
  {
 
2086
  if (!(rollup.null_items= (Item_null_result**) session->alloc((sizeof(Item*) +
 
2087
                                                sizeof(Item**) +
 
2088
                                                sizeof(List<Item>) +
 
2089
                        ref_pointer_array_size)
 
2090
                        * send_group_parts )))
2269
2091
    return 1;
2270
 
  }
2271
2092
 
2272
 
  rollup.setFields((List<Item>*) (rollup.getNullItems() + send_group_parts));
2273
 
  rollup.setRefPointerArrays((Item***) (rollup.getFields() + send_group_parts));
2274
 
  ref_array= (Item**) (rollup.getRefPointerArrays()+send_group_parts);
 
2093
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
2094
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
2095
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
2275
2096
 
2276
2097
  /*
2277
2098
    Prepare space for field list for the different levels
2278
2099
    These will be filled up in rollup_make_fields()
2279
2100
  */
2280
 
  for (uint32_t i= 0 ; i < send_group_parts ; i++)
 
2101
  for (i= 0 ; i < send_group_parts ; i++)
2281
2102
  {
2282
 
    rollup.getNullItems()[i]= new (session->mem_root) Item_null_result();
2283
 
    List<Item> *rollup_fields= &rollup.getFields()[i];
2284
 
    rollup_fields->clear();
2285
 
    rollup.getRefPointerArrays()[i]= ref_array;
 
2103
    rollup.null_items[i]= new (session->mem_root) Item_null_result();
 
2104
    List<Item> *rollup_fields= &rollup.fields[i];
 
2105
    rollup_fields->empty();
 
2106
    rollup.ref_pointer_arrays[i]= ref_array;
2286
2107
    ref_array+= all_fields.elements;
2287
2108
  }
2288
 
 
2289
 
  for (uint32_t i= 0 ; i < send_group_parts; i++)
 
2109
  for (i= 0 ; i < send_group_parts; i++)
2290
2110
  {
2291
 
    for (uint32_t j= 0 ; j < fields_list.elements ; j++)
2292
 
    {
2293
 
      rollup.getFields()[i].push_back(rollup.getNullItems()[i]);
2294
 
    }
 
2111
    for (j=0 ; j < fields_list.elements ; j++)
 
2112
      rollup.fields[i].push_back(rollup.null_items[i]);
2295
2113
  }
2296
 
 
2297
 
  List<Item>::iterator it(all_fields.begin());
 
2114
  List_iterator<Item> it(all_fields);
2298
2115
  Item *item;
2299
2116
  while ((item= it++))
2300
2117
  {
2301
 
    Order *group_tmp;
 
2118
    order_st *group_tmp;
2302
2119
    bool found_in_group= 0;
2303
2120
 
2304
2121
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
2327
2144
            return 1;
2328
2145
          new_item->fix_fields(session, (Item **) 0);
2329
2146
          session->change_item_tree(it.ref(), new_item);
2330
 
          for (Order *tmp= group_tmp; tmp; tmp= tmp->next)
 
2147
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
2331
2148
          {
2332
2149
            if (*tmp->item == item)
2333
2150
              session->change_item_tree(tmp->item, new_item);
2369
2186
*/
2370
2187
bool Join::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields, Item_sum ***func)
2371
2188
{
2372
 
  List<Item>::iterator it(fields_arg.begin());
 
2189
  List_iterator_fast<Item> it(fields_arg);
2373
2190
  Item *first_field= sel_fields.head();
2374
2191
  uint32_t level;
2375
2192
 
2400
2217
    uint32_t pos= send_group_parts - level -1;
2401
2218
    bool real_fields= 0;
2402
2219
    Item *item;
2403
 
    List<Item>::iterator new_it(rollup.getFields()[pos].begin());
2404
 
    Item **ref_array_start= rollup.getRefPointerArrays()[pos];
2405
 
    Order *start_group;
 
2220
    List_iterator<Item> new_it(rollup.fields[pos]);
 
2221
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
2222
    order_st *start_group;
2406
2223
 
2407
2224
    /* Point to first hidden field */
2408
2225
    Item **ref_array= ref_array_start + fields_arg.elements-1;
2414
2231
    for (i= 0, start_group= group_list ;i++ < pos ;start_group= start_group->next)
2415
2232
    {}
2416
2233
 
2417
 
    it= fields_arg.begin();
 
2234
    it.rewind();
2418
2235
    while ((item= it++))
2419
2236
    {
2420
2237
      if (item == first_field)
2444
2261
      else
2445
2262
      {
2446
2263
        /* Check if this is something that is part of this group by */
2447
 
        Order *group_tmp;
 
2264
        order_st *group_tmp;
2448
2265
        for (group_tmp= start_group, i= pos ;
2449
2266
                  group_tmp ; group_tmp= group_tmp->next, i++)
2450
2267
        {
2499
2316
*/
2500
2317
int Join::rollup_send_data(uint32_t idx)
2501
2318
{
2502
 
  for (uint32_t i= send_group_parts ; i-- > idx ; )
 
2319
  uint32_t i;
 
2320
  for (i= send_group_parts ; i-- > idx ; )
2503
2321
  {
2504
2322
    /* Get reference pointers to sum functions in place */
2505
 
    memcpy(ref_pointer_array, rollup.getRefPointerArrays()[i], ref_pointer_array_size);
2506
 
 
 
2323
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
2324
     ref_pointer_array_size);
2507
2325
    if ((!having || having->val_int()))
2508
2326
    {
2509
 
      if (send_records < unit->select_limit_cnt && do_send_rows && result->send_data(rollup.getFields()[i]))
2510
 
      {
2511
 
        return 1;
2512
 
      }
 
2327
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
2328
    result->send_data(rollup.fields[i]))
 
2329
  return 1;
2513
2330
      send_records++;
2514
2331
    }
2515
2332
  }
2516
2333
  /* Restore ref_pointer_array */
2517
2334
  set_items_ref_array(current_ref_pointer_array);
2518
 
 
2519
2335
  return 0;
2520
2336
}
2521
2337
 
2540
2356
*/
2541
2357
int Join::rollup_write_data(uint32_t idx, Table *table_arg)
2542
2358
{
2543
 
  for (uint32_t i= send_group_parts ; i-- > idx ; )
 
2359
  uint32_t i;
 
2360
  for (i= send_group_parts ; i-- > idx ; )
2544
2361
  {
2545
2362
    /* Get reference pointers to sum functions in place */
2546
 
    memcpy(ref_pointer_array, rollup.getRefPointerArrays()[i],
 
2363
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
2547
2364
           ref_pointer_array_size);
2548
2365
    if ((!having || having->val_int()))
2549
2366
    {
2550
2367
      int write_error;
2551
2368
      Item *item;
2552
 
      List<Item>::iterator it(rollup.getFields()[i].begin());
 
2369
      List_iterator_fast<Item> it(rollup.fields[i]);
2553
2370
      while ((item= it++))
2554
2371
      {
2555
2372
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
2565
2382
  }
2566
2383
  /* Restore ref_pointer_array */
2567
2384
  set_items_ref_array(current_ref_pointer_array);
2568
 
 
2569
2385
  return 0;
2570
2386
}
2571
2387
 
2661
2477
    return NESTED_LOOP_ERROR;
2662
2478
  if (error < 0)
2663
2479
    return NESTED_LOOP_NO_MORE_ROWS;
2664
 
  if (join->session->getKilled())                       // Aborted by user
 
2480
  if (join->session->killed)                    // Aborted by user
2665
2481
  {
2666
2482
    join->session->send_kill_message();
2667
2483
    return NESTED_LOOP_KILLED;
2873
2689
  info= &join_tab->read_record;
2874
2690
  do
2875
2691
  {
2876
 
    if (join->session->getKilled())
 
2692
    if (join->session->killed)
2877
2693
    {
2878
2694
      join->session->send_kill_message();
2879
2695
      return NESTED_LOOP_KILLED;
3005
2821
{
3006
2822
  Table *table= join->tmp_table;
3007
2823
 
3008
 
  if (join->session->getKilled())                       // Aborted by user
 
2824
  if (join->session->killed)                    // Aborted by user
3009
2825
  {
3010
2826
    join->session->send_kill_message();
3011
2827
    return NESTED_LOOP_KILLED;
3013
2829
  if (!end_of_records)
3014
2830
  {
3015
2831
    copy_fields(&join->tmp_table_param);
3016
 
    if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3017
 
      return NESTED_LOOP_ERROR;
 
2832
    copy_funcs(join->tmp_table_param.items_to_copy);
3018
2833
    if (!join->having || join->having->val_int())
3019
2834
    {
3020
2835
      int error;
3022
2837
      if ((error=table->cursor->insertRecord(table->getInsertRecord())))
3023
2838
      {
3024
2839
        if (!table->cursor->is_fatal_error(error, HA_CHECK_DUP))
3025
 
        {
3026
 
          return NESTED_LOOP_OK;
3027
 
        }
 
2840
          goto end;
3028
2841
 
3029
2842
        my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
3030
2843
        return NESTED_LOOP_ERROR;        // Table is_full error
3039
2852
      }
3040
2853
    }
3041
2854
  }
3042
 
 
 
2855
end:
3043
2856
  return NESTED_LOOP_OK;
3044
2857
}
3045
2858
 
3047
2860
enum_nested_loop_state end_update(Join *join, JoinTable *, bool end_of_records)
3048
2861
{
3049
2862
  Table *table= join->tmp_table;
3050
 
  Order *group;
 
2863
  order_st *group;
3051
2864
  int   error;
3052
2865
 
3053
2866
  if (end_of_records)
3054
2867
    return NESTED_LOOP_OK;
3055
 
  if (join->session->getKilled())                       // Aborted by user
 
2868
  if (join->session->killed)                    // Aborted by user
3056
2869
  {
3057
2870
    join->session->send_kill_message();
3058
2871
    return NESTED_LOOP_KILLED;
3099
2912
      memcpy(table->getInsertRecord()+key_part->offset, group->buff, 1);
3100
2913
  }
3101
2914
  init_tmptable_sum_functions(join->sum_funcs);
3102
 
  if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3103
 
    return NESTED_LOOP_ERROR;
 
2915
  copy_funcs(join->tmp_table_param.items_to_copy);
3104
2916
  if ((error=table->cursor->insertRecord(table->getInsertRecord())))
3105
2917
  {
3106
2918
    my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
3118
2930
 
3119
2931
  if (end_of_records)
3120
2932
    return NESTED_LOOP_OK;
3121
 
  if (join->session->getKilled())                       // Aborted by user
 
2933
  if (join->session->killed)                    // Aborted by user
3122
2934
  {
3123
2935
    join->session->send_kill_message();
3124
2936
    return NESTED_LOOP_KILLED;
3126
2938
 
3127
2939
  init_tmptable_sum_functions(join->sum_funcs);
3128
2940
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
3129
 
  if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3130
 
    return NESTED_LOOP_ERROR;
 
2941
  copy_funcs(join->tmp_table_param.items_to_copy);
3131
2942
 
3132
2943
  if (!(error= table->cursor->insertRecord(table->getInsertRecord())))
3133
2944
    join->send_records++;                       // New group
3186
2997
/**
3187
2998
  calc how big buffer we need for comparing group entries.
3188
2999
*/
3189
 
static void calc_group_buffer(Join *join, Order *group)
 
3000
static void calc_group_buffer(Join *join,order_st *group)
3190
3001
{
3191
3002
  uint32_t key_length=0, parts=0, null_parts=0;
3192
3003
 
3212
3023
      case REAL_RESULT:
3213
3024
        key_length+= sizeof(double);
3214
3025
        break;
3215
 
 
3216
3026
      case INT_RESULT:
3217
3027
        key_length+= sizeof(int64_t);
3218
3028
        break;
3219
 
 
3220
3029
      case DECIMAL_RESULT:
3221
 
        key_length+= class_decimal_get_binary_size(group_item->max_length -
 
3030
        key_length+= my_decimal_get_binary_size(group_item->max_length -
3222
3031
                                                (group_item->decimals ? 1 : 0),
3223
3032
                                                group_item->decimals);
3224
3033
        break;
3225
 
 
3226
3034
      case STRING_RESULT:
3227
 
        {
3228
 
          enum enum_field_types type= group_item->field_type();
 
3035
      {
 
3036
        enum enum_field_types type= group_item->field_type();
 
3037
        /*
 
3038
          As items represented as DATE/TIME fields in the group buffer
 
3039
          have STRING_RESULT result type, we increase the length
 
3040
          by 8 as maximum pack length of such fields.
 
3041
        */
 
3042
        if (type == DRIZZLE_TYPE_DATE ||
 
3043
            type == DRIZZLE_TYPE_DATETIME ||
 
3044
            type == DRIZZLE_TYPE_TIMESTAMP)
 
3045
        {
 
3046
          key_length+= 8;
 
3047
        }
 
3048
        else
 
3049
        {
3229
3050
          /*
3230
 
            As items represented as DATE/TIME fields in the group buffer
3231
 
            have STRING_RESULT result type, we increase the length
3232
 
            by 8 as maximum pack length of such fields.
 
3051
            Group strings are taken as varstrings and require an length field.
 
3052
            A field is not yet created by create_tmp_field()
 
3053
            and the sizes should match up.
3233
3054
          */
3234
 
          if (field::isDateTime(type))
3235
 
          {
3236
 
            key_length+= 8;
3237
 
          }
3238
 
          else
3239
 
          {
3240
 
            /*
3241
 
              Group strings are taken as varstrings and require an length field.
3242
 
              A field is not yet created by create_tmp_field()
3243
 
              and the sizes should match up.
3244
 
            */
3245
 
            key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
3246
 
          }
3247
 
 
3248
 
          break;
 
3055
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
3249
3056
        }
3250
 
 
3251
 
      case ROW_RESULT:
 
3057
        break;
 
3058
      }
 
3059
      default:
3252
3060
        /* This case should never be choosen */
3253
3061
        assert(0);
3254
3062
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
3255
3063
      }
3256
3064
    }
3257
 
 
3258
3065
    parts++;
3259
 
 
3260
3066
    if (group_item->maybe_null)
3261
3067
      null_parts++;
3262
3068
  }
3263
 
 
3264
3069
  join->tmp_table_param.group_length=key_length+null_parts;
3265
3070
  join->tmp_table_param.group_parts=parts;
3266
3071
  join->tmp_table_param.group_null_parts=null_parts;
3271
3076
 
3272
3077
  Groups are saved in reverse order for easyer check loop.
3273
3078
*/
3274
 
static bool alloc_group_fields(Join *join, Order *group)
 
3079
static bool alloc_group_fields(Join *join,order_st *group)
3275
3080
{
3276
3081
  if (group)
3277
3082
  {
3403
3208
 
3404
3209
  table_count=join->tables;
3405
3210
  if (!(join->join_tab=join_tab=
3406
 
  (JoinTable*) session->getMemRoot()->allocate(sizeof(JoinTable)*table_count)))
 
3211
  (JoinTable*) session->alloc(sizeof(JoinTable)*table_count)))
3407
3212
    return(true);
3408
3213
 
3409
 
  for (i= 0; i < table_count; i++)
3410
 
    new (join_tab+i) JoinTable();
3411
 
 
3412
3214
  join->full_join=0;
3413
3215
 
3414
3216
  used_tables= OUTER_REF_TABLE_BIT;   // Outer row is already read
3527
3329
    i.e. they have subqueries, unions or call stored procedures.
3528
3330
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
3529
3331
  */
3530
 
  if (join->session->getLex()->is_single_level_stmt())
 
3332
  if (join->session->lex->is_single_level_stmt())
3531
3333
    join->session->status_var.last_query_cost= join->best_read;
3532
3334
  return(false);
3533
3335
}
4040
3842
        will ensure that this will be used
4041
3843
      */
4042
3844
      best= tmp;
4043
 
      records= rnd_records;
 
3845
      records= rows2double(rnd_records);
4044
3846
      best_key= 0;
4045
3847
      /* range/index_merge/ALL/index access method are "independent", so: */
4046
3848
      best_ref_depends_map= 0;
4393
4195
                                             uint32_t prune_level)
4394
4196
{
4395
4197
  Session *session= join->session;
4396
 
  if (session->getKilled())  // Abort
 
4198
  if (session->killed)  // Abort
4397
4199
    return(true);
4398
4200
 
4399
4201
  /*
4561
4363
  */
4562
4364
  if (!join->table_reexec)
4563
4365
  {
4564
 
    if (!(join->table_reexec= (Table**) join->session->getMemRoot()->allocate(sizeof(Table*))))
 
4366
    if (!(join->table_reexec= (Table**) join->session->alloc(sizeof(Table*))))
4565
4367
      return(true);
4566
4368
    if (join->tmp_join)
4567
4369
      join->tmp_join->table_reexec= join->table_reexec;
4569
4371
  if (!join->join_tab_reexec)
4570
4372
  {
4571
4373
    if (!(join->join_tab_reexec=
4572
 
          (JoinTable*) join->session->getMemRoot()->allocate(sizeof(JoinTable))))
 
4374
          (JoinTable*) join->session->alloc(sizeof(JoinTable))))
4573
4375
      return(true);
4574
 
    new (join->join_tab_reexec) JoinTable();
4575
4376
    if (join->tmp_join)
4576
4377
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
4577
4378
  }
4592
4393
  join->row_limit=join->unit->select_limit_cnt;
4593
4394
  join->do_send_rows = (join->row_limit) ? 1 : 0;
4594
4395
 
 
4396
  join_tab->cache.buff=0;                       /* No caching */
4595
4397
  join_tab->table=tmp_table;
4596
4398
  join_tab->select=0;
4597
4399
  join_tab->select_cond=0;
4607
4409
  join_tab->read_first_record= join_init_read_record;
4608
4410
  join_tab->join=join;
4609
4411
  join_tab->ref.key_parts= 0;
4610
 
  join_tab->read_record.init();
 
4412
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
4611
4413
  tmp_table->status=0;
4612
4414
  tmp_table->null_row=0;
4613
 
 
4614
 
  return false;
 
4415
  return(false);
4615
4416
}
4616
4417
 
4617
4418
/**
4682
4483
      /* Ignore sj-nests: */
4683
4484
      if (!embedding->on_expr)
4684
4485
        continue;
4685
 
      NestedJoin *nested_join= embedding->getNestedJoin();
 
4486
      nested_join_st *nested_join= embedding->getNestedJoin();
4686
4487
      if (!nested_join->counter_)
4687
4488
      {
4688
4489
        /*
4721
4522
      if (join->tables > 1)
4722
4523
        cond->update_used_tables();             // Tablenr may have changed
4723
4524
      if (join->const_tables == join->tables &&
4724
 
          session->getLex()->current_select->master_unit() ==
4725
 
          &session->getLex()->unit)             // not upper level SELECT
 
4525
          session->lex->current_select->master_unit() ==
 
4526
          &session->lex->unit)          // not upper level SELECT
4726
4527
        join->const_table_map|=RAND_TABLE_BIT;
4727
4528
      {                                         // Check const tables
4728
4529
        COND *const_cond=
4791
4592
        tab->ref.key= -1;
4792
4593
        tab->ref.key_parts= 0;          // Don't use ref key.
4793
4594
        cur_pos= join->getPosFromOptimalPlan(i);
4794
 
        cur_pos.setFanout(tab->quick->records);
 
4595
        cur_pos.setFanout(rows2double(tab->quick->records));
4795
4596
        /*
4796
4597
           We will use join cache here : prevent sorting of the first
4797
4598
           table only and sort at the end.
4800
4601
          join->full_join= 1;
4801
4602
      }
4802
4603
 
4803
 
      if (join->full_join and not session->getLex()->current_select->is_cross and not cond)
4804
 
      {
4805
 
        my_error(ER_CARTESIAN_JOIN_ATTEMPTED, MYF(0));
4806
 
        return 1;
4807
 
      }
4808
 
 
4809
4604
      tmp= NULL;
4810
4605
      if (cond)
4811
4606
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
4836
4631
          tab->type == AM_EQ_REF)
4837
4632
      {
4838
4633
        optimizer::SqlSelect *sel= tab->select= ((optimizer::SqlSelect*)
4839
 
            session->getMemRoot()->duplicate((unsigned char*) select,
 
4634
            session->memdup((unsigned char*) select,
4840
4635
              sizeof(*select)));
4841
4636
        if (! sel)
4842
4637
          return 1;                     // End of memory
4974
4769
                                         current_map, 0)))
4975
4770
            {
4976
4771
              tab->cache.select= (optimizer::SqlSelect*)
4977
 
                session->getMemRoot()->duplicate((unsigned char*) sel, sizeof(optimizer::SqlSelect));
 
4772
                session->memdup((unsigned char*) sel, sizeof(optimizer::SqlSelect));
4978
4773
              tab->cache.select->cond= tmp;
4979
4774
              tab->cache.select->read_tables= join->const_table_map;
4980
4775
            }
5110
4905
 
5111
4906
    if (tab->insideout_match_tab)
5112
4907
    {
5113
 
      if (! (tab->insideout_buf= (unsigned char*) join->session->getMemRoot()->allocate(tab->table->key_info
 
4908
      if (! (tab->insideout_buf= (unsigned char*) join->session->alloc(tab->table->key_info
5114
4909
                                                                       [tab->index].
5115
4910
                                                                       key_length)))
5116
4911
        return true;
5161
4956
}
5162
4957
 
5163
4958
/** Update the dependency map for the sort order. */
5164
 
static void update_depend_map(Join *join, Order *order)
 
4959
static void update_depend_map(Join *join, order_st *order)
5165
4960
{
5166
4961
  for (; order ; order=order->next)
5167
4962
  {
5199
4994
  @return
5200
4995
    Returns new sort order
5201
4996
*/
5202
 
static Order *remove_constants(Join *join,Order *first_order, COND *cond, bool change_list, bool *simple_order)
 
4997
static order_st *remove_constants(Join *join,order_st *first_order, COND *cond, bool change_list, bool *simple_order)
5203
4998
{
5204
4999
  if (join->tables == join->const_tables)
5205
5000
    return change_list ? 0 : first_order;               // No need to sort
5206
5001
 
5207
 
  Order *order,**prev_ptr;
 
5002
  order_st *order,**prev_ptr;
5208
5003
  table_map first_table= join->join_tab[join->const_tables].table->map;
5209
5004
  table_map not_const_tables= ~join->const_table_map;
5210
5005
  table_map ref;
5292
5087
  {
5293
5088
    if (send_row)
5294
5089
    {
5295
 
      List<Item>::iterator it(fields.begin());
 
5090
      List_iterator_fast<Item> it(fields);
5296
5091
      Item *item;
5297
5092
      while ((item= it++))
5298
5093
        item->no_rows_in_result();
5428
5223
static COND *simplify_joins(Join *join, List<TableList> *join_list, COND *conds, bool top)
5429
5224
{
5430
5225
  TableList *table;
5431
 
  NestedJoin *nested_join;
 
5226
  nested_join_st *nested_join;
5432
5227
  TableList *prev_table= 0;
5433
 
  List<TableList>::iterator li(join_list->begin());
 
5228
  List_iterator<TableList> li(*join_list);
5434
5229
 
5435
5230
  /*
5436
5231
    Try to simplify join operations from join_list.
5538
5333
 
5539
5334
    if (prev_table)
5540
5335
    {
5541
 
      //If this is straight join, set prev table to be dependent on all tables
5542
 
      //from this nested join, so that correct join order is selected.
5543
 
      if ((test(join->select_options & SELECT_STRAIGHT_JOIN)) ||
5544
 
          prev_table->straight)
 
5336
      /* The order of tables is reverse: prev_table follows table */
 
5337
      if (prev_table->straight)
5545
5338
        prev_table->setDepTables(prev_table->getDepTables() | used_tables);
5546
5339
      if (prev_table->on_expr)
5547
5340
      {
5566
5359
    Flatten nested joins that can be flattened.
5567
5360
    no ON expression and not a semi-join => can be flattened.
5568
5361
  */
5569
 
  li= join_list->begin();
 
5362
  li.rewind();
5570
5363
  while ((table= li++))
5571
5364
  {
5572
5365
    nested_join= table->getNestedJoin();
5573
5366
    if (nested_join && !table->on_expr)
5574
5367
    {
5575
5368
      TableList *tbl;
5576
 
      List<TableList>::iterator it(nested_join->join_list.begin());
 
5369
      List_iterator<TableList> it(nested_join->join_list);
5577
5370
      while ((tbl= it++))
5578
5371
      {
5579
5372
        tbl->setEmbedding(table->getEmbedding());
5596
5389
 
5597
5390
  /* Calculate how many saved fields there is in list */
5598
5391
  field_count=0;
5599
 
  List<Item>::iterator it(fields.begin());
 
5392
  List_iterator<Item> it(fields);
5600
5393
  Item *item;
5601
5394
  while ((item=it++))
5602
5395
  {
5645
5438
                               List<Item> &fields,
5646
5439
                               List<Item> &all_fields,
5647
5440
                               COND **conds,
5648
 
                               Order *order,
5649
 
                               Order *group,
 
5441
                               order_st *order,
 
5442
                               order_st *group,
5650
5443
                               bool *hidden_group_fields)
5651
5444
{
5652
5445
  int res;
5653
 
  nesting_map save_allow_sum_func=session->getLex()->allow_sum_func ;
 
5446
  nesting_map save_allow_sum_func=session->lex->allow_sum_func ;
5654
5447
 
5655
 
  session->getLex()->allow_sum_func&= ~(1 << session->getLex()->current_select->nest_level);
 
5448
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
5656
5449
  res= session->setup_conds(tables, conds);
5657
5450
 
5658
 
  session->getLex()->allow_sum_func|= 1 << session->getLex()->current_select->nest_level;
 
5451
  session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
5659
5452
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
5660
5453
                          order);
5661
 
  session->getLex()->allow_sum_func&= ~(1 << session->getLex()->current_select->nest_level);
 
5454
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
5662
5455
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
5663
5456
                          group, hidden_group_fields);
5664
 
  session->getLex()->allow_sum_func= save_allow_sum_func;
 
5457
  session->lex->allow_sum_func= save_allow_sum_func;
5665
5458
  return(res);
5666
5459
}
5667
5460
 
5701
5494
 
5702
5495
  table_count= join->tables;
5703
5496
  stat= (JoinTable*) join->session->calloc(sizeof(JoinTable)*table_count);
5704
 
  stat_ref= (JoinTable**) join->session->getMemRoot()->allocate(sizeof(JoinTable*)*MAX_TABLES);
5705
 
  table_vector= (Table**) join->session->getMemRoot()->allocate(sizeof(Table*)*(table_count*2));
 
5497
  stat_ref= (JoinTable**) join->session->alloc(sizeof(JoinTable*)*MAX_TABLES);
 
5498
  table_vector= (Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
5706
5499
  if (! stat || ! stat_ref || ! table_vector)
5707
5500
    return 1;
5708
5501
 
5766
5559
      s->embedding_map.reset();
5767
5560
      do
5768
5561
      {
5769
 
        NestedJoin *nested_join= embedding->getNestedJoin();
 
5562
        nested_join_st *nested_join= embedding->getNestedJoin();
5770
5563
        s->embedding_map|= nested_join->nj_map;
5771
5564
        s->dependent|= embedding->getDepTables();
5772
5565
        embedding= embedding->getEmbedding();
5795
5588
       As we use bitmaps to represent the relation the complexity
5796
5589
       of the algorithm is O((number of tables)^2).
5797
5590
    */
5798
 
    for (i= 0; i < table_count; i++)
 
5591
    for (i= 0, s= stat ; i < table_count ; i++, s++)
5799
5592
    {
5800
 
      uint32_t j;
5801
 
      table= stat[i].table;
5802
 
 
5803
 
      if (!table->reginfo.join_tab->dependent)
5804
 
        continue;
5805
 
 
5806
 
      for (j= 0, s= stat; j < table_count; j++, s++)
 
5593
      for (uint32_t j= 0 ; j < table_count ; j++)
5807
5594
      {
 
5595
        table= stat[j].table;
5808
5596
        if (s->dependent & table->map)
5809
 
        {
5810
 
          table_map was_dependent= s->dependent;
5811
5597
          s->dependent |= table->reginfo.join_tab->dependent;
5812
 
          if (i > j && s->dependent != was_dependent)
5813
 
          {
5814
 
            i= j= 1;
5815
 
            break;
5816
 
          }
5817
 
        }
5818
5598
      }
 
5599
      if (s->dependent)
 
5600
        s->table->maybe_null= 1;
5819
5601
    }
5820
5602
    /* Catch illegal cross references for outer joins */
5821
5603
    for (i= 0, s= stat ; i < table_count ; i++, s++)
5826
5608
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
5827
5609
        return 1;
5828
5610
      }
5829
 
      if (outer_join & s->table->map)
5830
 
        s->table->maybe_null= 1;
5831
 
 
5832
5611
      s->key_dependent= s->dependent;
5833
5612
    }
5834
5613
  }
5850
5629
    s= p_pos->getJoinTable();
5851
5630
    s->type= AM_SYSTEM;
5852
5631
    join->const_table_map|=s->table->map;
5853
 
    if ((tmp= s->joinReadConstTable(p_pos)))
 
5632
    if ((tmp= join_read_const_table(s, p_pos)))
5854
5633
    {
5855
5634
      if (tmp > 0)
5856
5635
        return 1;                       // Fatal error
5924
5703
          join->const_table_map|=table->map;
5925
5704
          set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
5926
5705
          partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5927
 
          if ((tmp= s->joinReadConstTable(partial_pos)))
 
5706
          if ((tmp= join_read_const_table(s, partial_pos)))
5928
5707
          {
5929
5708
            if (tmp > 0)
5930
5709
              return 1;                 // Fatal error
5976
5755
                if (create_ref_for_key(join, s, start_keyuse, found_const_table_map))
5977
5756
                  return 1;
5978
5757
                partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5979
 
                if ((tmp=s->joinReadConstTable(partial_pos)))
 
5758
                if ((tmp=join_read_const_table(s, partial_pos)))
5980
5759
                {
5981
5760
                  if (tmp > 0)
5982
5761
                    return 1;                   // Fatal error
6060
5839
      s->quick=select->quick;
6061
5840
      s->needed_reg=select->needed_reg;
6062
5841
      select->quick=0;
6063
 
 
6064
5842
      if (records == 0 && s->table->reginfo.impossible_range)
6065
5843
      {
6066
5844
        /*
6100
5878
  if (join->const_tables != join->tables)
6101
5879
  {
6102
5880
    optimize_keyuse(join, keyuse_array);
6103
 
    // @note c_str() is not likely to be valid here if dtrace expects it to
6104
 
    // exist for any period of time.
6105
 
    DRIZZLE_QUERY_OPT_CHOOSE_PLAN_START(join->session->getQueryString()->c_str(), join->session->thread_id);
 
5881
    DRIZZLE_QUERY_OPT_CHOOSE_PLAN_START(join->session->query.c_str(), join->session->thread_id);
6106
5882
    bool res= choose_plan(join, all_table_map & ~join->const_table_map);
6107
5883
    DRIZZLE_QUERY_OPT_CHOOSE_PLAN_DONE(res ? 1 : 0);
6108
5884
    if (res)
6114
5890
    join->best_read= 1.0;
6115
5891
  }
6116
5892
  /* Generate an execution plan from the found optimal join order. */
6117
 
  return (join->session->getKilled() || get_best_combination(join));
 
5893
  return (join->session->killed || get_best_combination(join));
6118
5894
}
6119
5895
 
6120
5896
/**
6138
5914
*/
6139
5915
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused)
6140
5916
{
6141
 
  List<TableList>::iterator li(join_list->begin());
 
5917
  List_iterator<TableList> li(*join_list);
6142
5918
  TableList *table;
6143
5919
  while ((table= li++))
6144
5920
  {
6145
 
    NestedJoin *nested_join;
 
5921
    nested_join_st *nested_join;
6146
5922
    if ((nested_join= table->getNestedJoin()))
6147
5923
    {
6148
5924
      /*
6174
5950
  Return table number if there is only one table in sort order
6175
5951
  and group and order is compatible, else return 0.
6176
5952
*/
6177
 
static Table *get_sort_by_table(Order *a, Order *b,TableList *tables)
 
5953
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables)
6178
5954
{
6179
5955
  table_map map= (table_map) 0;
6180
5956
 
6199
5975
}
6200
5976
 
6201
5977
/**
6202
 
  Set NestedJoin::counter=0 in all nested joins in passed list.
 
5978
  Set nested_join_st::counter=0 in all nested joins in passed list.
6203
5979
 
6204
 
    Recursively set NestedJoin::counter=0 for all nested joins contained in
 
5980
    Recursively set nested_join_st::counter=0 for all nested joins contained in
6205
5981
    the passed join_list.
6206
5982
 
6207
5983
  @param join_list  List of nested joins to process. It may also contain base
6209
5985
*/
6210
5986
static void reset_nj_counters(List<TableList> *join_list)
6211
5987
{
6212
 
  List<TableList>::iterator li(join_list->begin());
 
5988
  List_iterator<TableList> li(*join_list);
6213
5989
  TableList *table;
6214
5990
  while ((table= li++))
6215
5991
  {
6216
 
    NestedJoin *nested_join;
 
5992
    nested_join_st *nested_join;
6217
5993
    if ((nested_join= table->getNestedJoin()))
6218
5994
    {
6219
5995
      nested_join->counter_= 0;
6229
6005
  If first parts has different direction, change it to second part
6230
6006
  (group is sorted like order)
6231
6007
*/
6232
 
static bool test_if_subpart(Order *a, Order *b)
 
6008
static bool test_if_subpart(order_st *a,order_st *b)
6233
6009
{
6234
6010
  for (; a && b; a=a->next,b=b->next)
6235
6011
  {
6299
6075
  Join *join= last->join;
6300
6076
  for (;last_emb != NULL; last_emb= last_emb->getEmbedding())
6301
6077
  {
6302
 
    NestedJoin *nest= last_emb->getNestedJoin();
 
6078
    nested_join_st *nest= last_emb->getNestedJoin();
6303
6079
    
6304
6080
    bool was_fully_covered= nest->is_fully_covered();
6305
6081