~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_select.cc

  • Committer: Mark Atwood
  • Date: 2008-07-12 07:25:25 UTC
  • mto: This revision was merged to the branch mainline in revision 139.
  • Revision ID: me@mark.atwood.name-20080712072525-s1dq9mtwo5td7af7
more hackery to get plugin UDFs working

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>
 
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>
33
37
 
34
38
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
35
39
                              "MAYBE_REF","ALL","range","index",
40
44
struct st_sargable_param;
41
45
 
42
46
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
43
 
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds,
 
47
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
44
48
                                 DYNAMIC_ARRAY *keyuse);
45
49
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
46
50
                                JOIN_TAB *join_tab,
47
 
                                uint32_t tables, COND *conds,
 
51
                                uint tables, COND *conds,
48
52
                                COND_EQUAL *cond_equal,
49
53
                                table_map table_map, SELECT_LEX *select_lex,
50
54
                                st_sargable_param **sargables);
51
55
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
52
 
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);
53
57
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
54
58
                               table_map used_tables);
55
59
static bool choose_plan(JOIN *join,table_map join_tables);
56
60
 
57
61
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
58
 
                             table_map remaining_tables, uint32_t idx,
 
62
                             table_map remaining_tables, uint idx,
59
63
                             double record_count, double read_time);
60
64
static void optimize_straight_join(JOIN *join, table_map join_tables);
61
65
static bool greedy_search(JOIN *join, table_map remaining_tables,
62
 
                             uint32_t depth, uint32_t prune_level);
 
66
                             uint depth, uint prune_level);
63
67
static bool best_extension_by_limited_search(JOIN *join,
64
68
                                             table_map remaining_tables,
65
 
                                             uint32_t idx, double record_count,
66
 
                                             double read_time, uint32_t depth,
67
 
                                             uint32_t prune_level);
68
 
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);
69
73
static int join_tab_cmp(const void* ptr1, const void* ptr2);
70
74
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
71
75
/*
72
76
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
73
77
  tested and approved.
74
78
*/
75
 
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,
76
80
                      double record_count,double read_time);
77
 
static uint32_t cache_record_length(JOIN *join,uint32_t index);
78
 
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);
79
83
static bool get_best_combination(JOIN *join);
80
84
static store_key *get_store_key(THD *thd,
81
85
                                KEYUSE *keyuse, table_map used_tables,
82
 
                                KEY_PART_INFO *key_part, unsigned char *key_buff,
83
 
                                uint32_t maybe_null);
84
 
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);
85
89
static void make_outerjoin_info(JOIN *join);
86
90
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
87
 
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
88
 
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);
89
93
static void update_depend_map(JOIN *join);
90
 
static void update_depend_map(JOIN *join, order_st *order);
91
 
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,
92
96
                           bool change_list, bool *simple_order);
93
 
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,
94
98
                            List<Item> &fields, bool send_row,
95
 
                            uint64_t select_options, const char *info,
 
99
                            ulonglong select_options, const char *info,
96
100
                            Item *having);
97
101
static COND *build_equal_items(THD *thd, COND *cond,
98
102
                               COND_EQUAL *inherited,
99
 
                               List<TableList> *join_list,
 
103
                               List<TABLE_LIST> *join_list,
100
104
                               COND_EQUAL **cond_equal_ref);
101
105
static COND* substitute_for_best_equal_field(COND *cond,
102
106
                                             COND_EQUAL *cond_equal,
103
107
                                             void *table_join_idx);
104
 
static COND *simplify_joins(JOIN *join, List<TableList> *join_list,
 
108
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
105
109
                            COND *conds, bool top, bool in_sj);
106
110
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
107
111
static void restore_prev_nj_state(JOIN_TAB *last);
108
 
static void reset_nj_counters(List<TableList> *join_list);
109
 
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
110
 
                                          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);
111
115
 
112
116
static 
113
117
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
115
119
                                  const JOIN_TAB *tab);
116
120
 
117
121
static COND *optimize_cond(JOIN *join, COND *conds,
118
 
                           List<TableList> *join_list,
 
122
                           List<TABLE_LIST> *join_list,
119
123
                           Item::cond_result *cond_value);
120
124
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
121
 
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);
122
131
 
123
132
static enum_nested_loop_state
124
133
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
159
168
static COND *make_cond_for_table(COND *cond,table_map table,
160
169
                                 table_map used_table,
161
170
                                 bool exclude_expensive_cond);
162
 
static Item* part_of_refkey(Table *form,Field *field);
163
 
static bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,
 
171
static Item* part_of_refkey(TABLE *form,Field *field);
 
172
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
164
173
                                    ha_rows select_limit, bool no_changes,
165
174
                                    const key_map *map);
166
 
static bool list_contains_unique_index(Table *table,
 
175
static bool list_contains_unique_index(TABLE *table,
167
176
                          bool (*find_func) (Field *, void *), void *data);
168
177
static bool find_field_in_item_list (Field *field, void *data);
169
178
static bool find_field_in_order_list (Field *field, void *data);
170
 
static int create_sort_index(THD *thd, JOIN *join, order_st *order,
 
179
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
171
180
                             ha_rows filesort_limit, ha_rows select_limit,
172
181
                             bool is_order_by);
173
 
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields,
 
182
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
174
183
                             Item *having);
175
 
static int remove_dup_with_compare(THD *thd, Table *entry, Field **field,
 
184
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
176
185
                                   ulong offset,Item *having);
177
 
static int remove_dup_with_hash_index(THD *thd,Table *table,
178
 
                                      uint32_t field_count, Field **first_field,
 
186
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
 
187
                                      uint field_count, Field **first_field,
179
188
 
180
189
                                      ulong key_length,Item *having);
181
 
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint32_t table_count);
 
190
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
182
191
static ulong used_blob_length(CACHE_FIELD **ptr);
183
192
static bool store_record_in_cache(JOIN_CACHE *cache);
184
193
static void reset_cache_read(JOIN_CACHE *cache);
185
194
static void reset_cache_write(JOIN_CACHE *cache);
186
195
static void read_cached_record(JOIN_TAB *tab);
187
196
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
188
 
static order_st *create_distinct_group(THD *thd, Item **ref_pointer_array,
189
 
                                    order_st *order, List<Item> &fields,
 
197
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
 
198
                                    ORDER *order, List<Item> &fields,
190
199
                                    List<Item> &all_fields,
191
200
                                    bool *all_order_by_fields_used);
192
 
static bool test_if_subpart(order_st *a,order_st *b);
193
 
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
194
 
static void calc_group_buffer(JOIN *join,order_st *group);
 
201
static bool test_if_subpart(ORDER *a,ORDER *b);
 
202
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
 
203
static void calc_group_buffer(JOIN *join,ORDER *group);
195
204
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
196
 
static bool alloc_group_fields(JOIN *join,order_st *group);
 
205
static bool alloc_group_fields(JOIN *join,ORDER *group);
197
206
// Create list for using with tempory table
198
207
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
199
208
                                     List<Item> &new_list1,
200
209
                                     List<Item> &new_list2,
201
 
                                     uint32_t elements, List<Item> &items);
 
210
                                     uint elements, List<Item> &items);
202
211
// Create list for using with tempory table
203
212
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
204
213
                                      List<Item> &new_list1,
205
214
                                      List<Item> &new_list2,
206
 
                                      uint32_t elements, List<Item> &items);
 
215
                                      uint elements, List<Item> &items);
207
216
static void init_tmptable_sum_functions(Item_sum **func);
208
 
static void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
 
217
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
209
218
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
210
219
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
211
220
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
212
221
static bool init_sum_functions(Item_sum **func, Item_sum **end);
213
222
static bool update_sum_func(Item_sum **func);
214
223
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
215
 
                            bool distinct, const char *message=NULL);
 
224
                            bool distinct, const char *message=NullS);
216
225
static Item *remove_additional_cond(Item* conds);
217
226
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
218
227
static bool test_if_ref(Item_field *left_item,Item *right_item);
241
250
{
242
251
  bool res;
243
252
  register SELECT_LEX *select_lex = &lex->select_lex;
244
 
  DRIZZLE_SELECT_START();
 
253
  MYSQL_SELECT_START();
245
254
 
246
255
  if (select_lex->master_unit()->is_union() || 
247
256
      select_lex->master_unit()->fake_select_lex)
257
266
      setup_tables_done_option changed for next rexecution
258
267
    */
259
268
    res= mysql_select(thd, &select_lex->ref_pointer_array,
260
 
                      (TableList*) select_lex->table_list.first,
 
269
                      (TABLE_LIST*) select_lex->table_list.first,
261
270
                      select_lex->with_wild, select_lex->item_list,
262
271
                      select_lex->where,
263
272
                      select_lex->order_list.elements +
264
273
                      select_lex->group_list.elements,
265
 
                      (order_st*) select_lex->order_list.first,
266
 
                      (order_st*) select_lex->group_list.first,
 
274
                      (ORDER*) select_lex->order_list.first,
 
275
                      (ORDER*) select_lex->group_list.first,
267
276
                      select_lex->having,
268
 
                      (order_st*) lex->proc_list.first,
 
277
                      (ORDER*) lex->proc_list.first,
269
278
                      select_lex->options | thd->options |
270
279
                      setup_tables_done_option,
271
280
                      result, unit, select_lex);
274
283
  if (unlikely(res))
275
284
    result->abort();
276
285
 
277
 
  DRIZZLE_SELECT_END();
 
286
  MYSQL_SELECT_END();
278
287
  return(res);
279
288
}
280
289
 
394
403
  Function to setup clauses without sum functions.
395
404
*/
396
405
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
397
 
                               TableList *tables,
398
 
                               TableList *leaves,
 
406
                               TABLE_LIST *tables,
 
407
                               TABLE_LIST *leaves,
399
408
                               List<Item> &fields,
400
409
                               List<Item> &all_fields,
401
410
                               COND **conds,
402
 
                               order_st *order,
403
 
                               order_st *group, bool *hidden_group_fields)
 
411
                               ORDER *order,
 
412
                               ORDER *group, bool *hidden_group_fields)
404
413
{
405
414
  int res;
406
415
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
437
446
*/
438
447
int
439
448
JOIN::prepare(Item ***rref_pointer_array,
440
 
              TableList *tables_init,
441
 
              uint32_t wild_num, COND *conds_init, uint32_t og_num,
442
 
              order_st *order_init, order_st *group_init,
 
449
              TABLE_LIST *tables_init,
 
450
              uint wild_num, COND *conds_init, uint og_num,
 
451
              ORDER *order_init, ORDER *group_init,
443
452
              Item *having_init,
444
 
              order_st *proc_param_init, SELECT_LEX *select_lex_arg,
 
453
              ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
445
454
              SELECT_LEX_UNIT *unit_arg)
446
455
{
447
456
  // to prevent double initialization on EXPLAIN
475
484
                                    false))
476
485
      return(-1);
477
486
 
478
 
  TableList *table_ptr;
 
487
  TABLE_LIST *table_ptr;
479
488
  for (table_ptr= select_lex->leaf_tables;
480
489
       table_ptr;
481
490
       table_ptr= table_ptr->next_leaf)
508
517
    thd->lex->allow_sum_func= save_allow_sum_func;
509
518
  }
510
519
 
 
520
  if (!thd->lex->view_prepare_mode)
511
521
  {
512
522
    Item_subselect *subselect;
513
523
    Item_in_subselect *in_subs= NULL;
528
538
        requirements are:
529
539
          1. Subquery predicate is an IN/=ANY subq predicate
530
540
          2. Subquery is a single SELECT (not a UNION)
531
 
          3. Subquery does not have GROUP BY or order_st BY
 
541
          3. Subquery does not have GROUP BY or ORDER BY
532
542
          4. Subquery does not use aggregate functions or HAVING
533
543
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
534
544
          6. No execution method was already chosen (by a prepared statement).
571
581
 
572
582
        /* Register the subquery for further processing */
573
583
        select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
574
 
        in_subs->expr_join_nest= (TableList*)thd->thd_marker;
 
584
        in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
575
585
      }
576
586
      else
577
587
      {
594
604
             (Subquery is non-correlated ||
595
605
              Subquery is correlated to any query outer to IN predicate ||
596
606
              (Subquery is correlated to the immediate outer query &&
597
 
               Subquery !contains {GROUP BY, order_st BY [LIMIT],
 
607
               Subquery !contains {GROUP BY, ORDER BY [LIMIT],
598
608
               aggregate functions) && subquery predicate is not under "NOT IN"))
599
609
          6. No execution method was already chosen (by a prepared statement).
600
610
 
622
632
        if ((trans_res= subselect->select_transformer(this)) !=
623
633
            Item_subselect::RES_OK)
624
634
        {
 
635
          select_lex->fix_prepare_information(thd, &conds, &having);
625
636
          return((trans_res == Item_subselect::RES_ERROR));
626
637
        }
627
638
      }
628
639
    }
629
640
  }
630
641
 
 
642
  select_lex->fix_prepare_information(thd, &conds, &having);
 
643
 
631
644
  if (order)
632
645
  {
633
 
    order_st *ord;
 
646
    ORDER *ord;
634
647
    for (ord= order; ord; ord= ord->next)
635
648
    {
636
649
      Item *item= *ord->item;
673
686
  {
674
687
    /* Caclulate the number of groups */
675
688
    send_group_parts= 0;
676
 
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
689
    for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
677
690
      send_group_parts++;
678
691
  }
679
692
  
779
792
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
780
793
  if (where)
781
794
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
782
 
  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
 
795
  for (uint i = 0; i < join_tab->ref.key_parts; i++)
783
796
  {
784
797
    if (join_tab->ref.cond_guards[i])
785
798
    {
825
838
  /* Check if this table is functionally dependent on the tables that
826
839
     are within the same outer join nest
827
840
  */
828
 
  TableList *embedding= join_tab->table->pos_in_table_list->embedding;
 
841
  TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
829
842
  if (join_tab->type == JT_EQ_REF)
830
843
  {
831
844
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
832
 
    uint32_t idx;
 
845
    uint idx;
833
846
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
834
847
    {
835
848
      JOIN_TAB *ref_tab= join->join_tab + idx;
844
857
}
845
858
 
846
859
 
 
860
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg,
 
861
                                          SJ_TMP_TABLE *sjtbl);
 
862
 
 
863
 
847
864
/*
848
865
  Setup the strategies to eliminate semi-join duplicates.
849
866
  
941
958
*/
942
959
 
943
960
static
944
 
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
961
int setup_semijoin_dups_elimination(JOIN *join, ulonglong options, uint no_jbuf_after)
945
962
{
946
963
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
947
964
  struct {
951
968
      2 - Temptable (maybe confluent),
952
969
      3 - Temptable with join buffering
953
970
    */
954
 
    uint32_t strategy;
955
 
    uint32_t start_idx; /* Left range bound */
956
 
    uint32_t end_idx;   /* Right range bound */
 
971
    uint strategy;
 
972
    uint start_idx; /* Left range bound */
 
973
    uint end_idx;   /* Right range bound */
957
974
    /* 
958
975
      For Temptable strategy: Bitmap of all outer and correlated tables from 
959
976
      all involved join nests.
961
978
    table_map outer_tables;
962
979
  } dups_ranges [MAX_TABLES];
963
980
 
964
 
  TableList *emb_insideout_nest= NULL;
 
981
  TABLE_LIST *emb_insideout_nest= NULL;
965
982
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
966
983
                               tables) whose ranges we're in */
967
984
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
968
985
  table_map range_start_map= 0; /* table_map at current range start */
969
986
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
970
987
  int cur_range= 0;
971
 
  uint32_t i;
 
988
  uint i;
972
989
 
973
990
  /*
974
991
    First pass: locate the duplicate-generating ranges and pick the strategies.
976
993
  for (i=join->const_tables ; i < join->tables ; i++)
977
994
  {
978
995
    JOIN_TAB *tab=join->join_tab+i;
979
 
    Table *table=tab->table;
 
996
    TABLE *table=tab->table;
980
997
    cur_map |= table->map;
981
998
 
982
999
    if (tab->emb_sj_nest) // Encountered an sj-inner table
1109
1126
    {
1110
1127
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
1111
1128
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
1112
 
      uint32_t jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
1113
 
      uint32_t jt_null_bits= 0;    // # null bits in tuple bytes
 
1129
      uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
1130
      uint jt_null_bits= 0;    // # null bits in tuple bytes
1114
1131
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
1115
 
      uint32_t rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1132
      uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
1116
1133
      JOIN_TAB *last_outer_tab= tab - 1;
1117
1134
      /*
1118
1135
        Walk through the range and remember
1144
1161
      if (jt_rowid_offset) /* Temptable has at least one rowid */
1145
1162
      {
1146
1163
        SJ_TMP_TABLE *sjtbl;
1147
 
        uint32_t tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1164
        uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1148
1165
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
1149
1166
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
1150
1167
          return(true);
1191
1208
  {
1192
1209
    if (sj_tbl->tmp_table)
1193
1210
    {
1194
 
      sj_tbl->tmp_table->free_tmp_table(join->thd);
 
1211
      free_tmp_table(join->thd, sj_tbl->tmp_table);
1195
1212
    }
1196
1213
  }
1197
1214
  join->sj_tmp_tables= NULL;
1198
1215
}
1199
1216
 
1200
 
uint32_t make_join_orderinfo(JOIN *join);
 
1217
uint make_join_orderinfo(JOIN *join);
1201
1218
 
1202
1219
/**
1203
1220
  global select optimisation.
1253
1270
    }
1254
1271
  }
1255
1272
#endif
1256
 
 
1257
 
  /* Convert all outer joins to inner joins if possible */
1258
 
  conds= simplify_joins(this, join_list, conds, true, false);
1259
 
  build_bitmap_for_nested_joins(join_list, 0);
 
1273
  SELECT_LEX *sel= thd->lex->current_select;
 
1274
  if (sel->first_cond_optimization)
 
1275
  {
 
1276
    /*
 
1277
      The following code will allocate the new items in a permanent
 
1278
      MEMROOT for prepared statements and stored procedures.
 
1279
    */
 
1280
    sel->first_cond_optimization= 0;
 
1281
 
 
1282
    /* Convert all outer joins to inner joins if possible */
 
1283
    conds= simplify_joins(this, join_list, conds, true, false);
 
1284
    build_bitmap_for_nested_joins(join_list, 0);
 
1285
 
 
1286
    sel->prep_where= conds ? conds->copy_andor_structure(thd) : 0;
 
1287
  }
1260
1288
 
1261
1289
  conds= optimize_cond(this, conds, join_list, &cond_value);   
1262
1290
  if (thd->is_error())
1287
1315
    }
1288
1316
  }
1289
1317
 
1290
 
  /* Optimize count(*), cmin() and cmax() */
 
1318
  /* Optimize count(*), min() and max() */
1291
1319
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
1292
1320
  {
1293
1321
    int res;
1383
1411
  if (!conds && outer_join)
1384
1412
  {
1385
1413
    /* Handle the case where we have an OUTER JOIN without a WHERE */
1386
 
    conds=new Item_int((int64_t) 1,1);  // Always true
 
1414
    conds=new Item_int((longlong) 1,1); // Always true
1387
1415
  }
1388
1416
  select= make_select(*table, const_table_map,
1389
1417
                      const_table_map, conds, 1, &error);
1427
1455
      (select_options & SELECT_DESCRIBE) &&
1428
1456
      select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
1429
1457
  {
1430
 
    conds=new Item_int((int64_t) 0,1);  // Always false
 
1458
    conds=new Item_int((longlong) 0,1); // Always false
1431
1459
  }
1432
1460
  if (make_join_select(this, select, conds))
1433
1461
  {
1440
1468
 
1441
1469
  /* Optimize distinct away if possible */
1442
1470
  {
1443
 
    order_st *org_order= order;
 
1471
    ORDER *org_order= order;
1444
1472
    order=remove_const(this, order,conds,1, &simple_order);
1445
1473
    if (thd->is_error())
1446
1474
    {
1449
1477
    }
1450
1478
 
1451
1479
    /*
1452
 
      If we are using order_st BY NULL or order_st BY const_expression,
 
1480
      If we are using ORDER BY NULL or ORDER BY const_expression,
1453
1481
      return result in any order (even if we are using a GROUP BY)
1454
1482
    */
1455
1483
    if (!order && org_order)
1483
1511
        We have found that grouping can be removed since groups correspond to
1484
1512
        only one row anyway, but we still have to guarantee correct result
1485
1513
        order. The line below effectively rewrites the query from GROUP BY
1486
 
        <fields> to order_st BY <fields>. There are two exceptions:
 
1514
        <fields> to ORDER BY <fields>. There are two exceptions:
1487
1515
        - if skip_sort_order is set (see above), then we can simply skip
1488
1516
          GROUP BY;
1489
 
        - we can only rewrite order_st BY if the order_st BY fields are 'compatible'
 
1517
        - we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
1490
1518
          with the GROUP BY ones, i.e. either one is a prefix of another.
1491
 
          We only check if the order_st BY is a prefix of GROUP BY. In this case
 
1519
          We only check if the ORDER BY is a prefix of GROUP BY. In this case
1492
1520
          test_if_subpart() copies the ASC/DESC attributes from the original
1493
 
          order_st BY fields.
1494
 
          If GROUP BY is a prefix of order_st BY, then it is safe to leave
 
1521
          ORDER BY fields.
 
1522
          If GROUP BY is a prefix of ORDER BY, then it is safe to leave
1495
1523
          'order' as is.
1496
1524
       */
1497
1525
      if (!order || test_if_subpart(group_list, order))
1498
1526
          order= skip_sort_order ? 0 : group_list;
1499
1527
      /*
1500
1528
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be 
1501
 
        rewritten to IGNORE INDEX FOR order_st BY(fields).
 
1529
        rewritten to IGNORE INDEX FOR ORDER BY(fields).
1502
1530
      */
1503
1531
      join_tab->table->keys_in_use_for_order_by=
1504
1532
        join_tab->table->keys_in_use_for_group_by;
1523
1551
    /*
1524
1552
      We are only using one table. In this case we change DISTINCT to a
1525
1553
      GROUP BY query if:
1526
 
      - The GROUP BY can be done through indexes (no sort) and the order_st
 
1554
      - The GROUP BY can be done through indexes (no sort) and the ORDER
1527
1555
        BY only uses selected fields.
1528
 
        (In this case we can later optimize away GROUP BY and order_st BY)
 
1556
        (In this case we can later optimize away GROUP BY and ORDER BY)
1529
1557
      - We are scanning the whole table without LIMIT
1530
1558
        This can happen if:
1531
1559
        - We are using CALC_FOUND_ROWS
1532
 
        - We are using an order_st BY that can't be optimized away.
 
1560
        - We are using an ORDER BY that can't be optimized away.
1533
1561
 
1534
1562
      We don't want to use this optimization when we are using LIMIT
1535
1563
      because in this case we can just create a temporary table that
1561
1589
          {
1562
1590
            /*
1563
1591
              Force MySQL to read the table in sorted order to get result in
1564
 
              order_st BY order.
 
1592
              ORDER BY order.
1565
1593
            */
1566
1594
            tmp_table_param.quick_group=0;
1567
1595
          }
1577
1605
  }
1578
1606
  simple_group= 0;
1579
1607
  {
1580
 
    order_st *old_group_list;
 
1608
    ORDER *old_group_list;
1581
1609
    group_list= remove_const(this, (old_group_list= group_list), conds,
1582
1610
                             rollup.state == ROLLUP::STATE_NONE,
1583
1611
                             &simple_group);
1618
1646
    This has to be done if all tables are not already read (const tables)
1619
1647
    and one of the following conditions holds:
1620
1648
    - We are using DISTINCT (simple distinct's are already optimized away)
1621
 
    - We are using an order_st BY or GROUP BY on fields not in the first table
1622
 
    - We are using different order_st BY and GROUP BY orders
 
1649
    - We are using an ORDER BY or GROUP BY on fields not in the first table
 
1650
    - We are using different ORDER BY and GROUP BY orders
1623
1651
    - The user wants us to buffer the result.
1624
1652
  */
1625
1653
  need_tmp= (const_tables != tables &&
1627
1655
              (group_list && order) ||
1628
1656
              test(select_options & OPTION_BUFFER_RESULT)));
1629
1657
 
1630
 
  uint32_t no_jbuf_after= make_join_orderinfo(this);
1631
 
  uint64_t select_opts_for_readinfo= 
 
1658
  uint no_jbuf_after= make_join_orderinfo(this);
 
1659
  ulonglong select_opts_for_readinfo= 
1632
1660
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
1633
1661
 
1634
1662
  sj_tmp_tables= NULL;
1711
1739
  */
1712
1740
  if (need_tmp || select_distinct || group_list || order)
1713
1741
  {
1714
 
    for (uint32_t i = const_tables; i < tables; i++)
 
1742
    for (uint i = const_tables; i < tables; i++)
1715
1743
      join_tab[i].table->prepare_for_position();
1716
1744
  }
1717
1745
 
1751
1779
        Force using of tmp table if sorting by a SP or UDF function due to
1752
1780
        their expensive and probably non-deterministic nature.
1753
1781
      */
1754
 
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
1782
      for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1755
1783
      {
1756
1784
        Item *item= *tmp_order->item;
1757
1785
        if (item->is_expensive())
1795
1823
 
1796
1824
    tmp_table_param.hidden_field_count= (all_fields.elements -
1797
1825
                                         fields_list.elements);
1798
 
    order_st *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1799
 
                                                             (order_st*) 0);
 
1826
    ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1827
                                                             (ORDER*) 0);
1800
1828
    /*
1801
1829
      Pushing LIMIT to the temporary table creation is not applicable
1802
 
      when there is order_st BY or GROUP BY or there is no GROUP BY, but
 
1830
      when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1803
1831
      there are aggregate functions, because in all these cases we need
1804
1832
      all result rows.
1805
1833
    */
1924
1952
{
1925
1953
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
1926
1954
                                    select_lex->offset_limit->val_uint() :
1927
 
                                    0UL);
 
1955
                                    0ULL);
1928
1956
 
1929
1957
  first_record= 0;
1930
1958
 
1989
2017
{
1990
2018
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
1991
2019
  {
1992
 
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((unsigned char*) join_tab,
 
2020
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
1993
2021
                                                sizeof(JOIN_TAB) * tables)))
1994
2022
      return 1;
1995
2023
  }
2082
2110
  if (select_options & SELECT_DESCRIBE)
2083
2111
  {
2084
2112
    /*
2085
 
      Check if we managed to optimize order_st BY away and don't use temporary
2086
 
      table to resolve order_st BY: in that case, we only may need to do
 
2113
      Check if we managed to optimize ORDER BY away and don't use temporary
 
2114
      table to resolve ORDER BY: in that case, we only may need to do
2087
2115
      filesort for GROUP BY.
2088
2116
    */
2089
2117
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
2109
2137
    select_describe(this, need_tmp,
2110
2138
                    order != 0 && !skip_sort_order,
2111
2139
                    select_distinct,
2112
 
                    !tables ? "No tables used" : NULL);
 
2140
                    !tables ? "No tables used" : NullS);
2113
2141
    return;
2114
2142
  }
2115
2143
 
2116
2144
  JOIN *curr_join= this;
2117
2145
  List<Item> *curr_all_fields= &all_fields;
2118
2146
  List<Item> *curr_fields_list= &fields_list;
2119
 
  Table *curr_tmp_table= 0;
 
2147
  TABLE *curr_tmp_table= 0;
2120
2148
  /*
2121
2149
    Initialize examined rows here because the values from all join parts
2122
2150
    must be accumulated in examined_row_count. Hence every join
2244
2272
              exec_tmp_table2= create_tmp_table(thd,
2245
2273
                                                &curr_join->tmp_table_param,
2246
2274
                                                *curr_all_fields,
2247
 
                                                (order_st*) 0,
 
2275
                                                (ORDER*) 0,
2248
2276
                                                curr_join->select_distinct && 
2249
2277
                                                !curr_join->group_list,
2250
2278
                                                1, curr_join->select_options,
2455
2483
        return;
2456
2484
      }
2457
2485
      /*
2458
 
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
 
2486
        Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
2459
2487
        chose FILESORT to be faster than INDEX SCAN or there is no 
2460
2488
        suitable index present.
2461
2489
        Note, that create_sort_index calls test_if_skip_sort_order and may
2546
2574
 
2547
2575
  cleanup(1);
2548
2576
  if (exec_tmp_table1)
2549
 
    exec_tmp_table1->free_tmp_table(thd);
 
2577
    free_tmp_table(thd, exec_tmp_table1);
2550
2578
  if (exec_tmp_table2)
2551
 
    exec_tmp_table2->free_tmp_table(thd);
 
2579
    free_tmp_table(thd, exec_tmp_table2);
2552
2580
  delete select;
2553
2581
  delete_dynamic(&keyuse);
2554
2582
  return(error);
2575
2603
                              for a, b and c in this list.
2576
2604
  @param conds                top level item of an expression representing
2577
2605
                              WHERE clause of the top level select
2578
 
  @param og_num               total number of order_st BY and GROUP BY clauses
 
2606
  @param og_num               total number of ORDER BY and GROUP BY clauses
2579
2607
                              arguments
2580
 
  @param order                linked list of order_st BY agruments
 
2608
  @param order                linked list of ORDER BY agruments
2581
2609
  @param group                linked list of GROUP BY arguments
2582
2610
  @param having               top level item of HAVING expression
2583
2611
  @param proc_param           list of PROCEDUREs
2602
2630
 
2603
2631
bool
2604
2632
mysql_select(THD *thd, Item ***rref_pointer_array,
2605
 
             TableList *tables, uint32_t wild_num, List<Item> &fields,
2606
 
             COND *conds, uint32_t og_num,  order_st *order, order_st *group,
2607
 
             Item *having, order_st *proc_param, uint64_t select_options,
 
2633
             TABLE_LIST *tables, uint wild_num, List<Item> &fields,
 
2634
             COND *conds, uint og_num,  ORDER *order, ORDER *group,
 
2635
             Item *having, ORDER *proc_param, ulonglong select_options,
2608
2636
             select_result *result, SELECT_LEX_UNIT *unit,
2609
2637
             SELECT_LEX *select_lex)
2610
2638
{
2658
2686
    }
2659
2687
  }
2660
2688
 
2661
 
  /* dump_TableList_graph(select_lex, select_lex->leaf_tables); */
 
2689
  /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
2662
2690
  if (join->flatten_subqueries())
2663
2691
  {
2664
2692
    err= 1;
2665
2693
    goto err;
2666
2694
  }
2667
 
  /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
 
2695
  /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
2668
2696
 
2669
2697
  if ((err= join->optimize()))
2670
2698
  {
2713
2741
}
2714
2742
 
2715
2743
 
2716
 
static TableList *alloc_join_nest(THD *thd)
 
2744
static TABLE_LIST *alloc_join_nest(THD *thd)
2717
2745
{
2718
 
  TableList *tbl;
2719
 
  if (!(tbl= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
2720
 
                                       sizeof(nested_join_st))))
 
2746
  TABLE_LIST *tbl;
 
2747
  if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
 
2748
                                       sizeof(NESTED_JOIN))))
2721
2749
    return NULL;
2722
 
  tbl->nested_join= (nested_join_st*) ((unsigned char*)tbl + 
2723
 
                                    ALIGN_SIZE(sizeof(TableList)));
 
2750
  tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl + 
 
2751
                                    ALIGN_SIZE(sizeof(TABLE_LIST)));
