~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join.cc

  • Committer: Monty Taylor
  • Date: 2010-10-21 23:10:12 UTC
  • mto: (1879.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1880.
  • Revision ID: mordred@inaugust.com-20101021231012-uhsebiqo23xi0ygy
Updated AUTHORS list with everyone from bzr logs.

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=
1352
1189
*/
1353
1190
bool Join::init_save_join_tab()
1354
1191
{
1355
 
  if (!(tmp_join= (Join*)session->getMemRoot()->allocate(sizeof(Join))))
 
1192
  if (!(tmp_join= (Join*)session->alloc(sizeof(Join))))
1356
1193
    return 1;
1357
 
 
1358
1194
  error= 0;              // Ensure that tmp_join.error= 0
1359
1195
  restore_tmp();
1360
 
 
1361
1196
  return 0;
1362
1197
}
1363
1198
 
1365
1200
{
1366
1201
  if (! join_tab_save && select_lex->master_unit()->uncacheable.any())
1367
1202
  {
1368
 
    if (!(join_tab_save= (JoinTable*)session->getMemRoot()->duplicate((unsigned char*) join_tab,
 
1203
    if (!(join_tab_save= (JoinTable*)session->memdup((unsigned char*) join_tab,
1369
1204
            sizeof(JoinTable) * tables)))
1370
1205
      return 1;
1371
1206
  }
1613
1448
              exec_tmp_table2= create_tmp_table(session,
1614
1449
                                                &curr_join->tmp_table_param,
1615
1450
                                                *curr_all_fields,
1616
 
                                                (Order*) 0,
 
1451
                                                (order_st*) 0,
1617
1452
                                                curr_join->select_distinct &&
1618
1453
                                                !curr_join->group_list,
1619
1454
                                                1, curr_join->select_options,
1767
1602
      if (sort_table_cond)
1768
1603
      {
1769
1604
        if (!curr_table->select)
1770
 
          if (!(curr_table->select= new optimizer::SqlSelect()))
 
1605
          if (!(curr_table->select= new optimizer::SqlSelect))
1771
1606
            return;
1772
1607
        if (!curr_table->select->cond)
1773
1608
          curr_table->select->cond= sort_table_cond;
1871
1706
    for a derived table which is always materialized.
1872
1707
    Otherwise we would not be able to print the query  correctly.
1873
1708
  */
1874
 
  if (items0 && (session->getLex()->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
 
1709
  if (items0 && (session->lex->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
1875
1710
    set_items_ref_array(items0);
1876
1711
 
1877
1712
  return;
1971
1806
    is called after all rows are sent, but before EOF packet is sent.
1972
1807
 
1973
1808
    For a simple SELECT with no subqueries this function performs a full
1974
 
    cleanup of the Join and calls unlockReadTables to free used base
 
1809
    cleanup of the Join and calls mysql_unlock_read_tables to free used base
1975
1810
    tables.
1976
1811
 
1977
1812
    If a Join is executed for a subquery or if it has a subquery, we can't
2003
1838
    Optimization: if not EXPLAIN and we are done with the Join,
2004
1839
    free all tables.
2005
1840
  */
2006
 
  bool full= (select_lex->uncacheable.none() && ! session->getLex()->describe);
 
1841
  bool full= (select_lex->uncacheable.none() && ! session->lex->describe);
2007
1842
  bool can_unlock= full;
2008
1843
 
2009
1844
  cleanup(full);
2014
1849
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
2015
1850
    {
2016
1851
      Item_subselect *subselect= sl->master_unit()->item;
2017
 
      bool full_local= full && (!subselect || 
2018
 
                                (subselect->is_evaluated() &&
2019
 
                                !subselect->is_uncacheable()));
 
1852
      bool full_local= full && (!subselect || subselect->is_evaluated());
2020
1853
      /*
2021
1854
        If this join is evaluated, we can fully clean it up and clean up all
2022
1855
        its underlying joins even if they are correlated -- they will not be
2038
1871
  if (can_unlock && lock && session->lock &&
2039
1872
      !(select_options & SELECT_NO_UNLOCK) &&
2040
1873
      !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)))
 
1874
      (select_lex == (session->lex->unit.fake_select_lex ?
 
1875
                      session->lex->unit.fake_select_lex : &session->lex->select_lex)))
2043
1876
  {
2044
1877
    /*
2045
1878
      TODO: unlock tables even if the join isn't top level select in the
2046
1879
      tree.
2047
1880
    */
2048
 
    session->unlockReadTables(lock);           // Don't free join->lock
 
1881
    mysql_unlock_read_tables(session, lock);           // Don't free join->lock
2049
1882
    lock= 0;
2050
1883
  }
2051
1884
 
2068
1901
{
2069
1902
  if (table)
2070
1903
  {
 
1904
    JoinTable *tab,*end;
2071
1905
    /*
2072
1906
      Only a sorted table may be cached.  This sorted table is always the
2073
1907
      first non const table in join->table
2077
1911
      table[const_tables]->free_io_cache();
2078
1912
      table[const_tables]->filesort_free_buffers(full);
2079
1913
    }
2080
 
  }
2081
 
 
2082
 
  if (join_tab)
2083
 
  {
2084
 
    JoinTable *tab,*end;
2085
1914
 
2086
1915
    if (full)
2087
1916
    {
2098
1927
      }
2099
1928
    }
2100
1929
  }
2101
 
 
2102
1930
  /*
2103
1931
    We are not using tables anymore
2104
1932
    Unlock all tables. We may be in an INSERT .... SELECT statement.
2112
1940
      We can't call delete_elements() on copy_funcs as this will cause
2113
1941
      problems in free_elements() as some of the elements are then deleted.
2114
1942
    */
2115
 
    tmp_table_param.copy_funcs.clear();
 
1943
    tmp_table_param.copy_funcs.empty();
2116
1944
    /*
2117
1945
      If we have tmp_join and 'this' Join is not tmp_join and
2118
1946
      tmp_table_param.copy_field's  of them are equal then we have to remove
2142
1970
    are not re-calculated.
2143
1971
  */
2144
1972
  for (uint32_t i= join->const_tables; i < join->tables; i++)
2145
 
  {
2146
1973
    join->table[i]->mark_as_null_row();   // All fields are NULL
2147
 
  }
2148
1974
}
2149
1975
 
2150
1976
/**
2165
1991
    If we are using rollup, we need a copy of the summary functions for
2166
1992
    each level
2167
1993
  */
2168
 
  if (rollup.getState() != Rollup::STATE_NONE)
 
1994
  if (rollup.state != ROLLUP::STATE_NONE)
2169
1995
    func_count*= (send_group_parts+1);
2170
1996
 
2171
1997
  group_parts= send_group_parts;
2182
2008
    */
2183
2009
    if (order)
2184
2010
    {
2185
 
      Order *ord;
 
2011
      order_st *ord;
2186
2012
      for (ord= order; ord; ord= ord->next)
2187
2013
        group_parts++;
2188
2014
    }
2213
2039
                              bool before_group_by, 
2214
2040
                              bool recompute)
2215
2041
{
2216
 
  List<Item>::iterator it(field_list.begin());
 
2042
  List_iterator_fast<Item> it(field_list);
2217
2043
  Item_sum **func;
2218
2044
  Item *item;
2219
2045
 
2228
2054
         ((Item_sum *)item)->depended_from() == select_lex))
2229
2055
      *func++= (Item_sum*) item;
2230
2056
  }
2231
 
  if (before_group_by && rollup.getState() == Rollup::STATE_INITED)
 
2057
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
2232
2058
  {
2233
 
    rollup.setState(Rollup::STATE_READY);
 
2059
    rollup.state= ROLLUP::STATE_READY;
2234
2060
    if (rollup_make_fields(field_list, send_fields, &func))
2235
 
      return true;     // Should never happen
 
2061
      return(true);     // Should never happen
2236
2062
  }
2237
 
  else if (rollup.getState() == Rollup::STATE_NONE)
 
2063
  else if (rollup.state == ROLLUP::STATE_NONE)
2238
2064
  {
2239
2065
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
2240
2066
      sum_funcs_end[i]= func;
2241
2067
  }
2242
 
  else if (rollup.getState() == Rollup::STATE_READY)
 
2068
  else if (rollup.state == ROLLUP::STATE_READY)
2243
2069
    return(false);                         // Don't put end marker
2244
2070
  *func=0;          // End marker
2245
2071
  return(false);
2248
2074
/** Allocate memory needed for other rollup functions. */
2249
2075
bool Join::rollup_init()
2250
2076
{
 
2077
  uint32_t i,j;
2251
2078
  Item **ref_array;
2252
2079
 
2253
2080
  tmp_table_param.quick_group= 0; // Can't create groups in tmp table
2254
 
  rollup.setState(Rollup::STATE_INITED);
 
2081
  rollup.state= ROLLUP::STATE_INITED;
2255
2082
 
2256
2083
  /*
2257
2084
    Create pointers to the different sum function groups
2259
2086
  */
2260
2087
  tmp_table_param.group_parts= send_group_parts;
2261
2088
 
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
 
  {
 
2089
  if (!(rollup.null_items= (Item_null_result**) session->alloc((sizeof(Item*) +
 
2090
                                                sizeof(Item**) +
 
2091
                                                sizeof(List<Item>) +
 
2092
                        ref_pointer_array_size)
 
2093
                        * send_group_parts )))
2269
2094
    return 1;
2270
 
  }
2271
2095
 
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);
 
2096
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
2097
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
2098
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
2275
2099
 
2276
2100
  /*
2277
2101
    Prepare space for field list for the different levels
2278
2102
    These will be filled up in rollup_make_fields()
2279
2103
  */
2280
 
  for (uint32_t i= 0 ; i < send_group_parts ; i++)
 
2104
  for (i= 0 ; i < send_group_parts ; i++)
2281
2105
  {
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;
 
2106
    rollup.null_items[i]= new (session->mem_root) Item_null_result();
 
2107
    List<Item> *rollup_fields= &rollup.fields[i];
 
2108
    rollup_fields->empty();
 
2109
    rollup.ref_pointer_arrays[i]= ref_array;
2286
2110
    ref_array+= all_fields.elements;
2287
2111
  }
2288
 
 
2289
 
  for (uint32_t i= 0 ; i < send_group_parts; i++)
 
2112
  for (i= 0 ; i < send_group_parts; i++)
2290
2113
  {
2291
 
    for (uint32_t j= 0 ; j < fields_list.elements ; j++)
2292
 
    {
2293
 
      rollup.getFields()[i].push_back(rollup.getNullItems()[i]);
2294
 
    }
 
2114
    for (j=0 ; j < fields_list.elements ; j++)
 
2115
      rollup.fields[i].push_back(rollup.null_items[i]);
2295
2116
  }
2296
 
 
2297
 
  List<Item>::iterator it(all_fields.begin());
 
2117
  List_iterator<Item> it(all_fields);
2298
2118
  Item *item;
2299
2119
  while ((item= it++))
2300
2120
  {
2301
 
    Order *group_tmp;
 
2121
    order_st *group_tmp;
2302
2122
    bool found_in_group= 0;
2303
2123
 
2304
2124
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
2327
2147
            return 1;
2328
2148
          new_item->fix_fields(session, (Item **) 0);
2329
2149
          session->change_item_tree(it.ref(), new_item);
2330
 
          for (Order *tmp= group_tmp; tmp; tmp= tmp->next)
 
2150
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
2331
2151
          {
2332
2152
            if (*tmp->item == item)
2333
2153
              session->change_item_tree(tmp->item, new_item);
2369
2189
*/
2370
2190
bool Join::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields, Item_sum ***func)
2371
2191
{
2372
 
  List<Item>::iterator it(fields_arg.begin());
 
2192
  List_iterator_fast<Item> it(fields_arg);
2373
2193
  Item *first_field= sel_fields.head();
2374
2194
  uint32_t level;
2375
2195
 
2400
2220
    uint32_t pos= send_group_parts - level -1;
2401
2221
    bool real_fields= 0;
2402
2222
    Item *item;
2403
 
    List<Item>::iterator new_it(rollup.getFields()[pos].begin());
2404
 
    Item **ref_array_start= rollup.getRefPointerArrays()[pos];
2405
 
    Order *start_group;
 
2223
    List_iterator<Item> new_it(rollup.fields[pos]);
 
2224
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
2225
    order_st *start_group;
2406
2226
 
2407
2227
    /* Point to first hidden field */
2408
2228
    Item **ref_array= ref_array_start + fields_arg.elements-1;
2414
2234
    for (i= 0, start_group= group_list ;i++ < pos ;start_group= start_group->next)
2415
2235
    {}
2416
2236
 
2417
 
    it= fields_arg.begin();
 
2237
    it.rewind();
2418
2238
    while ((item= it++))
2419
2239
    {
2420
2240
      if (item == first_field)
2444
2264
      else
2445
2265
      {
2446
2266
        /* Check if this is something that is part of this group by */
2447
 
        Order *group_tmp;
 
2267
        order_st *group_tmp;
2448
2268
        for (group_tmp= start_group, i= pos ;
2449
2269
                  group_tmp ; group_tmp= group_tmp->next, i++)
2450
2270
        {
2499
2319
*/
2500
2320
int Join::rollup_send_data(uint32_t idx)
2501
2321
{
2502
 
  for (uint32_t i= send_group_parts ; i-- > idx ; )
 
2322
  uint32_t i;
 
2323
  for (i= send_group_parts ; i-- > idx ; )
2503
2324
  {
2504
2325
    /* Get reference pointers to sum functions in place */
2505
 
    memcpy(ref_pointer_array, rollup.getRefPointerArrays()[i], ref_pointer_array_size);
2506
 
 
 
2326
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
2327
     ref_pointer_array_size);
2507
2328
    if ((!having || having->val_int()))
2508
2329
    {
2509
 
      if (send_records < unit->select_limit_cnt && do_send_rows && result->send_data(rollup.getFields()[i]))
2510
 
      {
2511
 
        return 1;
2512
 
      }
 
2330
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
2331
    result->send_data(rollup.fields[i]))
 
2332
  return 1;
2513
2333
      send_records++;
2514
2334
    }
2515
2335
  }
2516
2336
  /* Restore ref_pointer_array */
2517
2337
  set_items_ref_array(current_ref_pointer_array);
2518
 
 
2519
2338
  return 0;
2520
2339
}
2521
2340
 
2540
2359
*/
2541
2360
int Join::rollup_write_data(uint32_t idx, Table *table_arg)
2542
2361
{
2543
 
  for (uint32_t i= send_group_parts ; i-- > idx ; )
 
2362
  uint32_t i;
 
2363
  for (i= send_group_parts ; i-- > idx ; )
2544
2364
  {
2545
2365
    /* Get reference pointers to sum functions in place */
2546
 
    memcpy(ref_pointer_array, rollup.getRefPointerArrays()[i],
 
2366
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
2547
2367
           ref_pointer_array_size);
2548
2368
    if ((!having || having->val_int()))
2549
2369
    {
2550
2370
      int write_error;
2551
2371
      Item *item;
2552
 
      List<Item>::iterator it(rollup.getFields()[i].begin());
 
2372
      List_iterator_fast<Item> it(rollup.fields[i]);
2553
2373
      while ((item= it++))
2554
2374
      {
2555
2375
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
2565
2385
  }
2566
2386
  /* Restore ref_pointer_array */
2567
2387
  set_items_ref_array(current_ref_pointer_array);
2568
 
 
2569
2388
  return 0;
2570
2389
}
2571
2390
 
2661
2480
    return NESTED_LOOP_ERROR;
2662
2481
  if (error < 0)
2663
2482
    return NESTED_LOOP_NO_MORE_ROWS;
2664
 
  if (join->session->getKilled())                       // Aborted by user
 
2483
  if (join->session->killed)                    // Aborted by user
2665
2484
  {
2666
2485
    join->session->send_kill_message();
2667
2486
    return NESTED_LOOP_KILLED;
2873
2692
  info= &join_tab->read_record;
2874
2693
  do
2875
2694
  {
2876
 
    if (join->session->getKilled())
 
2695
    if (join->session->killed)
2877
2696
    {
2878
2697
      join->session->send_kill_message();
2879
2698
      return NESTED_LOOP_KILLED;
3005
2824
{
3006
2825
  Table *table= join->tmp_table;
3007
2826
 
3008
 
  if (join->session->getKilled())                       // Aborted by user
 
2827
  if (join->session->killed)                    // Aborted by user
3009
2828
  {
3010
2829
    join->session->send_kill_message();
3011
2830
    return NESTED_LOOP_KILLED;
3013
2832
  if (!end_of_records)
3014
2833
  {
3015
2834
    copy_fields(&join->tmp_table_param);
3016
 
    if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3017
 
      return NESTED_LOOP_ERROR;
 
2835
    copy_funcs(join->tmp_table_param.items_to_copy);
3018
2836
    if (!join->having || join->having->val_int())
3019
2837
    {
3020
2838
      int error;
3022
2840
      if ((error=table->cursor->insertRecord(table->getInsertRecord())))
3023
2841
      {
3024
2842
        if (!table->cursor->is_fatal_error(error, HA_CHECK_DUP))
3025
 
        {
3026
 
          return NESTED_LOOP_OK;
3027
 
        }
 
2843
          goto end;
3028
2844
 
3029
2845
        my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
3030
2846
        return NESTED_LOOP_ERROR;        // Table is_full error
3039
2855
      }
3040
2856
    }
3041
2857
  }
3042
 
 
 
2858
end:
3043
2859
  return NESTED_LOOP_OK;
3044
2860
}
3045
2861
 
3047
2863
enum_nested_loop_state end_update(Join *join, JoinTable *, bool end_of_records)
3048
2864
{
3049
2865
  Table *table= join->tmp_table;
3050
 
  Order *group;
 
2866
  order_st *group;
3051
2867
  int   error;
3052
2868
 
3053
2869
  if (end_of_records)
3054
2870
    return NESTED_LOOP_OK;
3055
 
  if (join->session->getKilled())                       // Aborted by user
 
2871
  if (join->session->killed)                    // Aborted by user
3056
2872
  {
3057
2873
    join->session->send_kill_message();
3058
2874
    return NESTED_LOOP_KILLED;
3099
2915
      memcpy(table->getInsertRecord()+key_part->offset, group->buff, 1);
3100
2916
  }
3101
2917
  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;
 
2918
  copy_funcs(join->tmp_table_param.items_to_copy);
3104
2919
  if ((error=table->cursor->insertRecord(table->getInsertRecord())))
3105
2920
  {
3106
2921
    my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
3118
2933
 
3119
2934
  if (end_of_records)
3120
2935
    return NESTED_LOOP_OK;
3121
 
  if (join->session->getKilled())                       // Aborted by user
 
2936
  if (join->session->killed)                    // Aborted by user
3122
2937
  {
3123
2938
    join->session->send_kill_message();
3124
2939
    return NESTED_LOOP_KILLED;
3126
2941
 
3127
2942
  init_tmptable_sum_functions(join->sum_funcs);
3128
2943
  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;
 
2944
  copy_funcs(join->tmp_table_param.items_to_copy);
3131
2945
 
3132
2946
  if (!(error= table->cursor->insertRecord(table->getInsertRecord())))
3133
2947
    join->send_records++;                       // New group
3186
3000
/**
3187
3001
  calc how big buffer we need for comparing group entries.
3188
3002
*/
3189
 
static void calc_group_buffer(Join *join, Order *group)
 
3003
static void calc_group_buffer(Join *join,order_st *group)
3190
3004
{
3191
3005
  uint32_t key_length=0, parts=0, null_parts=0;
3192
3006
 
3212
3026
      case REAL_RESULT:
3213
3027
        key_length+= sizeof(double);
3214
3028
        break;
3215
 
 
3216
3029
      case INT_RESULT:
3217
3030
        key_length+= sizeof(int64_t);
3218
3031
        break;
3219
 
 
3220
3032
      case DECIMAL_RESULT:
3221
 
        key_length+= class_decimal_get_binary_size(group_item->max_length -
 
3033
        key_length+= my_decimal_get_binary_size(group_item->max_length -
3222
3034
                                                (group_item->decimals ? 1 : 0),
3223
3035
                                                group_item->decimals);
3224
3036
        break;
3225
 
 
3226
3037
      case STRING_RESULT:
3227
 
        {
3228
 
          enum enum_field_types type= group_item->field_type();
 
3038
      {
 
3039
        enum enum_field_types type= group_item->field_type();
 
3040
        /*
 
3041
          As items represented as DATE/TIME fields in the group buffer
 
3042
          have STRING_RESULT result type, we increase the length
 
3043
          by 8 as maximum pack length of such fields.
 
3044
        */
 
3045
        if (type == DRIZZLE_TYPE_DATE ||
 
3046
            type == DRIZZLE_TYPE_DATETIME ||
 
3047
            type == DRIZZLE_TYPE_TIMESTAMP)
 
3048
        {
 
3049
          key_length+= 8;
 
3050
        }
 
3051
        else
 
3052
        {
3229
3053
          /*
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.
 
3054
            Group strings are taken as varstrings and require an length field.
 
3055
            A field is not yet created by create_tmp_field()
 
3056
            and the sizes should match up.
3233
3057
          */
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;
 
3058
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
3249
3059
        }
3250
 
 
3251
 
      case ROW_RESULT:
 
3060
        break;
 
3061
      }
 
3062
      default:
3252
3063
        /* This case should never be choosen */
3253
3064
        assert(0);
3254
3065
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
3255
3066
      }
3256
3067
    }
3257
 
 
3258
3068
    parts++;
3259
 
 
3260
3069
    if (group_item->maybe_null)
3261
3070
      null_parts++;
3262
3071
  }
3263
 
 
3264
3072
  join->tmp_table_param.group_length=key_length+null_parts;
3265
3073
  join->tmp_table_param.group_parts=parts;
3266
3074
  join->tmp_table_param.group_null_parts=null_parts;
3271
3079
 
3272
3080
  Groups are saved in reverse order for easyer check loop.
3273
3081
*/
3274
 
static bool alloc_group_fields(Join *join, Order *group)
 
3082
static bool alloc_group_fields(Join *join,order_st *group)
3275
3083
{
3276
3084
  if (group)
3277
3085
  {
3403
3211
 
3404
3212
  table_count=join->tables;
3405
3213
  if (!(join->join_tab=join_tab=
3406
 
  (JoinTable*) session->getMemRoot()->allocate(sizeof(JoinTable)*table_count)))
 
3214
  (JoinTable*) session->alloc(sizeof(JoinTable)*table_count)))
3407
3215
    return(true);
3408
3216
 
3409
 
  for (i= 0; i < table_count; i++)
3410
 
    new (join_tab+i) JoinTable();
3411
 
 
3412
3217
  join->full_join=0;
3413
3218
 
3414
3219
  used_tables= OUTER_REF_TABLE_BIT;   // Outer row is already read
3527
3332
    i.e. they have subqueries, unions or call stored procedures.
3528
3333
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
3529
3334
  */
3530
 
  if (join->session->getLex()->is_single_level_stmt())
 
3335
  if (join->session->lex->is_single_level_stmt())
3531
3336
    join->session->status_var.last_query_cost= join->best_read;
3532
3337
  return(false);
3533
3338
}
4040
3845
        will ensure that this will be used
4041
3846
      */
4042
3847
      best= tmp;
4043
 
      records= rnd_records;
 
3848
      records= rows2double(rnd_records);
4044
3849
      best_key= 0;
4045
3850
      /* range/index_merge/ALL/index access method are "independent", so: */
4046
3851
      best_ref_depends_map= 0;
4393
4198
                                             uint32_t prune_level)
4394
4199
{
4395
4200
  Session *session= join->session;
4396
 
  if (session->getKilled())  // Abort
 
4201
  if (session->killed)  // Abort
4397
4202
    return(true);
4398
4203
 
4399
4204
  /*
4561
4366
  */
4562
4367
  if (!join->table_reexec)
4563
4368
  {
4564
 
    if (!(join->table_reexec= (Table**) join->session->getMemRoot()->allocate(sizeof(Table*))))
 
4369
    if (!(join->table_reexec= (Table**) join->session->alloc(sizeof(Table*))))
4565
4370
      return(true);
4566
4371
    if (join->tmp_join)
4567
4372
      join->tmp_join->table_reexec= join->table_reexec;
4569
4374
  if (!join->join_tab_reexec)
4570
4375
  {
4571
4376
    if (!(join->join_tab_reexec=
4572
 
          (JoinTable*) join->session->getMemRoot()->allocate(sizeof(JoinTable))))
 
4377
          (JoinTable*) join->session->alloc(sizeof(JoinTable))))
4573
4378
      return(true);
4574
 
    new (join->join_tab_reexec) JoinTable();
4575
4379
    if (join->tmp_join)
4576
4380
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
4577
4381
  }
4592
4396
  join->row_limit=join->unit->select_limit_cnt;
4593
4397
  join->do_send_rows = (join->row_limit) ? 1 : 0;
4594
4398
 
 
4399
  join_tab->cache.buff=0;                       /* No caching */
4595
4400
  join_tab->table=tmp_table;
4596
4401
  join_tab->select=0;
4597
4402
  join_tab->select_cond=0;
4682
4487
      /* Ignore sj-nests: */
4683
4488
      if (!embedding->on_expr)
4684
4489
        continue;
4685
 
      NestedJoin *nested_join= embedding->getNestedJoin();
 
4490
      nested_join_st *nested_join= embedding->getNestedJoin();
4686
4491
      if (!nested_join->counter_)
4687
4492
      {
4688
4493
        /*
4721
4526
      if (join->tables > 1)
4722
4527
        cond->update_used_tables();             // Tablenr may have changed
4723
4528
      if (join->const_tables == join->tables &&
4724
 
          session->getLex()->current_select->master_unit() ==
4725
 
          &session->getLex()->unit)             // not upper level SELECT
 
4529
          session->lex->current_select->master_unit() ==
 
4530
          &session->lex->unit)          // not upper level SELECT
4726
4531
        join->const_table_map|=RAND_TABLE_BIT;
4727
4532
      {                                         // Check const tables
4728
4533
        COND *const_cond=
4791
4596
        tab->ref.key= -1;
4792
4597
        tab->ref.key_parts= 0;          // Don't use ref key.
4793
4598
        cur_pos= join->getPosFromOptimalPlan(i);
4794
 
        cur_pos.setFanout(tab->quick->records);
 
4599
        cur_pos.setFanout(rows2double(tab->quick->records));
4795
4600
        /*
4796
4601
           We will use join cache here : prevent sorting of the first
4797
4602
           table only and sort at the end.
4800
4605
          join->full_join= 1;
4801
4606
      }
4802
4607
 
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
4608
      tmp= NULL;
4810
4609
      if (cond)
4811
4610
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
4836
4635
          tab->type == AM_EQ_REF)
4837
4636
      {
4838
4637
        optimizer::SqlSelect *sel= tab->select= ((optimizer::SqlSelect*)
4839
 
            session->getMemRoot()->duplicate((unsigned char*) select,
 
4638
            session->memdup((unsigned char*) select,
4840
4639
              sizeof(*select)));
4841
4640
        if (! sel)
4842
4641
          return 1;                     // End of memory
4974
4773
                                         current_map, 0)))
4975
4774
            {
4976
4775
              tab->cache.select= (optimizer::SqlSelect*)
4977
 
                session->getMemRoot()->duplicate((unsigned char*) sel, sizeof(optimizer::SqlSelect));
 
4776
                session->memdup((unsigned char*) sel, sizeof(optimizer::SqlSelect));
4978
4777
              tab->cache.select->cond= tmp;
4979
4778
              tab->cache.select->read_tables= join->const_table_map;
4980
4779
            }
5110
4909
 
5111
4910
    if (tab->insideout_match_tab)
5112
4911
    {
5113
 
      if (! (tab->insideout_buf= (unsigned char*) join->session->getMemRoot()->allocate(tab->table->key_info
 
4912
      if (! (tab->insideout_buf= (unsigned char*) join->session->alloc(tab->table->key_info
5114
4913
                                                                       [tab->index].
5115
4914
                                                                       key_length)))
5116
4915
        return true;
5161
4960
}
5162
4961
 
5163
4962
/** Update the dependency map for the sort order. */
5164
 
static void update_depend_map(Join *join, Order *order)
 
4963
static void update_depend_map(Join *join, order_st *order)
5165
4964
{
5166
4965
  for (; order ; order=order->next)
5167
4966
  {
5199
4998
  @return
5200
4999
    Returns new sort order
5201
5000
*/
5202
 
static Order *remove_constants(Join *join,Order *first_order, COND *cond, bool change_list, bool *simple_order)
 
5001
static order_st *remove_constants(Join *join,order_st *first_order, COND *cond, bool change_list, bool *simple_order)
5203
5002
{
5204
5003
  if (join->tables == join->const_tables)
5205
5004
    return change_list ? 0 : first_order;               // No need to sort
5206
5005
 
5207
 
  Order *order,**prev_ptr;
 
5006
  order_st *order,**prev_ptr;
5208
5007
  table_map first_table= join->join_tab[join->const_tables].table->map;
5209
5008
  table_map not_const_tables= ~join->const_table_map;
5210
5009
  table_map ref;
5292
5091
  {
5293
5092
    if (send_row)
5294
5093
    {
5295
 
      List<Item>::iterator it(fields.begin());
 
5094
      List_iterator_fast<Item> it(fields);
5296
5095
      Item *item;
5297
5096
      while ((item= it++))
5298
5097
        item->no_rows_in_result();
5428
5227
static COND *simplify_joins(Join *join, List<TableList> *join_list, COND *conds, bool top)
5429
5228
{
5430
5229
  TableList *table;
5431
 
  NestedJoin *nested_join;
 
5230
  nested_join_st *nested_join;
5432
5231
  TableList *prev_table= 0;
5433
 
  List<TableList>::iterator li(join_list->begin());
 
5232
  List_iterator<TableList> li(*join_list);
5434
5233
 
5435
5234
  /*
5436
5235
    Try to simplify join operations from join_list.
5566
5365
    Flatten nested joins that can be flattened.
5567
5366
    no ON expression and not a semi-join => can be flattened.
5568
5367
  */
5569
 
  li= join_list->begin();
 
5368
  li.rewind();
5570
5369
  while ((table= li++))
5571
5370
  {
5572
5371
    nested_join= table->getNestedJoin();
5573
5372
    if (nested_join && !table->on_expr)
5574
5373
    {
5575
5374
      TableList *tbl;
5576
 
      List<TableList>::iterator it(nested_join->join_list.begin());
 
5375
      List_iterator<TableList> it(nested_join->join_list);
5577
5376
      while ((tbl= it++))
5578
5377
      {
5579
5378
        tbl->setEmbedding(table->getEmbedding());
5596
5395
 
5597
5396
  /* Calculate how many saved fields there is in list */
5598
5397
  field_count=0;
5599
 
  List<Item>::iterator it(fields.begin());
 
5398
  List_iterator<Item> it(fields);
5600
5399
  Item *item;
5601
5400
  while ((item=it++))
5602
5401
  {
5645
5444
                               List<Item> &fields,
5646
5445
                               List<Item> &all_fields,
5647
5446
                               COND **conds,
5648
 
                               Order *order,
5649
 
                               Order *group,
 
5447
                               order_st *order,
 
5448
                               order_st *group,
5650
5449
                               bool *hidden_group_fields)
5651
5450
{
5652
5451
  int res;
5653
 
  nesting_map save_allow_sum_func=session->getLex()->allow_sum_func ;
 
5452
  nesting_map save_allow_sum_func=session->lex->allow_sum_func ;
5654
5453
 
5655
 
  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);
5656
5455
  res= session->setup_conds(tables, conds);
5657
5456
 
5658
 
  session->getLex()->allow_sum_func|= 1 << session->getLex()->current_select->nest_level;
 
5457
  session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
5659
5458
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
5660
5459
                          order);
5661
 
  session->getLex()->allow_sum_func&= ~(1 << session->getLex()->current_select->nest_level);
 
5460
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
5662
5461
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
5663
5462
                          group, hidden_group_fields);
5664
 
  session->getLex()->allow_sum_func= save_allow_sum_func;
 
5463
  session->lex->allow_sum_func= save_allow_sum_func;
5665
5464
  return(res);
5666
5465
}
5667
5466
 
5701
5500
 
5702
5501
  table_count= join->tables;
5703
5502
  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));
 
5503
  stat_ref= (JoinTable**) join->session->alloc(sizeof(JoinTable*)*MAX_TABLES);
 
5504
  table_vector= (Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
5706
5505
  if (! stat || ! stat_ref || ! table_vector)
5707
5506
    return 1;
5708
5507
 
5766
5565
      s->embedding_map.reset();
5767
5566
      do
5768
5567
      {
5769
 
        NestedJoin *nested_join= embedding->getNestedJoin();
 
5568
        nested_join_st *nested_join= embedding->getNestedJoin();
5770
5569
        s->embedding_map|= nested_join->nj_map;
5771
5570
        s->dependent|= embedding->getDepTables();
5772
5571
        embedding= embedding->getEmbedding();
5795
5594
       As we use bitmaps to represent the relation the complexity
5796
5595
       of the algorithm is O((number of tables)^2).
5797
5596
    */
5798
 
    for (i= 0; i < table_count; i++)
 
5597
    for (i= 0, s= stat ; i < table_count ; i++, s++)
5799
5598
    {
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++)
 
5599
      for (uint32_t j= 0 ; j < table_count ; j++)
5807
5600
      {
 
5601
        table= stat[j].table;
5808
5602
        if (s->dependent & table->map)
5809
 
        {
5810
 
          table_map was_dependent= s->dependent;
5811
5603
          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
5604
      }
 
5605
      if (s->dependent)
 
5606
        s->table->maybe_null= 1;
5819
5607
    }
5820
5608
    /* Catch illegal cross references for outer joins */
5821
5609
    for (i= 0, s= stat ; i < table_count ; i++, s++)
5826
5614
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
5827
5615
        return 1;
5828
5616
      }
5829
 
      if (outer_join & s->table->map)
5830
 
        s->table->maybe_null= 1;
5831
 
 
5832
5617
      s->key_dependent= s->dependent;
5833
5618
    }
5834
5619
  }
5850
5635
    s= p_pos->getJoinTable();
5851
5636
    s->type= AM_SYSTEM;
5852
5637
    join->const_table_map|=s->table->map;
5853
 
    if ((tmp= s->joinReadConstTable(p_pos)))
 
5638
    if ((tmp= join_read_const_table(s, p_pos)))
5854
5639
    {
5855
5640
      if (tmp > 0)
5856
5641
        return 1;                       // Fatal error
5924
5709
          join->const_table_map|=table->map;
5925
5710
          set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
5926
5711
          partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5927
 
          if ((tmp= s->joinReadConstTable(partial_pos)))
 
5712
          if ((tmp= join_read_const_table(s, partial_pos)))
5928
5713
          {
5929
5714
            if (tmp > 0)
5930
5715
              return 1;                 // Fatal error
5976
5761
                if (create_ref_for_key(join, s, start_keyuse, found_const_table_map))
5977
5762
                  return 1;
5978
5763
                partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5979
 
                if ((tmp=s->joinReadConstTable(partial_pos)))
 
5764
                if ((tmp=join_read_const_table(s, partial_pos)))
5980
5765
                {
5981
5766
                  if (tmp > 0)
5982
5767
                    return 1;                   // Fatal error
6060
5845
      s->quick=select->quick;
6061
5846
      s->needed_reg=select->needed_reg;
6062
5847
      select->quick=0;
6063
 
 
6064
5848
      if (records == 0 && s->table->reginfo.impossible_range)
6065
5849
      {
6066
5850
        /*
6100
5884
  if (join->const_tables != join->tables)
6101
5885
  {
6102
5886
    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);
 
5887
    DRIZZLE_QUERY_OPT_CHOOSE_PLAN_START(join->session->query.c_str(), join->session->thread_id);
6106
5888
    bool res= choose_plan(join, all_table_map & ~join->const_table_map);
6107
5889
    DRIZZLE_QUERY_OPT_CHOOSE_PLAN_DONE(res ? 1 : 0);
6108
5890
    if (res)
6114
5896
    join->best_read= 1.0;
6115
5897
  }
6116
5898
  /* Generate an execution plan from the found optimal join order. */
6117
 
  return (join->session->getKilled() || get_best_combination(join));
 
5899
  return (join->session->killed || get_best_combination(join));
6118
5900
}
6119
5901
 
6120
5902
/**
6138
5920
*/
6139
5921
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused)
6140
5922
{
6141
 
  List<TableList>::iterator li(join_list->begin());
 
5923
  List_iterator<TableList> li(*join_list);
6142
5924
  TableList *table;
6143
5925
  while ((table= li++))
6144
5926
  {
6145
 
    NestedJoin *nested_join;
 
5927
    nested_join_st *nested_join;
6146
5928
    if ((nested_join= table->getNestedJoin()))
6147
5929
    {
6148
5930
      /*
6174
5956
  Return table number if there is only one table in sort order
6175
5957
  and group and order is compatible, else return 0.
6176
5958
*/
6177
 
static Table *get_sort_by_table(Order *a, Order *b,TableList *tables)
 
5959
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables)
6178
5960
{
6179
5961
  table_map map= (table_map) 0;
6180
5962
 
6199
5981
}
6200
5982
 
6201
5983
/**
6202
 
  Set NestedJoin::counter=0 in all nested joins in passed list.
 
5984
  Set nested_join_st::counter=0 in all nested joins in passed list.
6203
5985
 
6204
 
    Recursively set NestedJoin::counter=0 for all nested joins contained in
 
5986
    Recursively set nested_join_st::counter=0 for all nested joins contained in
6205
5987
    the passed join_list.
6206
5988
 
6207
5989
  @param join_list  List of nested joins to process. It may also contain base
6209
5991
*/
6210
5992
static void reset_nj_counters(List<TableList> *join_list)
6211
5993
{
6212
 
  List<TableList>::iterator li(join_list->begin());
 
5994
  List_iterator<TableList> li(*join_list);
6213
5995
  TableList *table;
6214
5996
  while ((table= li++))
6215
5997
  {
6216
 
    NestedJoin *nested_join;
 
5998
    nested_join_st *nested_join;
6217
5999
    if ((nested_join= table->getNestedJoin()))
6218
6000
    {
6219
6001
      nested_join->counter_= 0;
6229
6011
  If first parts has different direction, change it to second part
6230
6012
  (group is sorted like order)
6231
6013
*/
6232
 
static bool test_if_subpart(Order *a, Order *b)
 
6014
static bool test_if_subpart(order_st *a,order_st *b)
6233
6015
{
6234
6016
  for (; a && b; a=a->next,b=b->next)
6235
6017
  {
6299
6081
  Join *join= last->join;
6300
6082
  for (;last_emb != NULL; last_emb= last_emb->getEmbedding())
6301
6083
  {
6302
 
    NestedJoin *nest= last_emb->getNestedJoin();
 
6084
    nested_join_st *nest= last_emb->getNestedJoin();
6303
6085
    
6304
6086
    bool was_fully_covered= nest->is_fully_covered();
6305
6087