~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/sql_select.cc

Removed DBUG symbols and fixed TRUE/FALSE

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
  @defgroup Query_Optimizer  Query Optimizer
24
24
  @{
25
25
*/
26
 
#include <drizzled/server_includes.h>
27
 
#include <drizzled/sql_select.h>
28
 
#include "sj_tmp_table.h"
29
 
 
30
 
#include <mysys/my_bit.h>
31
 
#include <drizzled/drizzled_error_messages.h>
32
 
#include <libdrizzle/gettext.h>
33
 
#include <drizzled/util/test.h>
 
26
 
 
27
#ifdef USE_PRAGMA_IMPLEMENTATION
 
28
#pragma implementation                          // gcc: Class implementation
 
29
#endif
 
30
 
 
31
#include "mysql_priv.h"
 
32
#include "sql_select.h"
 
33
 
 
34
#include <m_ctype.h>
 
35
#include <my_bit.h>
 
36
#include <hash.h>
34
37
 
35
38
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
36
39
                              "MAYBE_REF","ALL","range","index",
41
44
struct st_sargable_param;
42
45
 
43
46
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
44
 
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds,
 
47
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
45
48
                                 DYNAMIC_ARRAY *keyuse);
46
49
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
47
50
                                JOIN_TAB *join_tab,
48
 
                                uint32_t tables, COND *conds,
 
51
                                uint tables, COND *conds,
49
52
                                COND_EQUAL *cond_equal,
50
53
                                table_map table_map, SELECT_LEX *select_lex,
51
54
                                st_sargable_param **sargables);
52
55
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
53
 
static void set_position(JOIN *join,uint32_t index,JOIN_TAB *table,KEYUSE *key);
 
56
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
54
57
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
55
58
                               table_map used_tables);
56
59
static bool choose_plan(JOIN *join,table_map join_tables);
57
60
 
58
61
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
59
 
                             table_map remaining_tables, uint32_t idx,
 
62
                             table_map remaining_tables, uint idx,
60
63
                             double record_count, double read_time);
61
64
static void optimize_straight_join(JOIN *join, table_map join_tables);
62
65
static bool greedy_search(JOIN *join, table_map remaining_tables,
63
 
                             uint32_t depth, uint32_t prune_level);
 
66
                             uint depth, uint prune_level);
64
67
static bool best_extension_by_limited_search(JOIN *join,
65
68
                                             table_map remaining_tables,
66
 
                                             uint32_t idx, double record_count,
67
 
                                             double read_time, uint32_t depth,
68
 
                                             uint32_t prune_level);
69
 
static uint32_t determine_search_depth(JOIN* join);
 
69
                                             uint idx, double record_count,
 
70
                                             double read_time, uint depth,
 
71
                                             uint prune_level);
 
72
static uint determine_search_depth(JOIN* join);
70
73
static int join_tab_cmp(const void* ptr1, const void* ptr2);
71
74
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
72
75
/*
73
76
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
74
77
  tested and approved.
75
78
*/
76
 
static bool find_best(JOIN *join,table_map rest_tables,uint32_t index,
 
79
static bool find_best(JOIN *join,table_map rest_tables,uint index,
77
80
                      double record_count,double read_time);
78
 
static uint32_t cache_record_length(JOIN *join,uint32_t index);
79
 
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
 
81
static uint cache_record_length(JOIN *join,uint index);
 
82
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
80
83
static bool get_best_combination(JOIN *join);
81
84
static store_key *get_store_key(THD *thd,
82
85
                                KEYUSE *keyuse, table_map used_tables,
83
 
                                KEY_PART_INFO *key_part, unsigned char *key_buff,
84
 
                                uint32_t maybe_null);
85
 
static bool make_simple_join(JOIN *join,Table *tmp_table);
 
86
                                KEY_PART_INFO *key_part, uchar *key_buff,
 
87
                                uint maybe_null);
 
88
static bool make_simple_join(JOIN *join,TABLE *tmp_table);
86
89
static void make_outerjoin_info(JOIN *join);
87
90
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
88
 
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
89
 
static bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
 
91
static bool make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after);
 
92
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables);
90
93
static void update_depend_map(JOIN *join);
91
 
static void update_depend_map(JOIN *join, order_st *order);
92
 
static order_st *remove_const(JOIN *join,order_st *first_order,COND *cond,
 
94
static void update_depend_map(JOIN *join, ORDER *order);
 
95
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond,
93
96
                           bool change_list, bool *simple_order);
94
 
static int return_zero_rows(JOIN *join, select_result *res,TableList *tables,
 
97
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables,
95
98
                            List<Item> &fields, bool send_row,
96
 
                            uint64_t select_options, const char *info,
 
99
                            ulonglong select_options, const char *info,
97
100
                            Item *having);
98
101
static COND *build_equal_items(THD *thd, COND *cond,
99
102
                               COND_EQUAL *inherited,
100
 
                               List<TableList> *join_list,
 
103
                               List<TABLE_LIST> *join_list,
101
104
                               COND_EQUAL **cond_equal_ref);
102
105
static COND* substitute_for_best_equal_field(COND *cond,
103
106
                                             COND_EQUAL *cond_equal,
104
107
                                             void *table_join_idx);
105
 
static COND *simplify_joins(JOIN *join, List<TableList> *join_list,
 
108
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
106
109
                            COND *conds, bool top, bool in_sj);
107
110
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
108
111
static void restore_prev_nj_state(JOIN_TAB *last);
109
 
static void reset_nj_counters(List<TableList> *join_list);
110
 
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
111
 
                                          uint32_t first_unused);
 
112
static void reset_nj_counters(List<TABLE_LIST> *join_list);
 
113
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
 
114
                                          uint first_unused);
112
115
 
113
116
static 
114
117
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
116
119
                                  const JOIN_TAB *tab);
117
120
 
118
121
static COND *optimize_cond(JOIN *join, COND *conds,
119
 
                           List<TableList> *join_list,
 
122
                           List<TABLE_LIST> *join_list,
120
123
                           Item::cond_result *cond_value);
121
124
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
122
 
static int do_select(JOIN *join,List<Item> *fields,Table *tmp_table);
 
125
static bool open_tmp_table(TABLE *table);
 
126
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
 
127
                                    MI_COLUMNDEF *start_recinfo,
 
128
                                    MI_COLUMNDEF **recinfo,
 
129
                                    ulonglong options);
 
130
static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table);
123
131
 
124
132
static enum_nested_loop_state
125
133
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
160
168
static COND *make_cond_for_table(COND *cond,table_map table,
161
169
                                 table_map used_table,
162
170
                                 bool exclude_expensive_cond);
163
 
static Item* part_of_refkey(Table *form,Field *field);
164
 
static bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,
 
171
static Item* part_of_refkey(TABLE *form,Field *field);
 
172
uint find_shortest_key(TABLE *table, const key_map *usable_keys);
 
173
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
165
174
                                    ha_rows select_limit, bool no_changes,
166
175
                                    const key_map *map);
167
 
static bool list_contains_unique_index(Table *table,
 
176
static bool list_contains_unique_index(TABLE *table,
168
177
                          bool (*find_func) (Field *, void *), void *data);
169
178
static bool find_field_in_item_list (Field *field, void *data);
170
179
static bool find_field_in_order_list (Field *field, void *data);
171
 
static int create_sort_index(THD *thd, JOIN *join, order_st *order,
 
180
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
172
181
                             ha_rows filesort_limit, ha_rows select_limit,
173
182
                             bool is_order_by);
174
 
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields,
 
183
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
175
184
                             Item *having);
176
 
static int remove_dup_with_compare(THD *thd, Table *entry, Field **field,
 
185
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
177
186
                                   ulong offset,Item *having);
178
 
static int remove_dup_with_hash_index(THD *thd,Table *table,
179
 
                                      uint32_t field_count, Field **first_field,
 
187
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
 
188
                                      uint field_count, Field **first_field,
180
189
 
181
190
                                      ulong key_length,Item *having);
182
 
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint32_t table_count);
 
191
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
183
192
static ulong used_blob_length(CACHE_FIELD **ptr);
184
193
static bool store_record_in_cache(JOIN_CACHE *cache);
185
194
static void reset_cache_read(JOIN_CACHE *cache);
186
195
static void reset_cache_write(JOIN_CACHE *cache);
187
196
static void read_cached_record(JOIN_TAB *tab);
188
197
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
189
 
static order_st *create_distinct_group(THD *thd, Item **ref_pointer_array,
190
 
                                    order_st *order, List<Item> &fields,
 
198
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
 
199
                                    ORDER *order, List<Item> &fields,
191
200
                                    List<Item> &all_fields,
192
201
                                    bool *all_order_by_fields_used);
193
 
static bool test_if_subpart(order_st *a,order_st *b);
194
 
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
195
 
static void calc_group_buffer(JOIN *join,order_st *group);
 
202
static bool test_if_subpart(ORDER *a,ORDER *b);
 
203
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
 
204
static void calc_group_buffer(JOIN *join,ORDER *group);
196
205
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
197
 
static bool alloc_group_fields(JOIN *join,order_st *group);
 
206
static bool alloc_group_fields(JOIN *join,ORDER *group);
198
207
// Create list for using with tempory table
199
208
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
200
209
                                     List<Item> &new_list1,
201
210
                                     List<Item> &new_list2,
202
 
                                     uint32_t elements, List<Item> &items);
 
211
                                     uint elements, List<Item> &items);
203
212
// Create list for using with tempory table
204
213
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
205
214
                                      List<Item> &new_list1,
206
215
                                      List<Item> &new_list2,
207
 
                                      uint32_t elements, List<Item> &items);
 
216
                                      uint elements, List<Item> &items);
208
217
static void init_tmptable_sum_functions(Item_sum **func);
209
 
static void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
 
218
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
210
219
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
211
220
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
212
221
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
213
222
static bool init_sum_functions(Item_sum **func, Item_sum **end);
214
223
static bool update_sum_func(Item_sum **func);
215
224
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
216
 
                            bool distinct, const char *message=NULL);
 
225
                            bool distinct, const char *message=NullS);
217
226
static Item *remove_additional_cond(Item* conds);
218
227
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
219
228
static bool test_if_ref(Item_field *left_item,Item *right_item);
242
251
{
243
252
  bool res;
244
253
  register SELECT_LEX *select_lex = &lex->select_lex;
245
 
  DRIZZLE_SELECT_START();
 
254
  DBUG_ENTER("handle_select");
 
255
  MYSQL_SELECT_START();
246
256
 
247
257
  if (select_lex->master_unit()->is_union() || 
248
258
      select_lex->master_unit()->fake_select_lex)
258
268
      setup_tables_done_option changed for next rexecution
259
269
    */
260
270
    res= mysql_select(thd, &select_lex->ref_pointer_array,
261
 
                      (TableList*) select_lex->table_list.first,
 
271
                      (TABLE_LIST*) select_lex->table_list.first,
262
272
                      select_lex->with_wild, select_lex->item_list,
263
273
                      select_lex->where,
264
274
                      select_lex->order_list.elements +
265
275
                      select_lex->group_list.elements,
266
 
                      (order_st*) select_lex->order_list.first,
267
 
                      (order_st*) select_lex->group_list.first,
 
276
                      (ORDER*) select_lex->order_list.first,
 
277
                      (ORDER*) select_lex->group_list.first,
268
278
                      select_lex->having,
269
 
                      (order_st*) lex->proc_list.first,
 
279
                      (ORDER*) lex->proc_list.first,
270
280
                      select_lex->options | thd->options |
271
281
                      setup_tables_done_option,
272
282
                      result, unit, select_lex);
273
283
  }
 
284
  DBUG_PRINT("info",("res: %d  report_error: %d", res,
 
285
                     thd->is_error()));
274
286
  res|= thd->is_error();
275
287
  if (unlikely(res))
276
288
    result->abort();
277
289
 
278
 
  DRIZZLE_SELECT_END();
279
 
  return(res);
 
290
  MYSQL_SELECT_END();
 
291
  DBUG_RETURN(res);
280
292
}
281
293
 
282
294
 
395
407
  Function to setup clauses without sum functions.
396
408
*/
397
409
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
398
 
                               TableList *tables,
399
 
                               TableList *leaves,
 
410
                               TABLE_LIST *tables,
 
411
                               TABLE_LIST *leaves,
400
412
                               List<Item> &fields,
401
413
                               List<Item> &all_fields,
402
414
                               COND **conds,
403
 
                               order_st *order,
404
 
                               order_st *group, bool *hidden_group_fields)
 
415
                               ORDER *order,
 
416
                               ORDER *group, bool *hidden_group_fields)
405
417
{
406
418
  int res;
407
419
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
 
420
  DBUG_ENTER("setup_without_group");
408
421
 
409
422
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
410
423
  res= setup_conds(thd, tables, leaves, conds);
416
429
  res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
417
430
                          group, hidden_group_fields);
418
431
  thd->lex->allow_sum_func= save_allow_sum_func;
419
 
  return(res);
 
432
  DBUG_RETURN(res);
420
433
}
421
434
 
422
435
/*****************************************************************************
438
451
*/
439
452
int
440
453
JOIN::prepare(Item ***rref_pointer_array,
441
 
              TableList *tables_init,
442
 
              uint32_t wild_num, COND *conds_init, uint32_t og_num,
443
 
              order_st *order_init, order_st *group_init,
 
454
              TABLE_LIST *tables_init,
 
455
              uint wild_num, COND *conds_init, uint og_num,
 
456
              ORDER *order_init, ORDER *group_init,
444
457
              Item *having_init,
445
 
              order_st *proc_param_init, SELECT_LEX *select_lex_arg,
 
458
              ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
446
459
              SELECT_LEX_UNIT *unit_arg)
447
460
{
 
461
  DBUG_ENTER("JOIN::prepare");
 
462
 
448
463
  // to prevent double initialization on EXPLAIN
449
464
  if (optimized)
450
 
    return(0);
 
465
    DBUG_RETURN(0);
451
466
 
452
467
  conds= conds_init;
453
468
  order= order_init;
474
489
      setup_tables_and_check_access(thd, &select_lex->context, join_list,
475
490
                                    tables_list, &select_lex->leaf_tables,
476
491
                                    false))
477
 
      return(-1);
 
492
      DBUG_RETURN(-1);
478
493
 
479
 
  TableList *table_ptr;
 
494
  TABLE_LIST *table_ptr;
480
495
  for (table_ptr= select_lex->leaf_tables;
481
496
       table_ptr;
482
497
       table_ptr= table_ptr->next_leaf)
490
505
                          select_lex->leaf_tables, fields_list,
491
506
                          all_fields, &conds, order, group_list,
492
507
                          &hidden_group_fields))
493
 
    return(-1);                         /* purecov: inspected */
 
508
    DBUG_RETURN(-1);                            /* purecov: inspected */
494
509
 
495
510
  ref_pointer_array= *rref_pointer_array;
496
511
  
505
520
                          having->check_cols(1)));
506
521
    select_lex->having_fix_field= 0;
507
522
    if (having_fix_rc || thd->is_error())
508
 
      return(-1);                               /* purecov: inspected */
 
523
      DBUG_RETURN(-1);                          /* purecov: inspected */
509
524
    thd->lex->allow_sum_func= save_allow_sum_func;
510
525
  }
511
526
 
 
527
  if (!thd->lex->view_prepare_mode)
512
528
  {
513
529
    Item_subselect *subselect;
514
530
    Item_in_subselect *in_subs= NULL;
523
539
      if (subselect->substype() == Item_subselect::IN_SUBS)
524
540
        in_subs= (Item_in_subselect*)subselect;
525
541
 
 
542
      DBUG_PRINT("info", ("Checking if subq can be converted to semi-join"));
526
543
      /*
527
544
        Check if we're in subquery that is a candidate for flattening into a
528
545
        semi-join (which is done done in flatten_subqueries()). The
529
546
        requirements are:
530
547
          1. Subquery predicate is an IN/=ANY subq predicate
531
548
          2. Subquery is a single SELECT (not a UNION)
532
 
          3. Subquery does not have GROUP BY or order_st BY
 
549
          3. Subquery does not have GROUP BY or ORDER BY
533
550
          4. Subquery does not use aggregate functions or HAVING
534
551
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
535
552
          6. No execution method was already chosen (by a prepared statement).
551
568
          do_semijoin &&
552
569
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
553
570
      {
 
571
        DBUG_PRINT("info", ("Subquery is semi-join conversion candidate"));
554
572
        {
555
573
          if (!in_subs->left_expr->fixed &&
556
574
               in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
557
575
          {
558
 
            return(-1);
 
576
            DBUG_RETURN(-1);
559
577
          }
560
578
          /*
561
579
            Check that the right part of the subselect contains no more than one
566
584
              ((Item_in_subselect*)subselect)->left_expr->cols()))
567
585
          {
568
586
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
569
 
            return(-1);
 
587
            DBUG_RETURN(-1);
570
588
          }
571
589
        }
572
590
 
573
591
        /* Register the subquery for further processing */
574
592
        select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
575
 
        in_subs->expr_join_nest= (TableList*)thd->thd_marker;
 
593
        in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
576
594
      }
577
595
      else
578
596
      {
 
597
        DBUG_PRINT("info", ("Subquery can't be converted to semi-join"));
579
598
        bool do_materialize= !test(thd->variables.optimizer_switch &
580
599
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
581
600
        /*
595
614
             (Subquery is non-correlated ||
596
615
              Subquery is correlated to any query outer to IN predicate ||
597
616
              (Subquery is correlated to the immediate outer query &&
598
 
               Subquery !contains {GROUP BY, order_st BY [LIMIT],
 
617
               Subquery !contains {GROUP BY, ORDER BY [LIMIT],
599
618
               aggregate functions) && subquery predicate is not under "NOT IN"))
600
619
          6. No execution method was already chosen (by a prepared statement).
601
620
 
623
642
        if ((trans_res= subselect->select_transformer(this)) !=
624
643
            Item_subselect::RES_OK)
625
644
        {
626
 
          return((trans_res == Item_subselect::RES_ERROR));
 
645
          select_lex->fix_prepare_information(thd, &conds, &having);
 
646
          DBUG_RETURN((trans_res == Item_subselect::RES_ERROR));
627
647
        }
628
648
      }
629
649
    }
630
650
  }
631
651
 
 
652
  select_lex->fix_prepare_information(thd, &conds, &having);
 
653
 
632
654
  if (order)
633
655
  {
634
 
    order_st *ord;
 
656
    ORDER *ord;
635
657
    for (ord= order; ord; ord= ord->next)
636
658
    {
637
659
      Item *item= *ord->item;
657
679
 
658
680
  if (select_lex->inner_refs_list.elements &&
659
681
      fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
660
 
    return(-1);
 
682
    DBUG_RETURN(-1);
661
683
 
662
684
  /*
663
685
    Check if there are references to un-aggregated columns when computing 
669
691
  {
670
692
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
671
693
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
672
 
    return(-1);
 
694
    DBUG_RETURN(-1);
673
695
  }
674
696
  {
675
697
    /* Caclulate the number of groups */
676
698
    send_group_parts= 0;
677
 
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
699
    for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
678
700
      send_group_parts++;
679
701
  }
680
702
  
702
724
  if (alloc_func_list())
703
725
    goto err;
704
726
 
705
 
  return(0); // All OK
 
727
  DBUG_RETURN(0); // All OK
706
728
 
707
729
err:
708
 
  return(-1);                           /* purecov: inspected */
 
730
  DBUG_RETURN(-1);                              /* purecov: inspected */
709
731
}
710
732
 
711
733
 
780
802
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
781
803
  if (where)
782
804
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
783
 
  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
 
805
  for (uint i = 0; i < join_tab->ref.key_parts; i++)
784
806
  {
785
807
    if (join_tab->ref.cond_guards[i])
786
808
    {
826
848
  /* Check if this table is functionally dependent on the tables that
827
849
     are within the same outer join nest
828
850
  */
829
 
  TableList *embedding= join_tab->table->pos_in_table_list->embedding;
 
851
  TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
830
852
  if (join_tab->type == JT_EQ_REF)
831
853
  {
832
854
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
833
 
    uint32_t idx;
 
855
    uint idx;
834
856
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
835
857
    {
836
858
      JOIN_TAB *ref_tab= join->join_tab + idx;
845
867
}
846
868
 
847
869
 
 
870
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg,
 
871
                                          SJ_TMP_TABLE *sjtbl);
 
872
 
 
873
 
848
874
/*
849
875
  Setup the strategies to eliminate semi-join duplicates.
850
876
  
942
968
*/
943
969
 
944
970
static
945
 
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
971
int setup_semijoin_dups_elimination(JOIN *join, ulonglong options, uint no_jbuf_after)
946
972
{
947
973
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
948
974
  struct {
952
978
      2 - Temptable (maybe confluent),
953
979
      3 - Temptable with join buffering
954
980
    */
955
 
    uint32_t strategy;
956
 
    uint32_t start_idx; /* Left range bound */
957
 
    uint32_t end_idx;   /* Right range bound */
 
981
    uint strategy;
 
982
    uint start_idx; /* Left range bound */
 
983
    uint end_idx;   /* Right range bound */
958
984
    /* 
959
985
      For Temptable strategy: Bitmap of all outer and correlated tables from 
960
986
      all involved join nests.
962
988
    table_map outer_tables;
963
989
  } dups_ranges [MAX_TABLES];
964
990
 
965
 
  TableList *emb_insideout_nest= NULL;
 
991
  TABLE_LIST *emb_insideout_nest= NULL;
966
992
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
967
993
                               tables) whose ranges we're in */
968
994
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
969
995
  table_map range_start_map= 0; /* table_map at current range start */
970
996
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
971
997
  int cur_range= 0;
972
 
  uint32_t i;
 
998
  uint i;
973
999
 
 
1000
  DBUG_ENTER("setup_semijoin_dups_elimination");
974
1001
  /*
975
1002
    First pass: locate the duplicate-generating ranges and pick the strategies.
976
1003
  */
977
1004
  for (i=join->const_tables ; i < join->tables ; i++)
978
1005
  {
979
1006
    JOIN_TAB *tab=join->join_tab+i;
980
 
    Table *table=tab->table;
 
1007
    TABLE *table=tab->table;
981
1008
    cur_map |= table->map;
982
1009
 
983
1010
    if (tab->emb_sj_nest) // Encountered an sj-inner table
1110
1137
    {
1111
1138
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
1112
1139
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
1113
 
      uint32_t jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
1114
 
      uint32_t jt_null_bits= 0;    // # null bits in tuple bytes
 
1140
      uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
1141
      uint jt_null_bits= 0;    // # null bits in tuple bytes
1115
1142
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
1116
 
      uint32_t rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1143
      uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
1117
1144
      JOIN_TAB *last_outer_tab= tab - 1;
1118
1145
      /*
1119
1146
        Walk through the range and remember
1145
1172
      if (jt_rowid_offset) /* Temptable has at least one rowid */
1146
1173
      {
1147
1174
        SJ_TMP_TABLE *sjtbl;
1148
 
        uint32_t tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1175
        uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1149
1176
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
1150
1177
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
1151
 
          return(true);
 
1178
          DBUG_RETURN(true);
1152
1179
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
1153
1180
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
1154
1181
        sjtbl->rowid_len= jt_rowid_offset;
1181
1208
        jump_to= tab;
1182
1209
    }
1183
1210
  }
1184
 
  return(false);
 
1211
  DBUG_RETURN(false);
1185
1212
}
1186
1213
 
1187
1214
 
1192
1219
  {
1193
1220
    if (sj_tbl->tmp_table)
1194
1221
    {
1195
 
      sj_tbl->tmp_table->free_tmp_table(join->thd);
 
1222
      free_tmp_table(join->thd, sj_tbl->tmp_table);
1196
1223
    }
1197
1224
  }
1198
1225
  join->sj_tmp_tables= NULL;
1199
1226
}
1200
1227
 
1201
 
uint32_t make_join_orderinfo(JOIN *join);
 
1228
uint make_join_orderinfo(JOIN *join);
1202
1229
 
1203
1230
/**
1204
1231
  global select optimisation.
1215
1242
int
1216
1243
JOIN::optimize()
1217
1244
{
 
1245
  DBUG_ENTER("JOIN::optimize");
1218
1246
  // to prevent double initialization on EXPLAIN
1219
1247
  if (optimized)
1220
 
    return(0);
 
1248
    DBUG_RETURN(0);
1221
1249
  optimized= 1;
1222
1250
 
1223
 
  thd->set_proc_info("optimizing");
 
1251
  thd_proc_info(thd, "optimizing");
1224
1252
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
1225
1253
              unit->select_limit_cnt);
1226
1254
  /* select_limit is used to decide if we are likely to scan the whole table */
1254
1282
    }
1255
1283
  }
1256
1284
#endif
1257
 
 
1258
 
  /* Convert all outer joins to inner joins if possible */
1259
 
  conds= simplify_joins(this, join_list, conds, true, false);
1260
 
  build_bitmap_for_nested_joins(join_list, 0);
 
1285
  SELECT_LEX *sel= thd->lex->current_select;
 
1286
  if (sel->first_cond_optimization)
 
1287
  {
 
1288
    /*
 
1289
      The following code will allocate the new items in a permanent
 
1290
      MEMROOT for prepared statements and stored procedures.
 
1291
    */
 
1292
    sel->first_cond_optimization= 0;
 
1293
 
 
1294
    /* Convert all outer joins to inner joins if possible */
 
1295
    conds= simplify_joins(this, join_list, conds, true, false);
 
1296
    build_bitmap_for_nested_joins(join_list, 0);
 
1297
 
 
1298
    sel->prep_where= conds ? conds->copy_andor_structure(thd) : 0;
 
1299
  }
1261
1300
 
1262
1301
  conds= optimize_cond(this, conds, join_list, &cond_value);   
1263
1302
  if (thd->is_error())
1264
1303
  {
1265
1304
    error= 1;
1266
 
    return(1);
 
1305
    DBUG_PRINT("error",("Error from optimize_cond"));
 
1306
    DBUG_RETURN(1);
1267
1307
  }
1268
1308
 
1269
1309
  {
1271
1311
    if (thd->is_error())
1272
1312
    {
1273
1313
      error= 1;
1274
 
      return(1);
 
1314
      DBUG_PRINT("error",("Error from optimize_cond"));
 
1315
      DBUG_RETURN(1);
1275
1316
    }
1276
1317
    if (select_lex->where)
1277
1318
      select_lex->cond_value= cond_value;
1281
1322
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 
1282
1323
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
1283
1324
    {                                           /* Impossible cond */
 
1325
      DBUG_PRINT("info", (having_value == Item::COND_FALSE ? 
 
1326
                            "Impossible HAVING" : "Impossible WHERE"));
1284
1327
      zero_result_cause=  having_value == Item::COND_FALSE ?
1285
1328
                           "Impossible HAVING" : "Impossible WHERE";
1286
1329
      error= 0;
1287
 
      return(0);
 
1330
      DBUG_RETURN(0);
1288
1331
    }
1289
1332
  }
1290
1333
 
1291
 
  /* Optimize count(*), cmin() and cmax() */
 
1334
  /* Optimize count(*), min() and max() */
1292
1335
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
1293
1336
  {
1294
1337
    int res;
1302
1345
    {
1303
1346
      if (res == HA_ERR_KEY_NOT_FOUND)
1304
1347
      {
 
1348
        DBUG_PRINT("info",("No matching min/max row"));
1305
1349
        zero_result_cause= "No matching min/max row";
1306
1350
        error=0;
1307
 
        return(0);
 
1351
        DBUG_RETURN(0);
1308
1352
      }
1309
1353
      if (res > 1)
1310
1354
      {
1311
1355
        error= res;
1312
 
        return(1);
 
1356
        DBUG_PRINT("error",("Error from opt_sum_query"));
 
1357
        DBUG_RETURN(1);
1313
1358
      }
1314
1359
      if (res < 0)
1315
1360
      {
 
1361
        DBUG_PRINT("info",("No matching min/max row"));
1316
1362
        zero_result_cause= "No matching min/max row";
1317
1363
        error=0;
1318
 
        return(0);
 
1364
        DBUG_RETURN(0);
1319
1365
      }
 
1366
      DBUG_PRINT("info",("Select tables optimized away"));
1320
1367
      zero_result_cause= "Select tables optimized away";
1321
1368
      tables_list= 0;                           // All tables resolved
1322
1369
      /*
1333
1380
      {
1334
1381
        COND *table_independent_conds=
1335
1382
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
1383
        DBUG_EXECUTE("where",
 
1384
                     print_where(table_independent_conds,
 
1385
                                 "where after opt_sum_query()",
 
1386
                                 QT_ORDINARY););
1336
1387
        conds= table_independent_conds;
1337
1388
      }
1338
1389
    }
1339
1390
  }
1340
1391
  if (!tables_list)
1341
1392
  {
 
1393
    DBUG_PRINT("info",("No tables"));
1342
1394
    error= 0;
1343
 
    return(0);
 
1395
    DBUG_RETURN(0);
1344
1396
  }
1345
1397
  error= -1;                                    // Error is sent to client
1346
1398
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
1347
1399
 
1348
1400
  /* Calculate how to do the join */
1349
 
  thd->set_proc_info("statistics");
 
1401
  thd_proc_info(thd, "statistics");
1350
1402
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
1351
1403
      thd->is_fatal_error)
1352
1404
  {
1353
 
    return(1);
 
1405
    DBUG_PRINT("error",("Error: make_join_statistics() failed"));
 
1406
    DBUG_RETURN(1);
1354
1407
  }
1355
1408
 
1356
1409
  /* Remove distinct if only const tables */
1357
1410
  select_distinct= select_distinct && (const_tables != tables);
1358
 
  thd->set_proc_info("preparing");
 
1411
  thd_proc_info(thd, "preparing");
1359
1412
  if (result->initialize_tables(this))
1360
1413
  {
1361
 
    return(1);                          // error == -1
 
1414
    DBUG_PRINT("error",("Error: initialize_tables() failed"));
 
1415
    DBUG_RETURN(1);                             // error == -1
1362
1416
  }
1363
1417
  if (const_table_map != found_const_table_map &&
1364
1418
      !(select_options & SELECT_DESCRIBE) &&
1367
1421
       select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
1368
1422
  {
1369
1423
    zero_result_cause= "no matching row in const table";
 
1424
    DBUG_PRINT("error",("Error: %s", zero_result_cause));
1370
1425
    error= 0;
1371
 
    return(0);
 
1426
    DBUG_RETURN(0);
1372
1427
  }
1373
1428
  if (!(thd->options & OPTION_BIG_SELECTS) &&
1374
1429
      best_read > (double) thd->variables.max_join_size &&
1376
1431
  {                                             /* purecov: inspected */
1377
1432
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
1378
1433
    error= -1;
1379
 
    return(1);
 
1434
    DBUG_RETURN(1);
1380
1435
  }
1381
1436
  if (const_tables && !thd->locked_tables &&
1382
1437
      !(select_options & SELECT_NO_UNLOCK))
1384
1439
  if (!conds && outer_join)
1385
1440
  {
1386
1441
    /* Handle the case where we have an OUTER JOIN without a WHERE */
1387
 
    conds=new Item_int((int64_t) 1,1);  // Always true
 
1442
    conds=new Item_int((longlong) 1,1); // Always true
1388
1443
  }
1389
1444
  select= make_select(*table, const_table_map,
1390
1445
                      const_table_map, conds, 1, &error);
1391
1446
  if (error)
1392
1447
  {                                             /* purecov: inspected */
1393
1448
    error= -1;                                  /* purecov: inspected */
1394
 
    return(1);
 
1449
    DBUG_PRINT("error",("Error: make_select() failed"));
 
1450
    DBUG_RETURN(1);
1395
1451
  }
1396
1452
  
1397
1453
  reset_nj_counters(join_list);
1407
1463
  {
1408
1464
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
1409
1465
    conds->update_used_tables();
 
1466
    DBUG_EXECUTE("where",
 
1467
                 print_where(conds,
 
1468
                             "after substitute_best_equal",
 
1469
                             QT_ORDINARY););
1410
1470
  }
1411
1471
 
1412
1472
  /*
1428
1488
      (select_options & SELECT_DESCRIBE) &&
1429
1489
      select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
1430
1490
  {
1431
 
    conds=new Item_int((int64_t) 0,1);  // Always false
 
1491
    conds=new Item_int((longlong) 0,1); // Always false
1432
1492
  }
1433
1493
  if (make_join_select(this, select, conds))
1434
1494
  {
1435
1495
    zero_result_cause=
1436
1496
      "Impossible WHERE noticed after reading const tables";
1437
 
    return(0);                          // error == 0
 
1497
    DBUG_RETURN(0);                             // error == 0
1438
1498
  }
1439
1499
 
1440
1500
  error= -1;                                    /* if goto err */
1441
1501
 
1442
1502
  /* Optimize distinct away if possible */
1443
1503
  {
1444
 
    order_st *org_order= order;
 
1504
    ORDER *org_order= order;
1445
1505
    order=remove_const(this, order,conds,1, &simple_order);
1446
1506
    if (thd->is_error())
1447
1507
    {
1448
1508
      error= 1;
1449
 
      return(1);
 
1509
      DBUG_PRINT("error",("Error from remove_const"));
 
1510
      DBUG_RETURN(1);
1450
1511
    }
1451
1512
 
1452
1513
    /*
1453
 
      If we are using order_st BY NULL or order_st BY const_expression,
 
1514
      If we are using ORDER BY NULL or ORDER BY const_expression,
1454
1515
      return result in any order (even if we are using a GROUP BY)
1455
1516
    */
1456
1517
    if (!order && org_order)
1484
1545
        We have found that grouping can be removed since groups correspond to
1485
1546
        only one row anyway, but we still have to guarantee correct result
1486
1547
        order. The line below effectively rewrites the query from GROUP BY
1487
 
        <fields> to order_st BY <fields>. There are two exceptions:
 
1548
        <fields> to ORDER BY <fields>. There are two exceptions:
1488
1549
        - if skip_sort_order is set (see above), then we can simply skip
1489
1550
          GROUP BY;
1490
 
        - we can only rewrite order_st BY if the order_st BY fields are 'compatible'
 
1551
        - we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
1491
1552
          with the GROUP BY ones, i.e. either one is a prefix of another.
1492
 
          We only check if the order_st BY is a prefix of GROUP BY. In this case
 
1553
          We only check if the ORDER BY is a prefix of GROUP BY. In this case
1493
1554
          test_if_subpart() copies the ASC/DESC attributes from the original
1494
 
          order_st BY fields.
1495
 
          If GROUP BY is a prefix of order_st BY, then it is safe to leave
 
1555
          ORDER BY fields.
 
1556
          If GROUP BY is a prefix of ORDER BY, then it is safe to leave
1496
1557
          'order' as is.
1497
1558
       */
1498
1559
      if (!order || test_if_subpart(group_list, order))
1499
1560
          order= skip_sort_order ? 0 : group_list;
1500
1561
      /*
1501
1562
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be 
1502
 
        rewritten to IGNORE INDEX FOR order_st BY(fields).
 
1563
        rewritten to IGNORE INDEX FOR ORDER BY(fields).
1503
1564
      */
1504
1565
      join_tab->table->keys_in_use_for_order_by=
1505
1566
        join_tab->table->keys_in_use_for_group_by;
1524
1585
    /*
1525
1586
      We are only using one table. In this case we change DISTINCT to a
1526
1587
      GROUP BY query if:
1527
 
      - The GROUP BY can be done through indexes (no sort) and the order_st
 
1588
      - The GROUP BY can be done through indexes (no sort) and the ORDER
1528
1589
        BY only uses selected fields.
1529
 
        (In this case we can later optimize away GROUP BY and order_st BY)
 
1590
        (In this case we can later optimize away GROUP BY and ORDER BY)
1530
1591
      - We are scanning the whole table without LIMIT
1531
1592
        This can happen if:
1532
1593
        - We are using CALC_FOUND_ROWS
1533
 
        - We are using an order_st BY that can't be optimized away.
 
1594
        - We are using an ORDER BY that can't be optimized away.
1534
1595
 
1535
1596
      We don't want to use this optimization when we are using LIMIT
1536
1597
      because in this case we can just create a temporary table that
1562
1623
          {
1563
1624
            /*
1564
1625
              Force MySQL to read the table in sorted order to get result in
1565
 
              order_st BY order.
 
1626
              ORDER BY order.
1566
1627
            */
1567
1628
            tmp_table_param.quick_group=0;
1568
1629
          }
1574
1635
        group_list= 0;
1575
1636
    }
1576
1637
    else if (thd->is_fatal_error)                       // End of memory
1577
 
      return(1);
 
1638
      DBUG_RETURN(1);
1578
1639
  }
1579
1640
  simple_group= 0;
1580
1641
  {
1581
 
    order_st *old_group_list;
 
1642
    ORDER *old_group_list;
1582
1643
    group_list= remove_const(this, (old_group_list= group_list), conds,
1583
1644
                             rollup.state == ROLLUP::STATE_NONE,
1584
1645
                             &simple_group);
1585
1646
    if (thd->is_error())
1586
1647
    {
1587
1648
      error= 1;
1588
 
      return(1);
 
1649
      DBUG_PRINT("error",("Error from remove_const"));
 
1650
      DBUG_RETURN(1);
1589
1651
    }
1590
1652
    if (old_group_list && !group_list)
1591
1653
      select_distinct= 0;
1619
1681
    This has to be done if all tables are not already read (const tables)
1620
1682
    and one of the following conditions holds:
1621
1683
    - We are using DISTINCT (simple distinct's are already optimized away)
1622
 
    - We are using an order_st BY or GROUP BY on fields not in the first table
1623
 
    - We are using different order_st BY and GROUP BY orders
 
1684
    - We are using an ORDER BY or GROUP BY on fields not in the first table
 
1685
    - We are using different ORDER BY and GROUP BY orders
1624
1686
    - The user wants us to buffer the result.
1625
1687
  */
1626
1688
  need_tmp= (const_tables != tables &&
1628
1690
              (group_list && order) ||
1629
1691
              test(select_options & OPTION_BUFFER_RESULT)));
1630
1692
 
1631
 
  uint32_t no_jbuf_after= make_join_orderinfo(this);
1632
 
  uint64_t select_opts_for_readinfo= 
 
1693
  uint no_jbuf_after= make_join_orderinfo(this);
 
1694
  ulonglong select_opts_for_readinfo= 
1633
1695
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
1634
1696
 
1635
1697
  sj_tmp_tables= NULL;
1639
1701
 
1640
1702
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
1641
1703
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
1642
 
    return(1);
 
1704
    DBUG_RETURN(1);
1643
1705
 
1644
1706
  /* Create all structures needed for materialized subquery execution. */
1645
1707
  if (setup_subquery_materialization())
1646
 
    return(1);
 
1708
    DBUG_RETURN(1);
1647
1709
 
1648
1710
  /*
1649
1711
    is this simple IN subquery?
1663
1725
        save_index_subquery_explain_info(join_tab, where);
1664
1726
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
1665
1727
        error= 0;
1666
 
        return(unit->item->
 
1728
        DBUG_RETURN(unit->item->
1667
1729
                    change_engine(new
1668
1730
                                  subselect_uniquesubquery_engine(thd,
1669
1731
                                                                  join_tab,
1677
1739
        save_index_subquery_explain_info(join_tab, where);
1678
1740
        join_tab[0].type= JT_INDEX_SUBQUERY;
1679
1741
        error= 0;
1680
 
        return(unit->item->
 
1742
        DBUG_RETURN(unit->item->
1681
1743
                    change_engine(new
1682
1744
                                  subselect_indexsubquery_engine(thd,
1683
1745
                                                                 join_tab,
1694
1756
      error= 0;
1695
1757
      conds= remove_additional_cond(conds);
1696
1758
      save_index_subquery_explain_info(join_tab, conds);
1697
 
      return(unit->item->
 
1759
      DBUG_RETURN(unit->item->
1698
1760
                  change_engine(new subselect_indexsubquery_engine(thd,
1699
1761
                                                                   join_tab,
1700
1762
                                                                   unit->item,
1712
1774
  */
1713
1775
  if (need_tmp || select_distinct || group_list || order)
1714
1776
  {
1715
 
    for (uint32_t i = const_tables; i < tables; i++)
 
1777
    for (uint i = const_tables; i < tables; i++)
1716
1778
      join_tab[i].table->prepare_for_position();
1717
1779
  }
1718
1780
 
 
1781
  DBUG_EXECUTE("info",TEST_join(this););
 
1782
 
1719
1783
  if (const_tables != tables)
1720
1784
  {
1721
1785
    /*
1730
1794
        ((order && simple_order) || (group_list && simple_group)))
1731
1795
    {
1732
1796
      if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
1733
 
        return(1);
 
1797
        DBUG_RETURN(1);
1734
1798
      }
1735
1799
    }
1736
1800
    
1752
1816
        Force using of tmp table if sorting by a SP or UDF function due to
1753
1817
        their expensive and probably non-deterministic nature.
1754
1818
      */
1755
 
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
1819
      for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1756
1820
      {
1757
1821
        Item *item= *tmp_order->item;
1758
1822
        if (item->is_expensive())
1769
1833
  if (select_options & SELECT_DESCRIBE)
1770
1834
  {
1771
1835
    error= 0;
1772
 
    return(0);
 
1836
    DBUG_RETURN(0);
1773
1837
  }
1774
1838
  having= 0;
1775
1839
 
1790
1854
  /* Create a tmp table if distinct or if the sort is too complicated */
1791
1855
  if (need_tmp)
1792
1856
  {
1793
 
    thd->set_proc_info("Creating tmp table");
 
1857
    DBUG_PRINT("info",("Creating tmp table"));
 
1858
    thd_proc_info(thd, "Creating tmp table");
1794
1859
 
1795
1860
    init_items_ref_array();
1796
1861
 
1797
1862
    tmp_table_param.hidden_field_count= (all_fields.elements -
1798
1863
                                         fields_list.elements);
1799
 
    order_st *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1800
 
                                                             (order_st*) 0);
 
1864
    ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1865
                                                             (ORDER*) 0);
1801
1866
    /*
1802
1867
      Pushing LIMIT to the temporary table creation is not applicable
1803
 
      when there is order_st BY or GROUP BY or there is no GROUP BY, but
 
1868
      when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1804
1869
      there are aggregate functions, because in all these cases we need
1805
1870
      all result rows.
1806
1871
    */
1818
1883
                           tmp_rows_limit,
1819
1884
                           (char *) "")))
1820
1885
                {
1821
 
      return(1);
 
1886
      DBUG_RETURN(1);
1822
1887
    }
1823
1888
 
1824
1889
    /*
1838
1903
    /* if group or order on first table, sort first */
1839
1904
    if (group_list && simple_group)
1840
1905
    {
1841
 
      thd->set_proc_info("Sorting for group");
 
1906
      DBUG_PRINT("info",("Sorting for group"));
 
1907
      thd_proc_info(thd, "Sorting for group");
1842
1908
      if (create_sort_index(thd, this, group_list,
1843
1909
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
1844
1910
          alloc_group_fields(this, group_list) ||
1845
1911
          make_sum_func_list(all_fields, fields_list, 1) ||
1846
1912
          setup_sum_funcs(thd, sum_funcs))
1847
1913
      {
1848
 
        return(1);
 
1914
        DBUG_RETURN(1);
1849
1915
      }
1850
1916
      group_list=0;
1851
1917
    }
1854
1920
      if (make_sum_func_list(all_fields, fields_list, 0) ||
1855
1921
          setup_sum_funcs(thd, sum_funcs))
1856
1922
      {
1857
 
        return(1);
 
1923
        DBUG_RETURN(1);
1858
1924
      }
1859
1925
 
1860
1926
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1861
1927
      {
1862
 
        thd->set_proc_info("Sorting for order");
 
1928
        DBUG_PRINT("info",("Sorting for order"));
 
1929
        thd_proc_info(thd, "Sorting for order");
1863
1930
        if (create_sort_index(thd, this, order,
1864
1931
                              HA_POS_ERROR, HA_POS_ERROR, true))
1865
1932
        {
1866
 
          return(1);
 
1933
          DBUG_RETURN(1);
1867
1934
        }
1868
1935
        order=0;
1869
1936
      }
1903
1970
    */
1904
1971
    if (select_lex->uncacheable && !is_top_level_join() &&
1905
1972
        init_save_join_tab())
1906
 
      return(-1);                         /* purecov: inspected */
 
1973
      DBUG_RETURN(-1);                         /* purecov: inspected */
1907
1974
  }
1908
1975
 
1909
1976
  error= 0;
1910
 
  return(0);
 
1977
  DBUG_RETURN(0);
1911
1978
}
1912
1979
 
1913
1980
 
1923
1990
int
1924
1991
JOIN::reinit()
1925
1992
{
 
1993
  DBUG_ENTER("JOIN::reinit");
 
1994
 
1926
1995
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
1927
1996
                                    select_lex->offset_limit->val_uint() :
1928
 
                                    0UL);
 
1997
                                    0ULL);
1929
1998
 
1930
1999
  first_record= 0;
1931
2000
 
1960
2029
      func->clear();
1961
2030
  }
1962
2031
 
1963
 
  return(0);
 
2032
  DBUG_RETURN(0);
1964
2033
}
1965
2034
 
1966
2035
/**
1990
2059
{
1991
2060
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
1992
2061
  {
1993
 
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((unsigned char*) join_tab,
 
2062
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
1994
2063
                                                sizeof(JOIN_TAB) * tables)))
1995
2064
      return 1;
1996
2065
  }
2014
2083
{
2015
2084
  List<Item> *columns_list= &fields_list;
2016
2085
  int      tmp_error;
 
2086
  DBUG_ENTER("JOIN::exec");
2017
2087
 
2018
 
  thd->set_proc_info("executing");
 
2088
  thd_proc_info(thd, "executing");
2019
2089
  error= 0;
2020
2090
  (void) result->prepare2(); // Currently, this cannot fail.
2021
2091
 
2055
2125
    /* Single select (without union) always returns 0 or 1 row */
2056
2126
    thd->limit_found_rows= send_records;
2057
2127
    thd->examined_row_count= 0;
2058
 
    return;
 
2128
    DBUG_VOID_RETURN;
2059
2129
  }
2060
2130
  /*
2061
2131
    Don't reset the found rows count if there're no tables as
2073
2143
                            select_options,
2074
2144
                            zero_result_cause,
2075
2145
                            having);
2076
 
    return;
 
2146
    DBUG_VOID_RETURN;
2077
2147
  }
2078
2148
 
2079
2149
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
2080
2150
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2081
 
    return;
 
2151
    DBUG_VOID_RETURN;
2082
2152
 
2083
2153
  if (select_options & SELECT_DESCRIBE)
2084
2154
  {
2085
2155
    /*
2086
 
      Check if we managed to optimize order_st BY away and don't use temporary
2087
 
      table to resolve order_st BY: in that case, we only may need to do
 
2156
      Check if we managed to optimize ORDER BY away and don't use temporary
 
2157
      table to resolve ORDER BY: in that case, we only may need to do
2088
2158
      filesort for GROUP BY.
2089
2159
    */
2090
2160
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
2110
2180
    select_describe(this, need_tmp,
2111
2181
                    order != 0 && !skip_sort_order,
2112
2182
                    select_distinct,
2113
 
                    !tables ? "No tables used" : NULL);
2114
 
    return;
 
2183
                    !tables ? "No tables used" : NullS);
 
2184
    DBUG_VOID_RETURN;
2115
2185
  }
2116
2186
 
2117
2187
  JOIN *curr_join= this;
2118
2188
  List<Item> *curr_all_fields= &all_fields;
2119
2189
  List<Item> *curr_fields_list= &fields_list;
2120
 
  Table *curr_tmp_table= 0;
 
2190
  TABLE *curr_tmp_table= 0;
2121
2191
  /*
2122
2192
    Initialize examined rows here because the values from all join parts
2123
2193
    must be accumulated in examined_row_count. Hence every join
2141
2211
    curr_tmp_table= exec_tmp_table1;
2142
2212
 
2143
2213
    /* Copy data to the temporary table */
2144
 
    thd->set_proc_info("Copying to tmp table");
 
2214
    thd_proc_info(thd, "Copying to tmp table");
 
2215
    DBUG_PRINT("info", ("%s", thd->proc_info));
2145
2216
    if (!curr_join->sort_and_group &&
2146
2217
        curr_join->const_tables != curr_join->tables)
2147
2218
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
2148
2219
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2149
2220
    {
2150
2221
      error= tmp_error;
2151
 
      return;
 
2222
      DBUG_VOID_RETURN;
2152
2223
    }
2153
2224
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
2154
2225
    
2165
2236
        if (change_to_use_tmp_fields(thd, items1,
2166
2237
                                     tmp_fields_list1, tmp_all_fields1,
2167
2238
                                     fields_list.elements, all_fields))
2168
 
          return;
 
2239
          DBUG_VOID_RETURN;
2169
2240
      }
2170
2241
      else
2171
2242
      {
2172
2243
        if (change_refs_to_tmp_fields(thd, items1,
2173
2244
                                      tmp_fields_list1, tmp_all_fields1,
2174
2245
                                      fields_list.elements, all_fields))
2175
 
          return;
 
2246
          DBUG_VOID_RETURN;
2176
2247
      }
2177
2248
      curr_join->tmp_all_fields1= tmp_all_fields1;
2178
2249
      curr_join->tmp_fields_list1= tmp_fields_list1;
2214
2285
 
2215
2286
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
2216
2287
    {                                   /* Must copy to another table */
 
2288
      DBUG_PRINT("info",("Creating group table"));
 
2289
      
2217
2290
      /* Free first data from old join */
2218
2291
      curr_join->join_free();
2219
2292
      if (make_simple_join(curr_join, curr_tmp_table))
2220
 
        return;
 
2293
        DBUG_VOID_RETURN;
2221
2294
      calc_group_buffer(curr_join, group_list);
2222
2295
      count_field_types(select_lex, &curr_join->tmp_table_param,
2223
2296
                        curr_join->tmp_all_fields1,
2245
2318
              exec_tmp_table2= create_tmp_table(thd,
2246
2319
                                                &curr_join->tmp_table_param,
2247
2320
                                                *curr_all_fields,
2248
 
                                                (order_st*) 0,
 
2321
                                                (ORDER*) 0,
2249
2322
                                                curr_join->select_distinct && 
2250
2323
                                                !curr_join->group_list,
2251
2324
                                                1, curr_join->select_options,
2252
2325
                                                HA_POS_ERROR,
2253
2326
                                                (char *) "")))
2254
 
          return;
 
2327
          DBUG_VOID_RETURN;
2255
2328
        curr_join->exec_tmp_table2= exec_tmp_table2;
2256
2329
      }
2257
2330
      if (curr_join->group_list)
2258
2331
      {
2259
 
        thd->set_proc_info("Creating sort index");
 
2332
        thd_proc_info(thd, "Creating sort index");
2260
2333
        if (curr_join->join_tab == join_tab && save_join_tab())
2261
2334
        {
2262
 
          return;
 
2335
          DBUG_VOID_RETURN;
2263
2336
        }
2264
2337
        if (create_sort_index(thd, curr_join, curr_join->group_list,
2265
2338
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
2266
2339
            make_group_fields(this, curr_join))
2267
2340
        {
2268
 
          return;
 
2341
          DBUG_VOID_RETURN;
2269
2342
        }
2270
2343
        sortorder= curr_join->sortorder;
2271
2344
      }
2272
2345
      
2273
 
      thd->set_proc_info("Copying to group table");
 
2346
      thd_proc_info(thd, "Copying to group table");
 
2347
      DBUG_PRINT("info", ("%s", thd->proc_info));
2274
2348
      tmp_error= -1;
2275
2349
      if (curr_join != this)
2276
2350
      {
2288
2362
      }
2289
2363
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2290
2364
                                        1, true))
2291
 
        return;
 
2365
        DBUG_VOID_RETURN;
2292
2366
      curr_join->group_list= 0;
2293
2367
      if (!curr_join->sort_and_group &&
2294
2368
          curr_join->const_tables != curr_join->tables)
2297
2371
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2298
2372
      {
2299
2373
        error= tmp_error;
2300
 
        return;
 
2374
        DBUG_VOID_RETURN;
2301
2375
      }
2302
2376
      end_read_record(&curr_join->join_tab->read_record);
2303
2377
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2310
2384
        if (change_to_use_tmp_fields(thd, items2,
2311
2385
                                     tmp_fields_list2, tmp_all_fields2, 
2312
2386
                                     fields_list.elements, tmp_all_fields1))
2313
 
          return;
 
2387
          DBUG_VOID_RETURN;
2314
2388
        curr_join->tmp_fields_list2= tmp_fields_list2;
2315
2389
        curr_join->tmp_all_fields2= tmp_all_fields2;
2316
2390
      }
2327
2401
    curr_join->join_free();                     /* Free quick selects */
2328
2402
    if (curr_join->select_distinct && ! curr_join->group_list)
2329
2403
    {
2330
 
      thd->set_proc_info("Removing duplicates");
 
2404
      thd_proc_info(thd, "Removing duplicates");
2331
2405
      if (curr_join->tmp_having)
2332
2406
        curr_join->tmp_having->update_used_tables();
2333
2407
      if (remove_duplicates(curr_join, curr_tmp_table,
2334
2408
                            *curr_fields_list, curr_join->tmp_having))
2335
 
        return;
 
2409
        DBUG_VOID_RETURN;
2336
2410
      curr_join->tmp_having=0;
2337
2411
      curr_join->select_distinct=0;
2338
2412
    }
2339
2413
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
2340
2414
    if (make_simple_join(curr_join, curr_tmp_table))
2341
 
      return;
 
2415
      DBUG_VOID_RETURN;
2342
2416
    calc_group_buffer(curr_join, curr_join->group_list);
2343
2417
    count_field_types(select_lex, &curr_join->tmp_table_param, 
2344
2418
                      *curr_all_fields, 0);
2349
2423
  {
2350
2424
    if (make_group_fields(this, curr_join))
2351
2425
    {
2352
 
      return;
 
2426
      DBUG_VOID_RETURN;
2353
2427
    }
2354
2428
    if (!items3)
2355
2429
    {
2381
2455
                                      1, true) || 
2382
2456
        setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2383
2457
        thd->is_fatal_error)
2384
 
      return;
 
2458
      DBUG_VOID_RETURN;
2385
2459
  }
2386
2460
  if (curr_join->group_list || curr_join->order)
2387
2461
  {
2388
 
    thd->set_proc_info("Sorting result");
 
2462
    DBUG_PRINT("info",("Sorting for send_fields"));
 
2463
    thd_proc_info(thd, "Sorting result");
2389
2464
    /* If we have already done the group, add HAVING to sorted table */
2390
2465
    if (curr_join->tmp_having && ! curr_join->group_list && 
2391
2466
        ! curr_join->sort_and_group)
2403
2478
      {
2404
2479
        if (!curr_table->select)
2405
2480
          if (!(curr_table->select= new SQL_SELECT))
2406
 
            return;
 
2481
            DBUG_VOID_RETURN;
2407
2482
        if (!curr_table->select->cond)
2408
2483
          curr_table->select->cond= sort_table_cond;
2409
2484
        else                                    // This should never happen
2411
2486
          if (!(curr_table->select->cond=
2412
2487
                new Item_cond_and(curr_table->select->cond,
2413
2488
                                  sort_table_cond)))
2414
 
            return;
 
2489
            DBUG_VOID_RETURN;
2415
2490
          /*
2416
2491
            Item_cond_and do not need fix_fields for execution, its parameters
2417
2492
            are fixed or do not need fix_fields, too
2420
2495
        }
2421
2496
        curr_table->select_cond= curr_table->select->cond;
2422
2497
        curr_table->select_cond->top_level_item();
 
2498
        DBUG_EXECUTE("where",print_where(curr_table->select->cond,
 
2499
                                         "select and having",
 
2500
                                         QT_ORDINARY););
2423
2501
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2424
2502
                                                   ~ (table_map) 0,
2425
2503
                                                   ~used_tables, 0);
 
2504
        DBUG_EXECUTE("where",print_where(curr_join->tmp_having,
 
2505
                                         "having after sort",
 
2506
                                         QT_ORDINARY););
2426
2507
      }
2427
2508
    }
2428
2509
    {
2453
2534
      }
2454
2535
      if (curr_join->join_tab == join_tab && save_join_tab())
2455
2536
      {
2456
 
        return;
 
2537
        DBUG_VOID_RETURN;
2457
2538
      }
2458
2539
      /*
2459
 
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
 
2540
        Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
2460
2541
        chose FILESORT to be faster than INDEX SCAN or there is no 
2461
2542
        suitable index present.
2462
2543
        Note, that create_sort_index calls test_if_skip_sort_order and may
2471
2552
                            (select_options & OPTION_FOUND_ROWS ?
2472
2553
                             HA_POS_ERROR : unit->select_limit_cnt),
2473
2554
                            curr_join->group_list ? true : false))
2474
 
        return;
 
2555
        DBUG_VOID_RETURN;
2475
2556
      sortorder= curr_join->sortorder;
2476
2557
      if (curr_join->const_tables != curr_join->tables &&
2477
2558
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2489
2570
  if (thd->is_error())
2490
2571
  {
2491
2572
    error= thd->is_error();
2492
 
    return;
 
2573
    DBUG_VOID_RETURN;
2493
2574
  }
2494
2575
  curr_join->having= curr_join->tmp_having;
2495
2576
  curr_join->fields= curr_fields_list;
2496
2577
 
2497
2578
  {
2498
 
    thd->set_proc_info("Sending data");
 
2579
    thd_proc_info(thd, "Sending data");
 
2580
    DBUG_PRINT("info", ("%s", thd->proc_info));
2499
2581
    result->send_fields(*curr_fields_list,
2500
2582
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2501
2583
    error= do_select(curr_join, curr_fields_list, NULL);
2504
2586
 
2505
2587
  /* Accumulate the counts from all join iterations of all join parts. */
2506
2588
  thd->examined_row_count+= curr_join->examined_rows;
 
2589
  DBUG_PRINT("counts", ("thd->examined_row_count: %lu",
 
2590
                        (ulong) thd->examined_row_count));
2507
2591
 
2508
2592
  /* 
2509
2593
    With EXPLAIN EXTENDED we have to restore original ref_array
2515
2599
      select_lex->linkage == DERIVED_TABLE_TYPE)      
2516
2600
    set_items_ref_array(items0);
2517
2601
 
2518
 
  return;
 
2602
  DBUG_VOID_RETURN;
2519
2603
}
2520
2604
 
2521
2605
 
2529
2613
int
2530
2614
JOIN::destroy()
2531
2615
{
 
2616
  DBUG_ENTER("JOIN::destroy");
2532
2617
  select_lex->join= 0;
2533
2618
 
2534
2619
  if (tmp_join)
2541
2626
    }
2542
2627
    tmp_join->tmp_join= 0;
2543
2628
    tmp_table_param.copy_field=0;
2544
 
    return(tmp_join->destroy());
 
2629
    DBUG_RETURN(tmp_join->destroy());
2545
2630
  }
2546
2631
  cond_equal= 0;
2547
2632
 
2548
2633
  cleanup(1);
2549
2634
  if (exec_tmp_table1)
2550
 
    exec_tmp_table1->free_tmp_table(thd);
 
2635
    free_tmp_table(thd, exec_tmp_table1);
2551
2636
  if (exec_tmp_table2)
2552
 
    exec_tmp_table2->free_tmp_table(thd);
 
2637
    free_tmp_table(thd, exec_tmp_table2);
2553
2638
  delete select;
2554
2639
  delete_dynamic(&keyuse);
2555
 
  return(error);
 
2640
  DBUG_RETURN(error);
2556
2641
}
2557
2642
 
2558
2643
 
2576
2661
                              for a, b and c in this list.
2577
2662
  @param conds                top level item of an expression representing
2578
2663
                              WHERE clause of the top level select
2579
 
  @param og_num               total number of order_st BY and GROUP BY clauses
 
2664
  @param og_num               total number of ORDER BY and GROUP BY clauses
2580
2665
                              arguments
2581
 
  @param order                linked list of order_st BY agruments
 
2666
  @param order                linked list of ORDER BY agruments
2582
2667
  @param group                linked list of GROUP BY arguments
2583
2668
  @param having               top level item of HAVING expression
2584
2669
  @param proc_param           list of PROCEDUREs
2603
2688
 
2604
2689
bool
2605
2690
mysql_select(THD *thd, Item ***rref_pointer_array,
2606
 
             TableList *tables, uint32_t wild_num, List<Item> &fields,
2607
 
             COND *conds, uint32_t og_num,  order_st *order, order_st *group,
2608
 
             Item *having, order_st *proc_param, uint64_t select_options,
 
2691
             TABLE_LIST *tables, uint wild_num, List<Item> &fields,
 
2692
             COND *conds, uint og_num,  ORDER *order, ORDER *group,
 
2693
             Item *having, ORDER *proc_param, ulonglong select_options,
2609
2694
             select_result *result, SELECT_LEX_UNIT *unit,
2610
2695
             SELECT_LEX *select_lex)
2611
2696
{
2612
2697
  bool err;
2613
2698
  bool free_join= 1;
 
2699
  DBUG_ENTER("mysql_select");
2614
2700
 
2615
2701
  select_lex->context.resolve_in_select_list= true;
2616
2702
  JOIN *join;
2629
2715
        //here is EXPLAIN of subselect or derived table
2630
2716
        if (join->change_result(result))
2631
2717
        {
2632
 
          return(true);
 
2718
          DBUG_RETURN(true);
2633
2719
        }
2634
2720
      }
2635
2721
      else
2648
2734
  else
2649
2735
  {
2650
2736
    if (!(join= new JOIN(thd, fields, select_options, result)))
2651
 
        return(true);
2652
 
    thd->set_proc_info("init");
 
2737
        DBUG_RETURN(true);
 
2738
    thd_proc_info(thd, "init");
2653
2739
    thd->used_tables=0;                         // Updated by setup_fields
2654
2740
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2655
2741
                           conds, og_num, order, group, having, proc_param,
2659
2745
    }
2660
2746
  }
2661
2747
 
2662
 
  /* dump_TableList_graph(select_lex, select_lex->leaf_tables); */
 
2748
  /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
2663
2749
  if (join->flatten_subqueries())
2664
2750
  {
2665
2751
    err= 1;
2666
2752
    goto err;
2667
2753
  }
2668
 
  /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
 
2754
  /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
2669
2755
 
2670
2756
  if ((err= join->optimize()))
2671
2757
  {
2692
2778
err:
2693
2779
  if (free_join)
2694
2780
  {
2695
 
    thd->set_proc_info("end");
 
2781
    thd_proc_info(thd, "end");
2696
2782
    err|= select_lex->cleanup();
2697
 
    return(err || thd->is_error());
 
2783
    DBUG_RETURN(err || thd->is_error());
2698
2784
  }
2699
 
  return(join->error);
 
2785
  DBUG_RETURN(join->error);
2700
2786
}
2701
2787
 
2702
2788
 
2714
2800
}
2715
2801
 
2716
2802
 
2717
 
static TableList *alloc_join_nest(THD *thd)
 
2803
static TABLE_LIST *alloc_join_nest(THD *thd)
2718
2804
{
2719
 
  TableList *tbl;
2720
 
  if (!(tbl= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
2721
 
                                       sizeof(nested_join_st))))
 
2805
  TABLE_LIST *tbl;
 
2806
  if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
 
2807
                                       sizeof(NESTED_JOIN))))
2722
2808
    return NULL;
2723
 
  tbl->nested_join= (nested_join_st*) ((unsigned char*)tbl + 
2724
 
                                    ALIGN_SIZE(sizeof(TableList)));
 
2809
  tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl + 
 
2810
                                    ALIGN_SIZE(sizeof(TABLE_LIST)));
2725
2811
  return tbl;
2726
2812
}
2727
2813
 
2728
2814
 
2729
 
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TableList> *tlist)
 
2815
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
2730
2816
{
2731
 
  List_iterator<TableList> it(*tlist);
2732
 
  TableList *table;
 
2817
  List_iterator<TABLE_LIST> it(*tlist);
 
2818
  TABLE_LIST *table;
2733
2819
  while ((table= it++))
2734
2820
  {
2735
2821
    if (table->on_expr)
2741
2827
 
2742
2828
 
2743
2829
/*
2744
 
  Convert a subquery predicate into a TableList semi-join nest
 
2830
  Convert a subquery predicate into a TABLE_LIST semi-join nest
2745
2831
 
2746
2832
  SYNOPSIS
2747
2833
    convert_subq_to_sj()
2750
2836
       subq_pred    Subquery predicate to be converted
2751
2837
  
2752
2838
  DESCRIPTION
2753
 
    Convert a subquery predicate into a TableList semi-join nest. All the 
 
2839
    Convert a subquery predicate into a TABLE_LIST semi-join nest. All the 
2754
2840
    prerequisites are already checked, so the conversion is always successfull.
2755
2841
 
2756
2842
    Prepared Statements: the transformation is permanent:
2757
 
     - Changes in TableList structures are naturally permanent
 
2843
     - Changes in TABLE_LIST structures are naturally permanent
2758
2844
     - Item tree changes are performed on statement MEM_ROOT:
2759
2845
        = we activate statement MEM_ROOT 
2760
2846
        = this function is called before the first fix_prepare_information
2771
2857
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
2772
2858
{
2773
2859
  SELECT_LEX *parent_lex= parent_join->select_lex;
2774
 
  TableList *emb_tbl_nest= NULL;
2775
 
  List<TableList> *emb_join_list= &parent_lex->top_join_list;
 
2860
  TABLE_LIST *emb_tbl_nest= NULL;
 
2861
  List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
2776
2862
  THD *thd= parent_join->thd;
 
2863
  DBUG_ENTER("convert_subq_to_sj");
2777
2864
 
2778
2865
  /*
2779
2866
    1. Find out where to put the predicate into.
2809
2896
    }
2810
2897
    else if (!subq_pred->expr_join_nest->nested_join)
2811
2898
    {
2812
 
      TableList *outer_tbl= subq_pred->expr_join_nest;      
2813
 
      TableList *wrap_nest;
 
2899
      TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;      
 
2900
      TABLE_LIST *wrap_nest;
2814
2901
      /*
2815
2902
        We're dealing with
2816
2903
 
2825
2912
        Q:  other subqueries may be pointing to this element. What to do?
2826
2913
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
2827
2914
            But we'll need to fix other pointers.
2828
 
        A2: Another way: have TableList::next_ptr so the following
 
2915
        A2: Another way: have TABLE_LIST::next_ptr so the following
2829
2916
            subqueries know the table has been nested.
2830
 
        A3: changes in the TableList::outer_join will make everything work
 
2917
        A3: changes in the TABLE_LIST::outer_join will make everything work
2831
2918
            automatically.
2832
2919
      */
2833
2920
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
2834
2921
      {
2835
 
        return(true);
 
2922
        DBUG_RETURN(true);
2836
2923
      }
2837
2924
      wrap_nest->embedding= outer_tbl->embedding;
2838
2925
      wrap_nest->join_list= outer_tbl->join_list;
2854
2941
      wrap_nest->on_expr= outer_tbl->on_expr;
2855
2942
      outer_tbl->on_expr= NULL;
2856
2943
 
2857
 
      List_iterator<TableList> li(*wrap_nest->join_list);
2858
 
      TableList *tbl;
 
2944
      List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
 
2945
      TABLE_LIST *tbl;
2859
2946
      while ((tbl= li++))
2860
2947
      {
2861
2948
        if (tbl == outer_tbl)
2873
2960
    }
2874
2961
  }
2875
2962
 
2876
 
  TableList *sj_nest;
2877
 
  nested_join_st *nested_join;
 
2963
  TABLE_LIST *sj_nest;
 
2964
  NESTED_JOIN *nested_join;
2878
2965
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
2879
2966
  {
2880
 
    return(true);
 
2967
    DBUG_RETURN(true);
2881
2968
  }
2882
2969
  nested_join= sj_nest->nested_join;
2883
2970
 
2899
2986
  */
2900
2987
  st_select_lex *subq_lex= subq_pred->unit->first_select();
2901
2988
  nested_join->join_list.empty();
2902
 
  List_iterator_fast<TableList> li(subq_lex->top_join_list);
2903
 
  TableList *tl, *last_leaf;
 
2989
  List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
 
2990
  TABLE_LIST *tl, *last_leaf;
2904
2991
  while ((tl= li++))
2905
2992
  {
2906
2993
    tl->embedding= sj_nest;
2936
3023
  /*TODO: also reset the 'with_subselect' there. */
2937
3024
 
2938
3025
  /* n. Adjust the parent_join->tables counter */
2939
 
  uint32_t table_no= parent_join->tables;
 
3026
  uint table_no= parent_join->tables;
2940
3027
  /* n. Walk through child's tables and adjust table->map */
2941
3028
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
2942
3029
  {
2944
3031
    tl->table->map= ((table_map)1) << table_no;
2945
3032
    SELECT_LEX *old_sl= tl->select_lex;
2946
3033
    tl->select_lex= parent_join->select_lex; 
2947
 
    for(TableList *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
 
3034
    for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
2948
3035
      emb->select_lex= parent_join->select_lex;
2949
3036
  }
2950
3037
  parent_join->tables += subq_lex->join->tables;
2957
3044
  thd->lex->current_select=subq_lex;
2958
3045
  if (!subq_pred->left_expr->fixed &&
2959
3046
       subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
2960
 
    return(true);
 
3047
    DBUG_RETURN(true);
2961
3048
  thd->lex->current_select=save_lex;
2962
3049
 
2963
3050
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
2994
3081
  }
2995
3082
  else
2996
3083
  {
2997
 
    for (uint32_t i= 0; i < subq_pred->left_expr->cols(); i++)
 
3084
    for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
2998
3085
    {
2999
3086
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
3000
3087
                                                element_index(i));
3019
3106
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
3020
3107
  subq_lex->master_unit()->exclude_level();
3021
3108
 
 
3109
  DBUG_EXECUTE("where",
 
3110
               print_where(sj_nest->sj_on_expr,"SJ-EXPR", QT_ORDINARY););
 
3111
 
3022
3112
  /* Inject sj_on_expr into the parent's WHERE or ON */
3023
3113
  if (emb_tbl_nest)
3024
3114
  {
3034
3124
    parent_join->select_lex->where= parent_join->conds;
3035
3125
  }
3036
3126
 
3037
 
  return(false);
 
3127
  DBUG_RETURN(false);
3038
3128
}
3039
3129
 
3040
3130
 
3056
3146
{
3057
3147
  Item_in_subselect **in_subq;
3058
3148
  Item_in_subselect **in_subq_end;
 
3149
  DBUG_ENTER("JOIN::flatten_subqueries");
3059
3150
 
3060
3151
  if (sj_subselects.elements() == 0)
3061
 
    return(false);
 
3152
    DBUG_RETURN(false);
3062
3153
 
3063
3154
  /* 1. Fix children subqueries */
3064
3155
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); 
3067
3158
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
3068
3159
    child_join->outer_tables = child_join->tables;
3069
3160
    if (child_join->flatten_subqueries())
3070
 
      return(true);
 
3161
      DBUG_RETURN(true);
3071
3162
    (*in_subq)->sj_convert_priority= 
3072
3163
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
3073
3164
  }
3074
3165
 
3075
 
  //dump_TableList_struct(select_lex, select_lex->leaf_tables);
 
3166
  //dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
3076
3167
  /* 
3077
3168
    2. Pick which subqueries to convert:
3078
3169
      sort the subquery array
3088
3179
       in_subq++)
3089
3180
  {
3090
3181
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
3091
 
      return(true);
 
3182
      DBUG_RETURN(true);
3092
3183
  }
3093
3184
 
3094
3185
  for (in_subq= sj_subselects.front(); 
3097
3188
       in_subq++)
3098
3189
  {
3099
3190
    if (convert_subq_to_sj(this, *in_subq))
3100
 
      return(true);
 
3191
      DBUG_RETURN(true);
3101
3192
  }
3102
3193
 
3103
3194
  /* 3. Finalize those we didn't convert */
3109
3200
    (*in_subq)->fixed= 0;
3110
3201
    res= (*in_subq)->select_transformer(child_join);
3111
3202
    if (res == Item_subselect::RES_ERROR)
3112
 
      return(true);
 
3203
      DBUG_RETURN(true);
3113
3204
 
3114
3205
    (*in_subq)->changed= 1;
3115
3206
    (*in_subq)->fixed= 1;
3117
3208
    Item *substitute= (*in_subq)->substitution;
3118
3209
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
3119
3210
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
3120
 
      return(true);
 
3211
      DBUG_RETURN(true);
3121
3212
 
3122
3213
    //if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
3123
 
    //  return(true);
 
3214
    //  DBUG_RETURN(true);
3124
3215
  }
3125
3216
  sj_subselects.clear();
3126
 
  return(false);
 
3217
  DBUG_RETURN(false);
3127
3218
}
3128
3219
 
3129
3220
 
3193
3284
    false - Otherwise
3194
3285
*/
3195
3286
 
3196
 
bool find_eq_ref_candidate(Table *table, table_map sj_inner_tables)
 
3287
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
3197
3288
{
3198
3289
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
3199
 
  uint32_t key;
 
3290
  uint key;
3200
3291
 
3201
3292
  if (keyuse)
3202
3293
  {
3263
3354
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
3264
3355
       tables)
3265
3356
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
3266
 
     * Semi-join nests TableList::sj_inner_tables
 
3357
     * Semi-join nests TABLE_LIST::sj_inner_tables
3267
3358
 
3268
3359
    This operation is (and should be) performed at each PS execution since
3269
3360
    tables may become/cease to be constant across PS reexecutions.
3275
3366
 
3276
3367
int pull_out_semijoin_tables(JOIN *join)
3277
3368
{
3278
 
  TableList *sj_nest;
3279
 
  List_iterator<TableList> sj_list_it(join->select_lex->sj_nests);
 
3369
  TABLE_LIST *sj_nest;
 
3370
  DBUG_ENTER("pull_out_semijoin_tables");
 
3371
  List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
3280
3372
   
3281
3373
  /* Try pulling out of the each of the semi-joins */
3282
3374
  while ((sj_nest= sj_list_it++))
3284
3376
    /* Action #1: Mark the constant tables to be pulled out */
3285
3377
    table_map pulled_tables= 0;
3286
3378
     
3287
 
    List_iterator<TableList> child_li(sj_nest->nested_join->join_list);
3288
 
    TableList *tbl;
 
3379
    List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
 
3380
    TABLE_LIST *tbl;
3289
3381
    while ((tbl= child_li++))
3290
3382
    {
3291
3383
      if (tbl->table)
3294
3386
        if (tbl->table->map & join->const_table_map)
3295
3387
        {
3296
3388
          pulled_tables |= tbl->table->map;
 
3389
          DBUG_PRINT("info", ("Table %s pulled out (reason: constant)",
 
3390
                              tbl->table->alias));
3297
3391
        }
3298
3392
      }
3299
3393
    }
3318
3412
          {
3319
3413
            pulled_a_table= true;
3320
3414
            pulled_tables |= tbl->table->map;
 
3415
            DBUG_PRINT("info", ("Table %s pulled out (reason: func dep)",
 
3416
                                tbl->table->alias));
3321
3417
          }
3322
3418
        }
3323
3419
      }
3327
3423
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
3328
3424
    {
3329
3425
      (sj_nest)->sj_inner_tables= 0;
 
3426
      DBUG_PRINT("info", ("All semi-join nest tables were pulled out"));
3330
3427
      while ((tbl= child_li++))
3331
3428
      {
3332
3429
        if (tbl->table)
3351
3448
      }
3352
3449
    }
3353
3450
  }
3354
 
  return(0);
 
3451
  DBUG_RETURN(0);
3355
3452
}
3356
3453
 
3357
3454
/*****************************************************************************
3361
3458
 
3362
3459
 
3363
3460
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
3364
 
                                      Table *table,
 
3461
                                      TABLE *table,
3365
3462
                                      const key_map *keys,ha_rows limit)
3366
3463
{
3367
3464
  int error;
 
3465
  DBUG_ENTER("get_quick_record_count");
3368
3466
  if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
3369
 
    return(0);                           // Fatal error flag is set
 
3467
    DBUG_RETURN(0);                           // Fatal error flag is set
3370
3468
  if (select)
3371
3469
  {
3372
3470
    select->head=table;
3373
3471
    table->reginfo.impossible_range=0;
3374
3472
    if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
3375
3473
                                          limit, 0, false)) == 1)
3376
 
      return(select->quick->records);
 
3474
      DBUG_RETURN(select->quick->records);
3377
3475
    if (error == -1)
3378
3476
    {
3379
3477
      table->reginfo.impossible_range=1;
3380
 
      return(0);
 
3478
      DBUG_RETURN(0);
3381
3479
    }
 
3480
    DBUG_PRINT("warning",("Couldn't use record count on const keypart"));
3382
3481
  }
3383
 
  return(HA_POS_ERROR);                 /* This shouldn't happend */
 
3482
  DBUG_RETURN(HA_POS_ERROR);                    /* This shouldn't happend */
3384
3483
}
3385
3484
 
3386
3485
/*
3394
3493
{
3395
3494
  Field *field;              /* field against which to check sargability */
3396
3495
  Item **arg_value;          /* values of potential keys for lookups     */
3397
 
  uint32_t num_values;           /* number of values in the above array      */
 
3496
  uint num_values;           /* number of values in the above array      */
3398
3497
} SARGABLE_PARAM;  
3399
3498
 
3400
3499
/**
3407
3506
*/
3408
3507
 
3409
3508
static bool
3410
 
make_join_statistics(JOIN *join, TableList *tables, COND *conds,
 
3509
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
3411
3510
                     DYNAMIC_ARRAY *keyuse_array)
3412
3511
{
3413
3512
  int error;
3414
 
  Table *table;
3415
 
  uint32_t i,table_count,const_count,key;
 
3513
  TABLE *table;
 
3514
  uint i,table_count,const_count,key;
3416
3515
  table_map found_const_table_map, all_table_map, found_ref, refs;
3417
3516
  key_map const_ref, eq_part;
3418
 
  Table **table_vector;
 
3517
  TABLE **table_vector;
3419
3518
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
3420
3519
  KEYUSE *keyuse,*start_keyuse;
3421
3520
  table_map outer_join=0;
3422
3521
  SARGABLE_PARAM *sargables= 0;
3423
3522
  JOIN_TAB *stat_vector[MAX_TABLES+1];
 
3523
  DBUG_ENTER("make_join_statistics");
3424
3524
 
3425
3525
  table_count=join->tables;
3426
3526
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
3427
3527
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3428
 
  table_vector=(Table**) join->thd->alloc(sizeof(Table*)*(table_count*2));
 
3528
  table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
3429
3529
  if (!stat || !stat_ref || !table_vector)
3430
 
    return(1);                          // Eom /* purecov: inspected */
 
3530
    DBUG_RETURN(1);                             // Eom /* purecov: inspected */
3431
3531
 
3432
3532
  join->best_ref=stat_vector;
3433
3533
 
3439
3539
       tables;
3440
3540
       s++, tables= tables->next_leaf, i++)
3441
3541
  {
3442
 
    TableList *embedding= tables->embedding;
 
3542
    TABLE_LIST *embedding= tables->embedding;
3443
3543
    stat_vector[i]=s;
3444
3544
    s->keys.init();
3445
3545
    s->const_keys.init();
3451
3551
    if(error)
3452
3552
    {
3453
3553
        table->file->print_error(error, MYF(0));
3454
 
        return(1);
 
3554
        DBUG_RETURN(1);
3455
3555
    }
3456
3556
    table->quick_keys.clear_all();
3457
3557
    table->reginfo.join_tab=s;
3458
3558
    table->reginfo.not_exists_optimize=0;
3459
 
    memset(table->const_key_parts, 0,
3460
 
           sizeof(key_part_map)*table->s->keys);
 
3559
    bzero((char*) table->const_key_parts, sizeof(key_part_map)*table->s->keys);
3461
3560
    all_table_map|= table->map;
3462
3561
    s->join=join;
3463
3562
    s->info=0;                                  // For describe
3490
3589
      s->embedding_map= 0;
3491
3590
      do
3492
3591
      {
3493
 
        nested_join_st *nested_join= embedding->nested_join;
 
3592
        NESTED_JOIN *nested_join= embedding->nested_join;
3494
3593
        s->embedding_map|=nested_join->nj_map;
3495
3594
        s->dependent|= embedding->dep_tables;
3496
3595
        embedding= embedding->embedding;
3499
3598
      while (embedding);
3500
3599
      continue;
3501
3600
    }
3502
 
    if ((table->file->stats.records <= 1) &&
 
3601
    if ((table->s->system || table->file->stats.records <= 1) &&
3503
3602
        !s->dependent &&
3504
 
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
 
3603
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3604
        !table->fulltext_searched && !join->no_const_tables)
3505
3605
    {
3506
3606
      set_position(join,const_count++,s,(KEYUSE*) 0);
3507
3607
    }
3521
3621
    */
3522
3622
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3523
3623
    {
3524
 
      for (uint32_t j= 0 ; j < table_count ; j++)
 
3624
      for (uint j= 0 ; j < table_count ; j++)
3525
3625
      {
3526
3626
        table= stat[j].table;
3527
3627
        if (s->dependent & table->map)
3537
3637
      {
3538
3638
        join->tables=0;                 // Don't use join->table
3539
3639
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
3540
 
        return(1);
 
3640
        DBUG_RETURN(1);
3541
3641
      }
3542
3642
      s->key_dependent= s->dependent;
3543
3643
    }
3547
3647
    if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
3548
3648
                            conds, join->cond_equal,
3549
3649
                            ~outer_join, join->select_lex, &sargables))
3550
 
      return(1);
 
3650
      DBUG_RETURN(1);
3551
3651
 
3552
3652
  /* Read tables with 0 or 1 rows (system tables) */
3553
3653
  join->const_table_map= 0;
3563
3663
    if ((tmp=join_read_const_table(s, p_pos)))
3564
3664
    {
3565
3665
      if (tmp > 0)
3566
 
        return(1);                      // Fatal error
 
3666
        DBUG_RETURN(1);                 // Fatal error
3567
3667
    }
3568
3668
    else
3569
3669
      found_const_table_map|= s->table->map;
3635
3735
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
3636
3736
          {
3637
3737
            if (tmp > 0)
3638
 
              return(1);                        // Fatal error
 
3738
              DBUG_RETURN(1);                   // Fatal error
3639
3739
          }
3640
3740
          else
3641
3741
            found_const_table_map|= table->map;
3669
3769
          } while (keyuse->table == table && keyuse->key == key);
3670
3770
 
3671
3771
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3772
              !table->fulltext_searched && 
3672
3773
              !table->pos_in_table_list->embedding)
3673
3774
          {
3674
3775
            if ((table->key_info[key].flags & (HA_NOSAME))
3683
3784
                set_position(join,const_count++,s,start_keyuse);
3684
3785
                if (create_ref_for_key(join, s, start_keyuse,
3685
3786
                                       found_const_table_map))
3686
 
                  return(1);
 
3787
                  DBUG_RETURN(1);
3687
3788
                if ((tmp=join_read_const_table(s,
3688
3789
                                               join->positions+const_count-1)))
3689
3790
                {
3690
3791
                  if (tmp > 0)
3691
 
                    return(1);                  // Fatal error
 
3792
                    DBUG_RETURN(1);                     // Fatal error
3692
3793
                }
3693
3794
                else
3694
3795
                  found_const_table_map|= table->map;
3718
3819
      key_map possible_keys= field->key_start;
3719
3820
      possible_keys.intersect(field->table->keys_in_use_for_query);
3720
3821
      bool is_const= 1;
3721
 
      for (uint32_t j=0; j < sargables->num_values; j++)
 
3822
      for (uint j=0; j < sargables->num_values; j++)
3722
3823
        is_const&= sargables->arg_value[j]->const_item();
3723
3824
      if (is_const)
3724
3825
        join_tab[0].const_keys.merge(possible_keys);
3726
3827
  }
3727
3828
 
3728
3829
  if (pull_out_semijoin_tables(join))
3729
 
    return(true);
 
3830
    DBUG_RETURN(true);
3730
3831
 
3731
3832
  /* Calc how many (possible) matched records in each table */
3732
3833
 
3747
3848
      This is can't be to high as otherwise we are likely to use
3748
3849
      table scan.
3749
3850
    */
3750
 
    s->worst_seeks= cmin((double) s->found_records / 10,
 
3851
    s->worst_seeks= min((double) s->found_records / 10,
3751
3852
                        (double) s->read_time*3);
3752
3853
    if (s->worst_seeks < 2.0)                   // Fix for small tables
3753
3854
      s->worst_seeks=2.0;
3768
3869
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
3769
3870
                          1, &error);
3770
3871
      if (!select)
3771
 
        return(1);
 
3872
        DBUG_RETURN(1);
3772
3873
      records= get_quick_record_count(join->thd, select, s->table,
3773
3874
                                      &s->const_keys, join->row_limit);
3774
3875
      s->quick=select->quick;
3814
3915
  {
3815
3916
    optimize_keyuse(join, keyuse_array);
3816
3917
    if (choose_plan(join, all_table_map & ~join->const_table_map))
3817
 
      return(true);
 
3918
      DBUG_RETURN(true);
3818
3919
  }
3819
3920
  else
3820
3921
  {
3821
 
    memcpy(join->best_positions, join->positions,
3822
 
           sizeof(POSITION)*join->const_tables);
 
3922
    memcpy((uchar*) join->best_positions,(uchar*) join->positions,
 
3923
           sizeof(POSITION)*join->const_tables);
3823
3924
    join->best_read=1.0;
3824
3925
  }
3825
3926
  /* Generate an execution plan from the found optimal join order. */
3826
 
  return(join->thd->killed || get_best_combination(join));
 
3927
  DBUG_RETURN(join->thd->killed || get_best_combination(join));
3827
3928
}
3828
3929
 
3829
3930
 
3848
3949
  */
3849
3950
  bool          null_rejecting; 
3850
3951
  bool          *cond_guard; /* See KEYUSE::cond_guard */
3851
 
  uint32_t          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3952
  uint          sj_pred_no; /* See KEYUSE::sj_pred_no */
3852
3953
} KEY_FIELD;
3853
3954
 
3854
3955
/**
3877
3978
 
3878
3979
static KEY_FIELD *
3879
3980
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
3880
 
                 uint32_t and_level)
 
3981
                 uint and_level)
3881
3982
{
3882
3983
  if (start == new_fields)
3883
3984
    return start;                               // Impossible or
3900
4001
          The cause is as follows: Some of the tables are already known to be
3901
4002
          const tables (the detection code is in make_join_statistics(),
3902
4003
          above the update_ref_and_keys() call), but we didn't propagate 
3903
 
          information about this: Table::const_table is not set to true, and
 
4004
          information about this: TABLE::const_table is not set to true, and
3904
4005
          Item::update_used_tables() hasn't been called for each item.
3905
4006
          The result of this is that we're missing some 'ref' accesses.
3906
4007
          TODO: OptimizerTeam: Fix this
4004
4105
*/
4005
4106
 
4006
4107
static void
4007
 
add_key_field(KEY_FIELD **key_fields,uint32_t and_level, Item_func *cond,
4008
 
              Field *field, bool eq_func, Item **value, uint32_t num_values,
 
4108
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
 
4109
              Field *field, bool eq_func, Item **value, uint num_values,
4009
4110
              table_map usable_tables, SARGABLE_PARAM **sargables)
4010
4111
{
4011
 
  uint32_t exists_optimize= 0;
 
4112
  uint exists_optimize= 0;
4012
4113
  if (!(field->flags & PART_KEY_FLAG))
4013
4114
  {
4014
4115
    // Don't remove column IS NULL on a LEFT JOIN table
4016
4117
        !field->table->maybe_null || field->null_ptr)
4017
4118
      return;                                   // Not a key. Skip it
4018
4119
    exists_optimize= KEY_OPTIMIZE_EXISTS;
4019
 
    assert(num_values == 1);
 
4120
    DBUG_ASSERT(num_values == 1);
4020
4121
  }
4021
4122
  else
4022
4123
  {
4023
4124
    table_map used_tables=0;
4024
4125
    bool optimizable=0;
4025
 
    for (uint32_t i=0; i<num_values; i++)
 
4126
    for (uint i=0; i<num_values; i++)
4026
4127
    {
4027
4128
      used_tables|=(value[i])->used_tables();
4028
4129
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
4058
4159
      stat[0].key_dependent|=used_tables;
4059
4160
 
4060
4161
      bool is_const=1;
4061
 
      for (uint32_t i=0; i<num_values; i++)
 
4162
      for (uint i=0; i<num_values; i++)
4062
4163
      {
4063
4164
        if (!(is_const&= value[i]->const_item()))
4064
4165
          break;
4128
4229
    For the moment eq_func is always true. This slot is reserved for future
4129
4230
    extensions where we want to remembers other things than just eq comparisons
4130
4231
  */
4131
 
  assert(eq_func);
 
4232
  DBUG_ASSERT(eq_func);
4132
4233
  /* Store possible eq field */
4133
4234
  (*key_fields)->field=         field;
4134
4235
  (*key_fields)->eq_func=       eq_func;
4176
4277
*/
4177
4278
 
4178
4279
static void
4179
 
add_key_equal_fields(KEY_FIELD **key_fields, uint32_t and_level,
 
4280
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
4180
4281
                     Item_func *cond, Item_field *field_item,
4181
4282
                     bool eq_func, Item **val,
4182
 
                     uint32_t num_values, table_map usable_tables,
 
4283
                     uint num_values, table_map usable_tables,
4183
4284
                     SARGABLE_PARAM **sargables)
4184
4285
{
4185
4286
  Field *field= field_item->field;
4207
4308
}
4208
4309
 
4209
4310
static void
4210
 
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint32_t *and_level,
 
4311
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
4211
4312
               COND *cond, table_map usable_tables,
4212
4313
               SARGABLE_PARAM **sargables)
4213
4314
{
4289
4390
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4290
4391
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4291
4392
        values--;
4292
 
      assert(cond_func->functype() != Item_func::IN_FUNC ||
 
4393
      DBUG_ASSERT(cond_func->functype() != Item_func::IN_FUNC ||
4293
4394
                  cond_func->argument_count() != 2);
4294
4395
      add_key_equal_fields(key_fields, *and_level, cond_func,
4295
4396
                           (Item_field*) (cond_func->key_item()->real_item()),
4300
4401
    if (cond_func->functype() == Item_func::BETWEEN)
4301
4402
    {
4302
4403
      values= cond_func->arguments();
4303
 
      for (uint32_t i= 1 ; i < cond_func->argument_count() ; i++)
 
4404
      for (uint i= 1 ; i < cond_func->argument_count() ; i++)
4304
4405
      {
4305
4406
        Item_field *field_item;
4306
4407
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
4411
4512
static uint
4412
4513
max_part_bit(key_part_map bits)
4413
4514
{
4414
 
  uint32_t found;
 
4515
  uint found;
4415
4516
  for (found=0; bits & 1 ; found++,bits>>=1) ;
4416
4517
  return found;
4417
4518
}
4420
4521
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
4421
4522
{
4422
4523
  Field *field=key_field->field;
4423
 
  Table *form= field->table;
 
4524
  TABLE *form= field->table;
4424
4525
  KEYUSE keyuse;
4425
4526
 
4426
4527
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
4427
4528
  {
4428
 
    for (uint32_t key= 0 ; key < form->sizeKeys() ; key++)
 
4529
    for (uint key=0 ; key < form->s->keys ; key++)
4429
4530
    {
4430
4531
      if (!(form->keys_in_use_for_query.is_set(key)))
4431
4532
        continue;
4432
4533
 
4433
 
      uint32_t key_parts= (uint) form->key_info[key].key_parts;
4434
 
      for (uint32_t part=0 ; part <  key_parts ; part++)
 
4534
      uint key_parts= (uint) form->key_info[key].key_parts;
 
4535
      for (uint part=0 ; part <  key_parts ; part++)
4435
4536
      {
4436
4537
        if (field->eq(form->key_info[key].key_part[part].field))
4437
4538
        {
4445
4546
          keyuse.null_rejecting= key_field->null_rejecting;
4446
4547
          keyuse.cond_guard= key_field->cond_guard;
4447
4548
          keyuse.sj_pred_no= key_field->sj_pred_no;
4448
 
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
 
4549
          VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
4449
4550
        }
4450
4551
      }
4451
4552
    }
4507
4608
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
4508
4609
*/
4509
4610
 
4510
 
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
4511
 
                                  KEY_FIELD **end, uint32_t *and_level,
 
4611
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
 
4612
                                  KEY_FIELD **end, uint *and_level,
4512
4613
                                  SARGABLE_PARAM **sargables)
4513
4614
{
4514
 
  List_iterator<TableList> li(nested_join_table->nested_join->join_list);
4515
 
  List_iterator<TableList> li2(nested_join_table->nested_join->join_list);
 
4615
  List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
 
4616
  List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
4516
4617
  bool have_another = false;
4517
4618
  table_map tables= 0;
4518
 
  TableList *table;
4519
 
  assert(nested_join_table->nested_join);
 
4619
  TABLE_LIST *table;
 
4620
  DBUG_ASSERT(nested_join_table->nested_join);
4520
4621
 
4521
4622
  while ((table= li++) || (have_another && (li=li2, have_another=false,
4522
4623
                                            (table= li++))))
4528
4629
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
4529
4630
        have_another= true;
4530
4631
        li2= li;
4531
 
        li= List_iterator<TableList>(table->nested_join->join_list); 
 
4632
        li= List_iterator<TABLE_LIST>(table->nested_join->join_list); 
4532
4633
      }
4533
4634
      else
4534
4635
        add_key_fields_for_nj(join, table, end, and_level, sargables);
4566
4667
 
4567
4668
static bool
4568
4669
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
4569
 
                    uint32_t tables, COND *cond,
4570
 
                    COND_EQUAL *cond_equal __attribute__((unused)),
 
4670
                    uint tables, COND *cond, COND_EQUAL *cond_equal,
4571
4671
                    table_map normal_tables, SELECT_LEX *select_lex,
4572
4672
                    SARGABLE_PARAM **sargables)
4573
4673
{
4574
4674
  uint  and_level,i,found_eq_constant;
4575
4675
  KEY_FIELD *key_fields, *end, *field;
4576
 
  uint32_t sz;
4577
 
  uint32_t m= cmax(select_lex->max_equal_elems,(uint32_t)1);
 
4676
  uint sz;
 
4677
  uint m= max(select_lex->max_equal_elems,1);
4578
4678
  
4579
4679
  /* 
4580
4680
    We use the same piece of memory to store both  KEY_FIELD 
4597
4697
    can be not more than select_lex->max_equal_elems such 
4598
4698
    substitutions.
4599
4699
  */ 
4600
 
  sz= cmax(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4700
  sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
4601
4701
      (((thd->lex->current_select->cond_count+1)*2 +
4602
4702
        thd->lex->current_select->between_count)*m+1);
4603
4703
  if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
4643
4743
 
4644
4744
  /* Process ON conditions for the nested joins */
4645
4745
  {
4646
 
    List_iterator<TableList> li(*join_tab->join->join_list);
4647
 
    TableList *table;
 
4746
    List_iterator<TABLE_LIST> li(*join_tab->join->join_list);
 
4747
    TABLE_LIST *table;
4648
4748
    while ((table= li++))
4649
4749
    {
4650
4750
      if (table->nested_join)
4665
4765
    - keyparts without previous keyparts
4666
4766
      (e.g. if there is a key(a,b,c) but only b < 5 (or a=2 and c < 3) is
4667
4767
      used in the query, we drop the partial key parts from consideration).
 
4768
    Special treatment for ft-keys.
4668
4769
  */
4669
4770
  if (keyuse->elements)
4670
4771
  {
4673
4774
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
4674
4775
          (qsort_cmp) sort_keyuse);
4675
4776
 
4676
 
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
4677
 
    insert_dynamic(keyuse,(unsigned char*) &key_end);
 
4777
    bzero((char*) &key_end,sizeof(key_end));    /* Add for easy testing */
 
4778
    VOID(insert_dynamic(keyuse,(uchar*) &key_end));
4678
4779
 
4679
4780
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
4680
4781
    prev= &key_end;
4707
4808
      save_pos++;
4708
4809
    }
4709
4810
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
4710
 
    set_dynamic(keyuse,(unsigned char*) &key_end,i);
 
4811
    VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
4711
4812
    keyuse->elements=i;
4712
4813
  }
 
4814
  DBUG_EXECUTE("opt", print_keyuse_array(keyuse););
4713
4815
  return false;
4714
4816
}
4715
4817
 
4737
4839
        (map= (keyuse->used_tables & ~join->const_table_map &
4738
4840
               ~OUTER_REF_TABLE_BIT)))
4739
4841
    {
4740
 
      uint32_t tablenr;
 
4842
      uint tablenr;
4741
4843
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
4742
4844
      if (map == 1)                     // Only one table
4743
4845
      {
4744
 
        Table *tmp_table=join->all_tables[tablenr];
4745
 
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
 
4846
        TABLE *tmp_table=join->all_tables[tablenr];
 
4847
        keyuse->ref_table_rows= max(tmp_table->file->stats.records, 100);
4746
4848
      }
4747
4849
    }
4748
4850
    /*
4778
4880
{
4779
4881
  List<Item_field> indexed_fields;
4780
4882
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
4781
 
  order_st      *cur_group;
 
4883
  ORDER      *cur_group;
4782
4884
  Item_field *cur_item;
4783
4885
  key_map possible_keys(0);
4784
4886
 
4786
4888
  { /* Collect all query fields referenced in the GROUP clause. */
4787
4889
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
4788
4890
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
4789
 
                               (unsigned char*) &indexed_fields);
 
4891
                               (uchar*) &indexed_fields);
4790
4892
  }
4791
4893
  else if (join->select_distinct)
4792
4894
  { /* Collect all query fields referenced in the SELECT clause. */
4795
4897
    Item *item;
4796
4898
    while ((item= select_items_it++))
4797
4899
      item->walk(&Item::collect_item_field_processor, 0,
4798
 
                 (unsigned char*) &indexed_fields);
 
4900
                 (uchar*) &indexed_fields);
4799
4901
  }
4800
4902
  else
4801
4903
    return;
4824
4926
/** Save const tables first as used tables. */
4825
4927
 
4826
4928
static void
4827
 
set_position(JOIN *join,uint32_t idx,JOIN_TAB *table,KEYUSE *key)
 
4929
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
4828
4930
{
4829
4931
  join->positions[idx].table= table;
4830
4932
  join->positions[idx].key=key;
4861
4963
    Bitmap of bound IN-equalities.
4862
4964
*/
4863
4965
 
4864
 
uint64_t get_bound_sj_equalities(TableList *sj_nest, 
 
4966
ulonglong get_bound_sj_equalities(TABLE_LIST *sj_nest, 
4865
4967
                                  table_map remaining_tables)
4866
4968
{
4867
4969
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
4868
4970
  Item *item;
4869
 
  uint32_t i= 0;
4870
 
  uint64_t res= 0;
 
4971
  uint i= 0;
 
4972
  ulonglong res= 0;
4871
4973
  while ((item= li++))
4872
4974
  {
4873
4975
    /*
4878
4980
    */
4879
4981
    if (!(item->used_tables() & remaining_tables))
4880
4982
    {
4881
 
      res |= 1UL < i;
 
4983
      res |= 1ULL < i;
4882
4984
    }
4883
4985
  }
4884
4986
  return res;
4915
5017
                 JOIN_TAB  *s,
4916
5018
                 THD       *thd,
4917
5019
                 table_map remaining_tables,
4918
 
                 uint32_t      idx,
 
5020
                 uint      idx,
4919
5021
                 double    record_count,
4920
 
                 double    read_time __attribute__((unused)))
 
5022
                 double    read_time)
4921
5023
{
4922
5024
  KEYUSE *best_key=         0;
4923
 
  uint32_t best_max_key_part=   0;
4924
 
  bool found_constraint= 0;
 
5025
  uint best_max_key_part=   0;
 
5026
  my_bool found_constraint= 0;
4925
5027
  double best=              DBL_MAX;
4926
5028
  double best_time=         DBL_MAX;
4927
5029
  double records=           DBL_MAX;
4928
5030
  table_map best_ref_depends_map= 0;
4929
5031
  double tmp;
4930
5032
  ha_rows rec;
4931
 
  uint32_t best_is_sj_inside_out=    0;
 
5033
  uint best_is_sj_inside_out=    0;
 
5034
  DBUG_ENTER("best_access_path");
4932
5035
 
4933
5036
  if (s->keyuse)
4934
5037
  {                                            /* Use key if possible */
4935
 
    Table *table= s->table;
 
5038
    TABLE *table= s->table;
4936
5039
    KEYUSE *keyuse,*start_key=0;
4937
5040
    double best_records= DBL_MAX;
4938
 
    uint32_t max_key_part=0;
4939
 
    uint64_t bound_sj_equalities= 0;
 
5041
    uint max_key_part=0;
 
5042
    ulonglong bound_sj_equalities= 0;
4940
5043
    bool try_sj_inside_out= false;
4941
5044
    /*
4942
5045
      Discover the bound equalites. We need to do this, if
4966
5069
    {
4967
5070
      key_part_map found_part= 0;
4968
5071
      table_map found_ref= 0;
4969
 
      uint32_t key= keyuse->key;
 
5072
      uint key= keyuse->key;
4970
5073
      KEY *keyinfo= table->key_info+key;
4971
5074
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
4972
5075
      key_part_map const_part= 0;
4975
5078
 
4976
5079
      /* Calculate how many key segments of the current key we can use */
4977
5080
      start_key= keyuse;
4978
 
      uint64_t handled_sj_equalities=0;
 
5081
      ulonglong handled_sj_equalities=0;
4979
5082
      key_part_map sj_insideout_map= 0;
4980
5083
 
4981
5084
      do /* For each keypart */
4982
5085
      {
4983
 
        uint32_t keypart= keyuse->keypart;
 
5086
        uint keypart= keyuse->keypart;
4984
5087
        table_map best_part_found_ref= 0;
4985
5088
        double best_prev_record_reads= DBL_MAX;
4986
5089
        
5019
5122
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
5020
5123
          {
5021
5124
            if (!(remaining_tables & keyuse->used_tables))
5022
 
              bound_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5125
              bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5023
5126
            else
5024
5127
            {
5025
 
              handled_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5128
              handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5026
5129
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
5027
5130
            }
5028
5131
          }
5054
5157
        if (try_sj_inside_out && 
5055
5158
            table->covering_keys.is_set(key) &&
5056
5159
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
5057
 
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
 
5160
            PREV_BITS(ulonglong, s->emb_sj_nest->sj_in_exprs)) // (1)
5058
5161
        {
5059
 
          uint32_t n_fixed_parts= max_part_bit(found_part);
 
5162
          uint n_fixed_parts= max_part_bit(found_part);
5060
5163
          if (n_fixed_parts != keyinfo->key_parts &&
5061
5164
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
5062
5165
               PREV_BITS(uint, keyinfo->key_parts))
5066
5169
              check if all of them are covered.
5067
5170
            */
5068
5171
            sj_inside_out_scan= true;
 
5172
            DBUG_PRINT("info", ("Using sj InsideOut scan"));
5069
5173
            if (!n_fixed_parts)
5070
5174
            {
5071
5175
              /*
5097
5201
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
5098
5202
            !ref_or_null_part)
5099
5203
        {                                         /* use eq key */
5100
 
          max_key_part= UINT32_MAX;
 
5204
          max_key_part= (uint) ~0;
5101
5205
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
5102
5206
          {
5103
5207
            tmp = prev_record_reads(join, idx, found_ref);
5171
5275
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5172
5276
            }
5173
5277
            else
5174
 
              tmp= record_count*cmin(tmp,s->worst_seeks);
 
5278
              tmp= record_count*min(tmp,s->worst_seeks);
5175
5279
          }
5176
5280
        }
5177
5281
        else
5227
5331
            */
5228
5332
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
5229
5333
                table->quick_key_parts[key] == max_key_part &&          //(C2)
5230
 
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
 
5334
                table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
5231
5335
            {
5232
5336
              tmp= records= (double) table->quick_rows[key];
5233
5337
            }
5320
5424
              if (table->quick_keys.is_set(key) &&
5321
5425
                  table->quick_key_parts[key] <= max_key_part &&
5322
5426
                  const_part & (1 << table->quick_key_parts[key]) &&
5323
 
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
5324
 
                                                     const_part) ? 1 : 0) &&
 
5427
                  table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
 
5428
                                                         const_part) &&
5325
5429
                  records > (double) table->quick_rows[key])
5326
5430
              {
5327
5431
                tmp= records= (double) table->quick_rows[key];
5336
5440
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5337
5441
            }
5338
5442
            else
5339
 
              tmp= record_count * cmin(tmp,s->worst_seeks);
 
5443
              tmp= record_count * min(tmp,s->worst_seeks);
5340
5444
          }
5341
5445
          else
5342
5446
            tmp= best_time;                    // Do nothing
5474
5578
    /*
5475
5579
      We estimate the cost of evaluating WHERE clause for found records
5476
5580
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
5477
 
      tmp give us total cost of using Table SCAN
 
5581
      tmp give us total cost of using TABLE SCAN
5478
5582
    */
5479
5583
    if (best == DBL_MAX ||
5480
5584
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
5505
5609
      idx == join->const_tables &&
5506
5610
      s->table == join->sort_by_table &&
5507
5611
      join->unit->select_limit_cnt >= records)
5508
 
    join->sort_by_table= (Table*) 1;  // Must use temporary table
 
5612
    join->sort_by_table= (TABLE*) 1;  // Must use temporary table
5509
5613
 
5510
 
  return;
 
5614
  DBUG_VOID_RETURN;
5511
5615
}
5512
5616
 
5513
5617
 
5537
5641
static bool
5538
5642
choose_plan(JOIN *join, table_map join_tables)
5539
5643
{
5540
 
  uint32_t search_depth= join->thd->variables.optimizer_search_depth;
5541
 
  uint32_t prune_level=  join->thd->variables.optimizer_prune_level;
 
5644
  uint search_depth= join->thd->variables.optimizer_search_depth;
 
5645
  uint prune_level=  join->thd->variables.optimizer_prune_level;
5542
5646
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
5647
  DBUG_ENTER("choose_plan");
5543
5648
 
5544
5649
  join->cur_embedding_map= 0;
5545
5650
  reset_nj_counters(join->join_list);
5568
5673
      */
5569
5674
      join->best_read= DBL_MAX;
5570
5675
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
5571
 
        return(true);
 
5676
        DBUG_RETURN(true);
5572
5677
    } 
5573
5678
    else
5574
5679
    {
5576
5681
        /* Automatically determine a reasonable value for 'search_depth' */
5577
5682
        search_depth= determine_search_depth(join);
5578
5683
      if (greedy_search(join, join_tables, search_depth, prune_level))
5579
 
        return(true);
 
5684
        DBUG_RETURN(true);
5580
5685
    }
5581
5686
  }
5582
5687
 
5588
5693
  */
5589
5694
  if (join->thd->lex->is_single_level_stmt())
5590
5695
    join->thd->status_var.last_query_cost= join->best_read;
5591
 
  return(false);
 
5696
  DBUG_RETURN(false);
5592
5697
}
5593
5698
 
5594
5699
 
5672
5777
 
5673
5778
  @todo
5674
5779
    this value should be determined dynamically, based on statistics:
5675
 
    uint32_t max_tables_for_exhaustive_opt= 7;
 
5780
    uint max_tables_for_exhaustive_opt= 7;
5676
5781
 
5677
5782
  @todo
5678
5783
    this value could be determined by some mapping of the form:
5687
5792
static uint
5688
5793
determine_search_depth(JOIN *join)
5689
5794
{
5690
 
  uint32_t table_count=  join->tables - join->const_tables;
5691
 
  uint32_t search_depth;
 
5795
  uint table_count=  join->tables - join->const_tables;
 
5796
  uint search_depth;
5692
5797
  /* TODO: this value should be determined dynamically, based on statistics: */
5693
 
  uint32_t max_tables_for_exhaustive_opt= 7;
 
5798
  uint max_tables_for_exhaustive_opt= 7;
5694
5799
 
5695
5800
  if (table_count <= max_tables_for_exhaustive_opt)
5696
5801
    search_depth= table_count+1; // use exhaustive for small number of tables
5732
5837
optimize_straight_join(JOIN *join, table_map join_tables)
5733
5838
{
5734
5839
  JOIN_TAB *s;
5735
 
  uint32_t idx= join->const_tables;
 
5840
  uint idx= join->const_tables;
5736
5841
  double    record_count= 1.0;
5737
5842
  double    read_time=    0.0;
5738
5843
 
5753
5858
  if (join->sort_by_table &&
5754
5859
      join->sort_by_table != join->positions[join->const_tables].table->table)
5755
5860
    read_time+= record_count;  // We have to make a temp table
5756
 
  memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
5861
  memcpy((uchar*) join->best_positions, (uchar*) join->positions,
 
5862
         sizeof(POSITION)*idx);
5757
5863
  join->best_read= read_time;
5758
5864
}
5759
5865
 
5842
5948
static bool
5843
5949
greedy_search(JOIN      *join,
5844
5950
              table_map remaining_tables,
5845
 
              uint32_t      search_depth,
5846
 
              uint32_t      prune_level)
 
5951
              uint      search_depth,
 
5952
              uint      prune_level)
5847
5953
{
5848
5954
  double    record_count= 1.0;
5849
5955
  double    read_time=    0.0;
5850
 
  uint32_t      idx= join->const_tables; // index into 'join->best_ref'
5851
 
  uint32_t      best_idx;
5852
 
  uint32_t      size_remain;    // cardinality of remaining_tables
 
5956
  uint      idx= join->const_tables; // index into 'join->best_ref'
 
5957
  uint      best_idx;
 
5958
  uint      size_remain;    // cardinality of remaining_tables
5853
5959
  POSITION  best_pos;
5854
5960
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
5855
5961
 
 
5962
  DBUG_ENTER("greedy_search");
 
5963
 
5856
5964
  /* number of tables that remain to be optimized */
5857
5965
  size_remain= my_count_bits(remaining_tables);
5858
5966
 
5861
5969
    join->best_read= DBL_MAX;
5862
5970
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
5863
5971
                                         read_time, search_depth, prune_level))
5864
 
      return(true);
 
5972
      DBUG_RETURN(true);
5865
5973
 
5866
5974
    if (size_remain <= search_depth)
5867
5975
    {
5869
5977
        'join->best_positions' contains a complete optimal extension of the
5870
5978
        current partial QEP.
5871
5979
      */
5872
 
      return(false);
 
5980
      DBUG_EXECUTE("opt", print_plan(join, join->tables,
 
5981
                                     record_count, read_time, read_time,
 
5982
                                     "optimal"););
 
5983
      DBUG_RETURN(false);
5873
5984
    }
5874
5985
 
5875
5986
    /* select the first table in the optimal extension as most promising */
5887
5998
    JOIN_TAB *pos= join->best_ref[best_idx];
5888
5999
    while (pos && best_table != pos)
5889
6000
      pos= join->best_ref[++best_idx];
5890
 
    assert((pos != NULL)); // should always find 'best_table'
 
6001
    DBUG_ASSERT((pos != NULL)); // should always find 'best_table'
5891
6002
    /* move 'best_table' at the first free position in the array of joins */
5892
 
    std::swap(join->best_ref[idx], join->best_ref[best_idx]);
 
6003
    swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
5893
6004
 
5894
6005
    /* compute the cost of the new plan extended with 'best_table' */
5895
6006
    record_count*= join->positions[idx].records_read;
5898
6009
    remaining_tables&= ~(best_table->table->map);
5899
6010
    --size_remain;
5900
6011
    ++idx;
 
6012
 
 
6013
    DBUG_EXECUTE("opt", print_plan(join, join->tables,
 
6014
                                   record_count, read_time, read_time,
 
6015
                                   "extended"););
5901
6016
  } while (true);
5902
6017
}
5903
6018
 
6022
6137
static bool
6023
6138
best_extension_by_limited_search(JOIN      *join,
6024
6139
                                 table_map remaining_tables,
6025
 
                                 uint32_t      idx,
 
6140
                                 uint      idx,
6026
6141
                                 double    record_count,
6027
6142
                                 double    read_time,
6028
 
                                 uint32_t      search_depth,
6029
 
                                 uint32_t      prune_level)
 
6143
                                 uint      search_depth,
 
6144
                                 uint      prune_level)
6030
6145
{
 
6146
  DBUG_ENTER("best_extension_by_limited_search");
 
6147
 
6031
6148
  THD *thd= join->thd;
6032
6149
  if (thd->killed)  // Abort
6033
 
    return(true);
 
6150
    DBUG_RETURN(true);
 
6151
 
 
6152
  DBUG_EXECUTE("opt", print_plan(join, idx, read_time, record_count, idx,
 
6153
                                 "SOFAR:"););
6034
6154
 
6035
6155
  /* 
6036
6156
     'join' is a partial plan with lower cost than the best plan so far,
6040
6160
  double best_record_count= DBL_MAX;
6041
6161
  double best_read_time=    DBL_MAX;
6042
6162
 
 
6163
  DBUG_EXECUTE("opt", print_plan(join, idx, record_count, read_time, read_time,
 
6164
                                "part_plan"););
 
6165
 
6043
6166
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
6044
6167
  {
6045
6168
    table_map real_table_bit= s->table->map;
6068
6191
      if ((current_read_time +
6069
6192
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
6070
6193
      {
 
6194
        DBUG_EXECUTE("opt", print_plan(join, idx+1,
 
6195
                                       current_record_count,
 
6196
                                       read_time,
 
6197
                                       (current_read_time +
 
6198
                                        current_record_count / 
 
6199
                                        (double) TIME_FOR_COMPARE),
 
6200
                                       "prune_by_cost"););
6071
6201
        restore_prev_nj_state(s);
6072
6202
        restore_prev_sj_state(remaining_tables, s);
6073
6203
        continue;
6095
6225
        }
6096
6226
        else
6097
6227
        {
 
6228
          DBUG_EXECUTE("opt", print_plan(join, idx+1,
 
6229
                                         current_record_count,
 
6230
                                         read_time,
 
6231
                                         current_read_time,
 
6232
                                         "pruned_by_heuristic"););
6098
6233
          restore_prev_nj_state(s);
6099
6234
          restore_prev_sj_state(remaining_tables, s);
6100
6235
          continue;
6103
6238
 
6104
6239
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
6105
6240
      { /* Recursively expand the current partial plan */
6106
 
        std::swap(join->best_ref[idx], *pos);
 
6241
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6107
6242
        if (best_extension_by_limited_search(join,
6108
6243
                                             remaining_tables & ~real_table_bit,
6109
6244
                                             idx + 1,
6111
6246
                                             current_read_time,
6112
6247
                                             search_depth - 1,
6113
6248
                                             prune_level))
6114
 
          return(true);
6115
 
        std::swap(join->best_ref[idx], *pos);
 
6249
          DBUG_RETURN(true);
 
6250
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6116
6251
      }
6117
6252
      else
6118
6253
      { /*
6127
6262
          current_read_time+= current_record_count;
6128
6263
        if ((search_depth == 1) || (current_read_time < join->best_read))
6129
6264
        {
6130
 
          memcpy(join->best_positions, join->positions,
 
6265
          memcpy((uchar*) join->best_positions, (uchar*) join->positions,
6131
6266
                 sizeof(POSITION) * (idx + 1));
6132
6267
          join->best_read= current_read_time - 0.001;
6133
6268
        }
 
6269
        DBUG_EXECUTE("opt", print_plan(join, idx+1,
 
6270
                                       current_record_count,
 
6271
                                       read_time,
 
6272
                                       current_read_time,
 
6273
                                       "full_plan"););
6134
6274
      }
6135
6275
      restore_prev_nj_state(s);
6136
6276
      restore_prev_sj_state(remaining_tables, s);
6137
6277
    }
6138
6278
  }
6139
 
  return(false);
 
6279
  DBUG_RETURN(false);
6140
6280
}
6141
6281
 
6142
6282
 
6150
6290
    true        Fatal error
6151
6291
*/
6152
6292
static bool
6153
 
find_best(JOIN *join,table_map rest_tables,uint32_t idx,double record_count,
 
6293
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
6154
6294
          double read_time)
6155
6295
{
 
6296
  DBUG_ENTER("find_best");
6156
6297
  THD *thd= join->thd;
6157
6298
  if (thd->killed)
6158
 
    return(true);
 
6299
    DBUG_RETURN(true);
6159
6300
  if (!rest_tables)
6160
6301
  {
 
6302
    DBUG_PRINT("best",("read_time: %g  record_count: %g",read_time,
 
6303
                       record_count));
 
6304
 
6161
6305
    read_time+=record_count/(double) TIME_FOR_COMPARE;
6162
6306
    if (join->sort_by_table &&
6163
6307
        join->sort_by_table !=
6165
6309
      read_time+=record_count;                  // We have to make a temp table
6166
6310
    if (read_time < join->best_read)
6167
6311
    {
6168
 
      memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
6312
      memcpy((uchar*) join->best_positions,(uchar*) join->positions,
 
6313
             sizeof(POSITION)*idx);
6169
6314
      join->best_read= read_time - 0.001;
6170
6315
    }
6171
 
    return(false);
 
6316
    DBUG_RETURN(false);
6172
6317
  }
6173
6318
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
6174
 
    return(false);                                      /* Found better before */
 
6319
    DBUG_RETURN(false);                                 /* Found better before */
6175
6320
 
6176
6321
  JOIN_TAB *s;
6177
6322
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
6204
6349
          best_record_count=current_record_count;
6205
6350
          best_read_time=current_read_time;
6206
6351
        }
6207
 
        std::swap(join->best_ref[idx], *pos);
 
6352
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6208
6353
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
6209
6354
                      current_record_count,current_read_time))
6210
 
          return(true);
6211
 
        std::swap(join->best_ref[idx], *pos);
 
6355
          DBUG_RETURN(true);
 
6356
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6212
6357
      }
6213
6358
      restore_prev_nj_state(s);
6214
6359
      restore_prev_sj_state(rest_tables, s);
6216
6361
        break;                          // Don't test all combinations
6217
6362
    }
6218
6363
  }
6219
 
  return(false);
 
6364
  DBUG_RETURN(false);
6220
6365
}
6221
6366
 
6222
6367
 
6224
6369
  Find how much space the prevous read not const tables takes in cache.
6225
6370
*/
6226
6371
 
6227
 
static void calc_used_field_length(THD *thd __attribute__((unused)),
6228
 
                                   JOIN_TAB *join_tab)
 
6372
static void calc_used_field_length(THD *thd, JOIN_TAB *join_tab)
6229
6373
{
6230
 
  uint32_t null_fields,blobs,fields,rec_length;
 
6374
  uint null_fields,blobs,fields,rec_length;
6231
6375
  Field **f_ptr,*field;
6232
6376
  MY_BITMAP *read_set= join_tab->table->read_set;;
6233
6377
 
6236
6380
  {
6237
6381
    if (bitmap_is_set(read_set, field->field_index))
6238
6382
    {
6239
 
      uint32_t flags=field->flags;
 
6383
      uint flags=field->flags;
6240
6384
      fields++;
6241
6385
      rec_length+=field->pack_length();
6242
6386
      if (flags & BLOB_FLAG)
6246
6390
    }
6247
6391
  }
6248
6392
  if (null_fields)
6249
 
    rec_length+=(join_tab->table->getNullFields() + 7)/8;
 
6393
    rec_length+=(join_tab->table->s->null_fields+7)/8;
6250
6394
  if (join_tab->table->maybe_null)
6251
 
    rec_length+=sizeof(bool);
 
6395
    rec_length+=sizeof(my_bool);
6252
6396
  if (blobs)
6253
6397
  {
6254
 
    uint32_t blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
6255
 
                             (join_tab->table->getRecordLength()- rec_length));
6256
 
    rec_length+=(uint) cmax((uint)4,blob_length);
 
6398
    uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
 
6399
                             (join_tab->table->s->reclength- rec_length));
 
6400
    rec_length+=(uint) max(4,blob_length);
6257
6401
  }
6258
6402
  join_tab->used_fields=fields;
6259
6403
  join_tab->used_fieldlength=rec_length;
6262
6406
 
6263
6407
 
6264
6408
static uint
6265
 
cache_record_length(JOIN *join,uint32_t idx)
 
6409
cache_record_length(JOIN *join,uint idx)
6266
6410
{
6267
 
  uint32_t length=0;
 
6411
  uint length=0;
6268
6412
  JOIN_TAB **pos,**end;
6269
6413
  THD *thd=join->thd;
6270
6414
 
6333
6477
*/
6334
6478
 
6335
6479
static double
6336
 
prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref)
 
6480
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
6337
6481
{
6338
6482
  double found=1.0;
6339
6483
  POSITION *pos_end= join->positions - 1;
6373
6517
static bool
6374
6518
get_best_combination(JOIN *join)
6375
6519
{
6376
 
  uint32_t i,tablenr;
 
6520
  uint i,tablenr;
6377
6521
  table_map used_tables;
6378
6522
  JOIN_TAB *join_tab,*j;
6379
6523
  KEYUSE *keyuse;
6380
 
  uint32_t table_count;
 
6524
  uint table_count;
6381
6525
  THD *thd=join->thd;
 
6526
  DBUG_ENTER("get_best_combination");
6382
6527
 
6383
6528
  table_count=join->tables;
6384
6529
  if (!(join->join_tab=join_tab=
6385
6530
        (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
6386
 
    return(true);
 
6531
    DBUG_RETURN(true);
6387
6532
 
6388
6533
  join->full_join=0;
6389
6534
 
6390
6535
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
6391
6536
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
6392
6537
  {
6393
 
    Table *form;
 
6538
    TABLE *form;
6394
6539
    *j= *join->best_positions[tablenr].table;
6395
6540
    form=join->table[tablenr]=j->table;
6396
6541
    used_tables|= form->map;
6397
6542
    form->reginfo.join_tab=j;
6398
6543
    if (!*j->on_expr_ref)
6399
6544
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
 
6545
    DBUG_PRINT("info",("type: %d", j->type));
6400
6546
    if (j->type == JT_CONST)
6401
6547
      continue;                                 // Handled in make_join_stat..
6402
6548
 
6412
6558
        join->full_join=1;
6413
6559
    }
6414
6560
    else if (create_ref_for_key(join, j, keyuse, used_tables))
6415
 
      return(true);                        // Something went wrong
 
6561
      DBUG_RETURN(true);                        // Something went wrong
6416
6562
  }
6417
6563
 
6418
6564
  for (i=0 ; i < table_count ; i++)
6419
6565
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
6420
6566
  update_depend_map(join);
6421
 
  return(0);
 
6567
  DBUG_RETURN(0);
6422
6568
}
6423
6569
 
6424
6570
 
6427
6573
{
6428
6574
  KEYUSE *keyuse=org_keyuse;
6429
6575
  THD  *thd= join->thd;
6430
 
  uint32_t keyparts,length,key;
6431
 
  Table *table;
 
6576
  uint keyparts,length,key;
 
6577
  TABLE *table;
6432
6578
  KEY *keyinfo;
 
6579
  DBUG_ENTER("create_ref_for_key");
6433
6580
 
6434
6581
  /*  Use best key from find_best */
6435
6582
  table=j->table;
6438
6585
 
6439
6586
  {
6440
6587
    keyparts=length=0;
6441
 
    uint32_t found_part_ref_or_null= 0;
 
6588
    uint found_part_ref_or_null= 0;
6442
6589
    /*
6443
6590
      Calculate length for the used key
6444
6591
      Stop if there is a missing key part or when we find second key_part
6465
6612
  j->ref.key_parts=keyparts;
6466
6613
  j->ref.key_length=length;
6467
6614
  j->ref.key=(int) key;
6468
 
  if (!(j->ref.key_buff= (unsigned char*) thd->calloc(ALIGN_SIZE(length)*2)) ||
 
6615
  if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
6469
6616
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
6470
6617
                                                   (keyparts+1)))) ||
6471
6618
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
6472
6619
      !(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
6473
6620
  {
6474
 
    return(true);
 
6621
    DBUG_RETURN(true);
6475
6622
  }
6476
6623
  j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length);
6477
6624
  j->ref.key_err=1;
6480
6627
  keyuse=org_keyuse;
6481
6628
 
6482
6629
  store_key **ref_key= j->ref.key_copy;
6483
 
  unsigned char *key_buff=j->ref.key_buff, *null_ref_key= 0;
 
6630
  uchar *key_buff=j->ref.key_buff, *null_ref_key= 0;
6484
6631
  bool keyuse_uses_no_tables= true;
6485
6632
  {
6486
 
    uint32_t i;
 
6633
    uint i;
6487
6634
    for (i=0 ; i < keyparts ; keyuse++,i++)
6488
6635
    {
6489
6636
      while (keyuse->keypart != i ||
6490
6637
             ((~used_tables) & keyuse->used_tables))
6491
6638
        keyuse++;                               /* Skip other parts */
6492
6639
 
6493
 
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
 
6640
      uint maybe_null= test(keyinfo->key_part[i].null_bit);
6494
6641
      j->ref.items[i]=keyuse->val;              // Save for cond removal
6495
6642
      j->ref.cond_guards[i]= keyuse->cond_guard;
6496
6643
      if (keyuse->null_rejecting) 
6504
6651
                           maybe_null ?  key_buff : 0,
6505
6652
                           keyinfo->key_part[i].length, keyuse->val);
6506
6653
        if (thd->is_fatal_error)
6507
 
          return(true);
 
6654
          DBUG_RETURN(true);
6508
6655
        tmp.copy();
6509
6656
      }
6510
6657
      else
6545
6692
  }
6546
6693
  else
6547
6694
    j->type=JT_EQ_REF;
6548
 
  return(0);
 
6695
  DBUG_RETURN(0);
6549
6696
}
6550
6697
 
6551
6698
 
6552
6699
 
6553
6700
static store_key *
6554
6701
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
6555
 
              KEY_PART_INFO *key_part, unsigned char *key_buff, uint32_t maybe_null)
 
6702
              KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
6556
6703
{
6557
6704
  if (!((~used_tables) & keyuse->used_tables))          // if const item
6558
6705
  {
6595
6742
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
6596
6743
{
6597
6744
  bool error;
6598
 
  Table *table= field->table;
 
6745
  TABLE *table= field->table;
6599
6746
  THD *thd= table->in_use;
6600
6747
  ha_rows cuted_fields=thd->cuted_fields;
 
6748
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
 
6749
                                                   table->write_set);
6601
6750
 
6602
6751
  /*
6603
6752
    we should restore old value of count_cuted_fields because
6608
6757
  thd->count_cuted_fields= check_flag;
6609
6758
  error= item->save_in_field(field, 1);
6610
6759
  thd->count_cuted_fields= old_count_cuted_fields;
 
6760
  dbug_tmp_restore_column_map(table->write_set, old_map);
6611
6761
  return error || cuted_fields != thd->cuted_fields;
6612
6762
}
6613
6763
 
6614
6764
 
6615
6765
static bool
6616
 
make_simple_join(JOIN *join,Table *tmp_table)
 
6766
make_simple_join(JOIN *join,TABLE *tmp_table)
6617
6767
{
6618
 
  Table **tableptr;
 
6768
  TABLE **tableptr;
6619
6769
  JOIN_TAB *join_tab;
 
6770
  DBUG_ENTER("make_simple_join");
6620
6771
 
6621
6772
  /*
6622
 
    Reuse Table * and JOIN_TAB if already allocated by a previous call
 
6773
    Reuse TABLE * and JOIN_TAB if already allocated by a previous call
6623
6774
    to this function through JOIN::exec (may happen for sub-queries).
6624
6775
  */
6625
6776
  if (!join->table_reexec)
6626
6777
  {
6627
 
    if (!(join->table_reexec= (Table**) join->thd->alloc(sizeof(Table*))))
6628
 
      return(true);                        /* purecov: inspected */
 
6778
    if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*))))
 
6779
      DBUG_RETURN(true);                        /* purecov: inspected */
6629
6780
    if (join->tmp_join)
6630
6781
      join->tmp_join->table_reexec= join->table_reexec;
6631
6782
  }
6633
6784
  {
6634
6785
    if (!(join->join_tab_reexec=
6635
6786
          (JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
6636
 
      return(true);                        /* purecov: inspected */
 
6787
      DBUG_RETURN(true);                        /* purecov: inspected */
6637
6788
    if (join->tmp_join)
6638
6789
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
6639
6790
  }
6673
6824
  join_tab->ref.key_parts= 0;
6674
6825
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
6675
6826
  join_tab->do_firstmatch= NULL;
6676
 
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
 
6827
  bzero((char*) &join_tab->read_record,sizeof(join_tab->read_record));
6677
6828
  tmp_table->status=0;
6678
6829
  tmp_table->null_row=0;
6679
 
  return(false);
 
6830
  DBUG_RETURN(false);
6680
6831
}
6681
6832
 
6682
6833
 
6749
6900
 
6750
6901
static void add_not_null_conds(JOIN *join)
6751
6902
{
6752
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6903
  DBUG_ENTER("add_not_null_conds");
 
6904
  for (uint i=join->const_tables ; i < join->tables ; i++)
6753
6905
  {
6754
6906
    JOIN_TAB *tab=join->join_tab+i;
6755
6907
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
6756
6908
         tab->type == JT_REF_OR_NULL) &&
6757
6909
        !tab->table->maybe_null)
6758
6910
    {
6759
 
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
 
6911
      for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
6760
6912
      {
6761
6913
        if (tab->ref.null_rejecting & (1 << keypart))
6762
6914
        {
6763
6915
          Item *item= tab->ref.items[keypart];
6764
6916
          Item *notnull;
6765
 
          assert(item->type() == Item::FIELD_ITEM);
 
6917
          DBUG_ASSERT(item->type() == Item::FIELD_ITEM);
6766
6918
          Item_field *not_null_item= (Item_field*)item;
6767
6919
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
6768
6920
          /*
6773
6925
          if (!referred_tab || referred_tab->join != join)
6774
6926
            continue;
6775
6927
          if (!(notnull= new Item_func_isnotnull(not_null_item)))
6776
 
            return;
 
6928
            DBUG_VOID_RETURN;
6777
6929
          /*
6778
6930
            We need to do full fix_fields() call here in order to have correct
6779
6931
            notnull->const_item(). This is needed e.g. by test_quick_select 
6781
6933
            called.
6782
6934
          */
6783
6935
          if (notnull->fix_fields(join->thd, &notnull))
6784
 
            return;
 
6936
            DBUG_VOID_RETURN;
 
6937
          DBUG_EXECUTE("where",print_where(notnull,
 
6938
                                           referred_tab->table->alias,
 
6939
                                           QT_ORDINARY););
6785
6940
          add_cond_and_fix(&referred_tab->select_cond, notnull);
6786
6941
        }
6787
6942
      }
6788
6943
    }
6789
6944
  }
6790
 
  return;
 
6945
  DBUG_VOID_RETURN;
6791
6946
}
6792
6947
 
6793
6948
/**
6810
6965
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
6811
6966
{
6812
6967
  COND *tmp;
6813
 
  assert(cond != 0);
 
6968
  DBUG_ASSERT(cond != 0);
6814
6969
  if (tab == root_tab)
6815
6970
    return cond;
6816
6971
  if ((tmp= add_found_match_trig_cond(tab->first_upper, cond, root_tab)))
6869
7024
static void
6870
7025
make_outerjoin_info(JOIN *join)
6871
7026
{
6872
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
7027
  DBUG_ENTER("make_outerjoin_info");
 
7028
  for (uint i=join->const_tables ; i < join->tables ; i++)
6873
7029
  {
6874
7030
    JOIN_TAB *tab=join->join_tab+i;
6875
 
    Table *table=tab->table;
6876
 
    TableList *tbl= table->pos_in_table_list;
6877
 
    TableList *embedding= tbl->embedding;
 
7031
    TABLE *table=tab->table;
 
7032
    TABLE_LIST *tbl= table->pos_in_table_list;
 
7033
    TABLE_LIST *embedding= tbl->embedding;
6878
7034
 
6879
7035
    if (tbl->outer_join)
6880
7036
    {
6894
7050
      /* Ignore sj-nests: */
6895
7051
      if (!embedding->on_expr)
6896
7052
        continue;
6897
 
      nested_join_st *nested_join= embedding->nested_join;
 
7053
      NESTED_JOIN *nested_join= embedding->nested_join;
6898
7054
      if (!nested_join->counter_)
6899
7055
      {
6900
7056
        /* 
6915
7071
      nested_join->first_nested->last_inner= tab;
6916
7072
    }
6917
7073
  }
6918
 
  return;
 
7074
  DBUG_VOID_RETURN;
6919
7075
}
6920
7076
 
6921
7077
 
6923
7079
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
6924
7080
{
6925
7081
  THD *thd= join->thd;
 
7082
  DBUG_ENTER("make_join_select");
6926
7083
  if (select)
6927
7084
  {
6928
7085
    add_not_null_conds(join);
6940
7097
          make_cond_for_table(cond,
6941
7098
                              join->const_table_map,
6942
7099
                              (table_map) 0, 1);
 
7100
        DBUG_EXECUTE("where",print_where(const_cond,"constants", QT_ORDINARY););
6943
7101
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
6944
7102
             tab < join->join_tab+join->tables ; tab++)
6945
7103
        {
6953
7111
              continue;
6954
7112
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
6955
7113
            if (!tmp)
6956
 
              return(1);
 
7114
              DBUG_RETURN(1);
6957
7115
            tmp->quick_fix_field();
6958
7116
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
6959
7117
                                    new Item_cond_and(cond_tab->select_cond,
6960
7118
                                                      tmp);
6961
7119
            if (!cond_tab->select_cond)
6962
 
              return(1);
 
7120
              DBUG_RETURN(1);
6963
7121
            cond_tab->select_cond->quick_fix_field();
6964
7122
          }       
6965
7123
        }
6966
7124
        if (const_cond && !const_cond->val_int())
6967
7125
        {
6968
 
          return(1);     // Impossible const condition
 
7126
          DBUG_PRINT("info",("Found impossible WHERE condition"));
 
7127
          DBUG_RETURN(1);        // Impossible const condition
6969
7128
        }
6970
7129
      }
6971
7130
    }
6972
7131
    used_tables=((select->const_tables=join->const_table_map) |
6973
7132
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
6974
 
    for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
7133
    for (uint i=join->const_tables ; i < join->tables ; i++)
6975
7134
    {
6976
7135
      JOIN_TAB *tab=join->join_tab+i;
6977
7136
      /*
7032
7191
            in the ON part of an OUTER JOIN. In this case we want the code
7033
7192
            below to check if we should use 'quick' instead.
7034
7193
          */
7035
 
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
7194
          DBUG_PRINT("info", ("Item_int"));
 
7195
          tmp= new Item_int((longlong) 1,1);    // Always true
7036
7196
        }
7037
7197
 
7038
7198
      }
7039
7199
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
7040
7200
          tab->type == JT_EQ_REF)
7041
7201
      {
 
7202
        DBUG_EXECUTE("where",print_where(tmp,tab->table->alias, QT_ORDINARY););
7042
7203
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7043
 
                                       thd->memdup((unsigned char*) select,
 
7204
                                       thd->memdup((uchar*) select,
7044
7205
                                                   sizeof(*select)));
7045
7206
        if (!sel)
7046
 
          return(1);                    // End of memory
 
7207
          DBUG_RETURN(1);                       // End of memory
7047
7208
        /*
7048
7209
          If tab is an inner table of an outer join operation,
7049
7210
          add a match guard to the pushed down predicate.
7057
7218
            a cond, so neutralize the hack above.
7058
7219
          */
7059
7220
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
7060
 
            return(1);
 
7221
            DBUG_RETURN(1);
7061
7222
          tab->select_cond=sel->cond=tmp;
7062
7223
          /* Push condition to storage engine if this is enabled
7063
7224
             and the condition is not guarded */
7078
7239
          tab->select_cond= sel->cond= NULL;
7079
7240
 
7080
7241
        sel->head=tab->table;
 
7242
        DBUG_EXECUTE("where",print_where(tmp,tab->table->alias, QT_ORDINARY););
7081
7243
        if (tab->quick)
7082
7244
        {
7083
7245
          /* Use quick key read if it's a constant and it's not used
7095
7257
          }
7096
7258
          tab->quick=0;
7097
7259
        }
7098
 
        uint32_t ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
 
7260
        uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
7099
7261
        if (i == join->const_tables && ref_key)
7100
7262
        {
7101
7263
          if (!tab->const_keys.is_clear_all() &&
7102
7264
              tab->table->reginfo.impossible_range)
7103
 
            return(1);
 
7265
            DBUG_RETURN(1);
7104
7266
        }
7105
7267
        else if (tab->type == JT_ALL && ! use_quick_range)
7106
7268
        {
7107
7269
          if (!tab->const_keys.is_clear_all() &&
7108
7270
              tab->table->reginfo.impossible_range)
7109
 
            return(1);                          // Impossible range
 
7271
            DBUG_RETURN(1);                             // Impossible range
7110
7272
          /*
7111
7273
            We plan to scan all rows.
7112
7274
            Check again if we should use an index.
7152
7314
                                          HA_POS_ERROR :
7153
7315
                                          join->unit->select_limit_cnt),0,
7154
7316
                                          false) < 0)
7155
 
                return(1);                      // Impossible WHERE
 
7317
                DBUG_RETURN(1);                 // Impossible WHERE
7156
7318
            }
7157
7319
            else
7158
7320
              sel->cond=orig_cond;
7186
7348
                                         current_map,
7187
7349
                                         current_map, 0)))
7188
7350
            {
 
7351
              DBUG_EXECUTE("where",print_where(tmp,"cache", QT_ORDINARY););
7189
7352
              tab->cache.select=(SQL_SELECT*)
7190
 
                thd->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
 
7353
                thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
7191
7354
              tab->cache.select->cond=tmp;
7192
7355
              tab->cache.select->read_tables=join->const_table_map;
7193
7356
            }
7218
7381
            continue;
7219
7382
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7220
7383
          if (!tmp)
7221
 
            return(1);
 
7384
            DBUG_RETURN(1);
7222
7385
          tmp->quick_fix_field();
7223
7386
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7224
7387
                                    new Item_cond_and(cond_tab->select_cond,tmp);
7225
7388
          if (!cond_tab->select_cond)
7226
 
            return(1);
 
7389
            DBUG_RETURN(1);
7227
7390
          cond_tab->select_cond->quick_fix_field();
7228
7391
        }       
7229
7392
      }
7256
7419
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
7257
7420
                                                     tmp_cond,
7258
7421
                                                     first_inner_tab)))
7259
 
              return(1);
 
7422
              DBUG_RETURN(1);
7260
7423
            /* 
7261
7424
              Now add the guard turning the predicate off for 
7262
7425
              the null complemented row.
7263
7426
            */ 
 
7427
            DBUG_PRINT("info", ("Item_func_trig_cond"));
7264
7428
            tmp_cond= new Item_func_trig_cond(tmp_cond,
7265
7429
                                              &first_inner_tab->
7266
7430
                                              not_null_compl);
 
7431
            DBUG_PRINT("info", ("Item_func_trig_cond 0x%lx",
 
7432
                                (ulong) tmp_cond));
7267
7433
            if (tmp_cond)
7268
7434
              tmp_cond->quick_fix_field();
7269
7435
            /* Add the predicate to other pushed down predicates */
 
7436
            DBUG_PRINT("info", ("Item_cond_and"));
7270
7437
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
7271
7438
                                  new Item_cond_and(cond_tab->select_cond,
7272
7439
                                                    tmp_cond);
 
7440
            DBUG_PRINT("info", ("Item_cond_and 0x%lx",
 
7441
                                (ulong)cond_tab->select_cond));
7273
7442
            if (!cond_tab->select_cond)
7274
 
              return(1);
 
7443
              DBUG_RETURN(1);
7275
7444
            cond_tab->select_cond->quick_fix_field();
7276
7445
          }              
7277
7446
        }
7279
7448
      }
7280
7449
    }
7281
7450
  }
7282
 
  return(0);
 
7451
  DBUG_RETURN(0);
7283
7452
}
7284
7453
 
7285
7454
 
7305
7474
    false  No
7306
7475
*/
7307
7476
 
7308
 
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, 
 
7477
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno, 
7309
7478
                            bool other_tbls_ok)
7310
7479
{
7311
7480
  if (item->const_item())
7400
7569
    Index condition, or NULL if no condition could be inferred.
7401
7570
*/
7402
7571
 
7403
 
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno,
 
7572
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
7404
7573
                          bool other_tbls_ok)
7405
7574
{
7406
7575
  if (!cond)
7407
7576
    return NULL;
7408
7577
  if (cond->type() == Item::COND_ITEM)
7409
7578
  {
7410
 
    uint32_t n_marked= 0;
 
7579
    uint n_marked= 0;
7411
7580
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7412
7581
    {
7413
7582
      Item_cond_and *new_cond=new Item_cond_and;
7539
7708
    Try to extract and push the index condition down to table handler
7540
7709
*/
7541
7710
 
7542
 
static void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok)
 
7711
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
7543
7712
{
 
7713
  DBUG_ENTER("push_index_cond");
7544
7714
  Item *idx_cond;
7545
7715
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
7546
7716
      tab->join->thd->variables.engine_condition_pushdown)
7547
7717
  {
 
7718
    DBUG_EXECUTE("where",
 
7719
                 print_where(tab->select_cond, "full cond", QT_ORDINARY););
 
7720
 
7548
7721
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
7549
7722
                                  other_tbls_ok);
7550
7723
 
 
7724
    DBUG_EXECUTE("where",
 
7725
                 print_where(idx_cond, "idx cond", QT_ORDINARY););
 
7726
 
7551
7727
    if (idx_cond)
7552
7728
    {
7553
7729
      tab->pre_idx_push_select_cond= tab->select_cond;
7567
7743
 
7568
7744
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
7569
7745
 
 
7746
      DBUG_EXECUTE("where",
 
7747
                   print_where(row_cond, "remainder cond", QT_ORDINARY););
 
7748
      
7570
7749
      if (row_cond)
7571
7750
      {
7572
7751
        if (!idx_remainder_cond)
7583
7762
        tab->select_cond= idx_remainder_cond;
7584
7763
      if (tab->select)
7585
7764
      {
 
7765
        DBUG_EXECUTE("where",
 
7766
                     print_where(tab->select->cond,
 
7767
                                 "select_cond",
 
7768
                                 QT_ORDINARY););
 
7769
 
7586
7770
        tab->select->cond= tab->select_cond;
7587
7771
      }
7588
7772
    }
7589
7773
  }
7590
 
  return;
 
7774
  DBUG_VOID_RETURN;
7591
7775
}
7592
7776
 
7593
7777
 
7594
7778
 
7595
7779
    /*
7596
 
      Determine if the set is already ordered for order_st BY, so it can 
 
7780
      Determine if the set is already ordered for ORDER BY, so it can 
7597
7781
      disable join cache because it will change the ordering of the results.
7598
7782
      Code handles sort table that is at any location (not only first after 
7599
7783
      the const tables) despite the fact that it's currently prohibited.
7601
7785
      ordered. If there is a temp table the ordering is done as a last
7602
7786
      operation and doesn't prevent join cache usage.
7603
7787
    */
7604
 
uint32_t make_join_orderinfo(JOIN *join)
 
7788
uint make_join_orderinfo(JOIN *join)
7605
7789
{
7606
 
  uint32_t i;
 
7790
  uint i;
7607
7791
  if (join->need_tmp)
7608
7792
    return join->tables;
7609
7793
 
7610
7794
  for (i=join->const_tables ; i < join->tables ; i++)
7611
7795
  {
7612
7796
    JOIN_TAB *tab=join->join_tab+i;
7613
 
    Table *table=tab->table;
 
7797
    TABLE *table=tab->table;
7614
7798
    if ((table == join->sort_by_table && 
7615
7799
         (!join->order || join->skip_sort_order)) ||
7616
 
        (join->sort_by_table == (Table *) 1 && i != join->const_tables))
 
7800
        (join->sort_by_table == (TABLE *) 1 && i != join->const_tables))
7617
7801
    {
7618
7802
      break;
7619
7803
    }
7645
7829
*/
7646
7830
 
7647
7831
static bool
7648
 
make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
7832
make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
7649
7833
{
7650
 
  uint32_t i;
 
7834
  uint i;
7651
7835
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
7652
7836
  bool sorted= 1;
 
7837
  DBUG_ENTER("make_join_readinfo");
7653
7838
 
7654
7839
  for (i=join->const_tables ; i < join->tables ; i++)
7655
7840
  {
7656
7841
    JOIN_TAB *tab=join->join_tab+i;
7657
 
    Table *table=tab->table;
 
7842
    TABLE *table=tab->table;
7658
7843
    bool using_join_cache;
7659
7844
    tab->read_record.table= table;
7660
7845
    tab->read_record.file=table->file;
7667
7852
    sorted= 0;                                  // only first must be sorted
7668
7853
    if (tab->insideout_match_tab)
7669
7854
    {
7670
 
      if (!(tab->insideout_buf= (unsigned char*)join->thd->alloc(tab->table->key_info
 
7855
      if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
7671
7856
                                                         [tab->index].
7672
7857
                                                         key_length)))
7673
7858
        return true;
7819
8004
                  table->file->primary_key_is_clustered())
7820
8005
                tab->index= table->s->primary_key;
7821
8006
              else
7822
 
                tab->index= table->find_shortest_key(&table->covering_keys);
 
8007
                tab->index=find_shortest_key(table, & table->covering_keys);
7823
8008
            }
7824
8009
            tab->read_first_record= join_read_first;
7825
8010
            tab->type=JT_NEXT;          // Read with index_first / index_next
7831
8016
      }
7832
8017
      break;
7833
8018
    default:
 
8019
      DBUG_PRINT("error",("Table type %d found",tab->type)); /* purecov: deadcode */
7834
8020
      break;                                    /* purecov: deadcode */
7835
8021
    case JT_UNKNOWN:
7836
8022
    case JT_MAYBE_REF:
7838
8024
    }
7839
8025
  }
7840
8026
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
7841
 
  return(false);
 
8027
  DBUG_RETURN(false);
7842
8028
}
7843
8029
 
7844
8030
 
7883
8069
  select= 0;
7884
8070
  delete quick;
7885
8071
  quick= 0;
7886
 
  if (cache.buff)
7887
 
    free(cache.buff);
 
8072
  x_free(cache.buff);
7888
8073
  cache.buff= 0;
7889
8074
  limit= 0;
7890
8075
  if (table)
7938
8123
    a correlated subquery itself, but has subqueries, we can free it
7939
8124
    fully and also free JOINs of all its subqueries. The exception
7940
8125
    is a subquery in SELECT list, e.g: @n
7941
 
    SELECT a, (select cmax(b) from t1) group by c @n
 
8126
    SELECT a, (select max(b) from t1) group by c @n
7942
8127
    This subquery will not be evaluated at first sweep and its value will
7943
8128
    not be inserted into the temporary table. Instead, it's evaluated
7944
8129
    when selecting from the temporary table. Therefore, it can't be freed
7958
8143
  */
7959
8144
  bool full= (!select_lex->uncacheable && !thd->lex->describe);
7960
8145
  bool can_unlock= full;
 
8146
  DBUG_ENTER("JOIN::join_free");
7961
8147
 
7962
8148
  cleanup(full);
7963
8149
 
8000
8186
    lock= 0;
8001
8187
  }
8002
8188
 
8003
 
  return;
 
8189
  DBUG_VOID_RETURN;
8004
8190
}
8005
8191
 
8006
8192
 
8018
8204
 
8019
8205
void JOIN::cleanup(bool full)
8020
8206
{
 
8207
  DBUG_ENTER("JOIN::cleanup");
 
8208
 
8021
8209
  if (table)
8022
8210
  {
8023
8211
    JOIN_TAB *tab,*end;
8077
8265
    }
8078
8266
    tmp_table_param.cleanup();
8079
8267
  }
8080
 
  return;
 
8268
  DBUG_VOID_RETURN;
8081
8269
}
8082
8270
 
8083
8271
 
8084
8272
/**
8085
 
  Remove the following expressions from order_st BY and GROUP BY:
 
8273
  Remove the following expressions from ORDER BY and GROUP BY:
8086
8274
  Constant expressions @n
8087
8275
  Expression that only uses tables that are of type EQ_REF and the reference
8088
 
  is in the order_st list or if all refereed tables are of the above type.
 
8276
  is in the ORDER list or if all refereed tables are of the above type.
8089
8277
 
8090
8278
  In the following, the X field can be removed:
8091
8279
  @code
8092
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
8093
 
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
 
8280
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
 
8281
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
8094
8282
  @endcode
8095
8283
 
8096
8284
  These can't be optimized:
8097
8285
  @code
8098
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
8099
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
8100
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
 
8286
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
 
8287
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
 
8288
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
8101
8289
  @endcode
8102
8290
*/
8103
8291
 
8104
8292
static bool
8105
 
eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
 
8293
eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab)
8106
8294
{
8107
8295
  if (tab->cached_eq_ref_table)                 // If cached
8108
8296
    return tab->eq_ref_table;
8114
8302
    return (tab->eq_ref_table=0);               // We must use this
8115
8303
  Item **ref_item=tab->ref.items;
8116
8304
  Item **end=ref_item+tab->ref.key_parts;
8117
 
  uint32_t found=0;
 
8305
  uint found=0;
8118
8306
  table_map map=tab->table->map;
8119
8307
 
8120
8308
  for (; ref_item != end ; ref_item++)
8121
8309
  {
8122
8310
    if (! (*ref_item)->const_item())
8123
8311
    {                                           // Not a const ref
8124
 
      order_st *order;
 
8312
      ORDER *order;
8125
8313
      for (order=start_order ; order ; order=order->next)
8126
8314
      {
8127
8315
        if ((*ref_item)->eq(order->item[0],0))
8130
8318
      if (order)
8131
8319
      {
8132
8320
        found++;
8133
 
        assert(!(order->used & map));
 
8321
        DBUG_ASSERT(!(order->used & map));
8134
8322
        order->used|=map;
8135
 
        continue;                               // Used in order_st BY
 
8323
        continue;                               // Used in ORDER BY
8136
8324
      }
8137
8325
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
8138
8326
        return (tab->eq_ref_table=0);
8154
8342
 
8155
8343
 
8156
8344
static bool
8157
 
only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
 
8345
only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables)
8158
8346
{
 
8347
  if (specialflag &  SPECIAL_SAFE_MODE)
 
8348
    return 0;                   // skip this optimize /* purecov: inspected */
8159
8349
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
8160
8350
  {
8161
8351
    if (tables & 1 && !eq_ref_table(join, order, *tab))
8176
8366
    TABLE_REF *ref= &join_tab->ref;
8177
8367
    table_map depend_map=0;
8178
8368
    Item **item=ref->items;
8179
 
    uint32_t i;
 
8369
    uint i;
8180
8370
    for (i=0 ; i < ref->key_parts ; i++,item++)
8181
8371
      depend_map|=(*item)->used_tables();
8182
8372
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
8194
8384
 
8195
8385
/** Update the dependency map for the sort order. */
8196
8386
 
8197
 
static void update_depend_map(JOIN *join, order_st *order)
 
8387
static void update_depend_map(JOIN *join, ORDER *order)
8198
8388
{
8199
8389
  for (; order ; order=order->next)
8200
8390
  {
8218
8408
 
8219
8409
 
8220
8410
/**
8221
 
  Remove all constants and check if order_st only contains simple
 
8411
  Remove all constants and check if ORDER only contains simple
8222
8412
  expressions.
8223
8413
 
8224
8414
  simple_order is set to 1 if sort_order only uses fields from head table
8236
8426
    Returns new sort order
8237
8427
*/
8238
8428
 
8239
 
static order_st *
8240
 
remove_const(JOIN *join,order_st *first_order, COND *cond,
 
8429
static ORDER *
 
8430
remove_const(JOIN *join,ORDER *first_order, COND *cond,
8241
8431
             bool change_list, bool *simple_order)
8242
8432
{
8243
8433
  if (join->tables == join->const_tables)
8244
8434
    return change_list ? 0 : first_order;               // No need to sort
8245
8435
 
8246
 
  order_st *order,**prev_ptr;
 
8436
  ORDER *order,**prev_ptr;
8247
8437
  table_map first_table= join->join_tab[join->const_tables].table->map;
8248
8438
  table_map not_const_tables= ~join->const_table_map;
8249
8439
  table_map ref;
 
8440
  DBUG_ENTER("remove_const");
8250
8441
 
8251
8442
  prev_ptr= &first_order;
8252
8443
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
8263
8454
    {
8264
8455
      if (order->item[0]->with_subselect)
8265
8456
        order->item[0]->val_str(&order->item[0]->str_value);
 
8457
      DBUG_PRINT("info",("removing: %s", order->item[0]->full_name()));
8266
8458
      continue;                                 // skip const item
8267
8459
    }
8268
8460
    else
8274
8466
        Item *comp_item=0;
8275
8467
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
8276
8468
        {
 
8469
          DBUG_PRINT("info",("removing: %s", order->item[0]->full_name()));
8277
8470
          continue;
8278
8471
        }
8279
8472
        if ((ref=order_tables & (not_const_tables ^ first_table)))
8281
8474
          if (!(order_tables & first_table) &&
8282
8475
              only_eq_ref_tables(join,first_order, ref))
8283
8476
          {
 
8477
            DBUG_PRINT("info",("removing: %s", order->item[0]->full_name()));
8284
8478
            continue;
8285
8479
          }
8286
8480
          *simple_order=0;                      // Must do a temp table to sort
8295
8489
    *prev_ptr=0;
8296
8490
  if (prev_ptr == &first_order)                 // Nothing to sort/group
8297
8491
    *simple_order=1;
8298
 
  return(first_order);
 
8492
  DBUG_PRINT("exit",("simple_order: %d",(int) *simple_order));
 
8493
  DBUG_RETURN(first_order);
8299
8494
}
8300
8495
 
8301
8496
 
8302
8497
static int
8303
 
return_zero_rows(JOIN *join, select_result *result,TableList *tables,
8304
 
                 List<Item> &fields, bool send_row, uint64_t select_options,
 
8498
return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables,
 
8499
                 List<Item> &fields, bool send_row, ulonglong select_options,
8305
8500
                 const char *info, Item *having)
8306
8501
{
 
8502
  DBUG_ENTER("return_zero_rows");
 
8503
 
8307
8504
  if (select_options & SELECT_DESCRIBE)
8308
8505
  {
8309
8506
    select_describe(join, false, false, false, info);
8310
 
    return(0);
 
8507
    DBUG_RETURN(0);
8311
8508
  }
8312
8509
 
8313
8510
  join->join_free();
8314
8511
 
8315
8512
  if (send_row)
8316
8513
  {
8317
 
    for (TableList *table= tables; table; table= table->next_leaf)
 
8514
    for (TABLE_LIST *table= tables; table; table= table->next_leaf)
8318
8515
      mark_as_null_row(table->table);           // All fields are NULL
8319
8516
    if (having && having->val_int() == 0)
8320
8517
      send_row=0;
8334
8531
  }
8335
8532
  /* Update results for FOUND_ROWS */
8336
8533
  join->thd->limit_found_rows= join->thd->examined_row_count= 0;
8337
 
  return(0);
 
8534
  DBUG_RETURN(0);
8338
8535
}
8339
8536
 
8340
8537
/*
8346
8543
    must clear only the non-const tables, as const tables
8347
8544
    are not re-calculated.
8348
8545
  */
8349
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
8546
  for (uint i=join->const_tables ; i < join->tables ; i++)
8350
8547
    mark_as_null_row(join->table[i]);           // All fields are NULL
8351
8548
}
8352
8549
 
8638
8835
 
8639
8836
      if (field_item->result_type() == STRING_RESULT)
8640
8837
      {
8641
 
        const CHARSET_INFO * const cs= ((Field_str*) field_item->field)->charset();
 
8838
        CHARSET_INFO *cs= ((Field_str*) field_item->field)->charset();
8642
8839
        if (!item)
8643
8840
        {
8644
8841
          Item_func_eq *eq_item;
8710
8907
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
8711
8908
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
8712
8909
8713
 
  uint32_t n= left_row->cols();
8714
 
  for (uint32_t i= 0 ; i < n; i++)
 
8910
  uint n= left_row->cols();
 
8911
  for (uint i= 0 ; i < n; i++)
8715
8912
  {
8716
8913
    bool is_converted;
8717
8914
    Item *left_item= left_row->element_index(i);
8955
9152
    {
8956
9153
      int n= cond_equal.current_level.elements + eq_list.elements;
8957
9154
      if (n == 0)
8958
 
        return new Item_int((int64_t) 1,1);
 
9155
        return new Item_int((longlong) 1,1);
8959
9156
      else if (n == 1)
8960
9157
      {
8961
9158
        if ((item_equal= cond_equal.current_level.pop()))
8998
9195
      as soon the field is not of a string type or the field reference is
8999
9196
      an argument of a comparison predicate. 
9000
9197
    */ 
9001
 
    unsigned char *is_subst_valid= (unsigned char *) 1;
 
9198
    uchar *is_subst_valid= (uchar *) 1;
9002
9199
    cond= cond->compile(&Item::subst_argument_checker,
9003
9200
                        &is_subst_valid, 
9004
9201
                        &Item::equal_fields_propagator,
9005
 
                        (unsigned char *) inherited);
 
9202
                        (uchar *) inherited);
9006
9203
    cond->update_used_tables();
9007
9204
  }
9008
9205
  return cond;
9077
9274
   
9078
9275
static COND *build_equal_items(THD *thd, COND *cond,
9079
9276
                               COND_EQUAL *inherited,
9080
 
                               List<TableList> *join_list,
 
9277
                               List<TABLE_LIST> *join_list,
9081
9278
                               COND_EQUAL **cond_equal_ref)
9082
9279
{
9083
9280
  COND_EQUAL *cond_equal= 0;
9105
9302
 
9106
9303
  if (join_list)
9107
9304
  {
9108
 
    TableList *table;
9109
 
    List_iterator<TableList> li(*join_list);
 
9305
    TABLE_LIST *table;
 
9306
    List_iterator<TABLE_LIST> li(*join_list);
9110
9307
 
9111
9308
    while ((table= li++))
9112
9309
    {
9113
9310
      if (table->on_expr)
9114
9311
      {
9115
 
        List<TableList> *nested_join_list= table->nested_join ?
 
9312
        List<TABLE_LIST> *nested_join_list= table->nested_join ?
9116
9313
          &table->nested_join->join_list : NULL;
9117
9314
        /*
9118
9315
          We can modify table->on_expr because its old value will
9219
9416
  List<Item> eq_list;
9220
9417
  Item_func_eq *eq_item= 0;
9221
9418
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
9222
 
    return new Item_int((int64_t) 0,1); 
 
9419
    return new Item_int((longlong) 0,1); 
9223
9420
  Item *item_const= item_equal->get_const();
9224
9421
  Item_equal_iterator it(*item_equal);
9225
9422
  Item *head;
9264
9461
  if (!cond && !eq_list.head())
9265
9462
  {
9266
9463
    if (!eq_item)
9267
 
      return new Item_int((int64_t) 1,1);
 
9464
      return new Item_int((longlong) 1,1);
9268
9465
    return eq_item;
9269
9466
  }
9270
9467
 
9274
9471
    cond= new Item_cond_and(eq_list);
9275
9472
  else
9276
9473
  {
9277
 
    assert(cond->type() == Item::COND_ITEM);
 
9474
    DBUG_ASSERT(cond->type() == Item::COND_ITEM);
9278
9475
    ((Item_cond *) cond)->add_at_head(&eq_list);
9279
9476
  }
9280
9477
 
9366
9563
    }
9367
9564
    if (cond->type() == Item::COND_ITEM &&
9368
9565
        !((Item_cond*)cond)->argument_list()->elements)
9369
 
      cond= new Item_int((int32_t)cond->val_bool());
 
9566
      cond= new Item_int((int32)cond->val_bool());
9370
9567
 
9371
9568
  }
9372
9569
  else if (cond->type() == Item::FUNC_ITEM && 
9436
9633
        */  
9437
9634
        if (!possible_keys.is_clear_all())
9438
9635
        {
9439
 
          Table *tab= field->table;
 
9636
          TABLE *tab= field->table;
9440
9637
          KEYUSE *use;
9441
9638
          for (use= stat->keyuse; use && use->table == tab; use++)
9442
9639
            if (possible_keys.is_set(use->key) && 
9748
9945
*/
9749
9946
 
9750
9947
static COND *
9751
 
simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top,
 
9948
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
9752
9949
               bool in_sj)
9753
9950
{
9754
 
  TableList *table;
9755
 
  nested_join_st *nested_join;
9756
 
  TableList *prev_table= 0;
9757
 
  List_iterator<TableList> li(*join_list);
 
9951
  TABLE_LIST *table;
 
9952
  NESTED_JOIN *nested_join;
 
9953
  TABLE_LIST *prev_table= 0;
 
9954
  List_iterator<TABLE_LIST> li(*join_list);
 
9955
  DBUG_ENTER("simplify_joins");
9758
9956
 
9759
9957
  /* 
9760
9958
    Try to simplify join operations from join_list.
9787
9985
 
9788
9986
        if (!table->prep_on_expr || expr != table->on_expr)
9789
9987
        {
9790
 
          assert(expr);
 
9988
          DBUG_ASSERT(expr);
9791
9989
 
9792
9990
          table->on_expr= expr;
9793
9991
          table->prep_on_expr= expr->copy_andor_structure(join->thd);
9830
10028
          conds= and_conds(conds, table->on_expr);
9831
10029
          conds->top_level_item();
9832
10030
          /* conds is always a new item as both cond and on_expr existed */
9833
 
          assert(!conds->fixed);
 
10031
          DBUG_ASSERT(!conds->fixed);
9834
10032
          conds->fix_fields(join->thd, &conds);
9835
10033
        }
9836
10034
        else
9904
10102
    }
9905
10103
    else if (nested_join && !table->on_expr)
9906
10104
    {
9907
 
      TableList *tbl;
9908
 
      List_iterator<TableList> it(nested_join->join_list);
 
10105
      TABLE_LIST *tbl;
 
10106
      List_iterator<TABLE_LIST> it(nested_join->join_list);
9909
10107
      while ((tbl= it++))
9910
10108
      {
9911
10109
        tbl->embedding= table->embedding;
9914
10112
      li.replace(nested_join->join_list);
9915
10113
    }
9916
10114
  }
9917
 
  return(conds); 
 
10115
  DBUG_RETURN(conds); 
9918
10116
}
9919
10117
 
9920
10118
 
9938
10136
    First unused bit in nested_join_map after the call.
9939
10137
*/
9940
10138
 
9941
 
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, 
9942
 
                                          uint32_t first_unused)
 
10139
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, 
 
10140
                                          uint first_unused)
9943
10141
{
9944
 
  List_iterator<TableList> li(*join_list);
9945
 
  TableList *table;
 
10142
  List_iterator<TABLE_LIST> li(*join_list);
 
10143
  TABLE_LIST *table;
 
10144
  DBUG_ENTER("build_bitmap_for_nested_joins");
9946
10145
  while ((table= li++))
9947
10146
  {
9948
 
    nested_join_st *nested_join;
 
10147
    NESTED_JOIN *nested_join;
9949
10148
    if ((nested_join= table->nested_join))
9950
10149
    {
9951
10150
      /*
9969
10168
      }
9970
10169
    }
9971
10170
  }
9972
 
  return(first_unused);
 
10171
  DBUG_RETURN(first_unused);
9973
10172
}
9974
10173
 
9975
10174
 
9976
10175
/**
9977
 
  Set nested_join_st::counter=0 in all nested joins in passed list.
 
10176
  Set NESTED_JOIN::counter=0 in all nested joins in passed list.
9978
10177
 
9979
 
    Recursively set nested_join_st::counter=0 for all nested joins contained in
 
10178
    Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
9980
10179
    the passed join_list.
9981
10180
 
9982
10181
  @param join_list  List of nested joins to process. It may also contain base
9983
10182
                    tables which will be ignored.
9984
10183
*/
9985
10184
 
9986
 
static void reset_nj_counters(List<TableList> *join_list)
 
10185
static void reset_nj_counters(List<TABLE_LIST> *join_list)
9987
10186
{
9988
 
  List_iterator<TableList> li(*join_list);
9989
 
  TableList *table;
 
10187
  List_iterator<TABLE_LIST> li(*join_list);
 
10188
  TABLE_LIST *table;
 
10189
  DBUG_ENTER("reset_nj_counters");
9990
10190
  while ((table= li++))
9991
10191
  {
9992
 
    nested_join_st *nested_join;
 
10192
    NESTED_JOIN *nested_join;
9993
10193
    if ((nested_join= table->nested_join))
9994
10194
    {
9995
10195
      nested_join->counter_= 0;
9996
10196
      reset_nj_counters(&nested_join->join_list);
9997
10197
    }
9998
10198
  }
9999
 
  return;
 
10199
  DBUG_VOID_RETURN;
10000
10200
}
10001
10201
 
10002
10202
 
10012
10212
 
10013
10213
  @verbatim
10014
10214
     IMPLEMENTATION 
10015
 
       LIMITATIONS ON JOIN order_st
 
10215
       LIMITATIONS ON JOIN ORDER
10016
10216
         The nested [outer] joins executioner algorithm imposes these limitations
10017
10217
         on join order:
10018
10218
         1. "Outer tables first" -  any "outer" table must be before any 
10076
10276
         position:
10077
10277
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
10078
10278
             joins) we've opened but didn't close.
10079
 
          2. {each nested_join_st structure not simplified away}->counter - number
 
10279
          2. {each NESTED_JOIN structure not simplified away}->counter - number
10080
10280
             of this nested join's children that have already been added to to
10081
10281
             the partial join order.
10082
10282
  @endverbatim
10095
10295
 
10096
10296
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
10097
10297
{
10098
 
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
 
10298
  TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding;
10099
10299
  JOIN *join= last_tab->join;
10100
10300
 
10101
10301
  if (join->cur_embedding_map & ~next_tab->embedding_map)
10152
10352
 
10153
10353
static void restore_prev_nj_state(JOIN_TAB *last)
10154
10354
{
10155
 
  TableList *last_emb= last->table->pos_in_table_list->embedding;
 
10355
  TABLE_LIST *last_emb= last->table->pos_in_table_list->embedding;
10156
10356
  JOIN *join= last->join;
10157
10357
  while (last_emb)
10158
10358
  {
10175
10375
static 
10176
10376
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
10177
10377
{
10178
 
  TableList *emb_sj_nest;
 
10378
  TABLE_LIST *emb_sj_nest;
10179
10379
  if ((emb_sj_nest= tab->emb_sj_nest))
10180
10380
  {
10181
10381
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
10193
10393
static void restore_prev_sj_state(const table_map remaining_tables, 
10194
10394
                                  const JOIN_TAB *tab)
10195
10395
{
10196
 
  TableList *emb_sj_nest;
 
10396
  TABLE_LIST *emb_sj_nest;
10197
10397
  if ((emb_sj_nest= tab->emb_sj_nest))
10198
10398
  {
10199
10399
    /* If we're removing the last SJ-inner table, remove the sj-nest */
10207
10407
 
10208
10408
 
10209
10409
static COND *
10210
 
optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list,
 
10410
optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list,
10211
10411
              Item::cond_result *cond_value)
10212
10412
{
10213
10413
  THD *thd= join->thd;
 
10414
  DBUG_ENTER("optimize_cond");
10214
10415
 
10215
10416
  if (!conds)
10216
10417
    *cond_value= Item::COND_TRUE;
10224
10425
      predicate. Substitute a constant instead of this field if the
10225
10426
      multiple equality contains a constant.
10226
10427
    */ 
 
10428
    DBUG_EXECUTE("where", print_where(conds, "original", QT_ORDINARY););
10227
10429
    conds= build_equal_items(join->thd, conds, NULL, join_list,
10228
10430
                             &join->cond_equal);
 
10431
    DBUG_EXECUTE("where",print_where(conds,"after equal_items", QT_ORDINARY););
10229
10432
 
10230
10433
    /* change field = field to field = const for each found field = const */
10231
10434
    propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
10233
10436
      Remove all instances of item == item
10234
10437
      Remove all and-levels where CONST item != CONST item
10235
10438
    */
 
10439
    DBUG_EXECUTE("where",print_where(conds,"after const change", QT_ORDINARY););
10236
10440
    conds= remove_eq_conds(thd, conds, cond_value) ;
 
10441
    DBUG_EXECUTE("info",print_where(conds,"after remove", QT_ORDINARY););
10237
10442
  }
10238
 
  return(conds);
 
10443
  DBUG_RETURN(conds);
10239
10444
}
10240
10445
 
10241
10446
 
10270
10475
        li.remove();
10271
10476
      else if (item != new_item)
10272
10477
      {
10273
 
        li.replace(new_item);
 
10478
        VOID(li.replace(new_item));
10274
10479
        should_fix_fields=1;
10275
10480
      }
10276
10481
      if (*cond_value == Item::COND_UNDEF)
10355
10560
        thd->substitute_null_with_insert_id= false;
10356
10561
      }
10357
10562
      /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
10358
 
      else if (((field->type() == DRIZZLE_TYPE_NEWDATE) ||
10359
 
                (field->type() == DRIZZLE_TYPE_DATETIME)) &&
 
10563
      else if (((field->type() == MYSQL_TYPE_NEWDATE) ||
 
10564
                (field->type() == MYSQL_TYPE_DATETIME)) &&
10360
10565
                (field->flags & NOT_NULL_FLAG) &&
10361
10566
               !field->table->maybe_null)
10362
10567
      {
10503
10708
  return 0;
10504
10709
}
10505
10710
 
 
10711
/****************************************************************************
 
10712
  Create internal temporary table
 
10713
****************************************************************************/
 
10714
 
 
10715
/**
 
10716
  Create field for temporary table from given field.
 
10717
 
 
10718
  @param thd           Thread handler
 
10719
  @param org_field    field from which new field will be created
 
10720
  @param name         New field name
 
10721
  @param table         Temporary table
 
10722
  @param item          !=NULL if item->result_field should point to new field.
 
10723
                      This is relevant for how fill_record() is going to work:
 
10724
                      If item != NULL then fill_record() will update
 
10725
                      the record in the original table.
 
10726
                      If item == NULL then fill_record() will update
 
10727
                      the temporary table
 
10728
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
 
10729
                               field instead of blob.
 
10730
 
 
10731
  @retval
 
10732
    NULL                on error
 
10733
  @retval
 
10734
    new_created field
 
10735
*/
 
10736
 
 
10737
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
 
10738
                                   const char *name, TABLE *table,
 
10739
                                   Item_field *item, uint convert_blob_length)
 
10740
{
 
10741
  Field *new_field;
 
10742
 
 
10743
  /* 
 
10744
    Make sure that the blob fits into a Field_varstring which has 
 
10745
    2-byte lenght. 
 
10746
  */
 
10747
  if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
 
10748
      (org_field->flags & BLOB_FLAG))
 
10749
    new_field= new Field_varstring(convert_blob_length,
 
10750
                                   org_field->maybe_null(),
 
10751
                                   org_field->field_name, table->s,
 
10752
                                   org_field->charset());
 
10753
  else
 
10754
    new_field= org_field->new_field(thd->mem_root, table,
 
10755
                                    table == org_field->table);
 
10756
  if (new_field)
 
10757
  {
 
10758
    new_field->init(table);
 
10759
    new_field->orig_table= org_field->orig_table;
 
10760
    if (item)
 
10761
      item->result_field= new_field;
 
10762
    else
 
10763
      new_field->field_name= name;
 
10764
    new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
 
10765
    if (org_field->maybe_null() || (item && item->maybe_null))
 
10766
      new_field->flags&= ~NOT_NULL_FLAG;        // Because of outer join
 
10767
    if (org_field->type() == MYSQL_TYPE_VAR_STRING ||
 
10768
        org_field->type() == MYSQL_TYPE_VARCHAR)
 
10769
      table->s->db_create_options|= HA_OPTION_PACK_RECORD;
 
10770
    else if (org_field->type() == FIELD_TYPE_DOUBLE)
 
10771
      ((Field_double *) new_field)->not_fixed= true;
 
10772
  }
 
10773
  return new_field;
 
10774
}
 
10775
 
 
10776
/**
 
10777
  Create field for temporary table using type of given item.
 
10778
 
 
10779
  @param thd                   Thread handler
 
10780
  @param item                  Item to create a field for
 
10781
  @param table                 Temporary table
 
10782
  @param copy_func             If set and item is a function, store copy of
 
10783
                               item in this array
 
10784
  @param modify_item           1 if item->result_field should point to new
 
10785
                               item. This is relevent for how fill_record()
 
10786
                               is going to work:
 
10787
                               If modify_item is 1 then fill_record() will
 
10788
                               update the record in the original table.
 
10789
                               If modify_item is 0 then fill_record() will
 
10790
                               update the temporary table
 
10791
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
 
10792
                               field instead of blob.
 
10793
 
 
10794
  @retval
 
10795
    0  on error
 
10796
  @retval
 
10797
    new_created field
 
10798
*/
 
10799
 
 
10800
static Field *create_tmp_field_from_item(THD *thd, Item *item, TABLE *table,
 
10801
                                         Item ***copy_func, bool modify_item,
 
10802
                                         uint convert_blob_length)
 
10803
{
 
10804
  bool maybe_null= item->maybe_null;
 
10805
  Field *new_field;
 
10806
 
 
10807
  switch (item->result_type()) {
 
10808
  case REAL_RESULT:
 
10809
    new_field= new Field_double(item->max_length, maybe_null,
 
10810
                                item->name, item->decimals, true);
 
10811
    break;
 
10812
  case INT_RESULT:
 
10813
    /* 
 
10814
      Select an integer type with the minimal fit precision.
 
10815
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
 
10816
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into 
 
10817
      Field_long : make them Field_longlong.  
 
10818
    */
 
10819
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
10820
      new_field=new Field_longlong(item->max_length, maybe_null,
 
10821
                                   item->name, item->unsigned_flag);
 
10822
    else
 
10823
      new_field=new Field_long(item->max_length, maybe_null,
 
10824
                               item->name, item->unsigned_flag);
 
10825
    break;
 
10826
  case STRING_RESULT:
 
10827
    DBUG_ASSERT(item->collation.collation);
 
10828
  
 
10829
    enum enum_field_types type;
 
10830
    /*
 
10831
      DATE/TIME fields have STRING_RESULT result type. 
 
10832
      To preserve type they needed to be handled separately.
 
10833
    */
 
10834
    if ((type= item->field_type()) == MYSQL_TYPE_DATETIME ||
 
10835
        type == MYSQL_TYPE_TIME || type == MYSQL_TYPE_NEWDATE ||
 
10836
        type == MYSQL_TYPE_TIMESTAMP)
 
10837
      new_field= item->tmp_table_field_from_field_type(table, 1);
 
10838
    /* 
 
10839
      Make sure that the blob fits into a Field_varstring which has 
 
10840
      2-byte lenght. 
 
10841
    */
 
10842
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
 
10843
             convert_blob_length <= Field_varstring::MAX_SIZE && 
 
10844
             convert_blob_length)
 
10845
      new_field= new Field_varstring(convert_blob_length, maybe_null,
 
10846
                                     item->name, table->s,
 
10847
                                     item->collation.collation);
 
10848
    else
 
10849
      new_field= item->make_string_field(table);
 
10850
    new_field->set_derivation(item->collation.derivation);
 
10851
    break;
 
10852
  case DECIMAL_RESULT:
 
10853
  {
 
10854
    uint8 dec= item->decimals;
 
10855
    uint8 intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
10856
    uint32 len= item->max_length;
 
10857
 
 
10858
    /*
 
10859
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
10860
      will always throw a warning. We must limit dec to
 
10861
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
10862
    */
 
10863
 
 
10864
    if (dec > 0)
 
10865
    {
 
10866
      signed int overflow;
 
10867
 
 
10868
      dec= min(dec, DECIMAL_MAX_SCALE);
 
10869
 
 
10870
      /*
 
10871
        If the value still overflows the field with the corrected dec,
 
10872
        we'll throw out decimals rather than integers. This is still
 
10873
        bad and of course throws a truncation warning.
 
10874
        +1: for decimal point
 
10875
      */
 
10876
 
 
10877
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
10878
                                               item->unsigned_flag) - len;
 
10879
 
 
10880
      if (overflow > 0)
 
10881
        dec= max(0, dec - overflow);            // too long, discard fract
 
10882
      else
 
10883
        len -= item->decimals - dec;            // corrected value fits
 
10884
    }
 
10885
 
 
10886
    new_field= new Field_new_decimal(len, maybe_null, item->name,
 
10887
                                     dec, item->unsigned_flag);
 
10888
    break;
 
10889
  }
 
10890
  case ROW_RESULT:
 
10891
  default:
 
10892
    // This case should never be choosen
 
10893
    DBUG_ASSERT(0);
 
10894
    new_field= 0;
 
10895
    break;
 
10896
  }
 
10897
  if (new_field)
 
10898
    new_field->init(table);
 
10899
    
 
10900
  if (copy_func && item->is_result_field())
 
10901
    *((*copy_func)++) = item;                   // Save for copy_funcs
 
10902
  if (modify_item)
 
10903
    item->set_result_field(new_field);
 
10904
  if (item->type() == Item::NULL_ITEM)
 
10905
    new_field->is_created_from_null_item= true;
 
10906
  return new_field;
 
10907
}
 
10908
 
 
10909
 
 
10910
/**
 
10911
  Create field for information schema table.
 
10912
 
 
10913
  @param thd            Thread handler
 
10914
  @param table          Temporary table
 
10915
  @param item           Item to create a field for
 
10916
 
 
10917
  @retval
 
10918
    0                   on error
 
10919
  @retval
 
10920
    new_created field
 
10921
*/
 
10922
 
 
10923
Field *create_tmp_field_for_schema(THD *thd, Item *item, TABLE *table)
 
10924
{
 
10925
  if (item->field_type() == MYSQL_TYPE_VARCHAR)
 
10926
  {
 
10927
    Field *field;
 
10928
    if (item->max_length > MAX_FIELD_VARCHARLENGTH)
 
10929
      field= new Field_blob(item->max_length, item->maybe_null,
 
10930
                            item->name, item->collation.collation);
 
10931
    else
 
10932
      field= new Field_varstring(item->max_length, item->maybe_null,
 
10933
                                 item->name,
 
10934
                                 table->s, item->collation.collation);
 
10935
    if (field)
 
10936
      field->init(table);
 
10937
    return field;
 
10938
  }
 
10939
  return item->tmp_table_field_from_field_type(table, 0);
 
10940
}
 
10941
 
 
10942
 
 
10943
/**
 
10944
  Create field for temporary table.
 
10945
 
 
10946
  @param thd            Thread handler
 
10947
  @param table          Temporary table
 
10948
  @param item           Item to create a field for
 
10949
  @param type           Type of item (normally item->type)
 
10950
  @param copy_func      If set and item is a function, store copy of item
 
10951
                       in this array
 
10952
  @param from_field    if field will be created using other field as example,
 
10953
                       pointer example field will be written here
 
10954
  @param default_field  If field has a default value field, store it here
 
10955
  @param group          1 if we are going to do a relative group by on result
 
10956
  @param modify_item    1 if item->result_field should point to new item.
 
10957
                       This is relevent for how fill_record() is going to
 
10958
                       work:
 
10959
                       If modify_item is 1 then fill_record() will update
 
10960
                       the record in the original table.
 
10961
                       If modify_item is 0 then fill_record() will update
 
10962
                       the temporary table
 
10963
  @param convert_blob_length If >0 create a varstring(convert_blob_length)
 
10964
                             field instead of blob.
 
10965
 
 
10966
  @retval
 
10967
    0                   on error
 
10968
  @retval
 
10969
    new_created field
 
10970
*/
 
10971
 
 
10972
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
 
10973
                        Item ***copy_func, Field **from_field,
 
10974
                        Field **default_field,
 
10975
                        bool group, bool modify_item,
 
10976
                        bool table_cant_handle_bit_fields,
 
10977
                        bool make_copy_field,
 
10978
                        uint convert_blob_length)
 
10979
{
 
10980
  Field *result;
 
10981
  Item::Type orig_type= type;
 
10982
  Item *orig_item= 0;
 
10983
 
 
10984
  if (type != Item::FIELD_ITEM &&
 
10985
      item->real_item()->type() == Item::FIELD_ITEM)
 
10986
  {
 
10987
    orig_item= item;
 
10988
    item= item->real_item();
 
10989
    type= Item::FIELD_ITEM;
 
10990
  }
 
10991
 
 
10992
  switch (type) {
 
10993
  case Item::SUM_FUNC_ITEM:
 
10994
  {
 
10995
    Item_sum *item_sum=(Item_sum*) item;
 
10996
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
 
10997
    if (!result)
 
10998
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
10999
    return result;
 
11000
  }
 
11001
  case Item::FIELD_ITEM:
 
11002
  case Item::DEFAULT_VALUE_ITEM:
 
11003
  {
 
11004
    Item_field *field= (Item_field*) item;
 
11005
    bool orig_modify= modify_item;
 
11006
    if (orig_type == Item::REF_ITEM)
 
11007
      modify_item= 0;
 
11008
    /*
 
11009
      If item have to be able to store NULLs but underlaid field can't do it,
 
11010
      create_tmp_field_from_field() can't be used for tmp field creation.
 
11011
    */
 
11012
    if (field->maybe_null && !field->field->maybe_null())
 
11013
    {
 
11014
      result= create_tmp_field_from_item(thd, item, table, NULL,
 
11015
                                         modify_item, convert_blob_length);
 
11016
      *from_field= field->field;
 
11017
      if (result && modify_item)
 
11018
        field->result_field= result;
 
11019
    } 
 
11020
    else
 
11021
      result= create_tmp_field_from_field(thd, (*from_field= field->field),
 
11022
                                          orig_item ? orig_item->name :
 
11023
                                          item->name,
 
11024
                                          table,
 
11025
                                          modify_item ? field :
 
11026
                                          NULL,
 
11027
                                          convert_blob_length);
 
11028
    if (orig_type == Item::REF_ITEM && orig_modify)
 
11029
      ((Item_ref*)orig_item)->set_result_field(result);
 
11030
    if (field->field->eq_def(result))
 
11031
      *default_field= field->field;
 
11032
    return result;
 
11033
  }
 
11034
  /* Fall through */
 
11035
  case Item::FUNC_ITEM:
 
11036
    /* Fall through */
 
11037
  case Item::COND_ITEM:
 
11038
  case Item::FIELD_AVG_ITEM:
 
11039
  case Item::FIELD_STD_ITEM:
 
11040
  case Item::SUBSELECT_ITEM:
 
11041
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
 
11042
  case Item::PROC_ITEM:
 
11043
  case Item::INT_ITEM:
 
11044
  case Item::REAL_ITEM:
 
11045
  case Item::DECIMAL_ITEM:
 
11046
  case Item::STRING_ITEM:
 
11047
  case Item::REF_ITEM:
 
11048
  case Item::NULL_ITEM:
 
11049
  case Item::VARBIN_ITEM:
 
11050
    if (make_copy_field)
 
11051
    {
 
11052
      DBUG_ASSERT(((Item_result_field*)item)->result_field);
 
11053
      *from_field= ((Item_result_field*)item)->result_field;
 
11054
    }
 
11055
    return create_tmp_field_from_item(thd, item, table,
 
11056
                                      (make_copy_field ? 0 : copy_func),
 
11057
                                       modify_item, convert_blob_length);
 
11058
  case Item::TYPE_HOLDER:  
 
11059
    result= ((Item_type_holder *)item)->make_field_by_type(table);
 
11060
    result->set_derivation(item->collation.derivation);
 
11061
    return result;
 
11062
  default:                                      // Dosen't have to be stored
 
11063
    return 0;
 
11064
  }
 
11065
}
 
11066
 
 
11067
/*
 
11068
  Set up column usage bitmaps for a temporary table
 
11069
 
 
11070
  IMPLEMENTATION
 
11071
    For temporary tables, we need one bitmap with all columns set and
 
11072
    a tmp_set bitmap to be used by things like filesort.
 
11073
*/
 
11074
 
 
11075
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
 
11076
{
 
11077
  uint field_count= table->s->fields;
 
11078
  bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
 
11079
              false);
 
11080
  bitmap_init(&table->tmp_set,
 
11081
              (my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
 
11082
              field_count, false);
 
11083
  /* write_set and all_set are copies of read_set */
 
11084
  table->def_write_set= table->def_read_set;
 
11085
  table->s->all_set= table->def_read_set;
 
11086
  bitmap_set_all(&table->s->all_set);
 
11087
  table->default_column_bitmaps();
 
11088
}
 
11089
 
 
11090
 
 
11091
/**
 
11092
  Create a temp table according to a field list.
 
11093
 
 
11094
  Given field pointers are changed to point at tmp_table for
 
11095
  send_fields. The table object is self contained: it's
 
11096
  allocated in its own memory root, as well as Field objects
 
11097
  created for table columns.
 
11098
  This function will replace Item_sum items in 'fields' list with
 
11099
  corresponding Item_field items, pointing at the fields in the
 
11100
  temporary table, unless this was prohibited by true
 
11101
  value of argument save_sum_fields. The Item_field objects
 
11102
  are created in THD memory root.
 
11103
 
 
11104
  @param thd                  thread handle
 
11105
  @param param                a description used as input to create the table
 
11106
  @param fields               list of items that will be used to define
 
11107
                              column types of the table (also see NOTES)
 
11108
  @param group                TODO document
 
11109
  @param distinct             should table rows be distinct
 
11110
  @param save_sum_fields      see NOTES
 
11111
  @param select_options
 
11112
  @param rows_limit
 
11113
  @param table_alias          possible name of the temporary table that can
 
11114
                              be used for name resolving; can be "".
 
11115
*/
 
11116
 
 
11117
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
 
11118
#define AVG_STRING_LENGTH_TO_PACK_ROWS   64
 
11119
#define RATIO_TO_PACK_ROWS             2
 
11120
#define MIN_STRING_LENGTH_TO_PACK_ROWS   10
 
11121
 
 
11122
TABLE *
 
11123
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
 
11124
                 ORDER *group, bool distinct, bool save_sum_fields,
 
11125
                 ulonglong select_options, ha_rows rows_limit,
 
11126
                 char *table_alias)
 
11127
{
 
11128
  MEM_ROOT *mem_root_save, own_root;
 
11129
  TABLE *table;
 
11130
  TABLE_SHARE *share;
 
11131
  uint  i,field_count,null_count,null_pack_length;
 
11132
  uint  copy_func_count= param->func_count;
 
11133
  uint  hidden_null_count, hidden_null_pack_length, hidden_field_count;
 
11134
  uint  blob_count,group_null_items, string_count;
 
11135
  uint  temp_pool_slot=MY_BIT_NONE;
 
11136
  uint fieldnr= 0;
 
11137
  ulong reclength, string_total_length;
 
11138
  bool  using_unique_constraint= 0;
 
11139
  bool  use_packed_rows= 0;
 
11140
  bool  not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
 
11141
  char  *tmpname,path[FN_REFLEN];
 
11142
  uchar *pos, *group_buff, *bitmaps;
 
11143
  uchar *null_flags;
 
11144
  Field **reg_field, **from_field, **default_field;
 
11145
  uint *blob_field;
 
11146
  Copy_field *copy=0;
 
11147
  KEY *keyinfo;
 
11148
  KEY_PART_INFO *key_part_info;
 
11149
  Item **copy_func;
 
11150
  MI_COLUMNDEF *recinfo;
 
11151
  uint total_uneven_bit_length= 0;
 
11152
  bool force_copy_fields= param->force_copy_fields;
 
11153
  DBUG_ENTER("create_tmp_table");
 
11154
  DBUG_PRINT("enter",
 
11155
             ("distinct: %d  save_sum_fields: %d  rows_limit: %lu  group: %d",
 
11156
              (int) distinct, (int) save_sum_fields,
 
11157
              (ulong) rows_limit,test(group)));
 
11158
 
 
11159
  status_var_increment(thd->status_var.created_tmp_tables);
 
11160
 
 
11161
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
 
11162
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
 
11163
 
 
11164
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
 
11165
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
 
11166
            current_pid, temp_pool_slot);
 
11167
  else
 
11168
  {
 
11169
    /* if we run out of slots or we are not using tempool */
 
11170
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
 
11171
            thd->thread_id, thd->tmp_table++);
 
11172
  }
 
11173
 
 
11174
  /*
 
11175
    No need to change table name to lower case as we are only creating
 
11176
    MyISAM or HEAP tables here
 
11177
  */
 
11178
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
 
11179
 
 
11180
 
 
11181
  if (group)
 
11182
  {
 
11183
    if (!param->quick_group)
 
11184
      group=0;                                  // Can't use group key
 
11185
    else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
 
11186
    {
 
11187
      /*
 
11188
        marker == 4 means two things:
 
11189
        - store NULLs in the key, and
 
11190
        - convert BIT fields to 64-bit long, needed because MEMORY tables
 
11191
          can't index BIT fields.
 
11192
      */
 
11193
      (*tmp->item)->marker= 4;
 
11194
      if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
 
11195
        using_unique_constraint=1;
 
11196
    }
 
11197
    if (param->group_length >= MAX_BLOB_WIDTH)
 
11198
      using_unique_constraint=1;
 
11199
    if (group)
 
11200
      distinct=0;                               // Can't use distinct
 
11201
  }
 
11202
 
 
11203
  field_count=param->field_count+param->func_count+param->sum_func_count;
 
11204
  hidden_field_count=param->hidden_field_count;
 
11205
 
 
11206
  /*
 
11207
    When loose index scan is employed as access method, it already
 
11208
    computes all groups and the result of all aggregate functions. We
 
11209
    make space for the items of the aggregate function in the list of
 
11210
    functions TMP_TABLE_PARAM::items_to_copy, so that the values of
 
11211
    these items are stored in the temporary table.
 
11212
  */
 
11213
  if (param->precomputed_group_by)
 
11214
    copy_func_count+= param->sum_func_count;
 
11215
  
 
11216
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
11217
 
 
11218
  if (!multi_alloc_root(&own_root,
 
11219
                        &table, sizeof(*table),
 
11220
                        &share, sizeof(*share),
 
11221
                        &reg_field, sizeof(Field*) * (field_count+1),
 
11222
                        &default_field, sizeof(Field*) * (field_count),
 
11223
                        &blob_field, sizeof(uint)*(field_count+1),
 
11224
                        &from_field, sizeof(Field*)*field_count,
 
11225
                        &copy_func, sizeof(*copy_func)*(copy_func_count+1),
 
11226
                        &param->keyinfo, sizeof(*param->keyinfo),
 
11227
                        &key_part_info,
 
11228
                        sizeof(*key_part_info)*(param->group_parts+1),
 
11229
                        &param->start_recinfo,
 
11230
                        sizeof(*param->recinfo)*(field_count*2+4),
 
11231
                        &tmpname, (uint) strlen(path)+1,
 
11232
                        &group_buff, (group && ! using_unique_constraint ?
 
11233
                                      param->group_length : 0),
 
11234
                        &bitmaps, bitmap_buffer_size(field_count)*2,
 
11235
                        NullS))
 
11236
  {
 
11237
    if (temp_pool_slot != MY_BIT_NONE)
 
11238
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11239
    DBUG_RETURN(NULL);                          /* purecov: inspected */
 
11240
  }
 
11241
  /* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
 
11242
  if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
 
11243
  {
 
11244
    if (temp_pool_slot != MY_BIT_NONE)
 
11245
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11246
    free_root(&own_root, MYF(0));               /* purecov: inspected */
 
11247
    DBUG_RETURN(NULL);                          /* purecov: inspected */
 
11248
  }
 
11249
  param->items_to_copy= copy_func;
 
11250
  strmov(tmpname,path);
 
11251
  /* make table according to fields */
 
11252
 
 
11253
  bzero((char*) table,sizeof(*table));
 
11254
  bzero((char*) reg_field,sizeof(Field*)*(field_count+1));
 
11255
  bzero((char*) default_field, sizeof(Field*) * (field_count));
 
11256
  bzero((char*) from_field,sizeof(Field*)*field_count);
 
11257
 
 
11258
  table->mem_root= own_root;
 
11259
  mem_root_save= thd->mem_root;
 
11260
  thd->mem_root= &table->mem_root;
 
11261
 
 
11262
  table->field=reg_field;
 
11263
  table->alias= table_alias;
 
11264
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
 
11265
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
 
11266
  table->map=1;
 
11267
  table->temp_pool_slot = temp_pool_slot;
 
11268
  table->copy_blobs= 1;
 
11269
  table->in_use= thd;
 
11270
  table->quick_keys.init();
 
11271
  table->covering_keys.init();
 
11272
  table->keys_in_use_for_query.init();
 
11273
 
 
11274
  table->s= share;
 
11275
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
 
11276
  share->blob_field= blob_field;
 
11277
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
11278
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
 
11279
  share->table_charset= param->table_charset;
 
11280
  share->primary_key= MAX_KEY;               // Indicate no primary key
 
11281
  share->keys_for_keyread.init();
 
11282
  share->keys_in_use.init();
 
11283
 
 
11284
  /* Calculate which type of fields we will store in the temporary table */
 
11285
 
 
11286
  reclength= string_total_length= 0;
 
11287
  blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
 
11288
  param->using_indirect_summary_function=0;
 
11289
 
 
11290
  List_iterator_fast<Item> li(fields);
 
11291
  Item *item;
 
11292
  Field **tmp_from_field=from_field;
 
11293
  while ((item=li++))
 
11294
  {
 
11295
    Item::Type type=item->type();
 
11296
    if (not_all_columns)
 
11297
    {
 
11298
      if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
 
11299
      {
 
11300
        if (item->used_tables() & OUTER_REF_TABLE_BIT)
 
11301
          item->update_used_tables();
 
11302
        if (type == Item::SUBSELECT_ITEM ||
 
11303
            (item->used_tables() & ~OUTER_REF_TABLE_BIT))
 
11304
        {
 
11305
          /*
 
11306
            Mark that the we have ignored an item that refers to a summary
 
11307
            function. We need to know this if someone is going to use
 
11308
            DISTINCT on the result.
 
11309
          */
 
11310
          param->using_indirect_summary_function=1;
 
11311
          continue;
 
11312
        }
 
11313
      }
 
11314
      if (item->const_item() && (int) hidden_field_count <= 0)
 
11315
        continue; // We don't have to store this
 
11316
    }
 
11317
    if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
 
11318
    {                                           /* Can't calc group yet */
 
11319
      ((Item_sum*) item)->result_field=0;
 
11320
      for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
 
11321
      {
 
11322
        Item **argp= ((Item_sum*) item)->args + i;
 
11323
        Item *arg= *argp;
 
11324
        if (!arg->const_item())
 
11325
        {
 
11326
          Field *new_field=
 
11327
            create_tmp_field(thd, table, arg, arg->type(), &copy_func,
 
11328
                             tmp_from_field, &default_field[fieldnr],
 
11329
                             group != 0,not_all_columns,
 
11330
                             distinct, 0,
 
11331
                             param->convert_blob_length);
 
11332
          if (!new_field)
 
11333
            goto err;                                   // Should be OOM
 
11334
          tmp_from_field++;
 
11335
          reclength+=new_field->pack_length();
 
11336
          if (new_field->flags & BLOB_FLAG)
 
11337
          {
 
11338
            *blob_field++= fieldnr;
 
11339
            blob_count++;
 
11340
          }
 
11341
          *(reg_field++)= new_field;
 
11342
          if (new_field->real_type() == MYSQL_TYPE_STRING ||
 
11343
              new_field->real_type() == MYSQL_TYPE_VARCHAR)
 
11344
          {
 
11345
            string_count++;
 
11346
            string_total_length+= new_field->pack_length();
 
11347
          }
 
11348
          thd->mem_root= mem_root_save;
 
11349
          thd->change_item_tree(argp, new Item_field(new_field));
 
11350
          thd->mem_root= &table->mem_root;
 
11351
          if (!(new_field->flags & NOT_NULL_FLAG))
 
11352
          {
 
11353
            null_count++;
 
11354
            /*
 
11355
              new_field->maybe_null() is still false, it will be
 
11356
              changed below. But we have to setup Item_field correctly
 
11357
            */
 
11358
            (*argp)->maybe_null=1;
 
11359
          }
 
11360
          new_field->field_index= fieldnr++;
 
11361
        }
 
11362
      }
 
11363
    }
 
11364
    else
 
11365
    {
 
11366
      /*
 
11367
        The last parameter to create_tmp_field() is a bit tricky:
 
11368
 
 
11369
        We need to set it to 0 in union, to get fill_record() to modify the
 
11370
        temporary table.
 
11371
        We need to set it to 1 on multi-table-update and in select to
 
11372
        write rows to the temporary table.
 
11373
        We here distinguish between UNION and multi-table-updates by the fact
 
11374
        that in the later case group is set to the row pointer.
 
11375
      */
 
11376
      Field *new_field= (param->schema_table) ?
 
11377
        create_tmp_field_for_schema(thd, item, table) :
 
11378
        create_tmp_field(thd, table, item, type, &copy_func,
 
11379
                         tmp_from_field, &default_field[fieldnr],
 
11380
                         group != 0,
 
11381
                         !force_copy_fields &&
 
11382
                           (not_all_columns || group !=0),
 
11383
                         /*
 
11384
                           If item->marker == 4 then we force create_tmp_field
 
11385
                           to create a 64-bit longs for BIT fields because HEAP
 
11386
                           tables can't index BIT fields directly. We do the same
 
11387
                           for distinct, as we want the distinct index to be
 
11388
                           usable in this case too.
 
11389
                         */
 
11390
                         item->marker == 4 || param->bit_fields_as_long,
 
11391
                         force_copy_fields,
 
11392
                         param->convert_blob_length);
 
11393
 
 
11394
      if (!new_field)
 
11395
      {
 
11396
        if (thd->is_fatal_error)
 
11397
          goto err;                             // Got OOM
 
11398
        continue;                               // Some kindf of const item
 
11399
      }
 
11400
      if (type == Item::SUM_FUNC_ITEM)
 
11401
        ((Item_sum *) item)->result_field= new_field;
 
11402
      tmp_from_field++;
 
11403
      reclength+=new_field->pack_length();
 
11404
      if (!(new_field->flags & NOT_NULL_FLAG))
 
11405
        null_count++;
 
11406
      if (new_field->flags & BLOB_FLAG)
 
11407
      {
 
11408
        *blob_field++= fieldnr;
 
11409
        blob_count++;
 
11410
      }
 
11411
      if (item->marker == 4 && item->maybe_null)
 
11412
      {
 
11413
        group_null_items++;
 
11414
        new_field->flags|= GROUP_FLAG;
 
11415
      }
 
11416
      new_field->field_index= fieldnr++;
 
11417
      *(reg_field++)= new_field;
 
11418
    }
 
11419
    if (!--hidden_field_count)
 
11420
    {
 
11421
      /*
 
11422
        This was the last hidden field; Remember how many hidden fields could
 
11423
        have null
 
11424
      */
 
11425
      hidden_null_count=null_count;
 
11426
      /*
 
11427
        We need to update hidden_field_count as we may have stored group
 
11428
        functions with constant arguments
 
11429
      */
 
11430
      param->hidden_field_count= fieldnr;
 
11431
      null_count= 0;
 
11432
    }
 
11433
  }
 
11434
  DBUG_ASSERT(fieldnr == (uint) (reg_field - table->field));
 
11435
  DBUG_ASSERT(field_count >= (uint) (reg_field - table->field));
 
11436
  field_count= fieldnr;
 
11437
  *reg_field= 0;
 
11438
  *blob_field= 0;                               // End marker
 
11439
  share->fields= field_count;
 
11440
 
 
11441
  /* If result table is small; use a heap */
 
11442
  /* future: storage engine selection can be made dynamic? */
 
11443
  if (blob_count || using_unique_constraint ||
 
11444
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
 
11445
      OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
 
11446
  {
 
11447
    share->db_plugin= ha_lock_engine(0, myisam_hton);
 
11448
    table->file= get_new_handler(share, &table->mem_root,
 
11449
                                 share->db_type());
 
11450
    if (group &&
 
11451
        (param->group_parts > table->file->max_key_parts() ||
 
11452
         param->group_length > table->file->max_key_length()))
 
11453
      using_unique_constraint=1;
 
11454
  }
 
11455
  else
 
11456
  {
 
11457
    share->db_plugin= ha_lock_engine(0, heap_hton);
 
11458
    table->file= get_new_handler(share, &table->mem_root,
 
11459
                                 share->db_type());
 
11460
  }
 
11461
  if (!table->file)
 
11462
    goto err;
 
11463
 
 
11464
 
 
11465
  if (!using_unique_constraint)
 
11466
    reclength+= group_null_items;       // null flag is stored separately
 
11467
 
 
11468
  share->blob_fields= blob_count;
 
11469
  if (blob_count == 0)
 
11470
  {
 
11471
    /* We need to ensure that first byte is not 0 for the delete link */
 
11472
    if (param->hidden_field_count)
 
11473
      hidden_null_count++;
 
11474
    else
 
11475
      null_count++;
 
11476
  }
 
11477
  hidden_null_pack_length=(hidden_null_count+7)/8;
 
11478
  null_pack_length= (hidden_null_pack_length +
 
11479
                     (null_count + total_uneven_bit_length + 7) / 8);
 
11480
  reclength+=null_pack_length;
 
11481
  if (!reclength)
 
11482
    reclength=1;                                // Dummy select
 
11483
  /* Use packed rows if there is blobs or a lot of space to gain */
 
11484
  if (blob_count || ((string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS) && (reclength / string_total_length <= RATIO_TO_PACK_ROWS || (string_total_length / string_count) >= AVG_STRING_LENGTH_TO_PACK_ROWS)))
 
11485
    use_packed_rows= 1;
 
11486
 
 
11487
  share->reclength= reclength;
 
11488
  {
 
11489
    uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
 
11490
    share->rec_buff_length= alloc_length;
 
11491
    if (!(table->record[0]= (uchar*)
 
11492
                            alloc_root(&table->mem_root, alloc_length*3)))
 
11493
      goto err;
 
11494
    table->record[1]= table->record[0]+alloc_length;
 
11495
    share->default_values= table->record[1]+alloc_length;
 
11496
  }
 
11497
  copy_func[0]=0;                               // End marker
 
11498
  param->func_count= copy_func - param->items_to_copy; 
 
11499
 
 
11500
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
11501
 
 
11502
  recinfo=param->start_recinfo;
 
11503
  null_flags=(uchar*) table->record[0];
 
11504
  pos=table->record[0]+ null_pack_length;
 
11505
  if (null_pack_length)
 
11506
  {
 
11507
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11508
    recinfo->type=FIELD_NORMAL;
 
11509
    recinfo->length=null_pack_length;
 
11510
    recinfo++;
 
11511
    bfill(null_flags,null_pack_length,255);     // Set null fields
 
11512
 
 
11513
    table->null_flags= (uchar*) table->record[0];
 
11514
    share->null_fields= null_count+ hidden_null_count;
 
11515
    share->null_bytes= null_pack_length;
 
11516
  }
 
11517
  null_count= (blob_count == 0) ? 1 : 0;
 
11518
  hidden_field_count=param->hidden_field_count;
 
11519
  for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
 
11520
  {
 
11521
    Field *field= *reg_field;
 
11522
    uint length;
 
11523
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11524
 
 
11525
    if (!(field->flags & NOT_NULL_FLAG))
 
11526
    {
 
11527
      if (field->flags & GROUP_FLAG && !using_unique_constraint)
 
11528
      {
 
11529
        /*
 
11530
          We have to reserve one byte here for NULL bits,
 
11531
          as this is updated by 'end_update()'
 
11532
        */
 
11533
        *pos++=0;                               // Null is stored here
 
11534
        recinfo->length=1;
 
11535
        recinfo->type=FIELD_NORMAL;
 
11536
        recinfo++;
 
11537
        bzero((uchar*) recinfo,sizeof(*recinfo));
 
11538
      }
 
11539
      else
 
11540
      {
 
11541
        recinfo->null_bit= 1 << (null_count & 7);
 
11542
        recinfo->null_pos= null_count/8;
 
11543
      }
 
11544
      field->move_field(pos,null_flags+null_count/8,
 
11545
                        1 << (null_count & 7));
 
11546
      null_count++;
 
11547
    }
 
11548
    else
 
11549
      field->move_field(pos,(uchar*) 0,0);
 
11550
    field->reset();
 
11551
 
 
11552
    /*
 
11553
      Test if there is a default field value. The test for ->ptr is to skip
 
11554
      'offset' fields generated by initalize_tables
 
11555
    */
 
11556
    if (default_field[i] && default_field[i]->ptr)
 
11557
    {
 
11558
      /* 
 
11559
         default_field[i] is set only in the cases  when 'field' can
 
11560
         inherit the default value that is defined for the field referred
 
11561
         by the Item_field object from which 'field' has been created.
 
11562
      */
 
11563
      my_ptrdiff_t diff;
 
11564
      Field *orig_field= default_field[i];
 
11565
      /* Get the value from default_values */
 
11566
      diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
 
11567
                            orig_field->table->record[0]);
 
11568
      orig_field->move_field_offset(diff);      // Points now at default_values
 
11569
      if (orig_field->is_real_null())
 
11570
        field->set_null();
 
11571
      else
 
11572
      {
 
11573
        field->set_notnull();
 
11574
        memcpy(field->ptr, orig_field->ptr, field->pack_length());
 
11575
      }
 
11576
      orig_field->move_field_offset(-diff);     // Back to record[0]
 
11577
    } 
 
11578
 
 
11579
    if (from_field[i])
 
11580
    {                                           /* Not a table Item */
 
11581
      copy->set(field,from_field[i],save_sum_fields);
 
11582
      copy++;
 
11583
    }
 
11584
    length=field->pack_length();
 
11585
    pos+= length;
 
11586
 
 
11587
    /* Make entry for create table */
 
11588
    recinfo->length=length;
 
11589
    if (field->flags & BLOB_FLAG)
 
11590
      recinfo->type= (int) FIELD_BLOB;
 
11591
    else if (use_packed_rows &&
 
11592
             field->real_type() == MYSQL_TYPE_STRING &&
 
11593
             length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
 
11594
      recinfo->type=FIELD_SKIP_ENDSPACE;
 
11595
    else
 
11596
      recinfo->type=FIELD_NORMAL;
 
11597
    if (!--hidden_field_count)
 
11598
      null_count=(null_count+7) & ~7;           // move to next byte
 
11599
 
 
11600
    // fix table name in field entry
 
11601
    field->table_name= &table->alias;
 
11602
  }
 
11603
 
 
11604
  param->copy_field_end=copy;
 
11605
  param->recinfo=recinfo;
 
11606
  store_record(table,s->default_values);        // Make empty default record
 
11607
 
 
11608
  if (thd->variables.tmp_table_size == ~ (ulonglong) 0)         // No limit
 
11609
    share->max_rows= ~(ha_rows) 0;
 
11610
  else
 
11611
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
 
11612
                                 min(thd->variables.tmp_table_size,
 
11613
                                     thd->variables.max_heap_table_size) :
 
11614
                                 thd->variables.tmp_table_size) /
 
11615
                                 share->reclength);
 
11616
  set_if_bigger(share->max_rows,1);             // For dummy start options
 
11617
  /*
 
11618
    Push the LIMIT clause to the temporary table creation, so that we
 
11619
    materialize only up to 'rows_limit' records instead of all result records.
 
11620
  */
 
11621
  set_if_smaller(share->max_rows, rows_limit);
 
11622
  param->end_write_records= rows_limit;
 
11623
 
 
11624
  keyinfo= param->keyinfo;
 
11625
 
 
11626
  if (group)
 
11627
  {
 
11628
    DBUG_PRINT("info",("Creating group key in temporary table"));
 
11629
    table->group=group;                         /* Table is grouped by key */
 
11630
    param->group_buff=group_buff;
 
11631
    share->keys=1;
 
11632
    share->uniques= test(using_unique_constraint);
 
11633
    table->key_info=keyinfo;
 
11634
    keyinfo->key_part=key_part_info;
 
11635
    keyinfo->flags=HA_NOSAME;
 
11636
    keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
 
11637
    keyinfo->key_length=0;
 
11638
    keyinfo->rec_per_key=0;
 
11639
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11640
    keyinfo->name= (char*) "group_key";
 
11641
    ORDER *cur_group= group;
 
11642
    for (; cur_group ; cur_group= cur_group->next, key_part_info++)
 
11643
    {
 
11644
      Field *field=(*cur_group->item)->get_tmp_table_field();
 
11645
      bool maybe_null=(*cur_group->item)->maybe_null;
 
11646
      key_part_info->null_bit=0;
 
11647
      key_part_info->field=  field;
 
11648
      key_part_info->offset= field->offset(table->record[0]);
 
11649
      key_part_info->length= (uint16) field->key_length();
 
11650
      key_part_info->type=   (uint8) field->key_type();
 
11651
      key_part_info->key_type =
 
11652
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
 
11653
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
 
11654
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
 
11655
        0 : FIELDFLAG_BINARY;
 
11656
      if (!using_unique_constraint)
 
11657
      {
 
11658
        cur_group->buff=(char*) group_buff;
 
11659
        if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
 
11660
                                                     group_buff +
 
11661
                                                     test(maybe_null),
 
11662
                                                     field->null_ptr,
 
11663
                                                     field->null_bit)))
 
11664
          goto err; /* purecov: inspected */
 
11665
        if (maybe_null)
 
11666
        {
 
11667
          /*
 
11668
            To be able to group on NULL, we reserved place in group_buff
 
11669
            for the NULL flag just before the column. (see above).
 
11670
            The field data is after this flag.
 
11671
            The NULL flag is updated in 'end_update()' and 'end_write()'
 
11672
          */
 
11673
          keyinfo->flags|= HA_NULL_ARE_EQUAL;   // def. that NULL == NULL
 
11674
          key_part_info->null_bit=field->null_bit;
 
11675
          key_part_info->null_offset= (uint) (field->null_ptr -
 
11676
                                              (uchar*) table->record[0]);
 
11677
          cur_group->buff++;                        // Pointer to field data
 
11678
          group_buff++;                         // Skipp null flag
 
11679
        }
 
11680
        /* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
 
11681
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
 
11682
        group_buff+= cur_group->field->pack_length();
 
11683
      }
 
11684
      keyinfo->key_length+=  key_part_info->length;
 
11685
    }
 
11686
  }
 
11687
 
 
11688
  if (distinct && field_count != param->hidden_field_count)
 
11689
  {
 
11690
    /*
 
11691
      Create an unique key or an unique constraint over all columns
 
11692
      that should be in the result.  In the temporary table, there are
 
11693
      'param->hidden_field_count' extra columns, whose null bits are stored
 
11694
      in the first 'hidden_null_pack_length' bytes of the row.
 
11695
    */
 
11696
    DBUG_PRINT("info",("hidden_field_count: %d", param->hidden_field_count));
 
11697
 
 
11698
    if (blob_count)
 
11699
    {
 
11700
      /*
 
11701
        Special mode for index creation in MyISAM used to support unique
 
11702
        indexes on blobs with arbitrary length. Such indexes cannot be
 
11703
        used for lookups.
 
11704
      */
 
11705
      share->uniques= 1;
 
11706
    }
 
11707
    null_pack_length-=hidden_null_pack_length;
 
11708
    keyinfo->key_parts= ((field_count-param->hidden_field_count)+
 
11709
                         (share->uniques ? test(null_pack_length) : 0));
 
11710
    table->distinct= 1;
 
11711
    share->keys= 1;
 
11712
    if (!(key_part_info= (KEY_PART_INFO*)
 
11713
          alloc_root(&table->mem_root,
 
11714
                     keyinfo->key_parts * sizeof(KEY_PART_INFO))))
 
11715
      goto err;
 
11716
    bzero((void*) key_part_info, keyinfo->key_parts * sizeof(KEY_PART_INFO));
 
11717
    table->key_info=keyinfo;
 
11718
    keyinfo->key_part=key_part_info;
 
11719
    keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
 
11720
    keyinfo->key_length=(uint16) reclength;
 
11721
    keyinfo->name= (char*) "distinct_key";
 
11722
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11723
    keyinfo->rec_per_key=0;
 
11724
 
 
11725
    /*
 
11726
      Create an extra field to hold NULL bits so that unique indexes on
 
11727
      blobs can distinguish NULL from 0. This extra field is not needed
 
11728
      when we do not use UNIQUE indexes for blobs.
 
11729
    */
 
11730
    if (null_pack_length && share->uniques)
 
11731
    {
 
11732
      key_part_info->null_bit=0;
 
11733
      key_part_info->offset=hidden_null_pack_length;
 
11734
      key_part_info->length=null_pack_length;
 
11735
      key_part_info->field= new Field_string(table->record[0],
 
11736
                                             (uint32) key_part_info->length,
 
11737
                                             (uchar*) 0,
 
11738
                                             (uint) 0,
 
11739
                                             Field::NONE,
 
11740
                                             NullS, &my_charset_bin);
 
11741
      if (!key_part_info->field)
 
11742
        goto err;
 
11743
      key_part_info->field->init(table);
 
11744
      key_part_info->key_type=FIELDFLAG_BINARY;
 
11745
      key_part_info->type=    HA_KEYTYPE_BINARY;
 
11746
      key_part_info++;
 
11747
    }
 
11748
    /* Create a distinct key over the columns we are going to return */
 
11749
    for (i=param->hidden_field_count, reg_field=table->field + i ;
 
11750
         i < field_count;
 
11751
         i++, reg_field++, key_part_info++)
 
11752
    {
 
11753
      key_part_info->null_bit=0;
 
11754
      key_part_info->field=    *reg_field;
 
11755
      key_part_info->offset=   (*reg_field)->offset(table->record[0]);
 
11756
      key_part_info->length=   (uint16) (*reg_field)->pack_length();
 
11757
      /* TODO:
 
11758
        The below method of computing the key format length of the
 
11759
        key part is a copy/paste from opt_range.cc, and table.cc.
 
11760
        This should be factored out, e.g. as a method of Field.
 
11761
        In addition it is not clear if any of the Field::*_length
 
11762
        methods is supposed to compute the same length. If so, it
 
11763
        might be reused.
 
11764
      */
 
11765
      key_part_info->store_length= key_part_info->length;
 
11766
 
 
11767
      if ((*reg_field)->real_maybe_null())
 
11768
        key_part_info->store_length+= HA_KEY_NULL_LENGTH;
 
11769
      if ((*reg_field)->type() == MYSQL_TYPE_BLOB || 
 
11770
          (*reg_field)->real_type() == MYSQL_TYPE_VARCHAR)
 
11771
        key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
 
11772
 
 
11773
      key_part_info->type=     (uint8) (*reg_field)->key_type();
 
11774
      key_part_info->key_type =
 
11775
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
 
11776
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
 
11777
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
 
11778
        0 : FIELDFLAG_BINARY;
 
11779
    }
 
11780
  }
 
11781
 
 
11782
  if (thd->is_fatal_error)                              // If end of memory
 
11783
    goto err;                                    /* purecov: inspected */
 
11784
  share->db_record_offset= 1;
 
11785
  if (share->db_type() == myisam_hton)
 
11786
  {
 
11787
    if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
 
11788
                                &param->recinfo, select_options))
 
11789
      goto err;
 
11790
  }
 
11791
  if (open_tmp_table(table))
 
11792
    goto err;
 
11793
 
 
11794
  thd->mem_root= mem_root_save;
 
11795
 
 
11796
  DBUG_RETURN(table);
 
11797
 
 
11798
err:
 
11799
  thd->mem_root= mem_root_save;
 
11800
  free_tmp_table(thd,table);                    /* purecov: inspected */
 
11801
  if (temp_pool_slot != MY_BIT_NONE)
 
11802
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11803
  DBUG_RETURN(NULL);                            /* purecov: inspected */
 
11804
}
 
11805
 
 
11806
 
 
11807
 
 
11808
 
 
11809
/*
 
11810
  Create a temporary table to weed out duplicate rowid combinations
 
11811
 
 
11812
  SYNOPSIS
 
11813
 
 
11814
    create_duplicate_weedout_tmp_table()
 
11815
      thd
 
11816
      uniq_tuple_length_arg
 
11817
      SJ_TMP_TABLE 
 
11818
 
 
11819
  DESCRIPTION
 
11820
    Create a temporary table to weed out duplicate rowid combinations. The
 
11821
    table has a single column that is a concatenation of all rowids in the
 
11822
    combination. 
 
11823
 
 
11824
    Depending on the needed length, there are two cases:
 
11825
 
 
11826
    1. When the length of the column < max_key_length:
 
11827
 
 
11828
      CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
 
11829
 
 
11830
    2. Otherwise (not a valid SQL syntax but internally supported):
 
11831
 
 
11832
      CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
 
11833
 
 
11834
    The code in this function was produced by extraction of relevant parts
 
11835
    from create_tmp_table().
 
11836
 
 
11837
  RETURN
 
11838
    created table
 
11839
    NULL on error
 
11840
*/
 
11841
 
 
11842
TABLE *create_duplicate_weedout_tmp_table(THD *thd, 
 
11843
                                          uint uniq_tuple_length_arg,
 
11844
                                          SJ_TMP_TABLE *sjtbl)
 
11845
{
 
11846
  MEM_ROOT *mem_root_save, own_root;
 
11847
  TABLE *table;
 
11848
  TABLE_SHARE *share;
 
11849
  uint  temp_pool_slot=MY_BIT_NONE;
 
11850
  char  *tmpname,path[FN_REFLEN];
 
11851
  Field **reg_field;
 
11852
  KEY_PART_INFO *key_part_info;
 
11853
  KEY *keyinfo;
 
11854
  uchar *group_buff;
 
11855
  uchar *bitmaps;
 
11856
  uint *blob_field;
 
11857
  MI_COLUMNDEF *recinfo, *start_recinfo;
 
11858
  bool using_unique_constraint=false;
 
11859
  bool use_packed_rows= false;
 
11860
  Field *field, *key_field;
 
11861
  uint blob_count, null_pack_length, null_count;
 
11862
  uchar *null_flags;
 
11863
  uchar *pos;
 
11864
  DBUG_ENTER("create_duplicate_weedout_tmp_table");
 
11865
  
 
11866
  /*
 
11867
    STEP 1: Get temporary table name
 
11868
  */
 
11869
  statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
 
11870
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
 
11871
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
 
11872
 
 
11873
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
 
11874
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
 
11875
            current_pid, temp_pool_slot);
 
11876
  else
 
11877
  {
 
11878
    /* if we run out of slots or we are not using tempool */
 
11879
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
 
11880
            thd->thread_id, thd->tmp_table++);
 
11881
  }
 
11882
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
 
11883
 
 
11884
  /* STEP 2: Figure if we'll be using a key or blob+constraint */
 
11885
  if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
 
11886
    using_unique_constraint= true;
 
11887
 
 
11888
  /* STEP 3: Allocate memory for temptable description */
 
11889
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
11890
  if (!multi_alloc_root(&own_root,
 
11891
                        &table, sizeof(*table),
 
11892
                        &share, sizeof(*share),
 
11893
                        &reg_field, sizeof(Field*) * (1+1),
 
11894
                        &blob_field, sizeof(uint)*2,
 
11895
                        &keyinfo, sizeof(*keyinfo),
 
11896
                        &key_part_info, sizeof(*key_part_info) * 2,
 
11897
                        &start_recinfo,
 
11898
                        sizeof(*recinfo)*(1*2+4),
 
11899
                        &tmpname, (uint) strlen(path)+1,
 
11900
                        &group_buff, (!using_unique_constraint ?
 
11901
                                      uniq_tuple_length_arg : 0),
 
11902
                        &bitmaps, bitmap_buffer_size(1)*2,
 
11903
                        NullS))
 
11904
  {
 
11905
    if (temp_pool_slot != MY_BIT_NONE)
 
11906
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11907
    DBUG_RETURN(NULL);
 
11908
  }
 
11909
  strmov(tmpname,path);
 
11910
  
 
11911
 
 
11912
  /* STEP 4: Create TABLE description */
 
11913
  bzero((char*) table,sizeof(*table));
 
11914
  bzero((char*) reg_field,sizeof(Field*)*2);
 
11915
 
 
11916
  table->mem_root= own_root;
 
11917
  mem_root_save= thd->mem_root;
 
11918
  thd->mem_root= &table->mem_root;
 
11919
 
 
11920
  table->field=reg_field;
 
11921
  table->alias= "weedout-tmp";
 
11922
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
 
11923
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
 
11924
  table->map=1;
 
11925
  table->temp_pool_slot = temp_pool_slot;
 
11926
  table->copy_blobs= 1;
 
11927
  table->in_use= thd;
 
11928
  table->quick_keys.init();
 
11929
  table->covering_keys.init();
 
11930
  table->keys_in_use_for_query.init();
 
11931
 
 
11932
  table->s= share;
 
11933
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
 
11934
  share->blob_field= blob_field;
 
11935
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
11936
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
 
11937
  share->table_charset= NULL;
 
11938
  share->primary_key= MAX_KEY;               // Indicate no primary key
 
11939
  share->keys_for_keyread.init();
 
11940
  share->keys_in_use.init();
 
11941
 
 
11942
  blob_count= 0;
 
11943
 
 
11944
  /* Create the field */
 
11945
  {
 
11946
    /*
 
11947
      For the sake of uniformity, always use Field_varstring (altough we could
 
11948
      use Field_string for shorter keys)
 
11949
    */
 
11950
    field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
 
11951
                               &my_charset_bin);
 
11952
    if (!field)
 
11953
      DBUG_RETURN(0);
 
11954
    field->table= table;
 
11955
    field->key_start.init(0);
 
11956
    field->part_of_key.init(0);
 
11957
    field->part_of_sortkey.init(0);
 
11958
    field->unireg_check= Field::NONE;
 
11959
    field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
 
11960
    field->reset_fields();
 
11961
    field->init(table);
 
11962
    field->orig_table= NULL;
 
11963
     
 
11964
    field->field_index= 0;
 
11965
    
 
11966
    *(reg_field++)= field;
 
11967
    *blob_field= 0;
 
11968
    *reg_field= 0;
 
11969
 
 
11970
    share->fields= 1;
 
11971
    share->blob_fields= 0;
 
11972
  }
 
11973
 
 
11974
  uint reclength= field->pack_length();
 
11975
  if (using_unique_constraint)
 
11976
  { 
 
11977
    share->db_plugin= ha_lock_engine(0, myisam_hton);
 
11978
    table->file= get_new_handler(share, &table->mem_root,
 
11979
                                 share->db_type());
 
11980
    DBUG_ASSERT(uniq_tuple_length_arg <= table->file->max_key_length());
 
11981
  }
 
11982
  else
 
11983
  {
 
11984
    share->db_plugin= ha_lock_engine(0, heap_hton);
 
11985
    table->file= get_new_handler(share, &table->mem_root,
 
11986
                                 share->db_type());
 
11987
  }
 
11988
  if (!table->file)
 
11989
    goto err;
 
11990
 
 
11991
  null_count=1;
 
11992
  
 
11993
  null_pack_length= 1;
 
11994
  reclength += null_pack_length;
 
11995
 
 
11996
  share->reclength= reclength;
 
11997
  {
 
11998
    uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
 
11999
    share->rec_buff_length= alloc_length;
 
12000
    if (!(table->record[0]= (uchar*)
 
12001
                            alloc_root(&table->mem_root, alloc_length*3)))
 
12002
      goto err;
 
12003
    table->record[1]= table->record[0]+alloc_length;
 
12004
    share->default_values= table->record[1]+alloc_length;
 
12005
  }
 
12006
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
12007
 
 
12008
  recinfo= start_recinfo;
 
12009
  null_flags=(uchar*) table->record[0];
 
12010
  pos=table->record[0]+ null_pack_length;
 
12011
  if (null_pack_length)
 
12012
  {
 
12013
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
12014
    recinfo->type=FIELD_NORMAL;
 
12015
    recinfo->length=null_pack_length;
 
12016
    recinfo++;
 
12017
    bfill(null_flags,null_pack_length,255);     // Set null fields
 
12018
 
 
12019
    table->null_flags= (uchar*) table->record[0];
 
12020
    share->null_fields= null_count;
 
12021
    share->null_bytes= null_pack_length;
 
12022
  }
 
12023
  null_count=1;
 
12024
 
 
12025
  {
 
12026
    //Field *field= *reg_field;
 
12027
    uint length;
 
12028
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
12029
    field->move_field(pos,(uchar*) 0,0);
 
12030
 
 
12031
    field->reset();
 
12032
    /*
 
12033
      Test if there is a default field value. The test for ->ptr is to skip
 
12034
      'offset' fields generated by initalize_tables
 
12035
    */
 
12036
    // Initialize the table field:
 
12037
    bzero(field->ptr, field->pack_length());
 
12038
 
 
12039
    length=field->pack_length();
 
12040
    pos+= length;
 
12041
 
 
12042
    /* Make entry for create table */
 
12043
    recinfo->length=length;
 
12044
    if (field->flags & BLOB_FLAG)
 
12045
      recinfo->type= (int) FIELD_BLOB;
 
12046
    else if (use_packed_rows &&
 
12047
             field->real_type() == MYSQL_TYPE_STRING &&
 
12048
             length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
 
12049
      recinfo->type=FIELD_SKIP_ENDSPACE;
 
12050
    else
 
12051
      recinfo->type=FIELD_NORMAL;
 
12052
 
 
12053
    field->table_name= &table->alias;
 
12054
  }
 
12055
 
 
12056
  //param->recinfo=recinfo;
 
12057
  //store_record(table,s->default_values);        // Make empty default record
 
12058
 
 
12059
  if (thd->variables.tmp_table_size == ~ (ulonglong) 0)         // No limit
 
12060
    share->max_rows= ~(ha_rows) 0;
 
12061
  else
 
12062
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
 
12063
                                 min(thd->variables.tmp_table_size,
 
12064
                                     thd->variables.max_heap_table_size) :
 
12065
                                 thd->variables.tmp_table_size) /
 
12066
                                 share->reclength);
 
12067
  set_if_bigger(share->max_rows,1);             // For dummy start options
 
12068
 
 
12069
 
 
12070
  //// keyinfo= param->keyinfo;
 
12071
  if (true)
 
12072
  {
 
12073
    DBUG_PRINT("info",("Creating group key in temporary table"));
 
12074
    share->keys=1;
 
12075
    share->uniques= test(using_unique_constraint);
 
12076
    table->key_info=keyinfo;
 
12077
    keyinfo->key_part=key_part_info;
 
12078
    keyinfo->flags=HA_NOSAME;
 
12079
    keyinfo->usable_key_parts= keyinfo->key_parts= 1;
 
12080
    keyinfo->key_length=0;
 
12081
    keyinfo->rec_per_key=0;
 
12082
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
12083
    keyinfo->name= (char*) "weedout_key";
 
12084
    {
 
12085
      key_part_info->null_bit=0;
 
12086
      key_part_info->field=  field;
 
12087
      key_part_info->offset= field->offset(table->record[0]);
 
12088
      key_part_info->length= (uint16) field->key_length();
 
12089
      key_part_info->type=   (uint8) field->key_type();
 
12090
      key_part_info->key_type = FIELDFLAG_BINARY;
 
12091
      if (!using_unique_constraint)
 
12092
      {
 
12093
        if (!(key_field= field->new_key_field(thd->mem_root, table,
 
12094
                                              group_buff,
 
12095
                                              field->null_ptr,
 
12096
                                              field->null_bit)))
 
12097
          goto err;
 
12098
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
 
12099
      }
 
12100
      keyinfo->key_length+=  key_part_info->length;
 
12101
    }
 
12102
  }
 
12103
 
 
12104
  if (thd->is_fatal_error)                              // If end of memory
 
12105
    goto err;
 
12106
  share->db_record_offset= 1;
 
12107
  if (share->db_type() == myisam_hton)
 
12108
  {
 
12109
    recinfo++;
 
12110
    if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
 
12111
      goto err;
 
12112
  }
 
12113
  sjtbl->start_recinfo= start_recinfo;
 
12114
  sjtbl->recinfo=       recinfo;
 
12115
  if (open_tmp_table(table))
 
12116
    goto err;
 
12117
 
 
12118
  thd->mem_root= mem_root_save;
 
12119
  DBUG_RETURN(table);
 
12120
 
 
12121
err:
 
12122
  thd->mem_root= mem_root_save;
 
12123
  free_tmp_table(thd,table);                    /* purecov: inspected */
 
12124
  if (temp_pool_slot != MY_BIT_NONE)
 
12125
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
12126
  DBUG_RETURN(NULL);                            /* purecov: inspected */
 
12127
}
 
12128
 
 
12129
/****************************************************************************/
 
12130
 
 
12131
/**
 
12132
  Create a reduced TABLE object with properly set up Field list from a
 
12133
  list of field definitions.
 
12134
 
 
12135
    The created table doesn't have a table handler associated with
 
12136
    it, has no keys, no group/distinct, no copy_funcs array.
 
12137
    The sole purpose of this TABLE object is to use the power of Field
 
12138
    class to read/write data to/from table->record[0]. Then one can store
 
12139
    the record in any container (RB tree, hash, etc).
 
12140
    The table is created in THD mem_root, so are the table's fields.
 
12141
    Consequently, if you don't BLOB fields, you don't need to free it.
 
12142
 
 
12143
  @param thd         connection handle
 
12144
  @param field_list  list of column definitions
 
12145
 
 
12146
  @return
 
12147
    0 if out of memory, TABLE object in case of success
 
12148
*/
 
12149
 
 
12150
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
 
12151
{
 
12152
  uint field_count= field_list.elements;
 
12153
  uint blob_count= 0;
 
12154
  Field **field;
 
12155
  Create_field *cdef;                           /* column definition */
 
12156
  uint record_length= 0;
 
12157
  uint null_count= 0;                 /* number of columns which may be null */
 
12158
  uint null_pack_length;              /* NULL representation array length */
 
12159
  uint *blob_field;
 
12160
  uchar *bitmaps;
 
12161
  TABLE *table;
 
12162
  TABLE_SHARE *share;
 
12163
 
 
12164
  if (!multi_alloc_root(thd->mem_root,
 
12165
                        &table, sizeof(*table),
 
12166
                        &share, sizeof(*share),
 
12167
                        &field, (field_count + 1) * sizeof(Field*),
 
12168
                        &blob_field, (field_count+1) *sizeof(uint),
 
12169
                        &bitmaps, bitmap_buffer_size(field_count)*2,
 
12170
                        NullS))
 
12171
    return 0;
 
12172
 
 
12173
  bzero(table, sizeof(*table));
 
12174
  bzero(share, sizeof(*share));
 
12175
  table->field= field;
 
12176
  table->s= share;
 
12177
  share->blob_field= blob_field;
 
12178
  share->fields= field_count;
 
12179
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
12180
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
12181
 
 
12182
  /* Create all fields and calculate the total length of record */
 
12183
  List_iterator_fast<Create_field> it(field_list);
 
12184
  while ((cdef= it++))
 
12185
  {
 
12186
    *field= make_field(share, 0, cdef->length,
 
12187
                       (uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
 
12188
                       f_maybe_null(cdef->pack_flag) ? 1 : 0,
 
12189
                       cdef->pack_flag, cdef->sql_type, cdef->charset,
 
12190
                       cdef->unireg_check,
 
12191
                       cdef->interval, cdef->field_name);
 
12192
    if (!*field)
 
12193
      goto error;
 
12194
    (*field)->init(table);
 
12195
    record_length+= (*field)->pack_length();
 
12196
    if (! ((*field)->flags & NOT_NULL_FLAG))
 
12197
      null_count++;
 
12198
 
 
12199
    if ((*field)->flags & BLOB_FLAG)
 
12200
      share->blob_field[blob_count++]= (uint) (field - table->field);
 
12201
 
 
12202
    field++;
 
12203
  }
 
12204
  *field= NULL;                             /* mark the end of the list */
 
12205
  share->blob_field[blob_count]= 0;            /* mark the end of the list */
 
12206
  share->blob_fields= blob_count;
 
12207
 
 
12208
  null_pack_length= (null_count + 7)/8;
 
12209
  share->reclength= record_length + null_pack_length;
 
12210
  share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
 
12211
  table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
 
12212
  if (!table->record[0])
 
12213
    goto error;
 
12214
 
 
12215
  if (null_pack_length)
 
12216
  {
 
12217
    table->null_flags= (uchar*) table->record[0];
 
12218
    share->null_fields= null_count;
 
12219
    share->null_bytes= null_pack_length;
 
12220
  }
 
12221
 
 
12222
  table->in_use= thd;           /* field->reset() may access table->in_use */
 
12223
  {
 
12224
    /* Set up field pointers */
 
12225
    uchar *null_pos= table->record[0];
 
12226
    uchar *field_pos= null_pos + share->null_bytes;
 
12227
    uint null_bit= 1;
 
12228
 
 
12229
    for (field= table->field; *field; ++field)
 
12230
    {
 
12231
      Field *cur_field= *field;
 
12232
      if ((cur_field->flags & NOT_NULL_FLAG))
 
12233
        cur_field->move_field(field_pos);
 
12234
      else
 
12235
      {
 
12236
        cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
 
12237
        null_bit<<= 1;
 
12238
        if (null_bit == (1 << 8))
 
12239
        {
 
12240
          ++null_pos;
 
12241
          null_bit= 1;
 
12242
        }
 
12243
      }
 
12244
      cur_field->reset();
 
12245
 
 
12246
      field_pos+= cur_field->pack_length();
 
12247
    }
 
12248
  }
 
12249
  return table;
 
12250
error:
 
12251
  for (field= table->field; *field; ++field)
 
12252
    delete *field;                         /* just invokes field destructor */
 
12253
  return 0;
 
12254
}
 
12255
 
 
12256
 
 
12257
static bool open_tmp_table(TABLE *table)
 
12258
{
 
12259
  int error;
 
12260
  if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
 
12261
                                  HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
 
12262
  {
 
12263
    table->file->print_error(error,MYF(0)); /* purecov: inspected */
 
12264
    table->db_stat=0;
 
12265
    return(1);
 
12266
  }
 
12267
  (void) table->file->extra(HA_EXTRA_QUICK);            /* Faster */
 
12268
  return(0);
 
12269
}
 
12270
 
 
12271
 
 
12272
/*
 
12273
  Create MyISAM temporary table
 
12274
 
 
12275
  SYNOPSIS
 
12276
    create_myisam_tmp_table()
 
12277
      table           Table object that descrimes the table to be created
 
12278
      keyinfo         Description of the index (there is always one index)
 
12279
      start_recinfo   MyISAM's column descriptions
 
12280
      recinfo INOUT   End of MyISAM's column descriptions
 
12281
      options         Option bits
 
12282
   
 
12283
  DESCRIPTION
 
12284
    Create a MyISAM temporary table according to passed description. The is
 
12285
    assumed to have one unique index or constraint.
 
12286
 
 
12287
    The passed array or MI_COLUMNDEF structures must have this form:
 
12288
 
 
12289
      1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
 
12290
         when there are many nullable columns)
 
12291
      2. Table columns
 
12292
      3. One free MI_COLUMNDEF element (*recinfo points here)
 
12293
   
 
12294
    This function may use the free element to create hash column for unique
 
12295
    constraint.
 
12296
 
 
12297
   RETURN
 
12298
     false - OK
 
12299
     true  - Error
 
12300
*/
 
12301
 
 
12302
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
 
12303
                                    MI_COLUMNDEF *start_recinfo,
 
12304
                                    MI_COLUMNDEF **recinfo, 
 
12305
                                    ulonglong options)
 
12306
{
 
12307
  int error;
 
12308
  MI_KEYDEF keydef;
 
12309
  MI_UNIQUEDEF uniquedef;
 
12310
  TABLE_SHARE *share= table->s;
 
12311
  DBUG_ENTER("create_myisam_tmp_table");
 
12312
 
 
12313
  if (share->keys)
 
12314
  {                                             // Get keys for ni_create
 
12315
    bool using_unique_constraint=0;
 
12316
    HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
 
12317
                                            sizeof(*seg) * keyinfo->key_parts);
 
12318
    if (!seg)
 
12319
      goto err;
 
12320
 
 
12321
    bzero(seg, sizeof(*seg) * keyinfo->key_parts);
 
12322
    if (keyinfo->key_length >= table->file->max_key_length() ||
 
12323
        keyinfo->key_parts > table->file->max_key_parts() ||
 
12324
        share->uniques)
 
12325
    {
 
12326
      /* Can't create a key; Make a unique constraint instead of a key */
 
12327
      share->keys=    0;
 
12328
      share->uniques= 1;
 
12329
      using_unique_constraint=1;
 
12330
      bzero((char*) &uniquedef,sizeof(uniquedef));
 
12331
      uniquedef.keysegs=keyinfo->key_parts;
 
12332
      uniquedef.seg=seg;
 
12333
      uniquedef.null_are_equal=1;
 
12334
 
 
12335
      /* Create extra column for hash value */
 
12336
      bzero((uchar*) *recinfo,sizeof(**recinfo));
 
12337
      (*recinfo)->type= FIELD_CHECK;
 
12338
      (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
 
12339
      (*recinfo)++;
 
12340
      share->reclength+=MI_UNIQUE_HASH_LENGTH;
 
12341
    }
 
12342
    else
 
12343
    {
 
12344
      /* Create an unique key */
 
12345
      bzero((char*) &keydef,sizeof(keydef));
 
12346
      keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
 
12347
      keydef.keysegs=  keyinfo->key_parts;
 
12348
      keydef.seg= seg;
 
12349
    }
 
12350
    for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
 
12351
    {
 
12352
      Field *field=keyinfo->key_part[i].field;
 
12353
      seg->flag=     0;
 
12354
      seg->language= field->charset()->number;
 
12355
      seg->length=   keyinfo->key_part[i].length;
 
12356
      seg->start=    keyinfo->key_part[i].offset;
 
12357
      if (field->flags & BLOB_FLAG)
 
12358
      {
 
12359
        seg->type=
 
12360
        ((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
 
12361
         HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
 
12362
        seg->bit_start= (uint8)(field->pack_length() - share->blob_ptr_size);
 
12363
        seg->flag= HA_BLOB_PART;
 
12364
        seg->length=0;                  // Whole blob in unique constraint
 
12365
      }
 
12366
      else
 
12367
      {
 
12368
        seg->type= keyinfo->key_part[i].type;
 
12369
        /* Tell handler if it can do suffic space compression */
 
12370
        if (field->real_type() == MYSQL_TYPE_STRING &&
 
12371
            keyinfo->key_part[i].length > 4)
 
12372
          seg->flag|= HA_SPACE_PACK;
 
12373
      }
 
12374
      if (!(field->flags & NOT_NULL_FLAG))
 
12375
      {
 
12376
        seg->null_bit= field->null_bit;
 
12377
        seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
 
12378
        /*
 
12379
          We are using a GROUP BY on something that contains NULL
 
12380
          In this case we have to tell MyISAM that two NULL should
 
12381
          on INSERT be regarded at the same value
 
12382
        */
 
12383
        if (!using_unique_constraint)
 
12384
          keydef.flag|= HA_NULL_ARE_EQUAL;
 
12385
      }
 
12386
    }
 
12387
  }
 
12388
  MI_CREATE_INFO create_info;
 
12389
  bzero((char*) &create_info,sizeof(create_info));
 
12390
 
 
12391
  if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
 
12392
      OPTION_BIG_TABLES)
 
12393
    create_info.data_file_length= ~(ulonglong) 0;
 
12394
 
 
12395
  if ((error=mi_create(share->table_name.str, share->keys, &keydef,
 
12396
                       (uint) (*recinfo-start_recinfo),
 
12397
                       start_recinfo,
 
12398
                       share->uniques, &uniquedef,
 
12399
                       &create_info,
 
12400
                       HA_CREATE_TMP_TABLE)))
 
12401
  {
 
12402
    table->file->print_error(error,MYF(0));     /* purecov: inspected */
 
12403
    table->db_stat=0;
 
12404
    goto err;
 
12405
  }
 
12406
  status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
 
12407
  share->db_record_offset= 1;
 
12408
  DBUG_RETURN(0);
 
12409
 err:
 
12410
  DBUG_RETURN(1);
 
12411
}
 
12412
 
 
12413
 
 
12414
void
 
12415
free_tmp_table(THD *thd, TABLE *entry)
 
12416
{
 
12417
  MEM_ROOT own_root= entry->mem_root;
 
12418
  const char *save_proc_info;
 
12419
  DBUG_ENTER("free_tmp_table");
 
12420
  DBUG_PRINT("enter",("table: %s",entry->alias));
 
12421
 
 
12422
  save_proc_info=thd->proc_info;
 
12423
  thd_proc_info(thd, "removing tmp table");
 
12424
 
 
12425
  if (entry->file)
 
12426
  {
 
12427
    if (entry->db_stat)
 
12428
      entry->file->ha_drop_table(entry->s->table_name.str);
 
12429
    else
 
12430
      entry->file->ha_delete_table(entry->s->table_name.str);
 
12431
    delete entry->file;
 
12432
  }
 
12433
 
 
12434
  /* free blobs */
 
12435
  for (Field **ptr=entry->field ; *ptr ; ptr++)
 
12436
    (*ptr)->free();
 
12437
  free_io_cache(entry);
 
12438
 
 
12439
  if (entry->temp_pool_slot != MY_BIT_NONE)
 
12440
    bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
 
12441
 
 
12442
  plugin_unlock(0, entry->s->db_plugin);
 
12443
 
 
12444
  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
 
12445
  thd_proc_info(thd, save_proc_info);
 
12446
 
 
12447
  DBUG_VOID_RETURN;
 
12448
}
 
12449
 
 
12450
/**
 
12451
  If a HEAP table gets full, create a MyISAM table and copy all rows
 
12452
  to this.
 
12453
*/
 
12454
 
 
12455
bool create_myisam_from_heap(THD *thd, TABLE *table,
 
12456
                             MI_COLUMNDEF *start_recinfo,
 
12457
                             MI_COLUMNDEF **recinfo, 
 
12458
                             int error, bool ignore_last_dupp_key_error)
 
12459
{
 
12460
  TABLE new_table;
 
12461
  TABLE_SHARE share;
 
12462
  const char *save_proc_info;
 
12463
  int write_err;
 
12464
  DBUG_ENTER("create_myisam_from_heap");
 
12465
 
 
12466
  if (table->s->db_type() != heap_hton || 
 
12467
      error != HA_ERR_RECORD_FILE_FULL)
 
12468
  {
 
12469
    table->file->print_error(error,MYF(0));
 
12470
    DBUG_RETURN(1);
 
12471
  }
 
12472
  new_table= *table;
 
12473
  share= *table->s;
 
12474
  new_table.s= &share;
 
12475
  new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
 
12476
  if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
 
12477
                                        new_table.s->db_type())))
 
12478
    DBUG_RETURN(1);                             // End of memory
 
12479
 
 
12480
  save_proc_info=thd->proc_info;
 
12481
  thd_proc_info(thd, "converting HEAP to MyISAM");
 
12482
 
 
12483
  if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
 
12484
                              recinfo, thd->lex->select_lex.options | 
 
12485
                                               thd->options))
 
12486
    goto err2;
 
12487
  if (open_tmp_table(&new_table))
 
12488
    goto err1;
 
12489
  if (table->file->indexes_are_disabled())
 
12490
    new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
 
12491
  table->file->ha_index_or_rnd_end();
 
12492
  table->file->ha_rnd_init(1);
 
12493
  if (table->no_rows)
 
12494
  {
 
12495
    new_table.file->extra(HA_EXTRA_NO_ROWS);
 
12496
    new_table.no_rows=1;
 
12497
  }
 
12498
 
 
12499
#ifdef TO_BE_DONE_LATER_IN_4_1
 
12500
  /*
 
12501
    To use start_bulk_insert() (which is new in 4.1) we need to find
 
12502
    all places where a corresponding end_bulk_insert() should be put.
 
12503
  */
 
12504
  table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
 
12505
  new_table.file->ha_start_bulk_insert(table->file->stats.records);
 
12506
#else
 
12507
  /* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
 
12508
  new_table.file->extra(HA_EXTRA_WRITE_CACHE);
 
12509
#endif
 
12510
 
 
12511
  /*
 
12512
    copy all old rows from heap table to MyISAM table
 
12513
    This is the only code that uses record[1] to read/write but this
 
12514
    is safe as this is a temporary MyISAM table without timestamp/autoincrement.
 
12515
  */
 
12516
  while (!table->file->rnd_next(new_table.record[1]))
 
12517
  {
 
12518
    write_err= new_table.file->ha_write_row(new_table.record[1]);
 
12519
    DBUG_EXECUTE_IF("raise_error", write_err= HA_ERR_FOUND_DUPP_KEY ;);
 
12520
    if (write_err)
 
12521
      goto err;
 
12522
  }
 
12523
  /* copy row that filled HEAP table */
 
12524
  if ((write_err=new_table.file->ha_write_row(table->record[0])))
 
12525
  {
 
12526
    if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
 
12527
        !ignore_last_dupp_key_error)
 
12528
      goto err;
 
12529
  }
 
12530
 
 
12531
  /* remove heap table and change to use myisam table */
 
12532
  (void) table->file->ha_rnd_end();
 
12533
  (void) table->file->close();                  // This deletes the table !
 
12534
  delete table->file;
 
12535
  table->file=0;
 
12536
  plugin_unlock(0, table->s->db_plugin);
 
12537
  share.db_plugin= my_plugin_lock(0, &share.db_plugin);
 
12538
  new_table.s= table->s;                       // Keep old share
 
12539
  *table= new_table;
 
12540
  *table->s= share;
 
12541
  
 
12542
  table->file->change_table_ptr(table, table->s);
 
12543
  table->use_all_columns();
 
12544
  if (save_proc_info)
 
12545
  {
 
12546
    const char *new_proc_info=
 
12547
      (!strcmp(save_proc_info,"Copying to tmp table") ?
 
12548
      "Copying to tmp table on disk" : save_proc_info);
 
12549
    thd_proc_info(thd, new_proc_info);
 
12550
  }
 
12551
  DBUG_RETURN(0);
 
12552
 
 
12553
 err:
 
12554
  DBUG_PRINT("error",("Got error: %d",write_err));
 
12555
  table->file->print_error(write_err, MYF(0));
 
12556
  (void) table->file->ha_rnd_end();
 
12557
  (void) new_table.file->close();
 
12558
 err1:
 
12559
  new_table.file->ha_delete_table(new_table.s->table_name.str);
 
12560
 err2:
 
12561
  delete new_table.file;
 
12562
  thd_proc_info(thd, save_proc_info);
 
12563
  table->mem_root= new_table.mem_root;
 
12564
  DBUG_RETURN(1);
 
12565
}
 
12566
 
10506
12567
 
10507
12568
/**
10508
12569
  @details
10518
12579
 
10519
12580
Next_select_func setup_end_select_func(JOIN *join)
10520
12581
{
10521
 
  Table *table= join->tmp_table;
 
12582
  TABLE *table= join->tmp_table;
10522
12583
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
10523
12584
  Next_select_func end_select;
10524
12585
 
10530
12591
    {
10531
12592
      if (table->s->keys)
10532
12593
      {
 
12594
        DBUG_PRINT("info",("Using end_update"));
10533
12595
        end_select=end_update;
10534
12596
      }
10535
12597
      else
10536
12598
      {
 
12599
        DBUG_PRINT("info",("Using end_unique_update"));
10537
12600
        end_select=end_unique_update;
10538
12601
      }
10539
12602
    }
10540
12603
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
10541
12604
    {
 
12605
      DBUG_PRINT("info",("Using end_write_group"));
10542
12606
      end_select=end_write_group;
10543
12607
    }
10544
12608
    else
10545
12609
    {
 
12610
      DBUG_PRINT("info",("Using end_write"));
10546
12611
      end_select=end_write;
10547
12612
      if (tmp_tbl->precomputed_group_by)
10548
12613
      {
10584
12649
*/
10585
12650
 
10586
12651
static int
10587
 
do_select(JOIN *join,List<Item> *fields,Table *table)
 
12652
do_select(JOIN *join,List<Item> *fields,TABLE *table)
10588
12653
{
10589
12654
  int rc= 0;
10590
12655
  enum_nested_loop_state error= NESTED_LOOP_OK;
10591
12656
  JOIN_TAB *join_tab= NULL;
 
12657
  DBUG_ENTER("do_select");
10592
12658
  
10593
12659
  join->tmp_table= table;                       /* Save for easy recursion */
10594
12660
  join->fields= fields;
10595
12661
 
10596
12662
  if (table)
10597
12663
  {
10598
 
    table->file->extra(HA_EXTRA_WRITE_CACHE);
 
12664
    VOID(table->file->extra(HA_EXTRA_WRITE_CACHE));
10599
12665
    empty_record(table);
10600
12666
    if (table->group && join->tmp_table_param.sum_func_count &&
10601
12667
        table->s->keys && !table->file->inited)
10629
12695
      */
10630
12696
      join->examined_rows++;
10631
12697
      join->thd->row_count++;
10632
 
      assert(join->examined_rows <= 1);
 
12698
      DBUG_ASSERT(join->examined_rows <= 1);
10633
12699
    }
10634
12700
    else if (join->send_row_on_empty_set())
10635
12701
    {
10639
12705
  }
10640
12706
  else
10641
12707
  {
10642
 
    assert(join->tables);
 
12708
    DBUG_ASSERT(join->tables);
10643
12709
    error= sub_select(join,join_tab,0);
10644
12710
    if (error == NESTED_LOOP_OK || error == NESTED_LOOP_NO_MORE_ROWS)
10645
12711
      error= sub_select(join,join_tab,1);
10665
12731
      if (join->result->send_eof())
10666
12732
        rc= 1;                                  // Don't send error
10667
12733
    }
 
12734
    DBUG_PRINT("info",("%ld records output", (long) join->send_records));
10668
12735
  }
10669
12736
  else
10670
12737
    rc= -1;
10673
12740
    int tmp, new_errno= 0;
10674
12741
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
10675
12742
    {
 
12743
      DBUG_PRINT("error",("extra(HA_EXTRA_NO_CACHE) failed"));
10676
12744
      new_errno= tmp;
10677
12745
    }
10678
12746
    if ((tmp=table->file->ha_index_or_rnd_end()))
10679
12747
    {
 
12748
      DBUG_PRINT("error",("ha_index_or_rnd_end() failed"));
10680
12749
      new_errno= tmp;
10681
12750
    }
10682
12751
    if (new_errno)
10683
12752
      table->file->print_error(new_errno,MYF(0));
10684
12753
  }
10685
 
  return(join->thd->is_error() ? -1 : rc);
 
12754
#ifndef DBUG_OFF
 
12755
  if (rc)
 
12756
  {
 
12757
    DBUG_PRINT("error",("Error: do_select() failed"));
 
12758
  }
 
12759
#endif
 
12760
  DBUG_RETURN(join->thd->is_error() ? -1 : rc);
10686
12761
}
10687
12762
 
10688
12763
 
10923
12998
  int error;
10924
12999
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
10925
13000
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
10926
 
  unsigned char *ptr= sjtbl->tmp_table->record[0] + 1;
10927
 
  unsigned char *nulls_ptr= ptr;
 
13001
  uchar *ptr= sjtbl->tmp_table->record[0] + 1;
 
13002
  uchar *nulls_ptr= ptr;
10928
13003
  
10929
13004
  /* Put the the rowids tuple into table->record[0]: */
10930
13005
 
10931
13006
  // 1. Store the length 
10932
13007
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
10933
13008
  {
10934
 
    *ptr= (unsigned char)(sjtbl->rowid_len + sjtbl->null_bytes);
 
13009
    *ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
10935
13010
    ptr++;
10936
13011
  }
10937
13012
  else
10943
13018
  // 2. Zero the null bytes 
10944
13019
  if (sjtbl->null_bytes)
10945
13020
  {
10946
 
    memset(ptr, 0, sjtbl->null_bytes);
 
13021
    bzero(ptr, sjtbl->null_bytes);
10947
13022
    ptr += sjtbl->null_bytes; 
10948
13023
  }
10949
13024
 
10950
13025
  // 3. Put the rowids
10951
 
  for (uint32_t i=0; tab != tab_end; tab++, i++)
 
13026
  for (uint i=0; tab != tab_end; tab++, i++)
10952
13027
  {
10953
13028
    handler *h= tab->join_tab->table->file;
10954
13029
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
10955
13030
    {
10956
13031
      /* It's a NULL-complemented row */
10957
13032
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
10958
 
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
 
13033
      bzero(ptr + tab->rowid_offset, h->ref_length);
10959
13034
    }
10960
13035
    else
10961
13036
    {
11017
13092
    join->thd->send_kill_message();
11018
13093
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
11019
13094
  }
 
13095
  DBUG_PRINT("info", ("select cond 0x%lx", (ulong)select_cond));
11020
13096
  if (!select_cond || select_cond->val_int())
11021
13097
  {
11022
13098
    /*
11099
13175
    */
11100
13176
    join->examined_rows++;
11101
13177
    join->thd->row_count++;
 
13178
    DBUG_PRINT("counts", ("join->examined_rows++: %lu",
 
13179
                          (ulong) join->examined_rows));
11102
13180
 
11103
13181
    if (found)
11104
13182
    {
11249
13327
    if (rc == NESTED_LOOP_OK &&
11250
13328
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
11251
13329
    {
11252
 
      uint32_t i;
 
13330
      uint i;
11253
13331
      reset_cache_read(&join_tab->cache);
11254
13332
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
11255
13333
      {
11284
13362
  return NESTED_LOOP_OK;
11285
13363
}
11286
13364
 
 
13365
 
 
13366
/*****************************************************************************
 
13367
  The different ways to read a record
 
13368
  Returns -1 if row was not found, 0 if row was found and 1 on errors
 
13369
*****************************************************************************/
 
13370
 
 
13371
/** Help function when we get some an error from the table handler. */
 
13372
 
 
13373
int report_error(TABLE *table, int error)
 
13374
{
 
13375
  if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
 
13376
  {
 
13377
    table->status= STATUS_GARBAGE;
 
13378
    return -1;                                  // key not found; ok
 
13379
  }
 
13380
  /*
 
13381
    Locking reads can legally return also these errors, do not
 
13382
    print them to the .err log
 
13383
  */
 
13384
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
 
13385
    sql_print_error("Got error %d when reading table '%s'",
 
13386
                    error, table->s->path.str);
 
13387
  table->file->print_error(error,MYF(0));
 
13388
  return 1;
 
13389
}
 
13390
 
 
13391
 
11287
13392
int safe_index_read(JOIN_TAB *tab)
11288
13393
{
11289
13394
  int error;
11290
 
  Table *table= tab->table;
 
13395
  TABLE *table= tab->table;
11291
13396
  if ((error=table->file->index_read_map(table->record[0],
11292
13397
                                         tab->ref.key_buff,
11293
13398
                                         make_prev_keypart_map(tab->ref.key_parts),
11294
13399
                                         HA_READ_KEY_EXACT)))
11295
 
    return table->report_error(error);
 
13400
    return report_error(table, error);
11296
13401
  return 0;
11297
13402
}
11298
13403
 
11301
13406
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
11302
13407
{
11303
13408
  int error;
11304
 
  Table *table=tab->table;
 
13409
  DBUG_ENTER("join_read_const_table");
 
13410
  TABLE *table=tab->table;
11305
13411
  table->const_table=1;
11306
13412
  table->null_row=0;
11307
13413
  table->status=STATUS_NO_RECORD;
11315
13421
      pos->records_read=0.0;
11316
13422
      pos->ref_depend_map= 0;
11317
13423
      if (!table->maybe_null || error > 0)
11318
 
        return(error);
 
13424
        DBUG_RETURN(error);
11319
13425
    }
11320
13426
  }
11321
13427
  else
11341
13447
      pos->records_read=0.0;
11342
13448
      pos->ref_depend_map= 0;
11343
13449
      if (!table->maybe_null || error > 0)
11344
 
        return(error);
 
13450
        DBUG_RETURN(error);
11345
13451
    }
11346
13452
  }
11347
13453
  if (*tab->on_expr_ref && !table->null_row)
11356
13462
  JOIN *join= tab->join;
11357
13463
  if (join->conds)
11358
13464
    update_const_equal_items(join->conds, tab);
11359
 
  TableList *tbl;
 
13465
  TABLE_LIST *tbl;
11360
13466
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
11361
13467
  {
11362
 
    TableList *embedded;
11363
 
    TableList *embedding= tbl;
 
13468
    TABLE_LIST *embedded;
 
13469
    TABLE_LIST *embedding= tbl;
11364
13470
    do
11365
13471
    {
11366
13472
      embedded= embedding;
11372
13478
           embedding->nested_join->join_list.head() == embedded);
11373
13479
  }
11374
13480
 
11375
 
  return(0);
 
13481
  DBUG_RETURN(0);
11376
13482
}
11377
13483
 
11378
13484
 
11379
13485
static int
11380
13486
join_read_system(JOIN_TAB *tab)
11381
13487
{
11382
 
  Table *table= tab->table;
 
13488
  TABLE *table= tab->table;
11383
13489
  int error;
11384
13490
  if (table->status & STATUS_GARBAGE)           // If first read
11385
13491
  {
11387
13493
                                           table->s->primary_key)))
11388
13494
    {
11389
13495
      if (error != HA_ERR_END_OF_FILE)
11390
 
        return table->report_error(error);
 
13496
        return report_error(table, error);
11391
13497
      mark_as_null_row(tab->table);
11392
13498
      empty_record(table);                      // Make empty record
11393
13499
      return -1;
11394
13500
    }
11395
 
    update_virtual_fields_marked_for_write(table);
11396
13501
    store_record(table,record[1]);
11397
13502
  }
11398
13503
  else if (!table->status)                      // Only happens with left join
11419
13524
join_read_const(JOIN_TAB *tab)
11420
13525
{
11421
13526
  int error;
11422
 
  Table *table= tab->table;
 
13527
  TABLE *table= tab->table;
11423
13528
  if (table->status & STATUS_GARBAGE)           // If first read
11424
13529
  {
11425
13530
    table->status= 0;
11426
 
    if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
 
13531
    if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
11427
13532
      error=HA_ERR_KEY_NOT_FOUND;
11428
13533
    else
11429
13534
    {
11430
13535
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
11431
 
                                            (unsigned char*) tab->ref.key_buff,
 
13536
                                            (uchar*) tab->ref.key_buff,
11432
13537
                                            make_prev_keypart_map(tab->ref.key_parts),
11433
13538
                                            HA_READ_KEY_EXACT);
11434
13539
    }
11438
13543
      mark_as_null_row(tab->table);
11439
13544
      empty_record(table);
11440
13545
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11441
 
        return table->report_error(error);
 
13546
        return report_error(table, error);
11442
13547
      return -1;
11443
13548
    }
11444
 
    update_virtual_fields_marked_for_write(table);
11445
13549
    store_record(table,record[1]);
11446
13550
  }
11447
13551
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
11475
13579
join_read_key(JOIN_TAB *tab)
11476
13580
{
11477
13581
  int error;
11478
 
  Table *table= tab->table;
 
13582
  TABLE *table= tab->table;
11479
13583
 
11480
13584
  if (!table->file->inited)
11481
13585
  {
11496
13600
                                      make_prev_keypart_map(tab->ref.key_parts),
11497
13601
                                      HA_READ_KEY_EXACT);
11498
13602
    if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11499
 
      return table->report_error(error);
 
13603
      return report_error(table, error);
11500
13604
  }
11501
13605
  table->null_row=0;
11502
13606
  return table->status ? -1 : 0;
11526
13630
join_read_always_key(JOIN_TAB *tab)
11527
13631
{
11528
13632
  int error;
11529
 
  Table *table= tab->table;
 
13633
  TABLE *table= tab->table;
11530
13634
 
11531
13635
  /* Initialize the index first */
11532
13636
  if (!table->file->inited)
11533
13637
    table->file->ha_index_init(tab->ref.key, tab->sorted);
11534
13638
 
11535
13639
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
11536
 
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
 
13640
  for (uint i= 0 ; i < tab->ref.key_parts ; i++)
11537
13641
  {
11538
13642
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
11539
13643
        return -1;
11540
13644
  }
11541
13645
 
11542
 
  if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
 
13646
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
11543
13647
    return -1;
11544
13648
  if ((error=table->file->index_read_map(table->record[0],
11545
13649
                                         tab->ref.key_buff,
11547
13651
                                         HA_READ_KEY_EXACT)))
11548
13652
  {
11549
13653
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11550
 
      return table->report_error(error);
 
13654
      return report_error(table, error);
11551
13655
    return -1; /* purecov: inspected */
11552
13656
  }
11553
 
  update_virtual_fields_marked_for_write(table);
11554
13657
  return 0;
11555
13658
}
11556
13659
 
11557
13660
 
11558
13661
/**
11559
 
  This function is used when optimizing away order_st BY in 
11560
 
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
 
13662
  This function is used when optimizing away ORDER BY in 
 
13663
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
11561
13664
*/
11562
13665
  
11563
13666
static int
11564
13667
join_read_last_key(JOIN_TAB *tab)
11565
13668
{
11566
13669
  int error;
11567
 
  Table *table= tab->table;
 
13670
  TABLE *table= tab->table;
11568
13671
 
11569
13672
  if (!table->file->inited)
11570
13673
    table->file->ha_index_init(tab->ref.key, tab->sorted);
11571
 
  if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
 
13674
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
11572
13675
    return -1;
11573
13676
  if ((error=table->file->index_read_last_map(table->record[0],
11574
13677
                                              tab->ref.key_buff,
11575
13678
                                              make_prev_keypart_map(tab->ref.key_parts))))
11576
13679
  {
11577
13680
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11578
 
      return table->report_error(error);
 
13681
      return report_error(table, error);
11579
13682
    return -1; /* purecov: inspected */
11580
13683
  }
11581
13684
  return 0;
11592
13695
static int
11593
13696
join_read_next_same_diff(READ_RECORD *info)
11594
13697
{
11595
 
  Table *table= info->table;
 
13698
  TABLE *table= info->table;
11596
13699
  JOIN_TAB *tab=table->reginfo.join_tab;
11597
13700
  if (tab->insideout_match_tab->found_match)
11598
13701
  {
11608
13711
                                              tab->ref.key_length)))
11609
13712
      {
11610
13713
        if (error != HA_ERR_END_OF_FILE)
11611
 
          return table->report_error(error);
 
13714
          return report_error(table, error);
11612
13715
        table->status= STATUS_GARBAGE;
11613
13716
        return -1;
11614
13717
      }
11625
13728
join_read_next_same(READ_RECORD *info)
11626
13729
{
11627
13730
  int error;
11628
 
  Table *table= info->table;
 
13731
  TABLE *table= info->table;
11629
13732
  JOIN_TAB *tab=table->reginfo.join_tab;
11630
13733
 
11631
13734
  if ((error=table->file->index_next_same(table->record[0],
11633
13736
                                          tab->ref.key_length)))
11634
13737
  {
11635
13738
    if (error != HA_ERR_END_OF_FILE)
11636
 
      return table->report_error(error);
 
13739
      return report_error(table, error);
11637
13740
    table->status= STATUS_GARBAGE;
11638
13741
    return -1;
11639
13742
  }
11640
 
  update_virtual_fields_marked_for_write(table);
11641
13743
  return 0;
11642
13744
}
11643
13745
 
11646
13748
join_read_prev_same(READ_RECORD *info)
11647
13749
{
11648
13750
  int error;
11649
 
  Table *table= info->table;
 
13751
  TABLE *table= info->table;
11650
13752
  JOIN_TAB *tab=table->reginfo.join_tab;
11651
13753
 
11652
13754
  if ((error=table->file->index_prev(table->record[0])))
11653
 
    return table->report_error(error);
 
13755
    return report_error(table, error);
11654
13756
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
11655
13757
                      tab->ref.key_length))
11656
13758
  {
11657
13759
    table->status=STATUS_NOT_FOUND;
11658
13760
    error= -1;
11659
13761
  }
11660
 
  update_virtual_fields_marked_for_write(table);
11661
13762
  return error;
11662
13763
}
11663
13764
 
11706
13807
join_read_first(JOIN_TAB *tab)
11707
13808
{
11708
13809
  int error;
11709
 
  Table *table=tab->table;
 
13810
  TABLE *table=tab->table;
11710
13811
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
11711
13812
      !table->no_keyread)
11712
13813
  {
11735
13836
  if ((error=tab->table->file->index_first(tab->table->record[0])))
11736
13837
  {
11737
13838
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11738
 
      table->report_error(error);
 
13839
      report_error(table, error);
11739
13840
    return -1;
11740
13841
  }
11741
 
  if (not error)
11742
 
    update_virtual_fields_marked_for_write(tab->table);
11743
13842
  return 0;
11744
13843
}
11745
13844
 
11758
13857
      key_copy(tab->insideout_buf, info->record, key, 0);
11759
13858
 
11760
13859
      if ((error=info->file->index_next(info->record)))
11761
 
        return info->table->report_error(error);
11762
 
      if (not error)
11763
 
        update_virtual_fields_marked_for_write(tab->table);
 
13860
        return report_error(info->table, error);
 
13861
      
11764
13862
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
11765
13863
                      tab->insideout_buf, key->key_length));
11766
13864
    tab->insideout_match_tab->found_match= 0;
11776
13874
{
11777
13875
  int error;
11778
13876
  if ((error=info->file->index_next(info->record)))
11779
 
    return info->table->report_error(error);
11780
 
  if (not error)
11781
 
    update_virtual_fields_marked_for_write(info->table);
 
13877
    return report_error(info->table, error);
11782
13878
  return 0;
11783
13879
}
11784
13880
 
11786
13882
static int
11787
13883
join_read_last(JOIN_TAB *tab)
11788
13884
{
11789
 
  Table *table=tab->table;
 
13885
  TABLE *table=tab->table;
11790
13886
  int error;
11791
13887
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
11792
13888
      !table->no_keyread)
11803
13899
  if (!table->file->inited)
11804
13900
    table->file->ha_index_init(tab->index, 1);
11805
13901
  if ((error= tab->table->file->index_last(tab->table->record[0])))
11806
 
    return table->report_error(error);
11807
 
  if (not error)
11808
 
    update_virtual_fields_marked_for_write(tab->table);
 
13902
    return report_error(table, error);
11809
13903
  return 0;
11810
13904
}
11811
13905
 
11815
13909
{
11816
13910
  int error;
11817
13911
  if ((error= info->file->index_prev(info->record)))
11818
 
    return info->table->report_error(error);
11819
 
  if (not error)
11820
 
    update_virtual_fields_marked_for_write(info->table);
 
13912
    return report_error(info->table, error);
11821
13913
  return 0;
11822
13914
}
11823
13915
 
11887
13979
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
11888
13980
         bool end_of_records)
11889
13981
{
 
13982
  DBUG_ENTER("end_send");
11890
13983
  if (!end_of_records)
11891
13984
  {
11892
13985
    int error;
11893
13986
    if (join->having && join->having->val_int() == 0)
11894
 
      return(NESTED_LOOP_OK);               // Didn't match having
 
13987
      DBUG_RETURN(NESTED_LOOP_OK);               // Didn't match having
11895
13988
    error=0;
11896
13989
    if (join->do_send_rows)
11897
13990
      error=join->result->send_data(*join->fields);
11898
13991
    if (error)
11899
 
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
 
13992
      DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */
11900
13993
    if (++join->send_records >= join->unit->select_limit_cnt &&
11901
13994
        join->do_send_rows)
11902
13995
    {
11910
14003
            (jt->ref.key < 0))
11911
14004
        {
11912
14005
          /* Join over all rows in table;  Return number of found rows */
11913
 
          Table *table=jt->table;
 
14006
          TABLE *table=jt->table;
11914
14007
 
11915
14008
          join->select_options ^= OPTION_FOUND_ROWS;
11916
14009
          if (table->sort.record_pointers ||
11930
14023
          join->do_send_rows= 0;
11931
14024
          if (join->unit->fake_select_lex)
11932
14025
            join->unit->fake_select_lex->select_limit= 0;
11933
 
          return(NESTED_LOOP_OK);
 
14026
          DBUG_RETURN(NESTED_LOOP_OK);
11934
14027
        }
11935
14028
      }
11936
 
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
 
14029
      DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
11937
14030
    }
11938
14031
    else if (join->send_records >= join->fetch_limit)
11939
14032
    {
11941
14034
        There is a server side cursor and all rows for
11942
14035
        this fetch request are sent.
11943
14036
      */
11944
 
      return(NESTED_LOOP_CURSOR_LIMIT);
 
14037
      DBUG_RETURN(NESTED_LOOP_CURSOR_LIMIT);
11945
14038
    }
11946
14039
  }
11947
14040
 
11948
 
  return(NESTED_LOOP_OK);
 
14041
  DBUG_RETURN(NESTED_LOOP_OK);
11949
14042
}
11950
14043
 
11951
14044
 
11956
14049
{
11957
14050
  int idx= -1;
11958
14051
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
 
14052
  DBUG_ENTER("end_send_group");
11959
14053
 
11960
14054
  if (!join->first_record || end_of_records ||
11961
14055
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
11992
14086
          }
11993
14087
        }
11994
14088
        if (error > 0)
11995
 
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
 
14089
          DBUG_RETURN(NESTED_LOOP_ERROR);        /* purecov: inspected */
11996
14090
        if (end_of_records)
11997
 
          return(NESTED_LOOP_OK);
 
14091
          DBUG_RETURN(NESTED_LOOP_OK);
11998
14092
        if (join->send_records >= join->unit->select_limit_cnt &&
11999
14093
            join->do_send_rows)
12000
14094
        {
12001
14095
          if (!(join->select_options & OPTION_FOUND_ROWS))
12002
 
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
14096
            DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
12003
14097
          join->do_send_rows=0;
12004
14098
          join->unit->select_limit_cnt = HA_POS_ERROR;
12005
14099
        }
12020
14114
    else
12021
14115
    {
12022
14116
      if (end_of_records)
12023
 
        return(NESTED_LOOP_OK);
 
14117
        DBUG_RETURN(NESTED_LOOP_OK);
12024
14118
      join->first_record=1;
12025
 
      test_if_item_cache_changed(join->group_fields);
 
14119
      VOID(test_if_item_cache_changed(join->group_fields));
12026
14120
    }
12027
14121
    if (idx < (int) join->send_group_parts)
12028
14122
    {
12032
14126
      */
12033
14127
      copy_fields(&join->tmp_table_param);
12034
14128
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
12035
 
        return(NESTED_LOOP_ERROR);
12036
 
      return(ok_code);
 
14129
        DBUG_RETURN(NESTED_LOOP_ERROR);
 
14130
      DBUG_RETURN(ok_code);
12037
14131
    }
12038
14132
  }
12039
14133
  if (update_sum_func(join->sum_funcs))
12040
 
    return(NESTED_LOOP_ERROR);
12041
 
  return(NESTED_LOOP_OK);
 
14134
    DBUG_RETURN(NESTED_LOOP_ERROR);
 
14135
  DBUG_RETURN(NESTED_LOOP_OK);
12042
14136
}
12043
14137
 
12044
14138
 
12047
14141
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
12048
14142
          bool end_of_records)
12049
14143
{
12050
 
  Table *table=join->tmp_table;
 
14144
  TABLE *table=join->tmp_table;
 
14145
  DBUG_ENTER("end_write");
12051
14146
 
12052
14147
  if (join->thd->killed)                        // Aborted by user
12053
14148
  {
12054
14149
    join->thd->send_kill_message();
12055
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
14150
    DBUG_RETURN(NESTED_LOOP_KILLED);             /* purecov: inspected */
12056
14151
  }
12057
14152
  if (!end_of_records)
12058
14153
  {
12062
14157
    if (!table->uniques)                        // If not unique handling
12063
14158
    {
12064
14159
      /* Copy null values from group to row */
12065
 
      order_st   *group;
 
14160
      ORDER   *group;
12066
14161
      for (group=table->group ; group ; group=group->next)
12067
14162
      {
12068
14163
        Item *item= *group->item;
12069
14164
        if (item->maybe_null)
12070
14165
        {
12071
14166
          Field *field=item->get_tmp_table_field();
12072
 
          field->ptr[-1]= (unsigned char) (field->is_null() ? 1 : 0);
 
14167
          field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0);
12073
14168
        }
12074
14169
      }
12075
14170
    }
12086
14181
                                    join->tmp_table_param.start_recinfo,
12087
14182
                                    &join->tmp_table_param.recinfo,
12088
14183
                                    error, 1))
12089
 
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
 
14184
          DBUG_RETURN(NESTED_LOOP_ERROR);        // Not a table_is_full error
12090
14185
        table->s->uniques=0;                    // To ensure rows are the same
12091
14186
      }
12092
14187
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
12093
14188
          join->do_send_rows)
12094
14189
      {
12095
14190
        if (!(join->select_options & OPTION_FOUND_ROWS))
12096
 
          return(NESTED_LOOP_QUERY_LIMIT);
 
14191
          DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT);
12097
14192
        join->do_send_rows=0;
12098
14193
        join->unit->select_limit_cnt = HA_POS_ERROR;
12099
 
        return(NESTED_LOOP_OK);
 
14194
        DBUG_RETURN(NESTED_LOOP_OK);
12100
14195
      }
12101
14196
    }
12102
14197
  }
12103
14198
end:
12104
 
  return(NESTED_LOOP_OK);
 
14199
  DBUG_RETURN(NESTED_LOOP_OK);
12105
14200
}
12106
14201
 
12107
14202
/* ARGSUSED */
12111
14206
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
12112
14207
           bool end_of_records)
12113
14208
{
12114
 
  Table *table=join->tmp_table;
12115
 
  order_st   *group;
 
14209
  TABLE *table=join->tmp_table;
 
14210
  ORDER   *group;
12116
14211
  int     error;
 
14212
  DBUG_ENTER("end_update");
12117
14213
 
12118
14214
  if (end_of_records)
12119
 
    return(NESTED_LOOP_OK);
 
14215
    DBUG_RETURN(NESTED_LOOP_OK);
12120
14216
  if (join->thd->killed)                        // Aborted by user
12121
14217
  {
12122
14218
    join->thd->send_kill_message();
12123
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
14219
    DBUG_RETURN(NESTED_LOOP_KILLED);             /* purecov: inspected */
12124
14220
  }
12125
14221
 
12126
14222
  join->found_records++;
12145
14241
                                          table->record[0])))
12146
14242
    {
12147
14243
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12148
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14244
      DBUG_RETURN(NESTED_LOOP_ERROR);            /* purecov: inspected */
12149
14245
    }
12150
 
    return(NESTED_LOOP_OK);
 
14246
    DBUG_RETURN(NESTED_LOOP_OK);
12151
14247
  }
12152
14248
 
12153
14249
  /*
12171
14267
                                join->tmp_table_param.start_recinfo,
12172
14268
                                &join->tmp_table_param.recinfo,
12173
14269
                                error, 0))
12174
 
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
 
14270
      DBUG_RETURN(NESTED_LOOP_ERROR);            // Not a table_is_full error
12175
14271
    /* Change method to update rows */
12176
14272
    table->file->ha_index_init(0, 0);
12177
14273
    join->join_tab[join->tables-1].next_select=end_unique_update;
12178
14274
  }
12179
14275
  join->send_records++;
12180
 
  return(NESTED_LOOP_OK);
 
14276
  DBUG_RETURN(NESTED_LOOP_OK);
12181
14277
}
12182
14278
 
12183
14279
 
12187
14283
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
12188
14284
                  bool end_of_records)
12189
14285
{
12190
 
  Table *table=join->tmp_table;
 
14286
  TABLE *table=join->tmp_table;
12191
14287
  int     error;
 
14288
  DBUG_ENTER("end_unique_update");
12192
14289
 
12193
14290
  if (end_of_records)
12194
 
    return(NESTED_LOOP_OK);
 
14291
    DBUG_RETURN(NESTED_LOOP_OK);
12195
14292
  if (join->thd->killed)                        // Aborted by user
12196
14293
  {
12197
14294
    join->thd->send_kill_message();
12198
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
14295
    DBUG_RETURN(NESTED_LOOP_KILLED);             /* purecov: inspected */
12199
14296
  }
12200
14297
 
12201
14298
  init_tmptable_sum_functions(join->sum_funcs);
12209
14306
    if ((int) table->file->get_dup_key(error) < 0)
12210
14307
    {
12211
14308
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12212
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14309
      DBUG_RETURN(NESTED_LOOP_ERROR);            /* purecov: inspected */
12213
14310
    }
12214
14311
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
12215
14312
    {
12216
14313
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12217
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14314
      DBUG_RETURN(NESTED_LOOP_ERROR);            /* purecov: inspected */
12218
14315
    }
12219
14316
    restore_record(table,record[1]);
12220
14317
    update_tmptable_sum_func(join->sum_funcs,table);
12222
14319
                                          table->record[0])))
12223
14320
    {
12224
14321
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12225
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14322
      DBUG_RETURN(NESTED_LOOP_ERROR);            /* purecov: inspected */
12226
14323
    }
12227
14324
  }
12228
 
  return(NESTED_LOOP_OK);
 
14325
  DBUG_RETURN(NESTED_LOOP_OK);
12229
14326
}
12230
14327
 
12231
14328
 
12234
14331
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
12235
14332
                bool end_of_records)
12236
14333
{
12237
 
  Table *table=join->tmp_table;
 
14334
  TABLE *table=join->tmp_table;
12238
14335
  int     idx= -1;
 
14336
  DBUG_ENTER("end_write_group");
12239
14337
 
12240
14338
  if (join->thd->killed)
12241
14339
  {                                             // Aborted by user
12242
14340
    join->thd->send_kill_message();
12243
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
14341
    DBUG_RETURN(NESTED_LOOP_KILLED);             /* purecov: inspected */
12244
14342
  }
12245
14343
  if (!join->first_record || end_of_records ||
12246
14344
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
12264
14362
                                               join->tmp_table_param.start_recinfo,
12265
14363
                                                &join->tmp_table_param.recinfo,
12266
14364
                                               error, 0))
12267
 
            return(NESTED_LOOP_ERROR);
 
14365
            DBUG_RETURN(NESTED_LOOP_ERROR);
12268
14366
        }
12269
14367
        if (join->rollup.state != ROLLUP::STATE_NONE)
12270
14368
        {
12271
14369
          if (join->rollup_write_data((uint) (idx+1), table))
12272
 
            return(NESTED_LOOP_ERROR);
 
14370
            DBUG_RETURN(NESTED_LOOP_ERROR);
12273
14371
        }
12274
14372
        if (end_of_records)
12275
 
          return(NESTED_LOOP_OK);
 
14373
          DBUG_RETURN(NESTED_LOOP_OK);
12276
14374
      }
12277
14375
    }
12278
14376
    else
12279
14377
    {
12280
14378
      if (end_of_records)
12281
 
        return(NESTED_LOOP_OK);
 
14379
        DBUG_RETURN(NESTED_LOOP_OK);
12282
14380
      join->first_record=1;
12283
 
      test_if_item_cache_changed(join->group_fields);
 
14381
      VOID(test_if_item_cache_changed(join->group_fields));
12284
14382
    }
12285
14383
    if (idx < (int) join->send_group_parts)
12286
14384
    {
12287
14385
      copy_fields(&join->tmp_table_param);
12288
14386
      copy_funcs(join->tmp_table_param.items_to_copy);
12289
14387
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
12290
 
        return(NESTED_LOOP_ERROR);
12291
 
      return(NESTED_LOOP_OK);
 
14388
        DBUG_RETURN(NESTED_LOOP_ERROR);
 
14389
      DBUG_RETURN(NESTED_LOOP_OK);
12292
14390
    }
12293
14391
  }
12294
14392
  if (update_sum_func(join->sum_funcs))
12295
 
    return(NESTED_LOOP_ERROR);
12296
 
  return(NESTED_LOOP_OK);
 
14393
    DBUG_RETURN(NESTED_LOOP_ERROR);
 
14394
  DBUG_RETURN(NESTED_LOOP_OK);
12297
14395
}
12298
14396
 
12299
14397
 
12341
14439
          frequent case?
12342
14440
        */
12343
14441
        if (field->binary() &&
12344
 
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
12345
 
            field->decimals() == 0)
 
14442
            field->real_type() != MYSQL_TYPE_STRING &&
 
14443
            field->real_type() != MYSQL_TYPE_VARCHAR &&
 
14444
            (field->type() != MYSQL_TYPE_FLOAT || field->decimals() == 0))
12346
14445
        {
12347
14446
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
12348
14447
        }
12542
14641
 
12543
14642
 
12544
14643
static Item *
12545
 
part_of_refkey(Table *table,Field *field)
 
14644
part_of_refkey(TABLE *table,Field *field)
12546
14645
{
12547
14646
  if (!table->reginfo.join_tab)
12548
14647
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
12549
14648
 
12550
 
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
 
14649
  uint ref_parts=table->reginfo.join_tab->ref.key_parts;
12551
14650
  if (ref_parts)
12552
14651
  {
12553
14652
    KEY_PART_INFO *key_part=
12554
14653
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
12555
 
    uint32_t part;
 
14654
    uint part;
12556
14655
 
12557
14656
    for (part=0 ; part < ref_parts ; part++)
12558
14657
    {
12570
14669
 
12571
14670
 
12572
14671
/**
12573
 
  Test if one can use the key to resolve order_st BY.
 
14672
  Test if one can use the key to resolve ORDER BY.
12574
14673
 
12575
14674
  @param order                 Sort order
12576
14675
  @param table                 Table to sort
12590
14689
    -1   Reverse key can be used
12591
14690
*/
12592
14691
 
12593
 
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx,
12594
 
                                uint32_t *used_key_parts)
 
14692
static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
 
14693
                                uint *used_key_parts)
12595
14694
{
12596
14695
  KEY_PART_INFO *key_part,*key_part_end;
12597
14696
  key_part=table->key_info[idx].key_part;
12598
14697
  key_part_end=key_part+table->key_info[idx].key_parts;
12599
14698
  key_part_map const_key_parts=table->const_key_parts[idx];
12600
14699
  int reverse=0;
12601
 
  bool on_primary_key= false;
 
14700
  my_bool on_primary_key= false;
 
14701
  DBUG_ENTER("test_if_order_by_key");
12602
14702
 
12603
14703
  for (; order ; order=order->next, const_key_parts>>=1)
12604
14704
  {
12607
14707
 
12608
14708
    /*
12609
14709
      Skip key parts that are constants in the WHERE clause.
12610
 
      These are already skipped in the order_st BY by const_expression_in_where()
 
14710
      These are already skipped in the ORDER BY by const_expression_in_where()
12611
14711
    */
12612
14712
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
12613
14713
      key_part++; 
12635
14735
         one row).  The sorting doesn't matter.
12636
14736
        */
12637
14737
        if (key_part == key_part_end && reverse == 0)
12638
 
          return(1);
 
14738
          DBUG_RETURN(1);
12639
14739
      }
12640
14740
      else
12641
 
        return(0);
 
14741
        DBUG_RETURN(0);
12642
14742
    }
12643
14743
 
12644
14744
    if (key_part->field != field)
12645
 
      return(0);
 
14745
      DBUG_RETURN(0);
12646
14746
 
12647
14747
    /* set flag to 1 if we can use read-next on key, else to -1 */
12648
14748
    flag= ((order->asc == !(key_part->key_part_flag & HA_REVERSE_SORT)) ?
12649
14749
           1 : -1);
12650
14750
    if (reverse && flag != reverse)
12651
 
      return(0);
 
14751
      DBUG_RETURN(0);
12652
14752
    reverse=flag;                               // Remember if reverse
12653
14753
    key_part++;
12654
14754
  }
12657
14757
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
12658
14758
                         HA_READ_PREV))
12659
14759
    reverse= 0;                                 // Index can't be used
12660
 
  return(reverse);
12661
 
}
12662
 
 
 
14760
  DBUG_RETURN(reverse);
 
14761
}
 
14762
 
 
14763
 
 
14764
uint find_shortest_key(TABLE *table, const key_map *usable_keys)
 
14765
{
 
14766
  uint min_length= (uint) ~0;
 
14767
  uint best= MAX_KEY;
 
14768
  if (!usable_keys->is_clear_all())
 
14769
  {
 
14770
    for (uint nr=0; nr < table->s->keys ; nr++)
 
14771
    {
 
14772
      if (usable_keys->is_set(nr))
 
14773
      {
 
14774
        if (table->key_info[nr].key_length < min_length)
 
14775
        {
 
14776
          min_length=table->key_info[nr].key_length;
 
14777
          best=nr;
 
14778
        }
 
14779
      }
 
14780
    }
 
14781
  }
 
14782
  return best;
 
14783
}
12663
14784
 
12664
14785
/**
12665
14786
  Test if a second key is the subkey of the first one.
12700
14821
*/
12701
14822
 
12702
14823
static uint
12703
 
test_if_subkey(order_st *order, Table *table, uint32_t ref, uint32_t ref_key_parts,
 
14824
test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts,
12704
14825
               const key_map *usable_keys)
12705
14826
{
12706
 
  uint32_t nr;
12707
 
  uint32_t min_length= UINT32_MAX;
12708
 
  uint32_t best= MAX_KEY;
12709
 
  uint32_t not_used;
 
14827
  uint nr;
 
14828
  uint min_length= (uint) ~0;
 
14829
  uint best= MAX_KEY;
 
14830
  uint not_used;
12710
14831
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
12711
14832
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
12712
14833
 
12760
14881
*/
12761
14882
 
12762
14883
static bool
12763
 
list_contains_unique_index(Table *table,
 
14884
list_contains_unique_index(TABLE *table,
12764
14885
                          bool (*find_func) (Field *, void *), void *data)
12765
14886
{
12766
 
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
 
14887
  for (uint keynr= 0; keynr < table->s->keys; keynr++)
12767
14888
  {
12768
14889
    if (keynr == table->s->primary_key ||
12769
14890
         (table->key_info[keynr].flags & HA_NOSAME))
12790
14911
 
12791
14912
/**
12792
14913
  Helper function for list_contains_unique_index.
12793
 
  Find a field reference in a list of order_st structures.
 
14914
  Find a field reference in a list of ORDER structures.
12794
14915
  Finds a direct reference of the Field in the list.
12795
14916
 
12796
14917
  @param field                The field to search for.
12797
 
  @param data                 order_st *.The list to search in
 
14918
  @param data                 ORDER *.The list to search in
12798
14919
 
12799
14920
  @retval
12800
14921
    1                    found
12805
14926
static bool
12806
14927
find_field_in_order_list (Field *field, void *data)
12807
14928
{
12808
 
  order_st *group= (order_st *) data;
 
14929
  ORDER *group= (ORDER *) data;
12809
14930
  bool part_found= 0;
12810
 
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
14931
  for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
12811
14932
  {
12812
14933
    Item *item= (*tmp_group->item)->real_item();
12813
14934
    if (item->type() == Item::FIELD_ITEM &&
12857
14978
 
12858
14979
 
12859
14980
/**
12860
 
  Test if we can skip the order_st BY by using an index.
 
14981
  Test if we can skip the ORDER BY by using an index.
12861
14982
 
12862
14983
  SYNOPSIS
12863
14984
    test_if_skip_sort_order()
12883
15004
*/
12884
15005
 
12885
15006
static bool
12886
 
test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,ha_rows select_limit,
 
15007
test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
12887
15008
                        bool no_changes, const key_map *map)
12888
15009
{
12889
 
  int32_t ref_key;
12890
 
  uint32_t ref_key_parts;
 
15010
  int ref_key;
 
15011
  uint ref_key_parts;
12891
15012
  int order_direction;
12892
 
  uint32_t used_key_parts;
12893
 
  Table *table=tab->table;
 
15013
  uint used_key_parts;
 
15014
  TABLE *table=tab->table;
12894
15015
  SQL_SELECT *select=tab->select;
12895
15016
  key_map usable_keys;
12896
15017
  QUICK_SELECT_I *save_quick= 0;
 
15018
  DBUG_ENTER("test_if_skip_sort_order");
12897
15019
 
12898
15020
  /*
12899
 
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
 
15021
    Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
12900
15022
    been taken into account.
12901
15023
  */
12902
15024
  usable_keys= *map;
12903
15025
 
12904
 
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
15026
  for (ORDER *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
12905
15027
  {
12906
15028
    Item *item= (*tmp_order->item)->real_item();
12907
15029
    if (item->type() != Item::FIELD_ITEM)
12908
15030
    {
12909
15031
      usable_keys.clear_all();
12910
 
      return(0);
 
15032
      DBUG_RETURN(0);
12911
15033
    }
12912
15034
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
12913
15035
    if (usable_keys.is_clear_all())
12914
 
      return(0);                                        // No usable keys
 
15036
      DBUG_RETURN(0);                                   // No usable keys
12915
15037
  }
12916
15038
 
12917
15039
  ref_key= -1;
12921
15043
    ref_key=       tab->ref.key;
12922
15044
    ref_key_parts= tab->ref.key_parts;
12923
15045
    if (tab->type == JT_REF_OR_NULL)
12924
 
      return(0);
 
15046
      DBUG_RETURN(0);
12925
15047
  }
12926
15048
  else if (select && select->quick)             // Range found by opt_range
12927
15049
  {
12936
15058
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || 
12937
15059
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
12938
15060
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
12939
 
      return(0);
 
15061
      DBUG_RETURN(0);
12940
15062
    ref_key=       select->quick->index;
12941
15063
    ref_key_parts= select->quick->used_key_parts;
12942
15064
  }
12951
15073
      /*
12952
15074
        We come here when ref_key is not among usable_keys
12953
15075
      */
12954
 
      uint32_t new_ref_key;
 
15076
      uint new_ref_key;
12955
15077
      /*
12956
15078
        If using index only read, only consider other possible index only
12957
15079
        keys
12980
15102
 
12981
15103
          if (create_ref_for_key(tab->join, tab, keyuse, 
12982
15104
                                 tab->join->const_table_map))
12983
 
            return(0);
 
15105
            DBUG_RETURN(0);
12984
15106
        }
12985
15107
        else
12986
15108
        {
13003
15125
                                        tab->join->unit->select_limit_cnt,0,
13004
15126
                                        true) <=
13005
15127
              0)
13006
 
            return(0);
 
15128
            DBUG_RETURN(0);
13007
15129
        }
13008
15130
        ref_key= new_ref_key;
13009
15131
      }
13019
15141
      Check whether there is an index compatible with the given order
13020
15142
      usage of which is cheaper than usage of the ref_key index (ref_key>=0)
13021
15143
      or a table scan.
13022
 
      It may be the case if order_st/GROUP BY is used with LIMIT.
 
15144
      It may be the case if ORDER/GROUP BY is used with LIMIT.
13023
15145
    */
13024
 
    uint32_t nr;
 
15146
    uint nr;
13025
15147
    key_map keys;
13026
 
    uint32_t best_key_parts= 0;
 
15148
    uint best_key_parts= 0;
13027
15149
    int best_key_direction= 0;
13028
15150
    ha_rows best_records= 0;
13029
15151
    double read_time;
13031
15153
    bool is_best_covering= false;
13032
15154
    double fanout= 1;
13033
15155
    JOIN *join= tab->join;
13034
 
    uint32_t tablenr= tab - join->join_tab;
 
15156
    uint tablenr= tab - join->join_tab;
13035
15157
    ha_rows table_records= table->file->stats.records;
13036
15158
    bool group= join->group && order == join->group_list;
13037
15159
 
13047
15169
        index order and not using join cache
13048
15170
        */
13049
15171
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
13050
 
        return(0);
 
15172
        DBUG_RETURN(0);
13051
15173
      keys= *table->file->keys_to_use_for_scanning();
13052
15174
      keys.merge(table->covering_keys);
13053
15175
 
13054
15176
      /*
13055
15177
        We are adding here also the index specified in FORCE INDEX clause, 
13056
15178
        if any.
13057
 
        This is to allow users to use index in order_st BY.
 
15179
        This is to allow users to use index in ORDER BY.
13058
15180
      */
13059
15181
      if (table->force_index) 
13060
15182
        keys.merge(group ? table->keys_in_use_for_group_by :
13065
15187
      keys= usable_keys;
13066
15188
 
13067
15189
    read_time= join->best_positions[tablenr].read_time;
13068
 
    for (uint32_t i= tablenr+1; i < join->tables; i++)
 
15190
    for (uint i= tablenr+1; i < join->tables; i++)
13069
15191
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
13070
15192
 
13071
15193
    for (nr=0; nr < table->s->keys ; nr++)
13077
15199
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
13078
15200
        
13079
15201
        /* 
13080
 
          Don't use an index scan with order_st BY without limit.
 
15202
          Don't use an index scan with ORDER BY without limit.
13081
15203
          For GROUP BY without limit always use index scan
13082
15204
          if there is a suitable index. 
13083
15205
          Why we hold to this asymmetry hardly can be explained
13150
15272
            index entry.
13151
15273
          */
13152
15274
          index_scan_time= select_limit/rec_per_key *
13153
 
                           cmin(rec_per_key, table->file->scan_time());
 
15275
                           min(rec_per_key, table->file->scan_time());
13154
15276
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
13155
15277
              index_scan_time < read_time)
13156
15278
          {
13160
15282
            if (table->quick_keys.is_set(nr))
13161
15283
              quick_records= table->quick_rows[nr];
13162
15284
            if (best_key < 0 ||
13163
 
                (select_limit <= cmin(quick_records,best_records) ?
 
15285
                (select_limit <= min(quick_records,best_records) ?
13164
15286
                 keyinfo->key_parts < best_key_parts :
13165
15287
                 quick_records < best_records))
13166
15288
            {
13223
15345
            We need to change the access method so as the quick access
13224
15346
            method is actually used.
13225
15347
          */
13226
 
          assert(tab->select->quick);
 
15348
          DBUG_ASSERT(tab->select->quick);
13227
15349
          tab->type=JT_ALL;
13228
15350
          tab->use_quick=1;
13229
15351
          tab->ref.key= -1;
13238
15360
      order_direction= best_key_direction;
13239
15361
    }
13240
15362
    else
13241
 
      return(0); 
 
15363
      DBUG_RETURN(0); 
13242
15364
  } 
13243
15365
 
13244
15366
check_reverse_order:                  
13245
 
  if (order_direction == -1)            // If order_st BY ... DESC
 
15367
  if (order_direction == -1)            // If ORDER BY ... DESC
13246
15368
  {
13247
15369
    if (select && select->quick)
13248
15370
    {
13262
15384
        {
13263
15385
          tab->limit= 0;
13264
15386
          select->quick= save_quick;
13265
 
          return(0);                   // Use filesort
 
15387
          DBUG_RETURN(0);                   // Use filesort
13266
15388
        }
13267
15389
            
13268
 
        /* order_st BY range_key DESC */
 
15390
        /* ORDER BY range_key DESC */
13269
15391
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
13270
15392
                                    used_key_parts, &error);
13271
15393
        if (!tmp || error)
13273
15395
          delete tmp;
13274
15396
          select->quick= save_quick;
13275
15397
          tab->limit= 0;
13276
 
          return(0);            // Reverse sort not supported
 
15398
          DBUG_RETURN(0);               // Reverse sort not supported
13277
15399
        }
13278
15400
        select->quick=tmp;
13279
15401
      }
13282
15404
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
13283
15405
    {
13284
15406
      /*
13285
 
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
 
15407
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
13286
15408
 
13287
15409
        Use a traversal function that starts by reading the last row
13288
15410
        with key part (A) and then traverse the index backwards.
13293
15415
  }
13294
15416
  else if (select && select->quick)
13295
15417
    select->quick->sorted= 1;
13296
 
  return(1);
 
15418
  DBUG_RETURN(1);
13297
15419
}
13298
15420
 
13299
15421
 
13308
15430
     filesort_limit     Max number of rows that needs to be sorted
13309
15431
     select_limit       Max number of rows in final output
13310
15432
                        Used to decide if we should use index or not
13311
 
     is_order_by        true if we are sorting on order_st BY, false if GROUP BY
 
15433
     is_order_by        true if we are sorting on ORDER BY, false if GROUP BY
13312
15434
                        Used to decide if we should use index or not     
13313
15435
 
13314
15436
 
13327
15449
*/
13328
15450
 
13329
15451
static int
13330
 
create_sort_index(THD *thd, JOIN *join, order_st *order,
 
15452
create_sort_index(THD *thd, JOIN *join, ORDER *order,
13331
15453
                  ha_rows filesort_limit, ha_rows select_limit,
13332
15454
                  bool is_order_by)
13333
15455
{
13334
 
  uint32_t length= 0;
 
15456
  uint length= 0;
13335
15457
  ha_rows examined_rows;
13336
 
  Table *table;
 
15458
  TABLE *table;
13337
15459
  SQL_SELECT *select;
13338
15460
  JOIN_TAB *tab;
 
15461
  DBUG_ENTER("create_sort_index");
13339
15462
 
13340
15463
  if (join->tables == join->const_tables)
13341
 
    return(0);                          // One row, no need to sort
 
15464
    DBUG_RETURN(0);                             // One row, no need to sort
13342
15465
  tab=    join->join_tab + join->const_tables;
13343
15466
  table=  tab->table;
13344
15467
  select= tab->select;
13355
15478
      test_if_skip_sort_order(tab,order,select_limit,0, 
13356
15479
                              is_order_by ?  &table->keys_in_use_for_order_by :
13357
15480
                              &table->keys_in_use_for_group_by))
13358
 
    return(0);
13359
 
  for (order_st *ord= join->order; ord; ord= ord->next)
 
15481
    DBUG_RETURN(0);
 
15482
  for (ORDER *ord= join->order; ord; ord= ord->next)
13360
15483
    length++;
13361
15484
  if (!(join->sortorder= 
13362
15485
        make_unireg_sortorder(order, &length, join->sortorder)))
13424
15547
    table->key_read=0;
13425
15548
    table->file->extra(HA_EXTRA_NO_KEYREAD);
13426
15549
  }
13427
 
  return(table->sort.found_records == HA_POS_ERROR);
 
15550
  DBUG_RETURN(table->sort.found_records == HA_POS_ERROR);
13428
15551
err:
13429
 
  return(-1);
 
15552
  DBUG_RETURN(-1);
 
15553
}
 
15554
 
 
15555
/*****************************************************************************
 
15556
  Remove duplicates from tmp table
 
15557
  This should be recoded to add a unique index to the table and remove
 
15558
  duplicates
 
15559
  Table is a locked single thread table
 
15560
  fields is the number of fields to check (from the end)
 
15561
*****************************************************************************/
 
15562
 
 
15563
static bool compare_record(TABLE *table, Field **ptr)
 
15564
{
 
15565
  for (; *ptr ; ptr++)
 
15566
  {
 
15567
    if ((*ptr)->cmp_offset(table->s->rec_buff_length))
 
15568
      return 1;
 
15569
  }
 
15570
  return 0;
13430
15571
}
13431
15572
 
13432
15573
static bool copy_blobs(Field **ptr)
13451
15592
 
13452
15593
 
13453
15594
static int
13454
 
remove_duplicates(JOIN *join, Table *entry,List<Item> &fields, Item *having)
 
15595
remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having)
13455
15596
{
13456
15597
  int error;
13457
15598
  ulong reclength,offset;
13458
 
  uint32_t field_count;
 
15599
  uint field_count;
13459
15600
  THD *thd= join->thd;
 
15601
  DBUG_ENTER("remove_duplicates");
13460
15602
 
13461
15603
  entry->reginfo.lock_type=TL_WRITE;
13462
15604
 
13473
15615
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) 
13474
15616
  {                    // only const items with no OPTION_FOUND_ROWS
13475
15617
    join->unit->select_limit_cnt= 1;            // Only send first row
13476
 
    return(0);
 
15618
    DBUG_RETURN(0);
13477
15619
  }
13478
15620
  Field **first_field=entry->field+entry->s->fields - field_count;
13479
15621
  offset= (field_count ? 
13495
15637
                                  having);
13496
15638
 
13497
15639
  free_blobs(first_field);
13498
 
  return(error);
 
15640
  DBUG_RETURN(error);
13499
15641
}
13500
15642
 
13501
15643
 
13502
 
static int remove_dup_with_compare(THD *thd, Table *table, Field **first_field,
 
15644
static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field,
13503
15645
                                   ulong offset, Item *having)
13504
15646
{
13505
15647
  handler *file=table->file;
13506
15648
  char *org_record,*new_record;
13507
 
  unsigned char *record;
 
15649
  uchar *record;
13508
15650
  int error;
13509
15651
  ulong reclength= table->s->reclength-offset;
 
15652
  DBUG_ENTER("remove_dup_with_compare");
13510
15653
 
13511
15654
  org_record=(char*) (record=table->record[0])+offset;
13512
15655
  new_record=(char*) table->record[1]+offset;
13556
15699
          break;
13557
15700
        goto err;
13558
15701
      }
13559
 
      if (table->compare_record(first_field) == 0)
 
15702
      if (compare_record(table, first_field) == 0)
13560
15703
      {
13561
15704
        if ((error=file->ha_delete_row(record)))
13562
15705
          goto err;
13574
15717
  }
13575
15718
 
13576
15719
  file->extra(HA_EXTRA_NO_CACHE);
13577
 
  return(0);
 
15720
  DBUG_RETURN(0);
13578
15721
err:
13579
15722
  file->extra(HA_EXTRA_NO_CACHE);
13580
15723
  if (error)
13581
15724
    file->print_error(error,MYF(0));
13582
 
  return(1);
 
15725
  DBUG_RETURN(1);
13583
15726
}
13584
15727
 
13585
15728
 
13590
15733
    Note that this will not work on tables with blobs!
13591
15734
*/
13592
15735
 
13593
 
static int remove_dup_with_hash_index(THD *thd, Table *table,
13594
 
                                      uint32_t field_count,
 
15736
static int remove_dup_with_hash_index(THD *thd, TABLE *table,
 
15737
                                      uint field_count,
13595
15738
                                      Field **first_field,
13596
15739
                                      ulong key_length,
13597
15740
                                      Item *having)
13598
15741
{
13599
 
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
 
15742
  uchar *key_buffer, *key_pos, *record=table->record[0];
13600
15743
  int error;
13601
15744
  handler *file= table->file;
13602
15745
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
13603
 
  uint32_t *field_lengths,*field_length;
 
15746
  uint *field_lengths,*field_length;
13604
15747
  HASH hash;
 
15748
  DBUG_ENTER("remove_dup_with_hash_index");
13605
15749
 
13606
15750
  if (!my_multi_malloc(MYF(MY_WME),
13607
15751
                       &key_buffer,
13609
15753
                               (long) file->stats.records),
13610
15754
                       &field_lengths,
13611
15755
                       (uint) (field_count*sizeof(*field_lengths)),
13612
 
                       NULL))
13613
 
    return(1);
 
15756
                       NullS))
 
15757
    DBUG_RETURN(1);
13614
15758
 
13615
15759
  {
13616
15760
    Field **ptr;
13617
15761
    ulong total_length= 0;
13618
15762
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
13619
15763
    {
13620
 
      uint32_t length= (*ptr)->sort_length();
 
15764
      uint length= (*ptr)->sort_length();
13621
15765
      (*field_length++)= length;
13622
15766
      total_length+= length;
13623
15767
    }
13624
 
    assert(total_length <= key_length);
 
15768
    DBUG_PRINT("info",("field_count: %u  key_length: %lu  total_length: %lu",
 
15769
                       field_count, key_length, total_length));
 
15770
    DBUG_ASSERT(total_length <= key_length);
13625
15771
    key_length= total_length;
13626
15772
    extra_length= ALIGN_SIZE(key_length)-key_length;
13627
15773
  }
13629
15775
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, 
13630
15776
                key_length, (hash_get_key) 0, 0, 0))
13631
15777
  {
13632
 
    free((char*) key_buffer);
13633
 
    return(1);
 
15778
    my_free((char*) key_buffer,MYF(0));
 
15779
    DBUG_RETURN(1);
13634
15780
  }
13635
15781
 
13636
15782
  file->ha_rnd_init(1);
13637
15783
  key_pos=key_buffer;
13638
15784
  for (;;)
13639
15785
  {
13640
 
    unsigned char *org_key_pos;
 
15786
    uchar *org_key_pos;
13641
15787
    if (thd->killed)
13642
15788
    {
13643
15789
      thd->send_kill_message();
13678
15824
      (void) my_hash_insert(&hash, org_key_pos);
13679
15825
    key_pos+=extra_length;
13680
15826
  }
13681
 
  free((char*) key_buffer);
 
15827
  my_free((char*) key_buffer,MYF(0));
13682
15828
  hash_free(&hash);
13683
15829
  file->extra(HA_EXTRA_NO_CACHE);
13684
15830
  (void) file->ha_rnd_end();
13685
 
  return(0);
 
15831
  DBUG_RETURN(0);
13686
15832
 
13687
15833
err:
13688
 
  free((char*) key_buffer);
 
15834
  my_free((char*) key_buffer,MYF(0));
13689
15835
  hash_free(&hash);
13690
15836
  file->extra(HA_EXTRA_NO_CACHE);
13691
15837
  (void) file->ha_rnd_end();
13692
15838
  if (error)
13693
15839
    file->print_error(error,MYF(0));
13694
 
  return(1);
 
15840
  DBUG_RETURN(1);
13695
15841
}
13696
15842
 
13697
15843
 
13698
 
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length,
 
15844
SORT_FIELD *make_unireg_sortorder(ORDER *order, uint *length,
13699
15845
                                  SORT_FIELD *sortorder)
13700
15846
{
13701
 
  uint32_t count;
 
15847
  uint count;
13702
15848
  SORT_FIELD *sort,*pos;
 
15849
  DBUG_ENTER("make_unireg_sortorder");
13703
15850
 
13704
15851
  count=0;
13705
 
  for (order_st *tmp = order; tmp; tmp=tmp->next)
 
15852
  for (ORDER *tmp = order; tmp; tmp=tmp->next)
13706
15853
    count++;
13707
15854
  if (!sortorder)
13708
15855
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
13709
 
                                       (cmax(count, *length) + 1));
 
15856
                                       (max(count, *length) + 1));
13710
15857
  pos= sort= sortorder;
13711
15858
 
13712
15859
  if (!pos)
13729
15876
    pos->reverse=! order->asc;
13730
15877
  }
13731
15878
  *length=count;
13732
 
  return(sort);
 
15879
  DBUG_RETURN(sort);
13733
15880
}
13734
15881
 
13735
15882
 
13741
15888
******************************************************************************/
13742
15889
 
13743
15890
static int
13744
 
join_init_cache(THD *thd,JOIN_TAB *tables,uint32_t table_count)
 
15891
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count)
13745
15892
{
13746
15893
  register unsigned int i;
13747
15894
  unsigned int length, blobs;
13749
15896
  CACHE_FIELD *copy,**blob_ptr;
13750
15897
  JOIN_CACHE  *cache;
13751
15898
  JOIN_TAB *join_tab;
 
15899
  DBUG_ENTER("join_init_cache");
13752
15900
 
13753
15901
  cache= &tables[table_count].cache;
13754
15902
  cache->fields=blobs=0;
13773
15921
 
13774
15922
                  sizeof(CACHE_FIELD*))))
13775
15923
  {
13776
 
    free((unsigned char*) cache->buff);         /* purecov: inspected */
 
15924
    my_free((uchar*) cache->buff,MYF(0));               /* purecov: inspected */
13777
15925
    cache->buff=0;                              /* purecov: inspected */
13778
 
    return(1);                          /* purecov: inspected */
 
15926
    DBUG_RETURN(1);                             /* purecov: inspected */
13779
15927
  }
13780
15928
  copy=cache->field;
13781
15929
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
13784
15932
  length=0;
13785
15933
  for (i=0 ; i < table_count ; i++)
13786
15934
  {
13787
 
    uint32_t null_fields=0, used_fields;
 
15935
    uint null_fields=0,used_fields;
13788
15936
    Field **f_ptr,*field;
13789
15937
    MY_BITMAP *read_set= tables[i].table->read_set;
13790
15938
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
13805
15953
      }
13806
15954
    }
13807
15955
    /* Copy null bits from table */
13808
 
    if (null_fields && tables[i].table->getNullFields())
 
15956
    if (null_fields && tables[i].table->s->null_fields)
13809
15957
    {                                           /* must copy null bits */
13810
15958
      copy->str= tables[i].table->null_flags;
13811
15959
      copy->length= tables[i].table->s->null_bytes;
13819
15967
    /* If outer join table, copy null_row flag */
13820
15968
    if (tables[i].table->maybe_null)
13821
15969
    {
13822
 
      copy->str= (unsigned char*) &tables[i].table->null_row;
 
15970
      copy->str= (uchar*) &tables[i].table->null_row;
13823
15971
      copy->length=sizeof(tables[i].table->null_row);
13824
15972
      copy->strip=0;
13825
15973
      copy->blob_field=0;
13850
15998
  cache->length=length+blobs*sizeof(char*);
13851
15999
  cache->blobs=blobs;
13852
16000
  *blob_ptr=0;                                  /* End sequentel */
13853
 
  size=cmax(thd->variables.join_buff_size, (ulong)cache->length);
13854
 
  if (!(cache->buff=(unsigned char*) my_malloc(size,MYF(0))))
13855
 
    return(1);                          /* Don't use cache */ /* purecov: inspected */
 
16001
  size=max(thd->variables.join_buff_size, cache->length);
 
16002
  if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
 
16003
    DBUG_RETURN(1);                             /* Don't use cache */ /* purecov: inspected */
13856
16004
  cache->end=cache->buff+size;
13857
16005
  reset_cache_write(cache);
13858
 
  return(0);
 
16006
  DBUG_RETURN(0);
13859
16007
}
13860
16008
 
13861
16009
 
13862
16010
static ulong
13863
16011
used_blob_length(CACHE_FIELD **ptr)
13864
16012
{
13865
 
  uint32_t length,blob_length;
 
16013
  uint length,blob_length;
13866
16014
  for (length=0 ; *ptr ; ptr++)
13867
16015
  {
13868
16016
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
13876
16024
static bool
13877
16025
store_record_in_cache(JOIN_CACHE *cache)
13878
16026
{
13879
 
  uint32_t length;
13880
 
  unsigned char *pos;
 
16027
  uint length;
 
16028
  uchar *pos;
13881
16029
  CACHE_FIELD *copy,*end_field;
13882
16030
  bool last_record;
13883
16031
 
13919
16067
 
13920
16068
      if (copy->strip)
13921
16069
      {
13922
 
        unsigned char *str,*end;
 
16070
        uchar *str,*end;
13923
16071
        for (str=copy->str,end= str+copy->length;
13924
16072
             end > str && end[-1] == ' ' ;
13925
16073
             end--) ;
13952
16100
{
13953
16101
  reset_cache_read(cache);
13954
16102
  cache->records= 0;
13955
 
  cache->ptr_record= UINT32_MAX;
 
16103
  cache->ptr_record= (uint) ~0;
13956
16104
}
13957
16105
 
13958
16106
 
13959
16107
static void
13960
16108
read_cached_record(JOIN_TAB *tab)
13961
16109
{
13962
 
  unsigned char *pos;
13963
 
  uint32_t length;
 
16110
  uchar *pos;
 
16111
  uint length;
13964
16112
  bool last_record;
13965
16113
  CACHE_FIELD *copy,*end_field;
13966
16114
 
14038
16186
  }
14039
16187
  else 
14040
16188
    no_prev_key= true;
14041
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, &tab->ref)) ||
 
16189
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, tab->table,
 
16190
                                            &tab->ref)) ||
14042
16191
      no_prev_key)
14043
16192
    return 1;
14044
16193
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
14047
16196
 
14048
16197
 
14049
16198
bool
14050
 
cp_buffer_from_ref(THD *thd, TABLE_REF *ref)
 
16199
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
14051
16200
{
14052
16201
  enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
14053
16202
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
16203
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
14054
16204
  bool result= 0;
14055
16205
 
14056
16206
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
14062
16212
    }
14063
16213
  }
14064
16214
  thd->count_cuted_fields= save_count_cuted_fields;
 
16215
  dbug_tmp_restore_column_map(table->write_set, old_map);
14065
16216
  return result;
14066
16217
}
14067
16218
 
14071
16222
*****************************************************************************/
14072
16223
 
14073
16224
/**
14074
 
  Resolve an order_st BY or GROUP BY column reference.
 
16225
  Resolve an ORDER BY or GROUP BY column reference.
14075
16226
 
14076
 
  Given a column reference (represented by 'order') from a GROUP BY or order_st
 
16227
  Given a column reference (represented by 'order') from a GROUP BY or ORDER
14077
16228
  BY clause, find the actual column it represents. If the column being
14078
16229
  resolved is from the GROUP BY clause, the procedure searches the SELECT
14079
16230
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
14080
 
  the order_st BY clause, only the SELECT list is being searched.
 
16231
  the ORDER BY clause, only the SELECT list is being searched.
14081
16232
 
14082
16233
  If 'order' is resolved to an Item, then order->item is set to the found
14083
16234
  Item. If there is no item for the found column (that is, it was resolved
14095
16246
    SELECT list)
14096
16247
  @param[in,out] all_fields         All select, group and order by fields
14097
16248
  @param[in] is_group_field         True if order is a GROUP field, false if
14098
 
    order_st by field
 
16249
    ORDER by field
14099
16250
 
14100
16251
  @retval
14101
16252
    false if OK
14104
16255
*/
14105
16256
 
14106
16257
static bool
14107
 
find_order_in_list(THD *thd, Item **ref_pointer_array, TableList *tables,
14108
 
                   order_st *order, List<Item> &fields, List<Item> &all_fields,
 
16258
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16259
                   ORDER *order, List<Item> &fields, List<Item> &all_fields,
14109
16260
                   bool is_group_field)
14110
16261
{
14111
 
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
 
16262
  Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */
14112
16263
  Item::Type order_item_type;
14113
16264
  Item **select_item; /* The corresponding item from the SELECT clause. */
14114
16265
  Field *from_field;  /* The corresponding field from the FROM clause. */
14115
 
  uint32_t counter;
 
16266
  uint counter;
14116
16267
  enum_resolution_type resolution;
14117
16268
 
14118
16269
  /*
14121
16272
  */
14122
16273
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
14123
16274
  {                                             /* Order by position */
14124
 
    uint32_t count= (uint) order_item->val_int();
 
16275
    uint count= (uint) order_item->val_int();
14125
16276
    if (!count || count > fields.elements)
14126
16277
    {
14127
16278
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
14134
16285
    order->counter_used= 1;
14135
16286
    return false;
14136
16287
  }
14137
 
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
 
16288
  /* Lookup the current GROUP/ORDER field in the SELECT clause. */
14138
16289
  select_item= find_item_in_list(order_item, fields, &counter,
14139
16290
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
14140
16291
  if (!select_item)
14200
16351
        warning so the user knows that the field from the FROM clause
14201
16352
        overshadows the column reference from the SELECT list.
14202
16353
      */
14203
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
16354
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
14204
16355
                          ER(ER_NON_UNIQ_ERROR),
14205
16356
                          ((Item_ident*) order_item)->field_name,
14206
16357
                          current_thd->where);
14225
16376
       thd->is_fatal_error))
14226
16377
    return true; /* Wrong field. */
14227
16378
 
14228
 
  uint32_t el= all_fields.elements;
 
16379
  uint el= all_fields.elements;
14229
16380
  all_fields.push_front(order_item); /* Add new field to field list. */
14230
16381
  ref_pointer_array[el]= order_item;
14231
16382
  order->item= ref_pointer_array + el;
14240
16391
  the field list.
14241
16392
*/
14242
16393
 
14243
 
int setup_order(THD *thd, Item **ref_pointer_array, TableList *tables,
14244
 
                List<Item> &fields, List<Item> &all_fields, order_st *order)
 
16394
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16395
                List<Item> &fields, List<Item> &all_fields, ORDER *order)
14245
16396
{
14246
16397
  thd->where="order clause";
14247
16398
  for (; order; order=order->next)
14281
16432
*/
14282
16433
 
14283
16434
int
14284
 
setup_group(THD *thd, Item **ref_pointer_array, TableList *tables,
14285
 
            List<Item> &fields, List<Item> &all_fields, order_st *order,
 
16435
setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16436
            List<Item> &fields, List<Item> &all_fields, ORDER *order,
14286
16437
            bool *hidden_group_fields)
14287
16438
{
14288
16439
  *hidden_group_fields=0;
14289
 
  order_st *ord;
 
16440
  ORDER *ord;
14290
16441
 
14291
16442
  if (!order)
14292
16443
    return 0;                           /* Everything is ok */
14293
16444
 
14294
 
  uint32_t org_fields=all_fields.elements;
 
16445
  uint org_fields=all_fields.elements;
14295
16446
 
14296
16447
  thd->where="group statement";
14297
16448
  for (ord= order; ord; ord= ord->next)
14377
16528
  optimize away 'order by'.
14378
16529
*/
14379
16530
 
14380
 
static order_st *
 
16531
static ORDER *
14381
16532
create_distinct_group(THD *thd, Item **ref_pointer_array,
14382
 
                      order_st *order_list, List<Item> &fields,
14383
 
                      List<Item> &all_fields __attribute__((unused)),
14384
 
                      bool *all_order_by_fields_used)
 
16533
                      ORDER *order_list, List<Item> &fields,
 
16534
                      List<Item> &all_fields,
 
16535
                      bool *all_order_by_fields_used)
14385
16536
{
14386
16537
  List_iterator<Item> li(fields);
14387
 
  Item *item;
14388
 
  order_st *order,*group,**prev;
 
16538
  Item *item, **orig_ref_pointer_array= ref_pointer_array;
 
16539
  ORDER *order,*group,**prev;
14389
16540
 
14390
16541
  *all_order_by_fields_used= 1;
14391
16542
  while ((item=li++))
14396
16547
  {
14397
16548
    if (order->in_field_list)
14398
16549
    {
14399
 
      order_st *ord=(order_st*) thd->memdup((char*) order,sizeof(order_st));
 
16550
      ORDER *ord=(ORDER*) thd->memdup((char*) order,sizeof(ORDER));
14400
16551
      if (!ord)
14401
16552
        return 0;
14402
16553
      *prev=ord;
14416
16567
        Don't put duplicate columns from the SELECT list into the 
14417
16568
        GROUP BY list.
14418
16569
      */
14419
 
      order_st *ord_iter;
 
16570
      ORDER *ord_iter;
14420
16571
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
14421
16572
        if ((*ord_iter->item)->eq(item, 1))
14422
16573
          goto next_item;
14423
16574
      
14424
 
      order_st *ord=(order_st*) thd->calloc(sizeof(order_st));
 
16575
      ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER));
14425
16576
      if (!ord)
14426
16577
        return 0;
14427
16578
 
14474
16625
            param->quick_group=0;                       // UDF SUM function
14475
16626
          param->sum_func_count++;
14476
16627
 
14477
 
          for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
 
16628
          for (uint i=0 ; i < sum_item->arg_count ; i++)
14478
16629
          {
14479
16630
            if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
14480
16631
              param->field_count++;
14503
16654
*/
14504
16655
 
14505
16656
static bool
14506
 
test_if_subpart(order_st *a,order_st *b)
 
16657
test_if_subpart(ORDER *a,ORDER *b)
14507
16658
{
14508
16659
  for (; a && b; a=a->next,b=b->next)
14509
16660
  {
14520
16671
  and group and order is compatible, else return 0.
14521
16672
*/
14522
16673
 
14523
 
static Table *
14524
 
get_sort_by_table(order_st *a,order_st *b,TableList *tables)
 
16674
static TABLE *
 
16675
get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables)
14525
16676
{
14526
16677
  table_map map= (table_map) 0;
 
16678
  DBUG_ENTER("get_sort_by_table");
14527
16679
 
14528
16680
  if (!a)
14529
16681
    a=b;                                        // Only one need to be given
14533
16685
  for (; a && b; a=a->next,b=b->next)
14534
16686
  {
14535
16687
    if (!(*a->item)->eq(*b->item,1))
14536
 
      return(0);
 
16688
      DBUG_RETURN(0);
14537
16689
    map|=a->item[0]->used_tables();
14538
16690
  }
14539
16691
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
14540
 
    return(0);
 
16692
    DBUG_RETURN(0);
14541
16693
 
14542
16694
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
14543
16695
  if (map != tables->table->map)
14544
 
    return(0);                          // More than one table
14545
 
  return(tables->table);
 
16696
    DBUG_RETURN(0);                             // More than one table
 
16697
  DBUG_PRINT("exit",("sort by table: %d",tables->table->tablenr));
 
16698
  DBUG_RETURN(tables->table);
14546
16699
}
14547
16700
 
14548
16701
 
14551
16704
*/
14552
16705
 
14553
16706
static void
14554
 
calc_group_buffer(JOIN *join,order_st *group)
 
16707
calc_group_buffer(JOIN *join,ORDER *group)
14555
16708
{
14556
 
  uint32_t key_length=0, parts=0, null_parts=0;
 
16709
  uint key_length=0, parts=0, null_parts=0;
14557
16710
 
14558
16711
  if (group)
14559
16712
    join->group= 1;
14564
16717
    if (field)
14565
16718
    {
14566
16719
      enum_field_types type;
14567
 
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
 
16720
      if ((type= field->type()) == MYSQL_TYPE_BLOB)
14568
16721
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
14569
 
      else if (type == DRIZZLE_TYPE_VARCHAR)
 
16722
      else if (type == MYSQL_TYPE_VARCHAR || type == MYSQL_TYPE_VAR_STRING)
14570
16723
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
14571
16724
      else
14572
16725
        key_length+= field->pack_length();
14578
16731
        key_length+= sizeof(double);
14579
16732
        break;
14580
16733
      case INT_RESULT:
14581
 
        key_length+= sizeof(int64_t);
 
16734
        key_length+= sizeof(longlong);
14582
16735
        break;
14583
16736
      case DECIMAL_RESULT:
14584
16737
        key_length+= my_decimal_get_binary_size(group_item->max_length - 
14593
16746
          have STRING_RESULT result type, we increase the length 
14594
16747
          by 8 as maximum pack length of such fields.
14595
16748
        */
14596
 
        if (type == DRIZZLE_TYPE_TIME ||
14597
 
            type == DRIZZLE_TYPE_NEWDATE ||
14598
 
            type == DRIZZLE_TYPE_DATETIME ||
14599
 
            type == DRIZZLE_TYPE_TIMESTAMP)
 
16749
        if (type == MYSQL_TYPE_TIME ||
 
16750
            type == MYSQL_TYPE_NEWDATE ||
 
16751
            type == MYSQL_TYPE_DATETIME ||
 
16752
            type == MYSQL_TYPE_TIMESTAMP)
14600
16753
        {
14601
16754
          key_length+= 8;
14602
16755
        }
14613
16766
      }
14614
16767
      default:
14615
16768
        /* This case should never be choosen */
14616
 
        assert(0);
 
16769
        DBUG_ASSERT(0);
14617
16770
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
14618
16771
      }
14619
16772
    }
14665
16818
*/
14666
16819
 
14667
16820
static bool
14668
 
alloc_group_fields(JOIN *join,order_st *group)
 
16821
alloc_group_fields(JOIN *join,ORDER *group)
14669
16822
{
14670
16823
  if (group)
14671
16824
  {
14696
16849
 
14697
16850
int test_if_item_cache_changed(List<Cached_item> &list)
14698
16851
{
 
16852
  DBUG_ENTER("test_if_item_cache_changed");
14699
16853
  List_iterator<Cached_item> li(list);
14700
16854
  int idx= -1,i;
14701
16855
  Cached_item *buff;
14705
16859
    if (buff->cmp())
14706
16860
      idx=i;
14707
16861
  }
14708
 
  return(idx);
 
16862
  DBUG_PRINT("info", ("idx: %d", idx));
 
16863
  DBUG_RETURN(idx);
14709
16864
}
14710
16865
 
14711
16866
 
14742
16897
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
14743
16898
                  Item **ref_pointer_array,
14744
16899
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
14745
 
                  uint32_t elements, List<Item> &all_fields)
 
16900
                  uint elements, List<Item> &all_fields)
14746
16901
{
14747
16902
  Item *pos;
14748
16903
  List_iterator_fast<Item> li(all_fields);
14751
16906
  res_all_fields.empty();
14752
16907
  List_iterator_fast<Item> itr(res_all_fields);
14753
16908
  List<Item> extra_funcs;
14754
 
  uint32_t i, border= all_fields.elements - elements;
 
16909
  uint i, border= all_fields.elements - elements;
 
16910
  DBUG_ENTER("setup_copy_fields");
14755
16911
 
14756
16912
  if (param->field_count && 
14757
16913
      !(copy=param->copy_field= new Copy_field[param->field_count]))
14761
16917
  for (i= 0; (pos= li++); i++)
14762
16918
  {
14763
16919
    Field *field;
14764
 
    unsigned char *tmp;
 
16920
    uchar *tmp;
14765
16921
    Item *real_pos= pos->real_item();
14766
16922
    if (real_pos->type() == Item::FIELD_ITEM)
14767
16923
    {
14804
16960
        /*
14805
16961
          We need to allocate one extra byte for null handling and
14806
16962
          another extra byte to not get warnings from purify in
14807
 
          Field_varstring::val_int
 
16963
          Field_string::val_int
14808
16964
        */
14809
 
        if (!(tmp= (unsigned char*) sql_alloc(field->pack_length()+2)))
 
16965
        if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
14810
16966
          goto err;
14811
16967
        if (copy)
14812
16968
        {
14834
16990
      */
14835
16991
      if (!(pos=new Item_copy_string(pos)))
14836
16992
        goto err;
14837
 
      if (i < border)                           // HAVING, order_st and GROUP BY
 
16993
      if (i < border)                           // HAVING, ORDER and GROUP BY
14838
16994
      {
14839
16995
        if (extra_funcs.push_back(pos))
14840
16996
          goto err;
14852
17008
    itr++;
14853
17009
  itr.sublist(res_selected_fields, elements);
14854
17010
  /*
14855
 
    Put elements from HAVING, order_st BY and GROUP BY last to ensure that any
 
17011
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
14856
17012
    reference used in these will resolve to a item that is already calculated
14857
17013
  */
14858
17014
  param->copy_funcs.concat(&extra_funcs);
14859
17015
 
14860
 
  return(0);
 
17016
  DBUG_RETURN(0);
14861
17017
 
14862
17018
 err:
14863
17019
  if (copy)
14864
17020
    delete [] param->copy_field;                        // This is never 0
14865
17021
  param->copy_field=0;
14866
17022
err2:
14867
 
  return(true);
 
17023
  DBUG_RETURN(true);
14868
17024
}
14869
17025
 
14870
17026
 
14903
17059
 
14904
17060
bool JOIN::alloc_func_list()
14905
17061
{
14906
 
  uint32_t func_count, group_parts;
 
17062
  uint func_count, group_parts;
 
17063
  DBUG_ENTER("alloc_func_list");
14907
17064
 
14908
17065
  func_count= tmp_table_param.sum_func_count;
14909
17066
  /*
14922
17079
  {
14923
17080
    group_parts+= fields_list.elements;
14924
17081
    /*
14925
 
      If the order_st clause is specified then it's possible that
 
17082
      If the ORDER clause is specified then it's possible that
14926
17083
      it also will be optimized, so reserve space for it too
14927
17084
    */
14928
17085
    if (order)
14929
17086
    {
14930
 
      order_st *ord;
 
17087
      ORDER *ord;
14931
17088
      for (ord= order; ord; ord= ord->next)
14932
17089
        group_parts++;
14933
17090
    }
14937
17094
  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
14938
17095
                                      sizeof(Item_sum***) * (group_parts+1));
14939
17096
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
14940
 
  return(sum_funcs == 0);
 
17097
  DBUG_RETURN(sum_funcs == 0);
14941
17098
}
14942
17099
 
14943
17100
 
14961
17118
  List_iterator_fast<Item> it(field_list);
14962
17119
  Item_sum **func;
14963
17120
  Item *item;
 
17121
  DBUG_ENTER("make_sum_func_list");
14964
17122
 
14965
17123
  if (*sum_funcs && !recompute)
14966
 
    return(false); /* We have already initialized sum_funcs. */
 
17124
    DBUG_RETURN(false); /* We have already initialized sum_funcs. */
14967
17125
 
14968
17126
  func= sum_funcs;
14969
17127
  while ((item=it++))
14977
17135
  {
14978
17136
    rollup.state= ROLLUP::STATE_READY;
14979
17137
    if (rollup_make_fields(field_list, send_fields, &func))
14980
 
      return(true);                     // Should never happen
 
17138
      DBUG_RETURN(true);                        // Should never happen
14981
17139
  }
14982
17140
  else if (rollup.state == ROLLUP::STATE_NONE)
14983
17141
  {
14984
 
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
 
17142
    for (uint i=0 ; i <= send_group_parts ;i++)
14985
17143
      sum_funcs_end[i]= func;
14986
17144
  }
14987
17145
  else if (rollup.state == ROLLUP::STATE_READY)
14988
 
    return(false);                         // Don't put end marker
 
17146
    DBUG_RETURN(false);                         // Don't put end marker
14989
17147
  *func=0;                                      // End marker
14990
 
  return(false);
 
17148
  DBUG_RETURN(false);
14991
17149
}
14992
17150
 
14993
17151
 
15012
17170
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
15013
17171
                         List<Item> &res_selected_fields,
15014
17172
                         List<Item> &res_all_fields,
15015
 
                         uint32_t elements, List<Item> &all_fields)
 
17173
                         uint elements, List<Item> &all_fields)
15016
17174
{
15017
17175
  List_iterator_fast<Item> it(all_fields);
15018
17176
  Item *item_field,*item;
 
17177
  DBUG_ENTER("change_to_use_tmp_fields");
15019
17178
 
15020
17179
  res_selected_fields.empty();
15021
17180
  res_all_fields.empty();
15022
17181
 
15023
 
  uint32_t i, border= all_fields.elements - elements;
 
17182
  uint i, border= all_fields.elements - elements;
15024
17183
  for (i= 0; (item= it++); i++)
15025
17184
  {
15026
17185
    Field *field;
15042
17201
        else
15043
17202
          item_field= (Item*) new Item_field(field);
15044
17203
        if (!item_field)
15045
 
          return(true);                    // Fatal error
 
17204
          DBUG_RETURN(true);                    // Fatal error
15046
17205
 
15047
17206
        if (item->real_item()->type() != Item::FIELD_ITEM)
15048
17207
          field->orig_table= 0;
15054
17213
          ifield->table_name= iref->table_name;
15055
17214
          ifield->db_name= iref->db_name;
15056
17215
        }
 
17216
#ifndef DBUG_OFF
 
17217
        if (!item_field->name)
 
17218
        {
 
17219
          char buff[256];
 
17220
          String str(buff,sizeof(buff),&my_charset_bin);
 
17221
          str.length(0);
 
17222
          item->print(&str, QT_ORDINARY);
 
17223
          item_field->name= sql_strmake(str.ptr(),str.length());
 
17224
        }
 
17225
#endif
15057
17226
      }
15058
17227
      else
15059
17228
        item_field= item;
15067
17236
  for (i= 0; i < border; i++)
15068
17237
    itr++;
15069
17238
  itr.sublist(res_selected_fields, elements);
15070
 
  return(false);
 
17239
  DBUG_RETURN(false);
15071
17240
}
15072
17241
 
15073
17242
 
15091
17260
static bool
15092
17261
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
15093
17262
                          List<Item> &res_selected_fields,
15094
 
                          List<Item> &res_all_fields, uint32_t elements,
 
17263
                          List<Item> &res_all_fields, uint elements,
15095
17264
                          List<Item> &all_fields)
15096
17265
{
15097
17266
  List_iterator_fast<Item> it(all_fields);
15099
17268
  res_selected_fields.empty();
15100
17269
  res_all_fields.empty();
15101
17270
 
15102
 
  uint32_t i, border= all_fields.elements - elements;
 
17271
  uint i, border= all_fields.elements - elements;
15103
17272
  for (i= 0; (item= it++); i++)
15104
17273
  {
15105
17274
    res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
15137
17306
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
15138
17307
{
15139
17308
  Item_sum *func;
 
17309
  DBUG_ENTER("setup_sum_funcs");
15140
17310
  while ((func= *(func_ptr++)))
15141
17311
  {
15142
17312
    if (func->setup(thd))
15143
 
      return(true);
 
17313
      DBUG_RETURN(true);
15144
17314
  }
15145
 
  return(false);
 
17315
  DBUG_RETURN(false);
15146
17316
}
15147
17317
 
15148
17318
 
15159
17329
 
15160
17330
static void
15161
17331
update_tmptable_sum_func(Item_sum **func_ptr,
15162
 
                         Table *tmp_table __attribute__((unused)))
 
17332
                         TABLE *tmp_table __attribute__((unused)))
15163
17333
{
15164
17334
  Item_sum *func;
15165
17335
  while ((func= *(func_ptr++)))
15224
17394
 
15225
17395
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
15226
17396
{
 
17397
  DBUG_ENTER("add_ref_to_table_cond");
15227
17398
  if (!join_tab->ref.key_parts)
15228
 
    return(false);
 
17399
    DBUG_RETURN(false);
15229
17400
 
15230
17401
  Item_cond_and *cond=new Item_cond_and();
15231
 
  Table *table=join_tab->table;
 
17402
  TABLE *table=join_tab->table;
15232
17403
  int error;
15233
17404
  if (!cond)
15234
 
    return(true);
 
17405
    DBUG_RETURN(true);
15235
17406
 
15236
 
  for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
 
17407
  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
15237
17408
  {
15238
17409
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
15239
17410
                              fieldnr-1];
15241
17412
    cond->add(new Item_func_equal(new Item_field(field), value));
15242
17413
  }
15243
17414
  if (thd->is_fatal_error)
15244
 
    return(true);
 
17415
    DBUG_RETURN(true);
15245
17416
 
15246
17417
  if (!cond->fixed)
15247
17418
    cond->fix_fields(thd, (Item**)&cond);
15254
17425
                                          &error)))
15255
17426
    join_tab->select_cond=cond;
15256
17427
 
15257
 
  return(error ? true : false);
 
17428
  DBUG_RETURN(error ? true : false);
15258
17429
}
15259
17430
 
15260
17431
 
15265
17436
  @param select   pointer to st_select_lex which subselects joins we will free
15266
17437
*/
15267
17438
 
15268
 
void free_underlaid_joins(THD *thd __attribute__((unused)),
15269
 
                          SELECT_LEX *select)
 
17439
void free_underlaid_joins(THD *thd, SELECT_LEX *select)
15270
17440
{
15271
17441
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
15272
17442
       unit;
15318
17488
    1   on error
15319
17489
*/
15320
17490
 
15321
 
static bool change_group_ref(THD *thd, Item_func *expr, order_st *group_list,
 
17491
static bool change_group_ref(THD *thd, Item_func *expr, ORDER *group_list,
15322
17492
                             bool *changed)
15323
17493
{
15324
17494
  if (expr->arg_count)
15333
17503
      Item *item= *arg;
15334
17504
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
15335
17505
      {
15336
 
        order_st *group_tmp;
 
17506
        ORDER *group_tmp;
15337
17507
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
15338
17508
        {
15339
17509
          if (item->eq(*group_tmp->item,0))
15367
17537
 
15368
17538
bool JOIN::rollup_init()
15369
17539
{
15370
 
  uint32_t i,j;
 
17540
  uint i,j;
15371
17541
  Item **ref_array;
15372
17542
 
15373
17543
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
15411
17581
  Item *item;
15412
17582
  while ((item= it++))
15413
17583
  {
15414
 
    order_st *group_tmp;
 
17584
    ORDER *group_tmp;
15415
17585
    bool found_in_group= 0;
15416
17586
 
15417
17587
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
15440
17610
            return 1;
15441
17611
          new_item->fix_fields(thd, (Item **) 0);
15442
17612
          thd->change_item_tree(it.ref(), new_item);
15443
 
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
 
17613
          for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
15444
17614
          { 
15445
17615
            if (*tmp->item == item)
15446
17616
              thd->change_item_tree(tmp->item, new_item);
15487
17657
{
15488
17658
  List_iterator_fast<Item> it(fields_arg);
15489
17659
  Item *first_field= sel_fields.head();
15490
 
  uint32_t level;
 
17660
  uint level;
15491
17661
 
15492
17662
  /*
15493
17663
    Create field lists for the different levels
15512
17682
 
15513
17683
  for (level=0 ; level < send_group_parts ; level++)
15514
17684
  {
15515
 
    uint32_t i;
15516
 
    uint32_t pos= send_group_parts - level -1;
 
17685
    uint i;
 
17686
    uint pos= send_group_parts - level -1;
15517
17687
    bool real_fields= 0;
15518
17688
    Item *item;
15519
17689
    List_iterator<Item> new_it(rollup.fields[pos]);
15520
17690
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
15521
 
    order_st *start_group;
 
17691
    ORDER *start_group;
15522
17692
 
15523
17693
    /* Point to first hidden field */
15524
17694
    Item **ref_array= ref_array_start + fields_arg.elements-1;
15562
17732
      else 
15563
17733
      {
15564
17734
        /* Check if this is something that is part of this group by */
15565
 
        order_st *group_tmp;
 
17735
        ORDER *group_tmp;
15566
17736
        for (group_tmp= start_group, i= pos ;
15567
17737
             group_tmp ; group_tmp= group_tmp->next, i++)
15568
17738
        {
15616
17786
    1   If send_data_failed()
15617
17787
*/
15618
17788
 
15619
 
int JOIN::rollup_send_data(uint32_t idx)
 
17789
int JOIN::rollup_send_data(uint idx)
15620
17790
{
15621
 
  uint32_t i;
 
17791
  uint i;
15622
17792
  for (i= send_group_parts ; i-- > idx ; )
15623
17793
  {
15624
17794
    /* Get reference pointers to sum functions in place */
15625
 
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
17795
    memcpy((char*) ref_pointer_array,
 
17796
           (char*) rollup.ref_pointer_arrays[i],
15626
17797
           ref_pointer_array_size);
15627
17798
    if ((!having || having->val_int()))
15628
17799
    {
15657
17828
    1   if write_data_failed()
15658
17829
*/
15659
17830
 
15660
 
int JOIN::rollup_write_data(uint32_t idx, Table *table_arg)
 
17831
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
15661
17832
{
15662
 
  uint32_t i;
 
17833
  uint i;
15663
17834
  for (i= send_group_parts ; i-- > idx ; )
15664
17835
  {
15665
17836
    /* Get reference pointers to sum functions in place */
15666
 
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
17837
    memcpy((char*) ref_pointer_array,
 
17838
           (char*) rollup.ref_pointer_arrays[i],
15667
17839
           ref_pointer_array_size);
15668
17840
    if ((!having || having->val_int()))
15669
17841
    {
15723
17895
  THD *thd=join->thd;
15724
17896
  select_result *result=join->result;
15725
17897
  Item *item_null= new Item_null();
15726
 
  const CHARSET_INFO * const cs= system_charset_info;
 
17898
  CHARSET_INFO *cs= system_charset_info;
15727
17899
  int quick_type;
 
17900
  DBUG_ENTER("select_describe");
 
17901
  DBUG_PRINT("info", ("Select 0x%lx, type %s, message %s",
 
17902
                      (ulong)join->select_lex, join->select_lex->type,
 
17903
                      message ? message : "NULL"));
15728
17904
  /* Don't log this into the slow query log */
15729
17905
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
15730
17906
  join->unit->offset_limit_cnt= 0;
15735
17911
  */
15736
17912
  if (message)
15737
17913
  {
15738
 
    item_list.push_back(new Item_int((int32_t)
 
17914
    item_list.push_back(new Item_int((int32)
15739
17915
                                     join->select_lex->select_number));
15740
17916
    item_list.push_back(new Item_string(join->select_lex->type,
15741
17917
                                        strlen(join->select_lex->type), cs));
15742
 
    for (uint32_t i=0 ; i < 7; i++)
 
17918
    for (uint i=0 ; i < 7; i++)
15743
17919
      item_list.push_back(item_null);
15744
17920
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
15745
17921
      item_list.push_back(item_null);
15768
17944
    /* table */
15769
17945
    {
15770
17946
      SELECT_LEX *sl= join->unit->first_select();
15771
 
      uint32_t len= 6, lastop= 0;
 
17947
      uint len= 6, lastop= 0;
15772
17948
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
15773
17949
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
15774
17950
      {
15818
17994
  else
15819
17995
  {
15820
17996
    table_map used_tables=0;
15821
 
    for (uint32_t i=0 ; i < join->tables ; i++)
 
17997
    for (uint i=0 ; i < join->tables ; i++)
15822
17998
    {
15823
17999
      JOIN_TAB *tab=join->join_tab+i;
15824
 
      Table *table=tab->table;
15825
 
      TableList *table_list= tab->table->pos_in_table_list;
 
18000
      TABLE *table=tab->table;
 
18001
      TABLE_LIST *table_list= tab->table->pos_in_table_list;
15826
18002
      char buff[512]; 
15827
18003
      char buff1[512], buff2[512], buff3[512];
15828
18004
      char keylen_str_buf[64];
15839
18015
      quick_type= -1;
15840
18016
      item_list.empty();
15841
18017
      /* id */
15842
 
      item_list.push_back(new Item_uint((uint32_t)
 
18018
      item_list.push_back(new Item_uint((uint32)
15843
18019
                                       join->select_lex->select_number));
15844
18020
      /* select_type */
15845
18021
      item_list.push_back(new Item_string(join->select_lex->type,
15866
18042
      }
15867
18043
      else
15868
18044
      {
15869
 
        TableList *real_table= table->pos_in_table_list; 
 
18045
        TABLE_LIST *real_table= table->pos_in_table_list; 
15870
18046
        item_list.push_back(new Item_string(real_table->alias,
15871
18047
                                            strlen(real_table->alias),
15872
18048
                                            cs));
15878
18054
      /* Build "possible_keys" value and add it to item_list */
15879
18055
      if (!tab->keys.is_clear_all())
15880
18056
      {
15881
 
        uint32_t j;
 
18057
        uint j;
15882
18058
        for (j=0 ; j < table->s->keys ; j++)
15883
18059
        {
15884
18060
          if (tab->keys.is_set(j))
15900
18076
      if (tab->ref.key_parts)
15901
18077
      {
15902
18078
        KEY *key_info=table->key_info+ tab->ref.key;
15903
 
        register uint32_t length;
 
18079
        register uint length;
15904
18080
        item_list.push_back(new Item_string(key_info->name,
15905
18081
                                            strlen(key_info->name),
15906
18082
                                            system_charset_info));
15907
 
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) - 
 
18083
        length= longlong2str(tab->ref.key_length, keylen_str_buf, 10) - 
15908
18084
                keylen_str_buf;
15909
18085
        item_list.push_back(new Item_string(keylen_str_buf, length,
15910
18086
                                            system_charset_info));
15920
18096
      else if (tab->type == JT_NEXT)
15921
18097
      {
15922
18098
        KEY *key_info=table->key_info+ tab->index;
15923
 
        register uint32_t length;
 
18099
        register uint length;
15924
18100
        item_list.push_back(new Item_string(key_info->name,
15925
18101
                                            strlen(key_info->name),cs));
15926
 
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) - 
 
18102
        length= longlong2str(key_info->key_length, keylen_str_buf, 10) - 
15927
18103
                keylen_str_buf;
15928
18104
        item_list.push_back(new Item_string(keylen_str_buf, 
15929
18105
                                            length,
15939
18115
      }
15940
18116
      else
15941
18117
      {
15942
 
        if (table_list->schema_table && table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
18118
        if (table_list->schema_table &&
 
18119
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
15943
18120
        {
15944
18121
          const char *tmp_buff;
15945
18122
          int f_idx;
15988
18165
        else
15989
18166
          examined_rows= join->best_positions[i].records_read; 
15990
18167
 
15991
 
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows, 
 
18168
        item_list.push_back(new Item_int((longlong) (ulonglong) examined_rows, 
15992
18169
                                         MY_INT64_NUM_DECIMAL_DIGITS));
15993
18170
 
15994
18171
        /* Add "filtered" field to item_list. */
16003
18180
      }
16004
18181
 
16005
18182
      /* Build "Extra" field and add it to item_list. */
16006
 
      bool key_read=table->key_read;
 
18183
      my_bool key_read=table->key_read;
16007
18184
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
16008
18185
          table->covering_keys.is_set(tab->index))
16009
18186
        key_read=1;
16023
18200
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
16024
18201
        /* Skip initial "; "*/
16025
18202
        const char *str= extra.ptr();
16026
 
        uint32_t len= extra.length();
 
18203
        uint32 len= extra.length();
16027
18204
        if (len)
16028
18205
        {
16029
18206
          str += 2;
16033
18210
      }
16034
18211
      else
16035
18212
      {
16036
 
        uint32_t keyno= MAX_KEY;
 
18213
        uint keyno= MAX_KEY;
16037
18214
        if (tab->ref.key_parts)
16038
18215
          keyno= tab->ref.key;
16039
18216
        else if (tab->select && tab->select->quick)
16139
18316
        else if (tab->do_firstmatch)
16140
18317
        {
16141
18318
          extra.append(STRING_WITH_LEN("; FirstMatch("));
16142
 
          Table *prev_table=tab->do_firstmatch->table;
 
18319
          TABLE *prev_table=tab->do_firstmatch->table;
16143
18320
          if (prev_table->derived_select_number)
16144
18321
          {
16145
18322
            char namebuf[NAME_LEN];
16154
18331
          extra.append(STRING_WITH_LEN(")"));
16155
18332
        }
16156
18333
 
16157
 
        for (uint32_t part= 0; part < tab->ref.key_parts; part++)
 
18334
        for (uint part= 0; part < tab->ref.key_parts; part++)
16158
18335
        {
16159
18336
          if (tab->ref.cond_guards[part])
16160
18337
          {
16168
18345
 
16169
18346
        /* Skip initial "; "*/
16170
18347
        const char *str= extra.ptr();
16171
 
        uint32_t len= extra.length();
 
18348
        uint32 len= extra.length();
16172
18349
        if (len)
16173
18350
        {
16174
18351
          str += 2;
16187
18364
       unit= unit->next_unit())
16188
18365
  {
16189
18366
    if (mysql_explain_union(thd, unit, result))
16190
 
      return;
 
18367
      DBUG_VOID_RETURN;
16191
18368
  }
16192
 
  return;
 
18369
  DBUG_VOID_RETURN;
16193
18370
}
16194
18371
 
16195
18372
 
16196
18373
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
16197
18374
{
 
18375
  DBUG_ENTER("mysql_explain_union");
16198
18376
  bool res= 0;
16199
18377
  SELECT_LEX *first= unit->first_select();
16200
18378
 
16203
18381
       sl= sl->next_select())
16204
18382
  {
16205
18383
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
16206
 
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
 
18384
    uint8 uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
16207
18385
    sl->type= (((&thd->lex->select_lex)==sl)?
16208
18386
               (sl->first_inner_unit() || sl->next_select() ? 
16209
18387
                "PRIMARY" : "SIMPLE"):
16234
18412
    thd->lex->current_select= first;
16235
18413
    unit->set_limit(unit->global_parameters);
16236
18414
    res= mysql_select(thd, &first->ref_pointer_array,
16237
 
                        (TableList*) first->table_list.first,
 
18415
                        (TABLE_LIST*) first->table_list.first,
16238
18416
                        first->with_wild, first->item_list,
16239
18417
                        first->where,
16240
18418
                        first->order_list.elements +
16241
18419
                        first->group_list.elements,
16242
 
                        (order_st*) first->order_list.first,
16243
 
                        (order_st*) first->group_list.first,
 
18420
                        (ORDER*) first->order_list.first,
 
18421
                        (ORDER*) first->group_list.first,
16244
18422
                        first->having,
16245
 
                        (order_st*) thd->lex->proc_list.first,
 
18423
                        (ORDER*) thd->lex->proc_list.first,
16246
18424
                        first->options | thd->options | SELECT_DESCRIBE,
16247
18425
                        result, unit, first);
16248
18426
  }
16249
 
  return(res || thd->is_error());
 
18427
  DBUG_RETURN(res || thd->is_error());
16250
18428
}
16251
18429
 
16252
18430
 
16253
 
static void print_table_array(THD *thd, String *str, TableList **table, 
16254
 
                              TableList **end)
 
18431
static void print_table_array(THD *thd, String *str, TABLE_LIST **table, 
 
18432
                              TABLE_LIST **end)
16255
18433
{
16256
18434
  (*table)->print(thd, str, QT_ORDINARY);
16257
18435
 
16258
 
  for (TableList **tbl= table + 1; tbl < end; tbl++)
 
18436
  for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
16259
18437
  {
16260
 
    TableList *curr= *tbl;
 
18438
    TABLE_LIST *curr= *tbl;
16261
18439
    if (curr->outer_join)
16262
18440
    {
16263
18441
      /* MySQL converts right to left joins */
16290
18468
 
16291
18469
static void print_join(THD *thd,
16292
18470
                       String *str,
16293
 
                       List<TableList> *tables,
16294
 
                       enum_query_type query_type __attribute__((unused)))
 
18471
                       List<TABLE_LIST> *tables,
 
18472
                       enum_query_type query_type)
16295
18473
{
16296
18474
  /* List is reversed => we should reverse it before using */
16297
 
  List_iterator_fast<TableList> ti(*tables);
16298
 
  TableList **table= (TableList **)thd->alloc(sizeof(TableList*) *
 
18475
  List_iterator_fast<TABLE_LIST> ti(*tables);
 
18476
  TABLE_LIST **table= (TABLE_LIST **)thd->alloc(sizeof(TABLE_LIST*) *
16299
18477
                                                tables->elements);
16300
18478
  if (table == 0)
16301
18479
    return;  // out of memory
16302
18480
 
16303
 
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
 
18481
  for (TABLE_LIST **t= table + (tables->elements - 1); t >= table; t--)
16304
18482
    *t= ti++;
16305
18483
  
16306
18484
  /* 
16309
18487
  */
16310
18488
  if ((*table)->sj_inner_tables)
16311
18489
  {
16312
 
    TableList **end= table + tables->elements;
16313
 
    for (TableList **t2= table; t2!=end; t2++)
 
18490
    TABLE_LIST **end= table + tables->elements;
 
18491
    for (TABLE_LIST **t2= table; t2!=end; t2++)
16314
18492
    {
16315
18493
      if (!(*t2)->sj_inner_tables)
16316
18494
      {
16317
 
        TableList *tmp= *t2;
 
18495
        TABLE_LIST *tmp= *t2;
16318
18496
        *t2= *table;
16319
18497
        *table= tmp;
16320
18498
        break;
16321
18499
      }
16322
18500
    }
16323
18501
  }
16324
 
  assert(tables->elements >= 1);
 
18502
  DBUG_ASSERT(tables->elements >= 1);
16325
18503
  print_table_array(thd, str, table, table + tables->elements);
16326
18504
}
16327
18505
 
16352
18530
  if (key_name.length)
16353
18531
  {
16354
18532
    if (thd && !my_strnncoll(system_charset_info,
16355
 
                             (const unsigned char *)key_name.str, key_name.length, 
16356
 
                             (const unsigned char *)primary_key_name, 
 
18533
                             (const uchar *)key_name.str, key_name.length, 
 
18534
                             (const uchar *)primary_key_name, 
16357
18535
                             strlen(primary_key_name)))
16358
18536
      str->append(primary_key_name);
16359
18537
    else
16369
18547
  @param str   string where table should be printed
16370
18548
*/
16371
18549
 
16372
 
void TableList::print(THD *thd, String *str, enum_query_type query_type)
 
18550
void TABLE_LIST::print(THD *thd, String *str, enum_query_type query_type)
16373
18551
{
16374
18552
  if (nested_join)
16375
18553
  {
16417
18595
      {
16418
18596
        if (alias && alias[0])
16419
18597
        {
16420
 
          my_stpcpy(t_alias_buff, alias);
 
18598
          strmov(t_alias_buff, alias);
16421
18599
          my_casedn_str(files_charset_info, t_alias_buff);
16422
18600
          t_alias= t_alias_buff;
16423
18601
        }
16515
18693
  if (group_list.elements)
16516
18694
  {
16517
18695
    str->append(STRING_WITH_LEN(" group by "));
16518
 
    print_order(str, (order_st *) group_list.first, query_type);
 
18696
    print_order(str, (ORDER *) group_list.first, query_type);
16519
18697
    switch (olap)
16520
18698
    {
16521
18699
      case CUBE_TYPE:
16546
18724
  if (order_list.elements)
16547
18725
  {
16548
18726
    str->append(STRING_WITH_LEN(" order by "));
16549
 
    print_order(str, (order_st *) order_list.first, query_type);
 
18727
    print_order(str, (ORDER *) order_list.first, query_type);
16550
18728
  }
16551
18729
 
16552
18730
  // limit
16569
18747
 
16570
18748
bool JOIN::change_result(select_result *res)
16571
18749
{
 
18750
  DBUG_ENTER("JOIN::change_result");
16572
18751
  result= res;
16573
18752
  if (result->prepare(fields_list, select_lex->master_unit()) ||
16574
18753
                     result->prepare2())
16575
18754
  {
16576
 
    return(true);
 
18755
    DBUG_RETURN(true);
16577
18756
  }
16578
 
  return(false);
 
18757
  DBUG_RETURN(false);
16579
18758
}
16580
18759
 
16581
18760
/**