2724
2752
  return tbl;
2725
2753
}
2726
2754
 
2727
2755
 
2728
 
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TableList> *tlist)
 
2756
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
2729
2757
{
2730
 
  List_iterator<TableList> it(*tlist);
2731
 
  TableList *table;
 
2758
  List_iterator<TABLE_LIST> it(*tlist);
 
2759
  TABLE_LIST *table;
2732
2760
  while ((table= it++))
2733
2761
  {
2734
2762
    if (table->on_expr)
2740
2768
 
2741
2769
 
2742
2770
/*
2743
 
  Convert a subquery predicate into a TableList semi-join nest
 
2771
  Convert a subquery predicate into a TABLE_LIST semi-join nest
2744
2772
 
2745
2773
  SYNOPSIS
2746
2774
    convert_subq_to_sj()
2749
2777
       subq_pred    Subquery predicate to be converted
2750
2778
  
2751
2779
  DESCRIPTION
2752
 
    Convert a subquery predicate into a TableList semi-join nest. All the 
 
2780
    Convert a subquery predicate into a TABLE_LIST semi-join nest. All the 
2753
2781
    prerequisites are already checked, so the conversion is always successfull.
2754
2782
 
2755
2783
    Prepared Statements: the transformation is permanent:
2756
 
     - Changes in TableList structures are naturally permanent
 
2784
     - Changes in TABLE_LIST structures are naturally permanent
2757
2785
     - Item tree changes are performed on statement MEM_ROOT:
2758
2786
        = we activate statement MEM_ROOT 
2759
2787
        = this function is called before the first fix_prepare_information
2770
2798
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
2771
2799
{
2772
2800
  SELECT_LEX *parent_lex= parent_join->select_lex;
2773
 
  TableList *emb_tbl_nest= NULL;
2774
 
  List<TableList> *emb_join_list= &parent_lex->top_join_list;
 
2801
  TABLE_LIST *emb_tbl_nest= NULL;
 
2802
  List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
2775
2803
  THD *thd= parent_join->thd;
2776
2804
 
2777
2805
  /*
2808
2836
    }
2809
2837
    else if (!subq_pred->expr_join_nest->nested_join)
2810
2838
    {
2811
 
      TableList *outer_tbl= subq_pred->expr_join_nest;      
2812
 
      TableList *wrap_nest;
 
2839
      TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;      
 
2840
      TABLE_LIST *wrap_nest;
2813
2841
      /*
2814
2842
        We're dealing with
2815
2843
 
2824
2852
        Q:  other subqueries may be pointing to this element. What to do?
2825
2853
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
2826
2854
            But we'll need to fix other pointers.
2827
 
        A2: Another way: have TableList::next_ptr so the following
 
2855
        A2: Another way: have TABLE_LIST::next_ptr so the following
2828
2856
            subqueries know the table has been nested.
2829
 
        A3: changes in the TableList::outer_join will make everything work
 
2857
        A3: changes in the TABLE_LIST::outer_join will make everything work
2830
2858
            automatically.
2831
2859
      */
2832
2860
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
2853
2881
      wrap_nest->on_expr= outer_tbl->on_expr;
2854
2882
      outer_tbl->on_expr= NULL;
2855
2883
 
2856
 
      List_iterator<TableList> li(*wrap_nest->join_list);
2857
 
      TableList *tbl;
 
2884
      List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
 
2885
      TABLE_LIST *tbl;
2858
2886
      while ((tbl= li++))
2859
2887
      {
2860
2888
        if (tbl == outer_tbl)
2872
2900
    }
2873
2901
  }
2874
2902
 
2875
 
  TableList *sj_nest;
2876
 
  nested_join_st *nested_join;
 
2903
  TABLE_LIST *sj_nest;
 
2904
  NESTED_JOIN *nested_join;
2877
2905
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
2878
2906
  {
2879
2907
    return(true);
2898
2926
  */
2899
2927
  st_select_lex *subq_lex= subq_pred->unit->first_select();
2900
2928
  nested_join->join_list.empty();
2901
 
  List_iterator_fast<TableList> li(subq_lex->top_join_list);
2902
 
  TableList *tl, *last_leaf;
 
2929
  List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
 
2930
  TABLE_LIST *tl, *last_leaf;
2903
2931
  while ((tl= li++))
2904
2932
  {
2905
2933
    tl->embedding= sj_nest;
2935
2963
  /*TODO: also reset the 'with_subselect' there. */
2936
2964
 
2937
2965
  /* n. Adjust the parent_join->tables counter */
2938
 
  uint32_t table_no= parent_join->tables;
 
2966
  uint table_no= parent_join->tables;
2939
2967
  /* n. Walk through child's tables and adjust table->map */
2940
2968
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
2941
2969
  {
2943
2971
    tl->table->map= ((table_map)1) << table_no;
2944
2972
    SELECT_LEX *old_sl= tl->select_lex;
2945
2973
    tl->select_lex= parent_join->select_lex; 
2946
 
    for(TableList *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
 
2974
    for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
2947
2975
      emb->select_lex= parent_join->select_lex;
2948
2976
  }
2949
2977
  parent_join->tables += subq_lex->join->tables;
2993
3021
  }
2994
3022
  else
2995
3023
  {
2996
 
    for (uint32_t i= 0; i < subq_pred->left_expr->cols(); i++)
 
3024
    for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
2997
3025
    {
2998
3026
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
2999
3027
                                                element_index(i));
3071
3099
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
3072
3100
  }
3073
3101
 
3074
 
  //dump_TableList_struct(select_lex, select_lex->leaf_tables);
 
3102
  //dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
3075
3103
  /* 
3076
3104
    2. Pick which subqueries to convert:
3077
3105
      sort the subquery array
3192
3220
    false - Otherwise
3193
3221
*/
3194
3222
 
3195
 
bool find_eq_ref_candidate(Table *table, table_map sj_inner_tables)
 
3223
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
3196
3224
{
3197
3225
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
3198
 
  uint32_t key;
 
3226
  uint key;
3199
3227
 
3200
3228
  if (keyuse)
3201
3229
  {
3262
3290
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
3263
3291
       tables)
3264
3292
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
3265
 
     * Semi-join nests TableList::sj_inner_tables
 
3293
     * Semi-join nests TABLE_LIST::sj_inner_tables
3266
3294
 
3267
3295
    This operation is (and should be) performed at each PS execution since
3268
3296
    tables may become/cease to be constant across PS reexecutions.
3274
3302
 
3275
3303
int pull_out_semijoin_tables(JOIN *join)
3276
3304
{
3277
 
  TableList *sj_nest;
3278
 
  List_iterator<TableList> sj_list_it(join->select_lex->sj_nests);
 
3305
  TABLE_LIST *sj_nest;
 
3306
  List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
3279
3307
   
3280
3308
  /* Try pulling out of the each of the semi-joins */
3281
3309
  while ((sj_nest= sj_list_it++))
3283
3311
    /* Action #1: Mark the constant tables to be pulled out */
3284
3312
    table_map pulled_tables= 0;
3285
3313
     
3286
 
    List_iterator<TableList> child_li(sj_nest->nested_join->join_list);
3287
 
    TableList *tbl;
 
3314
    List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
 
3315
    TABLE_LIST *tbl;
3288
3316
    while ((tbl= child_li++))
3289
3317
    {
3290
3318
      if (tbl->table)
3360
3388
 
3361
3389
 
3362
3390
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
3363
 
                                      Table *table,
 
3391
                                      TABLE *table,
3364
3392
                                      const key_map *keys,ha_rows limit)
3365
3393
{
3366
3394
  int error;
3393
3421
{
3394
3422
  Field *field;              /* field against which to check sargability */
3395
3423
  Item **arg_value;          /* values of potential keys for lookups     */
3396
 
  uint32_t num_values;           /* number of values in the above array      */
 
3424
  uint num_values;           /* number of values in the above array      */
3397
3425
} SARGABLE_PARAM;  
3398
3426
 
3399
3427
/**
3406
3434
*/
3407
3435
 
3408
3436
static bool
3409
 
make_join_statistics(JOIN *join, TableList *tables, COND *conds,
 
3437
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
3410
3438
                     DYNAMIC_ARRAY *keyuse_array)
3411
3439
{
3412
3440
  int error;
3413
 
  Table *table;
3414
 
  uint32_t i,table_count,const_count,key;
 
3441
  TABLE *table;
 
3442
  uint i,table_count,const_count,key;
3415
3443
  table_map found_const_table_map, all_table_map, found_ref, refs;
3416
3444
  key_map const_ref, eq_part;
3417
 
  Table **table_vector;
 
3445
  TABLE **table_vector;
3418
3446
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
3419
3447
  KEYUSE *keyuse,*start_keyuse;
3420
3448
  table_map outer_join=0;
3424
3452
  table_count=join->tables;
3425
3453
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
3426
3454
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3427
 
  table_vector=(Table**) join->thd->alloc(sizeof(Table*)*(table_count*2));
 
3455
  table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
3428
3456
  if (!stat || !stat_ref || !table_vector)
3429
3457
    return(1);                          // Eom /* purecov: inspected */
3430
3458
 
3438
3466
       tables;
3439
3467
       s++, tables= tables->next_leaf, i++)
3440
3468
  {
3441
 
    TableList *embedding= tables->embedding;
 
3469
    TABLE_LIST *embedding= tables->embedding;
3442
3470
    stat_vector[i]=s;
3443
3471
    s->keys.init();
3444
3472
    s->const_keys.init();
3455
3483
    table->quick_keys.clear_all();
3456
3484
    table->reginfo.join_tab=s;
3457
3485
    table->reginfo.not_exists_optimize=0;
3458
 
    memset(table->const_key_parts, 0,
3459
 
           sizeof(key_part_map)*table->s->keys);
 
3486
    bzero((char*) table->const_key_parts, sizeof(key_part_map)*table->s->keys);
3460
3487
    all_table_map|= table->map;
3461
3488
    s->join=join;
3462
3489
    s->info=0;                                  // For describe
3489
3516
      s->embedding_map= 0;
3490
3517
      do
3491
3518
      {
3492
 
        nested_join_st *nested_join= embedding->nested_join;
 
3519
        NESTED_JOIN *nested_join= embedding->nested_join;
3493
3520
        s->embedding_map|=nested_join->nj_map;
3494
3521
        s->dependent|= embedding->dep_tables;
3495
3522
        embedding= embedding->embedding;
3498
3525
      while (embedding);
3499
3526
      continue;
3500
3527
    }
3501
 
    if ((table->file->stats.records <= 1) &&
 
3528
    if ((table->s->system || table->file->stats.records <= 1) &&
3502
3529
        !s->dependent &&
3503
 
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
 
3530
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3531
        !table->fulltext_searched && !join->no_const_tables)
3504
3532
    {
3505
3533
      set_position(join,const_count++,s,(KEYUSE*) 0);
3506
3534
    }
3520
3548
    */
3521
3549
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3522
3550
    {
3523
 
      for (uint32_t j= 0 ; j < table_count ; j++)
 
3551
      for (uint j= 0 ; j < table_count ; j++)
3524
3552
      {
3525
3553
        table= stat[j].table;
3526
3554
        if (s->dependent & table->map)
3668
3696
          } while (keyuse->table == table && keyuse->key == key);
3669
3697
 
3670
3698
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3699
              !table->fulltext_searched && 
3671
3700
              !table->pos_in_table_list->embedding)
3672
3701
          {
3673
3702
            if ((table->key_info[key].flags & (HA_NOSAME))
3717
3746
      key_map possible_keys= field->key_start;
3718
3747
      possible_keys.intersect(field->table->keys_in_use_for_query);
3719
3748
      bool is_const= 1;
3720
 
      for (uint32_t j=0; j < sargables->num_values; j++)
 
3749
      for (uint j=0; j < sargables->num_values; j++)
3721
3750
        is_const&= sargables->arg_value[j]->const_item();
3722
3751
      if (is_const)
3723
3752
        join_tab[0].const_keys.merge(possible_keys);
3746
3775
      This is can't be to high as otherwise we are likely to use
3747
3776
      table scan.
3748
3777
    */
3749
 
    s->worst_seeks= cmin((double) s->found_records / 10,
 
3778
    s->worst_seeks= min((double) s->found_records / 10,
3750
3779
                        (double) s->read_time*3);
3751
3780
    if (s->worst_seeks < 2.0)                   // Fix for small tables
3752
3781
      s->worst_seeks=2.0;
3817
3846
  }
3818
3847
  else
3819
3848
  {
3820
 
    memcpy(join->best_positions, join->positions,
3821
 
           sizeof(POSITION)*join->const_tables);
 
3849
    memcpy((uchar*) join->best_positions,(uchar*) join->positions,
 
3850
           sizeof(POSITION)*join->const_tables);
3822
3851
    join->best_read=1.0;
3823
3852
  }
3824
3853
  /* Generate an execution plan from the found optimal join order. */
3847
3876
  */
3848
3877
  bool          null_rejecting; 
3849
3878
  bool          *cond_guard; /* See KEYUSE::cond_guard */
3850
 
  uint32_t          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3879
  uint          sj_pred_no; /* See KEYUSE::sj_pred_no */
3851
3880
} KEY_FIELD;
3852
3881
 
3853
3882
/**
3876
3905
 
3877
3906
static KEY_FIELD *
3878
3907
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
3879
 
                 uint32_t and_level)
 
3908
                 uint and_level)
3880
3909
{
3881
3910
  if (start == new_fields)
3882
3911
    return start;                               // Impossible or
3899
3928
          The cause is as follows: Some of the tables are already known to be
3900
3929
          const tables (the detection code is in make_join_statistics(),
3901
3930
          above the update_ref_and_keys() call), but we didn't propagate 
3902
 
          information about this: Table::const_table is not set to true, and
 
3931
          information about this: TABLE::const_table is not set to true, and
3903
3932
          Item::update_used_tables() hasn't been called for each item.
3904
3933
          The result of this is that we're missing some 'ref' accesses.
3905
3934
          TODO: OptimizerTeam: Fix this
4003
4032
*/
4004
4033
 
4005
4034
static void
4006
 
add_key_field(KEY_FIELD **key_fields,uint32_t and_level, Item_func *cond,
4007
 
              Field *field, bool eq_func, Item **value, uint32_t num_values,
 
4035
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
 
4036
              Field *field, bool eq_func, Item **value, uint num_values,
4008
4037
              table_map usable_tables, SARGABLE_PARAM **sargables)
4009
4038
{
4010
 
  uint32_t exists_optimize= 0;
 
4039
  uint exists_optimize= 0;
4011
4040
  if (!(field->flags & PART_KEY_FLAG))
4012
4041
  {
4013
4042
    // Don't remove column IS NULL on a LEFT JOIN table
4021
4050
  {
4022
4051
    table_map used_tables=0;
4023
4052
    bool optimizable=0;
4024
 
    for (uint32_t i=0; i<num_values; i++)
 
4053
    for (uint i=0; i<num_values; i++)
4025
4054
    {
4026
4055
      used_tables|=(value[i])->used_tables();
4027
4056
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
4057
4086
      stat[0].key_dependent|=used_tables;
4058
4087
 
4059
4088
      bool is_const=1;
4060
 
      for (uint32_t i=0; i<num_values; i++)
 
4089
      for (uint i=0; i<num_values; i++)
4061
4090
      {
4062
4091
        if (!(is_const&= value[i]->const_item()))
4063
4092
          break;
4175
4204
*/
4176
4205
 
4177
4206
static void
4178
 
add_key_equal_fields(KEY_FIELD **key_fields, uint32_t and_level,
 
4207
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
4179
4208
                     Item_func *cond, Item_field *field_item,
4180
4209
                     bool eq_func, Item **val,
4181
 
                     uint32_t num_values, table_map usable_tables,
 
4210
                     uint num_values, table_map usable_tables,
4182
4211
                     SARGABLE_PARAM **sargables)
4183
4212
{
4184
4213
  Field *field= field_item->field;
4206
4235
}
4207
4236
 
4208
4237
static void
4209
 
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint32_t *and_level,
 
4238
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
4210
4239
               COND *cond, table_map usable_tables,
4211
4240
               SARGABLE_PARAM **sargables)
4212
4241
{
4299
4328
    if (cond_func->functype() == Item_func::BETWEEN)
4300
4329
    {
4301
4330
      values= cond_func->arguments();
4302
 
      for (uint32_t i= 1 ; i < cond_func->argument_count() ; i++)
 
4331
      for (uint i= 1 ; i < cond_func->argument_count() ; i++)
4303
4332
      {
4304
4333
        Item_field *field_item;
4305
4334
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
4410
4439
static uint
4411
4440
max_part_bit(key_part_map bits)
4412
4441
{
4413
 
  uint32_t found;
 
4442
  uint found;
4414
4443
  for (found=0; bits & 1 ; found++,bits>>=1) ;
4415
4444
  return found;
4416
4445
}
4419
4448
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
4420
4449
{
4421
4450
  Field *field=key_field->field;
4422
 
  Table *form= field->table;
 
4451
  TABLE *form= field->table;
4423
4452
  KEYUSE keyuse;
4424
4453
 
4425
4454
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
4426
4455
  {
4427
 
    for (uint32_t key= 0 ; key < form->sizeKeys() ; key++)
 
4456
    for (uint key=0 ; key < form->s->keys ; key++)
4428
4457
    {
4429
4458
      if (!(form->keys_in_use_for_query.is_set(key)))
4430
4459
        continue;
4431
4460
 
4432
 
      uint32_t key_parts= (uint) form->key_info[key].key_parts;
4433
 
      for (uint32_t part=0 ; part <  key_parts ; part++)
 
4461
      uint key_parts= (uint) form->key_info[key].key_parts;
 
4462
      for (uint part=0 ; part <  key_parts ; part++)
4434
4463
      {
4435
4464
        if (field->eq(form->key_info[key].key_part[part].field))
4436
4465
        {
4444
4473
          keyuse.null_rejecting= key_field->null_rejecting;
4445
4474
          keyuse.cond_guard= key_field->cond_guard;
4446
4475
          keyuse.sj_pred_no= key_field->sj_pred_no;
4447
 
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
 
4476
          VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
4448
4477
        }
4449
4478
      }
4450
4479
    }
4506
4535
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
4507
4536
*/
4508
4537
 
4509
 
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
4510
 
                                  KEY_FIELD **end, uint32_t *and_level,
 
4538
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
 
4539
                                  KEY_FIELD **end, uint *and_level,
4511
4540
                                  SARGABLE_PARAM **sargables)
4512
4541
{
4513
 
  List_iterator<TableList> li(nested_join_table->nested_join->join_list);
4514
 
  List_iterator<TableList> li2(nested_join_table->nested_join->join_list);
 
4542
  List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
 
4543
  List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
4515
4544
  bool have_another = false;
4516
4545
  table_map tables= 0;
4517
 
  TableList *table;
 
4546
  TABLE_LIST *table;
4518
4547
  assert(nested_join_table->nested_join);
4519
4548
 
4520
4549
  while ((table= li++) || (have_another && (li=li2, have_another=false,
4527
4556
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
4528
4557
        have_another= true;
4529
4558
        li2= li;
4530
 
        li= List_iterator<TableList>(table->nested_join->join_list); 
 
4559
        li= List_iterator<TABLE_LIST>(table->nested_join->join_list); 
4531
4560
      }
4532
4561
      else
4533
4562
        add_key_fields_for_nj(join, table, end, and_level, sargables);
4565
4594
 
4566
4595
static bool
4567
4596
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
4568
 
                    uint32_t tables, COND *cond,
4569
 
                    COND_EQUAL *cond_equal __attribute__((unused)),
 
4597
                    uint tables, COND *cond,
 
4598
                    COND_EQUAL *cond_equal __attribute__((__unused__)),
4570
4599
                    table_map normal_tables, SELECT_LEX *select_lex,
4571
4600
                    SARGABLE_PARAM **sargables)
4572
4601
{
4573
4602
  uint  and_level,i,found_eq_constant;
4574
4603
  KEY_FIELD *key_fields, *end, *field;
4575
 
  uint32_t sz;
4576
 
  uint32_t m= cmax(select_lex->max_equal_elems,(uint32_t)1);
 
4604
  uint sz;
 
4605
  uint m= max(select_lex->max_equal_elems,1);
4577
4606
  
4578
4607
  /* 
4579
4608
    We use the same piece of memory to store both  KEY_FIELD 
4596
4625
    can be not more than select_lex->max_equal_elems such 
4597
4626
    substitutions.
4598
4627
  */ 
4599
 
  sz= cmax(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4628
  sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
4600
4629
      (((thd->lex->current_select->cond_count+1)*2 +
4601
4630
        thd->lex->current_select->between_count)*m+1);
4602
4631
  if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
4642
4671
 
4643
4672
  /* Process ON conditions for the nested joins */
4644
4673
  {
4645
 
    List_iterator<TableList> li(*join_tab->join->join_list);
4646
 
    TableList *table;
 
4674
    List_iterator<TABLE_LIST> li(*join_tab->join->join_list);
 
4675
    TABLE_LIST *table;
4647
4676
    while ((table= li++))
4648
4677
    {
4649
4678
      if (table->nested_join)
4664
4693
    - keyparts without previous keyparts
4665
4694
      (e.g. if there is a key(a,b,c) but only b < 5 (or a=2 and c < 3) is
4666
4695
      used in the query, we drop the partial key parts from consideration).
 
4696
    Special treatment for ft-keys.
4667
4697
  */
4668
4698
  if (keyuse->elements)
4669
4699
  {
4672
4702
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
4673
4703
          (qsort_cmp) sort_keyuse);
4674
4704
 
4675
 
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
4676
 
    insert_dynamic(keyuse,(unsigned char*) &key_end);
 
4705
    bzero((char*) &key_end,sizeof(key_end));    /* Add for easy testing */
 
4706
    VOID(insert_dynamic(keyuse,(uchar*) &key_end));
4677
4707
 
4678
4708
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
4679
4709
    prev= &key_end;
4706
4736
      save_pos++;
4707
4737
    }
4708
4738
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
4709
 
    set_dynamic(keyuse,(unsigned char*) &key_end,i);
 
4739
    VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
4710
4740
    keyuse->elements=i;
4711
4741
  }
4712
4742
  return false;
4736
4766
        (map= (keyuse->used_tables & ~join->const_table_map &
4737
4767
               ~OUTER_REF_TABLE_BIT)))
4738
4768
    {
4739
 
      uint32_t tablenr;
 
4769
      uint tablenr;
4740
4770
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
4741
4771
      if (map == 1)                     // Only one table
4742
4772
      {
4743
 
        Table *tmp_table=join->all_tables[tablenr];
4744
 
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
 
4773
        TABLE *tmp_table=join->all_tables[tablenr];
 
4774
        keyuse->ref_table_rows= max(tmp_table->file->stats.records, 100);
4745
4775
      }
4746
4776
    }
4747
4777
    /*
4777
4807
{
4778
4808
  List<Item_field> indexed_fields;
4779
4809
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
4780
 
  order_st      *cur_group;
 
4810
  ORDER      *cur_group;
4781
4811
  Item_field *cur_item;
4782
4812
  key_map possible_keys(0);
4783
4813
 
4785
4815
  { /* Collect all query fields referenced in the GROUP clause. */
4786
4816
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
4787
4817
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
4788
 
                               (unsigned char*) &indexed_fields);
 
4818
                               (uchar*) &indexed_fields);
4789
4819
  }
4790
4820
  else if (join->select_distinct)
4791
4821
  { /* Collect all query fields referenced in the SELECT clause. */
4794
4824
    Item *item;
4795
4825
    while ((item= select_items_it++))
4796
4826
      item->walk(&Item::collect_item_field_processor, 0,
4797
 
                 (unsigned char*) &indexed_fields);
 
4827
                 (uchar*) &indexed_fields);
4798
4828
  }
4799
4829
  else
4800
4830
    return;
4823
4853
/** Save const tables first as used tables. */
4824
4854
 
4825
4855
static void
4826
 
set_position(JOIN *join,uint32_t idx,JOIN_TAB *table,KEYUSE *key)
 
4856
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
4827
4857
{
4828
4858
  join->positions[idx].table= table;
4829
4859
  join->positions[idx].key=key;
4860
4890
    Bitmap of bound IN-equalities.
4861
4891
*/
4862
4892
 
4863
 
uint64_t get_bound_sj_equalities(TableList *sj_nest, 
 
4893
ulonglong get_bound_sj_equalities(TABLE_LIST *sj_nest, 
4864
4894
                                  table_map remaining_tables)
4865
4895
{
4866
4896
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
4867
4897
  Item *item;
4868
 
  uint32_t i= 0;
4869
 
  uint64_t res= 0;
 
4898
  uint i= 0;
 
4899
  ulonglong res= 0;
4870
4900
  while ((item= li++))
4871
4901
  {
4872
4902
    /*
4877
4907
    */
4878
4908
    if (!(item->used_tables() & remaining_tables))
4879
4909
    {
4880
 
      res |= 1UL < i;
 
4910
      res |= 1ULL < i;
4881
4911
    }
4882
4912
  }
4883
4913
  return res;
4914
4944
                 JOIN_TAB  *s,
4915
4945
                 THD       *thd,
4916
4946
                 table_map remaining_tables,
4917
 
                 uint32_t      idx,
 
4947
                 uint      idx,
4918
4948
                 double    record_count,
4919
 
                 double    read_time __attribute__((unused)))
 
4949
                 double    read_time __attribute__((__unused__)))
4920
4950
{
4921
4951
  KEYUSE *best_key=         0;
4922
 
  uint32_t best_max_key_part=   0;
4923
 
  bool found_constraint= 0;
 
4952
  uint best_max_key_part=   0;
 
4953
  my_bool found_constraint= 0;
4924
4954
  double best=              DBL_MAX;
4925
4955
  double best_time=         DBL_MAX;
4926
4956
  double records=           DBL_MAX;
4927
4957
  table_map best_ref_depends_map= 0;
4928
4958
  double tmp;
4929
4959
  ha_rows rec;
4930
 
  uint32_t best_is_sj_inside_out=    0;
 
4960
  uint best_is_sj_inside_out=    0;
4931
4961
 
4932
4962
  if (s->keyuse)
4933
4963
  {                                            /* Use key if possible */
4934
 
    Table *table= s->table;
 
4964
    TABLE *table= s->table;
4935
4965
    KEYUSE *keyuse,*start_key=0;
4936
4966
    double best_records= DBL_MAX;
4937
 
    uint32_t max_key_part=0;
4938
 
    uint64_t bound_sj_equalities= 0;
 
4967
    uint max_key_part=0;
 
4968
    ulonglong bound_sj_equalities= 0;
4939
4969
    bool try_sj_inside_out= false;
4940
4970
    /*
4941
4971
      Discover the bound equalites. We need to do this, if
4965
4995
    {
4966
4996
      key_part_map found_part= 0;
4967
4997
      table_map found_ref= 0;
4968
 
      uint32_t key= keyuse->key;
 
4998
      uint key= keyuse->key;
4969
4999
      KEY *keyinfo= table->key_info+key;
4970
5000
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
4971
5001
      key_part_map const_part= 0;
4974
5004
 
4975
5005
      /* Calculate how many key segments of the current key we can use */
4976
5006
      start_key= keyuse;
4977
 
      uint64_t handled_sj_equalities=0;
 
5007
      ulonglong handled_sj_equalities=0;
4978
5008
      key_part_map sj_insideout_map= 0;
4979
5009
 
4980
5010
      do /* For each keypart */
4981
5011
      {
4982
 
        uint32_t keypart= keyuse->keypart;
 
5012
        uint keypart= keyuse->keypart;
4983
5013
        table_map best_part_found_ref= 0;
4984
5014
        double best_prev_record_reads= DBL_MAX;
4985
5015
        
5018
5048
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
5019
5049
          {
5020
5050
            if (!(remaining_tables & keyuse->used_tables))
5021
 
              bound_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5051
              bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5022
5052
            else
5023
5053
            {
5024
 
              handled_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5054
              handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5025
5055
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
5026
5056
            }
5027
5057
          }
5053
5083
        if (try_sj_inside_out && 
5054
5084
            table->covering_keys.is_set(key) &&
5055
5085
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
5056
 
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
 
5086
            PREV_BITS(ulonglong, s->emb_sj_nest->sj_in_exprs)) // (1)
5057
5087
        {
5058
 
          uint32_t n_fixed_parts= max_part_bit(found_part);
 
5088
          uint n_fixed_parts= max_part_bit(found_part);
5059
5089
          if (n_fixed_parts != keyinfo->key_parts &&
5060
5090
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
5061
5091
               PREV_BITS(uint, keyinfo->key_parts))
5096
5126
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
5097
5127
            !ref_or_null_part)
5098
5128
        {                                         /* use eq key */
5099
 
          max_key_part= UINT32_MAX;
 
5129
          max_key_part= (uint) ~0;
5100
5130
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
5101
5131
          {
5102
5132
            tmp = prev_record_reads(join, idx, found_ref);
5170
5200
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5171
5201
            }
5172
5202
            else
5173
 
              tmp= record_count*cmin(tmp,s->worst_seeks);
 
5203
              tmp= record_count*min(tmp,s->worst_seeks);
5174
5204
          }
5175
5205
        }
5176
5206
        else
5226
5256
            */
5227
5257
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
5228
5258
                table->quick_key_parts[key] == max_key_part &&          //(C2)
5229
 
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
 
5259
                table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
5230
5260
            {
5231
5261
              tmp= records= (double) table->quick_rows[key];
5232
5262
            }
5319
5349
              if (table->quick_keys.is_set(key) &&
5320
5350
                  table->quick_key_parts[key] <= max_key_part &&
5321
5351
                  const_part & (1 << table->quick_key_parts[key]) &&
5322
 
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
5323
 
                                                     const_part) ? 1 : 0) &&
 
5352
                  table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
 
5353
                                                         const_part) &&
5324
5354
                  records > (double) table->quick_rows[key])
5325
5355
              {
5326
5356
                tmp= records= (double) table->quick_rows[key];
5335
5365
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5336
5366
            }
5337
5367
            else
5338
 
              tmp= record_count * cmin(tmp,s->worst_seeks);
 
5368
              tmp= record_count * min(tmp,s->worst_seeks);
5339
5369
          }
5340
5370
          else
5341
5371
            tmp= best_time;                    // Do nothing
5473
5503
    /*
5474
5504
      We estimate the cost of evaluating WHERE clause for found records
5475
5505
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
5476
 
      tmp give us total cost of using Table SCAN
 
5506
      tmp give us total cost of using TABLE SCAN
5477
5507
    */
5478
5508
    if (best == DBL_MAX ||
5479
5509
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
5504
5534
      idx == join->const_tables &&
5505
5535
      s->table == join->sort_by_table &&
5506
5536
      join->unit->select_limit_cnt >= records)
5507
 
    join->sort_by_table= (Table*) 1;  // Must use temporary table
 
5537
    join->sort_by_table= (TABLE*) 1;  // Must use temporary table
5508
5538
 
5509
5539
  return;
5510
5540
}
5536
5566
static bool
5537
5567
choose_plan(JOIN *join, table_map join_tables)
5538
5568
{
5539
 
  uint32_t search_depth= join->thd->variables.optimizer_search_depth;
5540
 
  uint32_t prune_level=  join->thd->variables.optimizer_prune_level;
 
5569
  uint search_depth= join->thd->variables.optimizer_search_depth;
 
5570
  uint prune_level=  join->thd->variables.optimizer_prune_level;
5541
5571
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
5542
5572
 
5543
5573
  join->cur_embedding_map= 0;
5671
5701
 
5672
5702
  @todo
5673
5703
    this value should be determined dynamically, based on statistics:
5674
 
    uint32_t max_tables_for_exhaustive_opt= 7;
 
5704
    uint max_tables_for_exhaustive_opt= 7;
5675
5705
 
5676
5706
  @todo
5677
5707
    this value could be determined by some mapping of the form:
5686
5716
static uint
5687
5717
determine_search_depth(JOIN *join)
5688
5718
{
5689
 
  uint32_t table_count=  join->tables - join->const_tables;
5690
 
  uint32_t search_depth;
 
5719
  uint table_count=  join->tables - join->const_tables;
 
5720
  uint search_depth;
5691
5721
  /* TODO: this value should be determined dynamically, based on statistics: */
5692
 
  uint32_t max_tables_for_exhaustive_opt= 7;
 
5722
  uint max_tables_for_exhaustive_opt= 7;
5693
5723
 
5694
5724
  if (table_count <= max_tables_for_exhaustive_opt)
5695
5725
    search_depth= table_count+1; // use exhaustive for small number of tables
5731
5761
optimize_straight_join(JOIN *join, table_map join_tables)
5732
5762
{
5733
5763
  JOIN_TAB *s;
5734
 
  uint32_t idx= join->const_tables;
 
5764
  uint idx= join->const_tables;
5735
5765
  double    record_count= 1.0;
5736
5766
  double    read_time=    0.0;
5737
5767
 
5752
5782
  if (join->sort_by_table &&
5753
5783
      join->sort_by_table != join->positions[join->const_tables].table->table)
5754
5784
    read_time+= record_count;  // We have to make a temp table
5755
 
  memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
5785
  memcpy((uchar*) join->best_positions, (uchar*) join->positions,
 
5786
         sizeof(POSITION)*idx);
5756
5787
  join->best_read= read_time;
5757
5788
}
5758
5789
 
5841
5872
static bool
5842
5873
greedy_search(JOIN      *join,
5843
5874
              table_map remaining_tables,
5844
 
              uint32_t      search_depth,
5845
 
              uint32_t      prune_level)
 
5875
              uint      search_depth,
 
5876
              uint      prune_level)
5846
5877
{
5847
5878
  double    record_count= 1.0;
5848
5879
  double    read_time=    0.0;
5849
 
  uint32_t      idx= join->const_tables; // index into 'join->best_ref'
5850
 
  uint32_t      best_idx;
5851
 
  uint32_t      size_remain;    // cardinality of remaining_tables
 
5880
  uint      idx= join->const_tables; // index into 'join->best_ref'
 
5881
  uint      best_idx;
 
5882
  uint      size_remain;    // cardinality of remaining_tables
5852
5883
  POSITION  best_pos;
5853
5884
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
5854
5885
 
5888
5919
      pos= join->best_ref[++best_idx];
5889
5920
    assert((pos != NULL)); // should always find 'best_table'
5890
5921
    /* move 'best_table' at the first free position in the array of joins */
5891
 
    std::swap(join->best_ref[idx], join->best_ref[best_idx]);
 
5922
    swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
5892
5923
 
5893
5924
    /* compute the cost of the new plan extended with 'best_table' */
5894
5925
    record_count*= join->positions[idx].records_read;
6021
6052
static bool
6022
6053
best_extension_by_limited_search(JOIN      *join,
6023
6054
                                 table_map remaining_tables,
6024
 
                                 uint32_t      idx,
 
6055
                                 uint      idx,
6025
6056
                                 double    record_count,
6026
6057
                                 double    read_time,
6027
 
                                 uint32_t      search_depth,
6028
 
                                 uint32_t      prune_level)
 
6058
                                 uint      search_depth,
 
6059
                                 uint      prune_level)
6029
6060
{
6030
6061
  THD *thd= join->thd;
6031
6062
  if (thd->killed)  // Abort
6102
6133
 
6103
6134
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
6104
6135
      { /* Recursively expand the current partial plan */
6105
 
        std::swap(join->best_ref[idx], *pos);
 
6136
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6106
6137
        if (best_extension_by_limited_search(join,
6107
6138
                                             remaining_tables & ~real_table_bit,
6108
6139
                                             idx + 1,
6111
6142
                                             search_depth - 1,
6112
6143
                                             prune_level))
6113
6144
          return(true);
6114
 
        std::swap(join->best_ref[idx], *pos);
 
6145
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6115
6146
      }
6116
6147
      else
6117
6148
      { /*
6126
6157
          current_read_time+= current_record_count;
6127
6158
        if ((search_depth == 1) || (current_read_time < join->best_read))
6128
6159
        {
6129
 
          memcpy(join->best_positions, join->positions,
 
6160
          memcpy((uchar*) join->best_positions, (uchar*) join->positions,
6130
6161
                 sizeof(POSITION) * (idx + 1));
6131
6162
          join->best_read= current_read_time - 0.001;
6132
6163
        }
6149
6180
    true        Fatal error
6150
6181
*/
6151
6182
static bool
6152
 
find_best(JOIN *join,table_map rest_tables,uint32_t idx,double record_count,
 
6183
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
6153
6184
          double read_time)
6154
6185
{
6155
6186
  THD *thd= join->thd;
6164
6195
      read_time+=record_count;                  // We have to make a temp table
6165
6196
    if (read_time < join->best_read)
6166
6197
    {
6167
 
      memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
6198
      memcpy((uchar*) join->best_positions,(uchar*) join->positions,
 
6199
             sizeof(POSITION)*idx);
6168
6200
      join->best_read= read_time - 0.001;
6169
6201
    }
6170
6202
    return(false);
6203
6235
          best_record_count=current_record_count;
6204
6236
          best_read_time=current_read_time;
6205
6237
        }
6206
 
        std::swap(join->best_ref[idx], *pos);
 
6238
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6207
6239
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
6208
6240
                      current_record_count,current_read_time))
6209
6241
          return(true);
6210
 
        std::swap(join->best_ref[idx], *pos);
 
6242
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6211
6243
      }
6212
6244
      restore_prev_nj_state(s);
6213
6245
      restore_prev_sj_state(rest_tables, s);
6223
6255
  Find how much space the prevous read not const tables takes in cache.
6224
6256
*/
6225
6257
 
6226
 
static void calc_used_field_length(THD *thd __attribute__((unused)),
 
6258
static void calc_used_field_length(THD *thd __attribute__((__unused__)),
6227
6259
                                   JOIN_TAB *join_tab)
6228
6260
{
6229
 
  uint32_t null_fields,blobs,fields,rec_length;
 
6261
  uint null_fields,blobs,fields,rec_length;
6230
6262
  Field **f_ptr,*field;
6231
6263
  MY_BITMAP *read_set= join_tab->table->read_set;;
6232
6264
 
6235
6267
  {
6236
6268
    if (bitmap_is_set(read_set, field->field_index))
6237
6269
    {
6238
 
      uint32_t flags=field->flags;
 
6270
      uint flags=field->flags;
6239
6271
      fields++;
6240
6272
      rec_length+=field->pack_length();
6241
6273
      if (flags & BLOB_FLAG)
6245
6277
    }
6246
6278
  }
6247
6279
  if (null_fields)
6248
 
    rec_length+=(join_tab->table->getNullFields() + 7)/8;
 
6280
    rec_length+=(join_tab->table->s->null_fields+7)/8;
6249
6281
  if (join_tab->table->maybe_null)
6250
 
    rec_length+=sizeof(bool);
 
6282
    rec_length+=sizeof(my_bool);
6251
6283
  if (blobs)
6252
6284
  {
6253
 
    uint32_t blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
6254
 
                             (join_tab->table->getRecordLength()- rec_length));
6255
 
    rec_length+=(uint) cmax((uint)4,blob_length);
 
6285
    uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
 
6286
                             (join_tab->table->s->reclength- rec_length));
 
6287
    rec_length+=(uint) max(4,blob_length);
6256
6288
  }
6257
6289
  join_tab->used_fields=fields;
6258
6290
  join_tab->used_fieldlength=rec_length;
6261
6293
 
6262
6294
 
6263
6295
static uint
6264
 
cache_record_length(JOIN *join,uint32_t idx)
 
6296
cache_record_length(JOIN *join,uint idx)
6265
6297
{
6266
 
  uint32_t length=0;
 
6298
  uint length=0;
6267
6299
  JOIN_TAB **pos,**end;
6268
6300
  THD *thd=join->thd;
6269
6301
 
6332
6364
*/
6333
6365
 
6334
6366
static double
6335
 
prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref)
 
6367
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
6336
6368
{
6337
6369
  double found=1.0;
6338
6370
  POSITION *pos_end= join->positions - 1;
6372
6404
static bool
6373
6405
get_best_combination(JOIN *join)
6374
6406
{
6375
 
  uint32_t i,tablenr;
 
6407
  uint i,tablenr;
6376
6408
  table_map used_tables;
6377
6409
  JOIN_TAB *join_tab,*j;
6378
6410
  KEYUSE *keyuse;
6379
 
  uint32_t table_count;
 
6411
  uint table_count;
6380
6412
  THD *thd=join->thd;
6381
6413
 
6382
6414
  table_count=join->tables;
6389
6421
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
6390
6422
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
6391
6423
  {
6392
 
    Table *form;
 
6424
    TABLE *form;
6393
6425
    *j= *join->best_positions[tablenr].table;
6394
6426
    form=join->table[tablenr]=j->table;
6395
6427
    used_tables|= form->map;
6426
6458
{
6427
6459
  KEYUSE *keyuse=org_keyuse;
6428
6460
  THD  *thd= join->thd;
6429
 
  uint32_t keyparts,length,key;
6430
 
  Table *table;
 
6461
  uint keyparts,length,key;
 
6462
  TABLE *table;
6431
6463
  KEY *keyinfo;
6432
6464
 
6433
6465
  /*  Use best key from find_best */
6437
6469
 
6438
6470
  {
6439
6471
    keyparts=length=0;
6440
 
    uint32_t found_part_ref_or_null= 0;
 
6472
    uint found_part_ref_or_null= 0;
6441
6473
    /*
6442
6474
      Calculate length for the used key
6443
6475
      Stop if there is a missing key part or when we find second key_part
6464
6496
  j->ref.key_parts=keyparts;
6465
6497
  j->ref.key_length=length;
6466
6498
  j->ref.key=(int) key;
6467
 
  if (!(j->ref.key_buff= (unsigned char*) thd->calloc(ALIGN_SIZE(length)*2)) ||
 
6499
  if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
6468
6500
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
6469
6501
                                                   (keyparts+1)))) ||
6470
6502
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
6479
6511
  keyuse=org_keyuse;
6480
6512
 
6481
6513
  store_key **ref_key= j->ref.key_copy;
6482
 
  unsigned char *key_buff=j->ref.key_buff, *null_ref_key= 0;
 
6514
  uchar *key_buff=j->ref.key_buff, *null_ref_key= 0;
6483
6515
  bool keyuse_uses_no_tables= true;
6484
6516
  {
6485
 
    uint32_t i;
 
6517
    uint i;
6486
6518
    for (i=0 ; i < keyparts ; keyuse++,i++)
6487
6519
    {
6488
6520
      while (keyuse->keypart != i ||
6489
6521
             ((~used_tables) & keyuse->used_tables))
6490
6522
        keyuse++;                               /* Skip other parts */
6491
6523
 
6492
 
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
 
6524
      uint maybe_null= test(keyinfo->key_part[i].null_bit);
6493
6525
      j->ref.items[i]=keyuse->val;              // Save for cond removal
6494
6526
      j->ref.cond_guards[i]= keyuse->cond_guard;
6495
6527
      if (keyuse->null_rejecting) 
6551
6583
 
6552
6584
static store_key *
6553
6585
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
6554
 
              KEY_PART_INFO *key_part, unsigned char *key_buff, uint32_t maybe_null)
 
6586
              KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
6555
6587
{
6556
6588
  if (!((~used_tables) & keyuse->used_tables))          // if const item
6557
6589
  {
6594
6626
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
6595
6627
{
6596
6628
  bool error;
6597
 
  Table *table= field->table;
 
6629
  TABLE *table= field->table;
6598
6630
  THD *thd= table->in_use;
6599
6631
  ha_rows cuted_fields=thd->cuted_fields;
 
6632
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
 
6633
                                                   table->write_set);
6600
6634
 
6601
6635
  /*
6602
6636
    we should restore old value of count_cuted_fields because
6607
6641
  thd->count_cuted_fields= check_flag;
6608
6642
  error= item->save_in_field(field, 1);
6609
6643
  thd->count_cuted_fields= old_count_cuted_fields;
 
6644
  dbug_tmp_restore_column_map(table->write_set, old_map);
6610
6645
  return error || cuted_fields != thd->cuted_fields;
6611
6646
}
6612
6647
 
6613
6648
 
6614
6649
static bool
6615
 
make_simple_join(JOIN *join,Table *tmp_table)
 
6650
make_simple_join(JOIN *join,TABLE *tmp_table)
6616
6651
{
6617
 
  Table **tableptr;
 
6652
  TABLE **tableptr;
6618
6653
  JOIN_TAB *join_tab;
6619
6654
 
6620
6655
  /*
6621
 
    Reuse Table * and JOIN_TAB if already allocated by a previous call
 
6656
    Reuse TABLE * and JOIN_TAB if already allocated by a previous call
6622
6657
    to this function through JOIN::exec (may happen for sub-queries).
6623
6658
  */
6624
6659
  if (!join->table_reexec)
6625
6660
  {
6626
 
    if (!(join->table_reexec= (Table**) join->thd->alloc(sizeof(Table*))))
 
6661
    if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*))))
6627
6662
      return(true);                        /* purecov: inspected */
6628
6663
    if (join->tmp_join)
6629
6664
      join->tmp_join->table_reexec= join->table_reexec;
6672
6707
  join_tab->ref.key_parts= 0;
6673
6708
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
6674
6709
  join_tab->do_firstmatch= NULL;
6675
 
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
 
6710
  bzero((char*) &join_tab->read_record,sizeof(join_tab->read_record));
6676
6711
  tmp_table->status=0;
6677
6712
  tmp_table->null_row=0;
6678
6713
  return(false);
6748
6783
 
6749
6784
static void add_not_null_conds(JOIN *join)
6750
6785
{
6751
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6786
  for (uint i=join->const_tables ; i < join->tables ; i++)
6752
6787
  {
6753
6788
    JOIN_TAB *tab=join->join_tab+i;
6754
6789
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
6755
6790
         tab->type == JT_REF_OR_NULL) &&
6756
6791
        !tab->table->maybe_null)
6757
6792
    {
6758
 
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
 
6793
      for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
6759
6794
      {
6760
6795
        if (tab->ref.null_rejecting & (1 << keypart))
6761
6796
        {
6868
6903
static void
6869
6904
make_outerjoin_info(JOIN *join)
6870
6905
{
6871
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6906
  for (uint i=join->const_tables ; i < join->tables ; i++)
6872
6907
  {
6873
6908
    JOIN_TAB *tab=join->join_tab+i;
6874
 
    Table *table=tab->table;
6875
 
    TableList *tbl= table->pos_in_table_list;
6876
 
    TableList *embedding= tbl->embedding;
 
6909
    TABLE *table=tab->table;
 
6910
    TABLE_LIST *tbl= table->pos_in_table_list;
 
6911
    TABLE_LIST *embedding= tbl->embedding;
6877
6912
 
6878
6913
    if (tbl->outer_join)
6879
6914
    {
6893
6928
      /* Ignore sj-nests: */
6894
6929
      if (!embedding->on_expr)
6895
6930
        continue;
6896
 
      nested_join_st *nested_join= embedding->nested_join;
 
6931
      NESTED_JOIN *nested_join= embedding->nested_join;
6897
6932
      if (!nested_join->counter_)
6898
6933
      {
6899
6934
        /* 
6970
7005
    }
6971
7006
    used_tables=((select->const_tables=join->const_table_map) |
6972
7007
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
6973
 
    for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
7008
    for (uint i=join->const_tables ; i < join->tables ; i++)
6974
7009
    {
6975
7010
      JOIN_TAB *tab=join->join_tab+i;
6976
7011
      /*
7031
7066
            in the ON part of an OUTER JOIN. In this case we want the code
7032
7067
            below to check if we should use 'quick' instead.
7033
7068
          */
7034
 
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
7069
          tmp= new Item_int((longlong) 1,1);    // Always true
7035
7070
        }
7036
7071
 
7037
7072
      }
7039
7074
          tab->type == JT_EQ_REF)
7040
7075
      {
7041
7076
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7042
 
                                       thd->memdup((unsigned char*) select,
 
7077
                                       thd->memdup((uchar*) select,
7043
7078
                                                   sizeof(*select)));
7044
7079
        if (!sel)
7045
7080
          return(1);                    // End of memory
7094
7129
          }
7095
7130
          tab->quick=0;
7096
7131
        }
7097
 
        uint32_t ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
 
7132
        uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
7098
7133
        if (i == join->const_tables && ref_key)
7099
7134
        {
7100
7135
          if (!tab->const_keys.is_clear_all() &&
7186
7221
                                         current_map, 0)))
7187
7222
            {
7188
7223
              tab->cache.select=(SQL_SELECT*)
7189
 
                thd->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
 
7224
                thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
7190
7225
              tab->cache.select->cond=tmp;
7191
7226
              tab->cache.select->read_tables=join->const_table_map;
7192
7227
            }
7304
7339
    false  No
7305
7340
*/
7306
7341
 
7307
 
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, 
 
7342
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno, 
7308
7343
                            bool other_tbls_ok)
7309
7344
{
7310
7345
  if (item->const_item())
7399
7434
    Index condition, or NULL if no condition could be inferred.
7400
7435
*/
7401
7436
 
7402
 
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno,
 
7437
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
7403
7438
                          bool other_tbls_ok)
7404
7439
{
7405
7440
  if (!cond)
7406
7441
    return NULL;
7407
7442
  if (cond->type() == Item::COND_ITEM)
7408
7443
  {
7409
 
    uint32_t n_marked= 0;
 
7444
    uint n_marked= 0;
7410
7445
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7411
7446
    {
7412
7447
      Item_cond_and *new_cond=new Item_cond_and;
7538
7573
    Try to extract and push the index condition down to table handler
7539
7574
*/
7540
7575
 
7541
 
static void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok)
 
7576
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
7542
7577
{
7543
7578
  Item *idx_cond;
7544
7579
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
7592
7627
 
7593
7628
 
7594
7629
    /*
7595
 
      Determine if the set is already ordered for order_st BY, so it can 
 
7630
      Determine if the set is already ordered for ORDER BY, so it can 
7596
7631
      disable join cache because it will change the ordering of the results.
7597
7632
      Code handles sort table that is at any location (not only first after 
7598
7633
      the const tables) despite the fact that it's currently prohibited.
7600
7635
      ordered. If there is a temp table the ordering is done as a last
7601
7636
      operation and doesn't prevent join cache usage.
7602
7637
    */
7603
 
uint32_t make_join_orderinfo(JOIN *join)
 
7638
uint make_join_orderinfo(JOIN *join)
7604
7639
{
7605
 
  uint32_t i;
 
7640
  uint i;
7606
7641
  if (join->need_tmp)
7607
7642
    return join->tables;
7608
7643
 
7609
7644
  for (i=join->const_tables ; i < join->tables ; i++)
7610
7645
  {
7611
7646
    JOIN_TAB *tab=join->join_tab+i;
7612
 
    Table *table=tab->table;
 
7647
    TABLE *table=tab->table;
7613
7648
    if ((table == join->sort_by_table && 
7614
7649
         (!join->order || join->skip_sort_order)) ||
7615
 
        (join->sort_by_table == (Table *) 1 && i != join->const_tables))
 
7650
        (join->sort_by_table == (TABLE *) 1 && i != join->const_tables))
7616
7651
    {
7617
7652
      break;
7618
7653
    }
7644
7679
*/
7645
7680
 
7646
7681
static bool
7647
 
make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
7682
make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
7648
7683
{
7649
 
  uint32_t i;
 
7684
  uint i;
7650
7685
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
7651
7686
  bool sorted= 1;
7652
7687
 
7653
7688
  for (i=join->const_tables ; i < join->tables ; i++)
7654
7689
  {
7655
7690
    JOIN_TAB *tab=join->join_tab+i;
7656
 
    Table *table=tab->table;
 
7691
    TABLE *table=tab->table;
7657
7692
    bool using_join_cache;
7658
7693
    tab->read_record.table= table;
7659
7694
    tab->read_record.file=table->file;
7666
7701
    sorted= 0;                                  // only first must be sorted
7667
7702
    if (tab->insideout_match_tab)
7668
7703
    {
7669
 
      if (!(tab->insideout_buf= (unsigned char*)join->thd->alloc(tab->table->key_info
 
7704
      if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
7670
7705
                                                         [tab->index].
7671
7706
                                                         key_length)))
7672
7707
        return true;
7818
7853
                  table->file->primary_key_is_clustered())
7819
7854
                tab->index= table->s->primary_key;
7820
7855
              else
7821
 
                tab->index= table->find_shortest_key(&table->covering_keys);
 
7856
                tab->index=find_shortest_key(table, & table->covering_keys);
7822
7857
            }
7823
7858
            tab->read_first_record= join_read_first;
7824
7859
            tab->type=JT_NEXT;          // Read with index_first / index_next
7882
7917
  select= 0;
7883
7918
  delete quick;
7884
7919
  quick= 0;
7885
 
  if (cache.buff)
7886
 
    free(cache.buff);
 
7920
  x_free(cache.buff);
7887
7921
  cache.buff= 0;
7888
7922
  limit= 0;
7889
7923
  if (table)
7937
7971
    a correlated subquery itself, but has subqueries, we can free it
7938
7972
    fully and also free JOINs of all its subqueries. The exception
7939
7973
    is a subquery in SELECT list, e.g: @n
7940
 
    SELECT a, (select cmax(b) from t1) group by c @n
 
7974
    SELECT a, (select max(b) from t1) group by c @n
7941
7975
    This subquery will not be evaluated at first sweep and its value will
7942
7976
    not be inserted into the temporary table. Instead, it's evaluated
7943
7977
    when selecting from the temporary table. Therefore, it can't be freed
8081
8115
 
8082
8116
 
8083
8117
/**
8084
 
  Remove the following expressions from order_st BY and GROUP BY:
 
8118
  Remove the following expressions from ORDER BY and GROUP BY:
8085
8119
  Constant expressions @n
8086
8120
  Expression that only uses tables that are of type EQ_REF and the reference
8087
 
  is in the order_st list or if all refereed tables are of the above type.
 
8121
  is in the ORDER list or if all refereed tables are of the above type.
8088
8122
 
8089
8123
  In the following, the X field can be removed:
8090
8124
  @code
8091
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
8092
 
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
 
8125
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
 
8126
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
8093
8127
  @endcode
8094
8128
 
8095
8129
  These can't be optimized:
8096
8130
  @code
8097
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
8098
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
8099
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
 
8131
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
 
8132
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
 
8133
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
8100
8134
  @endcode
8101
8135
*/
8102
8136
 
8103
8137
static bool
8104
 
eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
 
8138
eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab)
8105
8139
{
8106
8140
  if (tab->cached_eq_ref_table)                 // If cached
8107
8141
    return tab->eq_ref_table;
8113
8147
    return (tab->eq_ref_table=0);               // We must use this
8114
8148
  Item **ref_item=tab->ref.items;
8115
8149
  Item **end=ref_item+tab->ref.key_parts;
8116
 
  uint32_t found=0;
 
8150
  uint found=0;
8117
8151
  table_map map=tab->table->map;
8118
8152
 
8119
8153
  for (; ref_item != end ; ref_item++)
8120
8154
  {
8121
8155
    if (! (*ref_item)->const_item())
8122
8156
    {                                           // Not a const ref
8123
 
      order_st *order;
 
8157
      ORDER *order;
8124
8158
      for (order=start_order ; order ; order=order->next)
8125
8159
      {
8126
8160
        if ((*ref_item)->eq(order->item[0],0))
8131
8165
        found++;
8132
8166
        assert(!(order->used & map));
8133
8167
        order->used|=map;
8134
 
        continue;                               // Used in order_st BY
 
8168
        continue;                               // Used in ORDER BY
8135
8169
      }
8136
8170
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
8137
8171
        return (tab->eq_ref_table=0);
8153
8187
 
8154
8188
 
8155
8189
static bool
8156
 
only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
 
8190
only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables)
8157
8191
{
 
8192
  if (specialflag &  SPECIAL_SAFE_MODE)
 
8193
    return 0;                   // skip this optimize /* purecov: inspected */
8158
8194
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
8159
8195
  {
8160
8196
    if (tables & 1 && !eq_ref_table(join, order, *tab))
8175
8211
    TABLE_REF *ref= &join_tab->ref;
8176
8212
    table_map depend_map=0;
8177
8213
    Item **item=ref->items;
8178
 
    uint32_t i;
 
8214
    uint i;
8179
8215
    for (i=0 ; i < ref->key_parts ; i++,item++)
8180
8216
      depend_map|=(*item)->used_tables();
8181
8217
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
8193
8229
 
8194
8230
/** Update the dependency map for the sort order. */
8195
8231
 
8196
 
static void update_depend_map(JOIN *join, order_st *order)
 
8232
static void update_depend_map(JOIN *join, ORDER *order)
8197
8233
{
8198
8234
  for (; order ; order=order->next)
8199
8235
  {
8217
8253
 
8218
8254
 
8219
8255
/**
8220
 
  Remove all constants and check if order_st only contains simple
 
8256
  Remove all constants and check if ORDER only contains simple
8221
8257
  expressions.
8222
8258
 
8223
8259
  simple_order is set to 1 if sort_order only uses fields from head table
8235
8271
    Returns new sort order
8236
8272
*/
8237
8273
 
8238
 
static order_st *
8239
 
remove_const(JOIN *join,order_st *first_order, COND *cond,
 
8274
static ORDER *
 
8275
remove_const(JOIN *join,ORDER *first_order, COND *cond,
8240
8276
             bool change_list, bool *simple_order)
8241
8277
{
8242
8278
  if (join->tables == join->const_tables)
8243
8279
    return change_list ? 0 : first_order;               // No need to sort
8244
8280
 
8245
 
  order_st *order,**prev_ptr;
 
8281
  ORDER *order,**prev_ptr;
8246
8282
  table_map first_table= join->join_tab[join->const_tables].table->map;
8247
8283
  table_map not_const_tables= ~join->const_table_map;
8248
8284
  table_map ref;
8299
8335
 
8300
8336
 
8301
8337
static int
8302
 
return_zero_rows(JOIN *join, select_result *result,TableList *tables,
8303
 
                 List<Item> &fields, bool send_row, uint64_t select_options,
 
8338
return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables,
 
8339
                 List<Item> &fields, bool send_row, ulonglong select_options,
8304
8340
                 const char *info, Item *having)
8305
8341
{
8306
8342
  if (select_options & SELECT_DESCRIBE)
8313
8349
 
8314
8350
  if (send_row)
8315
8351
  {
8316
 
    for (TableList *table= tables; table; table= table->next_leaf)
 
8352
    for (TABLE_LIST *table= tables; table; table= table->next_leaf)
8317
8353
      mark_as_null_row(table->table);           // All fields are NULL
8318
8354
    if (having && having->val_int() == 0)
8319
8355
      send_row=0;
8345
8381
    must clear only the non-const tables, as const tables
8346
8382
    are not re-calculated.
8347
8383
  */
8348
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
8384
  for (uint i=join->const_tables ; i < join->tables ; i++)
8349
8385
    mark_as_null_row(join->table[i]);           // All fields are NULL
8350
8386
}
8351
8387
 
8637
8673
 
8638
8674
      if (field_item->result_type() == STRING_RESULT)
8639
8675
      {
8640
 
        const CHARSET_INFO * const cs= ((Field_str*) field_item->field)->charset();
 
8676
        CHARSET_INFO *cs= ((Field_str*) field_item->field)->charset();
8641
8677
        if (!item)
8642
8678
        {
8643
8679
          Item_func_eq *eq_item;
8709
8745
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
8710
8746
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
8711
8747
8712
 
  uint32_t n= left_row->cols();
8713
 
  for (uint32_t i= 0 ; i < n; i++)
 
8748
  uint n= left_row->cols();
 
8749
  for (uint i= 0 ; i < n; i++)
8714
8750
  {
8715
8751
    bool is_converted;
8716
8752
    Item *left_item= left_row->element_index(i);
8954
8990
    {
8955
8991
      int n= cond_equal.current_level.elements + eq_list.elements;
8956
8992
      if (n == 0)
8957
 
        return new Item_int((int64_t) 1,1);
 
8993
        return new Item_int((longlong) 1,1);
8958
8994
      else if (n == 1)
8959
8995
      {
8960
8996
        if ((item_equal= cond_equal.current_level.pop()))
8997
9033
      as soon the field is not of a string type or the field reference is
8998
9034
      an argument of a comparison predicate. 
8999
9035
    */ 
9000
 
    unsigned char *is_subst_valid= (unsigned char *) 1;
 
9036
    uchar *is_subst_valid= (uchar *) 1;
9001
9037
    cond= cond->compile(&Item::subst_argument_checker,
9002
9038
                        &is_subst_valid, 
9003
9039
                        &Item::equal_fields_propagator,
9004
 
                        (unsigned char *) inherited);
 
9040
                        (uchar *) inherited);
9005
9041
    cond->update_used_tables();
9006
9042
  }
9007
9043
  return cond;
9076
9112
   
9077
9113
static COND *build_equal_items(THD *thd, COND *cond,
9078
9114
                               COND_EQUAL *inherited,
9079
 
                               List<TableList> *join_list,
 
9115
                               List<TABLE_LIST> *join_list,
9080
9116
                               COND_EQUAL **cond_equal_ref)
9081
9117
{
9082
9118
  COND_EQUAL *cond_equal= 0;
9104
9140
 
9105
9141
  if (join_list)
9106
9142
  {
9107
 
    TableList *table;
9108
 
    List_iterator<TableList> li(*join_list);
 
9143
    TABLE_LIST *table;
 
9144
    List_iterator<TABLE_LIST> li(*join_list);
9109
9145
 
9110
9146
    while ((table= li++))
9111
9147
    {
9112
9148
      if (table->on_expr)
9113
9149
      {
9114
 
        List<TableList> *nested_join_list= table->nested_join ?
 
9150
        List<TABLE_LIST> *nested_join_list= table->nested_join ?
9115
9151
          &table->nested_join->join_list : NULL;
9116
9152
        /*
9117
9153
          We can modify table->on_expr because its old value will
9218
9254
  List<Item> eq_list;
9219
9255
  Item_func_eq *eq_item= 0;
9220
9256
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
9221
 
    return new Item_int((int64_t) 0,1); 
 
9257
    return new Item_int((longlong) 0,1); 
9222
9258
  Item *item_const= item_equal->get_const();
9223
9259
  Item_equal_iterator it(*item_equal);
9224
9260
  Item *head;
9263
9299
  if (!cond && !eq_list.head())
9264
9300
  {
9265
9301
    if (!eq_item)
9266
 
      return new Item_int((int64_t) 1,1);
 
9302
      return new Item_int((longlong) 1,1);
9267
9303
    return eq_item;
9268
9304
  }
9269
9305
 
9365
9401
    }
9366
9402
    if (cond->type() == Item::COND_ITEM &&
9367
9403
        !((Item_cond*)cond)->argument_list()->elements)
9368
 
      cond= new Item_int((int32_t)cond->val_bool());
 
9404
      cond= new Item_int((int32)cond->val_bool());
9369
9405
 
9370
9406
  }
9371
9407
  else if (cond->type() == Item::FUNC_ITEM && 
9435
9471
        */  
9436
9472
        if (!possible_keys.is_clear_all())
9437
9473
        {
9438
 
          Table *tab= field->table;
 
9474
          TABLE *tab= field->table;
9439
9475
          KEYUSE *use;
9440
9476
          for (use= stat->keyuse; use && use->table == tab; use++)
9441
9477
            if (possible_keys.is_set(use->key) && 
9747
9783
*/
9748
9784
 
9749
9785
static COND *
9750
 
simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top,
 
9786
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
9751
9787
               bool in_sj)
9752
9788
{
9753
 
  TableList *table;
9754
 
  nested_join_st *nested_join;
9755
 
  TableList *prev_table= 0;
9756
 
  List_iterator<TableList> li(*join_list);
 
9789
  TABLE_LIST *table;
 
9790
  NESTED_JOIN *nested_join;
 
9791
  TABLE_LIST *prev_table= 0;
 
9792
  List_iterator<TABLE_LIST> li(*join_list);
9757
9793
 
9758
9794
  /* 
9759
9795
    Try to simplify join operations from join_list.
9903
9939
    }
9904
9940
    else if (nested_join && !table->on_expr)
9905
9941
    {
9906
 
      TableList *tbl;
9907
 
      List_iterator<TableList> it(nested_join->join_list);
 
9942
      TABLE_LIST *tbl;
 
9943
      List_iterator<TABLE_LIST> it(nested_join->join_list);
9908
9944
      while ((tbl= it++))
9909
9945
      {
9910
9946
        tbl->embedding= table->embedding;
9937
9973
    First unused bit in nested_join_map after the call.
9938
9974
*/
9939
9975
 
9940
 
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, 
9941
 
                                          uint32_t first_unused)
 
9976
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, 
 
9977
                                          uint first_unused)
9942
9978
{
9943
 
  List_iterator<TableList> li(*join_list);
9944
 
  TableList *table;
 
9979
  List_iterator<TABLE_LIST> li(*join_list);
 
9980
  TABLE_LIST *table;
9945
9981
  while ((table= li++))
9946
9982
  {
9947
 
    nested_join_st *nested_join;
 
9983
    NESTED_JOIN *nested_join;
9948
9984
    if ((nested_join= table->nested_join))
9949
9985
    {
9950
9986
      /*
9973
10009
 
9974
10010
 
9975
10011
/**
9976
 
  Set nested_join_st::counter=0 in all nested joins in passed list.
 
10012
  Set NESTED_JOIN::counter=0 in all nested joins in passed list.
9977
10013
 
9978
 
    Recursively set nested_join_st::counter=0 for all nested joins contained in
 
10014
    Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
9979
10015
    the passed join_list.
9980
10016
 
9981
10017
  @param join_list  List of nested joins to process. It may also contain base
9982
10018
                    tables which will be ignored.
9983
10019
*/
9984
10020
 
9985
 
static void reset_nj_counters(List<TableList> *join_list)
 
10021
static void reset_nj_counters(List<TABLE_LIST> *join_list)
9986
10022
{
9987
 
  List_iterator<TableList> li(*join_list);
9988
 
  TableList *table;
 
10023
  List_iterator<TABLE_LIST> li(*join_list);
 
10024
  TABLE_LIST *table;
9989
10025
  while ((table= li++))
9990
10026
  {
9991
 
    nested_join_st *nested_join;
 
10027
    NESTED_JOIN *nested_join;
9992
10028
    if ((nested_join= table->nested_join))
9993
10029
    {
9994
10030
      nested_join->counter_= 0;
10011
10047
 
10012
10048
  @verbatim
10013
10049
     IMPLEMENTATION 
10014
 
       LIMITATIONS ON JOIN order_st
 
10050
       LIMITATIONS ON JOIN ORDER
10015
10051
         The nested [outer] joins executioner algorithm imposes these limitations
10016
10052
         on join order:
10017
10053
         1. "Outer tables first" -  any "outer" table must be before any 
10075
10111
         position:
10076
10112
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
10077
10113
             joins) we've opened but didn't close.
10078
 
          2. {each nested_join_st structure not simplified away}->counter - number
 
10114
          2. {each NESTED_JOIN structure not simplified away}->counter - number
10079
10115
             of this nested join's children that have already been added to to
10080
10116
             the partial join order.
10081
10117
  @endverbatim
10094
10130
 
10095
10131
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
10096
10132
{
10097
 
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
 
10133
  TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding;
10098
10134
  JOIN *join= last_tab->join;
10099
10135
 
10100
10136
  if (join->cur_embedding_map & ~next_tab->embedding_map)
10151
10187
 
10152
10188
static void restore_prev_nj_state(JOIN_TAB *last)
10153
10189
{
10154
 
  TableList *last_emb= last->table->pos_in_table_list->embedding;
 
10190
  TABLE_LIST *last_emb= last->table->pos_in_table_list->embedding;
10155
10191
  JOIN *join= last->join;
10156
10192
  while (last_emb)
10157
10193
  {
10174
10210
static 
10175
10211
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
10176
10212
{
10177
 
  TableList *emb_sj_nest;
 
10213
  TABLE_LIST *emb_sj_nest;
10178
10214
  if ((emb_sj_nest= tab->emb_sj_nest))
10179
10215
  {
10180
10216
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
10192
10228
static void restore_prev_sj_state(const table_map remaining_tables, 
10193
10229
                                  const JOIN_TAB *tab)
10194
10230
{
10195
 
  TableList *emb_sj_nest;
 
10231
  TABLE_LIST *emb_sj_nest;
10196
10232
  if ((emb_sj_nest= tab->emb_sj_nest))
10197
10233
  {
10198
10234
    /* If we're removing the last SJ-inner table, remove the sj-nest */
10206
10242
 
10207
10243
 
10208
10244
static COND *
10209
 
optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list,
 
10245
optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list,
10210
10246
              Item::cond_result *cond_value)
10211
10247
{
10212
10248
  THD *thd= join->thd;
10269
10305
        li.remove();
10270
10306
      else if (item != new_item)
10271
10307
      {
10272
 
        li.replace(new_item);
 
10308
        VOID(li.replace(new_item));
10273
10309
        should_fix_fields=1;
10274
10310
      }
10275
10311
      if (*cond_value == Item::COND_UNDEF)
10354
10390
        thd->substitute_null_with_insert_id= false;
10355
10391
      }
10356
10392
      /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
10357
 
      else if (((field->type() == DRIZZLE_TYPE_NEWDATE) ||
10358
 
                (field->type() == DRIZZLE_TYPE_DATETIME)) &&
 
10393
      else if (((field->type() == MYSQL_TYPE_NEWDATE) ||
 
10394
                (field->type() == MYSQL_TYPE_DATETIME)) &&
10359
10395
                (field->flags & NOT_NULL_FLAG) &&
10360
10396
               !field->table->maybe_null)
10361
10397
      {
10502
10538
  return 0;
10503
10539
}
10504
10540
 
 
10541
/****************************************************************************
 
10542
  Create internal temporary table
 
10543
****************************************************************************/
 
10544
 
 
10545
/**
 
10546
  Create field for temporary table from given field.
 
10547
 
 
10548
  @param thd           Thread handler
 
10549
  @param org_field    field from which new field will be created
 
10550
  @param name         New field name
 
10551
  @param table         Temporary table
 
10552
  @param item          !=NULL if item->result_field should point to new field.
 
10553
                      This is relevant for how fill_record() is going to work:
 
10554
                      If item != NULL then fill_record() will update
 
10555
                      the record in the original table.
 
10556
                      If item == NULL then fill_record() will update
 
10557
                      the temporary table
 
10558
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
 
10559
                               field instead of blob.
 
10560
 
 
10561
  @retval
 
10562
    NULL                on error
 
10563
  @retval
 
10564
    new_created field
 
10565
*/
 
10566
 
 
10567
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
 
10568
                                   const char *name, TABLE *table,
 
10569
                                   Item_field *item, uint convert_blob_length)
 
10570
{
 
10571
  Field *new_field;
 
10572
 
 
10573
  /* 
 
10574
    Make sure that the blob fits into a Field_varstring which has 
 
10575
    2-byte lenght. 
 
10576
  */
 
10577
  if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
 
10578
      (org_field->flags & BLOB_FLAG))
 
10579
    new_field= new Field_varstring(convert_blob_length,
 
10580
                                   org_field->maybe_null(),
 
10581
                                   org_field->field_name, table->s,
 
10582
                                   org_field->charset());
 
10583
  else
 
10584
    new_field= org_field->new_field(thd->mem_root, table,
 
10585
                                    table == org_field->table);
 
10586
  if (new_field)
 
10587
  {
 
10588
    new_field->init(table);
 
10589
    new_field->orig_table= org_field->orig_table;
 
10590
    if (item)
 
10591
      item->result_field= new_field;
 
10592
    else
 
10593
      new_field->field_name= name;
 
10594
    new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
 
10595
    if (org_field->maybe_null() || (item && item->maybe_null))
 
10596
      new_field->flags&= ~NOT_NULL_FLAG;        // Because of outer join
 
10597
    if (org_field->type() == MYSQL_TYPE_VAR_STRING ||
 
10598
        org_field->type() == MYSQL_TYPE_VARCHAR)
 
10599
      table->s->db_create_options|= HA_OPTION_PACK_RECORD;
 
10600
    else if (org_field->type() == FIELD_TYPE_DOUBLE)
 
10601
      ((Field_double *) new_field)->not_fixed= true;
 
10602
  }
 
10603
  return new_field;
 
10604
}
 
10605
 
 
10606
/**
 
10607
  Create field for temporary table using type of given item.
 
10608
 
 
10609
  @param thd                   Thread handler
 
10610
  @param item                  Item to create a field for
 
10611
  @param table                 Temporary table
 
10612
  @param copy_func             If set and item is a function, store copy of
 
10613
                               item in this array
 
10614
  @param modify_item           1 if item->result_field should point to new
 
10615
                               item. This is relevent for how fill_record()
 
10616
                               is going to work:
 
10617
                               If modify_item is 1 then fill_record() will
 
10618
                               update the record in the original table.
 
10619
                               If modify_item is 0 then fill_record() will
 
10620
                               update the temporary table
 
10621
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
 
10622
                               field instead of blob.
 
10623
 
 
10624
  @retval
 
10625
    0  on error
 
10626
  @retval
 
10627
    new_created field
 
10628
*/
 
10629
 
 
10630
static Field *create_tmp_field_from_item(THD *thd __attribute__((__unused__)),
 
10631
                                         Item *item, TABLE *table,
 
10632
                                         Item ***copy_func, bool modify_item,
 
10633
                                         uint convert_blob_length)
 
10634
{
 
10635
  bool maybe_null= item->maybe_null;
 
10636
  Field *new_field;
 
10637
 
 
10638
  switch (item->result_type()) {
 
10639
  case REAL_RESULT:
 
10640
    new_field= new Field_double(item->max_length, maybe_null,
 
10641
                                item->name, item->decimals, true);
 
10642
    break;
 
10643
  case INT_RESULT:
 
10644
    /* 
 
10645
      Select an integer type with the minimal fit precision.
 
10646
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
 
10647
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into 
 
10648
      Field_long : make them Field_longlong.  
 
10649
    */
 
10650
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
10651
      new_field=new Field_longlong(item->max_length, maybe_null,
 
10652
                                   item->name, item->unsigned_flag);
 
10653
    else
 
10654
      new_field=new Field_long(item->max_length, maybe_null,
 
10655
                               item->name, item->unsigned_flag);
 
10656
    break;
 
10657
  case STRING_RESULT:
 
10658
    assert(item->collation.collation);
 
10659
  
 
10660
    enum enum_field_types type;
 
10661
    /*
 
10662
      DATE/TIME fields have STRING_RESULT result type. 
 
10663
      To preserve type they needed to be handled separately.
 
10664
    */
 
10665
    if ((type= item->field_type()) == MYSQL_TYPE_DATETIME ||
 
10666
        type == MYSQL_TYPE_TIME || type == MYSQL_TYPE_NEWDATE ||
 
10667
        type == MYSQL_TYPE_TIMESTAMP)
 
10668
      new_field= item->tmp_table_field_from_field_type(table, 1);
 
10669
    /* 
 
10670
      Make sure that the blob fits into a Field_varstring which has 
 
10671
      2-byte lenght. 
 
10672
    */
 
10673
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
 
10674
             convert_blob_length <= Field_varstring::MAX_SIZE && 
 
10675
             convert_blob_length)
 
10676
      new_field= new Field_varstring(convert_blob_length, maybe_null,
 
10677
                                     item->name, table->s,
 
10678
                                     item->collation.collation);
 
10679
    else
 
10680
      new_field= item->make_string_field(table);
 
10681
    new_field->set_derivation(item->collation.derivation);
 
10682
    break;
 
10683
  case DECIMAL_RESULT:
 
10684
  {
 
10685
    uint8 dec= item->decimals;
 
10686
    uint8 intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
10687
    uint32 len= item->max_length;
 
10688
 
 
10689
    /*
 
10690
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
10691
      will always throw a warning. We must limit dec to
 
10692
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
10693
    */
 
10694
 
 
10695
    if (dec > 0)
 
10696
    {
 
10697
      signed int overflow;
 
10698
 
 
10699
      dec= min(dec, DECIMAL_MAX_SCALE);
 
10700
 
 
10701
      /*
 
10702
        If the value still overflows the field with the corrected dec,
 
10703
        we'll throw out decimals rather than integers. This is still
 
10704
        bad and of course throws a truncation warning.
 
10705
        +1: for decimal point
 
10706
      */
 
10707
 
 
10708
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
10709
                                               item->unsigned_flag) - len;
 
10710
 
 
10711
      if (overflow > 0)
 
10712
        dec= max(0, dec - overflow);            // too long, discard fract
 
10713
      else
 
10714
        len -= item->decimals - dec;            // corrected value fits
 
10715
    }
 
10716
 
 
10717
    new_field= new Field_new_decimal(len, maybe_null, item->name,
 
10718
                                     dec, item->unsigned_flag);
 
10719
    break;
 
10720
  }
 
10721
  case ROW_RESULT:
 
10722
  default:
 
10723
    // This case should never be choosen
 
10724
    assert(0);
 
10725
    new_field= 0;
 
10726
    break;
 
10727
  }
 
10728
  if (new_field)
 
10729
    new_field->init(table);
 
10730
    
 
10731
  if (copy_func && item->is_result_field())
 
10732
    *((*copy_func)++) = item;                   // Save for copy_funcs
 
10733
  if (modify_item)
 
10734
    item->set_result_field(new_field);
 
10735
  if (item->type() == Item::NULL_ITEM)
 
10736
    new_field->is_created_from_null_item= true;
 
10737
  return new_field;
 
10738
}
 
10739
 
 
10740
 
 
10741
/**
 
10742
  Create field for information schema table.
 
10743
 
 
10744
  @param thd            Thread handler
 
10745
  @param table          Temporary table
 
10746
  @param item           Item to create a field for
 
10747
 
 
10748
  @retval
 
10749
    0                   on error
 
10750
  @retval
 
10751
    new_created field
 
10752
*/
 
10753
 
 
10754
Field *create_tmp_field_for_schema(THD *thd __attribute__((__unused__)),
 
10755
                                   Item *item, TABLE *table)
 
10756
{
 
10757
  if (item->field_type() == MYSQL_TYPE_VARCHAR)
 
10758
  {
 
10759
    Field *field;
 
10760
    if (item->max_length > MAX_FIELD_VARCHARLENGTH)
 
10761
      field= new Field_blob(item->max_length, item->maybe_null,
 
10762
                            item->name, item->collation.collation);
 
10763
    else
 
10764
      field= new Field_varstring(item->max_length, item->maybe_null,
 
10765
                                 item->name,
 
10766
                                 table->s, item->collation.collation);
 
10767
    if (field)
 
10768
      field->init(table);
 
10769
    return field;
 
10770
  }
 
10771
  return item->tmp_table_field_from_field_type(table, 0);
 
10772
}
 
10773
 
 
10774
 
 
10775
/**
 
10776
  Create field for temporary table.
 
10777
 
 
10778
  @param thd            Thread handler
 
10779
  @param table          Temporary table
 
10780
  @param item           Item to create a field for
 
10781
  @param type           Type of item (normally item->type)
 
10782
  @param copy_func      If set and item is a function, store copy of item
 
10783
                       in this array
 
10784
  @param from_field    if field will be created using other field as example,
 
10785
                       pointer example field will be written here
 
10786
  @param default_field  If field has a default value field, store it here
 
10787
  @param group          1 if we are going to do a relative group by on result
 
10788
  @param modify_item    1 if item->result_field should point to new item.
 
10789
                       This is relevent for how fill_record() is going to
 
10790
                       work:
 
10791
                       If modify_item is 1 then fill_record() will update
 
10792
                       the record in the original table.
 
10793
                       If modify_item is 0 then fill_record() will update
 
10794
                       the temporary table
 
10795
  @param convert_blob_length If >0 create a varstring(convert_blob_length)
 
10796
                             field instead of blob.
 
10797
 
 
10798
  @retval
 
10799
    0                   on error
 
10800
  @retval
 
10801
    new_created field
 
10802
*/
 
10803
 
 
10804
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
 
10805
                        Item ***copy_func, Field **from_field,
 
10806
                        Field **default_field,
 
10807
                        bool group, bool modify_item,
 
10808
                        bool table_cant_handle_bit_fields __attribute__((__unused__)),
 
10809
                        bool make_copy_field,
 
10810
                        uint convert_blob_length)
 
10811
{
 
10812
  Field *result;
 
10813
  Item::Type orig_type= type;
 
10814
  Item *orig_item= 0;
 
10815
 
 
10816
  if (type != Item::FIELD_ITEM &&
 
10817
      item->real_item()->type() == Item::FIELD_ITEM)
 
10818
  {
 
10819
    orig_item= item;
 
10820
    item= item->real_item();
 
10821
    type= Item::FIELD_ITEM;
 
10822
  }
 
10823
 
 
10824
  switch (type) {
 
10825
  case Item::SUM_FUNC_ITEM:
 
10826
  {
 
10827
    Item_sum *item_sum=(Item_sum*) item;
 
10828
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
 
10829
    if (!result)
 
10830
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
10831
    return result;
 
10832
  }
 
10833
  case Item::FIELD_ITEM:
 
10834
  case Item::DEFAULT_VALUE_ITEM:
 
10835
  {
 
10836
    Item_field *field= (Item_field*) item;
 
10837
    bool orig_modify= modify_item;
 
10838
    if (orig_type == Item::REF_ITEM)
 
10839
      modify_item= 0;
 
10840
    /*
 
10841
      If item have to be able to store NULLs but underlaid field can't do it,
 
10842
      create_tmp_field_from_field() can't be used for tmp field creation.
 
10843
    */
 
10844
    if (field->maybe_null && !field->field->maybe_null())
 
10845
    {
 
10846
      result= create_tmp_field_from_item(thd, item, table, NULL,
 
10847
                                         modify_item, convert_blob_length);
 
10848
      *from_field= field->field;
 
10849
      if (result && modify_item)
 
10850
        field->result_field= result;
 
10851
    } 
 
10852
    else
 
10853
      result= create_tmp_field_from_field(thd, (*from_field= field->field),
 
10854
                                          orig_item ? orig_item->name :
 
10855
                                          item->name,
 
10856
                                          table,
 
10857
                                          modify_item ? field :
 
10858
                                          NULL,
 
10859
                                          convert_blob_length);
 
10860
    if (orig_type == Item::REF_ITEM && orig_modify)
 
10861
      ((Item_ref*)orig_item)->set_result_field(result);
 
10862
    if (field->field->eq_def(result))
 
10863
      *default_field= field->field;
 
10864
    return result;
 
10865
  }
 
10866
  /* Fall through */
 
10867
  case Item::FUNC_ITEM:
 
10868
    /* Fall through */
 
10869
  case Item::COND_ITEM:
 
10870
  case Item::FIELD_AVG_ITEM:
 
10871
  case Item::FIELD_STD_ITEM:
 
10872
  case Item::SUBSELECT_ITEM:
 
10873
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
 
10874
  case Item::PROC_ITEM:
 
10875
  case Item::INT_ITEM:
 
10876
  case Item::REAL_ITEM:
 
10877
  case Item::DECIMAL_ITEM:
 
10878
  case Item::STRING_ITEM:
 
10879
  case Item::REF_ITEM:
 
10880
  case Item::NULL_ITEM:
 
10881
  case Item::VARBIN_ITEM:
 
10882
    if (make_copy_field)
 
10883
    {
 
10884
      assert(((Item_result_field*)item)->result_field);
 
10885
      *from_field= ((Item_result_field*)item)->result_field;
 
10886
    }
 
10887
    return create_tmp_field_from_item(thd, item, table,
 
10888
                                      (make_copy_field ? 0 : copy_func),
 
10889
                                       modify_item, convert_blob_length);
 
10890
  case Item::TYPE_HOLDER:  
 
10891
    result= ((Item_type_holder *)item)->make_field_by_type(table);
 
10892
    result->set_derivation(item->collation.derivation);
 
10893
    return result;
 
10894
  default:                                      // Dosen't have to be stored
 
10895
    return 0;
 
10896
  }
 
10897
}
 
10898
 
 
10899
/*
 
10900
  Set up column usage bitmaps for a temporary table
 
10901
 
 
10902
  IMPLEMENTATION
 
10903
    For temporary tables, we need one bitmap with all columns set and
 
10904
    a tmp_set bitmap to be used by things like filesort.
 
10905
*/
 
10906
 
 
10907
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
 
10908
{
 
10909
  uint field_count= table->s->fields;
 
10910
  bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
 
10911
              false);
 
10912
  bitmap_init(&table->tmp_set,
 
10913
              (my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
 
10914
              field_count, false);
 
10915
  /* write_set and all_set are copies of read_set */
 
10916
  table->def_write_set= table->def_read_set;
 
10917
  table->s->all_set= table->def_read_set;
 
10918
  bitmap_set_all(&table->s->all_set);
 
10919
  table->default_column_bitmaps();
 
10920
}
 
10921
 
 
10922
 
 
10923
/**
 
10924
  Create a temp table according to a field list.
 
10925
 
 
10926
  Given field pointers are changed to point at tmp_table for
 
10927
  send_fields. The table object is self contained: it's
 
10928
  allocated in its own memory root, as well as Field objects
 
10929
  created for table columns.
 
10930
  This function will replace Item_sum items in 'fields' list with
 
10931
  corresponding Item_field items, pointing at the fields in the
 
10932
  temporary table, unless this was prohibited by true
 
10933
  value of argument save_sum_fields. The Item_field objects
 
10934
  are created in THD memory root.
 
10935
 
 
10936
  @param thd                  thread handle
 
10937
  @param param                a description used as input to create the table
 
10938
  @param fields               list of items that will be used to define
 
10939
                              column types of the table (also see NOTES)
 
10940
  @param group                TODO document
 
10941
  @param distinct             should table rows be distinct
 
10942
  @param save_sum_fields      see NOTES
 
10943
  @param select_options
 
10944
  @param rows_limit
 
10945
  @param table_alias          possible name of the temporary table that can
 
10946
                              be used for name resolving; can be "".
 
10947
*/
 
10948
 
 
10949
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
 
10950
#define AVG_STRING_LENGTH_TO_PACK_ROWS   64
 
10951
#define RATIO_TO_PACK_ROWS             2
 
10952
#define MIN_STRING_LENGTH_TO_PACK_ROWS   10
 
10953
 
 
10954
TABLE *
 
10955
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
 
10956
                 ORDER *group, bool distinct, bool save_sum_fields,
 
10957
                 ulonglong select_options, ha_rows rows_limit,
 
10958
                 char *table_alias)
 
10959
{
 
10960
  MEM_ROOT *mem_root_save, own_root;
 
10961
  TABLE *table;
 
10962
  TABLE_SHARE *share;
 
10963
  uint  i,field_count,null_count,null_pack_length;
 
10964
  uint  copy_func_count= param->func_count;
 
10965
  uint  hidden_null_count, hidden_null_pack_length, hidden_field_count;
 
10966
  uint  blob_count,group_null_items, string_count;
 
10967
  uint  temp_pool_slot=MY_BIT_NONE;
 
10968
  uint fieldnr= 0;
 
10969
  ulong reclength, string_total_length;
 
10970
  bool  using_unique_constraint= 0;
 
10971
  bool  use_packed_rows= 0;
 
10972
  bool  not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
 
10973
  char  *tmpname,path[FN_REFLEN];
 
10974
  uchar *pos, *group_buff, *bitmaps;
 
10975
  uchar *null_flags;
 
10976
  Field **reg_field, **from_field, **default_field;
 
10977
  uint *blob_field;
 
10978
  Copy_field *copy=0;
 
10979
  KEY *keyinfo;
 
10980
  KEY_PART_INFO *key_part_info;
 
10981
  Item **copy_func;
 
10982
  MI_COLUMNDEF *recinfo;
 
10983
  uint total_uneven_bit_length= 0;
 
10984
  bool force_copy_fields= param->force_copy_fields;
 
10985
 
 
10986
  status_var_increment(thd->status_var.created_tmp_tables);
 
10987
 
 
10988
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
 
10989
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
 
10990
 
 
10991
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
 
10992
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
 
10993
            current_pid, temp_pool_slot);
 
10994
  else
 
10995
  {
 
10996
    /* if we run out of slots or we are not using tempool */
 
10997
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
 
10998
            thd->thread_id, thd->tmp_table++);
 
10999
  }
 
11000
 
 
11001
  /*
 
11002
    No need to change table name to lower case as we are only creating
 
11003
    MyISAM or HEAP tables here
 
11004
  */
 
11005
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
 
11006
 
 
11007
 
 
11008
  if (group)
 
11009
  {
 
11010
    if (!param->quick_group)
 
11011
      group=0;                                  // Can't use group key
 
11012
    else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
 
11013
    {
 
11014
      /*
 
11015
        marker == 4 means two things:
 
11016
        - store NULLs in the key, and
 
11017
        - convert BIT fields to 64-bit long, needed because MEMORY tables
 
11018
          can't index BIT fields.
 
11019
      */
 
11020
      (*tmp->item)->marker= 4;
 
11021
      if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
 
11022
        using_unique_constraint=1;
 
11023
    }
 
11024
    if (param->group_length >= MAX_BLOB_WIDTH)
 
11025
      using_unique_constraint=1;
 
11026
    if (group)
 
11027
      distinct=0;                               // Can't use distinct
 
11028
  }
 
11029
 
 
11030
  field_count=param->field_count+param->func_count+param->sum_func_count;
 
11031
  hidden_field_count=param->hidden_field_count;
 
11032
 
 
11033
  /*
 
11034
    When loose index scan is employed as access method, it already
 
11035
    computes all groups and the result of all aggregate functions. We
 
11036
    make space for the items of the aggregate function in the list of
 
11037
    functions TMP_TABLE_PARAM::items_to_copy, so that the values of
 
11038
    these items are stored in the temporary table.
 
11039
  */
 
11040
  if (param->precomputed_group_by)
 
11041
    copy_func_count+= param->sum_func_count;
 
11042
  
 
11043
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
11044
 
 
11045
  if (!multi_alloc_root(&own_root,
 
11046
                        &table, sizeof(*table),
 
11047
                        &share, sizeof(*share),
 
11048
                        &reg_field, sizeof(Field*) * (field_count+1),
 
11049
                        &default_field, sizeof(Field*) * (field_count),
 
11050
                        &blob_field, sizeof(uint)*(field_count+1),
 
11051
                        &from_field, sizeof(Field*)*field_count,
 
11052
                        &copy_func, sizeof(*copy_func)*(copy_func_count+1),
 
11053
                        &param->keyinfo, sizeof(*param->keyinfo),
 
11054
                        &key_part_info,
 
11055
                        sizeof(*key_part_info)*(param->group_parts+1),
 
11056
                        &param->start_recinfo,
 
11057
                        sizeof(*param->recinfo)*(field_count*2+4),
 
11058
                        &tmpname, (uint) strlen(path)+1,
 
11059
                        &group_buff, (group && ! using_unique_constraint ?
 
11060
                                      param->group_length : 0),
 
11061
                        &bitmaps, bitmap_buffer_size(field_count)*2,
 
11062
                        NullS))
 
11063
  {
 
11064
    if (temp_pool_slot != MY_BIT_NONE)
 
11065
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11066
    return(NULL);                               /* purecov: inspected */
 
11067
  }
 
11068
  /* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
 
11069
  if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
 
11070
  {
 
11071
    if (temp_pool_slot != MY_BIT_NONE)
 
11072
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11073
    free_root(&own_root, MYF(0));               /* purecov: inspected */
 
11074
    return(NULL);                               /* purecov: inspected */
 
11075
  }
 
11076
  param->items_to_copy= copy_func;
 
11077
  strmov(tmpname,path);
 
11078
  /* make table according to fields */
 
11079
 
 
11080
  bzero((char*) table,sizeof(*table));
 
11081
  bzero((char*) reg_field,sizeof(Field*)*(field_count+1));
 
11082
  bzero((char*) default_field, sizeof(Field*) * (field_count));
 
11083
  bzero((char*) from_field,sizeof(Field*)*field_count);
 
11084
 
 
11085
  table->mem_root= own_root;
 
11086
  mem_root_save= thd->mem_root;
 
11087
  thd->mem_root= &table->mem_root;
 
11088
 
 
11089
  table->field=reg_field;
 
11090
  table->alias= table_alias;
 
11091
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
 
11092
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
 
11093
  table->map=1;
 
11094
  table->temp_pool_slot = temp_pool_slot;
 
11095
  table->copy_blobs= 1;
 
11096
  table->in_use= thd;
 
11097
  table->quick_keys.init();
 
11098
  table->covering_keys.init();
 
11099
  table->keys_in_use_for_query.init();
 
11100
 
 
11101
  table->s= share;
 
11102
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
 
11103
  share->blob_field= blob_field;
 
11104
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
11105
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
 
11106
  share->table_charset= param->table_charset;
 
11107
  share->primary_key= MAX_KEY;               // Indicate no primary key
 
11108
  share->keys_for_keyread.init();
 
11109
  share->keys_in_use.init();
 
11110
 
 
11111
  /* Calculate which type of fields we will store in the temporary table */
 
11112
 
 
11113
  reclength= string_total_length= 0;
 
11114
  blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
 
11115
  param->using_indirect_summary_function=0;
 
11116
 
 
11117
  List_iterator_fast<Item> li(fields);
 
11118
  Item *item;
 
11119
  Field **tmp_from_field=from_field;
 
11120
  while ((item=li++))
 
11121
  {
 
11122
    Item::Type type=item->type();
 
11123
    if (not_all_columns)
 
11124
    {
 
11125
      if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
 
11126
      {
 
11127
        if (item->used_tables() & OUTER_REF_TABLE_BIT)
 
11128
          item->update_used_tables();
 
11129
        if (type == Item::SUBSELECT_ITEM ||
 
11130
            (item->used_tables() & ~OUTER_REF_TABLE_BIT))
 
11131
        {
 
11132
          /*
 
11133
            Mark that the we have ignored an item that refers to a summary
 
11134
            function. We need to know this if someone is going to use
 
11135
            DISTINCT on the result.
 
11136
          */
 
11137
          param->using_indirect_summary_function=1;
 
11138
          continue;
 
11139
        }
 
11140
      }
 
11141
      if (item->const_item() && (int) hidden_field_count <= 0)
 
11142
        continue; // We don't have to store this
 
11143
    }
 
11144
    if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
 
11145
    {                                           /* Can't calc group yet */
 
11146
      ((Item_sum*) item)->result_field=0;
 
11147
      for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
 
11148
      {
 
11149
        Item **argp= ((Item_sum*) item)->args + i;
 
11150
        Item *arg= *argp;
 
11151
        if (!arg->const_item())
 
11152
        {
 
11153
          Field *new_field=
 
11154
            create_tmp_field(thd, table, arg, arg->type(), &copy_func,
 
11155
                             tmp_from_field, &default_field[fieldnr],
 
11156
                             group != 0,not_all_columns,
 
11157
                             distinct, 0,
 
11158
                             param->convert_blob_length);
 
11159
          if (!new_field)
 
11160
            goto err;                                   // Should be OOM
 
11161
          tmp_from_field++;
 
11162
          reclength+=new_field->pack_length();
 
11163
          if (new_field->flags & BLOB_FLAG)
 
11164
          {
 
11165
            *blob_field++= fieldnr;
 
11166
            blob_count++;
 
11167
          }
 
11168
          *(reg_field++)= new_field;
 
11169
          if (new_field->real_type() == MYSQL_TYPE_STRING ||
 
11170
              new_field->real_type() == MYSQL_TYPE_VARCHAR)
 
11171
          {
 
11172
            string_count++;
 
11173
            string_total_length+= new_field->pack_length();
 
11174
          }
 
11175
          thd->mem_root= mem_root_save;
 
11176
          thd->change_item_tree(argp, new Item_field(new_field));
 
11177
          thd->mem_root= &table->mem_root;
 
11178
          if (!(new_field->flags & NOT_NULL_FLAG))
 
11179
          {
 
11180
            null_count++;
 
11181
            /*
 
11182
              new_field->maybe_null() is still false, it will be
 
11183
              changed below. But we have to setup Item_field correctly
 
11184
            */
 
11185
            (*argp)->maybe_null=1;
 
11186
          }
 
11187
          new_field->field_index= fieldnr++;
 
11188
        }
 
11189
      }
 
11190
    }
 
11191
    else
 
11192
    {
 
11193
      /*
 
11194
        The last parameter to create_tmp_field() is a bit tricky:
 
11195
 
 
11196
        We need to set it to 0 in union, to get fill_record() to modify the
 
11197
        temporary table.
 
11198
        We need to set it to 1 on multi-table-update and in select to
 
11199
        write rows to the temporary table.
 
11200
        We here distinguish between UNION and multi-table-updates by the fact
 
11201
        that in the later case group is set to the row pointer.
 
11202
      */
 
11203
      Field *new_field= (param->schema_table) ?
 
11204
        create_tmp_field_for_schema(thd, item, table) :
 
11205
        create_tmp_field(thd, table, item, type, &copy_func,
 
11206
                         tmp_from_field, &default_field[fieldnr],
 
11207
                         group != 0,
 
11208
                         !force_copy_fields &&
 
11209
                           (not_all_columns || group !=0),
 
11210
                         /*
 
11211
                           If item->marker == 4 then we force create_tmp_field
 
11212
                           to create a 64-bit longs for BIT fields because HEAP
 
11213
                           tables can't index BIT fields directly. We do the same
 
11214
                           for distinct, as we want the distinct index to be
 
11215
                           usable in this case too.
 
11216
                         */
 
11217
                         item->marker == 4 || param->bit_fields_as_long,
 
11218
                         force_copy_fields,
 
11219
                         param->convert_blob_length);
 
11220
 
 
11221
      if (!new_field)
 
11222
      {
 
11223
        if (thd->is_fatal_error)
 
11224
          goto err;                             // Got OOM
 
11225
        continue;                               // Some kindf of const item
 
11226
      }
 
11227
      if (type == Item::SUM_FUNC_ITEM)
 
11228
        ((Item_sum *) item)->result_field= new_field;
 
11229
      tmp_from_field++;
 
11230
      reclength+=new_field->pack_length();
 
11231
      if (!(new_field->flags & NOT_NULL_FLAG))
 
11232
        null_count++;
 
11233
      if (new_field->flags & BLOB_FLAG)
 
11234
      {
 
11235
        *blob_field++= fieldnr;
 
11236
        blob_count++;
 
11237
      }
 
11238
      if (item->marker == 4 && item->maybe_null)
 
11239
      {
 
11240
        group_null_items++;
 
11241
        new_field->flags|= GROUP_FLAG;
 
11242
      }
 
11243
      new_field->field_index= fieldnr++;
 
11244
      *(reg_field++)= new_field;
 
11245
    }
 
11246
    if (!--hidden_field_count)
 
11247
    {
 
11248
      /*
 
11249
        This was the last hidden field; Remember how many hidden fields could
 
11250
        have null
 
11251
      */
 
11252
      hidden_null_count=null_count;
 
11253
      /*
 
11254
        We need to update hidden_field_count as we may have stored group
 
11255
        functions with constant arguments
 
11256
      */
 
11257
      param->hidden_field_count= fieldnr;
 
11258
      null_count= 0;
 
11259
    }
 
11260
  }
 
11261
  assert(fieldnr == (uint) (reg_field - table->field));
 
11262
  assert(field_count >= (uint) (reg_field - table->field));
 
11263
  field_count= fieldnr;
 
11264
  *reg_field= 0;
 
11265
  *blob_field= 0;                               // End marker
 
11266
  share->fields= field_count;
 
11267
 
 
11268
  /* If result table is small; use a heap */
 
11269
  /* future: storage engine selection can be made dynamic? */
 
11270
  if (blob_count || using_unique_constraint ||
 
11271
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
 
11272
      OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
 
11273
  {
 
11274
    share->db_plugin= ha_lock_engine(0, myisam_hton);
 
11275
    table->file= get_new_handler(share, &table->mem_root,
 
11276
                                 share->db_type());
 
11277
    if (group &&
 
11278
        (param->group_parts > table->file->max_key_parts() ||
 
11279
         param->group_length > table->file->max_key_length()))
 
11280
      using_unique_constraint=1;
 
11281
  }
 
11282
  else
 
11283
  {
 
11284
    share->db_plugin= ha_lock_engine(0, heap_hton);
 
11285
    table->file= get_new_handler(share, &table->mem_root,
 
11286
                                 share->db_type());
 
11287
  }
 
11288
  if (!table->file)
 
11289
    goto err;
 
11290
 
 
11291
 
 
11292
  if (!using_unique_constraint)
 
11293
    reclength+= group_null_items;       // null flag is stored separately
 
11294
 
 
11295
  share->blob_fields= blob_count;
 
11296
  if (blob_count == 0)
 
11297
  {
 
11298
    /* We need to ensure that first byte is not 0 for the delete link */
 
11299
    if (param->hidden_field_count)
 
11300
      hidden_null_count++;
 
11301
    else
 
11302
      null_count++;
 
11303
  }
 
11304
  hidden_null_pack_length=(hidden_null_count+7)/8;
 
11305
  null_pack_length= (hidden_null_pack_length +
 
11306
                     (null_count + total_uneven_bit_length + 7) / 8);
 
11307
  reclength+=null_pack_length;
 
11308
  if (!reclength)
 
11309
    reclength=1;                                // Dummy select
 
11310
  /* Use packed rows if there is blobs or a lot of space to gain */
 
11311
  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)))
 
11312
    use_packed_rows= 1;
 
11313
 
 
11314
  share->reclength= reclength;
 
11315
  {
 
11316
    uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
 
11317
    share->rec_buff_length= alloc_length;
 
11318
    if (!(table->record[0]= (uchar*)
 
11319
                            alloc_root(&table->mem_root, alloc_length*3)))
 
11320
      goto err;
 
11321
    table->record[1]= table->record[0]+alloc_length;
 
11322
    share->default_values= table->record[1]+alloc_length;
 
11323
  }
 
11324
  copy_func[0]=0;                               // End marker
 
11325
  param->func_count= copy_func - param->items_to_copy; 
 
11326
 
 
11327
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
11328
 
 
11329
  recinfo=param->start_recinfo;
 
11330
  null_flags=(uchar*) table->record[0];
 
11331
  pos=table->record[0]+ null_pack_length;
 
11332
  if (null_pack_length)
 
11333
  {
 
11334
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11335
    recinfo->type=FIELD_NORMAL;
 
11336
    recinfo->length=null_pack_length;
 
11337
    recinfo++;
 
11338
    bfill(null_flags,null_pack_length,255);     // Set null fields
 
11339
 
 
11340
    table->null_flags= (uchar*) table->record[0];
 
11341
    share->null_fields= null_count+ hidden_null_count;
 
11342
    share->null_bytes= null_pack_length;
 
11343
  }
 
11344
  null_count= (blob_count == 0) ? 1 : 0;
 
11345
  hidden_field_count=param->hidden_field_count;
 
11346
  for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
 
11347
  {
 
11348
    Field *field= *reg_field;
 
11349
    uint length;
 
11350
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11351
 
 
11352
    if (!(field->flags & NOT_NULL_FLAG))
 
11353
    {
 
11354
      if (field->flags & GROUP_FLAG && !using_unique_constraint)
 
11355
      {
 
11356
        /*
 
11357
          We have to reserve one byte here for NULL bits,
 
11358
          as this is updated by 'end_update()'
 
11359
        */
 
11360
        *pos++=0;                               // Null is stored here
 
11361
        recinfo->length=1;
 
11362
        recinfo->type=FIELD_NORMAL;
 
11363
        recinfo++;
 
11364
        bzero((uchar*) recinfo,sizeof(*recinfo));
 
11365
      }
 
11366
      else
 
11367
      {
 
11368
        recinfo->null_bit= 1 << (null_count & 7);
 
11369
        recinfo->null_pos= null_count/8;
 
11370
      }
 
11371
      field->move_field(pos,null_flags+null_count/8,
 
11372
                        1 << (null_count & 7));
 
11373
      null_count++;
 
11374
    }
 
11375
    else
 
11376
      field->move_field(pos,(uchar*) 0,0);
 
11377
    field->reset();
 
11378
 
 
11379
    /*
 
11380
      Test if there is a default field value. The test for ->ptr is to skip
 
11381
      'offset' fields generated by initalize_tables
 
11382
    */
 
11383
    if (default_field[i] && default_field[i]->ptr)
 
11384
    {
 
11385
      /* 
 
11386
         default_field[i] is set only in the cases  when 'field' can
 
11387
         inherit the default value that is defined for the field referred
 
11388
         by the Item_field object from which 'field' has been created.
 
11389
      */
 
11390
      my_ptrdiff_t diff;
 
11391
      Field *orig_field= default_field[i];
 
11392
      /* Get the value from default_values */
 
11393
      diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
 
11394
                            orig_field->table->record[0]);
 
11395
      orig_field->move_field_offset(diff);      // Points now at default_values
 
11396
      if (orig_field->is_real_null())
 
11397
        field->set_null();
 
11398
      else
 
11399
      {
 
11400
        field->set_notnull();
 
11401
        memcpy(field->ptr, orig_field->ptr, field->pack_length());
 
11402
      }
 
11403
      orig_field->move_field_offset(-diff);     // Back to record[0]
 
11404
    } 
 
11405
 
 
11406
    if (from_field[i])
 
11407
    {                                           /* Not a table Item */
 
11408
      copy->set(field,from_field[i],save_sum_fields);
 
11409
      copy++;
 
11410
    }
 
11411
    length=field->pack_length();
 
11412
    pos+= length;
 
11413
 
 
11414
    /* Make entry for create table */
 
11415
    recinfo->length=length;
 
11416
    if (field->flags & BLOB_FLAG)
 
11417
      recinfo->type= (int) FIELD_BLOB;
 
11418
    else if (use_packed_rows &&
 
11419
             field->real_type() == MYSQL_TYPE_STRING &&
 
11420
             length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
 
11421
      recinfo->type=FIELD_SKIP_ENDSPACE;
 
11422
    else
 
11423
      recinfo->type=FIELD_NORMAL;
 
11424
    if (!--hidden_field_count)
 
11425
      null_count=(null_count+7) & ~7;           // move to next byte
 
11426
 
 
11427
    // fix table name in field entry
 
11428
    field->table_name= &table->alias;
 
11429
  }
 
11430
 
 
11431
  param->copy_field_end=copy;
 
11432
  param->recinfo=recinfo;
 
11433
  store_record(table,s->default_values);        // Make empty default record
 
11434
 
 
11435
  if (thd->variables.tmp_table_size == ~ (ulonglong) 0)         // No limit
 
11436
    share->max_rows= ~(ha_rows) 0;
 
11437
  else
 
11438
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
 
11439
                                 min(thd->variables.tmp_table_size,
 
11440
                                     thd->variables.max_heap_table_size) :
 
11441
                                 thd->variables.tmp_table_size) /
 
11442
                                 share->reclength);
 
11443
  set_if_bigger(share->max_rows,1);             // For dummy start options
 
11444
  /*
 
11445
    Push the LIMIT clause to the temporary table creation, so that we
 
11446
    materialize only up to 'rows_limit' records instead of all result records.
 
11447
  */
 
11448
  set_if_smaller(share->max_rows, rows_limit);
 
11449
  param->end_write_records= rows_limit;
 
11450
 
 
11451
  keyinfo= param->keyinfo;
 
11452
 
 
11453
  if (group)
 
11454
  {
 
11455
    table->group=group;                         /* Table is grouped by key */
 
11456
    param->group_buff=group_buff;
 
11457
    share->keys=1;
 
11458
    share->uniques= test(using_unique_constraint);
 
11459
    table->key_info=keyinfo;
 
11460
    keyinfo->key_part=key_part_info;
 
11461
    keyinfo->flags=HA_NOSAME;
 
11462
    keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
 
11463
    keyinfo->key_length=0;
 
11464
    keyinfo->rec_per_key=0;
 
11465
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11466
    keyinfo->name= (char*) "group_key";
 
11467
    ORDER *cur_group= group;
 
11468
    for (; cur_group ; cur_group= cur_group->next, key_part_info++)
 
11469
    {
 
11470
      Field *field=(*cur_group->item)->get_tmp_table_field();
 
11471
      bool maybe_null=(*cur_group->item)->maybe_null;
 
11472
      key_part_info->null_bit=0;
 
11473
      key_part_info->field=  field;
 
11474
      key_part_info->offset= field->offset(table->record[0]);
 
11475
      key_part_info->length= (uint16) field->key_length();
 
11476
      key_part_info->type=   (uint8) field->key_type();
 
11477
      key_part_info->key_type =
 
11478
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
 
11479
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
 
11480
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
 
11481
        0 : FIELDFLAG_BINARY;
 
11482
      if (!using_unique_constraint)
 
11483
      {
 
11484
        cur_group->buff=(char*) group_buff;
 
11485
        if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
 
11486
                                                     group_buff +
 
11487
                                                     test(maybe_null),
 
11488
                                                     field->null_ptr,
 
11489
                                                     field->null_bit)))
 
11490
          goto err; /* purecov: inspected */
 
11491
        if (maybe_null)
 
11492
        {
 
11493
          /*
 
11494
            To be able to group on NULL, we reserved place in group_buff
 
11495
            for the NULL flag just before the column. (see above).
 
11496
            The field data is after this flag.
 
11497
            The NULL flag is updated in 'end_update()' and 'end_write()'
 
11498
          */
 
11499
          keyinfo->flags|= HA_NULL_ARE_EQUAL;   // def. that NULL == NULL
 
11500
          key_part_info->null_bit=field->null_bit;
 
11501
          key_part_info->null_offset= (uint) (field->null_ptr -
 
11502
                                              (uchar*) table->record[0]);
 
11503
          cur_group->buff++;                        // Pointer to field data
 
11504
          group_buff++;                         // Skipp null flag
 
11505
        }
 
11506
        /* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
 
11507
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
 
11508
        group_buff+= cur_group->field->pack_length();
 
11509
      }
 
11510
      keyinfo->key_length+=  key_part_info->length;
 
11511
    }
 
11512
  }
 
11513
 
 
11514
  if (distinct && field_count != param->hidden_field_count)
 
11515
  {
 
11516
    /*
 
11517
      Create an unique key or an unique constraint over all columns
 
11518
      that should be in the result.  In the temporary table, there are
 
11519
      'param->hidden_field_count' extra columns, whose null bits are stored
 
11520
      in the first 'hidden_null_pack_length' bytes of the row.
 
11521
    */
 
11522
    if (blob_count)
 
11523
    {
 
11524
      /*
 
11525
        Special mode for index creation in MyISAM used to support unique
 
11526
        indexes on blobs with arbitrary length. Such indexes cannot be
 
11527
        used for lookups.
 
11528
      */
 
11529
      share->uniques= 1;
 
11530
    }
 
11531
    null_pack_length-=hidden_null_pack_length;
 
11532
    keyinfo->key_parts= ((field_count-param->hidden_field_count)+
 
11533
                         (share->uniques ? test(null_pack_length) : 0));
 
11534
    table->distinct= 1;
 
11535
    share->keys= 1;
 
11536
    if (!(key_part_info= (KEY_PART_INFO*)
 
11537
          alloc_root(&table->mem_root,
 
11538
                     keyinfo->key_parts * sizeof(KEY_PART_INFO))))
 
11539
      goto err;
 
11540
    bzero((void*) key_part_info, keyinfo->key_parts * sizeof(KEY_PART_INFO));
 
11541
    table->key_info=keyinfo;
 
11542
    keyinfo->key_part=key_part_info;
 
11543
    keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
 
11544
    keyinfo->key_length=(uint16) reclength;
 
11545
    keyinfo->name= (char*) "distinct_key";
 
11546
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11547
    keyinfo->rec_per_key=0;
 
11548
 
 
11549
    /*
 
11550
      Create an extra field to hold NULL bits so that unique indexes on
 
11551
      blobs can distinguish NULL from 0. This extra field is not needed
 
11552
      when we do not use UNIQUE indexes for blobs.
 
11553
    */
 
11554
    if (null_pack_length && share->uniques)
 
11555
    {
 
11556
      key_part_info->null_bit=0;
 
11557
      key_part_info->offset=hidden_null_pack_length;
 
11558
      key_part_info->length=null_pack_length;
 
11559
      key_part_info->field= new Field_string(table->record[0],
 
11560
                                             (uint32) key_part_info->length,
 
11561
                                             (uchar*) 0,
 
11562
                                             (uint) 0,
 
11563
                                             Field::NONE,
 
11564
                                             NullS, &my_charset_bin);
 
11565
      if (!key_part_info->field)
 
11566
        goto err;
 
11567
      key_part_info->field->init(table);
 
11568
      key_part_info->key_type=FIELDFLAG_BINARY;
 
11569
      key_part_info->type=    HA_KEYTYPE_BINARY;
 
11570
      key_part_info++;
 
11571
    }
 
11572
    /* Create a distinct key over the columns we are going to return */
 
11573
    for (i=param->hidden_field_count, reg_field=table->field + i ;
 
11574
         i < field_count;
 
11575
         i++, reg_field++, key_part_info++)
 
11576
    {
 
11577
      key_part_info->null_bit=0;
 
11578
      key_part_info->field=    *reg_field;
 
11579
      key_part_info->offset=   (*reg_field)->offset(table->record[0]);
 
11580
      key_part_info->length=   (uint16) (*reg_field)->pack_length();
 
11581
      /* TODO:
 
11582
        The below method of computing the key format length of the
 
11583
        key part is a copy/paste from opt_range.cc, and table.cc.
 
11584
        This should be factored out, e.g. as a method of Field.
 
11585
        In addition it is not clear if any of the Field::*_length
 
11586
        methods is supposed to compute the same length. If so, it
 
11587
        might be reused.
 
11588
      */
 
11589
      key_part_info->store_length= key_part_info->length;
 
11590
 
 
11591
      if ((*reg_field)->real_maybe_null())
 
11592
        key_part_info->store_length+= HA_KEY_NULL_LENGTH;
 
11593
      if ((*reg_field)->type() == MYSQL_TYPE_BLOB || 
 
11594
          (*reg_field)->real_type() == MYSQL_TYPE_VARCHAR)
 
11595
        key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
 
11596
 
 
11597
      key_part_info->type=     (uint8) (*reg_field)->key_type();
 
11598
      key_part_info->key_type =
 
11599
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
 
11600
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
 
11601
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
 
11602
        0 : FIELDFLAG_BINARY;
 
11603
    }
 
11604
  }
 
11605
 
 
11606
  if (thd->is_fatal_error)                              // If end of memory
 
11607
    goto err;                                    /* purecov: inspected */
 
11608
  share->db_record_offset= 1;
 
11609
  if (share->db_type() == myisam_hton)
 
11610
  {
 
11611
    if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
 
11612
                                &param->recinfo, select_options))
 
11613
      goto err;
 
11614
  }
 
11615
  if (open_tmp_table(table))
 
11616
    goto err;
 
11617
 
 
11618
  thd->mem_root= mem_root_save;
 
11619
 
 
11620
  return(table);
 
11621
 
 
11622
err:
 
11623
  thd->mem_root= mem_root_save;
 
11624
  free_tmp_table(thd,table);                    /* purecov: inspected */
 
11625
  if (temp_pool_slot != MY_BIT_NONE)
 
11626
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11627
  return(NULL);                         /* purecov: inspected */
 
11628
}
 
11629
 
 
11630
 
 
11631
 
 
11632
 
 
11633
/*
 
11634
  Create a temporary table to weed out duplicate rowid combinations
 
11635
 
 
11636
  SYNOPSIS
 
11637
 
 
11638
    create_duplicate_weedout_tmp_table()
 
11639
      thd
 
11640
      uniq_tuple_length_arg
 
11641
      SJ_TMP_TABLE 
 
11642
 
 
11643
  DESCRIPTION
 
11644
    Create a temporary table to weed out duplicate rowid combinations. The
 
11645
    table has a single column that is a concatenation of all rowids in the
 
11646
    combination. 
 
11647
 
 
11648
    Depending on the needed length, there are two cases:
 
11649
 
 
11650
    1. When the length of the column < max_key_length:
 
11651
 
 
11652
      CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
 
11653
 
 
11654
    2. Otherwise (not a valid SQL syntax but internally supported):
 
11655
 
 
11656
      CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
 
11657
 
 
11658
    The code in this function was produced by extraction of relevant parts
 
11659
    from create_tmp_table().
 
11660
 
 
11661
  RETURN
 
11662
    created table
 
11663
    NULL on error
 
11664
*/
 
11665
 
 
11666
TABLE *create_duplicate_weedout_tmp_table(THD *thd, 
 
11667
                                          uint uniq_tuple_length_arg,
 
11668
                                          SJ_TMP_TABLE *sjtbl)
 
11669
{
 
11670
  MEM_ROOT *mem_root_save, own_root;
 
11671
  TABLE *table;
 
11672
  TABLE_SHARE *share;
 
11673
  uint  temp_pool_slot=MY_BIT_NONE;
 
11674
  char  *tmpname,path[FN_REFLEN];
 
11675
  Field **reg_field;
 
11676
  KEY_PART_INFO *key_part_info;
 
11677
  KEY *keyinfo;
 
11678
  uchar *group_buff;
 
11679
  uchar *bitmaps;
 
11680
  uint *blob_field;
 
11681
  MI_COLUMNDEF *recinfo, *start_recinfo;
 
11682
  bool using_unique_constraint=false;
 
11683
  bool use_packed_rows= false;
 
11684
  Field *field, *key_field;
 
11685
  uint blob_count, null_pack_length, null_count;
 
11686
  uchar *null_flags;
 
11687
  uchar *pos;
 
11688
  
 
11689
  /*
 
11690
    STEP 1: Get temporary table name
 
11691
  */
 
11692
  statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
 
11693
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
 
11694
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
 
11695
 
 
11696
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
 
11697
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
 
11698
            current_pid, temp_pool_slot);
 
11699
  else
 
11700
  {
 
11701
    /* if we run out of slots or we are not using tempool */
 
11702
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
 
11703
            thd->thread_id, thd->tmp_table++);
 
11704
  }
 
11705
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
 
11706
 
 
11707
  /* STEP 2: Figure if we'll be using a key or blob+constraint */
 
11708
  if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
 
11709
    using_unique_constraint= true;
 
11710
 
 
11711
  /* STEP 3: Allocate memory for temptable description */
 
11712
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
11713
  if (!multi_alloc_root(&own_root,
 
11714
                        &table, sizeof(*table),
 
11715
                        &share, sizeof(*share),
 
11716
                        &reg_field, sizeof(Field*) * (1+1),
 
11717
                        &blob_field, sizeof(uint)*2,
 
11718
                        &keyinfo, sizeof(*keyinfo),
 
11719
                        &key_part_info, sizeof(*key_part_info) * 2,
 
11720
                        &start_recinfo,
 
11721
                        sizeof(*recinfo)*(1*2+4),
 
11722
                        &tmpname, (uint) strlen(path)+1,
 
11723
                        &group_buff, (!using_unique_constraint ?
 
11724
                                      uniq_tuple_length_arg : 0),
 
11725
                        &bitmaps, bitmap_buffer_size(1)*2,
 
11726
                        NullS))
 
11727
  {
 
11728
    if (temp_pool_slot != MY_BIT_NONE)
 
11729
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11730
    return(NULL);
 
11731
  }
 
11732
  strmov(tmpname,path);
 
11733
  
 
11734
 
 
11735
  /* STEP 4: Create TABLE description */
 
11736
  bzero((char*) table,sizeof(*table));
 
11737
  bzero((char*) reg_field,sizeof(Field*)*2);
 
11738
 
 
11739
  table->mem_root= own_root;
 
11740
  mem_root_save= thd->mem_root;
 
11741
  thd->mem_root= &table->mem_root;
 
11742
 
 
11743
  table->field=reg_field;
 
11744
  table->alias= "weedout-tmp";
 
11745
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
 
11746
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
 
11747
  table->map=1;
 
11748
  table->temp_pool_slot = temp_pool_slot;
 
11749
  table->copy_blobs= 1;
 
11750
  table->in_use= thd;
 
11751
  table->quick_keys.init();
 
11752
  table->covering_keys.init();
 
11753
  table->keys_in_use_for_query.init();
 
11754
 
 
11755
  table->s= share;
 
11756
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
 
11757
  share->blob_field= blob_field;
 
11758
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
11759
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
 
11760
  share->table_charset= NULL;
 
11761
  share->primary_key= MAX_KEY;               // Indicate no primary key
 
11762
  share->keys_for_keyread.init();
 
11763
  share->keys_in_use.init();
 
11764
 
 
11765
  blob_count= 0;
 
11766
 
 
11767
  /* Create the field */
 
11768
  {
 
11769
    /*
 
11770
      For the sake of uniformity, always use Field_varstring (altough we could
 
11771
      use Field_string for shorter keys)
 
11772
    */
 
11773
    field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
 
11774
                               &my_charset_bin);
 
11775
    if (!field)
 
11776
      return(0);
 
11777
    field->table= table;
 
11778
    field->key_start.init(0);
 
11779
    field->part_of_key.init(0);
 
11780
    field->part_of_sortkey.init(0);
 
11781
    field->unireg_check= Field::NONE;
 
11782
    field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
 
11783
    field->reset_fields();
 
11784
    field->init(table);
 
11785
    field->orig_table= NULL;
 
11786
     
 
11787
    field->field_index= 0;
 
11788
    
 
11789
    *(reg_field++)= field;
 
11790
    *blob_field= 0;
 
11791
    *reg_field= 0;
 
11792
 
 
11793
    share->fields= 1;
 
11794
    share->blob_fields= 0;
 
11795
  }
 
11796
 
 
11797
  uint reclength= field->pack_length();
 
11798
  if (using_unique_constraint)
 
11799
  { 
 
11800
    share->db_plugin= ha_lock_engine(0, myisam_hton);
 
11801
    table->file= get_new_handler(share, &table->mem_root,
 
11802
                                 share->db_type());
 
11803
    assert(uniq_tuple_length_arg <= table->file->max_key_length());
 
11804
  }
 
11805
  else
 
11806
  {
 
11807
    share->db_plugin= ha_lock_engine(0, heap_hton);
 
11808
    table->file= get_new_handler(share, &table->mem_root,
 
11809
                                 share->db_type());
 
11810
  }
 
11811
  if (!table->file)
 
11812
    goto err;
 
11813
 
 
11814
  null_count=1;
 
11815
  
 
11816
  null_pack_length= 1;
 
11817
  reclength += null_pack_length;
 
11818
 
 
11819
  share->reclength= reclength;
 
11820
  {
 
11821
    uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
 
11822
    share->rec_buff_length= alloc_length;
 
11823
    if (!(table->record[0]= (uchar*)
 
11824
                            alloc_root(&table->mem_root, alloc_length*3)))
 
11825
      goto err;
 
11826
    table->record[1]= table->record[0]+alloc_length;
 
11827
    share->default_values= table->record[1]+alloc_length;
 
11828
  }
 
11829
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
11830
 
 
11831
  recinfo= start_recinfo;
 
11832
  null_flags=(uchar*) table->record[0];
 
11833
  pos=table->record[0]+ null_pack_length;
 
11834
  if (null_pack_length)
 
11835
  {
 
11836
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11837
    recinfo->type=FIELD_NORMAL;
 
11838
    recinfo->length=null_pack_length;
 
11839
    recinfo++;
 
11840
    bfill(null_flags,null_pack_length,255);     // Set null fields
 
11841
 
 
11842
    table->null_flags= (uchar*) table->record[0];
 
11843
    share->null_fields= null_count;
 
11844
    share->null_bytes= null_pack_length;
 
11845
  }
 
11846
  null_count=1;
 
11847
 
 
11848
  {
 
11849
    //Field *field= *reg_field;
 
11850
    uint length;
 
11851
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11852
    field->move_field(pos,(uchar*) 0,0);
 
11853
 
 
11854
    field->reset();
 
11855
    /*
 
11856
      Test if there is a default field value. The test for ->ptr is to skip
 
11857
      'offset' fields generated by initalize_tables
 
11858
    */
 
11859
    // Initialize the table field:
 
11860
    bzero(field->ptr, field->pack_length());
 
11861
 
 
11862
    length=field->pack_length();
 
11863
    pos+= length;
 
11864
 
 
11865
    /* Make entry for create table */
 
11866
    recinfo->length=length;
 
11867
    if (field->flags & BLOB_FLAG)
 
11868
      recinfo->type= (int) FIELD_BLOB;
 
11869
    else if (use_packed_rows &&
 
11870
             field->real_type() == MYSQL_TYPE_STRING &&
 
11871
             length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
 
11872
      recinfo->type=FIELD_SKIP_ENDSPACE;
 
11873
    else
 
11874
      recinfo->type=FIELD_NORMAL;
 
11875
 
 
11876
    field->table_name= &table->alias;
 
11877
  }
 
11878
 
 
11879
  //param->recinfo=recinfo;
 
11880
  //store_record(table,s->default_values);        // Make empty default record
 
11881
 
 
11882
  if (thd->variables.tmp_table_size == ~ (ulonglong) 0)         // No limit
 
11883
    share->max_rows= ~(ha_rows) 0;
 
11884
  else
 
11885
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
 
11886
                                 min(thd->variables.tmp_table_size,
 
11887
                                     thd->variables.max_heap_table_size) :
 
11888
                                 thd->variables.tmp_table_size) /
 
11889
                                 share->reclength);
 
11890
  set_if_bigger(share->max_rows,1);             // For dummy start options
 
11891
 
 
11892
 
 
11893
  //// keyinfo= param->keyinfo;
 
11894
  if (true)
 
11895
  {
 
11896
    share->keys=1;
 
11897
    share->uniques= test(using_unique_constraint);
 
11898
    table->key_info=keyinfo;
 
11899
    keyinfo->key_part=key_part_info;
 
11900
    keyinfo->flags=HA_NOSAME;
 
11901
    keyinfo->usable_key_parts= keyinfo->key_parts= 1;
 
11902
    keyinfo->key_length=0;
 
11903
    keyinfo->rec_per_key=0;
 
11904
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11905
    keyinfo->name= (char*) "weedout_key";
 
11906
    {
 
11907
      key_part_info->null_bit=0;
 
11908
      key_part_info->field=  field;
 
11909
      key_part_info->offset= field->offset(table->record[0]);
 
11910
      key_part_info->length= (uint16) field->key_length();
 
11911
      key_part_info->type=   (uint8) field->key_type();
 
11912
      key_part_info->key_type = FIELDFLAG_BINARY;
 
11913
      if (!using_unique_constraint)
 
11914
      {
 
11915
        if (!(key_field= field->new_key_field(thd->mem_root, table,
 
11916
                                              group_buff,
 
11917
                                              field->null_ptr,
 
11918
                                              field->null_bit)))
 
11919
          goto err;
 
11920
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
 
11921
      }
 
11922
      keyinfo->key_length+=  key_part_info->length;
 
11923
    }
 
11924
  }
 
11925
 
 
11926
  if (thd->is_fatal_error)                              // If end of memory
 
11927
    goto err;
 
11928
  share->db_record_offset= 1;
 
11929
  if (share->db_type() == myisam_hton)
 
11930
  {
 
11931
    recinfo++;
 
11932
    if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
 
11933
      goto err;
 
11934
  }
 
11935
  sjtbl->start_recinfo= start_recinfo;
 
11936
  sjtbl->recinfo=       recinfo;
 
11937
  if (open_tmp_table(table))
 
11938
    goto err;
 
11939
 
 
11940
  thd->mem_root= mem_root_save;
 
11941
  return(table);
 
11942
 
 
11943
err:
 
11944
  thd->mem_root= mem_root_save;
 
11945
  free_tmp_table(thd,table);                    /* purecov: inspected */
 
11946
  if (temp_pool_slot != MY_BIT_NONE)
 
11947
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11948
  return(NULL);                         /* purecov: inspected */
 
11949
}
 
11950
 
 
11951
/****************************************************************************/
 
11952
 
 
11953
/**
 
11954
  Create a reduced TABLE object with properly set up Field list from a
 
11955
  list of field definitions.
 
11956
 
 
11957
    The created table doesn't have a table handler associated with
 
11958
    it, has no keys, no group/distinct, no copy_funcs array.
 
11959
    The sole purpose of this TABLE object is to use the power of Field
 
11960
    class to read/write data to/from table->record[0]. Then one can store
 
11961
    the record in any container (RB tree, hash, etc).
 
11962
    The table is created in THD mem_root, so are the table's fields.
 
11963
    Consequently, if you don't BLOB fields, you don't need to free it.
 
11964
 
 
11965
  @param thd         connection handle
 
11966
  @param field_list  list of column definitions
 
11967
 
 
11968
  @return
 
11969
    0 if out of memory, TABLE object in case of success
 
11970
*/
 
11971
 
 
11972
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
 
11973
{
 
11974
  uint field_count= field_list.elements;
 
11975
  uint blob_count= 0;
 
11976
  Field **field;
 
11977
  Create_field *cdef;                           /* column definition */
 
11978
  uint record_length= 0;
 
11979
  uint null_count= 0;                 /* number of columns which may be null */
 
11980
  uint null_pack_length;              /* NULL representation array length */
 
11981
  uint *blob_field;
 
11982
  uchar *bitmaps;
 
11983
  TABLE *table;
 
11984
  TABLE_SHARE *share;
 
11985
 
 
11986
  if (!multi_alloc_root(thd->mem_root,
 
11987
                        &table, sizeof(*table),
 
11988
                        &share, sizeof(*share),
 
11989
                        &field, (field_count + 1) * sizeof(Field*),
 
11990
                        &blob_field, (field_count+1) *sizeof(uint),
 
11991
                        &bitmaps, bitmap_buffer_size(field_count)*2,
 
11992
                        NullS))
 
11993
    return 0;
 
11994
 
 
11995
  bzero(table, sizeof(*table));
 
11996
  bzero(share, sizeof(*share));
 
11997
  table->field= field;
 
11998
  table->s= share;
 
11999
  share->blob_field= blob_field;
 
12000
  share->fields= field_count;
 
12001
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
12002
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
12003
 
 
12004
  /* Create all fields and calculate the total length of record */
 
12005
  List_iterator_fast<Create_field> it(field_list);
 
12006
  while ((cdef= it++))
 
12007
  {
 
12008
    *field= make_field(share, 0, cdef->length,
 
12009
                       (uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
 
12010
                       f_maybe_null(cdef->pack_flag) ? 1 : 0,
 
12011
                       cdef->pack_flag, cdef->sql_type, cdef->charset,
 
12012
                       cdef->unireg_check,
 
12013
                       cdef->interval, cdef->field_name);
 
12014
    if (!*field)
 
12015
      goto error;
 
12016
    (*field)->init(table);
 
12017
    record_length+= (*field)->pack_length();
 
12018
    if (! ((*field)->flags & NOT_NULL_FLAG))
 
12019
      null_count++;
 
12020
 
 
12021
    if ((*field)->flags & BLOB_FLAG)
 
12022
      share->blob_field[blob_count++]= (uint) (field - table->field);
 
12023
 
 
12024
    field++;
 
12025
  }
 
12026
  *field= NULL;                             /* mark the end of the list */
 
12027
  share->blob_field[blob_count]= 0;            /* mark the end of the list */
 
12028
  share->blob_fields= blob_count;
 
12029
 
 
12030
  null_pack_length= (null_count + 7)/8;
 
12031
  share->reclength= record_length + null_pack_length;
 
12032
  share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
 
12033
  table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
 
12034
  if (!table->record[0])
 
12035
    goto error;
 
12036
 
 
12037
  if (null_pack_length)
 
12038
  {
 
12039
    table->null_flags= (uchar*) table->record[0];
 
12040
    share->null_fields= null_count;
 
12041
    share->null_bytes= null_pack_length;
 
12042
  }
 
12043
 
 
12044
  table->in_use= thd;           /* field->reset() may access table->in_use */
 
12045
  {
 
12046
    /* Set up field pointers */
 
12047
    uchar *null_pos= table->record[0];
 
12048
    uchar *field_pos= null_pos + share->null_bytes;
 
12049
    uint null_bit= 1;
 
12050
 
 
12051
    for (field= table->field; *field; ++field)
 
12052
    {
 
12053
      Field *cur_field= *field;
 
12054
      if ((cur_field->flags & NOT_NULL_FLAG))
 
12055
        cur_field->move_field(field_pos);
 
12056
      else
 
12057
      {
 
12058
        cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
 
12059
        null_bit<<= 1;
 
12060
        if (null_bit == (1 << 8))
 
12061
        {
 
12062
          ++null_pos;
 
12063
          null_bit= 1;
 
12064
        }
 
12065
      }
 
12066
      cur_field->reset();
 
12067
 
 
12068
      field_pos+= cur_field->pack_length();
 
12069
    }
 
12070
  }
 
12071
  return table;
 
12072
error:
 
12073
  for (field= table->field; *field; ++field)
 
12074
    delete *field;                         /* just invokes field destructor */
 
12075
  return 0;
 
12076
}
 
12077
 
 
12078
 
 
12079
static bool open_tmp_table(TABLE *table)
 
12080
{
 
12081
  int error;
 
12082
  if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
 
12083
                                  HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
 
12084
  {
 
12085
    table->file->print_error(error,MYF(0)); /* purecov: inspected */
 
12086
    table->db_stat=0;
 
12087
    return(1);
 
12088
  }
 
12089
  (void) table->file->extra(HA_EXTRA_QUICK);            /* Faster */
 
12090
  return(0);
 
12091
}
 
12092
 
 
12093
 
 
12094
/*
 
12095
  Create MyISAM temporary table
 
12096
 
 
12097
  SYNOPSIS
 
12098
    create_myisam_tmp_table()
 
12099
      table           Table object that descrimes the table to be created
 
12100
      keyinfo         Description of the index (there is always one index)
 
12101
      start_recinfo   MyISAM's column descriptions
 
12102
      recinfo INOUT   End of MyISAM's column descriptions
 
12103
      options         Option bits
 
12104
   
 
12105
  DESCRIPTION
 
12106
    Create a MyISAM temporary table according to passed description. The is
 
12107
    assumed to have one unique index or constraint.
 
12108
 
 
12109
    The passed array or MI_COLUMNDEF structures must have this form:
 
12110
 
 
12111
      1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
 
12112
         when there are many nullable columns)
 
12113
      2. Table columns
 
12114
      3. One free MI_COLUMNDEF element (*recinfo points here)
 
12115
   
 
12116
    This function may use the free element to create hash column for unique
 
12117
    constraint.
 
12118
 
 
12119
   RETURN
 
12120
     false - OK
 
12121
     true  - Error
 
12122
*/
 
12123
 
 
12124
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
 
12125
                                    MI_COLUMNDEF *start_recinfo,
 
12126
                                    MI_COLUMNDEF **recinfo, 
 
12127
                                    ulonglong options)
 
12128
{
 
12129
  int error;
 
12130
  MI_KEYDEF keydef;
 
12131
  MI_UNIQUEDEF uniquedef;
 
12132
  TABLE_SHARE *share= table->s;
 
12133
 
 
12134
  if (share->keys)
 
12135
  {                                             // Get keys for ni_create
 
12136
    bool using_unique_constraint=0;
 
12137
    HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
 
12138
                                            sizeof(*seg) * keyinfo->key_parts);
 
12139
    if (!seg)
 
12140
      goto err;
 
12141
 
 
12142
    bzero(seg, sizeof(*seg) * keyinfo->key_parts);
 
12143
    if (keyinfo->key_length >= table->file->max_key_length() ||
 
12144
        keyinfo->key_parts > table->file->max_key_parts() ||
 
12145
        share->uniques)
 
12146
    {
 
12147
      /* Can't create a key; Make a unique constraint instead of a key */
 
12148
      share->keys=    0;
 
12149
      share->uniques= 1;
 
12150
      using_unique_constraint=1;
 
12151
      bzero((char*) &uniquedef,sizeof(uniquedef));
 
12152
      uniquedef.keysegs=keyinfo->key_parts;
 
12153
      uniquedef.seg=seg;
 
12154
      uniquedef.null_are_equal=1;
 
12155
 
 
12156
      /* Create extra column for hash value */
 
12157
      bzero((uchar*) *recinfo,sizeof(**recinfo));
 
12158
      (*recinfo)->type= FIELD_CHECK;
 
12159
      (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
 
12160
      (*recinfo)++;
 
12161
      share->reclength+=MI_UNIQUE_HASH_LENGTH;
 
12162
    }
 
12163
    else
 
12164
    {
 
12165
      /* Create an unique key */
 
12166
      bzero((char*) &keydef,sizeof(keydef));
 
12167
      keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
 
12168
      keydef.keysegs=  keyinfo->key_parts;
 
12169
      keydef.seg= seg;
 
12170
    }
 
12171
    for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
 
12172
    {
 
12173
      Field *field=keyinfo->key_part[i].field;
 
12174
      seg->flag=     0;
 
12175
      seg->language= field->charset()->number;
 
12176
      seg->length=   keyinfo->key_part[i].length;
 
12177
      seg->start=    keyinfo->key_part[i].offset;
 
12178
      if (field->flags & BLOB_FLAG)
 
12179
      {
 
12180
        seg->type=
 
12181
        ((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
 
12182
         HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
 
12183
        seg->bit_start= (uint8)(field->pack_length() - share->blob_ptr_size);
 
12184
        seg->flag= HA_BLOB_PART;
 
12185
        seg->length=0;                  // Whole blob in unique constraint
 
12186
      }
 
12187
      else
 
12188
      {
 
12189
        seg->type= keyinfo->key_part[i].type;
 
12190
        /* Tell handler if it can do suffic space compression */
 
12191
        if (field->real_type() == MYSQL_TYPE_STRING &&
 
12192
            keyinfo->key_part[i].length > 4)
 
12193
          seg->flag|= HA_SPACE_PACK;
 
12194
      }
 
12195
      if (!(field->flags & NOT_NULL_FLAG))
 
12196
      {
 
12197
        seg->null_bit= field->null_bit;
 
12198
        seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
 
12199
        /*
 
12200
          We are using a GROUP BY on something that contains NULL
 
12201
          In this case we have to tell MyISAM that two NULL should
 
12202
          on INSERT be regarded at the same value
 
12203
        */
 
12204
        if (!using_unique_constraint)
 
12205
          keydef.flag|= HA_NULL_ARE_EQUAL;
 
12206
      }
 
12207
    }
 
12208
  }
 
12209
  MI_CREATE_INFO create_info;
 
12210
  bzero((char*) &create_info,sizeof(create_info));
 
12211
 
 
12212
  if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
 
12213
      OPTION_BIG_TABLES)
 
12214
    create_info.data_file_length= ~(ulonglong) 0;
 
12215
 
 
12216
  if ((error=mi_create(share->table_name.str, share->keys, &keydef,
 
12217
                       (uint) (*recinfo-start_recinfo),
 
12218
                       start_recinfo,
 
12219
                       share->uniques, &uniquedef,
 
12220
                       &create_info,
 
12221
                       HA_CREATE_TMP_TABLE)))
 
12222
  {
 
12223
    table->file->print_error(error,MYF(0));     /* purecov: inspected */
 
12224
    table->db_stat=0;
 
12225
    goto err;
 
12226
  }
 
12227
  status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
 
12228
  share->db_record_offset= 1;
 
12229
  return(0);
 
12230
 err:
 
12231
  return(1);
 
12232
}
 
12233
 
 
12234
 
 
12235
void
 
12236
free_tmp_table(THD *thd, TABLE *entry)
 
12237
{
 
12238
  MEM_ROOT own_root= entry->mem_root;
 
12239
  const char *save_proc_info;
 
12240
 
 
12241
  save_proc_info=thd->proc_info;
 
12242
  thd_proc_info(thd, "removing tmp table");
 
12243
 
 
12244
  if (entry->file)
 
12245
  {
 
12246
    if (entry->db_stat)
 
12247
      entry->file->ha_drop_table(entry->s->table_name.str);
 
12248
    else
 
12249
      entry->file->ha_delete_table(entry->s->table_name.str);
 
12250
    delete entry->file;
 
12251
  }
 
12252
 
 
12253
  /* free blobs */
 
12254
  for (Field **ptr=entry->field ; *ptr ; ptr++)
 
12255
    (*ptr)->free();
 
12256
  free_io_cache(entry);
 
12257
 
 
12258
  if (entry->temp_pool_slot != MY_BIT_NONE)
 
12259
    bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
 
12260
 
 
12261
  plugin_unlock(0, entry->s->db_plugin);
 
12262
 
 
12263
  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
 
12264
  thd_proc_info(thd, save_proc_info);
 
12265
 
 
12266
  return;
 
12267
}
 
12268
 
 
12269
/**
 
12270
  If a HEAP table gets full, create a MyISAM table and copy all rows
 
12271
  to this.
 
12272
*/
 
12273
 
 
12274
bool create_myisam_from_heap(THD *thd, TABLE *table,
 
12275
                             MI_COLUMNDEF *start_recinfo,
 
12276
                             MI_COLUMNDEF **recinfo, 
 
12277
                             int error, bool ignore_last_dupp_key_error)
 
12278
{
 
12279
  TABLE new_table;
 
12280
  TABLE_SHARE share;
 
12281
  const char *save_proc_info;
 
12282
  int write_err;
 
12283
 
 
12284
  if (table->s->db_type() != heap_hton || 
 
12285
      error != HA_ERR_RECORD_FILE_FULL)
 
12286
  {
 
12287
    table->file->print_error(error,MYF(0));
 
12288
    return(1);
 
12289
  }
 
12290
  new_table= *table;
 
12291
  share= *table->s;
 
12292
  new_table.s= &share;
 
12293
  new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
 
12294
  if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
 
12295
                                        new_table.s->db_type())))
 
12296
    return(1);                          // End of memory
 
12297
 
 
12298
  save_proc_info=thd->proc_info;
 
12299
  thd_proc_info(thd, "converting HEAP to MyISAM");
 
12300
 
 
12301
  if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
 
12302
                              recinfo, thd->lex->select_lex.options | 
 
12303
                                               thd->options))
 
12304
    goto err2;
 
12305
  if (open_tmp_table(&new_table))
 
12306
    goto err1;
 
12307
  if (table->file->indexes_are_disabled())
 
12308
    new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
 
12309
  table->file->ha_index_or_rnd_end();
 
12310
  table->file->ha_rnd_init(1);
 
12311
  if (table->no_rows)
 
12312
  {
 
12313
    new_table.file->extra(HA_EXTRA_NO_ROWS);
 
12314
    new_table.no_rows=1;
 
12315
  }
 
12316
 
 
12317
#ifdef TO_BE_DONE_LATER_IN_4_1
 
12318
  /*
 
12319
    To use start_bulk_insert() (which is new in 4.1) we need to find
 
12320
    all places where a corresponding end_bulk_insert() should be put.
 
12321
  */
 
12322
  table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
 
12323
  new_table.file->ha_start_bulk_insert(table->file->stats.records);
 
12324
#else
 
12325
  /* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
 
12326
  new_table.file->extra(HA_EXTRA_WRITE_CACHE);
 
12327
#endif
 
12328
 
 
12329
  /*
 
12330
    copy all old rows from heap table to MyISAM table
 
12331
    This is the only code that uses record[1] to read/write but this
 
12332
    is safe as this is a temporary MyISAM table without timestamp/autoincrement.
 
12333
  */
 
12334
  while (!table->file->rnd_next(new_table.record[1]))
 
12335
  {
 
12336
    write_err= new_table.file->ha_write_row(new_table.record[1]);
 
12337
    if (write_err)
 
12338
      goto err;
 
12339
  }
 
12340
  /* copy row that filled HEAP table */
 
12341
  if ((write_err=new_table.file->ha_write_row(table->record[0])))
 
12342
  {
 
12343
    if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
 
12344
        !ignore_last_dupp_key_error)
 
12345
      goto err;
 
12346
  }
 
12347
 
 
12348
  /* remove heap table and change to use myisam table */
 
12349
  (void) table->file->ha_rnd_end();
 
12350
  (void) table->file->close();                  // This deletes the table !
 
12351
  delete table->file;
 
12352
  table->file=0;
 
12353
  plugin_unlock(0, table->s->db_plugin);
 
12354
  share.db_plugin= my_plugin_lock(0, &share.db_plugin);
 
12355
  new_table.s= table->s;                       // Keep old share
 
12356
  *table= new_table;
 
12357
  *table->s= share;
 
12358
  
 
12359
  table->file->change_table_ptr(table, table->s);
 
12360
  table->use_all_columns();
 
12361
  if (save_proc_info)
 
12362
  {
 
12363
    const char *new_proc_info=
 
12364
      (!strcmp(save_proc_info,"Copying to tmp table") ?
 
12365
      "Copying to tmp table on disk" : save_proc_info);
 
12366
    thd_proc_info(thd, new_proc_info);
 
12367
  }
 
12368
  return(0);
 
12369
 
 
12370
 err:
 
12371
  table->file->print_error(write_err, MYF(0));
 
12372
  (void) table->file->ha_rnd_end();
 
12373
  (void) new_table.file->close();
 
12374
 err1:
 
12375
  new_table.file->ha_delete_table(new_table.s->table_name.str);
 
12376
 err2:
 
12377
  delete new_table.file;
 
12378
  thd_proc_info(thd, save_proc_info);
 
12379
  table->mem_root= new_table.mem_root;
 
12380
  return(1);
 
12381
}
 
12382
 
10505
12383
 
10506
12384
/**
10507
12385
  @details
10517
12395
 
10518
12396
Next_select_func setup_end_select_func(JOIN *join)
10519
12397
{
10520
 
  Table *table= join->tmp_table;
 
12398
  TABLE *table= join->tmp_table;
10521
12399
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
10522
12400
  Next_select_func end_select;
10523
12401
 
10583
12461
*/
10584
12462
 
10585
12463
static int
10586
 
do_select(JOIN *join,List<Item> *fields,Table *table)
 
12464
do_select(JOIN *join,List<Item> *fields,TABLE *table)
10587
12465
{
10588
12466
  int rc= 0;
10589
12467
  enum_nested_loop_state error= NESTED_LOOP_OK;
10594
12472
 
10595
12473
  if (table)
10596
12474
  {
10597
 
    table->file->extra(HA_EXTRA_WRITE_CACHE);
 
12475
    VOID(table->file->extra(HA_EXTRA_WRITE_CACHE));
10598
12476
    empty_record(table);
10599
12477
    if (table->group && join->tmp_table_param.sum_func_count &&
10600
12478
        table->s->keys && !table->file->inited)
10922
12800
  int error;
10923
12801
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
10924
12802
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
10925
 
  unsigned char *ptr= sjtbl->tmp_table->record[0] + 1;
10926
 
  unsigned char *nulls_ptr= ptr;
 
12803
  uchar *ptr= sjtbl->tmp_table->record[0] + 1;
 
12804
  uchar *nulls_ptr= ptr;
10927
12805
  
10928
12806
  /* Put the the rowids tuple into table->record[0]: */
10929
12807
 
10930
12808
  // 1. Store the length 
10931
12809
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
10932
12810
  {
10933
 
    *ptr= (unsigned char)(sjtbl->rowid_len + sjtbl->null_bytes);
 
12811
    *ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
10934
12812
    ptr++;
10935
12813
  }
10936
12814
  else
10942
12820
  // 2. Zero the null bytes 
10943
12821
  if (sjtbl->null_bytes)
10944
12822
  {
10945
 
    memset(ptr, 0, sjtbl->null_bytes);
 
12823
    bzero(ptr, sjtbl->null_bytes);
10946
12824
    ptr += sjtbl->null_bytes; 
10947
12825
  }
10948
12826
 
10949
12827
  // 3. Put the rowids
10950
 
  for (uint32_t i=0; tab != tab_end; tab++, i++)
 
12828
  for (uint i=0; tab != tab_end; tab++, i++)
10951
12829
  {
10952
12830
    handler *h= tab->join_tab->table->file;
10953
12831
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
10954
12832
    {
10955
12833
      /* It's a NULL-complemented row */
10956
12834
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
10957
 
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
 
12835
      bzero(ptr + tab->rowid_offset, h->ref_length);
10958
12836
    }
10959
12837
    else
10960
12838
    {
11248
13126
    if (rc == NESTED_LOOP_OK &&
11249
13127
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
11250
13128
    {
11251
 
      uint32_t i;
 
13129
      uint i;
11252
13130
      reset_cache_read(&join_tab->cache);
11253
13131
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
11254
13132
      {
11283
13161
  return NESTED_LOOP_OK;
11284
13162
}
11285
13163
 
 
13164
 
 
13165
/*****************************************************************************
 
13166
  The different ways to read a record
 
13167
  Returns -1 if row was not found, 0 if row was found and 1 on errors
 
13168
*****************************************************************************/
 
13169
 
 
13170
/** Help function when we get some an error from the table handler. */
 
13171
 
 
13172
int report_error(TABLE *table, int error)
 
13173
{
 
13174
  if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
 
13175
  {
 
13176
    table->status= STATUS_GARBAGE;
 
13177
    return -1;                                  // key not found; ok
 
13178
  }
 
13179
  /*
 
13180
    Locking reads can legally return also these errors, do not
 
13181
    print them to the .err log
 
13182
  */
 
13183
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
 
13184
    sql_print_error("Got error %d when reading table '%s'",
 
13185
                    error, table->s->path.str);
 
13186
  table->file->print_error(error,MYF(0));
 
13187
  return 1;
 
13188
}
 
13189
 
 
13190
 
11286
13191
int safe_index_read(JOIN_TAB *tab)
11287
13192
{
11288
13193
  int error;
11289
 
  Table *table= tab->table;
 
13194
  TABLE *table= tab->table;
11290
13195
  if ((error=table->file->index_read_map(table->record[0],
11291
13196
                                         tab->ref.key_buff,
11292
13197
                                         make_prev_keypart_map(tab->ref.key_parts),
11293
13198
                                         HA_READ_KEY_EXACT)))
11294
 
    return table->report_error(error);
 
13199
    return report_error(table, error);
11295
13200
  return 0;
11296
13201
}
11297
13202
 
11300
13205
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
11301
13206
{
11302
13207
  int error;
11303
 
  Table *table=tab->table;
 
13208
  TABLE *table=tab->table;
11304
13209
  table->const_table=1;
11305
13210
  table->null_row=0;
11306
13211
  table->status=STATUS_NO_RECORD;
11355
13260
  JOIN *join= tab->join;
11356
13261
  if (join->conds)
11357
13262
    update_const_equal_items(join->conds, tab);
11358
 
  TableList *tbl;
 
13263
  TABLE_LIST *tbl;
11359
13264
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
11360
13265
  {
11361
 
    TableList *embedded;
11362
 
    TableList *embedding= tbl;
 
13266
    TABLE_LIST *embedded;
 
13267
    TABLE_LIST *embedding= tbl;
11363
13268
    do
11364
13269
    {
11365
13270
      embedded= embedding;
11378
13283
static int
11379
13284
join_read_system(JOIN_TAB *tab)
11380
13285
{
11381
 
  Table *table= tab->table;
 
13286
  TABLE *table= tab->table;
11382
13287
  int error;
11383
13288
  if (table->status & STATUS_GARBAGE)           // If first read
11384
13289
  {
11386
13291
                                           table->s->primary_key)))
11387
13292
    {
11388
13293
      if (error != HA_ERR_END_OF_FILE)
11389
 
        return table->report_error(error);
 
13294
        return report_error(table, error);
11390
13295
      mark_as_null_row(tab->table);
11391
13296
      empty_record(table);                      // Make empty record
11392
13297
      return -1;
11417
13322
join_read_const(JOIN_TAB *tab)
11418
13323
{
11419
13324
  int error;
11420
 
  Table *table= tab->table;
 
13325
  TABLE *table= tab->table;
11421
13326
  if (table->status & STATUS_GARBAGE)           // If first read
11422
13327
  {
11423
13328
    table->status= 0;
11424
 
    if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
 
13329
    if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
11425
13330
      error=HA_ERR_KEY_NOT_FOUND;
11426
13331
    else
11427
13332
    {
11428
13333
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
11429
 
                                            (unsigned char*) tab->ref.key_buff,
 
13334
                                            (uchar*) tab->ref.key_buff,
11430
13335
                                            make_prev_keypart_map(tab->ref.key_parts),
11431
13336
                                            HA_READ_KEY_EXACT);
11432
13337
    }
11436
13341
      mark_as_null_row(tab->table);
11437
13342
      empty_record(table);
11438
13343
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11439
 
        return table->report_error(error);
 
13344
        return report_error(table, error);
11440
13345
      return -1;
11441
13346
    }
11442
13347
    store_record(table,record[1]);
11472
13377
join_read_key(JOIN_TAB *tab)
11473
13378
{
11474
13379
  int error;
11475
 
  Table *table= tab->table;
 
13380
  TABLE *table= tab->table;
11476
13381
 
11477
13382
  if (!table->file->inited)
11478
13383
  {
11493
13398
                                      make_prev_keypart_map(tab->ref.key_parts),
11494
13399
                                      HA_READ_KEY_EXACT);
11495
13400
    if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11496
 
      return table->report_error(error);
 
13401
      return report_error(table, error);
11497
13402
  }
11498
13403
  table->null_row=0;
11499
13404
  return table->status ? -1 : 0;
11523
13428
join_read_always_key(JOIN_TAB *tab)
11524
13429
{
11525
13430
  int error;
11526
 
  Table *table= tab->table;
 
13431
  TABLE *table= tab->table;
11527
13432
 
11528
13433
  /* Initialize the index first */
11529
13434
  if (!table->file->inited)
11530
13435
    table->file->ha_index_init(tab->ref.key, tab->sorted);
11531
13436
 
11532
13437
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
11533
 
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
 
13438
  for (uint i= 0 ; i < tab->ref.key_parts ; i++)
11534
13439
  {
11535
13440
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
11536
13441
        return -1;
11537
13442
  }
11538
13443
 
11539
 
  if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
 
13444
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
11540
13445
    return -1;
11541
13446
  if ((error=table->file->index_read_map(table->record[0],
11542
13447
                                         tab->ref.key_buff,
11544
13449
                                         HA_READ_KEY_EXACT)))
11545
13450
  {
11546
13451
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11547
 
      return table->report_error(error);
 
13452
      return report_error(table, error);
11548
13453
    return -1; /* purecov: inspected */
11549
13454
  }
11550
13455
  return 0;
11552
13457
 
11553
13458
 
11554
13459
/**
11555
 
  This function is used when optimizing away order_st BY in 
11556
 
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
 
13460
  This function is used when optimizing away ORDER BY in 
 
13461
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
11557
13462
*/
11558
13463
  
11559
13464
static int
11560
13465
join_read_last_key(JOIN_TAB *tab)
11561
13466
{
11562
13467
  int error;
11563
 
  Table *table= tab->table;
 
13468
  TABLE *table= tab->table;
11564
13469
 
11565
13470
  if (!table->file->inited)
11566
13471
    table->file->ha_index_init(tab->ref.key, tab->sorted);
11567
 
  if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
 
13472
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
11568
13473
    return -1;
11569
13474
  if ((error=table->file->index_read_last_map(table->record[0],
11570
13475
                                              tab->ref.key_buff,
11571
13476
                                              make_prev_keypart_map(tab->ref.key_parts))))
11572
13477
  {
11573
13478
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11574
 
      return table->report_error(error);
 
13479
      return report_error(table, error);
11575
13480
    return -1; /* purecov: inspected */
11576
13481
  }
11577
13482
  return 0;
11588
13493
static int
11589
13494
join_read_next_same_diff(READ_RECORD *info)
11590
13495
{
11591
 
  Table *table= info->table;
 
13496
  TABLE *table= info->table;
11592
13497
  JOIN_TAB *tab=table->reginfo.join_tab;
11593
13498
  if (tab->insideout_match_tab->found_match)
11594
13499
  {
11604
13509
                                              tab->ref.key_length)))
11605
13510
      {
11606
13511
        if (error != HA_ERR_END_OF_FILE)
11607
 
          return table->report_error(error);
 
13512
          return report_error(table, error);
11608
13513
        table->status= STATUS_GARBAGE;
11609
13514
        return -1;
11610
13515
      }
11621
13526
join_read_next_same(READ_RECORD *info)
11622
13527
{
11623
13528
  int error;
11624
 
  Table *table= info->table;
 
13529
  TABLE *table= info->table;
11625
13530
  JOIN_TAB *tab=table->reginfo.join_tab;
11626
13531
 
11627
13532
  if ((error=table->file->index_next_same(table->record[0],
11629
13534
                                          tab->ref.key_length)))
11630
13535
  {
11631
13536
    if (error != HA_ERR_END_OF_FILE)
11632
 
      return table->report_error(error);
 
13537
      return report_error(table, error);
11633
13538
    table->status= STATUS_GARBAGE;
11634
13539
    return -1;
11635
13540
  }
11641
13546
join_read_prev_same(READ_RECORD *info)
11642
13547
{
11643
13548
  int error;
11644
 
  Table *table= info->table;
 
13549
  TABLE *table= info->table;
11645
13550
  JOIN_TAB *tab=table->reginfo.join_tab;
11646
13551
 
11647
13552
  if ((error=table->file->index_prev(table->record[0])))
11648
 
    return table->report_error(error);
 
13553
    return report_error(table, error);
11649
13554
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
11650
13555
                      tab->ref.key_length))
11651
13556
  {
11700
13605
join_read_first(JOIN_TAB *tab)
11701
13606
{
11702
13607
  int error;
11703
 
  Table *table=tab->table;
 
13608
  TABLE *table=tab->table;
11704
13609
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
11705
13610
      !table->no_keyread)
11706
13611
  {
11729
13634
  if ((error=tab->table->file->index_first(tab->table->record[0])))
11730
13635
  {
11731
13636
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11732
 
      table->report_error(error);
 
13637
      report_error(table, error);
11733
13638
    return -1;
11734
13639
  }
11735
13640
  return 0;
11750
13655
      key_copy(tab->insideout_buf, info->record, key, 0);
11751
13656
 
11752
13657
      if ((error=info->file->index_next(info->record)))
11753
 
        return info->table->report_error(error);
 
13658
        return report_error(info->table, error);
11754
13659
      
11755
13660
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
11756
13661
                      tab->insideout_buf, key->key_length));
11767
13672
{
11768
13673
  int error;
11769
13674
  if ((error=info->file->index_next(info->record)))
11770
 
    return info->table->report_error(error);
 
13675
    return report_error(info->table, error);
11771
13676
  return 0;
11772
13677
}
11773
13678
 
11775
13680
static int
11776
13681
join_read_last(JOIN_TAB *tab)
11777
13682
{
11778
 
  Table *table=tab->table;
 
13683
  TABLE *table=tab->table;
11779
13684
  int error;
11780
13685
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
11781
13686
      !table->no_keyread)
11792
13697
  if (!table->file->inited)
11793
13698
    table->file->ha_index_init(tab->index, 1);
11794
13699
  if ((error= tab->table->file->index_last(tab->table->record[0])))
11795
 
    return table->report_error(error);
 
13700
    return report_error(table, error);
11796
13701
  return 0;
11797
13702
}
11798
13703
 
11802
13707
{
11803
13708
  int error;
11804
13709
  if ((error= info->file->index_prev(info->record)))
11805
 
    return info->table->report_error(error);
 
13710
    return report_error(info->table, error);
11806
13711
  return 0;
11807
13712
}
11808
13713
 
11895
13800
            (jt->ref.key < 0))
11896
13801
        {
11897
13802
          /* Join over all rows in table;  Return number of found rows */
11898
 
          Table *table=jt->table;
 
13803
          TABLE *table=jt->table;
11899
13804
 
11900
13805
          join->select_options ^= OPTION_FOUND_ROWS;
11901
13806
          if (table->sort.record_pointers ||
12007
13912
      if (end_of_records)
12008
13913
        return(NESTED_LOOP_OK);
12009
13914
      join->first_record=1;
12010
 
      test_if_item_cache_changed(join->group_fields);
 
13915
      VOID(test_if_item_cache_changed(join->group_fields));
12011
13916
    }
12012
13917
    if (idx < (int) join->send_group_parts)
12013
13918
    {
12032
13937
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
12033
13938
          bool end_of_records)
12034
13939
{
12035
 
  Table *table=join->tmp_table;
 
13940
  TABLE *table=join->tmp_table;
12036
13941
 
12037
13942
  if (join->thd->killed)                        // Aborted by user
12038
13943
  {
12047
13952
    if (!table->uniques)                        // If not unique handling
12048
13953
    {
12049
13954
      /* Copy null values from group to row */
12050
 
      order_st   *group;
 
13955
      ORDER   *group;
12051
13956
      for (group=table->group ; group ; group=group->next)
12052
13957
      {
12053
13958
        Item *item= *group->item;
12054
13959
        if (item->maybe_null)
12055
13960
        {
12056
13961
          Field *field=item->get_tmp_table_field();
12057
 
          field->ptr[-1]= (unsigned char) (field->is_null() ? 1 : 0);
 
13962
          field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0);
12058
13963
        }
12059
13964
      }
12060
13965
    }
12096
14001
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
12097
14002
           bool end_of_records)
12098
14003
{
12099
 
  Table *table=join->tmp_table;
12100
 
  order_st   *group;
 
14004
  TABLE *table=join->tmp_table;
 
14005
  ORDER   *group;
12101
14006
  int     error;
12102
14007
 
12103
14008
  if (end_of_records)
12172
14077
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
12173
14078
                  bool end_of_records)
12174
14079
{
12175
 
  Table *table=join->tmp_table;
 
14080
  TABLE *table=join->tmp_table;
12176
14081
  int     error;
12177
14082
 
12178
14083
  if (end_of_records)
12219
14124
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
12220
14125
                bool end_of_records)
12221
14126
{
12222
 
  Table *table=join->tmp_table;
 
14127
  TABLE *table=join->tmp_table;
12223
14128
  int     idx= -1;
12224
14129
 
12225
14130
  if (join->thd->killed)
12265
14170
      if (end_of_records)
12266
14171
        return(NESTED_LOOP_OK);
12267
14172
      join->first_record=1;
12268
 
      test_if_item_cache_changed(join->group_fields);
 
14173
      VOID(test_if_item_cache_changed(join->group_fields));
12269
14174
    }
12270
14175
    if (idx < (int) join->send_group_parts)
12271
14176
    {
12326
14231
          frequent case?
12327
14232
        */
12328
14233
        if (field->binary() &&
12329
 
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
12330
 
            field->decimals() == 0)
 
14234
            field->real_type() != MYSQL_TYPE_STRING &&
 
14235
            field->real_type() != MYSQL_TYPE_VARCHAR &&
 
14236
            (field->type() != MYSQL_TYPE_FLOAT || field->decimals() == 0))
12331
14237
        {
12332
14238
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
12333
14239
        }
12527
14433
 
12528
14434
 
12529
14435
static Item *
12530
 
part_of_refkey(Table *table,Field *field)
 
14436
part_of_refkey(TABLE *table,Field *field)
12531
14437
{
12532
14438
  if (!table->reginfo.join_tab)
12533
14439
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
12534
14440
 
12535
 
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
 
14441
  uint ref_parts=table->reginfo.join_tab->ref.key_parts;
12536
14442
  if (ref_parts)
12537
14443
  {
12538
14444
    KEY_PART_INFO *key_part=
12539
14445
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
12540
 
    uint32_t part;
 
14446
    uint part;
12541
14447
 
12542
14448
    for (part=0 ; part < ref_parts ; part++)
12543
14449
    {
12555
14461
 
12556
14462
 
12557
14463
/**
12558
 
  Test if one can use the key to resolve order_st BY.
 
14464
  Test if one can use the key to resolve ORDER BY.
12559
14465
 
12560
14466
  @param order                 Sort order
12561
14467
  @param table                 Table to sort
12575
14481
    -1   Reverse key can be used
12576
14482
*/
12577
14483
 
12578
 
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx,
12579
 
                                uint32_t *used_key_parts)
 
14484
static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
 
14485
                                uint *used_key_parts)
12580
14486
{
12581
14487
  KEY_PART_INFO *key_part,*key_part_end;
12582
14488
  key_part=table->key_info[idx].key_part;
12583
14489
  key_part_end=key_part+table->key_info[idx].key_parts;
12584
14490
  key_part_map const_key_parts=table->const_key_parts[idx];
12585
14491
  int reverse=0;
12586
 
  bool on_primary_key= false;
 
14492
  my_bool on_primary_key= false;
12587
14493
 
12588
14494
  for (; order ; order=order->next, const_key_parts>>=1)
12589
14495
  {
12592
14498
 
12593
14499
    /*
12594
14500
      Skip key parts that are constants in the WHERE clause.
12595
 
      These are already skipped in the order_st BY by const_expression_in_where()
 
14501
      These are already skipped in the ORDER BY by const_expression_in_where()
12596
14502
    */
12597
14503
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
12598
14504
      key_part++; 
12646
14552
}
12647
14553
 
12648
14554
 
 
14555
uint find_shortest_key(TABLE *table, const key_map *usable_keys)
 
14556
{
 
14557
  uint min_length= (uint) ~0;
 
14558
  uint best= MAX_KEY;
 
14559
  if (!usable_keys->is_clear_all())
 
14560
  {
 
14561
    for (uint nr=0; nr < table->s->keys ; nr++)
 
14562
    {
 
14563
      if (usable_keys->is_set(nr))
 
14564
      {
 
14565
        if (table->key_info[nr].key_length < min_length)
 
14566
        {
 
14567
          min_length=table->key_info[nr].key_length;
 
14568
          best=nr;
 
14569
        }
 
14570
      }
 
14571
    }
 
14572
  }
 
14573
  return best;
 
14574
}
 
14575
 
12649
14576
/**
12650
14577
  Test if a second key is the subkey of the first one.
12651
14578
 
12685
14612
*/
12686
14613
 
12687
14614
static uint
12688
 
test_if_subkey(order_st *order, Table *table, uint32_t ref, uint32_t ref_key_parts,
 
14615
test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts,
12689
14616
               const key_map *usable_keys)
12690
14617
{
12691
 
  uint32_t nr;
12692
 
  uint32_t min_length= UINT32_MAX;
12693
 
  uint32_t best= MAX_KEY;
12694
 
  uint32_t not_used;
 
14618
  uint nr;
 
14619
  uint min_length= (uint) ~0;
 
14620
  uint best= MAX_KEY;
 
14621
  uint not_used;
12695
14622
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
12696
14623
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
12697
14624
 
12745
14672
*/
12746
14673
 
12747
14674
static bool
12748
 
list_contains_unique_index(Table *table,
 
14675
list_contains_unique_index(TABLE *table,
12749
14676
                          bool (*find_func) (Field *, void *), void *data)
12750
14677
{
12751
 
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
 
14678
  for (uint keynr= 0; keynr < table->s->keys; keynr++)
12752
14679
  {
12753
14680
    if (keynr == table->s->primary_key ||
12754
14681
         (table->key_info[keynr].flags & HA_NOSAME))
12775
14702
 
12776
14703
/**
12777
14704
  Helper function for list_contains_unique_index.
12778
 
  Find a field reference in a list of order_st structures.
 
14705
  Find a field reference in a list of ORDER structures.
12779
14706
  Finds a direct reference of the Field in the list.
12780
14707
 
12781
14708
  @param field                The field to search for.
12782
 
  @param data                 order_st *.The list to search in
 
14709
  @param data                 ORDER *.The list to search in
12783
14710
 
12784
14711
  @retval
12785
14712
    1                    found
12790
14717
static bool
12791
14718
find_field_in_order_list (Field *field, void *data)
12792
14719
{
12793
 
  order_st *group= (order_st *) data;
 
14720
  ORDER *group= (ORDER *) data;
12794
14721
  bool part_found= 0;
12795
 
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
14722
  for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
12796
14723
  {
12797
14724
    Item *item= (*tmp_group->item)->real_item();
12798
14725
    if (item->type() == Item::FIELD_ITEM &&
12842
14769
 
12843
14770
 
12844
14771
/**
12845
 
  Test if we can skip the order_st BY by using an index.
 
14772
  Test if we can skip the ORDER BY by using an index.
12846
14773
 
12847
14774
  SYNOPSIS
12848
14775
    test_if_skip_sort_order()
12868
14795
*/
12869
14796
 
12870
14797
static bool
12871
 
test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,ha_rows select_limit,
 
14798
test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
12872
14799
                        bool no_changes, const key_map *map)
12873
14800
{
12874
 
  int32_t ref_key;
12875
 
  uint32_t ref_key_parts;
 
14801
  int ref_key;
 
14802
  uint ref_key_parts;
12876
14803
  int order_direction;
12877
 
  uint32_t used_key_parts;
12878
 
  Table *table=tab->table;
 
14804
  uint used_key_parts;
 
14805
  TABLE *table=tab->table;
12879
14806
  SQL_SELECT *select=tab->select;
12880
14807
  key_map usable_keys;
12881
14808
  QUICK_SELECT_I *save_quick= 0;
12882
14809
 
12883
14810
  /*
12884
 
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
 
14811
    Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
12885
14812
    been taken into account.
12886
14813
  */
12887
14814
  usable_keys= *map;
12888
14815
 
12889
 
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
14816
  for (ORDER *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
12890
14817
  {
12891
14818
    Item *item= (*tmp_order->item)->real_item();
12892
14819
    if (item->type() != Item::FIELD_ITEM)
12936
14863
      /*
12937
14864
        We come here when ref_key is not among usable_keys
12938
14865
      */
12939
 
      uint32_t new_ref_key;
 
14866
      uint new_ref_key;
12940
14867
      /*
12941
14868
        If using index only read, only consider other possible index only
12942
14869
        keys
13004
14931
      Check whether there is an index compatible with the given order
13005
14932
      usage of which is cheaper than usage of the ref_key index (ref_key>=0)
13006
14933
      or a table scan.
13007
 
      It may be the case if order_st/GROUP BY is used with LIMIT.
 
14934
      It may be the case if ORDER/GROUP BY is used with LIMIT.
13008
14935
    */
13009
 
    uint32_t nr;
 
14936
    uint nr;
13010
14937
    key_map keys;
13011
 
    uint32_t best_key_parts= 0;
 
14938
    uint best_key_parts= 0;
13012
14939
    int best_key_direction= 0;
13013
14940
    ha_rows best_records= 0;
13014
14941
    double read_time;
13016
14943
    bool is_best_covering= false;
13017
14944
    double fanout= 1;
13018
14945
    JOIN *join= tab->join;
13019
 
    uint32_t tablenr= tab - join->join_tab;
 
14946
    uint tablenr= tab - join->join_tab;
13020
14947
    ha_rows table_records= table->file->stats.records;
13021
14948
    bool group= join->group && order == join->group_list;
13022
14949
 
13039
14966
      /*
13040
14967
        We are adding here also the index specified in FORCE INDEX clause, 
13041
14968
        if any.
13042
 
        This is to allow users to use index in order_st BY.
 
14969
        This is to allow users to use index in ORDER BY.
13043
14970
      */
13044
14971
      if (table->force_index) 
13045
14972
        keys.merge(group ? table->keys_in_use_for_group_by :
13050
14977
      keys= usable_keys;
13051
14978
 
13052
14979
    read_time= join->best_positions[tablenr].read_time;
13053
 
    for (uint32_t i= tablenr+1; i < join->tables; i++)
 
14980
    for (uint i= tablenr+1; i < join->tables; i++)
13054
14981
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
13055
14982
 
13056
14983
    for (nr=0; nr < table->s->keys ; nr++)
13062
14989
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
13063
14990
        
13064
14991
        /* 
13065
 
          Don't use an index scan with order_st BY without limit.
 
14992
          Don't use an index scan with ORDER BY without limit.
13066
14993
          For GROUP BY without limit always use index scan
13067
14994
          if there is a suitable index. 
13068
14995
          Why we hold to this asymmetry hardly can be explained
13135
15062
            index entry.
13136
15063
          */
13137
15064
          index_scan_time= select_limit/rec_per_key *
13138
 
                           cmin(rec_per_key, table->file->scan_time());
 
15065
                           min(rec_per_key, table->file->scan_time());
13139
15066
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
13140
15067
              index_scan_time < read_time)
13141
15068
          {
13145
15072
            if (table->quick_keys.is_set(nr))
13146
15073
              quick_records= table->quick_rows[nr];
13147
15074
            if (best_key < 0 ||
13148
 
                (select_limit <= cmin(quick_records,best_records) ?
 
15075
                (select_limit <= min(quick_records,best_records) ?
13149
15076
                 keyinfo->key_parts < best_key_parts :
13150
15077
                 quick_records < best_records))
13151
15078
            {
13227
15154
  } 
13228
15155
 
13229
15156
check_reverse_order:                  
13230
 
  if (order_direction == -1)            // If order_st BY ... DESC
 
15157
  if (order_direction == -1)            // If ORDER BY ... DESC
13231
15158
  {
13232
15159
    if (select && select->quick)
13233
15160
    {
13250
15177
          return(0);                   // Use filesort
13251
15178
        }
13252
15179
            
13253
 
        /* order_st BY range_key DESC */
 
15180
        /* ORDER BY range_key DESC */
13254
15181
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
13255
15182
                                    used_key_parts, &error);
13256
15183
        if (!tmp || error)
13267
15194
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
13268
15195
    {
13269
15196
      /*
13270
 
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
 
15197
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
13271
15198
 
13272
15199
        Use a traversal function that starts by reading the last row
13273
15200
        with key part (A) and then traverse the index backwards.
13293
15220
     filesort_limit     Max number of rows that needs to be sorted
13294
15221
     select_limit       Max number of rows in final output
13295
15222
                        Used to decide if we should use index or not
13296
 
     is_order_by        true if we are sorting on order_st BY, false if GROUP BY
 
15223
     is_order_by        true if we are sorting on ORDER BY, false if GROUP BY
13297
15224
                        Used to decide if we should use index or not     
13298
15225
 
13299
15226
 
13312
15239
*/
13313
15240
 
13314
15241
static int
13315
 
create_sort_index(THD *thd, JOIN *join, order_st *order,
 
15242
create_sort_index(THD *thd, JOIN *join, ORDER *order,
13316
15243
                  ha_rows filesort_limit, ha_rows select_limit,
13317
15244
                  bool is_order_by)
13318
15245
{
13319
 
  uint32_t length= 0;
 
15246
  uint length= 0;
13320
15247
  ha_rows examined_rows;
13321
 
  Table *table;
 
15248
  TABLE *table;
13322
15249
  SQL_SELECT *select;
13323
15250
  JOIN_TAB *tab;
13324
15251
 
13341
15268
                              is_order_by ?  &table->keys_in_use_for_order_by :
13342
15269
                              &table->keys_in_use_for_group_by))
13343
15270
    return(0);
13344
 
  for (order_st *ord= join->order; ord; ord= ord->next)
 
15271
  for (ORDER *ord= join->order; ord; ord= ord->next)
13345
15272
    length++;
13346
15273
  if (!(join->sortorder= 
13347
15274
        make_unireg_sortorder(order, &length, join->sortorder)))
13414
15341
  return(-1);
13415
15342
}
13416
15343
 
 
15344
/*****************************************************************************
 
15345
  Remove duplicates from tmp table
 
15346
  This should be recoded to add a unique index to the table and remove
 
15347
  duplicates
 
15348
  Table is a locked single thread table
 
15349
  fields is the number of fields to check (from the end)
 
15350
*****************************************************************************/
 
15351
 
 
15352
static bool compare_record(TABLE *table, Field **ptr)
 
15353
{
 
15354
  for (; *ptr ; ptr++)
 
15355
  {
 
15356
    if ((*ptr)->cmp_offset(table->s->rec_buff_length))
 
15357
      return 1;
 
15358
  }
 
15359
  return 0;
 
15360
}
 
15361
 
13417
15362
static bool copy_blobs(Field **ptr)
13418
15363
{
13419
15364
  for (; *ptr ; ptr++)
13436
15381
 
13437
15382
 
13438
15383
static int
13439
 
remove_duplicates(JOIN *join, Table *entry,List<Item> &fields, Item *having)
 
15384
remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having)
13440
15385
{
13441
15386
  int error;
13442
15387
  ulong reclength,offset;
13443
 
  uint32_t field_count;
 
15388
  uint field_count;
13444
15389
  THD *thd= join->thd;
13445
15390
 
13446
15391
  entry->reginfo.lock_type=TL_WRITE;
13484
15429
}
13485
15430
 
13486
15431
 
13487
 
static int remove_dup_with_compare(THD *thd, Table *table, Field **first_field,
 
15432
static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field,
13488
15433
                                   ulong offset, Item *having)
13489
15434
{
13490
15435
  handler *file=table->file;
13491
15436
  char *org_record,*new_record;
13492
 
  unsigned char *record;
 
15437
  uchar *record;
13493
15438
  int error;
13494
15439
  ulong reclength= table->s->reclength-offset;
13495
15440
 
13541
15486
          break;
13542
15487
        goto err;
13543
15488
      }
13544
 
      if (table->compare_record(first_field) == 0)
 
15489
      if (compare_record(table, first_field) == 0)
13545
15490
      {
13546
15491
        if ((error=file->ha_delete_row(record)))
13547
15492
          goto err;
13575
15520
    Note that this will not work on tables with blobs!
13576
15521
*/
13577
15522
 
13578
 
static int remove_dup_with_hash_index(THD *thd, Table *table,
13579
 
                                      uint32_t field_count,
 
15523
static int remove_dup_with_hash_index(THD *thd, TABLE *table,
 
15524
                                      uint field_count,
13580
15525
                                      Field **first_field,
13581
15526
                                      ulong key_length,
13582
15527
                                      Item *having)
13583
15528
{
13584
 
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
 
15529
  uchar *key_buffer, *key_pos, *record=table->record[0];
13585
15530
  int error;
13586
15531
  handler *file= table->file;
13587
15532
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
13588
 
  uint32_t *field_lengths,*field_length;
 
15533
  uint *field_lengths,*field_length;
13589
15534
  HASH hash;
13590
15535
 
13591
15536
  if (!my_multi_malloc(MYF(MY_WME),
13594
15539
                               (long) file->stats.records),
13595
15540
                       &field_lengths,
13596
15541
                       (uint) (field_count*sizeof(*field_lengths)),
13597
 
                       NULL))
 
15542
                       NullS))
13598
15543
    return(1);
13599
15544
 
13600
15545
  {
13602
15547
    ulong total_length= 0;
13603
15548
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
13604
15549
    {
13605
 
      uint32_t length= (*ptr)->sort_length();
 
15550
      uint length= (*ptr)->sort_length();
13606
15551
      (*field_length++)= length;
13607
15552
      total_length+= length;
13608
15553
    }
13614
15559
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, 
13615
15560
                key_length, (hash_get_key) 0, 0, 0))
13616
15561
  {
13617
 
    free((char*) key_buffer);
 
15562
    my_free((char*) key_buffer,MYF(0));
13618
15563
    return(1);
13619
15564
  }
13620
15565
 
13622
15567
  key_pos=key_buffer;
13623
15568
  for (;;)
13624
15569
  {
13625
 
    unsigned char *org_key_pos;
 
15570
    uchar *org_key_pos;
13626
15571
    if (thd->killed)
13627
15572
    {
13628
15573
      thd->send_kill_message();
13663
15608
      (void) my_hash_insert(&hash, org_key_pos);
13664
15609
    key_pos+=extra_length;
13665
15610
  }
13666
 
  free((char*) key_buffer);
 
15611
  my_free((char*) key_buffer,MYF(0));
13667
15612
  hash_free(&hash);
13668
15613
  file->extra(HA_EXTRA_NO_CACHE);
13669
15614
  (void) file->ha_rnd_end();
13670
15615
  return(0);
13671
15616
 
13672
15617
err:
13673
 
  free((char*) key_buffer);
 
15618
  my_free((char*) key_buffer,MYF(0));
13674
15619
  hash_free(&hash);
13675
15620
  file->extra(HA_EXTRA_NO_CACHE);
13676
15621
  (void) file->ha_rnd_end();
13680
15625
}
13681
15626
 
13682
15627
 
13683
 
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length,
 
15628
SORT_FIELD *make_unireg_sortorder(ORDER *order, uint *length,
13684
15629
                                  SORT_FIELD *sortorder)
13685
15630
{
13686
 
  uint32_t count;
 
15631
  uint count;
13687
15632
  SORT_FIELD *sort,*pos;
13688
15633
 
13689
15634
  count=0;
13690
 
  for (order_st *tmp = order; tmp; tmp=tmp->next)
 
15635
  for (ORDER *tmp = order; tmp; tmp=tmp->next)
13691
15636
    count++;
13692
15637
  if (!sortorder)
13693
15638
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
13694
 
                                       (cmax(count, *length) + 1));
 
15639
                                       (max(count, *length) + 1));
13695
15640
  pos= sort= sortorder;
13696
15641
 
13697
15642
  if (!pos)
13726
15671
******************************************************************************/
13727
15672
 
13728
15673
static int
13729
 
join_init_cache(THD *thd,JOIN_TAB *tables,uint32_t table_count)
 
15674
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count)
13730
15675
{
13731
15676
  register unsigned int i;
13732
15677
  unsigned int length, blobs;
13758
15703
 
13759
15704
                  sizeof(CACHE_FIELD*))))
13760
15705
  {
13761
 
    free((unsigned char*) cache->buff);         /* purecov: inspected */
 
15706
    my_free((uchar*) cache->buff,MYF(0));               /* purecov: inspected */
13762
15707
    cache->buff=0;                              /* purecov: inspected */
13763
15708
    return(1);                          /* purecov: inspected */
13764
15709
  }
13769
15714
  length=0;
13770
15715
  for (i=0 ; i < table_count ; i++)
13771
15716
  {
13772
 
    uint32_t null_fields=0, used_fields;
 
15717
    uint null_fields=0,used_fields;
13773
15718
    Field **f_ptr,*field;
13774
15719
    MY_BITMAP *read_set= tables[i].table->read_set;
13775
15720
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
13790
15735
      }
13791
15736
    }
13792
15737
    /* Copy null bits from table */
13793
 
    if (null_fields && tables[i].table->getNullFields())
 
15738
    if (null_fields && tables[i].table->s->null_fields)
13794
15739
    {                                           /* must copy null bits */
13795
15740
      copy->str= tables[i].table->null_flags;
13796
15741
      copy->length= tables[i].table->s->null_bytes;
13804
15749
    /* If outer join table, copy null_row flag */
13805
15750
    if (tables[i].table->maybe_null)
13806
15751
    {
13807
 
      copy->str= (unsigned char*) &tables[i].table->null_row;
 
15752
      copy->str= (uchar*) &tables[i].table->null_row;
13808
15753
      copy->length=sizeof(tables[i].table->null_row);
13809
15754
      copy->strip=0;
13810
15755
      copy->blob_field=0;
13835
15780
  cache->length=length+blobs*sizeof(char*);
13836
15781
  cache->blobs=blobs;
13837
15782
  *blob_ptr=0;                                  /* End sequentel */
13838
 
  size=cmax(thd->variables.join_buff_size, (ulong)cache->length);
13839
 
  if (!(cache->buff=(unsigned char*) my_malloc(size,MYF(0))))
 
15783
  size=max(thd->variables.join_buff_size, cache->length);
 
15784
  if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
13840
15785
    return(1);                          /* Don't use cache */ /* purecov: inspected */
13841
15786
  cache->end=cache->buff+size;
13842
15787
  reset_cache_write(cache);
13847
15792
static ulong
13848
15793
used_blob_length(CACHE_FIELD **ptr)
13849
15794
{
13850
 
  uint32_t length,blob_length;
 
15795
  uint length,blob_length;
13851
15796
  for (length=0 ; *ptr ; ptr++)
13852
15797
  {
13853
15798
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
13861
15806
static bool
13862
15807
store_record_in_cache(JOIN_CACHE *cache)
13863
15808
{
13864
 
  uint32_t length;
13865
 
  unsigned char *pos;
 
15809
  uint length;
 
15810
  uchar *pos;
13866
15811
  CACHE_FIELD *copy,*end_field;
13867
15812
  bool last_record;
13868
15813
 
13904
15849
 
13905
15850
      if (copy->strip)
13906
15851
      {
13907
 
        unsigned char *str,*end;
 
15852
        uchar *str,*end;
13908
15853
        for (str=copy->str,end= str+copy->length;
13909
15854
             end > str && end[-1] == ' ' ;
13910
15855
             end--) ;
13937
15882
{
13938
15883
  reset_cache_read(cache);
13939
15884
  cache->records= 0;
13940
 
  cache->ptr_record= UINT32_MAX;
 
15885
  cache->ptr_record= (uint) ~0;
13941
15886
}
13942
15887
 
13943
15888
 
13944
15889
static void
13945
15890
read_cached_record(JOIN_TAB *tab)
13946
15891
{
13947
 
  unsigned char *pos;
13948
 
  uint32_t length;
 
15892
  uchar *pos;
 
15893
  uint length;
13949
15894
  bool last_record;
13950
15895
  CACHE_FIELD *copy,*end_field;
13951
15896
 
14023
15968
  }
14024
15969
  else 
14025
15970
    no_prev_key= true;
14026
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, &tab->ref)) ||
 
15971
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, tab->table,
 
15972
                                            &tab->ref)) ||
14027
15973
      no_prev_key)
14028
15974
    return 1;
14029
15975
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
14032
15978
 
14033
15979
 
14034
15980
bool
14035
 
cp_buffer_from_ref(THD *thd, TABLE_REF *ref)
 
15981
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
14036
15982
{
14037
15983
  enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
14038
15984
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
15985
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
14039
15986
  bool result= 0;
14040
15987
 
14041
15988
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
14047
15994
    }
14048
15995
  }
14049
15996
  thd->count_cuted_fields= save_count_cuted_fields;
 
15997
  dbug_tmp_restore_column_map(table->write_set, old_map);
14050
15998
  return result;
14051
15999
}
14052
16000
 
14056
16004
*****************************************************************************/
14057
16005
 
14058
16006
/**
14059
 
  Resolve an order_st BY or GROUP BY column reference.
 
16007
  Resolve an ORDER BY or GROUP BY column reference.
14060
16008
 
14061
 
  Given a column reference (represented by 'order') from a GROUP BY or order_st
 
16009
  Given a column reference (represented by 'order') from a GROUP BY or ORDER
14062
16010
  BY clause, find the actual column it represents. If the column being
14063
16011
  resolved is from the GROUP BY clause, the procedure searches the SELECT
14064
16012
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
14065
 
  the order_st BY clause, only the SELECT list is being searched.
 
16013
  the ORDER BY clause, only the SELECT list is being searched.
14066
16014
 
14067
16015
  If 'order' is resolved to an Item, then order->item is set to the found
14068
16016
  Item. If there is no item for the found column (that is, it was resolved
14080
16028
    SELECT list)
14081
16029
  @param[in,out] all_fields         All select, group and order by fields
14082
16030
  @param[in] is_group_field         True if order is a GROUP field, false if
14083
 
    order_st by field
 
16031
    ORDER by field
14084
16032
 
14085
16033
  @retval
14086
16034
    false if OK
14089
16037
*/
14090
16038
 
14091
16039
static bool
14092
 
find_order_in_list(THD *thd, Item **ref_pointer_array, TableList *tables,
14093
 
                   order_st *order, List<Item> &fields, List<Item> &all_fields,
 
16040
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16041
                   ORDER *order, List<Item> &fields, List<Item> &all_fields,
14094
16042
                   bool is_group_field)
14095
16043
{
14096
 
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
 
16044
  Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */
14097
16045
  Item::Type order_item_type;
14098
16046
  Item **select_item; /* The corresponding item from the SELECT clause. */
14099
16047
  Field *from_field;  /* The corresponding field from the FROM clause. */
14100
 
  uint32_t counter;
 
16048
  uint counter;
14101
16049
  enum_resolution_type resolution;
14102
16050
 
14103
16051
  /*
14106
16054
  */
14107
16055
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
14108
16056
  {                                             /* Order by position */
14109
 
    uint32_t count= (uint) order_item->val_int();
 
16057
    uint count= (uint) order_item->val_int();
14110
16058
    if (!count || count > fields.elements)
14111
16059
    {
14112
16060
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
14119
16067
    order->counter_used= 1;
14120
16068
    return false;
14121
16069
  }
14122
 
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
 
16070
  /* Lookup the current GROUP/ORDER field in the SELECT clause. */
14123
16071
  select_item= find_item_in_list(order_item, fields, &counter,
14124
16072
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
14125
16073
  if (!select_item)
14185
16133
        warning so the user knows that the field from the FROM clause
14186
16134
        overshadows the column reference from the SELECT list.
14187
16135
      */
14188
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
16136
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
14189
16137
                          ER(ER_NON_UNIQ_ERROR),
14190
16138
                          ((Item_ident*) order_item)->field_name,
14191
16139
                          current_thd->where);
14210
16158
       thd->is_fatal_error))
14211
16159
    return true; /* Wrong field. */
14212
16160
 
14213
 
  uint32_t el= all_fields.elements;
 
16161
  uint el= all_fields.elements;
14214
16162
  all_fields.push_front(order_item); /* Add new field to field list. */
14215
16163
  ref_pointer_array[el]= order_item;
14216
16164
  order->item= ref_pointer_array + el;
14225
16173
  the field list.
14226
16174
*/
14227
16175
 
14228
 
int setup_order(THD *thd, Item **ref_pointer_array, TableList *tables,
14229
 
                List<Item> &fields, List<Item> &all_fields, order_st *order)
 
16176
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16177
                List<Item> &fields, List<Item> &all_fields, ORDER *order)
14230
16178
{
14231
16179
  thd->where="order clause";
14232
16180
  for (; order; order=order->next)
14266
16214
*/
14267
16215
 
14268
16216
int
14269
 
setup_group(THD *thd, Item **ref_pointer_array, TableList *tables,
14270
 
            List<Item> &fields, List<Item> &all_fields, order_st *order,
 
16217
setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16218
            List<Item> &fields, List<Item> &all_fields, ORDER *order,
14271
16219
            bool *hidden_group_fields)
14272
16220
{
14273
16221
  *hidden_group_fields=0;
14274
 
  order_st *ord;
 
16222
  ORDER *ord;
14275
16223
 
14276
16224
  if (!order)
14277
16225
    return 0;                           /* Everything is ok */
14278
16226
 
14279
 
  uint32_t org_fields=all_fields.elements;
 
16227
  uint org_fields=all_fields.elements;
14280
16228
 
14281
16229
  thd->where="group statement";
14282
16230
  for (ord= order; ord; ord= ord->next)
14362
16310
  optimize away 'order by'.
14363
16311
*/
14364
16312
 
14365
 
static order_st *
 
16313
static ORDER *
14366
16314
create_distinct_group(THD *thd, Item **ref_pointer_array,
14367
 
                      order_st *order_list, List<Item> &fields,
14368
 
                      List<Item> &all_fields __attribute__((unused)),
 
16315
                      ORDER *order_list, List<Item> &fields,
 
16316
                      List<Item> &all_fields __attribute__((__unused__)),
14369
16317
                      bool *all_order_by_fields_used)
14370
16318
{
14371
16319
  List_iterator<Item> li(fields);
14372
16320
  Item *item;
14373
 
  order_st *order,*group,**prev;
 
16321
  ORDER *order,*group,**prev;
14374
16322
 
14375
16323
  *all_order_by_fields_used= 1;
14376
16324
  while ((item=li++))
14381
16329
  {
14382
16330
    if (order->in_field_list)
14383
16331
    {
14384
 
      order_st *ord=(order_st*) thd->memdup((char*) order,sizeof(order_st));
 
16332
      ORDER *ord=(ORDER*) thd->memdup((char*) order,sizeof(ORDER));
14385
16333
      if (!ord)
14386
16334
        return 0;
14387
16335
      *prev=ord;
14401
16349
        Don't put duplicate columns from the SELECT list into the 
14402
16350
        GROUP BY list.
14403
16351
      */
14404
 
      order_st *ord_iter;
 
16352
      ORDER *ord_iter;
14405
16353
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
14406
16354
        if ((*ord_iter->item)->eq(item, 1))
14407
16355
          goto next_item;
14408
16356
      
14409
 
      order_st *ord=(order_st*) thd->calloc(sizeof(order_st));
 
16357
      ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER));
14410
16358
      if (!ord)
14411
16359
        return 0;
14412
16360
 
14459
16407
            param->quick_group=0;                       // UDF SUM function
14460
16408
          param->sum_func_count++;
14461
16409
 
14462
 
          for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
 
16410
          for (uint i=0 ; i < sum_item->arg_count ; i++)
14463
16411
          {
14464
16412
            if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
14465
16413
              param->field_count++;
14488
16436
*/
14489
16437
 
14490
16438
static bool
14491
 
test_if_subpart(order_st *a,order_st *b)
 
16439
test_if_subpart(ORDER *a,ORDER *b)
14492
16440
{
14493
16441
  for (; a && b; a=a->next,b=b->next)
14494
16442
  {
14505
16453
  and group and order is compatible, else return 0.
14506
16454
*/
14507
16455
 
14508
 
static Table *
14509
 
get_sort_by_table(order_st *a,order_st *b,TableList *tables)
 
16456
static TABLE *
 
16457
get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables)
14510
16458
{
14511
16459
  table_map map= (table_map) 0;
14512
16460
 
14536
16484
*/
14537
16485
 
14538
16486
static void
14539
 
calc_group_buffer(JOIN *join,order_st *group)
 
16487
calc_group_buffer(JOIN *join,ORDER *group)
14540
16488
{
14541
 
  uint32_t key_length=0, parts=0, null_parts=0;
 
16489
  uint key_length=0, parts=0, null_parts=0;
14542
16490
 
14543
16491
  if (group)
14544
16492
    join->group= 1;
14549
16497
    if (field)
14550
16498
    {
14551
16499
      enum_field_types type;
14552
 
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
 
16500
      if ((type= field->type()) == MYSQL_TYPE_BLOB)
14553
16501
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
14554
 
      else if (type == DRIZZLE_TYPE_VARCHAR)
 
16502
      else if (type == MYSQL_TYPE_VARCHAR || type == MYSQL_TYPE_VAR_STRING)
14555
16503
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
14556
16504
      else
14557
16505
        key_length+= field->pack_length();
14563
16511
        key_length+= sizeof(double);
14564
16512
        break;
14565
16513
      case INT_RESULT:
14566
 
        key_length+= sizeof(int64_t);
 
16514
        key_length+= sizeof(longlong);
14567
16515
        break;
14568
16516
      case DECIMAL_RESULT:
14569
16517
        key_length+= my_decimal_get_binary_size(group_item->max_length - 
14578
16526
          have STRING_RESULT result type, we increase the length 
14579
16527
          by 8 as maximum pack length of such fields.
14580
16528
        */
14581
 
        if (type == DRIZZLE_TYPE_TIME ||
14582
 
            type == DRIZZLE_TYPE_NEWDATE ||
14583
 
            type == DRIZZLE_TYPE_DATETIME ||
14584
 
            type == DRIZZLE_TYPE_TIMESTAMP)
 
16529
        if (type == MYSQL_TYPE_TIME ||
 
16530
            type == MYSQL_TYPE_NEWDATE ||
 
16531
            type == MYSQL_TYPE_DATETIME ||
 
16532
            type == MYSQL_TYPE_TIMESTAMP)
14585
16533
        {
14586
16534
          key_length+= 8;
14587
16535
        }
14650
16598
*/
14651
16599
 
14652
16600
static bool
14653
 
alloc_group_fields(JOIN *join,order_st *group)
 
16601
alloc_group_fields(JOIN *join,ORDER *group)
14654
16602
{
14655
16603
  if (group)
14656
16604
  {
14727
16675
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
14728
16676
                  Item **ref_pointer_array,
14729
16677
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
14730
 
                  uint32_t elements, List<Item> &all_fields)
 
16678
                  uint elements, List<Item> &all_fields)
14731
16679
{
14732
16680
  Item *pos;
14733
16681
  List_iterator_fast<Item> li(all_fields);
14736
16684
  res_all_fields.empty();
14737
16685
  List_iterator_fast<Item> itr(res_all_fields);
14738
16686
  List<Item> extra_funcs;
14739
 
  uint32_t i, border= all_fields.elements - elements;
 
16687
  uint i, border= all_fields.elements - elements;
14740
16688
 
14741
16689
  if (param->field_count && 
14742
16690
      !(copy=param->copy_field= new Copy_field[param->field_count]))
14746
16694
  for (i= 0; (pos= li++); i++)
14747
16695
  {
14748
16696
    Field *field;
14749
 
    unsigned char *tmp;
 
16697
    uchar *tmp;
14750
16698
    Item *real_pos= pos->real_item();
14751
16699
    if (real_pos->type() == Item::FIELD_ITEM)
14752
16700
    {
14789
16737
        /*
14790
16738
          We need to allocate one extra byte for null handling and
14791
16739
          another extra byte to not get warnings from purify in
14792
 
          Field_varstring::val_int
 
16740
          Field_string::val_int
14793
16741
        */
14794
 
        if (!(tmp= (unsigned char*) sql_alloc(field->pack_length()+2)))
 
16742
        if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
14795
16743
          goto err;
14796
16744
        if (copy)
14797
16745
        {
14819
16767
      */
14820
16768
      if (!(pos=new Item_copy_string(pos)))
14821
16769
        goto err;
14822
 
      if (i < border)                           // HAVING, order_st and GROUP BY
 
16770
      if (i < border)                           // HAVING, ORDER and GROUP BY
14823
16771
      {
14824
16772
        if (extra_funcs.push_back(pos))
14825
16773
          goto err;
14837
16785
    itr++;
14838
16786
  itr.sublist(res_selected_fields, elements);
14839
16787
  /*
14840
 
    Put elements from HAVING, order_st BY and GROUP BY last to ensure that any
 
16788
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
14841
16789
    reference used in these will resolve to a item that is already calculated
14842
16790
  */
14843
16791
  param->copy_funcs.concat(&extra_funcs);
14888
16836
 
14889
16837
bool JOIN::alloc_func_list()
14890
16838
{
14891
 
  uint32_t func_count, group_parts;
 
16839
  uint func_count, group_parts;
14892
16840
 
14893
16841
  func_count= tmp_table_param.sum_func_count;
14894
16842
  /*
14907
16855
  {
14908
16856
    group_parts+= fields_list.elements;
14909
16857
    /*
14910
 
      If the order_st clause is specified then it's possible that
 
16858
      If the ORDER clause is specified then it's possible that
14911
16859
      it also will be optimized, so reserve space for it too
14912
16860
    */
14913
16861
    if (order)
14914
16862
    {
14915
 
      order_st *ord;
 
16863
      ORDER *ord;
14916
16864
      for (ord= order; ord; ord= ord->next)
14917
16865
        group_parts++;
14918
16866
    }
14966
16914
  }
14967
16915
  else if (rollup.state == ROLLUP::STATE_NONE)
14968
16916
  {
14969
 
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
 
16917
    for (uint i=0 ; i <= send_group_parts ;i++)
14970
16918
      sum_funcs_end[i]= func;
14971
16919
  }
14972
16920
  else if (rollup.state == ROLLUP::STATE_READY)
14997
16945
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
14998
16946
                         List<Item> &res_selected_fields,
14999
16947
                         List<Item> &res_all_fields,
15000
 
                         uint32_t elements, List<Item> &all_fields)
 
16948
                         uint elements, List<Item> &all_fields)
15001
16949
{
15002
16950
  List_iterator_fast<Item> it(all_fields);
15003
16951
  Item *item_field,*item;
15005
16953
  res_selected_fields.empty();
15006
16954
  res_all_fields.empty();
15007
16955
 
15008
 
  uint32_t i, border= all_fields.elements - elements;
 
16956
  uint i, border= all_fields.elements - elements;
15009
16957
  for (i= 0; (item= it++); i++)
15010
16958
  {
15011
16959
    Field *field;
15076
17024
static bool
15077
17025
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
15078
17026
                          List<Item> &res_selected_fields,
15079
 
                          List<Item> &res_all_fields, uint32_t elements,
 
17027
                          List<Item> &res_all_fields, uint elements,
15080
17028
                          List<Item> &all_fields)
15081
17029
{
15082
17030
  List_iterator_fast<Item> it(all_fields);
15084
17032
  res_selected_fields.empty();
15085
17033
  res_all_fields.empty();
15086
17034
 
15087
 
  uint32_t i, border= all_fields.elements - elements;
 
17035
  uint i, border= all_fields.elements - elements;
15088
17036
  for (i= 0; (item= it++); i++)
15089
17037
  {
15090
17038
    res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
15144
17092
 
15145
17093
static void
15146
17094
update_tmptable_sum_func(Item_sum **func_ptr,
15147
 
                         Table *tmp_table __attribute__((unused)))
 
17095
                         TABLE *tmp_table __attribute__((unused)))
15148
17096
{
15149
17097
  Item_sum *func;
15150
17098
  while ((func= *(func_ptr++)))
15213
17161
    return(false);
15214
17162
 
15215
17163
  Item_cond_and *cond=new Item_cond_and();
15216
 
  Table *table=join_tab->table;
 
17164
  TABLE *table=join_tab->table;
15217
17165
  int error;
15218
17166
  if (!cond)
15219
17167
    return(true);
15220
17168
 
15221
 
  for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
 
17169
  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
15222
17170
  {
15223
17171
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
15224
17172
                              fieldnr-1];
15250
17198
  @param select   pointer to st_select_lex which subselects joins we will free
15251
17199
*/
15252
17200
 
15253
 
void free_underlaid_joins(THD *thd __attribute__((unused)),
 
17201
void free_underlaid_joins(THD *thd __attribute__((__unused__)),
15254
17202
                          SELECT_LEX *select)
15255
17203
{
15256
17204
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
15303
17251
    1   on error
15304
17252
*/
15305
17253
 
15306
 
static bool change_group_ref(THD *thd, Item_func *expr, order_st *group_list,
 
17254
static bool change_group_ref(THD *thd, Item_func *expr, ORDER *group_list,
15307
17255
                             bool *changed)
15308
17256
{
15309
17257
  if (expr->arg_count)
15318
17266
      Item *item= *arg;
15319
17267
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
15320
17268
      {
15321
 
        order_st *group_tmp;
 
17269
        ORDER *group_tmp;
15322
17270
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
15323
17271
        {
15324
17272
          if (item->eq(*group_tmp->item,0))
15352
17300
 
15353
17301
bool JOIN::rollup_init()
15354
17302
{
15355
 
  uint32_t i,j;
 
17303
  uint i,j;
15356
17304
  Item **ref_array;
15357
17305
 
15358
17306
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
15396
17344
  Item *item;
15397
17345
  while ((item= it++))
15398
17346
  {
15399
 
    order_st *group_tmp;
 
17347
    ORDER *group_tmp;
15400
17348
    bool found_in_group= 0;
15401
17349
 
15402
17350
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
15425
17373
            return 1;
15426
17374
          new_item->fix_fields(thd, (Item **) 0);
15427
17375
          thd->change_item_tree(it.ref(), new_item);
15428
 
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
 
17376
          for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
15429
17377
          { 
15430
17378
            if (*tmp->item == item)
15431
17379
              thd->change_item_tree(tmp->item, new_item);
15472
17420
{
15473
17421
  List_iterator_fast<Item> it(fields_arg);
15474
17422
  Item *first_field= sel_fields.head();
15475
 
  uint32_t level;
 
17423
  uint level;
15476
17424
 
15477
17425
  /*
15478
17426
    Create field lists for the different levels
15497
17445
 
15498
17446
  for (level=0 ; level < send_group_parts ; level++)
15499
17447
  {
15500
 
    uint32_t i;
15501
 
    uint32_t pos= send_group_parts - level -1;
 
17448
    uint i;
 
17449
    uint pos= send_group_parts - level -1;
15502
17450
    bool real_fields= 0;
15503
17451
    Item *item;
15504
17452
    List_iterator<Item> new_it(rollup.fields[pos]);
15505
17453
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
15506
 
    order_st *start_group;
 
17454
    ORDER *start_group;
15507
17455
 
15508
17456
    /* Point to first hidden field */
15509
17457
    Item **ref_array= ref_array_start + fields_arg.elements-1;
15547
17495
      else 
15548
17496
      {
15549
17497
        /* Check if this is something that is part of this group by */
15550
 
        order_st *group_tmp;
 
17498
        ORDER *group_tmp;
15551
17499
        for (group_tmp= start_group, i= pos ;
15552
17500
             group_tmp ; group_tmp= group_tmp->next, i++)
15553
17501
        {
15601
17549
    1   If send_data_failed()
15602
17550
*/
15603
17551
 
15604
 
int JOIN::rollup_send_data(uint32_t idx)
 
17552
int JOIN::rollup_send_data(uint idx)
15605
17553
{
15606
 
  uint32_t i;
 
17554
  uint i;
15607
17555
  for (i= send_group_parts ; i-- > idx ; )
15608
17556
  {
15609
17557
    /* Get reference pointers to sum functions in place */
15610
 
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
17558
    memcpy((char*) ref_pointer_array,
 
17559
           (char*) rollup.ref_pointer_arrays[i],
15611
17560
           ref_pointer_array_size);
15612
17561
    if ((!having || having->val_int()))
15613
17562
    {
15642
17591
    1   if write_data_failed()
15643
17592
*/
15644
17593
 
15645
 
int JOIN::rollup_write_data(uint32_t idx, Table *table_arg)
 
17594
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
15646
17595
{
15647
 
  uint32_t i;
 
17596
  uint i;
15648
17597
  for (i= send_group_parts ; i-- > idx ; )
15649
17598
  {
15650
17599
    /* Get reference pointers to sum functions in place */
15651
 
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
17600
    memcpy((char*) ref_pointer_array,
 
17601
           (char*) rollup.ref_pointer_arrays[i],
15652
17602
           ref_pointer_array_size);
15653
17603
    if ((!having || having->val_int()))
15654
17604
    {
15708
17658
  THD *thd=join->thd;
15709
17659
  select_result *result=join->result;
15710
17660
  Item *item_null= new Item_null();
15711
 
  const CHARSET_INFO * const cs= system_charset_info;
 
17661
  CHARSET_INFO *cs= system_charset_info;
15712
17662
  int quick_type;
15713
17663
  /* Don't log this into the slow query log */
15714
17664
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
15720
17670
  */
15721
17671
  if (message)
15722
17672
  {
15723
 
    item_list.push_back(new Item_int((int32_t)
 
17673
    item_list.push_back(new Item_int((int32)
15724
17674
                                     join->select_lex->select_number));
15725
17675
    item_list.push_back(new Item_string(join->select_lex->type,
15726
17676
                                        strlen(join->select_lex->type), cs));
15727
 
    for (uint32_t i=0 ; i < 7; i++)
 
17677
    for (uint i=0 ; i < 7; i++)
15728
17678
      item_list.push_back(item_null);
15729
17679
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
15730
17680
      item_list.push_back(item_null);
15753
17703
    /* table */
15754
17704
    {
15755
17705
      SELECT_LEX *sl= join->unit->first_select();
15756
 
      uint32_t len= 6, lastop= 0;
 
17706
      uint len= 6, lastop= 0;
15757
17707
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
15758
17708
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
15759
17709
      {
15803
17753
  else
15804
17754
  {
15805
17755
    table_map used_tables=0;
15806
 
    for (uint32_t i=0 ; i < join->tables ; i++)
 
17756
    for (uint i=0 ; i < join->tables ; i++)
15807
17757
    {
15808
17758
      JOIN_TAB *tab=join->join_tab+i;
15809
 
      Table *table=tab->table;
15810
 
      TableList *table_list= tab->table->pos_in_table_list;
 
17759
      TABLE *table=tab->table;
 
17760
      TABLE_LIST *table_list= tab->table->pos_in_table_list;
15811
17761
      char buff[512]; 
15812
17762
      char buff1[512], buff2[512], buff3[512];
15813
17763
      char keylen_str_buf[64];
15824
17774
      quick_type= -1;
15825
17775
      item_list.empty();
15826
17776
      /* id */
15827
 
      item_list.push_back(new Item_uint((uint32_t)
 
17777
      item_list.push_back(new Item_uint((uint32)
15828
17778
                                       join->select_lex->select_number));
15829
17779
      /* select_type */
15830
17780
      item_list.push_back(new Item_string(join->select_lex->type,
15851
17801
      }
15852
17802
      else
15853
17803
      {
15854
 
        TableList *real_table= table->pos_in_table_list; 
 
17804
        TABLE_LIST *real_table= table->pos_in_table_list; 
15855
17805
        item_list.push_back(new Item_string(real_table->alias,
15856
17806
                                            strlen(real_table->alias),
15857
17807
                                            cs));
15863
17813
      /* Build "possible_keys" value and add it to item_list */
15864
17814
      if (!tab->keys.is_clear_all())
15865
17815
      {
15866
 
        uint32_t j;
 
17816
        uint j;
15867
17817
        for (j=0 ; j < table->s->keys ; j++)
15868
17818
        {
15869
17819
          if (tab->keys.is_set(j))
15885
17835
      if (tab->ref.key_parts)
15886
17836
      {
15887
17837
        KEY *key_info=table->key_info+ tab->ref.key;
15888
 
        register uint32_t length;
 
17838
        register uint length;
15889
17839
        item_list.push_back(new Item_string(key_info->name,
15890
17840
                                            strlen(key_info->name),
15891
17841
                                            system_charset_info));
15892
 
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) - 
 
17842
        length= longlong2str(tab->ref.key_length, keylen_str_buf, 10) - 
15893
17843
                keylen_str_buf;
15894
17844
        item_list.push_back(new Item_string(keylen_str_buf, length,
15895
17845
                                            system_charset_info));
15905
17855
      else if (tab->type == JT_NEXT)
15906
17856
      {
15907
17857
        KEY *key_info=table->key_info+ tab->index;
15908
 
        register uint32_t length;
 
17858
        register uint length;
15909
17859
        item_list.push_back(new Item_string(key_info->name,
15910
17860
                                            strlen(key_info->name),cs));
15911
 
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) - 
 
17861
        length= longlong2str(key_info->key_length, keylen_str_buf, 10) - 
15912
17862
                keylen_str_buf;
15913
17863
        item_list.push_back(new Item_string(keylen_str_buf, 
15914
17864
                                            length,
15924
17874
      }
15925
17875
      else
15926
17876
      {
15927
 
        if (table_list->schema_table && table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
17877
        if (table_list->schema_table &&
 
17878
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
15928
17879
        {
15929
17880
          const char *tmp_buff;
15930
17881
          int f_idx;
15973
17924
        else
15974
17925
          examined_rows= join->best_positions[i].records_read; 
15975
17926
 
15976
 
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows, 
 
17927
        item_list.push_back(new Item_int((longlong) (ulonglong) examined_rows, 
15977
17928
                                         MY_INT64_NUM_DECIMAL_DIGITS));
15978
17929
 
15979
17930
        /* Add "filtered" field to item_list. */
15988
17939
      }
15989
17940
 
15990
17941
      /* Build "Extra" field and add it to item_list. */
15991
 
      bool key_read=table->key_read;
 
17942
      my_bool key_read=table->key_read;
15992
17943
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
15993
17944
          table->covering_keys.is_set(tab->index))
15994
17945
        key_read=1;
16008
17959
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
16009
17960
        /* Skip initial "; "*/
16010
17961
        const char *str= extra.ptr();
16011
 
        uint32_t len= extra.length();
 
17962
        uint32 len= extra.length();
16012
17963
        if (len)
16013
17964
        {
16014
17965
          str += 2;
16018
17969
      }
16019
17970
      else
16020
17971
      {
16021
 
        uint32_t keyno= MAX_KEY;
 
17972
        uint keyno= MAX_KEY;
16022
17973
        if (tab->ref.key_parts)
16023
17974
          keyno= tab->ref.key;
16024
17975
        else if (tab->select && tab->select->quick)
16124
18075
        else if (tab->do_firstmatch)
16125
18076
        {
16126
18077
          extra.append(STRING_WITH_LEN("; FirstMatch("));
16127
 
          Table *prev_table=tab->do_firstmatch->table;
 
18078
          TABLE *prev_table=tab->do_firstmatch->table;
16128
18079
          if (prev_table->derived_select_number)
16129
18080
          {
16130
18081
            char namebuf[NAME_LEN];
16139
18090
          extra.append(STRING_WITH_LEN(")"));
16140
18091
        }
16141
18092
 
16142
 
        for (uint32_t part= 0; part < tab->ref.key_parts; part++)
 
18093
        for (uint part= 0; part < tab->ref.key_parts; part++)
16143
18094
        {
16144
18095
          if (tab->ref.cond_guards[part])
16145
18096
          {
16153
18104
 
16154
18105
        /* Skip initial "; "*/
16155
18106
        const char *str= extra.ptr();
16156
 
        uint32_t len= extra.length();
 
18107
        uint32 len= extra.length();
16157
18108
        if (len)
16158
18109
        {
16159
18110
          str += 2;
16188
18139
       sl= sl->next_select())
16189
18140
  {
16190
18141
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
16191
 
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
 
18142
    uint8 uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
16192
18143
    sl->type= (((&thd->lex->select_lex)==sl)?
16193
18144
               (sl->first_inner_unit() || sl->next_select() ? 
16194
18145
                "PRIMARY" : "SIMPLE"):
16219
18170
    thd->lex->current_select= first;
16220
18171
    unit->set_limit(unit->global_parameters);
16221
18172
    res= mysql_select(thd, &first->ref_pointer_array,
16222
 
                        (TableList*) first->table_list.first,
 
18173
                        (TABLE_LIST*) first->table_list.first,
16223
18174
                        first->with_wild, first->item_list,
16224
18175
                        first->where,
16225
18176
                        first->order_list.elements +
16226
18177
                        first->group_list.elements,
16227
 
                        (order_st*) first->order_list.first,
16228
 
                        (order_st*) first->group_list.first,
 
18178
                        (ORDER*) first->order_list.first,
 
18179
                        (ORDER*) first->group_list.first,
16229
18180
                        first->having,
16230
 
                        (order_st*) thd->lex->proc_list.first,
 
18181
                        (ORDER*) thd->lex->proc_list.first,
16231
18182
                        first->options | thd->options | SELECT_DESCRIBE,
16232
18183
                        result, unit, first);
16233
18184
  }
16235
18186
}
16236
18187
 
16237
18188
 
16238
 
static void print_table_array(THD *thd, String *str, TableList **table, 
16239
 
                              TableList **end)
 
18189
static void print_table_array(THD *thd, String *str, TABLE_LIST **table, 
 
18190
                              TABLE_LIST **end)
16240
18191
{
16241
18192
  (*table)->print(thd, str, QT_ORDINARY);
16242
18193
 
16243
 
  for (TableList **tbl= table + 1; tbl < end; tbl++)
 
18194
  for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
16244
18195
  {
16245
 
    TableList *curr= *tbl;
 
18196
    TABLE_LIST *curr= *tbl;
16246
18197
    if (curr->outer_join)
16247
18198
    {
16248
18199
      /* MySQL converts right to left joins */
16275
18226
 
16276
18227
static void print_join(THD *thd,
16277
18228
                       String *str,
16278
 
                       List<TableList> *tables,
16279
 
                       enum_query_type query_type __attribute__((unused)))
 
18229
                       List<TABLE_LIST> *tables,
 
18230
                       enum_query_type query_type __attribute__((__unused__)))
16280
18231
{
16281
18232
  /* List is reversed => we should reverse it before using */
16282
 
  List_iterator_fast<TableList> ti(*tables);
16283
 
  TableList **table= (TableList **)thd->alloc(sizeof(TableList*) *
 
18233
  List_iterator_fast<TABLE_LIST> ti(*tables);
 
18234
  TABLE_LIST **table= (TABLE_LIST **)thd->alloc(sizeof(TABLE_LIST*) *
16284
18235
                                                tables->elements);
16285
18236
  if (table == 0)
16286
18237
    return;  // out of memory
16287
18238
 
16288
 
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
 
18239
  for (TABLE_LIST **t= table + (tables->elements - 1); t >= table; t--)
16289
18240
    *t= ti++;
16290
18241
  
16291
18242
  /* 
16294
18245
  */
16295
18246
  if ((*table)->sj_inner_tables)
16296
18247
  {
16297
 
    TableList **end= table + tables->elements;
16298
 
    for (TableList **t2= table; t2!=end; t2++)
 
18248
    TABLE_LIST **end= table + tables->elements;
 
18249
    for (TABLE_LIST **t2= table; t2!=end; t2++)
16299
18250
    {
16300
18251
      if (!(*t2)->sj_inner_tables)
16301
18252
      {
16302
 
        TableList *tmp= *t2;
 
18253
        TABLE_LIST *tmp= *t2;
16303
18254
        *t2= *table;
16304
18255
        *table= tmp;
16305
18256
        break;
16337
18288
  if (key_name.length)
16338
18289
  {
16339
18290
    if (thd && !my_strnncoll(system_charset_info,
16340
 
                             (const unsigned char *)key_name.str, key_name.length, 
16341
 
                             (const unsigned char *)primary_key_name, 
 
18291
                             (const uchar *)key_name.str, key_name.length, 
 
18292
                             (const uchar *)primary_key_name, 
16342
18293
                             strlen(primary_key_name)))
16343
18294
      str->append(primary_key_name);
16344
18295
    else
16354
18305
  @param str   string where table should be printed
16355
18306
*/
16356
18307
 
16357
 
void TableList::print(THD *thd, String *str, enum_query_type query_type)
 
18308
void TABLE_LIST::print(THD *thd, String *str, enum_query_type query_type)
16358
18309
{
16359
18310
  if (nested_join)
16360
18311
  {
16402
18353
      {
16403
18354
        if (alias && alias[0])
16404
18355
        {
16405
 
          my_stpcpy(t_alias_buff, alias);
 
18356
          strmov(t_alias_buff, alias);
16406
18357
          my_casedn_str(files_charset_info, t_alias_buff);
16407
18358
          t_alias= t_alias_buff;
16408
18359
        }
16500
18451
  if (group_list.elements)
16501
18452
  {
16502
18453
    str->append(STRING_WITH_LEN(" group by "));
16503
 
    print_order(str, (order_st *) group_list.first, query_type);
 
18454
    print_order(str, (ORDER *) group_list.first, query_type);
16504
18455
    switch (olap)
16505
18456
    {
16506
18457
      case CUBE_TYPE:
16531
18482
  if (order_list.elements)
16532
18483
  {
16533
18484
    str->append(STRING_WITH_LEN(" order by "));
16534
 
    print_order(str, (order_st *) order_list.first, query_type);
 
18485
    print_order(str, (ORDER *) order_list.first, query_type);
16535
18486
  }
16536
18487
 
16537
18488
  // limit