~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_select.cc

  • Committer: Jay Pipes
  • Date: 2008-07-17 18:48:58 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080717184858-2mbouxl8xi41gcge
Removed DBUG from CSV and Blackhole storage engines

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/**
17
17
  @file
18
18
 
19
19
  @brief
20
 
  select_query and join optimization
 
20
  mysql_select and join optimization
 
21
 
21
22
 
22
23
  @defgroup Query_Optimizer  Query Optimizer
23
24
  @{
24
25
*/
25
 
#include <config.h>
26
 
 
27
 
#include <string>
28
 
#include <iostream>
29
 
#include <algorithm>
30
 
#include <vector>
31
 
 
32
 
#include <drizzled/sql_select.h> /* include join.h */
33
 
 
34
 
#include <drizzled/error.h>
35
 
#include <drizzled/gettext.h>
36
 
#include <drizzled/util/test.h>
37
 
#include <drizzled/name_resolution_context_state.h>
38
 
#include <drizzled/nested_join.h>
39
 
#include <drizzled/probes.h>
40
 
#include <drizzled/show.h>
41
 
#include <drizzled/item/cache.h>
42
 
#include <drizzled/item/cmpfunc.h>
43
 
#include <drizzled/item/copy_string.h>
44
 
#include <drizzled/item/uint.h>
45
 
#include <drizzled/cached_item.h>
46
 
#include <drizzled/sql_base.h>
47
 
#include <drizzled/field/blob.h>
48
 
#include <drizzled/check_stack_overrun.h>
49
 
#include <drizzled/lock.h>
50
 
#include <drizzled/item/outer_ref.h>
51
 
#include <drizzled/index_hint.h>
52
 
#include <drizzled/records.h>
53
 
#include <drizzled/internal/iocache.h>
54
 
#include <drizzled/drizzled.h>
55
 
#include <drizzled/plugin/storage_engine.h>
56
 
 
57
 
#include <drizzled/sql_union.h>
58
 
#include <drizzled/optimizer/key_field.h>
59
 
#include <drizzled/optimizer/position.h>
60
 
#include <drizzled/optimizer/sargable_param.h>
61
 
#include <drizzled/optimizer/key_use.h>
62
 
#include <drizzled/optimizer/range.h>
63
 
#include <drizzled/optimizer/quick_range_select.h>
64
 
#include <drizzled/optimizer/quick_ror_intersect_select.h>
65
 
 
66
 
#include <drizzled/filesort.h>
67
 
#include <drizzled/sql_lex.h>
68
 
#include <drizzled/session.h>
69
 
#include <drizzled/sort_field.h>
70
 
#include <drizzled/select_result.h>
71
 
 
72
 
using namespace std;
73
 
 
74
 
namespace drizzled
75
 
{
76
 
 
77
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
78
 
static COND *build_equal_items(Session *session, COND *cond,
 
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>
 
37
 
 
38
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
 
39
                              "MAYBE_REF","ALL","range","index",
 
40
                              "ref_or_null","unique_subquery","index_subquery",
 
41
                              "index_merge"
 
42
};
 
43
 
 
44
struct st_sargable_param;
 
45
 
 
46
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
 
47
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
 
48
                                 DYNAMIC_ARRAY *keyuse);
 
49
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
 
50
                                JOIN_TAB *join_tab,
 
51
                                uint tables, COND *conds,
 
52
                                COND_EQUAL *cond_equal,
 
53
                                table_map table_map, SELECT_LEX *select_lex,
 
54
                                st_sargable_param **sargables);
 
55
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
 
56
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
 
57
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
58
                               table_map used_tables);
 
59
static bool choose_plan(JOIN *join,table_map join_tables);
 
60
 
 
61
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
 
62
                             table_map remaining_tables, uint idx,
 
63
                             double record_count, double read_time);
 
64
static void optimize_straight_join(JOIN *join, table_map join_tables);
 
65
static bool greedy_search(JOIN *join, table_map remaining_tables,
 
66
                             uint depth, uint prune_level);
 
67
static bool best_extension_by_limited_search(JOIN *join,
 
68
                                             table_map remaining_tables,
 
69
                                             uint idx, double record_count,
 
70
                                             double read_time, uint depth,
 
71
                                             uint prune_level);
 
72
static uint determine_search_depth(JOIN* join);
 
73
static int join_tab_cmp(const void* ptr1, const void* ptr2);
 
74
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
 
75
/*
 
76
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
 
77
  tested and approved.
 
78
*/
 
79
static bool find_best(JOIN *join,table_map rest_tables,uint index,
 
80
                      double record_count,double read_time);
 
81
static uint cache_record_length(JOIN *join,uint index);
 
82
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
 
83
static bool get_best_combination(JOIN *join);
 
84
static store_key *get_store_key(THD *thd,
 
85
                                KEYUSE *keyuse, table_map used_tables,
 
86
                                KEY_PART_INFO *key_part, uchar *key_buff,
 
87
                                uint maybe_null);
 
88
static bool make_simple_join(JOIN *join,TABLE *tmp_table);
 
89
static void make_outerjoin_info(JOIN *join);
 
90
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
 
91
static bool make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after);
 
92
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables);
 
93
static void update_depend_map(JOIN *join);
 
94
static void update_depend_map(JOIN *join, ORDER *order);
 
95
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond,
 
96
                           bool change_list, bool *simple_order);
 
97
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables,
 
98
                            List<Item> &fields, bool send_row,
 
99
                            uint64_t select_options, const char *info,
 
100
                            Item *having);
 
101
static COND *build_equal_items(THD *thd, COND *cond,
79
102
                               COND_EQUAL *inherited,
80
 
                               List<TableList> *join_list,
 
103
                               List<TABLE_LIST> *join_list,
81
104
                               COND_EQUAL **cond_equal_ref);
82
 
 
83
 
static Item* part_of_refkey(Table *form,Field *field);
84
 
static bool cmp_buffer_with_ref(JoinTable *tab);
85
 
static void change_cond_ref_to_const(Session *session,
86
 
                                     list<COND_CMP>& save_list,
87
 
                                     Item *and_father,
88
 
                                     Item *cond,
89
 
                                     Item *field,
90
 
                                     Item *value);
91
 
static bool copy_blobs(Field **ptr);
92
 
 
93
 
static bool eval_const_cond(COND *cond)
94
 
{
95
 
    return ((Item_func*) cond)->val_int() ? true : false;
96
 
}
 
105
static COND* substitute_for_best_equal_field(COND *cond,
 
106
                                             COND_EQUAL *cond_equal,
 
107
                                             void *table_join_idx);
 
108
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
 
109
                            COND *conds, bool top, bool in_sj);
 
110
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
 
111
static void restore_prev_nj_state(JOIN_TAB *last);
 
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);
 
115
 
 
116
static 
 
117
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
 
118
static void restore_prev_sj_state(const table_map remaining_tables, 
 
119
                                  const JOIN_TAB *tab);
 
120
 
 
121
static COND *optimize_cond(JOIN *join, COND *conds,
 
122
                           List<TABLE_LIST> *join_list,
 
123
                           Item::cond_result *cond_value);
 
124
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
 
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
                                    uint64_t options);
 
130
static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table);
 
131
 
 
132
static enum_nested_loop_state
 
133
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
134
                     int error);
 
135
static enum_nested_loop_state
 
136
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
 
137
static enum_nested_loop_state
 
138
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
 
139
static enum_nested_loop_state
 
140
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
141
static enum_nested_loop_state
 
142
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
143
static enum_nested_loop_state
 
144
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
145
static enum_nested_loop_state
 
146
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
147
 
 
148
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
 
149
static int join_read_system(JOIN_TAB *tab);
 
150
static int join_read_const(JOIN_TAB *tab);
 
151
static int join_read_key(JOIN_TAB *tab);
 
152
static int join_read_always_key(JOIN_TAB *tab);
 
153
static int join_read_last_key(JOIN_TAB *tab);
 
154
static int join_no_more_records(READ_RECORD *info);
 
155
static int join_read_next(READ_RECORD *info);
 
156
static int join_read_next_different(READ_RECORD *info);
 
157
static int join_init_quick_read_record(JOIN_TAB *tab);
 
158
static int test_if_quick_select(JOIN_TAB *tab);
 
159
static int join_init_read_record(JOIN_TAB *tab);
 
160
static int join_read_first(JOIN_TAB *tab);
 
161
static int join_read_next_same(READ_RECORD *info);
 
162
static int join_read_next_same_diff(READ_RECORD *info);
 
163
static int join_read_last(JOIN_TAB *tab);
 
164
static int join_read_prev_same(READ_RECORD *info);
 
165
static int join_read_prev(READ_RECORD *info);
 
166
int join_read_always_key_or_null(JOIN_TAB *tab);
 
167
int join_read_next_same_or_null(READ_RECORD *info);
 
168
static COND *make_cond_for_table(COND *cond,table_map table,
 
169
                                 table_map used_table,
 
170
                                 bool exclude_expensive_cond);
 
171
static Item* part_of_refkey(TABLE *form,Field *field);
 
172
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
 
173
                                    ha_rows select_limit, bool no_changes,
 
174
                                    const key_map *map);
 
175
static bool list_contains_unique_index(TABLE *table,
 
176
                          bool (*find_func) (Field *, void *), void *data);
 
177
static bool find_field_in_item_list (Field *field, void *data);
 
178
static bool find_field_in_order_list (Field *field, void *data);
 
179
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
 
180
                             ha_rows filesort_limit, ha_rows select_limit,
 
181
                             bool is_order_by);
 
182
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
 
183
                             Item *having);
 
184
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
 
185
                                   ulong offset,Item *having);
 
186
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
 
187
                                      uint field_count, Field **first_field,
 
188
 
 
189
                                      ulong key_length,Item *having);
 
190
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
 
191
static ulong used_blob_length(CACHE_FIELD **ptr);
 
192
static bool store_record_in_cache(JOIN_CACHE *cache);
 
193
static void reset_cache_read(JOIN_CACHE *cache);
 
194
static void reset_cache_write(JOIN_CACHE *cache);
 
195
static void read_cached_record(JOIN_TAB *tab);
 
196
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
 
197
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
 
198
                                    ORDER *order, List<Item> &fields,
 
199
                                    List<Item> &all_fields,
 
200
                                    bool *all_order_by_fields_used);
 
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);
 
204
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
 
205
static bool alloc_group_fields(JOIN *join,ORDER *group);
 
206
// Create list for using with tempory table
 
207
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
208
                                     List<Item> &new_list1,
 
209
                                     List<Item> &new_list2,
 
210
                                     uint elements, List<Item> &items);
 
211
// Create list for using with tempory table
 
212
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
213
                                      List<Item> &new_list1,
 
214
                                      List<Item> &new_list2,
 
215
                                      uint elements, List<Item> &items);
 
216
static void init_tmptable_sum_functions(Item_sum **func);
 
217
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
 
218
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
 
219
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
 
220
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
 
221
static bool init_sum_functions(Item_sum **func, Item_sum **end);
 
222
static bool update_sum_func(Item_sum **func);
 
223
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
 
224
                            bool distinct, const char *message=NullS);
 
225
static Item *remove_additional_cond(Item* conds);
 
226
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
 
227
static bool test_if_ref(Item_field *left_item,Item *right_item);
 
228
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
229
                                       Item *new_cond, bool fix_fields);
97
230
 
98
231
/*
99
232
  This is used to mark equalities that were made from i-th IN-equality.
103
236
const char *subq_sj_cond_name=
104
237
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
105
238
 
106
 
static bool copy_blobs(Field **ptr)
 
239
static bool bitmap_covers(const table_map x, const table_map y)
107
240
{
108
 
  for (; *ptr ; ptr++)
109
 
  {
110
 
    if ((*ptr)->flags & BLOB_FLAG)
111
 
      if (((Field_blob *) (*ptr))->copy())
112
 
        return 1;                               // Error
113
 
  }
114
 
  return 0;
 
241
  return !test(y & ~x);
115
242
}
116
243
 
117
244
/**
118
245
  This handles SELECT with and without UNION.
119
246
*/
120
 
bool handle_select(Session *session, LEX *lex, select_result *result,
121
 
                   uint64_t setup_tables_done_option)
 
247
 
 
248
bool handle_select(THD *thd, LEX *lex, select_result *result,
 
249
                   ulong setup_tables_done_option)
122
250
{
123
251
  bool res;
124
 
  register Select_Lex *select_lex= &lex->select_lex;
125
 
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
 
252
  register SELECT_LEX *select_lex = &lex->select_lex;
 
253
  MYSQL_SELECT_START();
126
254
 
127
 
  if (select_lex->master_unit()->is_union() ||
 
255
  if (select_lex->master_unit()->is_union() || 
128
256
      select_lex->master_unit()->fake_select_lex)
129
 
  {
130
 
    res= drizzle_union(session, lex, result, &lex->unit,
131
 
                       setup_tables_done_option);
132
 
  }
 
257
    res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
133
258
  else
134
259
  {
135
 
    Select_Lex_Unit *unit= &lex->unit;
 
260
    SELECT_LEX_UNIT *unit= &lex->unit;
136
261
    unit->set_limit(unit->global_parameters);
137
 
    session->session_marker= 0;
 
262
    thd->thd_marker= 0;
138
263
    /*
139
 
      'options' of select_query will be set in JOIN, as far as JOIN for
140
 
      every PS/SP execution new, we will not need reset this flag if
 
264
      'options' of mysql_select will be set in JOIN, as far as JOIN for
 
265
      every PS/SP execution new, we will not need reset this flag if 
141
266
      setup_tables_done_option changed for next rexecution
142
267
    */
143
 
    res= select_query(session,
144
 
                      &select_lex->ref_pointer_array,
145
 
                      (TableList*) select_lex->table_list.first,
146
 
                      select_lex->with_wild,
147
 
                      select_lex->item_list,
 
268
    res= mysql_select(thd, &select_lex->ref_pointer_array,
 
269
                      (TABLE_LIST*) select_lex->table_list.first,
 
270
                      select_lex->with_wild, select_lex->item_list,
148
271
                      select_lex->where,
149
272
                      select_lex->order_list.elements +
150
273
                      select_lex->group_list.elements,
151
 
                      (Order*) select_lex->order_list.first,
152
 
                      (Order*) select_lex->group_list.first,
 
274
                      (ORDER*) select_lex->order_list.first,
 
275
                      (ORDER*) select_lex->group_list.first,
153
276
                      select_lex->having,
154
 
                      select_lex->options | session->options |
 
277
                      (ORDER*) lex->proc_list.first,
 
278
                      select_lex->options | thd->options |
155
279
                      setup_tables_done_option,
156
280
                      result, unit, select_lex);
157
281
  }
158
 
  res|= session->is_error();
 
282
  res|= thd->is_error();
159
283
  if (unlikely(res))
160
284
    result->abort();
161
285
 
162
 
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
163
 
  return res;
 
286
  MYSQL_SELECT_END();
 
287
  return(res);
164
288
}
165
289
 
 
290
 
166
291
/*
167
292
  Fix fields referenced from inner selects.
168
293
 
169
294
  SYNOPSIS
170
295
    fix_inner_refs()
171
 
    session               Thread handle
 
296
    thd               Thread handle
172
297
    all_fields        List of all fields used in select
173
298
    select            Current select
174
299
    ref_pointer_array Array of references to Items used in current select
203
328
    true  an error occured
204
329
    false ok
205
330
*/
206
 
bool fix_inner_refs(Session *session, 
207
 
                    List<Item> &all_fields, 
208
 
                    Select_Lex *select, 
209
 
                    Item **ref_pointer_array)
 
331
 
 
332
bool
 
333
fix_inner_refs(THD *thd, List<Item> &all_fields, SELECT_LEX *select,
 
334
                 Item **ref_pointer_array)
210
335
{
211
336
  Item_outer_ref *ref;
212
337
  bool res= false;
213
338
  bool direct_ref= false;
214
339
 
215
 
  List<Item_outer_ref>::iterator ref_it(select->inner_refs_list.begin());
 
340
  List_iterator<Item_outer_ref> ref_it(select->inner_refs_list);
216
341
  while ((ref= ref_it++))
217
342
  {
218
343
    Item *item= ref->outer_ref;
219
344
    Item **item_ref= ref->ref;
220
345
    Item_ref *new_ref;
221
346
    /*
222
 
      @todo this field item already might be present in the select list.
 
347
      TODO: this field item already might be present in the select list.
223
348
      In this case instead of adding new field item we could use an
224
349
      existing one. The change will lead to less operations for copying fields,
225
350
      smaller temporary tables and less data passed through filesort.
266
391
    ref->outer_ref= new_ref;
267
392
    ref->ref= &ref->outer_ref;
268
393
 
269
 
    if (!ref->fixed && ref->fix_fields(session, 0))
 
394
    if (!ref->fixed && ref->fix_fields(thd, 0))
270
395
      return true;
271
 
    session->used_tables|= item->used_tables();
 
396
    thd->used_tables|= item->used_tables();
272
397
  }
273
398
  return res;
274
399
}
275
400
 
 
401
#define MAGIC_IN_WHERE_TOP_LEVEL 10
 
402
/**
 
403
  Function to setup clauses without sum functions.
 
404
*/
 
405
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
 
406
                               TABLE_LIST *tables,
 
407
                               TABLE_LIST *leaves,
 
408
                               List<Item> &fields,
 
409
                               List<Item> &all_fields,
 
410
                               COND **conds,
 
411
                               ORDER *order,
 
412
                               ORDER *group, bool *hidden_group_fields)
 
413
{
 
414
  int res;
 
415
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
 
416
 
 
417
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
418
  res= setup_conds(thd, tables, leaves, conds);
 
419
 
 
420
  thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
 
421
  res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
 
422
                          order);
 
423
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
424
  res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
 
425
                          group, hidden_group_fields);
 
426
  thd->lex->allow_sum_func= save_allow_sum_func;
 
427
  return(res);
 
428
}
 
429
 
276
430
/*****************************************************************************
277
431
  Check fields, find best join, do the select and output fields.
278
 
  select_query assumes that all tables are already opened
 
432
  mysql_select assumes that all tables are already opened
279
433
*****************************************************************************/
280
434
 
 
435
/**
 
436
  Prepare of whole select (including sub queries in future).
 
437
 
 
438
  @todo
 
439
    Add check of calculation of GROUP functions and fields:
 
440
    SELECT COUNT(*)+table.col1 from table1;
 
441
 
 
442
  @retval
 
443
    -1   on error
 
444
  @retval
 
445
    0   on success
 
446
*/
 
447
int
 
448
JOIN::prepare(Item ***rref_pointer_array,
 
449
              TABLE_LIST *tables_init,
 
450
              uint wild_num, COND *conds_init, uint og_num,
 
451
              ORDER *order_init, ORDER *group_init,
 
452
              Item *having_init,
 
453
              ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
 
454
              SELECT_LEX_UNIT *unit_arg)
 
455
{
 
456
  // to prevent double initialization on EXPLAIN
 
457
  if (optimized)
 
458
    return(0);
 
459
 
 
460
  conds= conds_init;
 
461
  order= order_init;
 
462
  group_list= group_init;
 
463
  having= having_init;
 
464
  proc_param= proc_param_init;
 
465
  tables_list= tables_init;
 
466
  select_lex= select_lex_arg;
 
467
  select_lex->join= this;
 
468
  join_list= &select_lex->top_join_list;
 
469
  union_part= unit_arg->is_union();
 
470
 
 
471
  thd->lex->current_select->is_item_list_lookup= 1;
 
472
  /*
 
473
    If we have already executed SELECT, then it have not sense to prevent
 
474
    its table from update (see unique_table())
 
475
  */
 
476
  if (thd->derived_tables_processing)
 
477
    select_lex->exclude_from_table_unique_test= true;
 
478
 
 
479
  /* Check that all tables, fields, conds and order are ok */
 
480
 
 
481
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
 
482
      setup_tables_and_check_access(thd, &select_lex->context, join_list,
 
483
                                    tables_list, &select_lex->leaf_tables,
 
484
                                    false))
 
485
      return(-1);
 
486
 
 
487
  TABLE_LIST *table_ptr;
 
488
  for (table_ptr= select_lex->leaf_tables;
 
489
       table_ptr;
 
490
       table_ptr= table_ptr->next_leaf)
 
491
    tables++;
 
492
 
 
493
  if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
 
494
      select_lex->setup_ref_array(thd, og_num) ||
 
495
      setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
 
496
                   &all_fields, 1) ||
 
497
      setup_without_group(thd, (*rref_pointer_array), tables_list,
 
498
                          select_lex->leaf_tables, fields_list,
 
499
                          all_fields, &conds, order, group_list,
 
500
                          &hidden_group_fields))
 
501
    return(-1);                         /* purecov: inspected */
 
502
 
 
503
  ref_pointer_array= *rref_pointer_array;
 
504
  
 
505
  if (having)
 
506
  {
 
507
    nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
 
508
    thd->where="having clause";
 
509
    thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
510
    select_lex->having_fix_field= 1;
 
511
    bool having_fix_rc= (!having->fixed &&
 
512
                         (having->fix_fields(thd, &having) ||
 
513
                          having->check_cols(1)));
 
514
    select_lex->having_fix_field= 0;
 
515
    if (having_fix_rc || thd->is_error())
 
516
      return(-1);                               /* purecov: inspected */
 
517
    thd->lex->allow_sum_func= save_allow_sum_func;
 
518
  }
 
519
 
 
520
  if (!thd->lex->view_prepare_mode)
 
521
  {
 
522
    Item_subselect *subselect;
 
523
    Item_in_subselect *in_subs= NULL;
 
524
    /*
 
525
      Are we in a subquery predicate?
 
526
      TODO: the block below will be executed for every PS execution without need.
 
527
    */
 
528
    if ((subselect= select_lex->master_unit()->item))
 
529
    {
 
530
      bool do_semijoin= !test(thd->variables.optimizer_switch &
 
531
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
 
532
      if (subselect->substype() == Item_subselect::IN_SUBS)
 
533
        in_subs= (Item_in_subselect*)subselect;
 
534
 
 
535
      /*
 
536
        Check if we're in subquery that is a candidate for flattening into a
 
537
        semi-join (which is done done in flatten_subqueries()). The
 
538
        requirements are:
 
539
          1. Subquery predicate is an IN/=ANY subq predicate
 
540
          2. Subquery is a single SELECT (not a UNION)
 
541
          3. Subquery does not have GROUP BY or ORDER BY
 
542
          4. Subquery does not use aggregate functions or HAVING
 
543
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
 
544
          6. No execution method was already chosen (by a prepared statement).
 
545
 
 
546
          (*). We are not in a subquery of a single table UPDATE/DELETE that 
 
547
               doesn't have a JOIN (TODO: We should handle this at some
 
548
               point by switching to multi-table UPDATE/DELETE)
 
549
 
 
550
          (**). We're not in a confluent table-less subquery, like
 
551
                "SELECT 1". 
 
552
      */
 
553
      if (in_subs &&                                                    // 1
 
554
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
555
          !select_lex->group_list.elements && !order &&                 // 3
 
556
          !having && !select_lex->with_sum_func &&                      // 4
 
557
          thd->thd_marker &&                                            // 5
 
558
          select_lex->outer_select()->join &&                           // (*)
 
559
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**) 
 
560
          do_semijoin &&
 
561
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
 
562
      {
 
563
        {
 
564
          if (!in_subs->left_expr->fixed &&
 
565
               in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
 
566
          {
 
567
            return(-1);
 
568
          }
 
569
          /*
 
570
            Check that the right part of the subselect contains no more than one
 
571
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
 
572
          */
 
573
          if (subselect->substype() == Item_subselect::IN_SUBS &&
 
574
             (select_lex->item_list.elements != 
 
575
              ((Item_in_subselect*)subselect)->left_expr->cols()))
 
576
          {
 
577
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
 
578
            return(-1);
 
579
          }
 
580
        }
 
581
 
 
582
        /* Register the subquery for further processing */
 
583
        select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
 
584
        in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
 
585
      }
 
586
      else
 
587
      {
 
588
        bool do_materialize= !test(thd->variables.optimizer_switch &
 
589
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
 
590
        /*
 
591
          Check if the subquery predicate can be executed via materialization.
 
592
          The required conditions are:
 
593
          1. Subquery predicate is an IN/=ANY subq predicate
 
594
          2. Subquery is a single SELECT (not a UNION)
 
595
          3. Subquery is not a table-less query. In this case there is no
 
596
             point in materializing.
 
597
          4. Subquery predicate is a top-level predicate
 
598
             (this implies it is not negated)
 
599
             TODO: this is a limitation that should be lifeted once we
 
600
             implement correct NULL semantics (WL#3830)
 
601
          5. Subquery is non-correlated
 
602
             TODO:
 
603
             This is an overly restrictive condition. It can be extended to:
 
604
             (Subquery is non-correlated ||
 
605
              Subquery is correlated to any query outer to IN predicate ||
 
606
              (Subquery is correlated to the immediate outer query &&
 
607
               Subquery !contains {GROUP BY, ORDER BY [LIMIT],
 
608
               aggregate functions) && subquery predicate is not under "NOT IN"))
 
609
          6. No execution method was already chosen (by a prepared statement).
 
610
 
 
611
          (*) The subquery must be part of a SELECT statement. The current
 
612
               condition also excludes multi-table update statements.
 
613
 
 
614
          We have to determine whether we will perform subquery materialization
 
615
          before calling the IN=>EXISTS transformation, so that we know whether to
 
616
          perform the whole transformation or only that part of it which wraps
 
617
          Item_in_subselect in an Item_in_optimizer.
 
618
        */
 
619
        if (do_materialize && 
 
620
            in_subs  &&                                                   // 1
 
621
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
622
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
 
623
            thd->lex->sql_command == SQLCOM_SELECT)                       // *
 
624
        {
 
625
          if (in_subs->is_top_level_item() &&                             // 4
 
626
              !in_subs->is_correlated &&                                  // 5
 
627
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
 
628
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
 
629
        }
 
630
 
 
631
        Item_subselect::trans_res trans_res;
 
632
        if ((trans_res= subselect->select_transformer(this)) !=
 
633
            Item_subselect::RES_OK)
 
634
        {
 
635
          select_lex->fix_prepare_information(thd, &conds, &having);
 
636
          return((trans_res == Item_subselect::RES_ERROR));
 
637
        }
 
638
      }
 
639
    }
 
640
  }
 
641
 
 
642
  select_lex->fix_prepare_information(thd, &conds, &having);
 
643
 
 
644
  if (order)
 
645
  {
 
646
    ORDER *ord;
 
647
    for (ord= order; ord; ord= ord->next)
 
648
    {
 
649
      Item *item= *ord->item;
 
650
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
 
651
        item->split_sum_func(thd, ref_pointer_array, all_fields);
 
652
    }
 
653
  }
 
654
 
 
655
  if (having && having->with_sum_func)
 
656
    having->split_sum_func2(thd, ref_pointer_array, all_fields,
 
657
                            &having, true);
 
658
  if (select_lex->inner_sum_func_list)
 
659
  {
 
660
    Item_sum *end=select_lex->inner_sum_func_list;
 
661
    Item_sum *item_sum= end;  
 
662
    do
 
663
    { 
 
664
      item_sum= item_sum->next;
 
665
      item_sum->split_sum_func2(thd, ref_pointer_array,
 
666
                                all_fields, item_sum->ref_by, false);
 
667
    } while (item_sum != end);
 
668
  }
 
669
 
 
670
  if (select_lex->inner_refs_list.elements &&
 
671
      fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
 
672
    return(-1);
 
673
 
 
674
  /*
 
675
    Check if there are references to un-aggregated columns when computing 
 
676
    aggregate functions with implicit grouping (there is no GROUP BY).
 
677
 
 
678
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
 
679
  */
 
680
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
 
681
  {
 
682
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
 
683
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
 
684
    return(-1);
 
685
  }
 
686
  {
 
687
    /* Caclulate the number of groups */
 
688
    send_group_parts= 0;
 
689
    for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
690
      send_group_parts++;
 
691
  }
 
692
  
 
693
  if (error)
 
694
    goto err;                                   /* purecov: inspected */
 
695
 
 
696
  if (result && result->prepare(fields_list, unit_arg))
 
697
    goto err;                                   /* purecov: inspected */
 
698
 
 
699
  /* Init join struct */
 
700
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
701
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
702
  this->group= group_list != 0;
 
703
  unit= unit_arg;
 
704
 
 
705
#ifdef RESTRICTED_GROUP
 
706
  if (sum_func_count && !group_list && (func_count || field_count))
 
707
  {
 
708
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
 
709
    goto err;
 
710
  }
 
711
#endif
 
712
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
 
713
    goto err;
 
714
  if (alloc_func_list())
 
715
    goto err;
 
716
 
 
717
  return(0); // All OK
 
718
 
 
719
err:
 
720
  return(-1);                           /* purecov: inspected */
 
721
}
 
722
 
 
723
 
 
724
/*
 
725
  Remove the predicates pushed down into the subquery
 
726
 
 
727
  SYNOPSIS
 
728
    JOIN::remove_subq_pushed_predicates()
 
729
      where   IN  Must be NULL
 
730
              OUT The remaining WHERE condition, or NULL
 
731
 
 
732
  DESCRIPTION
 
733
    Given that this join will be executed using (unique|index)_subquery,
 
734
    without "checking NULL", remove the predicates that were pushed down
 
735
    into the subquery.
 
736
 
 
737
    If the subquery compares scalar values, we can remove the condition that
 
738
    was wrapped into trig_cond (it will be checked when needed by the subquery
 
739
    engine)
 
740
 
 
741
    If the subquery compares row values, we need to keep the wrapped
 
742
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
 
743
    and non-NULL values, we'll do a full table scan and will rely on the
 
744
    equalities corresponding to non-NULL parts of left tuple to filter out
 
745
    non-matching records.
 
746
 
 
747
    TODO: We can remove the equalities that will be guaranteed to be true by the
 
748
    fact that subquery engine will be using index lookup. This must be done only
 
749
    for cases where there are no conversion errors of significance, e.g. 257
 
750
    that is searched in a byte. But this requires homogenization of the return 
 
751
    codes of all Field*::store() methods.
 
752
*/
 
753
 
 
754
void JOIN::remove_subq_pushed_predicates(Item **where)
 
755
{
 
756
  if (conds->type() == Item::FUNC_ITEM &&
 
757
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
 
758
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
 
759
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
 
760
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
 
761
                   ((Item_func *)conds)->arguments()[0]))
 
762
  {
 
763
    *where= 0;
 
764
    return;
 
765
  }
 
766
}
 
767
 
 
768
 
281
769
/*
282
770
  Index lookup-based subquery: save some flags for EXPLAIN output
283
771
 
290
778
  DESCRIPTION
291
779
    For index lookup-based subquery (i.e. one executed with
292
780
    subselect_uniquesubquery_engine or subselect_indexsubquery_engine),
293
 
    check its EXPLAIN output row should contain
294
 
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL)
 
781
    check its EXPLAIN output row should contain 
 
782
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL) 
295
783
      "Using Where" (TAB_INFO_USING_WHERE)
296
784
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
297
785
    and set appropriate flags in join_tab->packed_info.
298
786
*/
299
 
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
 
787
 
 
788
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
300
789
{
301
790
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
302
 
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
 
791
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
303
792
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
304
793
  if (where)
305
794
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
306
 
  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
 
795
  for (uint i = 0; i < join_tab->ref.key_parts; i++)
307
796
  {
308
797
    if (join_tab->ref.cond_guards[i])
309
798
    {
313
802
  }
314
803
}
315
804
 
 
805
 
 
806
 
 
807
 
 
808
/*
 
809
  Check if the table's rowid is included in the temptable
 
810
 
 
811
  SYNOPSIS
 
812
    sj_table_is_included()
 
813
      join      The join
 
814
      join_tab  The table to be checked
 
815
 
 
816
  DESCRIPTION
 
817
    SemiJoinDuplicateElimination: check the table's rowid should be included
 
818
    in the temptable. This is so if
 
819
 
 
820
    1. The table is not embedded within some semi-join nest
 
821
    2. The has been pulled out of a semi-join nest, or
 
822
 
 
823
    3. The table is functionally dependent on some previous table
 
824
 
 
825
    [4. This is also true for constant tables that can't be
 
826
        NULL-complemented but this function is not called for such tables]
 
827
 
 
828
  RETURN
 
829
    true  - Include table's rowid
 
830
    false - Don't
 
831
*/
 
832
 
 
833
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
 
834
{
 
835
  if (join_tab->emb_sj_nest)
 
836
    return false;
 
837
  
 
838
  /* Check if this table is functionally dependent on the tables that
 
839
     are within the same outer join nest
 
840
  */
 
841
  TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
 
842
  if (join_tab->type == JT_EQ_REF)
 
843
  {
 
844
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
 
845
    uint idx;
 
846
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
 
847
    {
 
848
      JOIN_TAB *ref_tab= join->join_tab + idx;
 
849
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
 
850
        return true;
 
851
    }
 
852
    /* Ok, functionally dependent */
 
853
    return false;
 
854
  }
 
855
  /* Not functionally dependent => need to include*/
 
856
  return true;
 
857
}
 
858
 
 
859
 
 
860
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg,
 
861
                                          SJ_TMP_TABLE *sjtbl);
 
862
 
 
863
 
 
864
/*
 
865
  Setup the strategies to eliminate semi-join duplicates.
 
866
  
 
867
  SYNOPSIS
 
868
    setup_semijoin_dups_elimination()
 
869
      join           Join to process
 
870
      options        Join options (needed to see if join buffering will be 
 
871
                     used or not)
 
872
      no_jbuf_after  Another bit of information re where join buffering will
 
873
                     be used.
 
874
 
 
875
  DESCRIPTION
 
876
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
 
877
    strategies:
 
878
 
 
879
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
 
880
                         of row combinations)
 
881
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
 
882
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
 
883
                  together and picking the 1st one)
 
884
    
 
885
    The join order has "duplicate-generating ranges", and every range is
 
886
    served by one strategy or a combination of FirstMatch with with some
 
887
    other strategy.
 
888
    
 
889
    "Duplicate-generating range" is defined as a range within the join order
 
890
    that contains all of the inner tables of a semi-join. All ranges must be
 
891
    disjoint, if tables of several semi-joins are interleaved, then the ranges
 
892
    are joined together, which is equivalent to converting
 
893
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
 
894
    to
 
895
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
 
896
    .
 
897
 
 
898
    Applicability conditions are as follows:
 
899
 
 
900
    DuplicateWeedout strategy
 
901
    ~~~~~~~~~~~~~~~~~~~~~~~~~
 
902
 
 
903
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
 
904
      +------+  +=========================+  +---+
 
905
        (1)                 (2)               (3)
 
906
 
 
907
       (1) - Prefix of OuterTables (those that participate in 
 
908
             IN-equality and/or are correlated with subquery) and outer 
 
909
             Noncorrelated Tables.
 
910
       (2) - The handled range. The range starts with the first sj-inner
 
911
             table, and covers all sj-inner and outer tables 
 
912
             Within the range,  Inner, Outer, outer Noncorrelated tables
 
913
             may follow in any order.
 
914
       (3) - The suffix of outer Noncorrelated tables.
 
915
    
 
916
    FirstMatch strategy
 
917
    ~~~~~~~~~~~~~~~~~~~
 
918
 
 
919
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
 
920
      +------+  +==================+  +---+
 
921
        (1)             (2)          (3)
 
922
 
 
923
      (1) - Prefix of outer and non-correlated tables
 
924
      (2) - The handled range, which may contain only inner and
 
925
            non-correlated tables.
 
926
      (3) - The suffix of outer Noncorrelated tables.
 
927
 
 
928
    InsideOut strategy 
 
929
    ~~~~~~~~~~~~~~~~~~
 
930
 
 
931
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
 
932
     +--------+   +===========+ +=============+   +------+
 
933
        (1)           (2)          (3)              (4)
 
934
     
 
935
      (1) - Prefix that may contain any outer tables. The prefix must contain
 
936
            all the non-trivially correlated outer tables. (non-trivially means
 
937
            that the correlation is not just through the IN-equality).
 
938
      
 
939
      (2) - Inner table for which the InsideOut scan is performed.
 
940
 
 
941
      (3) - The remainder of the duplicate-generating range. It is served by 
 
942
            application of FirstMatch strategy, with the exception that
 
943
            outer IN-correlated tables are considered to be non-correlated.
 
944
 
 
945
      (4) - THe suffix of outer and outer non-correlated tables.
 
946
 
 
947
    If several strategies are applicable, their relative priorities are:
 
948
      1. InsideOut
 
949
      2. FirstMatch 
 
950
      3. DuplicateWeedout
 
951
 
 
952
    This function walks over the join order and sets up the strategies by
 
953
    setting appropriate members in join_tab structures.
 
954
 
 
955
  RETURN
 
956
    false  OK 
 
957
    true   Out of memory error
 
958
*/
 
959
 
 
960
static
 
961
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint no_jbuf_after)
 
962
{
 
963
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
964
  struct {
 
965
    /* 
 
966
      0 - invalid (EOF marker), 
 
967
      1 - InsideOut, 
 
968
      2 - Temptable (maybe confluent),
 
969
      3 - Temptable with join buffering
 
970
    */
 
971
    uint strategy;
 
972
    uint start_idx; /* Left range bound */
 
973
    uint end_idx;   /* Right range bound */
 
974
    /* 
 
975
      For Temptable strategy: Bitmap of all outer and correlated tables from 
 
976
      all involved join nests.
 
977
    */
 
978
    table_map outer_tables;
 
979
  } dups_ranges [MAX_TABLES];
 
980
 
 
981
  TABLE_LIST *emb_insideout_nest= NULL;
 
982
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
 
983
                               tables) whose ranges we're in */
 
984
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
 
985
  table_map range_start_map= 0; /* table_map at current range start */
 
986
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
 
987
  int cur_range= 0;
 
988
  uint i;
 
989
 
 
990
  /*
 
991
    First pass: locate the duplicate-generating ranges and pick the strategies.
 
992
  */
 
993
  for (i=join->const_tables ; i < join->tables ; i++)
 
994
  {
 
995
    JOIN_TAB *tab=join->join_tab+i;
 
996
    TABLE *table=tab->table;
 
997
    cur_map |= table->map;
 
998
 
 
999
    if (tab->emb_sj_nest) // Encountered an sj-inner table
 
1000
    {
 
1001
      if (!emb_sj_map)
 
1002
      {
 
1003
        dups_ranges[cur_range].start_idx= i;
 
1004
        range_start_map= cur_map & ~table->map;
 
1005
        /*
 
1006
          Remember if this is a possible start of range that is covered by
 
1007
          the InsideOut strategy (the reason that it is not covered could
 
1008
          be that it overlaps with anther semi-join's range. we don't
 
1009
          support InsideOut for joined ranges)
 
1010
        */
 
1011
        if (join->best_positions[i].use_insideout_scan)
 
1012
          emb_insideout_nest= tab->emb_sj_nest;
 
1013
      }
 
1014
 
 
1015
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
 
1016
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
 
1017
 
 
1018
      if (tab->emb_sj_nest != emb_insideout_nest)
 
1019
      {
 
1020
        /*
 
1021
          Two different semi-joins interleave. This cannot be handled by
 
1022
          InsideOut strategy.
 
1023
        */
 
1024
        emb_insideout_nest= NULL;
 
1025
      }
 
1026
    }
 
1027
 
 
1028
    if (emb_sj_map) /* We're in duplicate-generating range */
 
1029
    {
 
1030
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
1031
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
 
1032
          i <= no_jbuf_after && !dealing_with_jbuf)
 
1033
      {
 
1034
        /*
 
1035
          This table uses join buffering, which makes use of FirstMatch or 
 
1036
          InsideOut strategies impossible for the current and (we assume) 
 
1037
          preceding duplicate-producing ranges.
 
1038
          That is, for the join order:
 
1039
 
 
1040
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
 
1041
                  |     |     |     |          | \
 
1042
                  +-----+     +-----+          |  join buffering use
 
1043
                     r1          r2         we're here
 
1044
 
 
1045
          we'll have to remove r1 and r2 and use duplicate-elimination
 
1046
          strategy that spans all the tables, starting from the very 1st
 
1047
          one.
 
1048
        */
 
1049
        dealing_with_jbuf= true;
 
1050
        emb_insideout_nest= false;
 
1051
 
 
1052
        /* 
 
1053
          Absorb all preceding duplicate-eliminating ranges. Their strategies
 
1054
          do not matter: 
 
1055
        */
 
1056
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
 
1057
        {
 
1058
          dups_ranges[cur_range].outer_tables |= 
 
1059
            dups_ranges[prev_range].outer_tables;
 
1060
        }
 
1061
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
 
1062
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
 
1063
        cur_range=  0;
 
1064
      }
 
1065
 
 
1066
      /*
 
1067
        Check if we are at the end of duplicate-producing range. We are if
 
1068
 
 
1069
        1. It's an InsideOut range (which presumes all correlated tables are
 
1070
           in the prefix), and all inner tables are in the join order prefix,
 
1071
           or
 
1072
        2. It's a DuplicateElimination range (possibly covering several
 
1073
           SJ-nests), and all inner, outer, and correlated tables of all 
 
1074
           sj-nests are in the join order prefix.
 
1075
      */
 
1076
      bool end_of_range= false;
 
1077
      if (emb_insideout_nest && 
 
1078
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
 
1079
      {
 
1080
        /* Save that this range is handled with InsideOut: */
 
1081
        dups_ranges[cur_range].strategy= 1;
 
1082
        end_of_range= true;
 
1083
      }
 
1084
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
 
1085
      {
 
1086
        /*
 
1087
          This is a complete range to be handled with either DuplicateWeedout 
 
1088
          or FirstMatch
 
1089
        */
 
1090
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
 
1091
        /* 
 
1092
          This will hold tables from within the range that need to be put 
 
1093
          into the join buffer before we can use the FirstMatch on its tail.
 
1094
        */
 
1095
        dups_ranges[cur_range].outer_tables= emb_outer_tables & 
 
1096
                                             ~range_start_map;
 
1097
        end_of_range= true;
 
1098
      }
 
1099
 
 
1100
      if (end_of_range)
 
1101
      {
 
1102
        dups_ranges[cur_range].end_idx= i+1;
 
1103
        emb_sj_map= emb_outer_tables= 0;
 
1104
        emb_insideout_nest= NULL;
 
1105
        dealing_with_jbuf= false;
 
1106
        dups_ranges[++cur_range].strategy= 0;
 
1107
      }
 
1108
    }
 
1109
  }
 
1110
 
 
1111
  THD *thd= join->thd;
 
1112
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
 
1113
  /*
 
1114
    Second pass: setup the chosen strategies    
 
1115
  */
 
1116
  for (int j= 0; j < cur_range; j++)
 
1117
  {
 
1118
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
 
1119
    JOIN_TAB *jump_to;
 
1120
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
 
1121
    {
 
1122
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
 
1123
      jump_to= tab++;
 
1124
    }
 
1125
    else // DuplicateWeedout strategy
 
1126
    {
 
1127
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
 
1128
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
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
 
1131
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
 
1132
      uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1133
      JOIN_TAB *last_outer_tab= tab - 1;
 
1134
      /*
 
1135
        Walk through the range and remember
 
1136
         - tables that need their rowids to be put into temptable
 
1137
         - the last outer table
 
1138
      */
 
1139
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1140
      {
 
1141
        if (sj_table_is_included(join, tab))
 
1142
        {
 
1143
          last_tab->join_tab= tab;
 
1144
          last_tab->rowid_offset= jt_rowid_offset;
 
1145
          jt_rowid_offset += tab->table->file->ref_length;
 
1146
          if (tab->table->maybe_null)
 
1147
          {
 
1148
            last_tab->null_byte= jt_null_bits / 8;
 
1149
            last_tab->null_bit= jt_null_bits++;
 
1150
          }
 
1151
          last_tab++;
 
1152
          tab->table->prepare_for_position();
 
1153
          tab->rowid_keep_flags= rowid_keep_flags;
 
1154
        }
 
1155
        cur_map |= tab->table->map;
 
1156
        if (!tab->emb_sj_nest && bitmap_covers(cur_map, 
 
1157
                                               dups_ranges[j].outer_tables))
 
1158
          last_outer_tab= tab;
 
1159
      }
 
1160
 
 
1161
      if (jt_rowid_offset) /* Temptable has at least one rowid */
 
1162
      {
 
1163
        SJ_TMP_TABLE *sjtbl;
 
1164
        uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1165
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
 
1166
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
 
1167
          return(true);
 
1168
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
 
1169
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
 
1170
        sjtbl->rowid_len= jt_rowid_offset;
 
1171
        sjtbl->null_bits= jt_null_bits;
 
1172
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
 
1173
 
 
1174
        *next_sjtbl_ptr= sjtbl;
 
1175
        next_sjtbl_ptr= &(sjtbl->next);
 
1176
        sjtbl->next= NULL;
 
1177
 
 
1178
        sjtbl->tmp_table= 
 
1179
          create_duplicate_weedout_tmp_table(thd, 
 
1180
                                             sjtbl->rowid_len + 
 
1181
                                             sjtbl->null_bytes,
 
1182
                                             sjtbl);
 
1183
 
 
1184
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
 
1185
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
 
1186
      }
 
1187
      tab= last_outer_tab + 1;
 
1188
      jump_to= last_outer_tab;
 
1189
    }
 
1190
 
 
1191
    /* Create the FirstMatch tail */
 
1192
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1193
    {
 
1194
      if (tab->emb_sj_nest)
 
1195
        tab->do_firstmatch= jump_to; 
 
1196
      else
 
1197
        jump_to= tab;
 
1198
    }
 
1199
  }
 
1200
  return(false);
 
1201
}
 
1202
 
 
1203
 
 
1204
static void cleanup_sj_tmp_tables(JOIN *join)
 
1205
{
 
1206
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl; 
 
1207
       sj_tbl= sj_tbl->next)
 
1208
  {
 
1209
    if (sj_tbl->tmp_table)
 
1210
    {
 
1211
      free_tmp_table(join->thd, sj_tbl->tmp_table);
 
1212
    }
 
1213
  }
 
1214
  join->sj_tmp_tables= NULL;
 
1215
}
 
1216
 
 
1217
uint make_join_orderinfo(JOIN *join);
 
1218
 
 
1219
/**
 
1220
  global select optimisation.
 
1221
 
 
1222
  @note
 
1223
    error code saved in field 'error'
 
1224
 
 
1225
  @retval
 
1226
    0   success
 
1227
  @retval
 
1228
    1   error
 
1229
*/
 
1230
 
 
1231
int
 
1232
JOIN::optimize()
 
1233
{
 
1234
  // to prevent double initialization on EXPLAIN
 
1235
  if (optimized)
 
1236
    return(0);
 
1237
  optimized= 1;
 
1238
 
 
1239
  thd_proc_info(thd, "optimizing");
 
1240
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
 
1241
              unit->select_limit_cnt);
 
1242
  /* select_limit is used to decide if we are likely to scan the whole table */
 
1243
  select_limit= unit->select_limit_cnt;
 
1244
  if (having || (select_options & OPTION_FOUND_ROWS))
 
1245
    select_limit= HA_POS_ERROR;
 
1246
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
 
1247
  // Ignore errors of execution if option IGNORE present
 
1248
  if (thd->lex->ignore)
 
1249
    thd->lex->current_select->no_error= 1;
 
1250
 
 
1251
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
 
1252
  /* Add HAVING to WHERE if possible */
 
1253
  if (having && !group_list && !sum_func_count)
 
1254
  {
 
1255
    if (!conds)
 
1256
    {
 
1257
      conds= having;
 
1258
      having= 0;
 
1259
    }
 
1260
    else if ((conds=new Item_cond_and(conds,having)))
 
1261
    {
 
1262
      /*
 
1263
        Item_cond_and can't be fixed after creation, so we do not check
 
1264
        conds->fixed
 
1265
      */
 
1266
      conds->fix_fields(thd, &conds);
 
1267
      conds->change_ref_to_fields(thd, tables_list);
 
1268
      conds->top_level_item();
 
1269
      having= 0;
 
1270
    }
 
1271
  }
 
1272
#endif
 
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
  }
 
1288
 
 
1289
  conds= optimize_cond(this, conds, join_list, &cond_value);   
 
1290
  if (thd->is_error())
 
1291
  {
 
1292
    error= 1;
 
1293
    return(1);
 
1294
  }
 
1295
 
 
1296
  {
 
1297
    having= optimize_cond(this, having, join_list, &having_value);
 
1298
    if (thd->is_error())
 
1299
    {
 
1300
      error= 1;
 
1301
      return(1);
 
1302
    }
 
1303
    if (select_lex->where)
 
1304
      select_lex->cond_value= cond_value;
 
1305
    if (select_lex->having)
 
1306
      select_lex->having_value= having_value;
 
1307
 
 
1308
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 
 
1309
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
 
1310
    {                                           /* Impossible cond */
 
1311
      zero_result_cause=  having_value == Item::COND_FALSE ?
 
1312
                           "Impossible HAVING" : "Impossible WHERE";
 
1313
      error= 0;
 
1314
      return(0);
 
1315
    }
 
1316
  }
 
1317
 
 
1318
  /* Optimize count(*), min() and max() */
 
1319
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
 
1320
  {
 
1321
    int res;
 
1322
    /*
 
1323
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
 
1324
      to the WHERE conditions,
 
1325
      or 1 if all items were resolved,
 
1326
      or 0, or an error number HA_ERR_...
 
1327
    */
 
1328
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
 
1329
    {
 
1330
      if (res == HA_ERR_KEY_NOT_FOUND)
 
1331
      {
 
1332
        zero_result_cause= "No matching min/max row";
 
1333
        error=0;
 
1334
        return(0);
 
1335
      }
 
1336
      if (res > 1)
 
1337
      {
 
1338
        error= res;
 
1339
        return(1);
 
1340
      }
 
1341
      if (res < 0)
 
1342
      {
 
1343
        zero_result_cause= "No matching min/max row";
 
1344
        error=0;
 
1345
        return(0);
 
1346
      }
 
1347
      zero_result_cause= "Select tables optimized away";
 
1348
      tables_list= 0;                           // All tables resolved
 
1349
      /*
 
1350
        Extract all table-independent conditions and replace the WHERE
 
1351
        clause with them. All other conditions were computed by opt_sum_query
 
1352
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
 
1353
        so there is no need to compute the whole WHERE clause again.
 
1354
        Notice that make_cond_for_table() will always succeed to remove all
 
1355
        computed conditions, because opt_sum_query() is applicable only to
 
1356
        conjunctions.
 
1357
        Preserve conditions for EXPLAIN.
 
1358
      */
 
1359
      if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
 
1360
      {
 
1361
        COND *table_independent_conds=
 
1362
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
1363
        conds= table_independent_conds;
 
1364
      }
 
1365
    }
 
1366
  }
 
1367
  if (!tables_list)
 
1368
  {
 
1369
    error= 0;
 
1370
    return(0);
 
1371
  }
 
1372
  error= -1;                                    // Error is sent to client
 
1373
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
 
1374
 
 
1375
  /* Calculate how to do the join */
 
1376
  thd_proc_info(thd, "statistics");
 
1377
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
 
1378
      thd->is_fatal_error)
 
1379
  {
 
1380
    return(1);
 
1381
  }
 
1382
 
 
1383
  /* Remove distinct if only const tables */
 
1384
  select_distinct= select_distinct && (const_tables != tables);
 
1385
  thd_proc_info(thd, "preparing");
 
1386
  if (result->initialize_tables(this))
 
1387
  {
 
1388
    return(1);                          // error == -1
 
1389
  }
 
1390
  if (const_table_map != found_const_table_map &&
 
1391
      !(select_options & SELECT_DESCRIBE) &&
 
1392
      (!conds ||
 
1393
       !(conds->used_tables() & RAND_TABLE_BIT) ||
 
1394
       select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
 
1395
  {
 
1396
    zero_result_cause= "no matching row in const table";
 
1397
    error= 0;
 
1398
    return(0);
 
1399
  }
 
1400
  if (!(thd->options & OPTION_BIG_SELECTS) &&
 
1401
      best_read > (double) thd->variables.max_join_size &&
 
1402
      !(select_options & SELECT_DESCRIBE))
 
1403
  {                                             /* purecov: inspected */
 
1404
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
 
1405
    error= -1;
 
1406
    return(1);
 
1407
  }
 
1408
  if (const_tables && !thd->locked_tables &&
 
1409
      !(select_options & SELECT_NO_UNLOCK))
 
1410
    mysql_unlock_some_tables(thd, table, const_tables);
 
1411
  if (!conds && outer_join)
 
1412
  {
 
1413
    /* Handle the case where we have an OUTER JOIN without a WHERE */
 
1414
    conds=new Item_int((int64_t) 1,1);  // Always true
 
1415
  }
 
1416
  select= make_select(*table, const_table_map,
 
1417
                      const_table_map, conds, 1, &error);
 
1418
  if (error)
 
1419
  {                                             /* purecov: inspected */
 
1420
    error= -1;                                  /* purecov: inspected */
 
1421
    return(1);
 
1422
  }
 
1423
  
 
1424
  reset_nj_counters(join_list);
 
1425
  make_outerjoin_info(this);
 
1426
 
 
1427
  /*
 
1428
    Among the equal fields belonging to the same multiple equality
 
1429
    choose the one that is to be retrieved first and substitute
 
1430
    all references to these in where condition for a reference for
 
1431
    the selected field.
 
1432
  */
 
1433
  if (conds)
 
1434
  {
 
1435
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
 
1436
    conds->update_used_tables();
 
1437
  }
 
1438
 
 
1439
  /*
 
1440
    Permorm the the optimization on fields evaluation mentioned above
 
1441
    for all on expressions.
 
1442
  */ 
 
1443
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
 
1444
  {
 
1445
    if (*tab->on_expr_ref)
 
1446
    {
 
1447
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
 
1448
                                                         tab->cond_equal,
 
1449
                                                         map2table);
 
1450
      (*tab->on_expr_ref)->update_used_tables();
 
1451
    }
 
1452
  }
 
1453
 
 
1454
  if (conds &&!outer_join && const_table_map != found_const_table_map && 
 
1455
      (select_options & SELECT_DESCRIBE) &&
 
1456
      select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
 
1457
  {
 
1458
    conds=new Item_int((int64_t) 0,1);  // Always false
 
1459
  }
 
1460
  if (make_join_select(this, select, conds))
 
1461
  {
 
1462
    zero_result_cause=
 
1463
      "Impossible WHERE noticed after reading const tables";
 
1464
    return(0);                          // error == 0
 
1465
  }
 
1466
 
 
1467
  error= -1;                                    /* if goto err */
 
1468
 
 
1469
  /* Optimize distinct away if possible */
 
1470
  {
 
1471
    ORDER *org_order= order;
 
1472
    order=remove_const(this, order,conds,1, &simple_order);
 
1473
    if (thd->is_error())
 
1474
    {
 
1475
      error= 1;
 
1476
      return(1);
 
1477
    }
 
1478
 
 
1479
    /*
 
1480
      If we are using ORDER BY NULL or ORDER BY const_expression,
 
1481
      return result in any order (even if we are using a GROUP BY)
 
1482
    */
 
1483
    if (!order && org_order)
 
1484
      skip_sort_order= 1;
 
1485
  }
 
1486
  /*
 
1487
     Check if we can optimize away GROUP BY/DISTINCT.
 
1488
     We can do that if there are no aggregate functions, the
 
1489
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
 
1490
     (if present) contain direct references to all key parts of
 
1491
     an unique index (in whatever order) and if the key parts of the
 
1492
     unique index cannot contain NULLs.
 
1493
     Note that the unique keys for DISTINCT and GROUP BY should not
 
1494
     be the same (as long as they are unique).
 
1495
 
 
1496
     The FROM clause must contain a single non-constant table.
 
1497
  */
 
1498
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
 
1499
      !tmp_table_param.sum_func_count &&
 
1500
      (!join_tab[const_tables].select ||
 
1501
       !join_tab[const_tables].select->quick ||
 
1502
       join_tab[const_tables].select->quick->get_type() != 
 
1503
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
 
1504
  {
 
1505
    if (group_list &&
 
1506
       list_contains_unique_index(join_tab[const_tables].table,
 
1507
                                 find_field_in_order_list,
 
1508
                                 (void *) group_list))
 
1509
    {
 
1510
      /*
 
1511
        We have found that grouping can be removed since groups correspond to
 
1512
        only one row anyway, but we still have to guarantee correct result
 
1513
        order. The line below effectively rewrites the query from GROUP BY
 
1514
        <fields> to ORDER BY <fields>. There are two exceptions:
 
1515
        - if skip_sort_order is set (see above), then we can simply skip
 
1516
          GROUP BY;
 
1517
        - we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
 
1518
          with the GROUP BY ones, i.e. either one is a prefix of another.
 
1519
          We only check if the ORDER BY is a prefix of GROUP BY. In this case
 
1520
          test_if_subpart() copies the ASC/DESC attributes from the original
 
1521
          ORDER BY fields.
 
1522
          If GROUP BY is a prefix of ORDER BY, then it is safe to leave
 
1523
          'order' as is.
 
1524
       */
 
1525
      if (!order || test_if_subpart(group_list, order))
 
1526
          order= skip_sort_order ? 0 : group_list;
 
1527
      /*
 
1528
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be 
 
1529
        rewritten to IGNORE INDEX FOR ORDER BY(fields).
 
1530
      */
 
1531
      join_tab->table->keys_in_use_for_order_by=
 
1532
        join_tab->table->keys_in_use_for_group_by;
 
1533
      group_list= 0;
 
1534
      group= 0;
 
1535
    }
 
1536
    if (select_distinct &&
 
1537
       list_contains_unique_index(join_tab[const_tables].table,
 
1538
                                 find_field_in_item_list,
 
1539
                                 (void *) &fields_list))
 
1540
    {
 
1541
      select_distinct= 0;
 
1542
    }
 
1543
  }
 
1544
  if (group_list || tmp_table_param.sum_func_count)
 
1545
  {
 
1546
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
1547
      select_distinct=0;
 
1548
  }
 
1549
  else if (select_distinct && tables - const_tables == 1)
 
1550
  {
 
1551
    /*
 
1552
      We are only using one table. In this case we change DISTINCT to a
 
1553
      GROUP BY query if:
 
1554
      - The GROUP BY can be done through indexes (no sort) and the ORDER
 
1555
        BY only uses selected fields.
 
1556
        (In this case we can later optimize away GROUP BY and ORDER BY)
 
1557
      - We are scanning the whole table without LIMIT
 
1558
        This can happen if:
 
1559
        - We are using CALC_FOUND_ROWS
 
1560
        - We are using an ORDER BY that can't be optimized away.
 
1561
 
 
1562
      We don't want to use this optimization when we are using LIMIT
 
1563
      because in this case we can just create a temporary table that
 
1564
      holds LIMIT rows and stop when this table is full.
 
1565
    */
 
1566
    JOIN_TAB *tab= &join_tab[const_tables];
 
1567
    bool all_order_fields_used;
 
1568
    if (order)
 
1569
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1, 
 
1570
        &tab->table->keys_in_use_for_order_by);
 
1571
    if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
 
1572
                                          order, fields_list, all_fields,
 
1573
                                          &all_order_fields_used)))
 
1574
    {
 
1575
      bool skip_group= (skip_sort_order &&
 
1576
        test_if_skip_sort_order(tab, group_list, select_limit, 1, 
 
1577
                                &tab->table->keys_in_use_for_group_by) != 0);
 
1578
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
1579
      if ((skip_group && all_order_fields_used) ||
 
1580
          select_limit == HA_POS_ERROR ||
 
1581
          (order && !skip_sort_order))
 
1582
      {
 
1583
        /*  Change DISTINCT to GROUP BY */
 
1584
        select_distinct= 0;
 
1585
        no_order= !order;
 
1586
        if (all_order_fields_used)
 
1587
        {
 
1588
          if (order && skip_sort_order)
 
1589
          {
 
1590
            /*
 
1591
              Force MySQL to read the table in sorted order to get result in
 
1592
              ORDER BY order.
 
1593
            */
 
1594
            tmp_table_param.quick_group=0;
 
1595
          }
 
1596
          order=0;
 
1597
        }
 
1598
        group=1;                                // For end_write_group
 
1599
      }
 
1600
      else
 
1601
        group_list= 0;
 
1602
    }
 
1603
    else if (thd->is_fatal_error)                       // End of memory
 
1604
      return(1);
 
1605
  }
 
1606
  simple_group= 0;
 
1607
  {
 
1608
    ORDER *old_group_list;
 
1609
    group_list= remove_const(this, (old_group_list= group_list), conds,
 
1610
                             rollup.state == ROLLUP::STATE_NONE,
 
1611
                             &simple_group);
 
1612
    if (thd->is_error())
 
1613
    {
 
1614
      error= 1;
 
1615
      return(1);
 
1616
    }
 
1617
    if (old_group_list && !group_list)
 
1618
      select_distinct= 0;
 
1619
  }
 
1620
  if (!group_list && group)
 
1621
  {
 
1622
    order=0;                                    // The output has only one row
 
1623
    simple_order=1;
 
1624
    select_distinct= 0;                       // No need in distinct for 1 row
 
1625
    group_optimized_away= 1;
 
1626
  }
 
1627
 
 
1628
  calc_group_buffer(this, group_list);
 
1629
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
 
1630
 
 
1631
  if (test_if_subpart(group_list, order) ||
 
1632
      (!group_list && tmp_table_param.sum_func_count))
 
1633
    order=0;
 
1634
 
 
1635
  // Can't use sort on head table if using row cache
 
1636
  if (full_join)
 
1637
  {
 
1638
    if (group_list)
 
1639
      simple_group=0;
 
1640
    if (order)
 
1641
      simple_order=0;
 
1642
  }
 
1643
 
 
1644
  /*
 
1645
    Check if we need to create a temporary table.
 
1646
    This has to be done if all tables are not already read (const tables)
 
1647
    and one of the following conditions holds:
 
1648
    - We are using DISTINCT (simple distinct's are already optimized away)
 
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
 
1651
    - The user wants us to buffer the result.
 
1652
  */
 
1653
  need_tmp= (const_tables != tables &&
 
1654
             ((select_distinct || !simple_order || !simple_group) ||
 
1655
              (group_list && order) ||
 
1656
              test(select_options & OPTION_BUFFER_RESULT)));
 
1657
 
 
1658
  uint no_jbuf_after= make_join_orderinfo(this);
 
1659
  uint64_t select_opts_for_readinfo= 
 
1660
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
 
1661
 
 
1662
  sj_tmp_tables= NULL;
 
1663
  if (!select_lex->sj_nests.is_empty())
 
1664
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
 
1665
                                    no_jbuf_after);
 
1666
 
 
1667
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
 
1668
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
 
1669
    return(1);
 
1670
 
 
1671
  /* Create all structures needed for materialized subquery execution. */
 
1672
  if (setup_subquery_materialization())
 
1673
    return(1);
 
1674
 
 
1675
  /*
 
1676
    is this simple IN subquery?
 
1677
  */
 
1678
  if (!group_list && !order &&
 
1679
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
 
1680
      tables == 1 && conds &&
 
1681
      !unit->is_union())
 
1682
  {
 
1683
    if (!having)
 
1684
    {
 
1685
      Item *where= conds;
 
1686
      if (join_tab[0].type == JT_EQ_REF &&
 
1687
          join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1688
      {
 
1689
        remove_subq_pushed_predicates(&where);
 
1690
        save_index_subquery_explain_info(join_tab, where);
 
1691
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
 
1692
        error= 0;
 
1693
        return(unit->item->
 
1694
                    change_engine(new
 
1695
                                  subselect_uniquesubquery_engine(thd,
 
1696
                                                                  join_tab,
 
1697
                                                                  unit->item,
 
1698
                                                                  where)));
 
1699
      }
 
1700
      else if (join_tab[0].type == JT_REF &&
 
1701
               join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1702
      {
 
1703
        remove_subq_pushed_predicates(&where);
 
1704
        save_index_subquery_explain_info(join_tab, where);
 
1705
        join_tab[0].type= JT_INDEX_SUBQUERY;
 
1706
        error= 0;
 
1707
        return(unit->item->
 
1708
                    change_engine(new
 
1709
                                  subselect_indexsubquery_engine(thd,
 
1710
                                                                 join_tab,
 
1711
                                                                 unit->item,
 
1712
                                                                 where,
 
1713
                                                                 NULL,
 
1714
                                                                 0)));
 
1715
      }
 
1716
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
 
1717
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
 
1718
               having->name == in_having_cond)
 
1719
    {
 
1720
      join_tab[0].type= JT_INDEX_SUBQUERY;
 
1721
      error= 0;
 
1722
      conds= remove_additional_cond(conds);
 
1723
      save_index_subquery_explain_info(join_tab, conds);
 
1724
      return(unit->item->
 
1725
                  change_engine(new subselect_indexsubquery_engine(thd,
 
1726
                                                                   join_tab,
 
1727
                                                                   unit->item,
 
1728
                                                                   conds,
 
1729
                                                                   having,
 
1730
                                                                   1)));
 
1731
    }
 
1732
 
 
1733
  }
 
1734
  /*
 
1735
    Need to tell handlers that to play it safe, it should fetch all
 
1736
    columns of the primary key of the tables: this is because MySQL may
 
1737
    build row pointers for the rows, and for all columns of the primary key
 
1738
    the read set has not necessarily been set by the server code.
 
1739
  */
 
1740
  if (need_tmp || select_distinct || group_list || order)
 
1741
  {
 
1742
    for (uint i = const_tables; i < tables; i++)
 
1743
      join_tab[i].table->prepare_for_position();
 
1744
  }
 
1745
 
 
1746
  if (const_tables != tables)
 
1747
  {
 
1748
    /*
 
1749
      Because filesort always does a full table scan or a quick range scan
 
1750
      we must add the removed reference to the select for the table.
 
1751
      We only need to do this when we have a simple_order or simple_group
 
1752
      as in other cases the join is done before the sort.
 
1753
    */
 
1754
    if ((order || group_list) &&
 
1755
        (join_tab[const_tables].type != JT_ALL) &&
 
1756
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
 
1757
        ((order && simple_order) || (group_list && simple_group)))
 
1758
    {
 
1759
      if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
 
1760
        return(1);
 
1761
      }
 
1762
    }
 
1763
    
 
1764
    if (!(select_options & SELECT_BIG_RESULT) &&
 
1765
        ((group_list &&
 
1766
          (!simple_group ||
 
1767
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
 
1768
                                    unit->select_limit_cnt, 0, 
 
1769
                                    &join_tab[const_tables].table->
 
1770
                                    keys_in_use_for_group_by))) ||
 
1771
         select_distinct) &&
 
1772
        tmp_table_param.quick_group)
 
1773
    {
 
1774
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
 
1775
    }
 
1776
    if (order)
 
1777
    {
 
1778
      /*
 
1779
        Force using of tmp table if sorting by a SP or UDF function due to
 
1780
        their expensive and probably non-deterministic nature.
 
1781
      */
 
1782
      for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
1783
      {
 
1784
        Item *item= *tmp_order->item;
 
1785
        if (item->is_expensive())
 
1786
        {
 
1787
          /* Force tmp table without sort */
 
1788
          need_tmp=1; simple_order=simple_group=0;
 
1789
          break;
 
1790
        }
 
1791
      }
 
1792
    }
 
1793
  }
 
1794
 
 
1795
  tmp_having= having;
 
1796
  if (select_options & SELECT_DESCRIBE)
 
1797
  {
 
1798
    error= 0;
 
1799
    return(0);
 
1800
  }
 
1801
  having= 0;
 
1802
 
 
1803
  /*
 
1804
    The loose index scan access method guarantees that all grouping or
 
1805
    duplicate row elimination (for distinct) is already performed
 
1806
    during data retrieval, and that all MIN/MAX functions are already
 
1807
    computed for each group. Thus all MIN/MAX functions should be
 
1808
    treated as regular functions, and there is no need to perform
 
1809
    grouping in the main execution loop.
 
1810
    Notice that currently loose index scan is applicable only for
 
1811
    single table queries, thus it is sufficient to test only the first
 
1812
    join_tab element of the plan for its access method.
 
1813
  */
 
1814
  if (join_tab->is_using_loose_index_scan())
 
1815
    tmp_table_param.precomputed_group_by= true;
 
1816
 
 
1817
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1818
  if (need_tmp)
 
1819
  {
 
1820
    thd_proc_info(thd, "Creating tmp table");
 
1821
 
 
1822
    init_items_ref_array();
 
1823
 
 
1824
    tmp_table_param.hidden_field_count= (all_fields.elements -
 
1825
                                         fields_list.elements);
 
1826
    ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1827
                                                             (ORDER*) 0);
 
1828
    /*
 
1829
      Pushing LIMIT to the temporary table creation is not applicable
 
1830
      when there is ORDER BY or GROUP BY or there is no GROUP BY, but
 
1831
      there are aggregate functions, because in all these cases we need
 
1832
      all result rows.
 
1833
    */
 
1834
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
 
1835
                             !tmp_group &&
 
1836
                             !thd->lex->current_select->with_sum_func) ?
 
1837
                            select_limit : HA_POS_ERROR;
 
1838
 
 
1839
    if (!(exec_tmp_table1=
 
1840
          create_tmp_table(thd, &tmp_table_param, all_fields,
 
1841
                           tmp_group,
 
1842
                           group_list ? 0 : select_distinct,
 
1843
                           group_list && simple_group,
 
1844
                           select_options,
 
1845
                           tmp_rows_limit,
 
1846
                           (char *) "")))
 
1847
                {
 
1848
      return(1);
 
1849
    }
 
1850
 
 
1851
    /*
 
1852
      We don't have to store rows in temp table that doesn't match HAVING if:
 
1853
      - we are sorting the table and writing complete group rows to the
 
1854
        temp table.
 
1855
      - We are using DISTINCT without resolving the distinct as a GROUP BY
 
1856
        on all columns.
 
1857
      
 
1858
      If having is not handled here, it will be checked before the row
 
1859
      is sent to the client.
 
1860
    */    
 
1861
    if (tmp_having && 
 
1862
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
 
1863
      having= tmp_having;
 
1864
 
 
1865
    /* if group or order on first table, sort first */
 
1866
    if (group_list && simple_group)
 
1867
    {
 
1868
      thd_proc_info(thd, "Sorting for group");
 
1869
      if (create_sort_index(thd, this, group_list,
 
1870
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1871
          alloc_group_fields(this, group_list) ||
 
1872
          make_sum_func_list(all_fields, fields_list, 1) ||
 
1873
          setup_sum_funcs(thd, sum_funcs))
 
1874
      {
 
1875
        return(1);
 
1876
      }
 
1877
      group_list=0;
 
1878
    }
 
1879
    else
 
1880
    {
 
1881
      if (make_sum_func_list(all_fields, fields_list, 0) ||
 
1882
          setup_sum_funcs(thd, sum_funcs))
 
1883
      {
 
1884
        return(1);
 
1885
      }
 
1886
 
 
1887
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
 
1888
      {
 
1889
        thd_proc_info(thd, "Sorting for order");
 
1890
        if (create_sort_index(thd, this, order,
 
1891
                              HA_POS_ERROR, HA_POS_ERROR, true))
 
1892
        {
 
1893
          return(1);
 
1894
        }
 
1895
        order=0;
 
1896
      }
 
1897
    }
 
1898
    
 
1899
    /*
 
1900
      Optimize distinct when used on some of the tables
 
1901
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
 
1902
      In this case we can stop scanning t2 when we have found one t1.a
 
1903
    */
 
1904
 
 
1905
    if (exec_tmp_table1->distinct)
 
1906
    {
 
1907
      table_map used_tables= thd->used_tables;
 
1908
      JOIN_TAB *last_join_tab= join_tab+tables-1;
 
1909
      do
 
1910
      {
 
1911
        if (used_tables & last_join_tab->table->map)
 
1912
          break;
 
1913
        last_join_tab->not_used_in_distinct=1;
 
1914
      } while (last_join_tab-- != join_tab);
 
1915
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
 
1916
      if (order && skip_sort_order)
 
1917
      {
 
1918
        /* Should always succeed */
 
1919
        if (test_if_skip_sort_order(&join_tab[const_tables],
 
1920
                                    order, unit->select_limit_cnt, 0, 
 
1921
                                    &join_tab[const_tables].table->
 
1922
                                      keys_in_use_for_order_by))
 
1923
          order=0;
 
1924
      }
 
1925
    }
 
1926
 
 
1927
    /* 
 
1928
      If this join belongs to an uncacheable subquery save 
 
1929
      the original join 
 
1930
    */
 
1931
    if (select_lex->uncacheable && !is_top_level_join() &&
 
1932
        init_save_join_tab())
 
1933
      return(-1);                         /* purecov: inspected */
 
1934
  }
 
1935
 
 
1936
  error= 0;
 
1937
  return(0);
 
1938
}
 
1939
 
 
1940
 
 
1941
/**
 
1942
  Restore values in temporary join.
 
1943
*/
 
1944
void JOIN::restore_tmp()
 
1945
{
 
1946
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
 
1947
}
 
1948
 
 
1949
 
 
1950
int
 
1951
JOIN::reinit()
 
1952
{
 
1953
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
 
1954
                                    select_lex->offset_limit->val_uint() :
 
1955
                                    0ULL);
 
1956
 
 
1957
  first_record= 0;
 
1958
 
 
1959
  if (exec_tmp_table1)
 
1960
  {
 
1961
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
 
1962
    exec_tmp_table1->file->ha_delete_all_rows();
 
1963
    free_io_cache(exec_tmp_table1);
 
1964
    filesort_free_buffers(exec_tmp_table1,0);
 
1965
  }
 
1966
  if (exec_tmp_table2)
 
1967
  {
 
1968
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
 
1969
    exec_tmp_table2->file->ha_delete_all_rows();
 
1970
    free_io_cache(exec_tmp_table2);
 
1971
    filesort_free_buffers(exec_tmp_table2,0);
 
1972
  }
 
1973
  if (items0)
 
1974
    set_items_ref_array(items0);
 
1975
 
 
1976
  if (join_tab_save)
 
1977
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
 
1978
 
 
1979
  if (tmp_join)
 
1980
    restore_tmp();
 
1981
 
 
1982
  /* Reset of sum functions */
 
1983
  if (sum_funcs)
 
1984
  {
 
1985
    Item_sum *func, **func_ptr= sum_funcs;
 
1986
    while ((func= *(func_ptr++)))
 
1987
      func->clear();
 
1988
  }
 
1989
 
 
1990
  return(0);
 
1991
}
 
1992
 
 
1993
/**
 
1994
   @brief Save the original join layout
 
1995
      
 
1996
   @details Saves the original join layout so it can be reused in 
 
1997
   re-execution and for EXPLAIN.
 
1998
             
 
1999
   @return Operation status
 
2000
   @retval 0      success.
 
2001
   @retval 1      error occurred.
 
2002
*/
 
2003
 
 
2004
bool
 
2005
JOIN::init_save_join_tab()
 
2006
{
 
2007
  if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
 
2008
    return 1;                                  /* purecov: inspected */
 
2009
  error= 0;                                    // Ensure that tmp_join.error= 0
 
2010
  restore_tmp();
 
2011
  return 0;
 
2012
}
 
2013
 
 
2014
 
 
2015
bool
 
2016
JOIN::save_join_tab()
 
2017
{
 
2018
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
 
2019
  {
 
2020
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
 
2021
                                                sizeof(JOIN_TAB) * tables)))
 
2022
      return 1;
 
2023
  }
 
2024
  return 0;
 
2025
}
 
2026
 
 
2027
 
 
2028
/**
 
2029
  Exec select.
 
2030
 
 
2031
  @todo
 
2032
    Note, that create_sort_index calls test_if_skip_sort_order and may
 
2033
    finally replace sorting with index scan if there is a LIMIT clause in
 
2034
    the query.  It's never shown in EXPLAIN!
 
2035
 
 
2036
  @todo
 
2037
    When can we have here thd->net.report_error not zero?
 
2038
*/
 
2039
void
 
2040
JOIN::exec()
 
2041
{
 
2042
  List<Item> *columns_list= &fields_list;
 
2043
  int      tmp_error;
 
2044
 
 
2045
  thd_proc_info(thd, "executing");
 
2046
  error= 0;
 
2047
  (void) result->prepare2(); // Currently, this cannot fail.
 
2048
 
 
2049
  if (!tables_list && (tables || !select_lex->with_sum_func))
 
2050
  {                                           // Only test of functions
 
2051
    if (select_options & SELECT_DESCRIBE)
 
2052
      select_describe(this, false, false, false,
 
2053
                      (zero_result_cause?zero_result_cause:"No tables used"));
 
2054
    else
 
2055
    {
 
2056
      result->send_fields(*columns_list,
 
2057
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2058
      /*
 
2059
        We have to test for 'conds' here as the WHERE may not be constant
 
2060
        even if we don't have any tables for prepared statements or if
 
2061
        conds uses something like 'rand()'.
 
2062
      */
 
2063
      if (cond_value != Item::COND_FALSE &&
 
2064
          (!conds || conds->val_int()) &&
 
2065
          (!having || having->val_int()))
 
2066
      {
 
2067
        if (do_send_rows && result->send_data(fields_list))
 
2068
          error= 1;
 
2069
        else
 
2070
        {
 
2071
          error= (int) result->send_eof();
 
2072
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
 
2073
                         thd->sent_row_count);
 
2074
        }
 
2075
      }
 
2076
      else
 
2077
      {
 
2078
        error=(int) result->send_eof();
 
2079
        send_records= 0;
 
2080
      }
 
2081
    }
 
2082
    /* Single select (without union) always returns 0 or 1 row */
 
2083
    thd->limit_found_rows= send_records;
 
2084
    thd->examined_row_count= 0;
 
2085
    return;
 
2086
  }
 
2087
  /*
 
2088
    Don't reset the found rows count if there're no tables as
 
2089
    FOUND_ROWS() may be called. Never reset the examined row count here.
 
2090
    It must be accumulated from all join iterations of all join parts.
 
2091
  */
 
2092
  if (tables)
 
2093
    thd->limit_found_rows= 0;
 
2094
 
 
2095
  if (zero_result_cause)
 
2096
  {
 
2097
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
 
2098
                            *columns_list,
 
2099
                            send_row_on_empty_set(),
 
2100
                            select_options,
 
2101
                            zero_result_cause,
 
2102
                            having);
 
2103
    return;
 
2104
  }
 
2105
 
 
2106
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
2107
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
2108
    return;
 
2109
 
 
2110
  if (select_options & SELECT_DESCRIBE)
 
2111
  {
 
2112
    /*
 
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
 
2115
      filesort for GROUP BY.
 
2116
    */
 
2117
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
 
2118
    {
 
2119
      /*
 
2120
        Reset 'order' to 'group_list' and reinit variables describing
 
2121
        'order'
 
2122
      */
 
2123
      order= group_list;
 
2124
      simple_order= simple_group;
 
2125
      skip_sort_order= 0;
 
2126
    }
 
2127
    if (order && 
 
2128
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
 
2129
        (const_tables == tables ||
 
2130
         ((simple_order || skip_sort_order) &&
 
2131
          test_if_skip_sort_order(&join_tab[const_tables], order,
 
2132
                                  select_limit, 0, 
 
2133
                                  &join_tab[const_tables].table->
 
2134
                                    keys_in_use_for_query))))
 
2135
      order=0;
 
2136
    having= tmp_having;
 
2137
    select_describe(this, need_tmp,
 
2138
                    order != 0 && !skip_sort_order,
 
2139
                    select_distinct,
 
2140
                    !tables ? "No tables used" : NullS);
 
2141
    return;
 
2142
  }
 
2143
 
 
2144
  JOIN *curr_join= this;
 
2145
  List<Item> *curr_all_fields= &all_fields;
 
2146
  List<Item> *curr_fields_list= &fields_list;
 
2147
  TABLE *curr_tmp_table= 0;
 
2148
  /*
 
2149
    Initialize examined rows here because the values from all join parts
 
2150
    must be accumulated in examined_row_count. Hence every join
 
2151
    iteration must count from zero.
 
2152
  */
 
2153
  curr_join->examined_rows= 0;
 
2154
 
 
2155
  /* Create a tmp table if distinct or if the sort is too complicated */
 
2156
  if (need_tmp)
 
2157
  {
 
2158
    if (tmp_join)
 
2159
    {
 
2160
      /*
 
2161
        We are in a non cacheable sub query. Get the saved join structure
 
2162
        after optimization.
 
2163
        (curr_join may have been modified during last exection and we need
 
2164
        to reset it)
 
2165
      */
 
2166
      curr_join= tmp_join;
 
2167
    }
 
2168
    curr_tmp_table= exec_tmp_table1;
 
2169
 
 
2170
    /* Copy data to the temporary table */
 
2171
    thd_proc_info(thd, "Copying to tmp table");
 
2172
    if (!curr_join->sort_and_group &&
 
2173
        curr_join->const_tables != curr_join->tables)
 
2174
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2175
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2176
    {
 
2177
      error= tmp_error;
 
2178
      return;
 
2179
    }
 
2180
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
 
2181
    
 
2182
    if (curr_join->having)
 
2183
      curr_join->having= curr_join->tmp_having= 0; // Allready done
 
2184
    
 
2185
    /* Change sum_fields reference to calculated fields in tmp_table */
 
2186
    curr_join->all_fields= *curr_all_fields;
 
2187
    if (!items1)
 
2188
    {
 
2189
      items1= items0 + all_fields.elements;
 
2190
      if (sort_and_group || curr_tmp_table->group)
 
2191
      {
 
2192
        if (change_to_use_tmp_fields(thd, items1,
 
2193
                                     tmp_fields_list1, tmp_all_fields1,
 
2194
                                     fields_list.elements, all_fields))
 
2195
          return;
 
2196
      }
 
2197
      else
 
2198
      {
 
2199
        if (change_refs_to_tmp_fields(thd, items1,
 
2200
                                      tmp_fields_list1, tmp_all_fields1,
 
2201
                                      fields_list.elements, all_fields))
 
2202
          return;
 
2203
      }
 
2204
      curr_join->tmp_all_fields1= tmp_all_fields1;
 
2205
      curr_join->tmp_fields_list1= tmp_fields_list1;
 
2206
      curr_join->items1= items1;
 
2207
    }
 
2208
    curr_all_fields= &tmp_all_fields1;
 
2209
    curr_fields_list= &tmp_fields_list1;
 
2210
    curr_join->set_items_ref_array(items1);
 
2211
    
 
2212
    if (sort_and_group || curr_tmp_table->group)
 
2213
    {
 
2214
      curr_join->tmp_table_param.field_count+= 
 
2215
        curr_join->tmp_table_param.sum_func_count+
 
2216
        curr_join->tmp_table_param.func_count;
 
2217
      curr_join->tmp_table_param.sum_func_count= 
 
2218
        curr_join->tmp_table_param.func_count= 0;
 
2219
    }
 
2220
    else
 
2221
    {
 
2222
      curr_join->tmp_table_param.field_count+= 
 
2223
        curr_join->tmp_table_param.func_count;
 
2224
      curr_join->tmp_table_param.func_count= 0;
 
2225
    }
 
2226
    
 
2227
    if (curr_tmp_table->group)
 
2228
    {                                           // Already grouped
 
2229
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
 
2230
        curr_join->order= curr_join->group_list;  /* order by group */
 
2231
      curr_join->group_list= 0;
 
2232
    }
 
2233
    
 
2234
    /*
 
2235
      If we have different sort & group then we must sort the data by group
 
2236
      and copy it to another tmp table
 
2237
      This code is also used if we are using distinct something
 
2238
      we haven't been able to store in the temporary table yet
 
2239
      like SEC_TO_TIME(SUM(...)).
 
2240
    */
 
2241
 
 
2242
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
 
2243
    {                                   /* Must copy to another table */
 
2244
      /* Free first data from old join */
 
2245
      curr_join->join_free();
 
2246
      if (make_simple_join(curr_join, curr_tmp_table))
 
2247
        return;
 
2248
      calc_group_buffer(curr_join, group_list);
 
2249
      count_field_types(select_lex, &curr_join->tmp_table_param,
 
2250
                        curr_join->tmp_all_fields1,
 
2251
                        curr_join->select_distinct && !curr_join->group_list);
 
2252
      curr_join->tmp_table_param.hidden_field_count= 
 
2253
        (curr_join->tmp_all_fields1.elements-
 
2254
         curr_join->tmp_fields_list1.elements);
 
2255
      
 
2256
      
 
2257
      if (exec_tmp_table2)
 
2258
        curr_tmp_table= exec_tmp_table2;
 
2259
      else
 
2260
      {
 
2261
        /* group data to new table */
 
2262
 
 
2263
        /*
 
2264
          If the access method is loose index scan then all MIN/MAX
 
2265
          functions are precomputed, and should be treated as regular
 
2266
          functions. See extended comment in JOIN::exec.
 
2267
        */
 
2268
        if (curr_join->join_tab->is_using_loose_index_scan())
 
2269
          curr_join->tmp_table_param.precomputed_group_by= true;
 
2270
 
 
2271
        if (!(curr_tmp_table=
 
2272
              exec_tmp_table2= create_tmp_table(thd,
 
2273
                                                &curr_join->tmp_table_param,
 
2274
                                                *curr_all_fields,
 
2275
                                                (ORDER*) 0,
 
2276
                                                curr_join->select_distinct && 
 
2277
                                                !curr_join->group_list,
 
2278
                                                1, curr_join->select_options,
 
2279
                                                HA_POS_ERROR,
 
2280
                                                (char *) "")))
 
2281
          return;
 
2282
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
2283
      }
 
2284
      if (curr_join->group_list)
 
2285
      {
 
2286
        thd_proc_info(thd, "Creating sort index");
 
2287
        if (curr_join->join_tab == join_tab && save_join_tab())
 
2288
        {
 
2289
          return;
 
2290
        }
 
2291
        if (create_sort_index(thd, curr_join, curr_join->group_list,
 
2292
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
 
2293
            make_group_fields(this, curr_join))
 
2294
        {
 
2295
          return;
 
2296
        }
 
2297
        sortorder= curr_join->sortorder;
 
2298
      }
 
2299
      
 
2300
      thd_proc_info(thd, "Copying to group table");
 
2301
      tmp_error= -1;
 
2302
      if (curr_join != this)
 
2303
      {
 
2304
        if (sum_funcs2)
 
2305
        {
 
2306
          curr_join->sum_funcs= sum_funcs2;
 
2307
          curr_join->sum_funcs_end= sum_funcs_end2; 
 
2308
        }
 
2309
        else
 
2310
        {
 
2311
          curr_join->alloc_func_list();
 
2312
          sum_funcs2= curr_join->sum_funcs;
 
2313
          sum_funcs_end2= curr_join->sum_funcs_end;
 
2314
        }
 
2315
      }
 
2316
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2317
                                        1, true))
 
2318
        return;
 
2319
      curr_join->group_list= 0;
 
2320
      if (!curr_join->sort_and_group &&
 
2321
          curr_join->const_tables != curr_join->tables)
 
2322
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2323
      if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2324
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2325
      {
 
2326
        error= tmp_error;
 
2327
        return;
 
2328
      }
 
2329
      end_read_record(&curr_join->join_tab->read_record);
 
2330
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
 
2331
      curr_join->join_tab[0].table= 0;           // Table is freed
 
2332
      
 
2333
      // No sum funcs anymore
 
2334
      if (!items2)
 
2335
      {
 
2336
        items2= items1 + all_fields.elements;
 
2337
        if (change_to_use_tmp_fields(thd, items2,
 
2338
                                     tmp_fields_list2, tmp_all_fields2, 
 
2339
                                     fields_list.elements, tmp_all_fields1))
 
2340
          return;
 
2341
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
2342
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
2343
      }
 
2344
      curr_fields_list= &curr_join->tmp_fields_list2;
 
2345
      curr_all_fields= &curr_join->tmp_all_fields2;
 
2346
      curr_join->set_items_ref_array(items2);
 
2347
      curr_join->tmp_table_param.field_count+= 
 
2348
        curr_join->tmp_table_param.sum_func_count;
 
2349
      curr_join->tmp_table_param.sum_func_count= 0;
 
2350
    }
 
2351
    if (curr_tmp_table->distinct)
 
2352
      curr_join->select_distinct=0;             /* Each row is unique */
 
2353
    
 
2354
    curr_join->join_free();                     /* Free quick selects */
 
2355
    if (curr_join->select_distinct && ! curr_join->group_list)
 
2356
    {
 
2357
      thd_proc_info(thd, "Removing duplicates");
 
2358
      if (curr_join->tmp_having)
 
2359
        curr_join->tmp_having->update_used_tables();
 
2360
      if (remove_duplicates(curr_join, curr_tmp_table,
 
2361
                            *curr_fields_list, curr_join->tmp_having))
 
2362
        return;
 
2363
      curr_join->tmp_having=0;
 
2364
      curr_join->select_distinct=0;
 
2365
    }
 
2366
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
 
2367
    if (make_simple_join(curr_join, curr_tmp_table))
 
2368
      return;
 
2369
    calc_group_buffer(curr_join, curr_join->group_list);
 
2370
    count_field_types(select_lex, &curr_join->tmp_table_param, 
 
2371
                      *curr_all_fields, 0);
 
2372
    
 
2373
  }
 
2374
  
 
2375
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
 
2376
  {
 
2377
    if (make_group_fields(this, curr_join))
 
2378
    {
 
2379
      return;
 
2380
    }
 
2381
    if (!items3)
 
2382
    {
 
2383
      if (!items0)
 
2384
        init_items_ref_array();
 
2385
      items3= ref_pointer_array + (all_fields.elements*4);
 
2386
      setup_copy_fields(thd, &curr_join->tmp_table_param,
 
2387
                        items3, tmp_fields_list3, tmp_all_fields3,
 
2388
                        curr_fields_list->elements, *curr_all_fields);
 
2389
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
 
2390
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
 
2391
      tmp_table_param.save_copy_field_end=
 
2392
        curr_join->tmp_table_param.copy_field_end;
 
2393
      curr_join->tmp_all_fields3= tmp_all_fields3;
 
2394
      curr_join->tmp_fields_list3= tmp_fields_list3;
 
2395
    }
 
2396
    else
 
2397
    {
 
2398
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
 
2399
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
 
2400
      curr_join->tmp_table_param.copy_field_end=
 
2401
        tmp_table_param.save_copy_field_end;
 
2402
    }
 
2403
    curr_fields_list= &tmp_fields_list3;
 
2404
    curr_all_fields= &tmp_all_fields3;
 
2405
    curr_join->set_items_ref_array(items3);
 
2406
 
 
2407
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2408
                                      1, true) || 
 
2409
        setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2410
        thd->is_fatal_error)
 
2411
      return;
 
2412
  }
 
2413
  if (curr_join->group_list || curr_join->order)
 
2414
  {
 
2415
    thd_proc_info(thd, "Sorting result");
 
2416
    /* If we have already done the group, add HAVING to sorted table */
 
2417
    if (curr_join->tmp_having && ! curr_join->group_list && 
 
2418
        ! curr_join->sort_and_group)
 
2419
    {
 
2420
      // Some tables may have been const
 
2421
      curr_join->tmp_having->update_used_tables();
 
2422
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
 
2423
      table_map used_tables= (curr_join->const_table_map |
 
2424
                              curr_table->table->map);
 
2425
 
 
2426
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
 
2427
                                                 used_tables,
 
2428
                                                 used_tables, 0);
 
2429
      if (sort_table_cond)
 
2430
      {
 
2431
        if (!curr_table->select)
 
2432
          if (!(curr_table->select= new SQL_SELECT))
 
2433
            return;
 
2434
        if (!curr_table->select->cond)
 
2435
          curr_table->select->cond= sort_table_cond;
 
2436
        else                                    // This should never happen
 
2437
        {
 
2438
          if (!(curr_table->select->cond=
 
2439
                new Item_cond_and(curr_table->select->cond,
 
2440
                                  sort_table_cond)))
 
2441
            return;
 
2442
          /*
 
2443
            Item_cond_and do not need fix_fields for execution, its parameters
 
2444
            are fixed or do not need fix_fields, too
 
2445
          */
 
2446
          curr_table->select->cond->quick_fix_field();
 
2447
        }
 
2448
        curr_table->select_cond= curr_table->select->cond;
 
2449
        curr_table->select_cond->top_level_item();
 
2450
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
2451
                                                   ~ (table_map) 0,
 
2452
                                                   ~used_tables, 0);
 
2453
      }
 
2454
    }
 
2455
    {
 
2456
      if (group)
 
2457
        curr_join->select_limit= HA_POS_ERROR;
 
2458
      else
 
2459
      {
 
2460
        /*
 
2461
          We can abort sorting after thd->select_limit rows if we there is no
 
2462
          WHERE clause for any tables after the sorted one.
 
2463
        */
 
2464
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
2465
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
2466
        for (; curr_table < end_table ; curr_table++)
 
2467
        {
 
2468
          /*
 
2469
            table->keyuse is set in the case there was an original WHERE clause
 
2470
            on the table that was optimized away.
 
2471
          */
 
2472
          if (curr_table->select_cond ||
 
2473
              (curr_table->keyuse && !curr_table->first_inner))
 
2474
          {
 
2475
            /* We have to sort all rows */
 
2476
            curr_join->select_limit= HA_POS_ERROR;
 
2477
            break;
 
2478
          }
 
2479
        }
 
2480
      }
 
2481
      if (curr_join->join_tab == join_tab && save_join_tab())
 
2482
      {
 
2483
        return;
 
2484
      }
 
2485
      /*
 
2486
        Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
 
2487
        chose FILESORT to be faster than INDEX SCAN or there is no 
 
2488
        suitable index present.
 
2489
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
2490
        finally replace sorting with index scan if there is a LIMIT clause in
 
2491
        the query. XXX: it's never shown in EXPLAIN!
 
2492
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
2493
      */
 
2494
      if (create_sort_index(thd, curr_join,
 
2495
                            curr_join->group_list ? 
 
2496
                            curr_join->group_list : curr_join->order,
 
2497
                            curr_join->select_limit,
 
2498
                            (select_options & OPTION_FOUND_ROWS ?
 
2499
                             HA_POS_ERROR : unit->select_limit_cnt),
 
2500
                            curr_join->group_list ? true : false))
 
2501
        return;
 
2502
      sortorder= curr_join->sortorder;
 
2503
      if (curr_join->const_tables != curr_join->tables &&
 
2504
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
 
2505
      {
 
2506
        /*
 
2507
          If no IO cache exists for the first table then we are using an
 
2508
          INDEX SCAN and no filesort. Thus we should not remove the sorted
 
2509
          attribute on the INDEX SCAN.
 
2510
        */
 
2511
        skip_sort_order= 1;
 
2512
      }
 
2513
    }
 
2514
  }
 
2515
  /* XXX: When can we have here thd->is_error() not zero? */
 
2516
  if (thd->is_error())
 
2517
  {
 
2518
    error= thd->is_error();
 
2519
    return;
 
2520
  }
 
2521
  curr_join->having= curr_join->tmp_having;
 
2522
  curr_join->fields= curr_fields_list;
 
2523
 
 
2524
  {
 
2525
    thd_proc_info(thd, "Sending data");
 
2526
    result->send_fields(*curr_fields_list,
 
2527
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2528
    error= do_select(curr_join, curr_fields_list, NULL);
 
2529
    thd->limit_found_rows= curr_join->send_records;
 
2530
  }
 
2531
 
 
2532
  /* Accumulate the counts from all join iterations of all join parts. */
 
2533
  thd->examined_row_count+= curr_join->examined_rows;
 
2534
 
 
2535
  /* 
 
2536
    With EXPLAIN EXTENDED we have to restore original ref_array
 
2537
    for a derived table which is always materialized.
 
2538
    Otherwise we would not be able to print the query  correctly.
 
2539
  */ 
 
2540
  if (items0 &&
 
2541
      (thd->lex->describe & DESCRIBE_EXTENDED) &&
 
2542
      select_lex->linkage == DERIVED_TABLE_TYPE)      
 
2543
    set_items_ref_array(items0);
 
2544
 
 
2545
  return;
 
2546
}
 
2547
 
 
2548
 
 
2549
/**
 
2550
  Clean up join.
 
2551
 
 
2552
  @return
 
2553
    Return error that hold JOIN.
 
2554
*/
 
2555
 
 
2556
int
 
2557
JOIN::destroy()
 
2558
{
 
2559
  select_lex->join= 0;
 
2560
 
 
2561
  if (tmp_join)
 
2562
  {
 
2563
    if (join_tab != tmp_join->join_tab)
 
2564
    {
 
2565
      JOIN_TAB *tab, *end;
 
2566
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
 
2567
        tab->cleanup();
 
2568
    }
 
2569
    tmp_join->tmp_join= 0;
 
2570
    tmp_table_param.copy_field=0;
 
2571
    return(tmp_join->destroy());
 
2572
  }
 
2573
  cond_equal= 0;
 
2574
 
 
2575
  cleanup(1);
 
2576
  if (exec_tmp_table1)
 
2577
    free_tmp_table(thd, exec_tmp_table1);
 
2578
  if (exec_tmp_table2)
 
2579
    free_tmp_table(thd, exec_tmp_table2);
 
2580
  delete select;
 
2581
  delete_dynamic(&keyuse);
 
2582
  return(error);
 
2583
}
 
2584
 
 
2585
 
 
2586
 
316
2587
/**
317
2588
  An entry point to single-unit select (a select without UNION).
318
2589
 
319
 
  @param session                  thread Cursor
 
2590
  @param thd                  thread handler
320
2591
  @param rref_pointer_array   a reference to ref_pointer_array of
321
2592
                              the top-level select_lex for this query
322
2593
  @param tables               list of all tables used in this query.
323
2594
                              The tables have been pre-opened.
324
 
  @param wild_num             number of wildcards used in the top level
 
2595
  @param wild_num             number of wildcards used in the top level 
325
2596
                              select of this query.
326
2597
                              For example statement
327
2598
                              SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
337
2608
  @param order                linked list of ORDER BY agruments
338
2609
  @param group                linked list of GROUP BY arguments
339
2610
  @param having               top level item of HAVING expression
 
2611
  @param proc_param           list of PROCEDUREs
340
2612
  @param select_options       select options (BIG_RESULT, etc)
341
2613
  @param result               an instance of result set handling class.
342
2614
                              This object is responsible for send result
343
2615
                              set rows to the client or inserting them
344
2616
                              into a table.
345
 
  @param select_lex           the only Select_Lex of this query
 
2617
  @param select_lex           the only SELECT_LEX of this query
346
2618
  @param unit                 top-level UNIT of this query
347
2619
                              UNIT is an artificial object created by the
348
2620
                              parser for every SELECT clause.
355
2627
  @retval
356
2628
    true   an error
357
2629
*/
358
 
bool select_query(Session *session,
359
 
                  Item ***rref_pointer_array,
360
 
                  TableList *tables, 
361
 
                  uint32_t wild_num, 
362
 
                  List<Item> &fields,
363
 
                  COND *conds, 
364
 
                  uint32_t og_num,  
365
 
                  Order *order,
366
 
                  Order *group,
367
 
                  Item *having, 
368
 
                  uint64_t select_options,
369
 
                  select_result *result, 
370
 
                  Select_Lex_Unit *unit,
371
 
                  Select_Lex *select_lex)
 
2630
 
 
2631
bool
 
2632
mysql_select(THD *thd, Item ***rref_pointer_array,
 
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, uint64_t select_options,
 
2636
             select_result *result, SELECT_LEX_UNIT *unit,
 
2637
             SELECT_LEX *select_lex)
372
2638
{
373
2639
  bool err;
374
2640
  bool free_join= 1;
375
2641
 
376
2642
  select_lex->context.resolve_in_select_list= true;
377
 
  Join *join;
 
2643
  JOIN *join;
378
2644
  if (select_lex->join != 0)
379
2645
  {
380
2646
    join= select_lex->join;
383
2649
      creation
384
2650
    */
385
2651
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
386
 
        (select_options & SELECT_DESCRIBE))
 
2652
        (select_options & SELECT_DESCRIBE))
387
2653
    {
388
2654
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
389
2655
      {
390
 
        //here is EXPLAIN of subselect or derived table
391
 
        if (join->change_result(result))
392
 
        {
393
 
          return(true);
394
 
        }
 
2656
        //here is EXPLAIN of subselect or derived table
 
2657
        if (join->change_result(result))
 
2658
        {
 
2659
          return(true);
 
2660
        }
395
2661
      }
396
2662
      else
397
2663
      {
398
2664
        if ((err= join->prepare(rref_pointer_array, tables, wild_num,
399
 
                               conds, og_num, order, group, having, select_lex, unit)))
400
 
        {
401
 
          goto err;
402
 
        }
 
2665
                               conds, og_num, order, group, having, proc_param,
 
2666
                               select_lex, unit)))
 
2667
        {
 
2668
          goto err;
 
2669
        }
403
2670
      }
404
2671
    }
405
2672
    free_join= 0;
407
2674
  }
408
2675
  else
409
2676
  {
410
 
    if (!(join= new Join(session, fields, select_options, result)))
411
 
      return(true);
412
 
    session->set_proc_info("init");
413
 
    session->used_tables=0;                         // Updated by setup_fields
 
2677
    if (!(join= new JOIN(thd, fields, select_options, result)))
 
2678
        return(true);
 
2679
    thd_proc_info(thd, "init");
 
2680
    thd->used_tables=0;                         // Updated by setup_fields
414
2681
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
415
 
                           conds, og_num, order, group, having,
 
2682
                           conds, og_num, order, group, having, proc_param,
416
2683
                           select_lex, unit)) == true)
417
2684
    {
418
2685
      goto err;
419
2686
    }
420
2687
  }
421
2688
 
422
 
  err= join->optimize();
423
 
  if (err)
424
 
  {
425
 
    goto err; // 1
426
 
  }
427
 
 
428
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
2689
  /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
 
2690
  if (join->flatten_subqueries())
 
2691
  {
 
2692
    err= 1;
 
2693
    goto err;
 
2694
  }
 
2695
  /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
 
2696
 
 
2697
  if ((err= join->optimize()))
 
2698
  {
 
2699
    goto err;                                   // 1
 
2700
  }
 
2701
 
 
2702
  if (thd->lex->describe & DESCRIBE_EXTENDED)
429
2703
  {
430
2704
    join->conds_history= join->conds;
431
2705
    join->having_history= (join->having?join->having:join->tmp_having);
432
2706
  }
433
2707
 
434
 
  if (session->is_error())
 
2708
  if (thd->is_error())
435
2709
    goto err;
436
2710
 
437
2711
  join->exec();
438
2712
 
439
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
2713
  if (thd->lex->describe & DESCRIBE_EXTENDED)
440
2714
  {
441
2715
    select_lex->where= join->conds_history;
442
2716
    select_lex->having= join->having_history;
445
2719
err:
446
2720
  if (free_join)
447
2721
  {
448
 
    session->set_proc_info("end");
 
2722
    thd_proc_info(thd, "end");
449
2723
    err|= select_lex->cleanup();
450
 
    return(err || session->is_error());
 
2724
    return(err || thd->is_error());
451
2725
  }
452
2726
  return(join->error);
453
2727
}
454
2728
 
455
 
inline Item *and_items(Item* cond, Item *item)
 
2729
 
 
2730
int subq_sj_candidate_cmp(Item_in_subselect* const *el1, 
 
2731
                          Item_in_subselect* const *el2)
 
2732
{
 
2733
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 : 
 
2734
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
 
2735
}
 
2736
 
 
2737
 
 
2738
inline Item * and_items(Item* cond, Item *item)
456
2739
{
457
2740
  return (cond? (new Item_cond_and(cond, item)) : item);
458
2741
}
459
2742
 
 
2743
 
 
2744
static TABLE_LIST *alloc_join_nest(THD *thd)
 
2745
{
 
2746
  TABLE_LIST *tbl;
 
2747
  if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
 
2748
                                       sizeof(NESTED_JOIN))))
 
2749
    return NULL;
 
2750
  tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl + 
 
2751
                                    ALIGN_SIZE(sizeof(TABLE_LIST)));
 
2752
  return tbl;
 
2753
}
 
2754
 
 
2755
 
 
2756
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
 
2757
{
 
2758
  List_iterator<TABLE_LIST> it(*tlist);
 
2759
  TABLE_LIST *table;
 
2760
  while ((table= it++))
 
2761
  {
 
2762
    if (table->on_expr)
 
2763
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
 
2764
    if (table->nested_join)
 
2765
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
 
2766
  }
 
2767
}
 
2768
 
 
2769
 
 
2770
/*
 
2771
  Convert a subquery predicate into a TABLE_LIST semi-join nest
 
2772
 
 
2773
  SYNOPSIS
 
2774
    convert_subq_to_sj()
 
2775
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON 
 
2776
                    clause
 
2777
       subq_pred    Subquery predicate to be converted
 
2778
  
 
2779
  DESCRIPTION
 
2780
    Convert a subquery predicate into a TABLE_LIST semi-join nest. All the 
 
2781
    prerequisites are already checked, so the conversion is always successfull.
 
2782
 
 
2783
    Prepared Statements: the transformation is permanent:
 
2784
     - Changes in TABLE_LIST structures are naturally permanent
 
2785
     - Item tree changes are performed on statement MEM_ROOT:
 
2786
        = we activate statement MEM_ROOT 
 
2787
        = this function is called before the first fix_prepare_information
 
2788
          call.
 
2789
 
 
2790
    This is intended because the criteria for subquery-to-sj conversion remain
 
2791
    constant for the lifetime of the Prepared Statement.
 
2792
 
 
2793
  RETURN
 
2794
    false  OK
 
2795
    true   Out of memory error
 
2796
*/
 
2797
 
 
2798
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
 
2799
{
 
2800
  SELECT_LEX *parent_lex= parent_join->select_lex;
 
2801
  TABLE_LIST *emb_tbl_nest= NULL;
 
2802
  List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
 
2803
  THD *thd= parent_join->thd;
 
2804
 
 
2805
  /*
 
2806
    1. Find out where to put the predicate into.
 
2807
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
 
2808
  */
 
2809
  if ((void*)subq_pred->expr_join_nest != (void*)1)
 
2810
  {
 
2811
    if (subq_pred->expr_join_nest->nested_join)
 
2812
    {
 
2813
      /*
 
2814
        We're dealing with
 
2815
 
 
2816
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
 
2817
 
 
2818
        The sj-nest will be inserted into the brackets nest.
 
2819
      */
 
2820
      emb_tbl_nest=  subq_pred->expr_join_nest;
 
2821
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2822
    }
 
2823
    else if (!subq_pred->expr_join_nest->outer_join)
 
2824
    {
 
2825
      /*
 
2826
        We're dealing with
 
2827
 
 
2828
          ... INNER JOIN tblX ON (subquery AND whatever) ...
 
2829
 
 
2830
        The sj-nest will be tblX's "sibling", i.e. another child of its
 
2831
        parent. This is ok because tblX is joined as an inner join.
 
2832
      */
 
2833
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
 
2834
      if (emb_tbl_nest)
 
2835
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2836
    }
 
2837
    else if (!subq_pred->expr_join_nest->nested_join)
 
2838
    {
 
2839
      TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;      
 
2840
      TABLE_LIST *wrap_nest;
 
2841
      /*
 
2842
        We're dealing with
 
2843
 
 
2844
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
 
2845
 
 
2846
        we'll need to convert it into:
 
2847
 
 
2848
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
 
2849
                        |                      |
 
2850
                        |<----- wrap_nest ---->|
 
2851
        
 
2852
        Q:  other subqueries may be pointing to this element. What to do?
 
2853
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
 
2854
            But we'll need to fix other pointers.
 
2855
        A2: Another way: have TABLE_LIST::next_ptr so the following
 
2856
            subqueries know the table has been nested.
 
2857
        A3: changes in the TABLE_LIST::outer_join will make everything work
 
2858
            automatically.
 
2859
      */
 
2860
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
 
2861
      {
 
2862
        return(true);
 
2863
      }
 
2864
      wrap_nest->embedding= outer_tbl->embedding;
 
2865
      wrap_nest->join_list= outer_tbl->join_list;
 
2866
      wrap_nest->alias= (char*) "(sj-wrap)";
 
2867
 
 
2868
      wrap_nest->nested_join->join_list.empty();
 
2869
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
 
2870
 
 
2871
      outer_tbl->embedding= wrap_nest;
 
2872
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
 
2873
 
 
2874
      /*
 
2875
        wrap_nest will take place of outer_tbl, so move the outer join flag
 
2876
        and on_expr
 
2877
      */
 
2878
      wrap_nest->outer_join= outer_tbl->outer_join;
 
2879
      outer_tbl->outer_join= 0;
 
2880
 
 
2881
      wrap_nest->on_expr= outer_tbl->on_expr;
 
2882
      outer_tbl->on_expr= NULL;
 
2883
 
 
2884
      List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
 
2885
      TABLE_LIST *tbl;
 
2886
      while ((tbl= li++))
 
2887
      {
 
2888
        if (tbl == outer_tbl)
 
2889
        {
 
2890
          li.replace(wrap_nest);
 
2891
          break;
 
2892
        }
 
2893
      }
 
2894
      /*
 
2895
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the 
 
2896
        semi-join nest into it
 
2897
      */
 
2898
      emb_join_list= &wrap_nest->nested_join->join_list;
 
2899
      emb_tbl_nest=  wrap_nest;
 
2900
    }
 
2901
  }
 
2902
 
 
2903
  TABLE_LIST *sj_nest;
 
2904
  NESTED_JOIN *nested_join;
 
2905
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
 
2906
  {
 
2907
    return(true);
 
2908
  }
 
2909
  nested_join= sj_nest->nested_join;
 
2910
 
 
2911
  sj_nest->join_list= emb_join_list;
 
2912
  sj_nest->embedding= emb_tbl_nest;
 
2913
  sj_nest->alias= (char*) "(sj-nest)";
 
2914
  /* Nests do not participate in those 'chains', so: */
 
2915
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
 
2916
  emb_join_list->push_back(sj_nest);
 
2917
 
 
2918
  /* 
 
2919
    nested_join->used_tables and nested_join->not_null_tables are
 
2920
    initialized in simplify_joins().
 
2921
  */
 
2922
  
 
2923
  /* 
 
2924
    2. Walk through subquery's top list and set 'embedding' to point to the
 
2925
       sj-nest.
 
2926
  */
 
2927
  st_select_lex *subq_lex= subq_pred->unit->first_select();
 
2928
  nested_join->join_list.empty();
 
2929
  List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
 
2930
  TABLE_LIST *tl, *last_leaf;
 
2931
  while ((tl= li++))
 
2932
  {
 
2933
    tl->embedding= sj_nest;
 
2934
    tl->join_list= &nested_join->join_list;
 
2935
    nested_join->join_list.push_back(tl);
 
2936
  }
 
2937
  
 
2938
  /*
 
2939
    Reconnect the next_leaf chain.
 
2940
    TODO: Do we have to put subquery's tables at the end of the chain?
 
2941
          Inserting them at the beginning would be a bit faster.
 
2942
    NOTE: We actually insert them at the front! That's because the order is
 
2943
          reversed in this list.
 
2944
  */
 
2945
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
 
2946
  tl->next_leaf= subq_lex->leaf_tables;
 
2947
  last_leaf= tl;
 
2948
 
 
2949
  /*
 
2950
    Same as above for next_local chain
 
2951
    (a theory: a next_local chain always starts with ::leaf_tables
 
2952
     because view's tables are inserted after the view)
 
2953
  */
 
2954
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
 
2955
  tl->next_local= subq_lex->leaf_tables;
 
2956
 
 
2957
  /* A theory: no need to re-connect the next_global chain */
 
2958
 
 
2959
  /* 3. Remove the original subquery predicate from the WHERE/ON */
 
2960
 
 
2961
  // The subqueries were replaced for Item_int(1) earlier
 
2962
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
 
2963
  /*TODO: also reset the 'with_subselect' there. */
 
2964
 
 
2965
  /* n. Adjust the parent_join->tables counter */
 
2966
  uint table_no= parent_join->tables;
 
2967
  /* n. Walk through child's tables and adjust table->map */
 
2968
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
 
2969
  {
 
2970
    tl->table->tablenr= table_no;
 
2971
    tl->table->map= ((table_map)1) << table_no;
 
2972
    SELECT_LEX *old_sl= tl->select_lex;
 
2973
    tl->select_lex= parent_join->select_lex; 
 
2974
    for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
 
2975
      emb->select_lex= parent_join->select_lex;
 
2976
  }
 
2977
  parent_join->tables += subq_lex->join->tables;
 
2978
 
 
2979
  /* 
 
2980
    Put the subquery's WHERE into semi-join's sj_on_expr
 
2981
    Add the subquery-induced equalities too.
 
2982
  */
 
2983
  SELECT_LEX *save_lex= thd->lex->current_select;
 
2984
  thd->lex->current_select=subq_lex;
 
2985
  if (!subq_pred->left_expr->fixed &&
 
2986
       subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
 
2987
    return(true);
 
2988
  thd->lex->current_select=save_lex;
 
2989
 
 
2990
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
 
2991
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
 
2992
                                        subq_pred->left_expr->used_tables();
 
2993
  sj_nest->sj_on_expr= subq_lex->where;
 
2994
 
 
2995
  /*
 
2996
    Create the IN-equalities and inject them into semi-join's ON expression.
 
2997
    Additionally, for InsideOut strategy
 
2998
     - Record the number of IN-equalities.
 
2999
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
 
3000
       see which of the expressions are bound and which are not (for those
 
3001
       we'll produce a distinct stream of (ie_i1,...ie_ik).
 
3002
 
 
3003
       (TODO: can we just create a list of pointers and hope the expressions
 
3004
       will not substitute themselves on fix_fields()? or we need to wrap
 
3005
       them into Item_direct_view_refs and store pointers to those. The
 
3006
       pointers to Item_direct_view_refs are guaranteed to be stable as 
 
3007
       Item_direct_view_refs doesn't substitute itself with anything in 
 
3008
       Item_direct_view_ref::fix_fields.
 
3009
  */
 
3010
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
 
3011
  sj_nest->nested_join->sj_outer_expr_list.empty();
 
3012
 
 
3013
  if (subq_pred->left_expr->cols() == 1)
 
3014
  {
 
3015
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
 
3016
 
 
3017
    Item *item_eq= new Item_func_eq(subq_pred->left_expr, 
 
3018
                                    subq_lex->ref_pointer_array[0]);
 
3019
    item_eq->name= (char*)subq_sj_cond_name;
 
3020
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3021
  }
 
3022
  else
 
3023
  {
 
3024
    for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
 
3025
    {
 
3026
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
 
3027
                                                element_index(i));
 
3028
      Item *item_eq= 
 
3029
        new Item_func_eq(subq_pred->left_expr->element_index(i), 
 
3030
                         subq_lex->ref_pointer_array[i]);
 
3031
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
 
3032
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3033
    }
 
3034
  }
 
3035
  /* Fix the created equality and AND */
 
3036
  sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
 
3037
 
 
3038
  /*
 
3039
    Walk through sj nest's WHERE and ON expressions and call
 
3040
    item->fix_table_changes() for all items.
 
3041
  */
 
3042
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
 
3043
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
 
3044
 
 
3045
 
 
3046
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
 
3047
  subq_lex->master_unit()->exclude_level();
 
3048
 
 
3049
  /* Inject sj_on_expr into the parent's WHERE or ON */
 
3050
  if (emb_tbl_nest)
 
3051
  {
 
3052
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr, 
 
3053
                                     sj_nest->sj_on_expr);
 
3054
    emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
 
3055
  }
 
3056
  else
 
3057
  {
 
3058
    /* Inject into the WHERE */
 
3059
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
 
3060
    parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
 
3061
    parent_join->select_lex->where= parent_join->conds;
 
3062
  }
 
3063
 
 
3064
  return(false);
 
3065
}
 
3066
 
 
3067
 
 
3068
/*
 
3069
  Convert candidate subquery predicates to semi-joins
 
3070
 
 
3071
  SYNOPSIS
 
3072
    JOIN::flatten_subqueries()
 
3073
 
 
3074
  DESCRIPTION
 
3075
    Convert candidate subquery predicates to semi-joins.
 
3076
 
 
3077
  RETURN 
 
3078
    false  OK
 
3079
    true   Error
 
3080
*/
 
3081
 
 
3082
bool JOIN::flatten_subqueries()
 
3083
{
 
3084
  Item_in_subselect **in_subq;
 
3085
  Item_in_subselect **in_subq_end;
 
3086
 
 
3087
  if (sj_subselects.elements() == 0)
 
3088
    return(false);
 
3089
 
 
3090
  /* 1. Fix children subqueries */
 
3091
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); 
 
3092
       in_subq != in_subq_end; in_subq++)
 
3093
  {
 
3094
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3095
    child_join->outer_tables = child_join->tables;
 
3096
    if (child_join->flatten_subqueries())
 
3097
      return(true);
 
3098
    (*in_subq)->sj_convert_priority= 
 
3099
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
 
3100
  }
 
3101
 
 
3102
  //dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
 
3103
  /* 
 
3104
    2. Pick which subqueries to convert:
 
3105
      sort the subquery array
 
3106
      - prefer correlated subqueries over uncorrelated;
 
3107
      - prefer subqueries that have greater number of outer tables;
 
3108
  */
 
3109
  sj_subselects.sort(subq_sj_candidate_cmp);
 
3110
  // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
 
3111
  /* Replace all subqueries to be flattened with Item_int(1) */
 
3112
  for (in_subq= sj_subselects.front(); 
 
3113
       in_subq != in_subq_end && 
 
3114
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3115
       in_subq++)
 
3116
  {
 
3117
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
 
3118
      return(true);
 
3119
  }
 
3120
 
 
3121
  for (in_subq= sj_subselects.front(); 
 
3122
       in_subq != in_subq_end && 
 
3123
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3124
       in_subq++)
 
3125
  {
 
3126
    if (convert_subq_to_sj(this, *in_subq))
 
3127
      return(true);
 
3128
  }
 
3129
 
 
3130
  /* 3. Finalize those we didn't convert */
 
3131
  for (; in_subq!= in_subq_end; in_subq++)
 
3132
  {
 
3133
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3134
    Item_subselect::trans_res res;
 
3135
    (*in_subq)->changed= 0;
 
3136
    (*in_subq)->fixed= 0;
 
3137
    res= (*in_subq)->select_transformer(child_join);
 
3138
    if (res == Item_subselect::RES_ERROR)
 
3139
      return(true);
 
3140
 
 
3141
    (*in_subq)->changed= 1;
 
3142
    (*in_subq)->fixed= 1;
 
3143
 
 
3144
    Item *substitute= (*in_subq)->substitution;
 
3145
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
 
3146
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
 
3147
      return(true);
 
3148
 
 
3149
    //if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
 
3150
    //  return(true);
 
3151
  }
 
3152
  sj_subselects.clear();
 
3153
  return(false);
 
3154
}
 
3155
 
 
3156
 
 
3157
/**
 
3158
  Setup for execution all subqueries of a query, for which the optimizer
 
3159
  chose hash semi-join.
 
3160
 
 
3161
  @details Iterate over all subqueries of the query, and if they are under an
 
3162
  IN predicate, and the optimizer chose to compute it via hash semi-join:
 
3163
  - try to initialize all data structures needed for the materialized execution
 
3164
    of the IN predicate,
 
3165
  - if this fails, then perform the IN=>EXISTS transformation which was
 
3166
    previously blocked during JOIN::prepare.
 
3167
 
 
3168
  This method is part of the "code generation" query processing phase.
 
3169
 
 
3170
  This phase must be called after substitute_for_best_equal_field() because
 
3171
  that function may replace items with other items from a multiple equality,
 
3172
  and we need to reference the correct items in the index access method of the
 
3173
  IN predicate.
 
3174
 
 
3175
  @return Operation status
 
3176
  @retval false     success.
 
3177
  @retval true      error occurred.
 
3178
*/
 
3179
 
 
3180
bool JOIN::setup_subquery_materialization()
 
3181
{
 
3182
  for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
 
3183
       un= un->next_unit())
 
3184
  {
 
3185
    for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
 
3186
    {
 
3187
      Item_subselect *subquery_predicate= sl->master_unit()->item;
 
3188
      if (subquery_predicate &&
 
3189
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
 
3190
      {
 
3191
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
 
3192
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
 
3193
            in_subs->setup_engine())
 
3194
          return true;
 
3195
      }
 
3196
    }
 
3197
  }
 
3198
  return false;
 
3199
}
 
3200
 
 
3201
 
 
3202
/*
 
3203
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3204
 
 
3205
  SYNOPSIS
 
3206
    find_eq_ref_candidate()
 
3207
      table             Table to be checked
 
3208
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
 
3209
                        count.
 
3210
 
 
3211
  DESCRIPTION
 
3212
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3213
 
 
3214
  TODO
 
3215
    Check again if it is feasible to factor common parts with constant table
 
3216
    search
 
3217
 
 
3218
  RETURN
 
3219
    true  - There exists an eq_ref(outer-tables) candidate
 
3220
    false - Otherwise
 
3221
*/
 
3222
 
 
3223
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
 
3224
{
 
3225
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
 
3226
  uint key;
 
3227
 
 
3228
  if (keyuse)
 
3229
  {
 
3230
    while (1) /* For each key */
 
3231
    {
 
3232
      key= keyuse->key;
 
3233
      KEY *keyinfo= table->key_info + key;
 
3234
      key_part_map bound_parts= 0;
 
3235
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
 
3236
      {
 
3237
        do  /* For all equalities on all key parts */
 
3238
        {
 
3239
          /* Check if this is "t.keypart = expr(outer_tables) */
 
3240
          if (!(keyuse->used_tables & sj_inner_tables) &&
 
3241
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
 
3242
          {
 
3243
            bound_parts |= 1 << keyuse->keypart;
 
3244
          }
 
3245
          keyuse++;
 
3246
        } while (keyuse->key == key && keyuse->table == table);
 
3247
 
 
3248
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
 
3249
          return true;
 
3250
        if (keyuse->table != table)
 
3251
          return false;
 
3252
      }
 
3253
      else
 
3254
      {
 
3255
        do
 
3256
        {
 
3257
          keyuse++;
 
3258
          if (keyuse->table != table)
 
3259
            return false;
 
3260
        }
 
3261
        while (keyuse->key == key);
 
3262
      }
 
3263
    }
 
3264
  }
 
3265
  return false;
 
3266
}
 
3267
 
 
3268
 
 
3269
/*
 
3270
  Pull tables out of semi-join nests, if possible
 
3271
 
 
3272
  SYNOPSIS
 
3273
    pull_out_semijoin_tables()
 
3274
      join  The join where to do the semi-join flattening
 
3275
 
 
3276
  DESCRIPTION
 
3277
    Try to pull tables out of semi-join nests.
 
3278
     
 
3279
    PRECONDITIONS
 
3280
    When this function is called, the join may have several semi-join nests
 
3281
    (possibly within different semi-join nests), but it is guaranteed that
 
3282
    one semi-join nest does not contain another.
 
3283
   
 
3284
    ACTION
 
3285
    A table can be pulled out of the semi-join nest if
 
3286
     - It is a constant table
 
3287
     - It is accessed 
 
3288
 
 
3289
    POSTCONDITIONS
 
3290
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
 
3291
       tables)
 
3292
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
 
3293
     * Semi-join nests TABLE_LIST::sj_inner_tables
 
3294
 
 
3295
    This operation is (and should be) performed at each PS execution since
 
3296
    tables may become/cease to be constant across PS reexecutions.
 
3297
 
 
3298
  RETURN 
 
3299
    0 - OK
 
3300
    1 - Out of memory error
 
3301
*/
 
3302
 
 
3303
int pull_out_semijoin_tables(JOIN *join)
 
3304
{
 
3305
  TABLE_LIST *sj_nest;
 
3306
  List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
 
3307
   
 
3308
  /* Try pulling out of the each of the semi-joins */
 
3309
  while ((sj_nest= sj_list_it++))
 
3310
  {
 
3311
    /* Action #1: Mark the constant tables to be pulled out */
 
3312
    table_map pulled_tables= 0;
 
3313
     
 
3314
    List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
 
3315
    TABLE_LIST *tbl;
 
3316
    while ((tbl= child_li++))
 
3317
    {
 
3318
      if (tbl->table)
 
3319
      {
 
3320
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
 
3321
        if (tbl->table->map & join->const_table_map)
 
3322
        {
 
3323
          pulled_tables |= tbl->table->map;
 
3324
        }
 
3325
      }
 
3326
    }
 
3327
    
 
3328
    /*
 
3329
      Action #2: Find which tables we can pull out based on
 
3330
      update_ref_and_keys() data. Note that pulling one table out can allow
 
3331
      us to pull out some other tables too.
 
3332
    */
 
3333
    bool pulled_a_table;
 
3334
    do 
 
3335
    {
 
3336
      pulled_a_table= false;
 
3337
      child_li.rewind();
 
3338
      while ((tbl= child_li++))
 
3339
      {
 
3340
        if (tbl->table && !(pulled_tables & tbl->table->map))
 
3341
        {
 
3342
          if (find_eq_ref_candidate(tbl->table, 
 
3343
                                    sj_nest->nested_join->used_tables & 
 
3344
                                    ~pulled_tables))
 
3345
          {
 
3346
            pulled_a_table= true;
 
3347
            pulled_tables |= tbl->table->map;
 
3348
          }
 
3349
        }
 
3350
      }
 
3351
    } while (pulled_a_table);
 
3352
 
 
3353
    child_li.rewind();
 
3354
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
 
3355
    {
 
3356
      (sj_nest)->sj_inner_tables= 0;
 
3357
      while ((tbl= child_li++))
 
3358
      {
 
3359
        if (tbl->table)
 
3360
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3361
      }
 
3362
    }
 
3363
    else
 
3364
    {
 
3365
      /* Record the bitmap of inner tables, mark the inner tables */
 
3366
      table_map inner_tables=(sj_nest)->nested_join->used_tables & 
 
3367
                             ~pulled_tables;
 
3368
      (sj_nest)->sj_inner_tables= inner_tables;
 
3369
      while ((tbl= child_li++))
 
3370
      {
 
3371
        if (tbl->table)
 
3372
        {
 
3373
          if (inner_tables & tbl->table->map)
 
3374
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
 
3375
          else
 
3376
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3377
        }
 
3378
      }
 
3379
    }
 
3380
  }
 
3381
  return(0);
 
3382
}
 
3383
 
460
3384
/*****************************************************************************
461
 
  Create JoinTableS, make a guess about the table types,
 
3385
  Create JOIN_TABS, make a guess about the table types,
462
3386
  Approximate how many records will be used in each table
463
3387
*****************************************************************************/
464
 
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
 
3388
 
 
3389
 
 
3390
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
 
3391
                                      TABLE *table,
 
3392
                                      const key_map *keys,ha_rows limit)
465
3393
{
466
3394
  int error;
467
 
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
 
3395
  if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
468
3396
    return(0);                           // Fatal error flag is set
469
3397
  if (select)
470
3398
  {
471
3399
    select->head=table;
472
3400
    table->reginfo.impossible_range=0;
473
 
    if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
 
3401
    if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
474
3402
                                          limit, 0, false)) == 1)
475
3403
      return(select->quick->records);
476
3404
    if (error == -1)
482
3410
  return(HA_POS_ERROR);                 /* This shouldn't happend */
483
3411
}
484
3412
 
 
3413
/*
 
3414
   This structure is used to collect info on potentially sargable
 
3415
   predicates in order to check whether they become sargable after
 
3416
   reading const tables.
 
3417
   We form a bitmap of indexes that can be used for sargable predicates.
 
3418
   Only such indexes are involved in range analysis.
 
3419
*/
 
3420
typedef struct st_sargable_param
 
3421
{
 
3422
  Field *field;              /* field against which to check sargability */
 
3423
  Item **arg_value;          /* values of potential keys for lookups     */
 
3424
  uint num_values;           /* number of values in the above array      */
 
3425
} SARGABLE_PARAM;  
 
3426
 
 
3427
/**
 
3428
  Calculate the best possible join and initialize the join structure.
 
3429
 
 
3430
  @retval
 
3431
    0   ok
 
3432
  @retval
 
3433
    1   Fatal error
 
3434
*/
 
3435
 
 
3436
static bool
 
3437
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
 
3438
                     DYNAMIC_ARRAY *keyuse_array)
 
3439
{
 
3440
  int error;
 
3441
  TABLE *table;
 
3442
  uint i,table_count,const_count,key;
 
3443
  table_map found_const_table_map, all_table_map, found_ref, refs;
 
3444
  key_map const_ref, eq_part;
 
3445
  TABLE **table_vector;
 
3446
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
 
3447
  KEYUSE *keyuse,*start_keyuse;
 
3448
  table_map outer_join=0;
 
3449
  SARGABLE_PARAM *sargables= 0;
 
3450
  JOIN_TAB *stat_vector[MAX_TABLES+1];
 
3451
 
 
3452
  table_count=join->tables;
 
3453
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
 
3454
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
 
3455
  table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
 
3456
  if (!stat || !stat_ref || !table_vector)
 
3457
    return(1);                          // Eom /* purecov: inspected */
 
3458
 
 
3459
  join->best_ref=stat_vector;
 
3460
 
 
3461
  stat_end=stat+table_count;
 
3462
  found_const_table_map= all_table_map=0;
 
3463
  const_count=0;
 
3464
 
 
3465
  for (s= stat, i= 0;
 
3466
       tables;
 
3467
       s++, tables= tables->next_leaf, i++)
 
3468
  {
 
3469
    TABLE_LIST *embedding= tables->embedding;
 
3470
    stat_vector[i]=s;
 
3471
    s->keys.init();
 
3472
    s->const_keys.init();
 
3473
    s->checked_keys.init();
 
3474
    s->needed_reg.init();
 
3475
    table_vector[i]=s->table=table=tables->table;
 
3476
    table->pos_in_table_list= tables;
 
3477
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
3478
    if(error)
 
3479
    {
 
3480
        table->file->print_error(error, MYF(0));
 
3481
        return(1);
 
3482
    }
 
3483
    table->quick_keys.clear_all();
 
3484
    table->reginfo.join_tab=s;
 
3485
    table->reginfo.not_exists_optimize=0;
 
3486
    bzero((char*) table->const_key_parts, sizeof(key_part_map)*table->s->keys);
 
3487
    all_table_map|= table->map;
 
3488
    s->join=join;
 
3489
    s->info=0;                                  // For describe
 
3490
 
 
3491
    s->dependent= tables->dep_tables;
 
3492
    s->key_dependent= 0;
 
3493
    if (tables->schema_table)
 
3494
      table->file->stats.records= 2;
 
3495
    table->quick_condition_rows= table->file->stats.records;
 
3496
 
 
3497
    s->on_expr_ref= &tables->on_expr;
 
3498
    if (*s->on_expr_ref)
 
3499
    {
 
3500
      /* s is the only inner table of an outer join */
 
3501
      if (!table->file->stats.records && !embedding)
 
3502
      {                                         // Empty table
 
3503
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
 
3504
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3505
        continue;
 
3506
      }
 
3507
      outer_join|= table->map;
 
3508
      s->embedding_map= 0;
 
3509
      for (;embedding; embedding= embedding->embedding)
 
3510
        s->embedding_map|= embedding->nested_join->nj_map;
 
3511
      continue;
 
3512
    }
 
3513
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
 
3514
    {
 
3515
      /* s belongs to a nested join, maybe to several embedded joins */
 
3516
      s->embedding_map= 0;
 
3517
      do
 
3518
      {
 
3519
        NESTED_JOIN *nested_join= embedding->nested_join;
 
3520
        s->embedding_map|=nested_join->nj_map;
 
3521
        s->dependent|= embedding->dep_tables;
 
3522
        embedding= embedding->embedding;
 
3523
        outer_join|= nested_join->used_tables;
 
3524
      }
 
3525
      while (embedding);
 
3526
      continue;
 
3527
    }
 
3528
    if ((table->s->system || table->file->stats.records <= 1) &&
 
3529
        !s->dependent &&
 
3530
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
 
3531
    {
 
3532
      set_position(join,const_count++,s,(KEYUSE*) 0);
 
3533
    }
 
3534
  }
 
3535
  stat_vector[i]=0;
 
3536
  join->outer_join=outer_join;
 
3537
 
 
3538
  if (join->outer_join)
 
3539
  {
 
3540
    /* 
 
3541
       Build transitive closure for relation 'to be dependent on'.
 
3542
       This will speed up the plan search for many cases with outer joins,
 
3543
       as well as allow us to catch illegal cross references/
 
3544
       Warshall's algorithm is used to build the transitive closure.
 
3545
       As we use bitmaps to represent the relation the complexity
 
3546
       of the algorithm is O((number of tables)^2). 
 
3547
    */
 
3548
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3549
    {
 
3550
      for (uint j= 0 ; j < table_count ; j++)
 
3551
      {
 
3552
        table= stat[j].table;
 
3553
        if (s->dependent & table->map)
 
3554
          s->dependent |= table->reginfo.join_tab->dependent;
 
3555
      }
 
3556
      if (s->dependent)
 
3557
        s->table->maybe_null= 1;
 
3558
    }
 
3559
    /* Catch illegal cross references for outer joins */
 
3560
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3561
    {
 
3562
      if (s->dependent & s->table->map)
 
3563
      {
 
3564
        join->tables=0;                 // Don't use join->table
 
3565
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
 
3566
        return(1);
 
3567
      }
 
3568
      s->key_dependent= s->dependent;
 
3569
    }
 
3570
  }
 
3571
 
 
3572
  if (conds || outer_join)
 
3573
    if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
 
3574
                            conds, join->cond_equal,
 
3575
                            ~outer_join, join->select_lex, &sargables))
 
3576
      return(1);
 
3577
 
 
3578
  /* Read tables with 0 or 1 rows (system tables) */
 
3579
  join->const_table_map= 0;
 
3580
 
 
3581
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
 
3582
       p_pos < p_end ;
 
3583
       p_pos++)
 
3584
  {
 
3585
    int tmp;
 
3586
    s= p_pos->table;
 
3587
    s->type=JT_SYSTEM;
 
3588
    join->const_table_map|=s->table->map;
 
3589
    if ((tmp=join_read_const_table(s, p_pos)))
 
3590
    {
 
3591
      if (tmp > 0)
 
3592
        return(1);                      // Fatal error
 
3593
    }
 
3594
    else
 
3595
      found_const_table_map|= s->table->map;
 
3596
  }
 
3597
 
 
3598
  /* loop until no more const tables are found */
 
3599
  int ref_changed;
 
3600
  do
 
3601
  {
 
3602
  more_const_tables_found:
 
3603
    ref_changed = 0;
 
3604
    found_ref=0;
 
3605
 
 
3606
    /*
 
3607
      We only have to loop from stat_vector + const_count as
 
3608
      set_position() will move all const_tables first in stat_vector
 
3609
    */
 
3610
 
 
3611
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
 
3612
    {
 
3613
      table=s->table;
 
3614
 
 
3615
      /* 
 
3616
        If equi-join condition by a key is null rejecting and after a
 
3617
        substitution of a const table the key value happens to be null
 
3618
        then we can state that there are no matches for this equi-join.
 
3619
      */  
 
3620
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
 
3621
      {
 
3622
        /* 
 
3623
          When performing an outer join operation if there are no matching rows
 
3624
          for the single row of the outer table all the inner tables are to be
 
3625
          null complemented and thus considered as constant tables.
 
3626
          Here we apply this consideration to the case of outer join operations 
 
3627
          with a single inner table only because the case with nested tables
 
3628
          would require a more thorough analysis.
 
3629
          TODO. Apply single row substitution to null complemented inner tables
 
3630
          for nested outer join operations. 
 
3631
        */              
 
3632
        while (keyuse->table == table)
 
3633
        {
 
3634
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
 
3635
              keyuse->val->is_null() && keyuse->null_rejecting)
 
3636
          {
 
3637
            s->type= JT_CONST;
 
3638
            mark_as_null_row(table);
 
3639
            found_const_table_map|= table->map;
 
3640
            join->const_table_map|= table->map;
 
3641
            set_position(join,const_count++,s,(KEYUSE*) 0);
 
3642
            goto more_const_tables_found;
 
3643
           }
 
3644
          keyuse++;
 
3645
        }
 
3646
      }
 
3647
 
 
3648
      if (s->dependent)                         // If dependent on some table
 
3649
      {
 
3650
        // All dep. must be constants
 
3651
        if (s->dependent & ~(found_const_table_map))
 
3652
          continue;
 
3653
        if (table->file->stats.records <= 1L &&
 
3654
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3655
            !table->pos_in_table_list->embedding)
 
3656
        {                                       // system table
 
3657
          int tmp= 0;
 
3658
          s->type=JT_SYSTEM;
 
3659
          join->const_table_map|=table->map;
 
3660
          set_position(join,const_count++,s,(KEYUSE*) 0);
 
3661
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
 
3662
          {
 
3663
            if (tmp > 0)
 
3664
              return(1);                        // Fatal error
 
3665
          }
 
3666
          else
 
3667
            found_const_table_map|= table->map;
 
3668
          continue;
 
3669
        }
 
3670
      }
 
3671
      /* check if table can be read by key or table only uses const refs */
 
3672
      if ((keyuse=s->keyuse))
 
3673
      {
 
3674
        s->type= JT_REF;
 
3675
        while (keyuse->table == table)
 
3676
        {
 
3677
          start_keyuse=keyuse;
 
3678
          key=keyuse->key;
 
3679
          s->keys.set_bit(key);               // QQ: remove this ?
 
3680
 
 
3681
          refs=0;
 
3682
          const_ref.clear_all();
 
3683
          eq_part.clear_all();
 
3684
          do
 
3685
          {
 
3686
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
 
3687
            {
 
3688
              if (!((~found_const_table_map) & keyuse->used_tables))
 
3689
                const_ref.set_bit(keyuse->keypart);
 
3690
              else
 
3691
                refs|=keyuse->used_tables;
 
3692
              eq_part.set_bit(keyuse->keypart);
 
3693
            }
 
3694
            keyuse++;
 
3695
          } while (keyuse->table == table && keyuse->key == key);
 
3696
 
 
3697
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3698
              !table->pos_in_table_list->embedding)
 
3699
          {
 
3700
            if ((table->key_info[key].flags & (HA_NOSAME))
 
3701
                 == HA_NOSAME)
 
3702
            {
 
3703
              if (const_ref == eq_part)
 
3704
              {                                 // Found everything for ref.
 
3705
                int tmp;
 
3706
                ref_changed = 1;
 
3707
                s->type= JT_CONST;
 
3708
                join->const_table_map|=table->map;
 
3709
                set_position(join,const_count++,s,start_keyuse);
 
3710
                if (create_ref_for_key(join, s, start_keyuse,
 
3711
                                       found_const_table_map))
 
3712
                  return(1);
 
3713
                if ((tmp=join_read_const_table(s,
 
3714
                                               join->positions+const_count-1)))
 
3715
                {
 
3716
                  if (tmp > 0)
 
3717
                    return(1);                  // Fatal error
 
3718
                }
 
3719
                else
 
3720
                  found_const_table_map|= table->map;
 
3721
                break;
 
3722
              }
 
3723
              else
 
3724
                found_ref|= refs;      // Table is const if all refs are const
 
3725
            }
 
3726
            else if (const_ref == eq_part)
 
3727
              s->const_keys.set_bit(key);
 
3728
          }
 
3729
        }
 
3730
      }
 
3731
    }
 
3732
  } while (join->const_table_map & found_ref && ref_changed);
 
3733
 
 
3734
  /* 
 
3735
    Update info on indexes that can be used for search lookups as
 
3736
    reading const tables may has added new sargable predicates. 
 
3737
  */
 
3738
  if (const_count && sargables)
 
3739
  {
 
3740
    for( ; sargables->field ; sargables++)
 
3741
    {
 
3742
      Field *field= sargables->field;
 
3743
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
 
3744
      key_map possible_keys= field->key_start;
 
3745
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
3746
      bool is_const= 1;
 
3747
      for (uint j=0; j < sargables->num_values; j++)
 
3748
        is_const&= sargables->arg_value[j]->const_item();
 
3749
      if (is_const)
 
3750
        join_tab[0].const_keys.merge(possible_keys);
 
3751
    }
 
3752
  }
 
3753
 
 
3754
  if (pull_out_semijoin_tables(join))
 
3755
    return(true);
 
3756
 
 
3757
  /* Calc how many (possible) matched records in each table */
 
3758
 
 
3759
  for (s=stat ; s < stat_end ; s++)
 
3760
  {
 
3761
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
 
3762
    {
 
3763
      /* Only one matching row */
 
3764
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
 
3765
      continue;
 
3766
    }
 
3767
    /* Approximate found rows and time to read them */
 
3768
    s->found_records=s->records=s->table->file->stats.records;
 
3769
    s->read_time=(ha_rows) s->table->file->scan_time();
 
3770
 
 
3771
    /*
 
3772
      Set a max range of how many seeks we can expect when using keys
 
3773
      This is can't be to high as otherwise we are likely to use
 
3774
      table scan.
 
3775
    */
 
3776
    s->worst_seeks= min((double) s->found_records / 10,
 
3777
                        (double) s->read_time*3);
 
3778
    if (s->worst_seeks < 2.0)                   // Fix for small tables
 
3779
      s->worst_seeks=2.0;
 
3780
 
 
3781
    /*
 
3782
      Add to stat->const_keys those indexes for which all group fields or
 
3783
      all select distinct fields participate in one index.
 
3784
    */
 
3785
    add_group_and_distinct_keys(join, s);
 
3786
 
 
3787
    if (!s->const_keys.is_clear_all() &&
 
3788
        !s->table->pos_in_table_list->embedding)
 
3789
    {
 
3790
      ha_rows records;
 
3791
      SQL_SELECT *select;
 
3792
      select= make_select(s->table, found_const_table_map,
 
3793
                          found_const_table_map,
 
3794
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
 
3795
                          1, &error);
 
3796
      if (!select)
 
3797
        return(1);
 
3798
      records= get_quick_record_count(join->thd, select, s->table,
 
3799
                                      &s->const_keys, join->row_limit);
 
3800
      s->quick=select->quick;
 
3801
      s->needed_reg=select->needed_reg;
 
3802
      select->quick=0;
 
3803
      if (records == 0 && s->table->reginfo.impossible_range)
 
3804
      {
 
3805
        /*
 
3806
          Impossible WHERE or ON expression
 
3807
          In case of ON, we mark that the we match one empty NULL row.
 
3808
          In case of WHERE, don't set found_const_table_map to get the
 
3809
          caller to abort with a zero row result.
 
3810
        */
 
3811
        join->const_table_map|= s->table->map;
 
3812
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3813
        s->type= JT_CONST;
 
3814
        if (*s->on_expr_ref)
 
3815
        {
 
3816
          /* Generate empty row */
 
3817
          s->info= "Impossible ON condition";
 
3818
          found_const_table_map|= s->table->map;
 
3819
          s->type= JT_CONST;
 
3820
          mark_as_null_row(s->table);           // All fields are NULL
 
3821
        }
 
3822
      }
 
3823
      if (records != HA_POS_ERROR)
 
3824
      {
 
3825
        s->found_records=records;
 
3826
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
3827
      }
 
3828
      delete select;
 
3829
    }
 
3830
  }
 
3831
 
 
3832
  join->join_tab=stat;
 
3833
  join->map2table=stat_ref;
 
3834
  join->table= join->all_tables=table_vector;
 
3835
  join->const_tables=const_count;
 
3836
  join->found_const_table_map=found_const_table_map;
 
3837
 
 
3838
  /* Find an optimal join order of the non-constant tables. */
 
3839
  if (join->const_tables != join->tables)
 
3840
  {
 
3841
    optimize_keyuse(join, keyuse_array);
 
3842
    if (choose_plan(join, all_table_map & ~join->const_table_map))
 
3843
      return(true);
 
3844
  }
 
3845
  else
 
3846
  {
 
3847
    memcpy((uchar*) join->best_positions,(uchar*) join->positions,
 
3848
           sizeof(POSITION)*join->const_tables);
 
3849
    join->best_read=1.0;
 
3850
  }
 
3851
  /* Generate an execution plan from the found optimal join order. */
 
3852
  return(join->thd->killed || get_best_combination(join));
 
3853
}
 
3854
 
 
3855
 
485
3856
/*****************************************************************************
486
3857
  Check with keys are used and with tables references with tables
487
3858
  Updates in stat:
490
3861
          keyuse     Pointer to possible keys
491
3862
*****************************************************************************/
492
3863
 
 
3864
/// Used when finding key fields
 
3865
typedef struct key_field_t {
 
3866
  Field         *field;
 
3867
  Item          *val;                   ///< May be empty if diff constant
 
3868
  uint          level;
 
3869
  uint          optimize; // KEY_OPTIMIZE_*
 
3870
  bool          eq_func;
 
3871
  /**
 
3872
    If true, the condition this struct represents will not be satisfied
 
3873
    when val IS NULL.
 
3874
  */
 
3875
  bool          null_rejecting; 
 
3876
  bool          *cond_guard; /* See KEYUSE::cond_guard */
 
3877
  uint          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3878
} KEY_FIELD;
 
3879
 
 
3880
/**
 
3881
  Merge new key definitions to old ones, remove those not used in both.
 
3882
 
 
3883
  This is called for OR between different levels.
 
3884
 
 
3885
  To be able to do 'ref_or_null' we merge a comparison of a column
 
3886
  and 'column IS NULL' to one test.  This is useful for sub select queries
 
3887
  that are internally transformed to something like:.
 
3888
 
 
3889
  @code
 
3890
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL 
 
3891
  @endcode
 
3892
 
 
3893
  KEY_FIELD::null_rejecting is processed as follows: @n
 
3894
  result has null_rejecting=true if it is set for both ORed references.
 
3895
  for example:
 
3896
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
 
3897
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
 
3898
 
 
3899
  @todo
 
3900
    The result of this is that we're missing some 'ref' accesses.
 
3901
    OptimizerTeam: Fix this
 
3902
*/
 
3903
 
 
3904
static KEY_FIELD *
 
3905
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
 
3906
                 uint and_level)
 
3907
{
 
3908
  if (start == new_fields)
 
3909
    return start;                               // Impossible or
 
3910
  if (new_fields == end)
 
3911
    return start;                               // No new fields, skip all
 
3912
 
 
3913
  KEY_FIELD *first_free=new_fields;
 
3914
 
 
3915
  /* Mark all found fields in old array */
 
3916
  for (; new_fields != end ; new_fields++)
 
3917
  {
 
3918
    for (KEY_FIELD *old=start ; old != first_free ; old++)
 
3919
    {
 
3920
      if (old->field == new_fields->field)
 
3921
      {
 
3922
        /*
 
3923
          NOTE: below const_item() call really works as "!used_tables()", i.e.
 
3924
          it can return false where it is feasible to make it return true.
 
3925
          
 
3926
          The cause is as follows: Some of the tables are already known to be
 
3927
          const tables (the detection code is in make_join_statistics(),
 
3928
          above the update_ref_and_keys() call), but we didn't propagate 
 
3929
          information about this: TABLE::const_table is not set to true, and
 
3930
          Item::update_used_tables() hasn't been called for each item.
 
3931
          The result of this is that we're missing some 'ref' accesses.
 
3932
          TODO: OptimizerTeam: Fix this
 
3933
        */
 
3934
        if (!new_fields->val->const_item())
 
3935
        {
 
3936
          /*
 
3937
            If the value matches, we can use the key reference.
 
3938
            If not, we keep it until we have examined all new values
 
3939
          */
 
3940
          if (old->val->eq(new_fields->val, old->field->binary()))
 
3941
          {
 
3942
            old->level= and_level;
 
3943
            old->optimize= ((old->optimize & new_fields->optimize &
 
3944
                             KEY_OPTIMIZE_EXISTS) |
 
3945
                            ((old->optimize | new_fields->optimize) &
 
3946
                             KEY_OPTIMIZE_REF_OR_NULL));
 
3947
            old->null_rejecting= (old->null_rejecting &&
 
3948
                                  new_fields->null_rejecting);
 
3949
          }
 
3950
        }
 
3951
        else if (old->eq_func && new_fields->eq_func &&
 
3952
                 old->val->eq_by_collation(new_fields->val, 
 
3953
                                           old->field->binary(),
 
3954
                                           old->field->charset()))
 
3955
 
 
3956
        {
 
3957
          old->level= and_level;
 
3958
          old->optimize= ((old->optimize & new_fields->optimize &
 
3959
                           KEY_OPTIMIZE_EXISTS) |
 
3960
                          ((old->optimize | new_fields->optimize) &
 
3961
                           KEY_OPTIMIZE_REF_OR_NULL));
 
3962
          old->null_rejecting= (old->null_rejecting &&
 
3963
                                new_fields->null_rejecting);
 
3964
        }
 
3965
        else if (old->eq_func && new_fields->eq_func &&
 
3966
                 ((old->val->const_item() && old->val->is_null()) || 
 
3967
                  new_fields->val->is_null()))
 
3968
        {
 
3969
          /* field = expression OR field IS NULL */
 
3970
          old->level= and_level;
 
3971
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
 
3972
          /*
 
3973
            Remember the NOT NULL value unless the value does not depend
 
3974
            on other tables.
 
3975
          */
 
3976
          if (!old->val->used_tables() && old->val->is_null())
 
3977
            old->val= new_fields->val;
 
3978
          /* The referred expression can be NULL: */ 
 
3979
          old->null_rejecting= 0;
 
3980
        }
 
3981
        else
 
3982
        {
 
3983
          /*
 
3984
            We are comparing two different const.  In this case we can't
 
3985
            use a key-lookup on this so it's better to remove the value
 
3986
            and let the range optimzier handle it
 
3987
          */
 
3988
          if (old == --first_free)              // If last item
 
3989
            break;
 
3990
          *old= *first_free;                    // Remove old value
 
3991
          old--;                                // Retry this value
 
3992
        }
 
3993
      }
 
3994
    }
 
3995
  }
 
3996
  /* Remove all not used items */
 
3997
  for (KEY_FIELD *old=start ; old != first_free ;)
 
3998
  {
 
3999
    if (old->level != and_level)
 
4000
    {                                           // Not used in all levels
 
4001
      if (old == --first_free)
 
4002
        break;
 
4003
      *old= *first_free;                        // Remove old value
 
4004
      continue;
 
4005
    }
 
4006
    old++;
 
4007
  }
 
4008
  return first_free;
 
4009
}
 
4010
 
 
4011
 
 
4012
/**
 
4013
  Add a possible key to array of possible keys if it's usable as a key
 
4014
 
 
4015
    @param key_fields      Pointer to add key, if usable
 
4016
    @param and_level       And level, to be stored in KEY_FIELD
 
4017
    @param cond            Condition predicate
 
4018
    @param field           Field used in comparision
 
4019
    @param eq_func         True if we used =, <=> or IS NULL
 
4020
    @param value           Value used for comparison with field
 
4021
    @param usable_tables   Tables which can be used for key optimization
 
4022
    @param sargables       IN/OUT Array of found sargable candidates
 
4023
 
 
4024
  @note
 
4025
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
 
4026
    table, we store this to be able to do not exists optimization later.
 
4027
 
 
4028
  @returns
 
4029
    *key_fields is incremented if we stored a key in the array
 
4030
*/
 
4031
 
 
4032
static void
 
4033
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
 
4034
              Field *field, bool eq_func, Item **value, uint num_values,
 
4035
              table_map usable_tables, SARGABLE_PARAM **sargables)
 
4036
{
 
4037
  uint exists_optimize= 0;
 
4038
  if (!(field->flags & PART_KEY_FLAG))
 
4039
  {
 
4040
    // Don't remove column IS NULL on a LEFT JOIN table
 
4041
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4042
        !field->table->maybe_null || field->null_ptr)
 
4043
      return;                                   // Not a key. Skip it
 
4044
    exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4045
    assert(num_values == 1);
 
4046
  }
 
4047
  else
 
4048
  {
 
4049
    table_map used_tables=0;
 
4050
    bool optimizable=0;
 
4051
    for (uint i=0; i<num_values; i++)
 
4052
    {
 
4053
      used_tables|=(value[i])->used_tables();
 
4054
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
 
4055
        optimizable=1;
 
4056
    }
 
4057
    if (!optimizable)
 
4058
      return;
 
4059
    if (!(usable_tables & field->table->map))
 
4060
    {
 
4061
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4062
          !field->table->maybe_null || field->null_ptr)
 
4063
        return;                                 // Can't use left join optimize
 
4064
      exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4065
    }
 
4066
    else
 
4067
    {
 
4068
      JOIN_TAB *stat=field->table->reginfo.join_tab;
 
4069
      key_map possible_keys=field->key_start;
 
4070
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
4071
      stat[0].keys.merge(possible_keys);             // Add possible keys
 
4072
 
 
4073
      /*
 
4074
        Save the following cases:
 
4075
        Field op constant
 
4076
        Field LIKE constant where constant doesn't start with a wildcard
 
4077
        Field = field2 where field2 is in a different table
 
4078
        Field op formula
 
4079
        Field IS NULL
 
4080
        Field IS NOT NULL
 
4081
         Field BETWEEN ...
 
4082
         Field IN ...
 
4083
      */
 
4084
      stat[0].key_dependent|=used_tables;
 
4085
 
 
4086
      bool is_const=1;
 
4087
      for (uint i=0; i<num_values; i++)
 
4088
      {
 
4089
        if (!(is_const&= value[i]->const_item()))
 
4090
          break;
 
4091
      }
 
4092
      if (is_const)
 
4093
        stat[0].const_keys.merge(possible_keys);
 
4094
      else if (!eq_func)
 
4095
      {
 
4096
        /* 
 
4097
          Save info to be able check whether this predicate can be 
 
4098
          considered as sargable for range analisis after reading const tables.
 
4099
          We do not save info about equalities as update_const_equal_items
 
4100
          will take care of updating info on keys from sargable equalities. 
 
4101
        */
 
4102
        (*sargables)--;
 
4103
        (*sargables)->field= field;
 
4104
        (*sargables)->arg_value= value;
 
4105
        (*sargables)->num_values= num_values;
 
4106
      }
 
4107
      /*
 
4108
        We can't always use indexes when comparing a string index to a
 
4109
        number. cmp_type() is checked to allow compare of dates to numbers.
 
4110
        eq_func is NEVER true when num_values > 1
 
4111
       */
 
4112
      if (!eq_func)
 
4113
      {
 
4114
        /* 
 
4115
          Additional optimization: if we're processing
 
4116
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
 
4117
          "t.key = c1".
 
4118
          TODO: This is a very limited fix. A more generic fix is possible. 
 
4119
          There are 2 options:
 
4120
          A) Make equality propagation code be able to handle BETWEEN
 
4121
             (including cases like t1.key BETWEEN t2.key AND t3.key)
 
4122
          B) Make range optimizer to infer additional "t.key = c" equalities
 
4123
             and use them in equality propagation process (see details in
 
4124
             OptimizerKBAndTodo)
 
4125
        */
 
4126
        if ((cond->functype() != Item_func::BETWEEN) ||
 
4127
            ((Item_func_between*) cond)->negated ||
 
4128
            !value[0]->eq(value[1], field->binary()))
 
4129
          return;
 
4130
        eq_func= true;
 
4131
      }
 
4132
 
 
4133
      if (field->result_type() == STRING_RESULT)
 
4134
      {
 
4135
        if ((*value)->result_type() != STRING_RESULT)
 
4136
        {
 
4137
          if (field->cmp_type() != (*value)->result_type())
 
4138
            return;
 
4139
        }
 
4140
        else
 
4141
        {
 
4142
          /*
 
4143
            We can't use indexes if the effective collation
 
4144
            of the operation differ from the field collation.
 
4145
          */
 
4146
          if (field->cmp_type() == STRING_RESULT &&
 
4147
              ((Field_str*)field)->charset() != cond->compare_collation())
 
4148
            return;
 
4149
        }
 
4150
      }
 
4151
    }
 
4152
  }
 
4153
  /*
 
4154
    For the moment eq_func is always true. This slot is reserved for future
 
4155
    extensions where we want to remembers other things than just eq comparisons
 
4156
  */
 
4157
  assert(eq_func);
 
4158
  /* Store possible eq field */
 
4159
  (*key_fields)->field=         field;
 
4160
  (*key_fields)->eq_func=       eq_func;
 
4161
  (*key_fields)->val=           *value;
 
4162
  (*key_fields)->level=         and_level;
 
4163
  (*key_fields)->optimize=      exists_optimize;
 
4164
  /*
 
4165
    If the condition has form "tbl.keypart = othertbl.field" and 
 
4166
    othertbl.field can be NULL, there will be no matches if othertbl.field 
 
4167
    has NULL value.
 
4168
    We use null_rejecting in add_not_null_conds() to add
 
4169
    'othertbl.field IS NOT NULL' to tab->select_cond.
 
4170
  */
 
4171
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
 
4172
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
 
4173
                                  ((*value)->type() == Item::FIELD_ITEM) &&
 
4174
                                  ((Item_field*)*value)->field->maybe_null());
 
4175
  (*key_fields)->cond_guard= NULL;
 
4176
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name && 
 
4177
                              cond->name < subq_sj_cond_name + 64)? 
 
4178
                              cond->name - subq_sj_cond_name: UINT_MAX;
 
4179
  (*key_fields)++;
 
4180
}
 
4181
 
 
4182
/**
 
4183
  Add possible keys to array of possible keys originated from a simple
 
4184
  predicate.
 
4185
 
 
4186
    @param  key_fields     Pointer to add key, if usable
 
4187
    @param  and_level      And level, to be stored in KEY_FIELD
 
4188
    @param  cond           Condition predicate
 
4189
    @param  field          Field used in comparision
 
4190
    @param  eq_func        True if we used =, <=> or IS NULL
 
4191
    @param  value          Value used for comparison with field
 
4192
                           Is NULL for BETWEEN and IN    
 
4193
    @param  usable_tables  Tables which can be used for key optimization
 
4194
    @param  sargables      IN/OUT Array of found sargable candidates
 
4195
 
 
4196
  @note
 
4197
    If field items f1 and f2 belong to the same multiple equality and
 
4198
    a key is added for f1, the the same key is added for f2.
 
4199
 
 
4200
  @returns
 
4201
    *key_fields is incremented if we stored a key in the array
 
4202
*/
 
4203
 
 
4204
static void
 
4205
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
 
4206
                     Item_func *cond, Item_field *field_item,
 
4207
                     bool eq_func, Item **val,
 
4208
                     uint num_values, table_map usable_tables,
 
4209
                     SARGABLE_PARAM **sargables)
 
4210
{
 
4211
  Field *field= field_item->field;
 
4212
  add_key_field(key_fields, and_level, cond, field,
 
4213
                eq_func, val, num_values, usable_tables, sargables);
 
4214
  Item_equal *item_equal= field_item->item_equal;
 
4215
  if (item_equal)
 
4216
  { 
 
4217
    /*
 
4218
      Add to the set of possible key values every substitution of
 
4219
      the field for an equal field included into item_equal
 
4220
    */
 
4221
    Item_equal_iterator it(*item_equal);
 
4222
    Item_field *item;
 
4223
    while ((item= it++))
 
4224
    {
 
4225
      if (!field->eq(item->field))
 
4226
      {
 
4227
        add_key_field(key_fields, and_level, cond, item->field,
 
4228
                      eq_func, val, num_values, usable_tables,
 
4229
                      sargables);
 
4230
      }
 
4231
    }
 
4232
  }
 
4233
}
 
4234
 
 
4235
static void
 
4236
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
 
4237
               COND *cond, table_map usable_tables,
 
4238
               SARGABLE_PARAM **sargables)
 
4239
{
 
4240
  if (cond->type() == Item_func::COND_ITEM)
 
4241
  {
 
4242
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
 
4243
    KEY_FIELD *org_key_fields= *key_fields;
 
4244
 
 
4245
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
4246
    {
 
4247
      Item *item;
 
4248
      while ((item=li++))
 
4249
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4250
                       sargables);
 
4251
      for (; org_key_fields != *key_fields ; org_key_fields++)
 
4252
        org_key_fields->level= *and_level;
 
4253
    }
 
4254
    else
 
4255
    {
 
4256
      (*and_level)++;
 
4257
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
 
4258
                     sargables);
 
4259
      Item *item;
 
4260
      while ((item=li++))
 
4261
      {
 
4262
        KEY_FIELD *start_key_fields= *key_fields;
 
4263
        (*and_level)++;
 
4264
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4265
                       sargables);
 
4266
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
 
4267
                                     *key_fields,++(*and_level));
 
4268
      }
 
4269
    }
 
4270
    return;
 
4271
  }
 
4272
 
 
4273
  /* 
 
4274
    Subquery optimization: Conditions that are pushed down into subqueries
 
4275
    are wrapped into Item_func_trig_cond. We process the wrapped condition
 
4276
    but need to set cond_guard for KEYUSE elements generated from it.
 
4277
  */
 
4278
  {
 
4279
    if (cond->type() == Item::FUNC_ITEM &&
 
4280
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
 
4281
    {
 
4282
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
 
4283
      if (!join->group_list && !join->order &&
 
4284
          join->unit->item && 
 
4285
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
 
4286
          !join->unit->is_union())
 
4287
      {
 
4288
        KEY_FIELD *save= *key_fields;
 
4289
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
 
4290
                       sargables);
 
4291
        // Indicate that this ref access candidate is for subquery lookup:
 
4292
        for (; save != *key_fields; save++)
 
4293
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
 
4294
      }
 
4295
      return;
 
4296
    }
 
4297
  }
 
4298
 
 
4299
  /* If item is of type 'field op field/constant' add it to key_fields */
 
4300
  if (cond->type() != Item::FUNC_ITEM)
 
4301
    return;
 
4302
  Item_func *cond_func= (Item_func*) cond;
 
4303
  switch (cond_func->select_optimize()) {
 
4304
  case Item_func::OPTIMIZE_NONE:
 
4305
    break;
 
4306
  case Item_func::OPTIMIZE_KEY:
 
4307
  {
 
4308
    Item **values;
 
4309
    // BETWEEN, IN, NE
 
4310
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
 
4311
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4312
    {
 
4313
      values= cond_func->arguments()+1;
 
4314
      if (cond_func->functype() == Item_func::NE_FUNC &&
 
4315
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4316
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4317
        values--;
 
4318
      assert(cond_func->functype() != Item_func::IN_FUNC ||
 
4319
                  cond_func->argument_count() != 2);
 
4320
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4321
                           (Item_field*) (cond_func->key_item()->real_item()),
 
4322
                           0, values, 
 
4323
                           cond_func->argument_count()-1,
 
4324
                           usable_tables, sargables);
 
4325
    }
 
4326
    if (cond_func->functype() == Item_func::BETWEEN)
 
4327
    {
 
4328
      values= cond_func->arguments();
 
4329
      for (uint i= 1 ; i < cond_func->argument_count() ; i++)
 
4330
      {
 
4331
        Item_field *field_item;
 
4332
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
 
4333
            &&
 
4334
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
 
4335
        {
 
4336
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
 
4337
          add_key_equal_fields(key_fields, *and_level, cond_func,
 
4338
                               field_item, 0, values, 1, usable_tables, 
 
4339
                               sargables);
 
4340
        }
 
4341
      }  
 
4342
    }
 
4343
    break;
 
4344
  }
 
4345
  case Item_func::OPTIMIZE_OP:
 
4346
  {
 
4347
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
 
4348
                     cond_func->functype() == Item_func::EQUAL_FUNC);
 
4349
 
 
4350
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4351
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4352
    {
 
4353
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4354
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4355
                           equal_func,
 
4356
                           cond_func->arguments()+1, 1, usable_tables,
 
4357
                           sargables);
 
4358
    }
 
4359
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4360
        cond_func->functype() != Item_func::LIKE_FUNC &&
 
4361
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
 
4362
    {
 
4363
      add_key_equal_fields(key_fields, *and_level, cond_func, 
 
4364
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
 
4365
                           equal_func,
 
4366
                           cond_func->arguments(),1,usable_tables,
 
4367
                           sargables);
 
4368
    }
 
4369
    break;
 
4370
  }
 
4371
  case Item_func::OPTIMIZE_NULL:
 
4372
    /* column_name IS [NOT] NULL */
 
4373
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4374
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4375
    {
 
4376
      Item *tmp=new Item_null;
 
4377
      if (unlikely(!tmp))                       // Should never be true
 
4378
        return;
 
4379
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4380
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4381
                    cond_func->functype() == Item_func::ISNULL_FUNC,
 
4382
                           &tmp, 1, usable_tables, sargables);
 
4383
    }
 
4384
    break;
 
4385
  case Item_func::OPTIMIZE_EQUAL:
 
4386
    Item_equal *item_equal= (Item_equal *) cond;
 
4387
    Item *const_item= item_equal->get_const();
 
4388
    Item_equal_iterator it(*item_equal);
 
4389
    Item_field *item;
 
4390
    if (const_item)
 
4391
    {
 
4392
      /*
 
4393
        For each field field1 from item_equal consider the equality 
 
4394
        field1=const_item as a condition allowing an index access of the table
 
4395
        with field1 by the keys value of field1.
 
4396
      */   
 
4397
      while ((item= it++))
 
4398
      {
 
4399
        add_key_field(key_fields, *and_level, cond_func, item->field,
 
4400
                      true, &const_item, 1, usable_tables, sargables);
 
4401
      }
 
4402
    }
 
4403
    else 
 
4404
    {
 
4405
      /*
 
4406
        Consider all pairs of different fields included into item_equal.
 
4407
        For each of them (field1, field1) consider the equality 
 
4408
        field1=field2 as a condition allowing an index access of the table
 
4409
        with field1 by the keys value of field2.
 
4410
      */   
 
4411
      Item_equal_iterator fi(*item_equal);
 
4412
      while ((item= fi++))
 
4413
      {
 
4414
        Field *field= item->field;
 
4415
        while ((item= it++))
 
4416
        {
 
4417
          if (!field->eq(item->field))
 
4418
          {
 
4419
            add_key_field(key_fields, *and_level, cond_func, field,
 
4420
                          true, (Item **) &item, 1, usable_tables,
 
4421
                          sargables);
 
4422
          }
 
4423
        }
 
4424
        it.rewind();
 
4425
      }
 
4426
    }
 
4427
    break;
 
4428
  }
 
4429
}
493
4430
 
494
4431
/**
495
4432
  Add all keys with uses 'field' for some keypart.
496
4433
 
497
4434
  If field->and_level != and_level then only mark key_part as const_part.
498
4435
*/
499
 
uint32_t max_part_bit(key_part_map bits)
 
4436
 
 
4437
static uint
 
4438
max_part_bit(key_part_map bits)
500
4439
{
501
 
  uint32_t found;
 
4440
  uint found;
502
4441
  for (found=0; bits & 1 ; found++,bits>>=1) ;
503
4442
  return found;
504
4443
}
505
4444
 
506
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
 
4445
static void
 
4446
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
 
4447
{
 
4448
  Field *field=key_field->field;
 
4449
  TABLE *form= field->table;
 
4450
  KEYUSE keyuse;
 
4451
 
 
4452
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
 
4453
  {
 
4454
    for (uint key=0 ; key < form->s->keys ; key++)
 
4455
    {
 
4456
      if (!(form->keys_in_use_for_query.is_set(key)))
 
4457
        continue;
 
4458
 
 
4459
      uint key_parts= (uint) form->key_info[key].key_parts;
 
4460
      for (uint part=0 ; part <  key_parts ; part++)
 
4461
      {
 
4462
        if (field->eq(form->key_info[key].key_part[part].field))
 
4463
        {
 
4464
          keyuse.table= field->table;
 
4465
          keyuse.val =  key_field->val;
 
4466
          keyuse.key =  key;
 
4467
          keyuse.keypart=part;
 
4468
          keyuse.keypart_map= (key_part_map) 1 << part;
 
4469
          keyuse.used_tables=key_field->val->used_tables();
 
4470
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
 
4471
          keyuse.null_rejecting= key_field->null_rejecting;
 
4472
          keyuse.cond_guard= key_field->cond_guard;
 
4473
          keyuse.sj_pred_no= key_field->sj_pred_no;
 
4474
          VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
 
4475
        }
 
4476
      }
 
4477
    }
 
4478
  }
 
4479
}
 
4480
 
 
4481
static int
 
4482
sort_keyuse(KEYUSE *a,KEYUSE *b)
507
4483
{
508
4484
  int res;
509
 
  if (a->getTable()->tablenr != b->getTable()->tablenr)
510
 
    return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
511
 
  if (a->getKey() != b->getKey())
512
 
    return static_cast<int>((a->getKey() - b->getKey()));
513
 
  if (a->getKeypart() != b->getKeypart())
514
 
    return static_cast<int>((a->getKeypart() - b->getKeypart()));
 
4485
  if (a->table->tablenr != b->table->tablenr)
 
4486
    return (int) (a->table->tablenr - b->table->tablenr);
 
4487
  if (a->key != b->key)
 
4488
    return (int) (a->key - b->key);
 
4489
  if (a->keypart != b->keypart)
 
4490
    return (int) (a->keypart - b->keypart);
515
4491
  // Place const values before other ones
516
 
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
517
 
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
 
4492
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
 
4493
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
518
4494
    return res;
519
4495
  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
520
 
  return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
521
 
                          (b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
 
4496
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
 
4497
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
 
4498
}
 
4499
 
 
4500
 
 
4501
/*
 
4502
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
 
4503
 
 
4504
    This function populates KEY_FIELD array with entries generated from the 
 
4505
    ON condition of the given nested join, and does the same for nested joins 
 
4506
    contained within this nested join.
 
4507
 
 
4508
  @param[in]      nested_join_table   Nested join pseudo-table to process
 
4509
  @param[in,out]  end                 End of the key field array
 
4510
  @param[in,out]  and_level           And-level
 
4511
  @param[in,out]  sargables           Array of found sargable candidates
 
4512
 
 
4513
 
 
4514
  @note
 
4515
    We can add accesses to the tables that are direct children of this nested 
 
4516
    join (1), and are not inner tables w.r.t their neighbours (2).
 
4517
    
 
4518
    Example for #1 (outer brackets pair denotes nested join this function is 
 
4519
    invoked for):
 
4520
    @code
 
4521
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
 
4522
    @endcode
 
4523
    Example for #2:
 
4524
    @code
 
4525
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
 
4526
    @endcode
 
4527
    In examples 1-2 for condition cond, we can add 'ref' access candidates to 
 
4528
    t1 only.
 
4529
    Example #3:
 
4530
    @code
 
4531
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
 
4532
    @endcode
 
4533
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
 
4534
*/
 
4535
 
 
4536
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
 
4537
                                  KEY_FIELD **end, uint *and_level,
 
4538
                                  SARGABLE_PARAM **sargables)
 
4539
{
 
4540
  List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
 
4541
  List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
 
4542
  bool have_another = false;
 
4543
  table_map tables= 0;
 
4544
  TABLE_LIST *table;
 
4545
  assert(nested_join_table->nested_join);
 
4546
 
 
4547
  while ((table= li++) || (have_another && (li=li2, have_another=false,
 
4548
                                            (table= li++))))
 
4549
  {
 
4550
    if (table->nested_join)
 
4551
    {
 
4552
      if (!table->on_expr)
 
4553
      {
 
4554
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
 
4555
        have_another= true;
 
4556
        li2= li;
 
4557
        li= List_iterator<TABLE_LIST>(table->nested_join->join_list); 
 
4558
      }
 
4559
      else
 
4560
        add_key_fields_for_nj(join, table, end, and_level, sargables);
 
4561
    }
 
4562
    else
 
4563
      if (!table->on_expr)
 
4564
        tables |= table->table->map;
 
4565
  }
 
4566
  if (nested_join_table->on_expr)
 
4567
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
 
4568
                   sargables);
522
4569
}
523
4570
 
524
4571
 
525
4572
/**
526
4573
  Update keyuse array with all possible keys we can use to fetch rows.
527
 
 
528
 
  @param       session
529
 
  @param[out]  keyuse         Put here ordered array of KeyUse structures
 
4574
  
 
4575
  @param       thd 
 
4576
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
530
4577
  @param       join_tab       Array in tablenr_order
531
4578
  @param       tables         Number of tables in join
532
4579
  @param       cond           WHERE condition (note that the function analyzes
535
4582
                              for which we can make ref access based the WHERE
536
4583
                              clause)
537
4584
  @param       select_lex     current SELECT
538
 
  @param[out]  sargables      std::vector of found sargable candidates
539
 
 
 
4585
  @param[out]  sargables      Array of found sargable candidates
 
4586
      
540
4587
   @retval
541
4588
     0  OK
542
4589
   @retval
543
4590
     1  Out of memory.
544
4591
*/
545
 
bool update_ref_and_keys(Session *session,
546
 
                         DYNAMIC_ARRAY *keyuse,
547
 
                         JoinTable *join_tab,
548
 
                         uint32_t tables,
549
 
                         COND *cond, 
550
 
                         COND_EQUAL *,
551
 
                         table_map normal_tables,
552
 
                         Select_Lex *select_lex,
553
 
                         vector<optimizer::SargableParam> &sargables)
 
4592
 
 
4593
static bool
 
4594
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
 
4595
                    uint tables, COND *cond,
 
4596
                    COND_EQUAL *cond_equal __attribute__((__unused__)),
 
4597
                    table_map normal_tables, SELECT_LEX *select_lex,
 
4598
                    SARGABLE_PARAM **sargables)
554
4599
{
555
 
  uint  and_level,found_eq_constant;
556
 
  optimizer::KeyField *key_fields, *end, *field;
557
 
  uint32_t sz;
558
 
  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
559
 
 
560
 
  /*
561
 
    All predicates that are used to fill arrays of KeyField
562
 
    and SargableParam classes have at most 2 arguments
563
 
    except BETWEEN predicates that have 3 arguments and
 
4600
  uint  and_level,i,found_eq_constant;
 
4601
  KEY_FIELD *key_fields, *end, *field;
 
4602
  uint sz;
 
4603
  uint m= max(select_lex->max_equal_elems,1);
 
4604
  
 
4605
  /* 
 
4606
    We use the same piece of memory to store both  KEY_FIELD 
 
4607
    and SARGABLE_PARAM structure.
 
4608
    KEY_FIELD values are placed at the beginning this memory
 
4609
    while  SARGABLE_PARAM values are put at the end.
 
4610
    All predicates that are used to fill arrays of KEY_FIELD
 
4611
    and SARGABLE_PARAM structures have at most 2 arguments
 
4612
    except BETWEEN predicates that have 3 arguments and 
564
4613
    IN predicates.
565
 
    This any predicate if it's not BETWEEN/IN can be used
566
 
    directly to fill at most 2 array elements, either of KeyField 
567
 
    or SargableParam type. For a BETWEEN predicate 3 elements
 
4614
    This any predicate if it's not BETWEEN/IN can be used 
 
4615
    directly to fill at most 2 array elements, either of KEY_FIELD
 
4616
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
568
4617
    can be filled as this predicate is considered as
569
4618
    saragable with respect to each of its argument.
570
4619
    An IN predicate can require at most 1 element as currently
571
4620
    it is considered as sargable only for its first argument.
572
4621
    Multiple equality can add  elements that are filled after
573
4622
    substitution of field arguments by equal fields. There
574
 
    can be not more than select_lex->max_equal_elems such
 
4623
    can be not more than select_lex->max_equal_elems such 
575
4624
    substitutions.
576
 
  */
577
 
  sz= sizeof(optimizer::KeyField) *
578
 
      (((session->getLex()->current_select->cond_count+1)*2 +
579
 
        session->getLex()->current_select->between_count)*m+1);
580
 
  if (! (key_fields= (optimizer::KeyField*) session->getMemRoot()->allocate(sz)))
581
 
    return true;
 
4625
  */ 
 
4626
  sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4627
      (((thd->lex->current_select->cond_count+1)*2 +
 
4628
        thd->lex->current_select->between_count)*m+1);
 
4629
  if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
 
4630
    return true; /* purecov: inspected */
582
4631
  and_level= 0;
583
4632
  field= end= key_fields;
 
4633
  *sargables= (SARGABLE_PARAM *) key_fields + 
 
4634
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
 
4635
  /* set a barrier for the array of SARGABLE_PARAM */
 
4636
  (*sargables)[0].field= 0; 
584
4637
 
585
 
  if (my_init_dynamic_array(keyuse, sizeof(optimizer::KeyUse), 20, 64))
 
4638
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
586
4639
    return true;
587
4640
  if (cond)
588
4641
  {
589
4642
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
590
4643
                   sargables);
591
 
    for (; field != end; field++)
 
4644
    for (; field != end ; field++)
592
4645
    {
593
 
      add_key_part(keyuse, field);
 
4646
      add_key_part(keyuse,field);
594
4647
      /* Mark that we can optimize LEFT JOIN */
595
 
      if (field->getValue()->type() == Item::NULL_ITEM &&
596
 
          ! field->getField()->real_maybe_null())
597
 
      {
598
 
        field->getField()->getTable()->reginfo.not_exists_optimize= 1;
599
 
      }
 
4648
      if (field->val->type() == Item::NULL_ITEM &&
 
4649
          !field->field->real_maybe_null())
 
4650
        field->field->table->reginfo.not_exists_optimize=1;
600
4651
    }
601
4652
  }
602
 
  for (uint32_t i= 0; i < tables; i++)
 
4653
  for (i=0 ; i < tables ; i++)
603
4654
  {
604
4655
    /*
605
4656
      Block the creation of keys for inner tables of outer joins.
609
4660
      In the future when we introduce conditional accesses
610
4661
      for inner tables in outer joins these keys will be taken
611
4662
      into account as well.
612
 
    */
 
4663
    */ 
613
4664
    if (*join_tab[i].on_expr_ref)
614
 
      add_key_fields(join_tab->join, &end, &and_level,
 
4665
      add_key_fields(join_tab->join, &end, &and_level, 
615
4666
                     *join_tab[i].on_expr_ref,
616
4667
                     join_tab[i].table->map, sargables);
617
4668
  }
618
4669
 
619
4670
  /* Process ON conditions for the nested joins */
620
4671
  {
621
 
    List<TableList>::iterator li(join_tab->join->join_list->begin());
622
 
    TableList *table;
 
4672
    List_iterator<TABLE_LIST> li(*join_tab->join->join_list);
 
4673
    TABLE_LIST *table;
623
4674
    while ((table= li++))
624
4675
    {
625
 
      if (table->getNestedJoin())
626
 
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
 
4676
      if (table->nested_join)
 
4677
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level, 
627
4678
                              sargables);
628
4679
    }
629
4680
  }
643
4694
  */
644
4695
  if (keyuse->elements)
645
4696
  {
646
 
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
647
 
 
648
 
    internal::my_qsort(keyuse->buffer,keyuse->elements,sizeof(optimizer::KeyUse),
649
 
                       (qsort_cmp) sort_keyuse);
650
 
 
651
 
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
652
 
    insert_dynamic(keyuse,(unsigned char*) &key_end);
653
 
 
654
 
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
 
4697
    KEYUSE key_end,*prev,*save_pos,*use;
 
4698
 
 
4699
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
 
4700
          (qsort_cmp) sort_keyuse);
 
4701
 
 
4702
    bzero((char*) &key_end,sizeof(key_end));    /* Add for easy testing */
 
4703
    VOID(insert_dynamic(keyuse,(uchar*) &key_end));
 
4704
 
 
4705
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
655
4706
    prev= &key_end;
656
 
    found_eq_constant= 0;
 
4707
    found_eq_constant=0;
 
4708
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
657
4709
    {
658
 
      uint32_t i;
659
 
 
660
 
      for (i= 0; i < keyuse->elements-1; i++, use++)
 
4710
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
 
4711
        use->table->const_key_parts[use->key]|= use->keypart_map;
661
4712
      {
662
 
        if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
663
 
          use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
664
 
        if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
665
 
        {
666
 
          if (prev->getKeypart() + 1 < use->getKeypart() || 
667
 
              ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
668
 
            continue;                           /* remove */
669
 
        }
670
 
        else if (use->getKeypart() != 0)                // First found must be 0
671
 
          continue;
 
4713
        if (use->key == prev->key && use->table == prev->table)
 
4714
        {
 
4715
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
 
4716
            continue;                           /* remove */
 
4717
        }
 
4718
        else if (use->keypart != 0)             // First found must be 0
 
4719
          continue;
 
4720
      }
672
4721
 
673
 
#ifdef HAVE_VALGRIND
674
 
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
675
 
        if (save_pos != use)
 
4722
#ifdef HAVE_purify
 
4723
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
 
4724
      if (save_pos != use)
676
4725
#endif
677
 
          *save_pos= *use;
678
 
        prev=use;
679
 
        found_eq_constant= ! use->getUsedTables();
680
 
        /* Save ptr to first use */
681
 
        if (! use->getTable()->reginfo.join_tab->keyuse)
682
 
          use->getTable()->reginfo.join_tab->keyuse= save_pos;
683
 
        use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
684
 
        save_pos++;
685
 
      }
686
 
      i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
687
 
      set_dynamic(keyuse, (unsigned char*) &key_end, i);
688
 
      keyuse->elements= i;
 
4726
        *save_pos= *use;
 
4727
      prev=use;
 
4728
      found_eq_constant= !use->used_tables;
 
4729
      /* Save ptr to first use */
 
4730
      if (!use->table->reginfo.join_tab->keyuse)
 
4731
        use->table->reginfo.join_tab->keyuse=save_pos;
 
4732
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
 
4733
      save_pos++;
689
4734
    }
 
4735
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
 
4736
    VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
 
4737
    keyuse->elements=i;
690
4738
  }
691
4739
  return false;
692
4740
}
694
4742
/**
695
4743
  Update some values in keyuse for faster choose_plan() loop.
696
4744
*/
697
 
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
 
4745
 
 
4746
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
698
4747
{
699
 
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
700
 
                                                  0, 
701
 
                                                  optimizer::KeyUse*);
 
4748
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
702
4749
 
703
4750
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
704
4751
  {
711
4758
      Constant tables are ignored.
712
4759
      To avoid bad matches, we don't make ref_table_rows less than 100.
713
4760
    */
714
 
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
715
 
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
 
4761
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
 
4762
    if (keyuse->used_tables &
 
4763
        (map= (keyuse->used_tables & ~join->const_table_map &
 
4764
               ~OUTER_REF_TABLE_BIT)))
716
4765
    {
717
 
      uint32_t tablenr;
 
4766
      uint tablenr;
718
4767
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
719
4768
      if (map == 1)                     // Only one table
720
4769
      {
721
 
        Table *tmp_table=join->all_tables[tablenr];
722
 
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
 
4770
        TABLE *tmp_table=join->all_tables[tablenr];
 
4771
        keyuse->ref_table_rows= max(tmp_table->file->stats.records, 100);
723
4772
      }
724
4773
    }
725
4774
    /*
726
4775
      Outer reference (external field) is constant for single executing
727
4776
      of subquery
728
4777
    */
729
 
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
730
 
      keyuse->setTableRows(1);
 
4778
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
 
4779
      keyuse->ref_table_rows= 1;
731
4780
  }
732
4781
}
733
4782
 
749
4798
  @return
750
4799
    None
751
4800
*/
752
 
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
 
4801
 
 
4802
static void
 
4803
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
753
4804
{
754
4805
  List<Item_field> indexed_fields;
755
 
  List<Item_field>::iterator indexed_fields_it(indexed_fields.begin());
756
 
  Order      *cur_group;
 
4806
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
 
4807
  ORDER      *cur_group;
757
4808
  Item_field *cur_item;
758
4809
  key_map possible_keys(0);
759
4810
 
761
4812
  { /* Collect all query fields referenced in the GROUP clause. */
762
4813
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
763
4814
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
764
 
                               (unsigned char*) &indexed_fields);
 
4815
                               (uchar*) &indexed_fields);
765
4816
  }
766
4817
  else if (join->select_distinct)
767
4818
  { /* Collect all query fields referenced in the SELECT clause. */
768
4819
    List<Item> &select_items= join->fields_list;
769
 
    List<Item>::iterator select_items_it(select_items.begin());
 
4820
    List_iterator<Item> select_items_it(select_items);
770
4821
    Item *item;
771
4822
    while ((item= select_items_it++))
772
4823
      item->walk(&Item::collect_item_field_processor, 0,
773
 
                 (unsigned char*) &indexed_fields);
 
4824
                 (uchar*) &indexed_fields);
774
4825
  }
775
4826
  else
776
4827
    return;
780
4831
 
781
4832
  /* Intersect the keys of all group fields. */
782
4833
  cur_item= indexed_fields_it++;
783
 
  possible_keys|= cur_item->field->part_of_key;
 
4834
  possible_keys.merge(cur_item->field->part_of_key);
784
4835
  while ((cur_item= indexed_fields_it++))
785
4836
  {
786
 
    possible_keys&= cur_item->field->part_of_key;
787
 
  }
788
 
 
789
 
  if (possible_keys.any())
790
 
    join_tab->const_keys|= possible_keys;
791
 
}
792
 
 
793
 
/**
794
 
  Compare two JoinTable objects based on the number of accessed records.
795
 
 
796
 
  @param ptr1 pointer to first JoinTable object
797
 
  @param ptr2 pointer to second JoinTable object
 
4837
    possible_keys.intersect(cur_item->field->part_of_key);
 
4838
  }
 
4839
 
 
4840
  if (!possible_keys.is_clear_all())
 
4841
    join_tab->const_keys.merge(possible_keys);
 
4842
}
 
4843
 
 
4844
 
 
4845
/*****************************************************************************
 
4846
  Go through all combinations of not marked tables and find the one
 
4847
  which uses least records
 
4848
*****************************************************************************/
 
4849
 
 
4850
/** Save const tables first as used tables. */
 
4851
 
 
4852
static void
 
4853
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
 
4854
{
 
4855
  join->positions[idx].table= table;
 
4856
  join->positions[idx].key=key;
 
4857
  join->positions[idx].records_read=1.0;        /* This is a const table */
 
4858
  join->positions[idx].ref_depend_map= 0;
 
4859
 
 
4860
  /* Move the const table as down as possible in best_ref */
 
4861
  JOIN_TAB **pos=join->best_ref+idx+1;
 
4862
  JOIN_TAB *next=join->best_ref[idx];
 
4863
  for (;next != table ; pos++)
 
4864
  {
 
4865
    JOIN_TAB *tmp=pos[0];
 
4866
    pos[0]=next;
 
4867
    next=tmp;
 
4868
  }
 
4869
  join->best_ref[idx]=table;
 
4870
}
 
4871
 
 
4872
 
 
4873
/*
 
4874
  Given a semi-join nest, find out which of the IN-equalities are bound
 
4875
 
 
4876
  SYNOPSIS
 
4877
    get_bound_sj_equalities()
 
4878
      sj_nest           Semi-join nest
 
4879
      remaining_tables  Tables that are not yet bound
 
4880
 
 
4881
  DESCRIPTION
 
4882
    Given a semi-join nest, find out which of the IN-equalities have their
 
4883
    left part expression bound (i.e. the said expression doesn't refer to
 
4884
    any of remaining_tables and can be evaluated).
 
4885
 
 
4886
  RETURN
 
4887
    Bitmap of bound IN-equalities.
 
4888
*/
 
4889
 
 
4890
uint64_t get_bound_sj_equalities(TABLE_LIST *sj_nest, 
 
4891
                                  table_map remaining_tables)
 
4892
{
 
4893
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
 
4894
  Item *item;
 
4895
  uint i= 0;
 
4896
  uint64_t res= 0;
 
4897
  while ((item= li++))
 
4898
  {
 
4899
    /*
 
4900
      Q: should this take into account equality propagation and how?
 
4901
      A: If e->outer_side is an Item_field, walk over the equality
 
4902
         class and see if there is an element that is bound?
 
4903
      (this is an optional feature)
 
4904
    */
 
4905
    if (!(item->used_tables() & remaining_tables))
 
4906
    {
 
4907
      res |= 1ULL < i;
 
4908
    }
 
4909
  }
 
4910
  return res;
 
4911
}
 
4912
 
 
4913
 
 
4914
/**
 
4915
  Find the best access path for an extension of a partial execution
 
4916
  plan and add this path to the plan.
 
4917
 
 
4918
  The function finds the best access path to table 's' from the passed
 
4919
  partial plan where an access path is the general term for any means to
 
4920
  access the data in 's'. An access path may use either an index or a scan,
 
4921
  whichever is cheaper. The input partial plan is passed via the array
 
4922
  'join->positions' of length 'idx'. The chosen access method for 's' and its
 
4923
  cost are stored in 'join->positions[idx]'.
 
4924
 
 
4925
  @param join             pointer to the structure providing all context info
 
4926
                          for the query
 
4927
  @param s                the table to be joined by the function
 
4928
  @param thd              thread for the connection that submitted the query
 
4929
  @param remaining_tables set of tables not included into the partial plan yet
 
4930
  @param idx              the length of the partial plan
 
4931
  @param record_count     estimate for the number of records returned by the
 
4932
                          partial plan
 
4933
  @param read_time        the cost of the partial plan
 
4934
 
 
4935
  @return
 
4936
    None
 
4937
*/
 
4938
 
 
4939
static void
 
4940
best_access_path(JOIN      *join,
 
4941
                 JOIN_TAB  *s,
 
4942
                 THD       *thd,
 
4943
                 table_map remaining_tables,
 
4944
                 uint      idx,
 
4945
                 double    record_count,
 
4946
                 double    read_time __attribute__((__unused__)))
 
4947
{
 
4948
  KEYUSE *best_key=         0;
 
4949
  uint best_max_key_part=   0;
 
4950
  bool found_constraint= 0;
 
4951
  double best=              DBL_MAX;
 
4952
  double best_time=         DBL_MAX;
 
4953
  double records=           DBL_MAX;
 
4954
  table_map best_ref_depends_map= 0;
 
4955
  double tmp;
 
4956
  ha_rows rec;
 
4957
  uint best_is_sj_inside_out=    0;
 
4958
 
 
4959
  if (s->keyuse)
 
4960
  {                                            /* Use key if possible */
 
4961
    TABLE *table= s->table;
 
4962
    KEYUSE *keyuse,*start_key=0;
 
4963
    double best_records= DBL_MAX;
 
4964
    uint max_key_part=0;
 
4965
    uint64_t bound_sj_equalities= 0;
 
4966
    bool try_sj_inside_out= false;
 
4967
    /*
 
4968
      Discover the bound equalites. We need to do this, if
 
4969
        1. The next table is an SJ-inner table, and
 
4970
        2. It is the first table from that semijoin, and
 
4971
        3. We're not within a semi-join range (i.e. all semi-joins either have
 
4972
           all or none of their tables in join_table_map), except
 
4973
           s->emb_sj_nest (which we've just entered).
 
4974
        3. All correlation references from this sj-nest are bound
 
4975
    */
 
4976
    if (s->emb_sj_nest &&                                                 // (1)
 
4977
        s->emb_sj_nest->sj_in_exprs < 64 && 
 
4978
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
 
4979
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
 
4980
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
 
4981
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
 
4982
    {
 
4983
      /* This table is an InsideOut scan candidate */
 
4984
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest, 
 
4985
                                                   remaining_tables);
 
4986
      try_sj_inside_out= true;
 
4987
    }
 
4988
 
 
4989
    /* Test how we can use keys */
 
4990
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
 
4991
    for (keyuse=s->keyuse ; keyuse->table == table ;)
 
4992
    {
 
4993
      key_part_map found_part= 0;
 
4994
      table_map found_ref= 0;
 
4995
      uint key= keyuse->key;
 
4996
      KEY *keyinfo= table->key_info+key;
 
4997
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
 
4998
      key_part_map const_part= 0;
 
4999
      /* The or-null keypart in ref-or-null access: */
 
5000
      key_part_map ref_or_null_part= 0;
 
5001
 
 
5002
      /* Calculate how many key segments of the current key we can use */
 
5003
      start_key= keyuse;
 
5004
      uint64_t handled_sj_equalities=0;
 
5005
      key_part_map sj_insideout_map= 0;
 
5006
 
 
5007
      do /* For each keypart */
 
5008
      {
 
5009
        uint keypart= keyuse->keypart;
 
5010
        table_map best_part_found_ref= 0;
 
5011
        double best_prev_record_reads= DBL_MAX;
 
5012
        
 
5013
        do /* For each way to access the keypart */
 
5014
        {
 
5015
 
 
5016
          /*
 
5017
            if 1. expression doesn't refer to forward tables
 
5018
               2. we won't get two ref-or-null's
 
5019
          */
 
5020
          if (!(remaining_tables & keyuse->used_tables) &&
 
5021
              !(ref_or_null_part && (keyuse->optimize &
 
5022
                                     KEY_OPTIMIZE_REF_OR_NULL)))
 
5023
          {
 
5024
            found_part|= keyuse->keypart_map;
 
5025
            if (!(keyuse->used_tables & ~join->const_table_map))
 
5026
              const_part|= keyuse->keypart_map;
 
5027
 
 
5028
            double tmp2= prev_record_reads(join, idx, (found_ref |
 
5029
                                                      keyuse->used_tables));
 
5030
            if (tmp2 < best_prev_record_reads)
 
5031
            {
 
5032
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
 
5033
              best_prev_record_reads= tmp2;
 
5034
            }
 
5035
            if (rec > keyuse->ref_table_rows)
 
5036
              rec= keyuse->ref_table_rows;
 
5037
            /*
 
5038
              If there is one 'key_column IS NULL' expression, we can
 
5039
              use this ref_or_null optimisation of this field
 
5040
            */
 
5041
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
 
5042
              ref_or_null_part |= keyuse->keypart_map;
 
5043
          }
 
5044
 
 
5045
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
 
5046
          {
 
5047
            if (!(remaining_tables & keyuse->used_tables))
 
5048
              bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
 
5049
            else
 
5050
            {
 
5051
              handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
 
5052
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
 
5053
            }
 
5054
          }
 
5055
 
 
5056
          keyuse++;
 
5057
        } while (keyuse->table == table && keyuse->key == key &&
 
5058
                 keyuse->keypart == keypart);
 
5059
        found_ref|= best_part_found_ref;
 
5060
      } while (keyuse->table == table && keyuse->key == key);
 
5061
 
 
5062
      /*
 
5063
        Assume that that each key matches a proportional part of table.
 
5064
      */
 
5065
      if (!found_part && !handled_sj_equalities)
 
5066
        continue;                               // Nothing usable found
 
5067
 
 
5068
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
 
5069
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
 
5070
 
 
5071
      bool sj_inside_out_scan= false;
 
5072
      {
 
5073
        found_constraint= 1;
 
5074
        /*
 
5075
          Check if InsideOut scan is applicable:
 
5076
          1. All IN-equalities are either "bound" or "handled"
 
5077
          2. Index keyparts are 
 
5078
             ...
 
5079
        */
 
5080
        if (try_sj_inside_out && 
 
5081
            table->covering_keys.is_set(key) &&
 
5082
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
 
5083
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
 
5084
        {
 
5085
          uint n_fixed_parts= max_part_bit(found_part);
 
5086
          if (n_fixed_parts != keyinfo->key_parts &&
 
5087
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
 
5088
               PREV_BITS(uint, keyinfo->key_parts))
 
5089
          {
 
5090
            /*
 
5091
              Not all parts are fixed. Produce bitmap of remaining bits and
 
5092
              check if all of them are covered.
 
5093
            */
 
5094
            sj_inside_out_scan= true;
 
5095
            if (!n_fixed_parts)
 
5096
            {
 
5097
              /*
 
5098
                It's a confluent ref scan.
 
5099
 
 
5100
                That is, all found KEYUSE elements refer to IN-equalities,
 
5101
                and there is really no ref access because there is no
 
5102
                  t.keypart0 = {bound expression}
 
5103
 
 
5104
                Calculate the cost of complete loose index scan.
 
5105
              */
 
5106
              records= (double)s->table->file->stats.records;
 
5107
 
 
5108
              /* The cost is entire index scan cost (divided by 2) */
 
5109
              best_time= s->table->file->index_only_read_time(key, records);
 
5110
 
 
5111
              /* Now figure how many different keys we will get */
 
5112
              ulong rpc;
 
5113
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5114
                records= records / rpc;
 
5115
              start_key= NULL;
 
5116
            }
 
5117
          }
 
5118
        }
 
5119
 
 
5120
        /*
 
5121
          Check if we found full key
 
5122
        */
 
5123
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
 
5124
            !ref_or_null_part)
 
5125
        {                                         /* use eq key */
 
5126
          max_key_part= (uint) ~0;
 
5127
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
 
5128
          {
 
5129
            tmp = prev_record_reads(join, idx, found_ref);
 
5130
            records=1.0;
 
5131
          }
 
5132
          else
 
5133
          {
 
5134
            if (!found_ref)
 
5135
            {                                     /* We found a const key */
 
5136
              /*
 
5137
                ReuseRangeEstimateForRef-1:
 
5138
                We get here if we've found a ref(const) (c_i are constants):
 
5139
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
 
5140
                
 
5141
                If range optimizer was able to construct a "range" 
 
5142
                access on this index, then its condition "quick_cond" was
 
5143
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
 
5144
                from the range optimizer.
 
5145
                
 
5146
                Proof of (*): By properties of range and ref optimizers 
 
5147
                quick_cond will be equal or tighther than ref_const_cond. 
 
5148
                ref_const_cond already covers "smallest" possible interval - 
 
5149
                a singlepoint interval over all keyparts. Therefore, 
 
5150
                quick_cond is equivalent to ref_const_cond (if it was an 
 
5151
                empty interval we wouldn't have got here).
 
5152
              */
 
5153
              if (table->quick_keys.is_set(key))
 
5154
                records= (double) table->quick_rows[key];
 
5155
              else
 
5156
              {
 
5157
                /* quick_range couldn't use key! */
 
5158
                records= (double) s->records/rec;
 
5159
              }
 
5160
            }
 
5161
            else
 
5162
            {
 
5163
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5164
              {                                   /* Prefer longer keys */
 
5165
                records=
 
5166
                  ((double) s->records / (double) rec *
 
5167
                   (1.0 +
 
5168
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
 
5169
                     (double) table->s->max_key_length)));
 
5170
                if (records < 2.0)
 
5171
                  records=2.0;               /* Can't be as good as a unique */
 
5172
              }
 
5173
              /*
 
5174
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
 
5175
                E(#rows) from range optimizer. Make another try:
 
5176
                
 
5177
                If range optimizer produced E(#rows) for a prefix of the ref
 
5178
                access we're considering, and that E(#rows) is lower then our
 
5179
                current estimate, make an adjustment. The criteria of when we
 
5180
                can make an adjustment is a special case of the criteria used
 
5181
                in ReuseRangeEstimateForRef-3.
 
5182
              */
 
5183
              if (table->quick_keys.is_set(key) &&
 
5184
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5185
                  table->quick_n_ranges[key] == 1 &&
 
5186
                  records > (double) table->quick_rows[key])
 
5187
              {
 
5188
                records= (double) table->quick_rows[key];
 
5189
              }
 
5190
            }
 
5191
            /* Limit the number of matched rows */
 
5192
            tmp= records;
 
5193
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5194
            if (table->covering_keys.is_set(key))
 
5195
            {
 
5196
              /* we can use only index tree */
 
5197
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5198
            }
 
5199
            else
 
5200
              tmp= record_count*min(tmp,s->worst_seeks);
 
5201
          }
 
5202
        }
 
5203
        else
 
5204
        {
 
5205
          /*
 
5206
            Use as much key-parts as possible and a uniq key is better
 
5207
            than a not unique key
 
5208
            Set tmp to (previous record count) * (records / combination)
 
5209
          */
 
5210
          if ((found_part & 1) &&
 
5211
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
 
5212
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
 
5213
          {
 
5214
            max_key_part= max_part_bit(found_part);
 
5215
            /*
 
5216
              ReuseRangeEstimateForRef-3:
 
5217
              We're now considering a ref[or_null] access via
 
5218
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR  
 
5219
              (same-as-above but with one cond replaced 
 
5220
               with "t.keypart_i IS NULL")]  (**)
 
5221
              
 
5222
              Try re-using E(#rows) from "range" optimizer:
 
5223
              We can do so if "range" optimizer used the same intervals as
 
5224
              in (**). The intervals used by range optimizer may be not 
 
5225
              available at this point (as "range" access might have choosen to
 
5226
              create quick select over another index), so we can't compare
 
5227
              them to (**). We'll make indirect judgements instead.
 
5228
              The sufficient conditions for re-use are:
 
5229
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
 
5230
                   this is not satisfied we have no way to know which ranges
 
5231
                   will be actually scanned by 'ref' until we execute the 
 
5232
                   join)
 
5233
              (C2) max #key parts in 'range' access == K == max_key_part (this
 
5234
                   is apparently a necessary requirement)
 
5235
 
 
5236
              We also have a property that "range optimizer produces equal or 
 
5237
              tighter set of scan intervals than ref(const) optimizer". Each
 
5238
              of the intervals in (**) are "tightest possible" intervals when 
 
5239
              one limits itself to using keyparts 1..K (which we do in #2).              
 
5240
              From here it follows that range access used either one, or
 
5241
              both of the (I1) and (I2) intervals:
 
5242
              
 
5243
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1) 
 
5244
               (same-as-above but with one cond replaced  
 
5245
                with "t.keypart_i IS NULL")               (I2)
 
5246
 
 
5247
              The remaining part is to exclude the situation where range
 
5248
              optimizer used one interval while we're considering
 
5249
              ref-or-null and looking for estimate for two intervals. This
 
5250
              is done by last limitation:
 
5251
 
 
5252
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
 
5253
            */
 
5254
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
 
5255
                table->quick_key_parts[key] == max_key_part &&          //(C2)
 
5256
                table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
 
5257
            {
 
5258
              tmp= records= (double) table->quick_rows[key];
 
5259
            }
 
5260
            else
 
5261
            {
 
5262
              /* Check if we have statistic about the distribution */
 
5263
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
 
5264
              {
 
5265
                /* 
 
5266
                  Fix for the case where the index statistics is too
 
5267
                  optimistic: If 
 
5268
                  (1) We're considering ref(const) and there is quick select
 
5269
                      on the same index, 
 
5270
                  (2) and that quick select uses more keyparts (i.e. it will
 
5271
                      scan equal/smaller interval then this ref(const))
 
5272
                  (3) and E(#rows) for quick select is higher then our
 
5273
                      estimate,
 
5274
                  Then 
 
5275
                    We'll use E(#rows) from quick select.
 
5276
 
 
5277
                  Q: Why do we choose to use 'ref'? Won't quick select be
 
5278
                  cheaper in some cases ?
 
5279
                  TODO: figure this out and adjust the plan choice if needed.
 
5280
                */
 
5281
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
 
5282
                    table->quick_key_parts[key] > max_key_part &&     // (2)
 
5283
                    records < (double)table->quick_rows[key])         // (3)
 
5284
                  records= (double)table->quick_rows[key];
 
5285
 
 
5286
                tmp= records;
 
5287
              }
 
5288
              else
 
5289
              {
 
5290
                /*
 
5291
                  Assume that the first key part matches 1% of the file
 
5292
                  and that the whole key matches 10 (duplicates) or 1
 
5293
                  (unique) records.
 
5294
                  Assume also that more key matches proportionally more
 
5295
                  records
 
5296
                  This gives the formula:
 
5297
                  records = (x * (b-a) + a*c-b)/(c-1)
 
5298
 
 
5299
                  b = records matched by whole key
 
5300
                  a = records matched by first key part (1% of all records?)
 
5301
                  c = number of key parts in key
 
5302
                  x = used key parts (1 <= x <= c)
 
5303
                */
 
5304
                double rec_per_key;
 
5305
                if (!(rec_per_key=(double)
 
5306
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5307
                  rec_per_key=(double) s->records/rec+1;
 
5308
 
 
5309
                if (!s->records)
 
5310
                  tmp = 0;
 
5311
                else if (rec_per_key/(double) s->records >= 0.01)
 
5312
                  tmp = rec_per_key;
 
5313
                else
 
5314
                {
 
5315
                  double a=s->records*0.01;
 
5316
                  if (keyinfo->key_parts > 1)
 
5317
                    tmp= (max_key_part * (rec_per_key - a) +
 
5318
                          a*keyinfo->key_parts - rec_per_key)/
 
5319
                         (keyinfo->key_parts-1);
 
5320
                  else
 
5321
                    tmp= a;
 
5322
                  set_if_bigger(tmp,1.0);
 
5323
                }
 
5324
                records = (ulong) tmp;
 
5325
              }
 
5326
 
 
5327
              if (ref_or_null_part)
 
5328
              {
 
5329
                /* We need to do two key searches to find key */
 
5330
                tmp *= 2.0;
 
5331
                records *= 2.0;
 
5332
              }
 
5333
 
 
5334
              /*
 
5335
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
 
5336
                E(#rows) from range optimizer. Make another try:
 
5337
                
 
5338
                If range optimizer produced E(#rows) for a prefix of the ref 
 
5339
                access we're considering, and that E(#rows) is lower then our
 
5340
                current estimate, make the adjustment.
 
5341
 
 
5342
                The decision whether we can re-use the estimate from the range
 
5343
                optimizer is the same as in ReuseRangeEstimateForRef-3,
 
5344
                applied to first table->quick_key_parts[key] key parts.
 
5345
              */
 
5346
              if (table->quick_keys.is_set(key) &&
 
5347
                  table->quick_key_parts[key] <= max_key_part &&
 
5348
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5349
                  table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
 
5350
                                                         const_part) &&
 
5351
                  records > (double) table->quick_rows[key])
 
5352
              {
 
5353
                tmp= records= (double) table->quick_rows[key];
 
5354
              }
 
5355
            }
 
5356
 
 
5357
            /* Limit the number of matched rows */
 
5358
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5359
            if (table->covering_keys.is_set(key))
 
5360
            {
 
5361
              /* we can use only index tree */
 
5362
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5363
            }
 
5364
            else
 
5365
              tmp= record_count * min(tmp,s->worst_seeks);
 
5366
          }
 
5367
          else
 
5368
            tmp= best_time;                    // Do nothing
 
5369
        }
 
5370
 
 
5371
        if (sj_inside_out_scan && !start_key)
 
5372
        {
 
5373
          tmp= tmp/2;
 
5374
          if (records)
 
5375
            records= records/2;
 
5376
        }
 
5377
 
 
5378
      }
 
5379
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
 
5380
      {
 
5381
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
 
5382
        best= tmp;
 
5383
        best_records= records;
 
5384
        best_key= start_key;
 
5385
        best_max_key_part= max_key_part;
 
5386
        best_ref_depends_map= found_ref;
 
5387
        best_is_sj_inside_out= sj_inside_out_scan;
 
5388
      }
 
5389
    }
 
5390
    records= best_records;
 
5391
  }
 
5392
 
 
5393
  /*
 
5394
    Don't test table scan if it can't be better.
 
5395
    Prefer key lookup if we would use the same key for scanning.
 
5396
 
 
5397
    Don't do a table scan on InnoDB tables, if we can read the used
 
5398
    parts of the row from any of the used index.
 
5399
    This is because table scans uses index and we would not win
 
5400
    anything by using a table scan.
 
5401
 
 
5402
    A word for word translation of the below if-statement in sergefp's
 
5403
    understanding: we check if we should use table scan if:
 
5404
    (1) The found 'ref' access produces more records than a table scan
 
5405
        (or index scan, or quick select), or 'ref' is more expensive than
 
5406
        any of them.
 
5407
    (2) This doesn't hold: the best way to perform table scan is to to perform
 
5408
        'range' access using index IDX, and the best way to perform 'ref' 
 
5409
        access is to use the same index IDX, with the same or more key parts.
 
5410
        (note: it is not clear how this rule is/should be extended to 
 
5411
        index_merge quick selects)
 
5412
    (3) See above note about InnoDB.
 
5413
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
 
5414
             path, but there is no quick select)
 
5415
        If the condition in the above brackets holds, then the only possible
 
5416
        "table scan" access method is ALL/index (there is no quick select).
 
5417
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
 
5418
        choose it over ALL/index, there is no need to consider a full table
 
5419
        scan.
 
5420
  */
 
5421
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
 
5422
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
 
5423
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
 
5424
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
 
5425
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
 
5426
      !(s->table->force_index && best_key && !s->quick))                 // (4)
 
5427
  {                                             // Check full join
 
5428
    ha_rows rnd_records= s->found_records;
 
5429
    /*
 
5430
      If there is a filtering condition on the table (i.e. ref analyzer found
 
5431
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
 
5432
      preceding this table in the join order we're now considering), then 
 
5433
      assume that 25% of the rows will be filtered out by this condition.
 
5434
 
 
5435
      This heuristic is supposed to force tables used in exprZ to be before
 
5436
      this table in join order.
 
5437
    */
 
5438
    if (found_constraint)
 
5439
      rnd_records-= rnd_records/4;
 
5440
 
 
5441
    /*
 
5442
      If applicable, get a more accurate estimate. Don't use the two
 
5443
      heuristics at once.
 
5444
    */
 
5445
    if (s->table->quick_condition_rows != s->found_records)
 
5446
      rnd_records= s->table->quick_condition_rows;
 
5447
 
 
5448
    /*
 
5449
      Range optimizer never proposes a RANGE if it isn't better
 
5450
      than FULL: so if RANGE is present, it's always preferred to FULL.
 
5451
      Here we estimate its cost.
 
5452
    */
 
5453
    if (s->quick)
 
5454
    {
 
5455
      /*
 
5456
        For each record we:
 
5457
        - read record range through 'quick'
 
5458
        - skip rows which does not satisfy WHERE constraints
 
5459
        TODO: 
 
5460
        We take into account possible use of join cache for ALL/index
 
5461
        access (see first else-branch below), but we don't take it into 
 
5462
        account here for range/index_merge access. Find out why this is so.
 
5463
      */
 
5464
      tmp= record_count *
 
5465
        (s->quick->read_time +
 
5466
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5467
    }
 
5468
    else
 
5469
    {
 
5470
      /* Estimate cost of reading table. */
 
5471
      tmp= s->table->file->scan_time();
 
5472
      if (s->table->map & join->outer_join)     // Can't use join cache
 
5473
      {
 
5474
        /*
 
5475
          For each record we have to:
 
5476
          - read the whole table record 
 
5477
          - skip rows which does not satisfy join condition
 
5478
        */
 
5479
        tmp= record_count *
 
5480
          (tmp +
 
5481
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5482
      }
 
5483
      else
 
5484
      {
 
5485
        /* We read the table as many times as join buffer becomes full. */
 
5486
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
 
5487
                           record_count /
 
5488
                           (double) thd->variables.join_buff_size));
 
5489
        /* 
 
5490
            We don't make full cartesian product between rows in the scanned
 
5491
           table and existing records because we skip all rows from the
 
5492
           scanned table, which does not satisfy join condition when 
 
5493
           we read the table (see flush_cached_records for details). Here we
 
5494
           take into account cost to read and skip these records.
 
5495
        */
 
5496
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
 
5497
      }
 
5498
    }
 
5499
 
 
5500
    /*
 
5501
      We estimate the cost of evaluating WHERE clause for found records
 
5502
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
 
5503
      tmp give us total cost of using TABLE SCAN
 
5504
    */
 
5505
    if (best == DBL_MAX ||
 
5506
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
 
5507
         best + record_count/(double) TIME_FOR_COMPARE*records))
 
5508
    {
 
5509
      /*
 
5510
        If the table has a range (s->quick is set) make_join_select()
 
5511
        will ensure that this will be used
 
5512
      */
 
5513
      best= tmp;
 
5514
      records= rows2double(rnd_records);
 
5515
      best_key= 0;
 
5516
      /* range/index_merge/ALL/index access method are "independent", so: */
 
5517
      best_ref_depends_map= 0;
 
5518
      best_is_sj_inside_out= false;
 
5519
    }
 
5520
  }
 
5521
 
 
5522
  /* Update the cost information for the current partial plan */
 
5523
  join->positions[idx].records_read= records;
 
5524
  join->positions[idx].read_time=    best;
 
5525
  join->positions[idx].key=          best_key;
 
5526
  join->positions[idx].table=        s;
 
5527
  join->positions[idx].ref_depend_map= best_ref_depends_map;
 
5528
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
 
5529
 
 
5530
  if (!best_key &&
 
5531
      idx == join->const_tables &&
 
5532
      s->table == join->sort_by_table &&
 
5533
      join->unit->select_limit_cnt >= records)
 
5534
    join->sort_by_table= (TABLE*) 1;  // Must use temporary table
 
5535
 
 
5536
  return;
 
5537
}
 
5538
 
 
5539
 
 
5540
/**
 
5541
  Selects and invokes a search strategy for an optimal query plan.
 
5542
 
 
5543
  The function checks user-configurable parameters that control the search
 
5544
  strategy for an optimal plan, selects the search method and then invokes
 
5545
  it. Each specific optimization procedure stores the final optimal plan in
 
5546
  the array 'join->best_positions', and the cost of the plan in
 
5547
  'join->best_read'.
 
5548
 
 
5549
  @param join         pointer to the structure providing all context info for
 
5550
                      the query
 
5551
  @param join_tables  set of the tables in the query
 
5552
 
 
5553
  @todo
 
5554
    'MAX_TABLES+2' denotes the old implementation of find_best before
 
5555
    the greedy version. Will be removed when greedy_search is approved.
 
5556
 
 
5557
  @retval
 
5558
    false       ok
 
5559
  @retval
 
5560
    true        Fatal error
 
5561
*/
 
5562
 
 
5563
static bool
 
5564
choose_plan(JOIN *join, table_map join_tables)
 
5565
{
 
5566
  uint search_depth= join->thd->variables.optimizer_search_depth;
 
5567
  uint prune_level=  join->thd->variables.optimizer_prune_level;
 
5568
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
5569
 
 
5570
  join->cur_embedding_map= 0;
 
5571
  reset_nj_counters(join->join_list);
 
5572
  /*
 
5573
    if (SELECT_STRAIGHT_JOIN option is set)
 
5574
      reorder tables so dependent tables come after tables they depend 
 
5575
      on, otherwise keep tables in the order they were specified in the query 
 
5576
    else
 
5577
      Apply heuristic: pre-sort all access plans with respect to the number of
 
5578
      records accessed.
 
5579
  */
 
5580
  my_qsort(join->best_ref + join->const_tables,
 
5581
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
 
5582
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
 
5583
  join->cur_emb_sj_nests= 0;
 
5584
  if (straight_join)
 
5585
  {
 
5586
    optimize_straight_join(join, join_tables);
 
5587
  }
 
5588
  else
 
5589
  {
 
5590
    if (search_depth == MAX_TABLES+2)
 
5591
    { /*
 
5592
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
 
5593
        the greedy version. Will be removed when greedy_search is approved.
 
5594
      */
 
5595
      join->best_read= DBL_MAX;
 
5596
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
 
5597
        return(true);
 
5598
    } 
 
5599
    else
 
5600
    {
 
5601
      if (search_depth == 0)
 
5602
        /* Automatically determine a reasonable value for 'search_depth' */
 
5603
        search_depth= determine_search_depth(join);
 
5604
      if (greedy_search(join, join_tables, search_depth, prune_level))
 
5605
        return(true);
 
5606
    }
 
5607
  }
 
5608
 
 
5609
  /* 
 
5610
    Store the cost of this query into a user variable
 
5611
    Don't update last_query_cost for statements that are not "flat joins" :
 
5612
    i.e. they have subqueries, unions or call stored procedures.
 
5613
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
 
5614
  */
 
5615
  if (join->thd->lex->is_single_level_stmt())
 
5616
    join->thd->status_var.last_query_cost= join->best_read;
 
5617
  return(false);
 
5618
}
 
5619
 
 
5620
 
 
5621
/**
 
5622
  Compare two JOIN_TAB objects based on the number of accessed records.
 
5623
 
 
5624
  @param ptr1 pointer to first JOIN_TAB object
 
5625
  @param ptr2 pointer to second JOIN_TAB object
798
5626
 
799
5627
  NOTES
800
5628
    The order relation implemented by join_tab_cmp() is not transitive,
806
5634
      a: dependent = 0x0 table->map = 0x1 found_records = 3 ptr = 0x907e6b0
807
5635
      b: dependent = 0x0 table->map = 0x2 found_records = 3 ptr = 0x907e838
808
5636
      c: dependent = 0x6 table->map = 0x10 found_records = 2 ptr = 0x907ecd0
809
 
 
 
5637
     
810
5638
  @retval
811
5639
    1  if first is bigger
812
5640
  @retval
814
5642
  @retval
815
5643
    0  if equal
816
5644
*/
817
 
int join_tab_cmp(const void* ptr1, const void* ptr2)
 
5645
 
 
5646
static int
 
5647
join_tab_cmp(const void* ptr1, const void* ptr2)
818
5648
{
819
 
  JoinTable *jt1= *(JoinTable**) ptr1;
820
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5649
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5650
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
821
5651
 
822
5652
  if (jt1->dependent & jt2->table->map)
823
5653
    return 1;
824
5654
  if (jt2->dependent & jt1->table->map)
825
 
    return -1;
 
5655
    return -1;  
826
5656
  if (jt1->found_records > jt2->found_records)
827
5657
    return 1;
828
5658
  if (jt1->found_records < jt2->found_records)
829
 
    return -1;
 
5659
    return -1; 
830
5660
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
831
5661
}
832
5662
 
 
5663
 
833
5664
/**
834
5665
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
835
5666
*/
836
 
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
 
5667
 
 
5668
static int
 
5669
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
837
5670
{
838
 
  JoinTable *jt1= *(JoinTable**) ptr1;
839
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5671
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5672
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
840
5673
 
841
5674
  if (jt1->dependent & jt2->table->map)
842
5675
    return 1;
846
5679
}
847
5680
 
848
5681
/**
 
5682
  Heuristic procedure to automatically guess a reasonable degree of
 
5683
  exhaustiveness for the greedy search procedure.
 
5684
 
 
5685
  The procedure estimates the optimization time and selects a search depth
 
5686
  big enough to result in a near-optimal QEP, that doesn't take too long to
 
5687
  find. If the number of tables in the query exceeds some constant, then
 
5688
  search_depth is set to this constant.
 
5689
 
 
5690
  @param join   pointer to the structure providing all context info for
 
5691
                the query
 
5692
 
 
5693
  @note
 
5694
    This is an extremely simplistic implementation that serves as a stub for a
 
5695
    more advanced analysis of the join. Ideally the search depth should be
 
5696
    determined by learning from previous query optimizations, because it will
 
5697
    depend on the CPU power (and other factors).
 
5698
 
 
5699
  @todo
 
5700
    this value should be determined dynamically, based on statistics:
 
5701
    uint max_tables_for_exhaustive_opt= 7;
 
5702
 
 
5703
  @todo
 
5704
    this value could be determined by some mapping of the form:
 
5705
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5706
 
 
5707
  @return
 
5708
    A positive integer that specifies the search depth (and thus the
 
5709
    exhaustiveness) of the depth-first search algorithm used by
 
5710
    'greedy_search'.
 
5711
*/
 
5712
 
 
5713
static uint
 
5714
determine_search_depth(JOIN *join)
 
5715
{
 
5716
  uint table_count=  join->tables - join->const_tables;
 
5717
  uint search_depth;
 
5718
  /* TODO: this value should be determined dynamically, based on statistics: */
 
5719
  uint max_tables_for_exhaustive_opt= 7;
 
5720
 
 
5721
  if (table_count <= max_tables_for_exhaustive_opt)
 
5722
    search_depth= table_count+1; // use exhaustive for small number of tables
 
5723
  else
 
5724
    /*
 
5725
      TODO: this value could be determined by some mapping of the form:
 
5726
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5727
    */
 
5728
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
 
5729
 
 
5730
  return search_depth;
 
5731
}
 
5732
 
 
5733
 
 
5734
/**
 
5735
  Select the best ways to access the tables in a query without reordering them.
 
5736
 
 
5737
    Find the best access paths for each query table and compute their costs
 
5738
    according to their order in the array 'join->best_ref' (thus without
 
5739
    reordering the join tables). The function calls sequentially
 
5740
    'best_access_path' for each table in the query to select the best table
 
5741
    access method. The final optimal plan is stored in the array
 
5742
    'join->best_positions', and the corresponding cost in 'join->best_read'.
 
5743
 
 
5744
  @param join          pointer to the structure providing all context info for
 
5745
                       the query
 
5746
  @param join_tables   set of the tables in the query
 
5747
 
 
5748
  @note
 
5749
    This function can be applied to:
 
5750
    - queries with STRAIGHT_JOIN
 
5751
    - internally to compute the cost of an arbitrary QEP
 
5752
  @par
 
5753
    Thus 'optimize_straight_join' can be used at any stage of the query
 
5754
    optimization process to finalize a QEP as it is.
 
5755
*/
 
5756
 
 
5757
static void
 
5758
optimize_straight_join(JOIN *join, table_map join_tables)
 
5759
{
 
5760
  JOIN_TAB *s;
 
5761
  uint idx= join->const_tables;
 
5762
  double    record_count= 1.0;
 
5763
  double    read_time=    0.0;
 
5764
 
 
5765
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
5766
  {
 
5767
    /* Find the best access method from 's' to the current partial plan */
 
5768
    advance_sj_state(join_tables, s);
 
5769
    best_access_path(join, s, join->thd, join_tables, idx,
 
5770
                     record_count, read_time);
 
5771
    /* compute the cost of the new plan extended with 's' */
 
5772
    record_count*= join->positions[idx].records_read;
 
5773
    read_time+=    join->positions[idx].read_time;
 
5774
    join_tables&= ~(s->table->map);
 
5775
    ++idx;
 
5776
  }
 
5777
 
 
5778
  read_time+= record_count / (double) TIME_FOR_COMPARE;
 
5779
  if (join->sort_by_table &&
 
5780
      join->sort_by_table != join->positions[join->const_tables].table->table)
 
5781
    read_time+= record_count;  // We have to make a temp table
 
5782
  memcpy((uchar*) join->best_positions, (uchar*) join->positions,
 
5783
         sizeof(POSITION)*idx);
 
5784
  join->best_read= read_time;
 
5785
}
 
5786
 
 
5787
 
 
5788
/**
 
5789
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
 
5790
 
 
5791
    The search procedure uses a hybrid greedy/exhaustive search with controlled
 
5792
    exhaustiveness. The search is performed in N = card(remaining_tables)
 
5793
    steps. Each step evaluates how promising is each of the unoptimized tables,
 
5794
    selects the most promising table, and extends the current partial QEP with
 
5795
    that table.  Currenly the most 'promising' table is the one with least
 
5796
    expensive extension.\
 
5797
 
 
5798
    There are two extreme cases:
 
5799
    -# When (card(remaining_tables) < search_depth), the estimate finds the
 
5800
    best complete continuation of the partial QEP. This continuation can be
 
5801
    used directly as a result of the search.
 
5802
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
 
5803
    consideres the extension of the current QEP with each of the remaining
 
5804
    unoptimized tables.
 
5805
 
 
5806
    All other cases are in-between these two extremes. Thus the parameter
 
5807
    'search_depth' controlls the exhaustiveness of the search. The higher the
 
5808
    value, the longer the optimizaton time and possibly the better the
 
5809
    resulting plan. The lower the value, the fewer alternative plans are
 
5810
    estimated, but the more likely to get a bad QEP.
 
5811
 
 
5812
    All intermediate and final results of the procedure are stored in 'join':
 
5813
    - join->positions     : modified for every partial QEP that is explored
 
5814
    - join->best_positions: modified for the current best complete QEP
 
5815
    - join->best_read     : modified for the current best complete QEP
 
5816
    - join->best_ref      : might be partially reordered
 
5817
 
 
5818
    The final optimal plan is stored in 'join->best_positions', and its
 
5819
    corresponding cost in 'join->best_read'.
 
5820
 
 
5821
  @note
 
5822
    The following pseudocode describes the algorithm of 'greedy_search':
 
5823
 
 
5824
    @code
 
5825
    procedure greedy_search
 
5826
    input: remaining_tables
 
5827
    output: pplan;
 
5828
    {
 
5829
      pplan = <>;
 
5830
      do {
 
5831
        (t, a) = best_extension(pplan, remaining_tables);
 
5832
        pplan = concat(pplan, (t, a));
 
5833
        remaining_tables = remaining_tables - t;
 
5834
      } while (remaining_tables != {})
 
5835
      return pplan;
 
5836
    }
 
5837
 
 
5838
  @endcode
 
5839
    where 'best_extension' is a placeholder for a procedure that selects the
 
5840
    most "promising" of all tables in 'remaining_tables'.
 
5841
    Currently this estimate is performed by calling
 
5842
    'best_extension_by_limited_search' to evaluate all extensions of the
 
5843
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
 
5844
    mainly depends on that of 'best_extension_by_limited_search'.
 
5845
 
 
5846
  @par
 
5847
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
 
5848
    worst-case complexity of this algorithm is <=
 
5849
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
 
5850
    complexity of greedy_search is O(N!).
 
5851
 
 
5852
  @par
 
5853
    In the future, 'greedy_search' might be extended to support other
 
5854
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
 
5855
 
 
5856
  @param join             pointer to the structure providing all context info
 
5857
                          for the query
 
5858
  @param remaining_tables set of tables not included into the partial plan yet
 
5859
  @param search_depth     controlls the exhaustiveness of the search
 
5860
  @param prune_level      the pruning heuristics that should be applied during
 
5861
                          search
 
5862
 
 
5863
  @retval
 
5864
    false       ok
 
5865
  @retval
 
5866
    true        Fatal error
 
5867
*/
 
5868
 
 
5869
static bool
 
5870
greedy_search(JOIN      *join,
 
5871
              table_map remaining_tables,
 
5872
              uint      search_depth,
 
5873
              uint      prune_level)
 
5874
{
 
5875
  double    record_count= 1.0;
 
5876
  double    read_time=    0.0;
 
5877
  uint      idx= join->const_tables; // index into 'join->best_ref'
 
5878
  uint      best_idx;
 
5879
  uint      size_remain;    // cardinality of remaining_tables
 
5880
  POSITION  best_pos;
 
5881
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
 
5882
 
 
5883
  /* number of tables that remain to be optimized */
 
5884
  size_remain= my_count_bits(remaining_tables);
 
5885
 
 
5886
  do {
 
5887
    /* Find the extension of the current QEP with the lowest cost */
 
5888
    join->best_read= DBL_MAX;
 
5889
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
 
5890
                                         read_time, search_depth, prune_level))
 
5891
      return(true);
 
5892
 
 
5893
    if (size_remain <= search_depth)
 
5894
    {
 
5895
      /*
 
5896
        'join->best_positions' contains a complete optimal extension of the
 
5897
        current partial QEP.
 
5898
      */
 
5899
      return(false);
 
5900
    }
 
5901
 
 
5902
    /* select the first table in the optimal extension as most promising */
 
5903
    best_pos= join->best_positions[idx];
 
5904
    best_table= best_pos.table;
 
5905
    /*
 
5906
      Each subsequent loop of 'best_extension_by_limited_search' uses
 
5907
      'join->positions' for cost estimates, therefore we have to update its
 
5908
      value.
 
5909
    */
 
5910
    join->positions[idx]= best_pos;
 
5911
 
 
5912
    /* find the position of 'best_table' in 'join->best_ref' */
 
5913
    best_idx= idx;
 
5914
    JOIN_TAB *pos= join->best_ref[best_idx];
 
5915
    while (pos && best_table != pos)
 
5916
      pos= join->best_ref[++best_idx];
 
5917
    assert((pos != NULL)); // should always find 'best_table'
 
5918
    /* move 'best_table' at the first free position in the array of joins */
 
5919
    swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
 
5920
 
 
5921
    /* compute the cost of the new plan extended with 'best_table' */
 
5922
    record_count*= join->positions[idx].records_read;
 
5923
    read_time+=    join->positions[idx].read_time;
 
5924
 
 
5925
    remaining_tables&= ~(best_table->table->map);
 
5926
    --size_remain;
 
5927
    ++idx;
 
5928
  } while (true);
 
5929
}
 
5930
 
 
5931
 
 
5932
/**
 
5933
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
 
5934
  exhaustive search.
 
5935
 
 
5936
    The procedure searches for the optimal ordering of the query tables in set
 
5937
    'remaining_tables' of size N, and the corresponding optimal access paths to
 
5938
    each table. The choice of a table order and an access path for each table
 
5939
    constitutes a query execution plan (QEP) that fully specifies how to
 
5940
    execute the query.
 
5941
   
 
5942
    The maximal size of the found plan is controlled by the parameter
 
5943
    'search_depth'. When search_depth == N, the resulting plan is complete and
 
5944
    can be used directly as a QEP. If search_depth < N, the found plan consists
 
5945
    of only some of the query tables. Such "partial" optimal plans are useful
 
5946
    only as input to query optimization procedures, and cannot be used directly
 
5947
    to execute a query.
 
5948
 
 
5949
    The algorithm begins with an empty partial plan stored in 'join->positions'
 
5950
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
 
5951
    evaluates the cost of the partial plan extended by all access plans for
 
5952
    each of the relations in 'remaining_tables', expands the current partial
 
5953
    plan with the access plan that results in lowest cost of the expanded
 
5954
    partial plan, and removes the corresponding relation from
 
5955
    'remaining_tables'. The algorithm continues until it either constructs a
 
5956
    complete optimal plan, or constructs an optimal plartial plan with size =
 
5957
    search_depth.
 
5958
 
 
5959
    The final optimal plan is stored in 'join->best_positions'. The
 
5960
    corresponding cost of the optimal plan is in 'join->best_read'.
 
5961
 
 
5962
  @note
 
5963
    The procedure uses a recursive depth-first search where the depth of the
 
5964
    recursion (and thus the exhaustiveness of the search) is controlled by the
 
5965
    parameter 'search_depth'.
 
5966
 
 
5967
  @note
 
5968
    The pseudocode below describes the algorithm of
 
5969
    'best_extension_by_limited_search'. The worst-case complexity of this
 
5970
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
 
5971
    the complexity of greedy_search is O(N!).
 
5972
 
 
5973
    @code
 
5974
    procedure best_extension_by_limited_search(
 
5975
      pplan in,             // in, partial plan of tables-joined-so-far
 
5976
      pplan_cost,           // in, cost of pplan
 
5977
      remaining_tables,     // in, set of tables not referenced in pplan
 
5978
      best_plan_so_far,     // in/out, best plan found so far
 
5979
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
 
5980
      search_depth)         // in, maximum size of the plans being considered
 
5981
    {
 
5982
      for each table T from remaining_tables
 
5983
      {
 
5984
        // Calculate the cost of using table T as above
 
5985
        cost = complex-series-of-calculations;
 
5986
 
 
5987
        // Add the cost to the cost so far.
 
5988
        pplan_cost+= cost;
 
5989
 
 
5990
        if (pplan_cost >= best_plan_so_far_cost)
 
5991
          // pplan_cost already too great, stop search
 
5992
          continue;
 
5993
 
 
5994
        pplan= expand pplan by best_access_method;
 
5995
        remaining_tables= remaining_tables - table T;
 
5996
        if (remaining_tables is not an empty set
 
5997
            and
 
5998
            search_depth > 1)
 
5999
        {
 
6000
          best_extension_by_limited_search(pplan, pplan_cost,
 
6001
                                           remaining_tables,
 
6002
                                           best_plan_so_far,
 
6003
                                           best_plan_so_far_cost,
 
6004
                                           search_depth - 1);
 
6005
        }
 
6006
        else
 
6007
        {
 
6008
          best_plan_so_far_cost= pplan_cost;
 
6009
          best_plan_so_far= pplan;
 
6010
        }
 
6011
      }
 
6012
    }
 
6013
    @endcode
 
6014
 
 
6015
  @note
 
6016
    When 'best_extension_by_limited_search' is called for the first time,
 
6017
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
 
6018
    The actual implementation provides a way to optionally use pruning
 
6019
    heuristic (controlled by the parameter 'prune_level') to reduce the search
 
6020
    space by skipping some partial plans.
 
6021
 
 
6022
  @note
 
6023
    The parameter 'search_depth' provides control over the recursion
 
6024
    depth, and thus the size of the resulting optimal plan.
 
6025
 
 
6026
  @param join             pointer to the structure providing all context info
 
6027
                          for the query
 
6028
  @param remaining_tables set of tables not included into the partial plan yet
 
6029
  @param idx              length of the partial QEP in 'join->positions';
 
6030
                          since a depth-first search is used, also corresponds
 
6031
                          to the current depth of the search tree;
 
6032
                          also an index in the array 'join->best_ref';
 
6033
  @param record_count     estimate for the number of records returned by the
 
6034
                          best partial plan
 
6035
  @param read_time        the cost of the best partial plan
 
6036
  @param search_depth     maximum depth of the recursion and thus size of the
 
6037
                          found optimal plan
 
6038
                          (0 < search_depth <= join->tables+1).
 
6039
  @param prune_level      pruning heuristics that should be applied during
 
6040
                          optimization
 
6041
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
 
6042
 
 
6043
  @retval
 
6044
    false       ok
 
6045
  @retval
 
6046
    true        Fatal error
 
6047
*/
 
6048
 
 
6049
static bool
 
6050
best_extension_by_limited_search(JOIN      *join,
 
6051
                                 table_map remaining_tables,
 
6052
                                 uint      idx,
 
6053
                                 double    record_count,
 
6054
                                 double    read_time,
 
6055
                                 uint      search_depth,
 
6056
                                 uint      prune_level)
 
6057
{
 
6058
  THD *thd= join->thd;
 
6059
  if (thd->killed)  // Abort
 
6060
    return(true);
 
6061
 
 
6062
  /* 
 
6063
     'join' is a partial plan with lower cost than the best plan so far,
 
6064
     so continue expanding it further with the tables in 'remaining_tables'.
 
6065
  */
 
6066
  JOIN_TAB *s;
 
6067
  double best_record_count= DBL_MAX;
 
6068
  double best_read_time=    DBL_MAX;
 
6069
 
 
6070
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
6071
  {
 
6072
    table_map real_table_bit= s->table->map;
 
6073
    if ((remaining_tables & real_table_bit) && 
 
6074
        !(remaining_tables & s->dependent) && 
 
6075
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6076
    {
 
6077
      double current_record_count, current_read_time;
 
6078
      advance_sj_state(remaining_tables, s);
 
6079
 
 
6080
      /*
 
6081
        psergey-insideout-todo: 
 
6082
          when best_access_path() detects it could do an InsideOut scan or 
 
6083
          some other scan, have it return an insideout scan and a flag that 
 
6084
          requests to "fork" this loop iteration. (Q: how does that behave 
 
6085
          when the depth is insufficient??)
 
6086
      */
 
6087
      /* Find the best access method from 's' to the current partial plan */
 
6088
      best_access_path(join, s, thd, remaining_tables, idx,
 
6089
                       record_count, read_time);
 
6090
      /* Compute the cost of extending the plan with 's' */
 
6091
      current_record_count= record_count * join->positions[idx].records_read;
 
6092
      current_read_time=    read_time + join->positions[idx].read_time;
 
6093
 
 
6094
      /* Expand only partial plans with lower cost than the best QEP so far */
 
6095
      if ((current_read_time +
 
6096
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
 
6097
      {
 
6098
        restore_prev_nj_state(s);
 
6099
        restore_prev_sj_state(remaining_tables, s);
 
6100
        continue;
 
6101
      }
 
6102
 
 
6103
      /*
 
6104
        Prune some less promising partial plans. This heuristic may miss
 
6105
        the optimal QEPs, thus it results in a non-exhaustive search.
 
6106
      */
 
6107
      if (prune_level == 1)
 
6108
      {
 
6109
        if (best_record_count > current_record_count ||
 
6110
            best_read_time > current_read_time ||
 
6111
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
 
6112
        {
 
6113
          if (best_record_count >= current_record_count &&
 
6114
              best_read_time >= current_read_time &&
 
6115
              /* TODO: What is the reasoning behind this condition? */
 
6116
              (!(s->key_dependent & remaining_tables) ||
 
6117
               join->positions[idx].records_read < 2.0))
 
6118
          {
 
6119
            best_record_count= current_record_count;
 
6120
            best_read_time=    current_read_time;
 
6121
          }
 
6122
        }
 
6123
        else
 
6124
        {
 
6125
          restore_prev_nj_state(s);
 
6126
          restore_prev_sj_state(remaining_tables, s);
 
6127
          continue;
 
6128
        }
 
6129
      }
 
6130
 
 
6131
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
 
6132
      { /* Recursively expand the current partial plan */
 
6133
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6134
        if (best_extension_by_limited_search(join,
 
6135
                                             remaining_tables & ~real_table_bit,
 
6136
                                             idx + 1,
 
6137
                                             current_record_count,
 
6138
                                             current_read_time,
 
6139
                                             search_depth - 1,
 
6140
                                             prune_level))
 
6141
          return(true);
 
6142
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6143
      }
 
6144
      else
 
6145
      { /*
 
6146
          'join' is either the best partial QEP with 'search_depth' relations,
 
6147
          or the best complete QEP so far, whichever is smaller.
 
6148
        */
 
6149
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
 
6150
        if (join->sort_by_table &&
 
6151
            join->sort_by_table !=
 
6152
            join->positions[join->const_tables].table->table)
 
6153
          /* We have to make a temp table */
 
6154
          current_read_time+= current_record_count;
 
6155
        if ((search_depth == 1) || (current_read_time < join->best_read))
 
6156
        {
 
6157
          memcpy((uchar*) join->best_positions, (uchar*) join->positions,
 
6158
                 sizeof(POSITION) * (idx + 1));
 
6159
          join->best_read= current_read_time - 0.001;
 
6160
        }
 
6161
      }
 
6162
      restore_prev_nj_state(s);
 
6163
      restore_prev_sj_state(remaining_tables, s);
 
6164
    }
 
6165
  }
 
6166
  return(false);
 
6167
}
 
6168
 
 
6169
 
 
6170
/**
 
6171
  @todo
 
6172
  - TODO: this function is here only temporarily until 'greedy_search' is
 
6173
  tested and accepted.
 
6174
 
 
6175
  RETURN VALUES
 
6176
    false       ok
 
6177
    true        Fatal error
 
6178
*/
 
6179
static bool
 
6180
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
 
6181
          double read_time)
 
6182
{
 
6183
  THD *thd= join->thd;
 
6184
  if (thd->killed)
 
6185
    return(true);
 
6186
  if (!rest_tables)
 
6187
  {
 
6188
    read_time+=record_count/(double) TIME_FOR_COMPARE;
 
6189
    if (join->sort_by_table &&
 
6190
        join->sort_by_table !=
 
6191
        join->positions[join->const_tables].table->table)
 
6192
      read_time+=record_count;                  // We have to make a temp table
 
6193
    if (read_time < join->best_read)
 
6194
    {
 
6195
      memcpy((uchar*) join->best_positions,(uchar*) join->positions,
 
6196
             sizeof(POSITION)*idx);
 
6197
      join->best_read= read_time - 0.001;
 
6198
    }
 
6199
    return(false);
 
6200
  }
 
6201
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
 
6202
    return(false);                                      /* Found better before */
 
6203
 
 
6204
  JOIN_TAB *s;
 
6205
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
 
6206
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
 
6207
  {
 
6208
    table_map real_table_bit=s->table->map;
 
6209
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
 
6210
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6211
    {
 
6212
      double records, best;
 
6213
      advance_sj_state(rest_tables, s);
 
6214
      best_access_path(join, s, thd, rest_tables, idx, record_count, 
 
6215
                       read_time);
 
6216
      records= join->positions[idx].records_read;
 
6217
      best= join->positions[idx].read_time;
 
6218
      /*
 
6219
        Go to the next level only if there hasn't been a better key on
 
6220
        this level! This will cut down the search for a lot simple cases!
 
6221
      */
 
6222
      double current_record_count=record_count*records;
 
6223
      double current_read_time=read_time+best;
 
6224
      if (best_record_count > current_record_count ||
 
6225
          best_read_time > current_read_time ||
 
6226
          (idx == join->const_tables && s->table == join->sort_by_table))
 
6227
      {
 
6228
        if (best_record_count >= current_record_count &&
 
6229
            best_read_time >= current_read_time &&
 
6230
            (!(s->key_dependent & rest_tables) || records < 2.0))
 
6231
        {
 
6232
          best_record_count=current_record_count;
 
6233
          best_read_time=current_read_time;
 
6234
        }
 
6235
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6236
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
 
6237
                      current_record_count,current_read_time))
 
6238
          return(true);
 
6239
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6240
      }
 
6241
      restore_prev_nj_state(s);
 
6242
      restore_prev_sj_state(rest_tables, s);
 
6243
      if (join->select_options & SELECT_STRAIGHT_JOIN)
 
6244
        break;                          // Don't test all combinations
 
6245
    }
 
6246
  }
 
6247
  return(false);
 
6248
}
 
6249
 
 
6250
 
 
6251
/**
849
6252
  Find how much space the prevous read not const tables takes in cache.
850
6253
*/
851
 
void calc_used_field_length(Session *, JoinTable *join_tab)
 
6254
 
 
6255
static void calc_used_field_length(THD *thd __attribute__((__unused__)),
 
6256
                                   JOIN_TAB *join_tab)
852
6257
{
853
 
  uint32_t null_fields,blobs,fields,rec_length;
 
6258
  uint null_fields,blobs,fields,rec_length;
854
6259
  Field **f_ptr,*field;
 
6260
  MY_BITMAP *read_set= join_tab->table->read_set;;
855
6261
 
856
6262
  null_fields= blobs= fields= rec_length=0;
857
 
  for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
 
6263
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
858
6264
  {
859
 
    if (field->isReadSet())
 
6265
    if (bitmap_is_set(read_set, field->field_index))
860
6266
    {
861
 
      uint32_t flags=field->flags;
 
6267
      uint flags=field->flags;
862
6268
      fields++;
863
6269
      rec_length+=field->pack_length();
864
6270
      if (flags & BLOB_FLAG)
865
 
        blobs++;
 
6271
        blobs++;
866
6272
      if (!(flags & NOT_NULL_FLAG))
867
 
        null_fields++;
 
6273
        null_fields++;
868
6274
    }
869
6275
  }
870
6276
  if (null_fields)
871
 
    rec_length+=(join_tab->table->getNullFields() + 7)/8;
 
6277
    rec_length+=(join_tab->table->s->null_fields+7)/8;
872
6278
  if (join_tab->table->maybe_null)
873
6279
    rec_length+=sizeof(bool);
874
6280
  if (blobs)
875
6281
  {
876
 
    uint32_t blob_length=(uint32_t) (join_tab->table->cursor->stats.mean_rec_length-
877
 
                                     (join_tab->table->getRecordLength()- rec_length));
878
 
    rec_length+= max((uint32_t)4,blob_length);
879
 
  }
880
 
  join_tab->used_fields= fields;
881
 
  join_tab->used_fieldlength= rec_length;
882
 
  join_tab->used_blobs= blobs;
883
 
}
884
 
 
885
 
StoredKey *get_store_key(Session *session,
886
 
                         optimizer::KeyUse *keyuse,
887
 
                         table_map used_tables,
888
 
                         KeyPartInfo *key_part,
889
 
                         unsigned char *key_buff,
890
 
                         uint32_t maybe_null)
891
 
{
892
 
  Item_ref *key_use_val= static_cast<Item_ref *>(keyuse->getVal());
893
 
  if (! ((~used_tables) & keyuse->getUsedTables())) // if const item
894
 
  {
895
 
    return new store_key_const_item(session,
896
 
                                    key_part->field,
897
 
                                    key_buff + maybe_null,
898
 
                                    maybe_null ? key_buff : 0,
899
 
                                    key_part->length,
900
 
                                    key_use_val);
901
 
  }
902
 
  else if (key_use_val->type() == Item::FIELD_ITEM ||
903
 
           (key_use_val->type() == Item::REF_ITEM &&
904
 
            key_use_val->ref_type() == Item_ref::OUTER_REF &&
905
 
            (*(Item_ref**)((Item_ref*)key_use_val)->ref)->ref_type() == Item_ref::DIRECT_REF &&
906
 
            key_use_val->real_item()->type() == Item::FIELD_ITEM))
907
 
  {
908
 
    return new store_key_field(session,
909
 
                               key_part->field,
910
 
                               key_buff + maybe_null,
911
 
                               maybe_null ? key_buff : 0,
912
 
                               key_part->length,
913
 
                               ((Item_field*) key_use_val->real_item())->field,
914
 
                               key_use_val->full_name());
915
 
  }
916
 
  return new store_key_item(session,
917
 
                            key_part->field,
918
 
                            key_buff + maybe_null,
919
 
                            maybe_null ? key_buff : 0,
920
 
                            key_part->length,
921
 
                            key_use_val);
922
 
}
 
6282
    uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
 
6283
                             (join_tab->table->s->reclength- rec_length));
 
6284
    rec_length+=(uint) max(4,blob_length);
 
6285
  }
 
6286
  join_tab->used_fields=fields;
 
6287
  join_tab->used_fieldlength=rec_length;
 
6288
  join_tab->used_blobs=blobs;
 
6289
}
 
6290
 
 
6291
 
 
6292
static uint
 
6293
cache_record_length(JOIN *join,uint idx)
 
6294
{
 
6295
  uint length=0;
 
6296
  JOIN_TAB **pos,**end;
 
6297
  THD *thd=join->thd;
 
6298
 
 
6299
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
 
6300
       pos != end ;
 
6301
       pos++)
 
6302
  {
 
6303
    JOIN_TAB *join_tab= *pos;
 
6304
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
6305
      calc_used_field_length(thd, join_tab);
 
6306
    length+=join_tab->used_fieldlength;
 
6307
  }
 
6308
  return length;
 
6309
}
 
6310
 
 
6311
 
 
6312
/*
 
6313
  Get the number of different row combinations for subset of partial join
 
6314
 
 
6315
  SYNOPSIS
 
6316
    prev_record_reads()
 
6317
      join       The join structure
 
6318
      idx        Number of tables in the partial join order (i.e. the
 
6319
                 partial join order is in join->positions[0..idx-1])
 
6320
      found_ref  Bitmap of tables for which we need to find # of distinct
 
6321
                 row combinations.
 
6322
 
 
6323
  DESCRIPTION
 
6324
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
 
6325
    tables within that join order (specified in found_ref), find out how many
 
6326
    distinct row combinations of subset tables will be in the result of the
 
6327
    partial join order.
 
6328
     
 
6329
    This is used as follows: Suppose we have a table accessed with a ref-based
 
6330
    method. The ref access depends on current rows of tables in found_ref.
 
6331
    We want to count # of different ref accesses. We assume two ref accesses
 
6332
    will be different if at least one of access parameters is different.
 
6333
    Example: consider a query
 
6334
 
 
6335
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
 
6336
 
 
6337
    and a join order:
 
6338
      t1,  ref access on t1.key=c1
 
6339
      t2,  ref access on t2.key=c2       
 
6340
      t3,  ref access on t3.key=t1.field 
 
6341
    
 
6342
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
 
6343
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
 
6344
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
 
6345
            n_distinct_ref_scans = #records_read(t1)
 
6346
    
 
6347
    The reason for having this function (at least the latest version of it)
 
6348
    is that we need to account for buffering in join execution. 
 
6349
    
 
6350
    An edge-case example: if we have a non-first table in join accessed via
 
6351
    ref(const) or ref(param) where there is a small number of different
 
6352
    values of param, then the access will likely hit the disk cache and will
 
6353
    not require any disk seeks.
 
6354
    
 
6355
    The proper solution would be to assume an LRU disk cache of some size,
 
6356
    calculate probability of cache hits, etc. For now we just count
 
6357
    identical ref accesses as one.
 
6358
 
 
6359
  RETURN 
 
6360
    Expected number of row combinations
 
6361
*/
 
6362
 
 
6363
static double
 
6364
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
 
6365
{
 
6366
  double found=1.0;
 
6367
  POSITION *pos_end= join->positions - 1;
 
6368
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
 
6369
  {
 
6370
    if (pos->table->table->map & found_ref)
 
6371
    {
 
6372
      found_ref|= pos->ref_depend_map;
 
6373
      /* 
 
6374
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table 
 
6375
        with no matching row we will get position[t2].records_read==0. 
 
6376
        Actually the size of output is one null-complemented row, therefore 
 
6377
        we will use value of 1 whenever we get records_read==0.
 
6378
 
 
6379
        Note
 
6380
        - the above case can't occur if inner part of outer join has more 
 
6381
          than one table: table with no matches will not be marked as const.
 
6382
 
 
6383
        - Ideally we should add 1 to records_read for every possible null-
 
6384
          complemented row. We're not doing it because: 1. it will require
 
6385
          non-trivial code and add overhead. 2. The value of records_read
 
6386
          is an inprecise estimate and adding 1 (or, in the worst case,
 
6387
          #max_nested_outer_joins=64-1) will not make it any more precise.
 
6388
      */
 
6389
      if (pos->records_read > DBL_EPSILON)
 
6390
        found*= pos->records_read;
 
6391
    }
 
6392
  }
 
6393
  return found;
 
6394
}
 
6395
 
923
6396
 
924
6397
/**
925
 
  This function is only called for const items on fields which are keys.
926
 
 
927
 
  @return
928
 
    returns 1 if there was some conversion made when the field was stored.
 
6398
  Set up join struct according to best position.
929
6399
*/
930
 
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
931
 
{
932
 
  bool error;
933
 
  Table *table= field->getTable();
934
 
  Session *session= table->in_use;
935
 
  ha_rows cuted_fields=session->cuted_fields;
936
 
 
937
 
  /*
938
 
    we should restore old value of count_cuted_fields because
939
 
    store_val_in_field can be called from insert_query
940
 
    with select_insert, which make count_cuted_fields= 1
941
 
   */
942
 
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
943
 
  session->count_cuted_fields= check_flag;
944
 
  error= item->save_in_field(field, 1);
945
 
  session->count_cuted_fields= old_count_cuted_fields;
946
 
  return error || cuted_fields != session->cuted_fields;
947
 
}
948
 
 
949
 
inline void add_cond_and_fix(Item **e1, Item *e2)
950
 
{
951
 
  if (*e1)
 
6400
 
 
6401
static bool
 
6402
get_best_combination(JOIN *join)
 
6403
{
 
6404
  uint i,tablenr;
 
6405
  table_map used_tables;
 
6406
  JOIN_TAB *join_tab,*j;
 
6407
  KEYUSE *keyuse;
 
6408
  uint table_count;
 
6409
  THD *thd=join->thd;
 
6410
 
 
6411
  table_count=join->tables;
 
6412
  if (!(join->join_tab=join_tab=
 
6413
        (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
 
6414
    return(true);
 
6415
 
 
6416
  join->full_join=0;
 
6417
 
 
6418
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
 
6419
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
952
6420
  {
953
 
    Item *res;
954
 
    if ((res= new Item_cond_and(*e1, e2)))
 
6421
    TABLE *form;
 
6422
    *j= *join->best_positions[tablenr].table;
 
6423
    form=join->table[tablenr]=j->table;
 
6424
    used_tables|= form->map;
 
6425
    form->reginfo.join_tab=j;
 
6426
    if (!*j->on_expr_ref)
 
6427
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
 
6428
    if (j->type == JT_CONST)
 
6429
      continue;                                 // Handled in make_join_stat..
 
6430
 
 
6431
    j->ref.key = -1;
 
6432
    j->ref.key_parts=0;
 
6433
 
 
6434
    if (j->type == JT_SYSTEM)
 
6435
      continue;
 
6436
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
955
6437
    {
956
 
      *e1= res;
957
 
      res->quick_fix_field();
 
6438
      j->type=JT_ALL;
 
6439
      if (tablenr != join->const_tables)
 
6440
        join->full_join=1;
958
6441
    }
 
6442
    else if (create_ref_for_key(join, j, keyuse, used_tables))
 
6443
      return(true);                        // Something went wrong
959
6444
  }
960
 
  else
961
 
    *e1= e2;
 
6445
 
 
6446
  for (i=0 ; i < table_count ; i++)
 
6447
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
 
6448
  update_depend_map(join);
 
6449
  return(0);
962
6450
}
963
6451
 
964
 
bool create_ref_for_key(Join *join, 
965
 
                        JoinTable *j, 
966
 
                        optimizer::KeyUse *org_keyuse,
967
 
                        table_map used_tables)
 
6452
 
 
6453
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
6454
                               table_map used_tables)
968
6455
{
969
 
  optimizer::KeyUse *keyuse= org_keyuse;
970
 
  Session  *session= join->session;
971
 
  uint32_t keyparts;
972
 
  uint32_t length;
973
 
  uint32_t key;
974
 
  Table *table= NULL;
975
 
  KeyInfo *keyinfo= NULL;
 
6456
  KEYUSE *keyuse=org_keyuse;
 
6457
  THD  *thd= join->thd;
 
6458
  uint keyparts,length,key;
 
6459
  TABLE *table;
 
6460
  KEY *keyinfo;
976
6461
 
977
6462
  /*  Use best key from find_best */
978
 
  table= j->table;
979
 
  key= keyuse->getKey();
980
 
  keyinfo= table->key_info + key;
 
6463
  table=j->table;
 
6464
  key=keyuse->key;
 
6465
  keyinfo=table->key_info+key;
981
6466
 
982
6467
  {
983
 
    keyparts= length= 0;
984
 
    uint32_t found_part_ref_or_null= 0;
 
6468
    keyparts=length=0;
 
6469
    uint found_part_ref_or_null= 0;
985
6470
    /*
986
6471
      Calculate length for the used key
987
6472
      Stop if there is a missing key part or when we find second key_part
989
6474
    */
990
6475
    do
991
6476
    {
992
 
      if (! (~used_tables & keyuse->getUsedTables()))
 
6477
      if (!(~used_tables & keyuse->used_tables))
993
6478
      {
994
 
        if (keyparts == keyuse->getKeypart() &&
995
 
            ! (found_part_ref_or_null & keyuse->getOptimizeFlags()))
996
 
        {
997
 
          keyparts++;
998
 
          length+= keyinfo->key_part[keyuse->getKeypart()].store_length;
999
 
          found_part_ref_or_null|= keyuse->getOptimizeFlags();
1000
 
        }
 
6479
        if (keyparts == keyuse->keypart &&
 
6480
            !(found_part_ref_or_null & keyuse->optimize))
 
6481
        {
 
6482
          keyparts++;
 
6483
          length+= keyinfo->key_part[keyuse->keypart].store_length;
 
6484
          found_part_ref_or_null|= keyuse->optimize;
 
6485
        }
1001
6486
      }
1002
6487
      keyuse++;
1003
 
    } while (keyuse->getTable() == table && keyuse->getKey() == key);
 
6488
    } while (keyuse->table == table && keyuse->key == key);
1004
6489
  }
1005
6490
 
1006
6491
  /* set up fieldref */
1008
6493
  j->ref.key_parts=keyparts;
1009
6494
  j->ref.key_length=length;
1010
6495
  j->ref.key=(int) key;
1011
 
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
1012
 
      !(j->ref.key_copy= (StoredKey**) session->getMemRoot()->allocate((sizeof(StoredKey*) *
1013
 
               (keyparts+1)))) ||
1014
 
      !(j->ref.items=    (Item**) session->getMemRoot()->allocate(sizeof(Item*)*keyparts)) ||
1015
 
      !(j->ref.cond_guards= (bool**) session->getMemRoot()->allocate(sizeof(uint*)*keyparts)))
 
6496
  if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
 
6497
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
 
6498
                                                   (keyparts+1)))) ||
 
6499
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
 
6500
      !(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
1016
6501
  {
1017
6502
    return(true);
1018
6503
  }
1022
6507
  j->ref.disable_cache= false;
1023
6508
  keyuse=org_keyuse;
1024
6509
 
1025
 
  StoredKey **ref_key= j->ref.key_copy;
1026
 
  unsigned char *key_buff= j->ref.key_buff, *null_ref_key= 0;
 
6510
  store_key **ref_key= j->ref.key_copy;
 
6511
  uchar *key_buff=j->ref.key_buff, *null_ref_key= 0;
1027
6512
  bool keyuse_uses_no_tables= true;
1028
6513
  {
1029
 
    for (uint32_t i= 0; i < keyparts; keyuse++, i++)
 
6514
    uint i;
 
6515
    for (i=0 ; i < keyparts ; keyuse++,i++)
1030
6516
    {
1031
 
      while (keyuse->getKeypart() != i ||
1032
 
             ((~used_tables) & keyuse->getUsedTables()))
1033
 
        keyuse++;       /* Skip other parts */
 
6517
      while (keyuse->keypart != i ||
 
6518
             ((~used_tables) & keyuse->used_tables))
 
6519
        keyuse++;                               /* Skip other parts */
1034
6520
 
1035
 
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
1036
 
      j->ref.items[i]= keyuse->getVal();    // Save for cond removal
1037
 
      j->ref.cond_guards[i]= keyuse->getConditionalGuard();
1038
 
      if (keyuse->isNullRejected())
 
6521
      uint maybe_null= test(keyinfo->key_part[i].null_bit);
 
6522
      j->ref.items[i]=keyuse->val;              // Save for cond removal
 
6523
      j->ref.cond_guards[i]= keyuse->cond_guard;
 
6524
      if (keyuse->null_rejecting) 
1039
6525
        j->ref.null_rejecting |= 1 << i;
1040
 
      keyuse_uses_no_tables= keyuse_uses_no_tables && ! keyuse->getUsedTables();
1041
 
      if (! keyuse->getUsedTables() &&  !(join->select_options & SELECT_DESCRIBE))
1042
 
      {         // Compare against constant
1043
 
        store_key_item tmp(session, keyinfo->key_part[i].field,
 
6526
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
 
6527
      if (!keyuse->used_tables &&
 
6528
          !(join->select_options & SELECT_DESCRIBE))
 
6529
      {                                 // Compare against constant
 
6530
        store_key_item tmp(thd, keyinfo->key_part[i].field,
1044
6531
                           key_buff + maybe_null,
1045
6532
                           maybe_null ?  key_buff : 0,
1046
 
                           keyinfo->key_part[i].length, keyuse->getVal());
1047
 
        if (session->is_fatal_error)
1048
 
          return(true);
1049
 
        tmp.copy();
 
6533
                           keyinfo->key_part[i].length, keyuse->val);
 
6534
        if (thd->is_fatal_error)
 
6535
          return(true);
 
6536
        tmp.copy();
1050
6537
      }
1051
6538
      else
1052
 
        *ref_key++= get_store_key(session,
1053
 
          keyuse,join->const_table_map,
1054
 
          &keyinfo->key_part[i],
1055
 
          key_buff, maybe_null);
 
6539
        *ref_key++= get_store_key(thd,
 
6540
                                  keyuse,join->const_table_map,
 
6541
                                  &keyinfo->key_part[i],
 
6542
                                  key_buff, maybe_null);
1056
6543
      /*
1057
 
        Remember if we are going to use REF_OR_NULL
1058
 
        But only if field _really_ can be null i.e. we force AM_REF
1059
 
        instead of AM_REF_OR_NULL in case if field can't be null
 
6544
        Remember if we are going to use REF_OR_NULL
 
6545
        But only if field _really_ can be null i.e. we force JT_REF
 
6546
        instead of JT_REF_OR_NULL in case if field can't be null
1060
6547
      */
1061
 
      if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
1062
 
        null_ref_key= key_buff;
 
6548
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
6549
        null_ref_key= key_buff;
1063
6550
      key_buff+=keyinfo->key_part[i].store_length;
1064
6551
    }
1065
6552
  }
1066
 
  *ref_key= 0;       // end_marker
1067
 
  if (j->type == AM_CONST)
 
6553
  *ref_key=0;                           // end_marker
 
6554
  if (j->type == JT_CONST)
1068
6555
    j->table->const_table= 1;
1069
6556
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
1070
6557
           keyparts != keyinfo->key_parts || null_ref_key)
1071
6558
  {
1072
6559
    /* Must read with repeat */
1073
 
    j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
 
6560
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
1074
6561
    j->ref.null_ref_key= null_ref_key;
1075
6562
  }
1076
6563
  else if (keyuse_uses_no_tables)
1082
6569
      Here we should not mark the table as a 'const' as a field may
1083
6570
      have a 'normal' value or a NULL value.
1084
6571
    */
1085
 
    j->type= AM_CONST;
1086
 
  }
1087
 
  else
1088
 
    j->type= AM_EQ_REF;
1089
 
  return 0;
1090
 
}
 
6572
    j->type=JT_CONST;
 
6573
  }
 
6574
  else
 
6575
    j->type=JT_EQ_REF;
 
6576
  return(0);
 
6577
}
 
6578
 
 
6579
 
 
6580
 
 
6581
static store_key *
 
6582
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
 
6583
              KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
 
6584
{
 
6585
  if (!((~used_tables) & keyuse->used_tables))          // if const item
 
6586
  {
 
6587
    return new store_key_const_item(thd,
 
6588
                                    key_part->field,
 
6589
                                    key_buff + maybe_null,
 
6590
                                    maybe_null ? key_buff : 0,
 
6591
                                    key_part->length,
 
6592
                                    keyuse->val);
 
6593
  }
 
6594
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
 
6595
           (keyuse->val->type() == Item::REF_ITEM &&
 
6596
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
 
6597
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
 
6598
             Item_ref::DIRECT_REF && 
 
6599
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
 
6600
    return new store_key_field(thd,
 
6601
                               key_part->field,
 
6602
                               key_buff + maybe_null,
 
6603
                               maybe_null ? key_buff : 0,
 
6604
                               key_part->length,
 
6605
                               ((Item_field*) keyuse->val->real_item())->field,
 
6606
                               keyuse->val->full_name());
 
6607
  return new store_key_item(thd,
 
6608
                            key_part->field,
 
6609
                            key_buff + maybe_null,
 
6610
                            maybe_null ? key_buff : 0,
 
6611
                            key_part->length,
 
6612
                            keyuse->val);
 
6613
}
 
6614
 
 
6615
/**
 
6616
  This function is only called for const items on fields which are keys.
 
6617
 
 
6618
  @return
 
6619
    returns 1 if there was some conversion made when the field was stored.
 
6620
*/
 
6621
 
 
6622
bool
 
6623
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
6624
{
 
6625
  bool error;
 
6626
  TABLE *table= field->table;
 
6627
  THD *thd= table->in_use;
 
6628
  ha_rows cuted_fields=thd->cuted_fields;
 
6629
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
 
6630
                                                   table->write_set);
 
6631
 
 
6632
  /*
 
6633
    we should restore old value of count_cuted_fields because
 
6634
    store_val_in_field can be called from mysql_insert 
 
6635
    with select_insert, which make count_cuted_fields= 1
 
6636
   */
 
6637
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
 
6638
  thd->count_cuted_fields= check_flag;
 
6639
  error= item->save_in_field(field, 1);
 
6640
  thd->count_cuted_fields= old_count_cuted_fields;
 
6641
  dbug_tmp_restore_column_map(table->write_set, old_map);
 
6642
  return error || cuted_fields != thd->cuted_fields;
 
6643
}
 
6644
 
 
6645
 
 
6646
static bool
 
6647
make_simple_join(JOIN *join,TABLE *tmp_table)
 
6648
{
 
6649
  TABLE **tableptr;
 
6650
  JOIN_TAB *join_tab;
 
6651
 
 
6652
  /*
 
6653
    Reuse TABLE * and JOIN_TAB if already allocated by a previous call
 
6654
    to this function through JOIN::exec (may happen for sub-queries).
 
6655
  */
 
6656
  if (!join->table_reexec)
 
6657
  {
 
6658
    if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*))))
 
6659
      return(true);                        /* purecov: inspected */
 
6660
    if (join->tmp_join)
 
6661
      join->tmp_join->table_reexec= join->table_reexec;
 
6662
  }
 
6663
  if (!join->join_tab_reexec)
 
6664
  {
 
6665
    if (!(join->join_tab_reexec=
 
6666
          (JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
 
6667
      return(true);                        /* purecov: inspected */
 
6668
    if (join->tmp_join)
 
6669
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
 
6670
  }
 
6671
  tableptr= join->table_reexec;
 
6672
  join_tab= join->join_tab_reexec;
 
6673
 
 
6674
  join->join_tab=join_tab;
 
6675
  join->table=tableptr; tableptr[0]=tmp_table;
 
6676
  join->tables=1;
 
6677
  join->const_tables=0;
 
6678
  join->const_table_map=0;
 
6679
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
 
6680
    join->tmp_table_param.func_count=0;
 
6681
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
 
6682
  join->first_record=join->sort_and_group=0;
 
6683
  join->send_records=(ha_rows) 0;
 
6684
  join->group=0;
 
6685
  join->row_limit=join->unit->select_limit_cnt;
 
6686
  join->do_send_rows = (join->row_limit) ? 1 : 0;
 
6687
 
 
6688
  join_tab->cache.buff=0;                       /* No caching */
 
6689
  join_tab->table=tmp_table;
 
6690
  join_tab->select=0;
 
6691
  join_tab->select_cond=0;
 
6692
  join_tab->quick=0;
 
6693
  join_tab->type= JT_ALL;                       /* Map through all records */
 
6694
  join_tab->keys.init();
 
6695
  join_tab->keys.set_all();                     /* test everything in quick */
 
6696
  join_tab->info=0;
 
6697
  join_tab->on_expr_ref=0;
 
6698
  join_tab->last_inner= 0;
 
6699
  join_tab->first_unmatched= 0;
 
6700
  join_tab->ref.key = -1;
 
6701
  join_tab->not_used_in_distinct=0;
 
6702
  join_tab->read_first_record= join_init_read_record;
 
6703
  join_tab->join=join;
 
6704
  join_tab->ref.key_parts= 0;
 
6705
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
 
6706
  join_tab->do_firstmatch= NULL;
 
6707
  bzero((char*) &join_tab->read_record,sizeof(join_tab->read_record));
 
6708
  tmp_table->status=0;
 
6709
  tmp_table->null_row=0;
 
6710
  return(false);
 
6711
}
 
6712
 
 
6713
 
 
6714
inline void add_cond_and_fix(Item **e1, Item *e2)
 
6715
{
 
6716
  if (*e1)
 
6717
  {
 
6718
    Item *res;
 
6719
    if ((res= new Item_cond_and(*e1, e2)))
 
6720
    {
 
6721
      *e1= res;
 
6722
      res->quick_fix_field();
 
6723
    }
 
6724
  }
 
6725
  else
 
6726
    *e1= e2;
 
6727
}
 
6728
 
1091
6729
 
1092
6730
/**
1093
6731
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
1102
6740
    add "t1.field IS NOT NULL" to t1's table condition. @n
1103
6741
 
1104
6742
    Description of the optimization:
1105
 
 
 
6743
    
1106
6744
      We look through equalities choosen to perform ref/eq_ref access,
1107
6745
      pick equalities that have form "tbl.part_of_key = othertbl.field"
1108
6746
      (where othertbl is a non-const table and othertbl.field may be NULL)
1130
6768
      This optimization doesn't affect the choices that ref, range, or join
1131
6769
      optimizer make. This was intentional because this was added after 4.1
1132
6770
      was GA.
1133
 
 
 
6771
      
1134
6772
    Implementation overview
1135
6773
      1. update_ref_and_keys() accumulates info about null-rejecting
1136
 
         predicates in in KeyField::null_rejecting
1137
 
      1.1 add_key_part saves these to KeyUse.
1138
 
      2. create_ref_for_key copies them to table_reference_st.
 
6774
         predicates in in KEY_FIELD::null_rejecting
 
6775
      1.1 add_key_part saves these to KEYUSE.
 
6776
      2. create_ref_for_key copies them to TABLE_REF.
1139
6777
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
1140
 
         appropiate JoinTable members.
 
6778
         appropiate JOIN_TAB members.
1141
6779
*/
1142
 
void add_not_null_conds(Join *join)
 
6780
 
 
6781
static void add_not_null_conds(JOIN *join)
1143
6782
{
1144
 
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
6783
  for (uint i=join->const_tables ; i < join->tables ; i++)
1145
6784
  {
1146
 
    JoinTable *tab=join->join_tab+i;
1147
 
    if ((tab->type == AM_REF || tab->type == AM_EQ_REF ||
1148
 
         tab->type == AM_REF_OR_NULL) &&
 
6785
    JOIN_TAB *tab=join->join_tab+i;
 
6786
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
 
6787
         tab->type == JT_REF_OR_NULL) &&
1149
6788
        !tab->table->maybe_null)
1150
6789
    {
1151
 
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
 
6790
      for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
1152
6791
      {
1153
6792
        if (tab->ref.null_rejecting & (1 << keypart))
1154
6793
        {
1156
6795
          Item *notnull;
1157
6796
          assert(item->type() == Item::FIELD_ITEM);
1158
6797
          Item_field *not_null_item= (Item_field*)item;
1159
 
          JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
 
6798
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
1160
6799
          /*
1161
6800
            For UPDATE queries such as:
1162
6801
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1168
6807
            return;
1169
6808
          /*
1170
6809
            We need to do full fix_fields() call here in order to have correct
1171
 
            notnull->const_item(). This is needed e.g. by test_quick_select
1172
 
            when it is called from make_join_select after this function is
 
6810
            notnull->const_item(). This is needed e.g. by test_quick_select 
 
6811
            when it is called from make_join_select after this function is 
1173
6812
            called.
1174
6813
          */
1175
 
          if (notnull->fix_fields(join->session, &notnull))
 
6814
          if (notnull->fix_fields(join->thd, &notnull))
1176
6815
            return;
1177
6816
          add_cond_and_fix(&referred_tab->select_cond, notnull);
1178
6817
        }
1197
6836
    -  pointer to the guarded predicate, if success
1198
6837
    -  0, otherwise
1199
6838
*/
1200
 
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
 
6839
 
 
6840
static COND*
 
6841
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
1201
6842
{
1202
6843
  COND *tmp;
1203
6844
  assert(cond != 0);
1213
6854
  return tmp;
1214
6855
}
1215
6856
 
 
6857
 
 
6858
/**
 
6859
  Fill in outer join related info for the execution plan structure.
 
6860
 
 
6861
    For each outer join operation left after simplification of the
 
6862
    original query the function set up the following pointers in the linear
 
6863
    structure join->join_tab representing the selected execution plan.
 
6864
    The first inner table t0 for the operation is set to refer to the last
 
6865
    inner table tk through the field t0->last_inner.
 
6866
    Any inner table ti for the operation are set to refer to the first
 
6867
    inner table ti->first_inner.
 
6868
    The first inner table t0 for the operation is set to refer to the
 
6869
    first inner table of the embedding outer join operation, if there is any,
 
6870
    through the field t0->first_upper.
 
6871
    The on expression for the outer join operation is attached to the
 
6872
    corresponding first inner table through the field t0->on_expr_ref.
 
6873
    Here ti are structures of the JOIN_TAB type.
 
6874
 
 
6875
  EXAMPLE. For the query: 
 
6876
  @code
 
6877
        SELECT * FROM t1
 
6878
                      LEFT JOIN
 
6879
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
 
6880
                      ON (t1.a=t2.a AND t1.b=t3.b)
 
6881
          WHERE t1.c > 5,
 
6882
  @endcode
 
6883
 
 
6884
    given the execution plan with the table order t1,t2,t3,t4
 
6885
    is selected, the following references will be set;
 
6886
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
 
6887
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
 
6888
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to 
 
6889
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
 
6890
 
 
6891
  @param join   reference to the info fully describing the query
 
6892
 
 
6893
  @note
 
6894
    The function assumes that the simplification procedure has been
 
6895
    already applied to the join query (see simplify_joins).
 
6896
    This function can be called only after the execution plan
 
6897
    has been chosen.
 
6898
*/
 
6899
 
 
6900
static void
 
6901
make_outerjoin_info(JOIN *join)
 
6902
{
 
6903
  for (uint i=join->const_tables ; i < join->tables ; i++)
 
6904
  {
 
6905
    JOIN_TAB *tab=join->join_tab+i;
 
6906
    TABLE *table=tab->table;
 
6907
    TABLE_LIST *tbl= table->pos_in_table_list;
 
6908
    TABLE_LIST *embedding= tbl->embedding;
 
6909
 
 
6910
    if (tbl->outer_join)
 
6911
    {
 
6912
      /* 
 
6913
        Table tab is the only one inner table for outer join.
 
6914
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
 
6915
        is in the query above.)
 
6916
      */
 
6917
      tab->last_inner= tab->first_inner= tab;
 
6918
      tab->on_expr_ref= &tbl->on_expr;
 
6919
      tab->cond_equal= tbl->cond_equal;
 
6920
      if (embedding)
 
6921
        tab->first_upper= embedding->nested_join->first_nested;
 
6922
    }    
 
6923
    for ( ; embedding ; embedding= embedding->embedding)
 
6924
    {
 
6925
      /* Ignore sj-nests: */
 
6926
      if (!embedding->on_expr)
 
6927
        continue;
 
6928
      NESTED_JOIN *nested_join= embedding->nested_join;
 
6929
      if (!nested_join->counter_)
 
6930
      {
 
6931
        /* 
 
6932
          Table tab is the first inner table for nested_join.
 
6933
          Save reference to it in the nested join structure.
 
6934
        */ 
 
6935
        nested_join->first_nested= tab;
 
6936
        tab->on_expr_ref= &embedding->on_expr;
 
6937
        tab->cond_equal= tbl->cond_equal;
 
6938
        if (embedding->embedding)
 
6939
          tab->first_upper= embedding->embedding->nested_join->first_nested;
 
6940
      }
 
6941
      if (!tab->first_inner)  
 
6942
        tab->first_inner= nested_join->first_nested;
 
6943
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
6944
        break;
 
6945
      /* Table tab is the last inner table for nested join. */
 
6946
      nested_join->first_nested->last_inner= tab;
 
6947
    }
 
6948
  }
 
6949
  return;
 
6950
}
 
6951
 
 
6952
 
 
6953
static bool
 
6954
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
 
6955
{
 
6956
  THD *thd= join->thd;
 
6957
  if (select)
 
6958
  {
 
6959
    add_not_null_conds(join);
 
6960
    table_map used_tables;
 
6961
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
 
6962
    {                        /* there may be a select without a cond. */    
 
6963
      if (join->tables > 1)
 
6964
        cond->update_used_tables();             // Tablenr may have changed
 
6965
      if (join->const_tables == join->tables &&
 
6966
          thd->lex->current_select->master_unit() ==
 
6967
          &thd->lex->unit)              // not upper level SELECT
 
6968
        join->const_table_map|=RAND_TABLE_BIT;
 
6969
      {                                         // Check const tables
 
6970
        COND *const_cond=
 
6971
          make_cond_for_table(cond,
 
6972
                              join->const_table_map,
 
6973
                              (table_map) 0, 1);
 
6974
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
 
6975
             tab < join->join_tab+join->tables ; tab++)
 
6976
        {
 
6977
          if (*tab->on_expr_ref)
 
6978
          {
 
6979
            JOIN_TAB *cond_tab= tab->first_inner;
 
6980
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
 
6981
                                           join->const_table_map,
 
6982
                                           (  table_map) 0, 0);
 
6983
            if (!tmp)
 
6984
              continue;
 
6985
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
6986
            if (!tmp)
 
6987
              return(1);
 
6988
            tmp->quick_fix_field();
 
6989
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
6990
                                    new Item_cond_and(cond_tab->select_cond,
 
6991
                                                      tmp);
 
6992
            if (!cond_tab->select_cond)
 
6993
              return(1);
 
6994
            cond_tab->select_cond->quick_fix_field();
 
6995
          }       
 
6996
        }
 
6997
        if (const_cond && !const_cond->val_int())
 
6998
        {
 
6999
          return(1);     // Impossible const condition
 
7000
        }
 
7001
      }
 
7002
    }
 
7003
    used_tables=((select->const_tables=join->const_table_map) |
 
7004
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7005
    for (uint i=join->const_tables ; i < join->tables ; i++)
 
7006
    {
 
7007
      JOIN_TAB *tab=join->join_tab+i;
 
7008
      /*
 
7009
        first_inner is the X in queries like:
 
7010
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
 
7011
      */
 
7012
      JOIN_TAB *first_inner_tab= tab->first_inner; 
 
7013
      table_map current_map= tab->table->map;
 
7014
      bool use_quick_range=0;
 
7015
      COND *tmp;
 
7016
 
 
7017
      /*
 
7018
        Following force including random expression in last table condition.
 
7019
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
 
7020
      */
 
7021
      if (i == join->tables-1)
 
7022
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
 
7023
      used_tables|=current_map;
 
7024
 
 
7025
      if (tab->type == JT_REF && tab->quick &&
 
7026
          (uint) tab->ref.key == tab->quick->index &&
 
7027
          tab->ref.key_length < tab->quick->max_used_key_length)
 
7028
      {
 
7029
        /* Range uses longer key;  Use this instead of ref on key */
 
7030
        tab->type=JT_ALL;
 
7031
        use_quick_range=1;
 
7032
        tab->use_quick=1;
 
7033
        tab->ref.key= -1;
 
7034
        tab->ref.key_parts=0;           // Don't use ref key.
 
7035
        join->best_positions[i].records_read= rows2double(tab->quick->records);
 
7036
        /* 
 
7037
          We will use join cache here : prevent sorting of the first
 
7038
          table only and sort at the end.
 
7039
        */
 
7040
        if (i != join->const_tables && join->tables > join->const_tables + 1)
 
7041
          join->full_join= 1;
 
7042
      }
 
7043
 
 
7044
      tmp= NULL;
 
7045
      if (cond)
 
7046
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
 
7047
      if (cond && !tmp && tab->quick)
 
7048
      {                                         // Outer join
 
7049
        if (tab->type != JT_ALL)
 
7050
        {
 
7051
          /*
 
7052
            Don't use the quick method
 
7053
            We come here in the case where we have 'key=constant' and
 
7054
            the test is removed by make_cond_for_table()
 
7055
          */
 
7056
          delete tab->quick;
 
7057
          tab->quick= 0;
 
7058
        }
 
7059
        else
 
7060
        {
 
7061
          /*
 
7062
            Hack to handle the case where we only refer to a table
 
7063
            in the ON part of an OUTER JOIN. In this case we want the code
 
7064
            below to check if we should use 'quick' instead.
 
7065
          */
 
7066
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
7067
        }
 
7068
 
 
7069
      }
 
7070
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
 
7071
          tab->type == JT_EQ_REF)
 
7072
      {
 
7073
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
 
7074
                                       thd->memdup((uchar*) select,
 
7075
                                                   sizeof(*select)));
 
7076
        if (!sel)
 
7077
          return(1);                    // End of memory
 
7078
        /*
 
7079
          If tab is an inner table of an outer join operation,
 
7080
          add a match guard to the pushed down predicate.
 
7081
          The guard will turn the predicate on only after
 
7082
          the first match for outer tables is encountered.
 
7083
        */        
 
7084
        if (cond && tmp)
 
7085
        {
 
7086
          /*
 
7087
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
 
7088
            a cond, so neutralize the hack above.
 
7089
          */
 
7090
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
 
7091
            return(1);
 
7092
          tab->select_cond=sel->cond=tmp;
 
7093
          /* Push condition to storage engine if this is enabled
 
7094
             and the condition is not guarded */
 
7095
          tab->table->file->pushed_cond= NULL;
 
7096
          if (thd->variables.engine_condition_pushdown)
 
7097
          {
 
7098
            COND *push_cond= 
 
7099
              make_cond_for_table(tmp, current_map, current_map, 0);
 
7100
            if (push_cond)
 
7101
            {
 
7102
              /* Push condition to handler */
 
7103
              if (!tab->table->file->cond_push(push_cond))
 
7104
                tab->table->file->pushed_cond= push_cond;
 
7105
            }
 
7106
          }
 
7107
        }
 
7108
        else
 
7109
          tab->select_cond= sel->cond= NULL;
 
7110
 
 
7111
        sel->head=tab->table;
 
7112
        if (tab->quick)
 
7113
        {
 
7114
          /* Use quick key read if it's a constant and it's not used
 
7115
             with key reading */
 
7116
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
 
7117
              && (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
 
7118
          {
 
7119
            sel->quick=tab->quick;              // Use value from get_quick_...
 
7120
            sel->quick_keys.clear_all();
 
7121
            sel->needed_reg.clear_all();
 
7122
          }
 
7123
          else
 
7124
          {
 
7125
            delete tab->quick;
 
7126
          }
 
7127
          tab->quick=0;
 
7128
        }
 
7129
        uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
 
7130
        if (i == join->const_tables && ref_key)
 
7131
        {
 
7132
          if (!tab->const_keys.is_clear_all() &&
 
7133
              tab->table->reginfo.impossible_range)
 
7134
            return(1);
 
7135
        }
 
7136
        else if (tab->type == JT_ALL && ! use_quick_range)
 
7137
        {
 
7138
          if (!tab->const_keys.is_clear_all() &&
 
7139
              tab->table->reginfo.impossible_range)
 
7140
            return(1);                          // Impossible range
 
7141
          /*
 
7142
            We plan to scan all rows.
 
7143
            Check again if we should use an index.
 
7144
            We could have used an column from a previous table in
 
7145
            the index if we are using limit and this is the first table
 
7146
          */
 
7147
 
 
7148
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
 
7149
              (!tab->const_keys.is_clear_all() && (i == join->const_tables) && (join->unit->select_limit_cnt < join->best_positions[i].records_read) && ((join->select_options & OPTION_FOUND_ROWS) == false)))
 
7150
          {
 
7151
            /* Join with outer join condition */
 
7152
            COND *orig_cond=sel->cond;
 
7153
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
 
7154
 
 
7155
            /*
 
7156
              We can't call sel->cond->fix_fields,
 
7157
              as it will break tab->on_expr if it's AND condition
 
7158
              (fix_fields currently removes extra AND/OR levels).
 
7159
              Yet attributes of the just built condition are not needed.
 
7160
              Thus we call sel->cond->quick_fix_field for safety.
 
7161
            */
 
7162
            if (sel->cond && !sel->cond->fixed)
 
7163
              sel->cond->quick_fix_field();
 
7164
 
 
7165
            if (sel->test_quick_select(thd, tab->keys,
 
7166
                                       used_tables & ~ current_map,
 
7167
                                       (join->select_options &
 
7168
                                        OPTION_FOUND_ROWS ?
 
7169
                                        HA_POS_ERROR :
 
7170
                                        join->unit->select_limit_cnt), 0,
 
7171
                                        false) < 0)
 
7172
            {
 
7173
              /*
 
7174
                Before reporting "Impossible WHERE" for the whole query
 
7175
                we have to check isn't it only "impossible ON" instead
 
7176
              */
 
7177
              sel->cond=orig_cond;
 
7178
              if (!*tab->on_expr_ref ||
 
7179
                  sel->test_quick_select(thd, tab->keys,
 
7180
                                         used_tables & ~ current_map,
 
7181
                                         (join->select_options &
 
7182
                                          OPTION_FOUND_ROWS ?
 
7183
                                          HA_POS_ERROR :
 
7184
                                          join->unit->select_limit_cnt),0,
 
7185
                                          false) < 0)
 
7186
                return(1);                      // Impossible WHERE
 
7187
            }
 
7188
            else
 
7189
              sel->cond=orig_cond;
 
7190
 
 
7191
            /* Fix for EXPLAIN */
 
7192
            if (sel->quick)
 
7193
              join->best_positions[i].records_read= (double)sel->quick->records;
 
7194
          }
 
7195
          else
 
7196
          {
 
7197
            sel->needed_reg=tab->needed_reg;
 
7198
            sel->quick_keys.clear_all();
 
7199
          }
 
7200
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
 
7201
              !sel->needed_reg.is_subset(tab->checked_keys))
 
7202
          {
 
7203
            tab->keys=sel->quick_keys;
 
7204
            tab->keys.merge(sel->needed_reg);
 
7205
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
 
7206
                             (select->quick_keys.is_clear_all() ||
 
7207
                              (select->quick &&
 
7208
                               (select->quick->records >= 100L)))) ?
 
7209
              2 : 1;
 
7210
            sel->read_tables= used_tables & ~current_map;
 
7211
          }
 
7212
          if (i != join->const_tables && tab->use_quick != 2)
 
7213
          {                                     /* Read with cache */
 
7214
            if (cond &&
 
7215
                (tmp=make_cond_for_table(cond,
 
7216
                                         join->const_table_map |
 
7217
                                         current_map,
 
7218
                                         current_map, 0)))
 
7219
            {
 
7220
              tab->cache.select=(SQL_SELECT*)
 
7221
                thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
 
7222
              tab->cache.select->cond=tmp;
 
7223
              tab->cache.select->read_tables=join->const_table_map;
 
7224
            }
 
7225
          }
 
7226
        }
 
7227
      }
 
7228
      
 
7229
      /* 
 
7230
        Push down conditions from all on expressions.
 
7231
        Each of these conditions are guarded by a variable
 
7232
        that turns if off just before null complemented row for
 
7233
        outer joins is formed. Thus, the condition from an
 
7234
        'on expression' are guaranteed not to be checked for
 
7235
        the null complemented row.
 
7236
      */ 
 
7237
 
 
7238
      /* First push down constant conditions from on expressions */
 
7239
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
 
7240
           join_tab < join->join_tab+join->tables ; join_tab++)
 
7241
      {
 
7242
        if (*join_tab->on_expr_ref)
 
7243
        {
 
7244
          JOIN_TAB *cond_tab= join_tab->first_inner;
 
7245
          COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
 
7246
                                         join->const_table_map,
 
7247
                                         (table_map) 0, 0);
 
7248
          if (!tmp)
 
7249
            continue;
 
7250
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
7251
          if (!tmp)
 
7252
            return(1);
 
7253
          tmp->quick_fix_field();
 
7254
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
7255
                                    new Item_cond_and(cond_tab->select_cond,tmp);
 
7256
          if (!cond_tab->select_cond)
 
7257
            return(1);
 
7258
          cond_tab->select_cond->quick_fix_field();
 
7259
        }       
 
7260
      }
 
7261
 
 
7262
      /* Push down non-constant conditions from on expressions */
 
7263
      JOIN_TAB *last_tab= tab;
 
7264
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
 
7265
      {  
 
7266
        /* 
 
7267
          Table tab is the last inner table of an outer join.
 
7268
          An on expression is always attached to it.
 
7269
        */     
 
7270
        COND *on_expr= *first_inner_tab->on_expr_ref;
 
7271
 
 
7272
        table_map used_tables2= (join->const_table_map |
 
7273
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7274
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
 
7275
        {
 
7276
          current_map= tab->table->map;
 
7277
          used_tables2|= current_map;
 
7278
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
 
7279
                                              current_map, 0);
 
7280
          if (tmp_cond)
 
7281
          {
 
7282
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
 
7283
            /*
 
7284
              First add the guards for match variables of
 
7285
              all embedding outer join operations.
 
7286
            */
 
7287
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
 
7288
                                                     tmp_cond,
 
7289
                                                     first_inner_tab)))
 
7290
              return(1);
 
7291
            /* 
 
7292
              Now add the guard turning the predicate off for 
 
7293
              the null complemented row.
 
7294
            */ 
 
7295
            tmp_cond= new Item_func_trig_cond(tmp_cond,
 
7296
                                              &first_inner_tab->
 
7297
                                              not_null_compl);
 
7298
            if (tmp_cond)
 
7299
              tmp_cond->quick_fix_field();
 
7300
            /* Add the predicate to other pushed down predicates */
 
7301
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
 
7302
                                  new Item_cond_and(cond_tab->select_cond,
 
7303
                                                    tmp_cond);
 
7304
            if (!cond_tab->select_cond)
 
7305
              return(1);
 
7306
            cond_tab->select_cond->quick_fix_field();
 
7307
          }              
 
7308
        }
 
7309
        first_inner_tab= first_inner_tab->first_upper;       
 
7310
      }
 
7311
    }
 
7312
  }
 
7313
  return(0);
 
7314
}
 
7315
 
 
7316
 
 
7317
/* 
 
7318
  Check if given expression uses only table fields covered by the given index
 
7319
 
 
7320
  SYNOPSIS
 
7321
    uses_index_fields_only()
 
7322
      item           Expression to check
 
7323
      tbl            The table having the index
 
7324
      keyno          The index number
 
7325
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7326
 
 
7327
  DESCRIPTION
 
7328
    Check if given expression only uses fields covered by index #keyno in the
 
7329
    table tbl. The expression can use any fields in any other tables.
 
7330
    
 
7331
    The expression is guaranteed not to be AND or OR - those constructs are 
 
7332
    handled outside of this function.
 
7333
 
 
7334
  RETURN
 
7335
    true   Yes
 
7336
    false  No
 
7337
*/
 
7338
 
 
7339
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno, 
 
7340
                            bool other_tbls_ok)
 
7341
{
 
7342
  if (item->const_item())
 
7343
    return true;
 
7344
 
 
7345
  /* 
 
7346
    Don't push down the triggered conditions. Nested outer joins execution 
 
7347
    code may need to evaluate a condition several times (both triggered and
 
7348
    untriggered), and there is no way to put thi
 
7349
    TODO: Consider cloning the triggered condition and using the copies for:
 
7350
      1. push the first copy down, to have most restrictive index condition
 
7351
         possible
 
7352
      2. Put the second copy into tab->select_cond. 
 
7353
  */
 
7354
  if (item->type() == Item::FUNC_ITEM && 
 
7355
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
 
7356
    return false;
 
7357
 
 
7358
  if (!(item->used_tables() & tbl->map))
 
7359
    return other_tbls_ok;
 
7360
 
 
7361
  Item::Type item_type= item->type();
 
7362
  switch (item_type) {
 
7363
  case Item::FUNC_ITEM:
 
7364
    {
 
7365
      /* This is a function, apply condition recursively to arguments */
 
7366
      Item_func *item_func= (Item_func*)item;
 
7367
      Item **child;
 
7368
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
 
7369
      for (child= item_func->arguments(); child != item_end; child++)
 
7370
      {
 
7371
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
 
7372
          return false;
 
7373
      }
 
7374
      return true;
 
7375
    }
 
7376
  case Item::COND_ITEM:
 
7377
    {
 
7378
      /* This is a function, apply condition recursively to arguments */
 
7379
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
 
7380
      Item *item;
 
7381
      while ((item=li++))
 
7382
      {
 
7383
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
 
7384
          return false;
 
7385
      }
 
7386
      return true;
 
7387
    }
 
7388
  case Item::FIELD_ITEM:
 
7389
    {
 
7390
      Item_field *item_field= (Item_field*)item;
 
7391
      if (item_field->field->table != tbl) 
 
7392
        return true;
 
7393
      return item_field->field->part_of_key.is_set(keyno);
 
7394
    }
 
7395
  case Item::REF_ITEM:
 
7396
    return uses_index_fields_only(item->real_item(), tbl, keyno,
 
7397
                                  other_tbls_ok);
 
7398
  default:
 
7399
    return false; /* Play it safe, don't push unknown non-const items */
 
7400
  }
 
7401
}
 
7402
 
 
7403
 
1216
7404
#define ICP_COND_USES_INDEX_ONLY 10
1217
7405
 
1218
 
 
1219
 
/**
1220
 
  cleanup JoinTable.
1221
 
*/
1222
 
void JoinTable::cleanup()
1223
 
{
1224
 
  safe_delete(select);
1225
 
  safe_delete(quick);
1226
 
 
1227
 
  if (cache.buff)
1228
 
  {
1229
 
    size_t size= cache.end - cache.buff;
1230
 
    global_join_buffer.sub(size);
1231
 
    free(cache.buff);
1232
 
  }
 
7406
/*
 
7407
  Get a part of the condition that can be checked using only index fields
 
7408
 
 
7409
  SYNOPSIS
 
7410
    make_cond_for_index()
 
7411
      cond           The source condition
 
7412
      table          The table that is partially available
 
7413
      keyno          The index in the above table. Only fields covered by the index
 
7414
                     are available
 
7415
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7416
 
 
7417
  DESCRIPTION
 
7418
    Get a part of the condition that can be checked when for the given table 
 
7419
    we have values only of fields covered by some index. The condition may
 
7420
    refer to other tables, it is assumed that we have values of all of their 
 
7421
    fields.
 
7422
 
 
7423
    Example:
 
7424
      make_cond_for_index(
 
7425
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
 
7426
          t2, keyno(t2.key1)) 
 
7427
      will return
 
7428
        "cond(t1.field) AND cond(t2.key2)"
 
7429
 
 
7430
  RETURN
 
7431
    Index condition, or NULL if no condition could be inferred.
 
7432
*/
 
7433
 
 
7434
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
 
7435
                          bool other_tbls_ok)
 
7436
{
 
7437
  if (!cond)
 
7438
    return NULL;
 
7439
  if (cond->type() == Item::COND_ITEM)
 
7440
  {
 
7441
    uint n_marked= 0;
 
7442
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7443
    {
 
7444
      Item_cond_and *new_cond=new Item_cond_and;
 
7445
      if (!new_cond)
 
7446
        return (COND*) 0;
 
7447
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7448
      Item *item;
 
7449
      while ((item=li++))
 
7450
      {
 
7451
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7452
        if (fix)
 
7453
          new_cond->argument_list()->push_back(fix);
 
7454
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7455
      }
 
7456
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7457
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7458
      switch (new_cond->argument_list()->elements) {
 
7459
      case 0:
 
7460
        return (COND*) 0;
 
7461
      case 1:
 
7462
        return new_cond->argument_list()->head();
 
7463
      default:
 
7464
        new_cond->quick_fix_field();
 
7465
        return new_cond;
 
7466
      }
 
7467
    }
 
7468
    else /* It's OR */
 
7469
    {
 
7470
      Item_cond_or *new_cond=new Item_cond_or;
 
7471
      if (!new_cond)
 
7472
        return (COND*) 0;
 
7473
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7474
      Item *item;
 
7475
      while ((item=li++))
 
7476
      {
 
7477
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7478
        if (!fix)
 
7479
          return (COND*) 0;
 
7480
        new_cond->argument_list()->push_back(fix);
 
7481
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7482
      }
 
7483
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7484
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7485
      new_cond->quick_fix_field();
 
7486
      new_cond->top_level_item();
 
7487
      return new_cond;
 
7488
    }
 
7489
  }
 
7490
 
 
7491
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
 
7492
    return (COND*) 0;
 
7493
  cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7494
  return cond;
 
7495
}
 
7496
 
 
7497
 
 
7498
Item *make_cond_remainder(Item *cond, bool exclude_index)
 
7499
{
 
7500
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
 
7501
    return 0; /* Already checked */
 
7502
 
 
7503
  if (cond->type() == Item::COND_ITEM)
 
7504
  {
 
7505
    table_map tbl_map= 0;
 
7506
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7507
    {
 
7508
      /* Create new top level AND item */
 
7509
      Item_cond_and *new_cond=new Item_cond_and;
 
7510
      if (!new_cond)
 
7511
        return (COND*) 0;
 
7512
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7513
      Item *item;
 
7514
      while ((item=li++))
 
7515
      {
 
7516
        Item *fix= make_cond_remainder(item, exclude_index);
 
7517
        if (fix)
 
7518
        {
 
7519
          new_cond->argument_list()->push_back(fix);
 
7520
          tbl_map |= fix->used_tables();
 
7521
        }
 
7522
      }
 
7523
      switch (new_cond->argument_list()->elements) {
 
7524
      case 0:
 
7525
        return (COND*) 0;
 
7526
      case 1:
 
7527
        return new_cond->argument_list()->head();
 
7528
      default:
 
7529
        new_cond->quick_fix_field();
 
7530
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7531
        return new_cond;
 
7532
      }
 
7533
    }
 
7534
    else /* It's OR */
 
7535
    {
 
7536
      Item_cond_or *new_cond=new Item_cond_or;
 
7537
      if (!new_cond)
 
7538
        return (COND*) 0;
 
7539
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7540
      Item *item;
 
7541
      while ((item=li++))
 
7542
      {
 
7543
        Item *fix= make_cond_remainder(item, false);
 
7544
        if (!fix)
 
7545
          return (COND*) 0;
 
7546
        new_cond->argument_list()->push_back(fix);
 
7547
        tbl_map |= fix->used_tables();
 
7548
      }
 
7549
      new_cond->quick_fix_field();
 
7550
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7551
      new_cond->top_level_item();
 
7552
      return new_cond;
 
7553
    }
 
7554
  }
 
7555
  return cond;
 
7556
}
 
7557
 
 
7558
 
 
7559
/*
 
7560
  Try to extract and push the index condition
 
7561
 
 
7562
  SYNOPSIS
 
7563
    push_index_cond()
 
7564
      tab            A join tab that has tab->table->file and its condition
 
7565
                     in tab->select_cond
 
7566
      keyno          Index for which extract and push the condition
 
7567
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7568
 
 
7569
  DESCRIPTION
 
7570
    Try to extract and push the index condition down to table handler
 
7571
*/
 
7572
 
 
7573
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
 
7574
{
 
7575
  Item *idx_cond;
 
7576
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
 
7577
      tab->join->thd->variables.engine_condition_pushdown)
 
7578
  {
 
7579
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
 
7580
                                  other_tbls_ok);
 
7581
 
 
7582
    if (idx_cond)
 
7583
    {
 
7584
      tab->pre_idx_push_select_cond= tab->select_cond;
 
7585
      Item *idx_remainder_cond= 
 
7586
        tab->table->file->idx_cond_push(keyno, idx_cond);
 
7587
 
 
7588
      /*
 
7589
        Disable eq_ref's "lookup cache" if we've pushed down an index
 
7590
        condition. 
 
7591
        TODO: This check happens to work on current ICP implementations, but
 
7592
        there may exist a compliant implementation that will not work 
 
7593
        correctly with it. Sort this out when we stabilize the condition
 
7594
        pushdown APIs.
 
7595
      */
 
7596
      if (idx_remainder_cond != idx_cond)
 
7597
        tab->ref.disable_cache= true;
 
7598
 
 
7599
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
 
7600
 
 
7601
      if (row_cond)
 
7602
      {
 
7603
        if (!idx_remainder_cond)
 
7604
          tab->select_cond= row_cond;
 
7605
        else
 
7606
        {
 
7607
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
 
7608
          tab->select_cond->quick_fix_field();
 
7609
          ((Item_cond_and*)tab->select_cond)->used_tables_cache= 
 
7610
            row_cond->used_tables() | idx_remainder_cond->used_tables();
 
7611
        }
 
7612
      }
 
7613
      else
 
7614
        tab->select_cond= idx_remainder_cond;
 
7615
      if (tab->select)
 
7616
      {
 
7617
        tab->select->cond= tab->select_cond;
 
7618
      }
 
7619
    }
 
7620
  }
 
7621
  return;
 
7622
}
 
7623
 
 
7624
 
 
7625
 
 
7626
    /*
 
7627
      Determine if the set is already ordered for ORDER BY, so it can 
 
7628
      disable join cache because it will change the ordering of the results.
 
7629
      Code handles sort table that is at any location (not only first after 
 
7630
      the const tables) despite the fact that it's currently prohibited.
 
7631
      We must disable join cache if the first non-const table alone is
 
7632
      ordered. If there is a temp table the ordering is done as a last
 
7633
      operation and doesn't prevent join cache usage.
 
7634
    */
 
7635
uint make_join_orderinfo(JOIN *join)
 
7636
{
 
7637
  uint i;
 
7638
  if (join->need_tmp)
 
7639
    return join->tables;
 
7640
 
 
7641
  for (i=join->const_tables ; i < join->tables ; i++)
 
7642
  {
 
7643
    JOIN_TAB *tab=join->join_tab+i;
 
7644
    TABLE *table=tab->table;
 
7645
    if ((table == join->sort_by_table && 
 
7646
         (!join->order || join->skip_sort_order)) ||
 
7647
        (join->sort_by_table == (TABLE *) 1 && i != join->const_tables))
 
7648
    {
 
7649
      break;
 
7650
    }
 
7651
  }
 
7652
  return i;
 
7653
}
 
7654
 
 
7655
 
 
7656
/*
 
7657
  Plan refinement stage: do various set ups for the executioner
 
7658
 
 
7659
  SYNOPSIS
 
7660
    make_join_readinfo()
 
7661
      join           Join being processed
 
7662
      options        Join's options (checking for SELECT_DESCRIBE, 
 
7663
                     SELECT_NO_JOIN_CACHE)
 
7664
      no_jbuf_after  Don't use join buffering after table with this number.
 
7665
 
 
7666
  DESCRIPTION
 
7667
    Plan refinement stage: do various set ups for the executioner
 
7668
      - set up use of join buffering
 
7669
      - push index conditions
 
7670
      - increment counters
 
7671
      - etc
 
7672
 
 
7673
  RETURN 
 
7674
    false - OK
 
7675
    true  - Out of memory
 
7676
*/
 
7677
 
 
7678
static bool
 
7679
make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after)
 
7680
{
 
7681
  uint i;
 
7682
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
 
7683
  bool sorted= 1;
 
7684
 
 
7685
  for (i=join->const_tables ; i < join->tables ; i++)
 
7686
  {
 
7687
    JOIN_TAB *tab=join->join_tab+i;
 
7688
    TABLE *table=tab->table;
 
7689
    bool using_join_cache;
 
7690
    tab->read_record.table= table;
 
7691
    tab->read_record.file=table->file;
 
7692
    tab->next_select=sub_select;                /* normal select */
 
7693
    /* 
 
7694
      TODO: don't always instruct first table's ref/range access method to 
 
7695
      produce sorted output.
 
7696
    */
 
7697
    tab->sorted= sorted;
 
7698
    sorted= 0;                                  // only first must be sorted
 
7699
    if (tab->insideout_match_tab)
 
7700
    {
 
7701
      if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
 
7702
                                                         [tab->index].
 
7703
                                                         key_length)))
 
7704
        return true;
 
7705
    }
 
7706
    switch (tab->type) {
 
7707
    case JT_SYSTEM:                             // Only happens with left join
 
7708
      table->status=STATUS_NO_RECORD;
 
7709
      tab->read_first_record= join_read_system;
 
7710
      tab->read_record.read_record= join_no_more_records;
 
7711
      break;
 
7712
    case JT_CONST:                              // Only happens with left join
 
7713
      table->status=STATUS_NO_RECORD;
 
7714
      tab->read_first_record= join_read_const;
 
7715
      tab->read_record.read_record= join_no_more_records;
 
7716
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7717
          !table->no_keyread)
 
7718
      {
 
7719
        table->key_read=1;
 
7720
        table->file->extra(HA_EXTRA_KEYREAD);
 
7721
      }
 
7722
      break;
 
7723
    case JT_EQ_REF:
 
7724
      table->status=STATUS_NO_RECORD;
 
7725
      if (tab->select)
 
7726
      {
 
7727
        delete tab->select->quick;
 
7728
        tab->select->quick=0;
 
7729
      }
 
7730
      delete tab->quick;
 
7731
      tab->quick=0;
 
7732
      tab->read_first_record= join_read_key;
 
7733
      tab->read_record.read_record= join_no_more_records;
 
7734
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7735
          !table->no_keyread)
 
7736
      {
 
7737
        table->key_read=1;
 
7738
        table->file->extra(HA_EXTRA_KEYREAD);
 
7739
      }
 
7740
      else
 
7741
        push_index_cond(tab, tab->ref.key, true);
 
7742
      break;
 
7743
    case JT_REF_OR_NULL:
 
7744
    case JT_REF:
 
7745
      table->status=STATUS_NO_RECORD;
 
7746
      if (tab->select)
 
7747
      {
 
7748
        delete tab->select->quick;
 
7749
        tab->select->quick=0;
 
7750
      }
 
7751
      delete tab->quick;
 
7752
      tab->quick=0;
 
7753
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7754
          !table->no_keyread)
 
7755
      {
 
7756
        table->key_read=1;
 
7757
        table->file->extra(HA_EXTRA_KEYREAD);
 
7758
      }
 
7759
      else
 
7760
        push_index_cond(tab, tab->ref.key, true);
 
7761
      if (tab->type == JT_REF)
 
7762
      {
 
7763
        tab->read_first_record= join_read_always_key;
 
7764
        tab->read_record.read_record= tab->insideout_match_tab? 
 
7765
           join_read_next_same_diff : join_read_next_same;
 
7766
      }
 
7767
      else
 
7768
      {
 
7769
        tab->read_first_record= join_read_always_key_or_null;
 
7770
        tab->read_record.read_record= join_read_next_same_or_null;
 
7771
      }
 
7772
      break;
 
7773
    case JT_ALL:
 
7774
      /*
 
7775
        If previous table use cache
 
7776
        If the incoming data set is already sorted don't use cache.
 
7777
      */
 
7778
      table->status=STATUS_NO_RECORD;
 
7779
      using_join_cache= false;
 
7780
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
7781
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
 
7782
          !tab->insideout_match_tab)
 
7783
      {
 
7784
        if ((options & SELECT_DESCRIBE) ||
 
7785
            !join_init_cache(join->thd,join->join_tab+join->const_tables,
 
7786
                             i-join->const_tables))
 
7787
        {
 
7788
          using_join_cache= true;
 
7789
          tab[-1].next_select=sub_select_cache; /* Patch previous */
 
7790
        }
 
7791
      }
 
7792
      /* These init changes read_record */
 
7793
      if (tab->use_quick == 2)
 
7794
      {
 
7795
        join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
 
7796
        tab->read_first_record= join_init_quick_read_record;
 
7797
        if (statistics)
 
7798
          status_var_increment(join->thd->status_var.select_range_check_count);
 
7799
      }
 
7800
      else
 
7801
      {
 
7802
        tab->read_first_record= join_init_read_record;
 
7803
        if (i == join->const_tables)
 
7804
        {
 
7805
          if (tab->select && tab->select->quick)
 
7806
          {
 
7807
            if (statistics)
 
7808
              status_var_increment(join->thd->status_var.select_range_count);
 
7809
          }
 
7810
          else
 
7811
          {
 
7812
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7813
            if (statistics)
 
7814
              status_var_increment(join->thd->status_var.select_scan_count);
 
7815
          }
 
7816
        }
 
7817
        else
 
7818
        {
 
7819
          if (tab->select && tab->select->quick)
 
7820
          {
 
7821
            if (statistics)
 
7822
              status_var_increment(join->thd->status_var.select_full_range_join_count);
 
7823
          }
 
7824
          else
 
7825
          {
 
7826
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7827
            if (statistics)
 
7828
              status_var_increment(join->thd->status_var.select_full_join_count);
 
7829
          }
 
7830
        }
 
7831
        if (!table->no_keyread)
 
7832
        {
 
7833
          if (tab->select && tab->select->quick &&
 
7834
              tab->select->quick->index != MAX_KEY && //not index_merge
 
7835
              table->covering_keys.is_set(tab->select->quick->index))
 
7836
          {
 
7837
            table->key_read=1;
 
7838
            table->file->extra(HA_EXTRA_KEYREAD);
 
7839
          }
 
7840
          else if (!table->covering_keys.is_clear_all() &&
 
7841
                   !(tab->select && tab->select->quick))
 
7842
          {                                     // Only read index tree
 
7843
            if (!tab->insideout_match_tab)
 
7844
            {
 
7845
              /*
 
7846
                See bug #26447: "Using the clustered index for a table scan
 
7847
                is always faster than using a secondary index".
 
7848
              */
 
7849
              if (table->s->primary_key != MAX_KEY &&
 
7850
                  table->file->primary_key_is_clustered())
 
7851
                tab->index= table->s->primary_key;
 
7852
              else
 
7853
                tab->index=find_shortest_key(table, & table->covering_keys);
 
7854
            }
 
7855
            tab->read_first_record= join_read_first;
 
7856
            tab->type=JT_NEXT;          // Read with index_first / index_next
 
7857
          }
 
7858
        }
 
7859
        if (tab->select && tab->select->quick &&
 
7860
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
 
7861
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
 
7862
      }
 
7863
      break;
 
7864
    default:
 
7865
      break;                                    /* purecov: deadcode */
 
7866
    case JT_UNKNOWN:
 
7867
    case JT_MAYBE_REF:
 
7868
      abort();                                  /* purecov: deadcode */
 
7869
    }
 
7870
  }
 
7871
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
 
7872
  return(false);
 
7873
}
 
7874
 
 
7875
 
 
7876
/**
 
7877
  Give error if we some tables are done with a full join.
 
7878
 
 
7879
  This is used by multi_table_update and multi_table_delete when running
 
7880
  in safe mode.
 
7881
 
 
7882
  @param join           Join condition
 
7883
 
 
7884
  @retval
 
7885
    0   ok
 
7886
  @retval
 
7887
    1   Error (full join used)
 
7888
*/
 
7889
 
 
7890
bool error_if_full_join(JOIN *join)
 
7891
{
 
7892
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
 
7893
       tab < end;
 
7894
       tab++)
 
7895
  {
 
7896
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
 
7897
    {
 
7898
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
 
7899
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
 
7900
      return(1);
 
7901
    }
 
7902
  }
 
7903
  return(0);
 
7904
}
 
7905
 
 
7906
 
 
7907
/**
 
7908
  cleanup JOIN_TAB.
 
7909
*/
 
7910
 
 
7911
void JOIN_TAB::cleanup()
 
7912
{
 
7913
  delete select;
 
7914
  select= 0;
 
7915
  delete quick;
 
7916
  quick= 0;
 
7917
  x_free(cache.buff);
1233
7918
  cache.buff= 0;
1234
7919
  limit= 0;
1235
7920
  if (table)
1237
7922
    if (table->key_read)
1238
7923
    {
1239
7924
      table->key_read= 0;
1240
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
7925
      table->file->extra(HA_EXTRA_NO_KEYREAD);
1241
7926
    }
1242
 
    table->cursor->ha_index_or_rnd_end();
 
7927
    table->file->ha_index_or_rnd_end();
1243
7928
    /*
1244
7929
      We need to reset this for next select
1245
7930
      (Tested in part_of_refkey)
1246
7931
    */
1247
7932
    table->reginfo.join_tab= 0;
1248
7933
  }
1249
 
  read_record.end_read_record();
1250
 
}
1251
 
 
1252
 
bool only_eq_ref_tables(Join *join,Order *order,table_map tables)
1253
 
{
1254
 
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
1255
 
  {
1256
 
    if (tables & 1 && !eq_ref_table(join, order, *tab))
1257
 
      return 0;
1258
 
  }
1259
 
  return 1;
1260
 
}
 
7934
  end_read_record(&read_record);
 
7935
}
 
7936
 
 
7937
 
 
7938
/**
 
7939
  Partially cleanup JOIN after it has executed: close index or rnd read
 
7940
  (table cursors), free quick selects.
 
7941
 
 
7942
    This function is called in the end of execution of a JOIN, before the used
 
7943
    tables are unlocked and closed.
 
7944
 
 
7945
    For a join that is resolved using a temporary table, the first sweep is
 
7946
    performed against actual tables and an intermediate result is inserted
 
7947
    into the temprorary table.
 
7948
    The last sweep is performed against the temporary table. Therefore,
 
7949
    the base tables and associated buffers used to fill the temporary table
 
7950
    are no longer needed, and this function is called to free them.
 
7951
 
 
7952
    For a join that is performed without a temporary table, this function
 
7953
    is called after all rows are sent, but before EOF packet is sent.
 
7954
 
 
7955
    For a simple SELECT with no subqueries this function performs a full
 
7956
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
 
7957
    tables.
 
7958
 
 
7959
    If a JOIN is executed for a subquery or if it has a subquery, we can't
 
7960
    do the full cleanup and need to do a partial cleanup only.
 
7961
    - If a JOIN is not the top level join, we must not unlock the tables
 
7962
    because the outer select may not have been evaluated yet, and we
 
7963
    can't unlock only selected tables of a query.
 
7964
    - Additionally, if this JOIN corresponds to a correlated subquery, we
 
7965
    should not free quick selects and join buffers because they will be
 
7966
    needed for the next execution of the correlated subquery.
 
7967
    - However, if this is a JOIN for a [sub]select, which is not
 
7968
    a correlated subquery itself, but has subqueries, we can free it
 
7969
    fully and also free JOINs of all its subqueries. The exception
 
7970
    is a subquery in SELECT list, e.g: @n
 
7971
    SELECT a, (select max(b) from t1) group by c @n
 
7972
    This subquery will not be evaluated at first sweep and its value will
 
7973
    not be inserted into the temporary table. Instead, it's evaluated
 
7974
    when selecting from the temporary table. Therefore, it can't be freed
 
7975
    here even though it's not correlated.
 
7976
 
 
7977
  @todo
 
7978
    Unlock tables even if the join isn't top level select in the tree
 
7979
*/
 
7980
 
 
7981
void JOIN::join_free()
 
7982
{
 
7983
  SELECT_LEX_UNIT *tmp_unit;
 
7984
  SELECT_LEX *sl;
 
7985
  /*
 
7986
    Optimization: if not EXPLAIN and we are done with the JOIN,
 
7987
    free all tables.
 
7988
  */
 
7989
  bool full= (!select_lex->uncacheable && !thd->lex->describe);
 
7990
  bool can_unlock= full;
 
7991
 
 
7992
  cleanup(full);
 
7993
 
 
7994
  for (tmp_unit= select_lex->first_inner_unit();
 
7995
       tmp_unit;
 
7996
       tmp_unit= tmp_unit->next_unit())
 
7997
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
 
7998
    {
 
7999
      Item_subselect *subselect= sl->master_unit()->item;
 
8000
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
8001
      /*
 
8002
        If this join is evaluated, we can fully clean it up and clean up all
 
8003
        its underlying joins even if they are correlated -- they will not be
 
8004
        used any more anyway.
 
8005
        If this join is not yet evaluated, we still must clean it up to
 
8006
        close its table cursors -- it may never get evaluated, as in case of
 
8007
        ... HAVING false OR a IN (SELECT ...))
 
8008
        but all table cursors must be closed before the unlock.
 
8009
      */
 
8010
      sl->cleanup_all_joins(full_local);
 
8011
      /* Can't unlock if at least one JOIN is still needed */
 
8012
      can_unlock= can_unlock && full_local;
 
8013
    }
 
8014
 
 
8015
  /*
 
8016
    We are not using tables anymore
 
8017
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8018
  */
 
8019
  if (can_unlock && lock && thd->lock &&
 
8020
      !(select_options & SELECT_NO_UNLOCK) &&
 
8021
      !select_lex->subquery_in_having &&
 
8022
      (select_lex == (thd->lex->unit.fake_select_lex ?
 
8023
                      thd->lex->unit.fake_select_lex : &thd->lex->select_lex)))
 
8024
  {
 
8025
    /*
 
8026
      TODO: unlock tables even if the join isn't top level select in the
 
8027
      tree.
 
8028
    */
 
8029
    mysql_unlock_read_tables(thd, lock);           // Don't free join->lock
 
8030
    lock= 0;
 
8031
  }
 
8032
 
 
8033
  return;
 
8034
}
 
8035
 
 
8036
 
 
8037
/**
 
8038
  Free resources of given join.
 
8039
 
 
8040
  @param fill   true if we should free all resources, call with full==1
 
8041
                should be last, before it this function can be called with
 
8042
                full==0
 
8043
 
 
8044
  @note
 
8045
    With subquery this function definitely will be called several times,
 
8046
    but even for simple query it can be called several times.
 
8047
*/
 
8048
 
 
8049
void JOIN::cleanup(bool full)
 
8050
{
 
8051
  if (table)
 
8052
  {
 
8053
    JOIN_TAB *tab,*end;
 
8054
    /*
 
8055
      Only a sorted table may be cached.  This sorted table is always the
 
8056
      first non const table in join->table
 
8057
    */
 
8058
    if (tables > const_tables) // Test for not-const tables
 
8059
    {
 
8060
      free_io_cache(table[const_tables]);
 
8061
      filesort_free_buffers(table[const_tables],full);
 
8062
    }
 
8063
 
 
8064
    if (full)
 
8065
    {
 
8066
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8067
        tab->cleanup();
 
8068
      table= 0;
 
8069
    }
 
8070
    else
 
8071
    {
 
8072
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8073
      {
 
8074
        if (tab->table)
 
8075
          tab->table->file->ha_index_or_rnd_end();
 
8076
      }
 
8077
    }
 
8078
    cleanup_sj_tmp_tables(this);//
 
8079
  }
 
8080
  /*
 
8081
    We are not using tables anymore
 
8082
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8083
  */
 
8084
  if (full)
 
8085
  {
 
8086
    if (tmp_join)
 
8087
      tmp_table_param.copy_field= 0;
 
8088
    group_fields.delete_elements();
 
8089
    /*
 
8090
      We can't call delete_elements() on copy_funcs as this will cause
 
8091
      problems in free_elements() as some of the elements are then deleted.
 
8092
    */
 
8093
    tmp_table_param.copy_funcs.empty();
 
8094
    /*
 
8095
      If we have tmp_join and 'this' JOIN is not tmp_join and
 
8096
      tmp_table_param.copy_field's  of them are equal then we have to remove
 
8097
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
 
8098
      be removed in tmp_table_param.cleanup().
 
8099
    */
 
8100
    if (tmp_join &&
 
8101
        tmp_join != this &&
 
8102
        tmp_join->tmp_table_param.copy_field ==
 
8103
        tmp_table_param.copy_field)
 
8104
    {
 
8105
      tmp_join->tmp_table_param.copy_field=
 
8106
        tmp_join->tmp_table_param.save_copy_field= 0;
 
8107
    }
 
8108
    tmp_table_param.cleanup();
 
8109
  }
 
8110
  return;
 
8111
}
 
8112
 
1261
8113
 
1262
8114
/**
1263
8115
  Remove the following expressions from ORDER BY and GROUP BY:
1264
8116
  Constant expressions @n
1265
8117
  Expression that only uses tables that are of type EQ_REF and the reference
1266
 
  is in the order_st list or if all refereed tables are of the above type.
 
8118
  is in the ORDER list or if all refereed tables are of the above type.
1267
8119
 
1268
8120
  In the following, the X field can be removed:
1269
8121
  @code
1278
8130
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
1279
8131
  @endcode
1280
8132
*/
1281
 
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
 
8133
 
 
8134
static bool
 
8135
eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab)
1282
8136
{
1283
8137
  if (tab->cached_eq_ref_table)                 // If cached
1284
8138
    return tab->eq_ref_table;
1285
8139
  tab->cached_eq_ref_table=1;
1286
8140
  /* We can skip const tables only if not an outer table */
1287
 
  if (tab->type == AM_CONST && !tab->first_inner)
1288
 
    return (tab->eq_ref_table=1);
1289
 
  if (tab->type != AM_EQ_REF || tab->table->maybe_null)
 
8141
  if (tab->type == JT_CONST && !tab->first_inner)
 
8142
    return (tab->eq_ref_table=1);               /* purecov: inspected */
 
8143
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
1290
8144
    return (tab->eq_ref_table=0);               // We must use this
1291
8145
  Item **ref_item=tab->ref.items;
1292
8146
  Item **end=ref_item+tab->ref.key_parts;
1293
 
  uint32_t found=0;
 
8147
  uint found=0;
1294
8148
  table_map map=tab->table->map;
1295
8149
 
1296
8150
  for (; ref_item != end ; ref_item++)
1297
8151
  {
1298
8152
    if (! (*ref_item)->const_item())
1299
8153
    {                                           // Not a const ref
1300
 
      Order *order;
 
8154
      ORDER *order;
1301
8155
      for (order=start_order ; order ; order=order->next)
1302
8156
      {
1303
 
        if ((*ref_item)->eq(order->item[0],0))
1304
 
          break;
 
8157
        if ((*ref_item)->eq(order->item[0],0))
 
8158
          break;
1305
8159
      }
1306
8160
      if (order)
1307
8161
      {
1308
 
        found++;
1309
 
        assert(!(order->used & map));
1310
 
        order->used|=map;
1311
 
        continue;                               // Used in order_st BY
 
8162
        found++;
 
8163
        assert(!(order->used & map));
 
8164
        order->used|=map;
 
8165
        continue;                               // Used in ORDER BY
1312
8166
      }
1313
8167
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1314
 
        return (tab->eq_ref_table= 0);
 
8168
        return (tab->eq_ref_table=0);
1315
8169
    }
1316
8170
  }
1317
8171
  /* Check that there was no reference to table before sort order */
1323
8177
      continue;
1324
8178
    }
1325
8179
    if (start_order->depend_map & map)
1326
 
      return (tab->eq_ref_table= 0);
1327
 
  }
1328
 
  return tab->eq_ref_table= 1;
1329
 
}
 
8180
      return (tab->eq_ref_table=0);
 
8181
  }
 
8182
  return tab->eq_ref_table=1;
 
8183
}
 
8184
 
 
8185
 
 
8186
static bool
 
8187
only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables)
 
8188
{
 
8189
  if (specialflag &  SPECIAL_SAFE_MODE)
 
8190
    return 0;                   // skip this optimize /* purecov: inspected */
 
8191
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
 
8192
  {
 
8193
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
8194
      return 0;
 
8195
  }
 
8196
  return 1;
 
8197
}
 
8198
 
 
8199
 
 
8200
/** Update the dependency map for the tables. */
 
8201
 
 
8202
static void update_depend_map(JOIN *join)
 
8203
{
 
8204
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
 
8205
 
 
8206
  for (; join_tab != end ; join_tab++)
 
8207
  {
 
8208
    TABLE_REF *ref= &join_tab->ref;
 
8209
    table_map depend_map=0;
 
8210
    Item **item=ref->items;
 
8211
    uint i;
 
8212
    for (i=0 ; i < ref->key_parts ; i++,item++)
 
8213
      depend_map|=(*item)->used_tables();
 
8214
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
 
8215
    depend_map&= ~OUTER_REF_TABLE_BIT;
 
8216
    for (JOIN_TAB **tab=join->map2table;
 
8217
         depend_map ;
 
8218
         tab++,depend_map>>=1 )
 
8219
    {
 
8220
      if (depend_map & 1)
 
8221
        ref->depend_map|=(*tab)->ref.depend_map;
 
8222
    }
 
8223
  }
 
8224
}
 
8225
 
 
8226
 
 
8227
/** Update the dependency map for the sort order. */
 
8228
 
 
8229
static void update_depend_map(JOIN *join, ORDER *order)
 
8230
{
 
8231
  for (; order ; order=order->next)
 
8232
  {
 
8233
    table_map depend_map;
 
8234
    order->item[0]->update_used_tables();
 
8235
    order->depend_map=depend_map=order->item[0]->used_tables();
 
8236
    // Not item_sum(), RAND() and no reference to table outside of sub select
 
8237
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
 
8238
        && !order->item[0]->with_sum_func)
 
8239
    {
 
8240
      for (JOIN_TAB **tab=join->map2table;
 
8241
           depend_map ;
 
8242
           tab++, depend_map>>=1)
 
8243
      {
 
8244
        if (depend_map & 1)
 
8245
          order->depend_map|=(*tab)->ref.depend_map;
 
8246
      }
 
8247
    }
 
8248
  }
 
8249
}
 
8250
 
 
8251
 
 
8252
/**
 
8253
  Remove all constants and check if ORDER only contains simple
 
8254
  expressions.
 
8255
 
 
8256
  simple_order is set to 1 if sort_order only uses fields from head table
 
8257
  and the head table is not a LEFT JOIN table.
 
8258
 
 
8259
  @param join                   Join handler
 
8260
  @param first_order            List of SORT or GROUP order
 
8261
  @param cond                   WHERE statement
 
8262
  @param change_list            Set to 1 if we should remove things from list.
 
8263
                               If this is not set, then only simple_order is
 
8264
                               calculated.
 
8265
  @param simple_order           Set to 1 if we are only using simple expressions
 
8266
 
 
8267
  @return
 
8268
    Returns new sort order
 
8269
*/
 
8270
 
 
8271
static ORDER *
 
8272
remove_const(JOIN *join,ORDER *first_order, COND *cond,
 
8273
             bool change_list, bool *simple_order)
 
8274
{
 
8275
  if (join->tables == join->const_tables)
 
8276
    return change_list ? 0 : first_order;               // No need to sort
 
8277
 
 
8278
  ORDER *order,**prev_ptr;
 
8279
  table_map first_table= join->join_tab[join->const_tables].table->map;
 
8280
  table_map not_const_tables= ~join->const_table_map;
 
8281
  table_map ref;
 
8282
 
 
8283
  prev_ptr= &first_order;
 
8284
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
 
8285
 
 
8286
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
 
8287
 
 
8288
  update_depend_map(join, first_order);
 
8289
  for (order=first_order; order ; order=order->next)
 
8290
  {
 
8291
    table_map order_tables=order->item[0]->used_tables();
 
8292
    if (order->item[0]->with_sum_func)
 
8293
      *simple_order=0;                          // Must do a temp table to sort
 
8294
    else if (!(order_tables & not_const_tables))
 
8295
    {
 
8296
      if (order->item[0]->with_subselect)
 
8297
        order->item[0]->val_str(&order->item[0]->str_value);
 
8298
      continue;                                 // skip const item
 
8299
    }
 
8300
    else
 
8301
    {
 
8302
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
 
8303
        *simple_order=0;
 
8304
      else
 
8305
      {
 
8306
        Item *comp_item=0;
 
8307
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
 
8308
        {
 
8309
          continue;
 
8310
        }
 
8311
        if ((ref=order_tables & (not_const_tables ^ first_table)))
 
8312
        {
 
8313
          if (!(order_tables & first_table) &&
 
8314
              only_eq_ref_tables(join,first_order, ref))
 
8315
          {
 
8316
            continue;
 
8317
          }
 
8318
          *simple_order=0;                      // Must do a temp table to sort
 
8319
        }
 
8320
      }
 
8321
    }
 
8322
    if (change_list)
 
8323
      *prev_ptr= order;                         // use this entry
 
8324
    prev_ptr= &order->next;
 
8325
  }
 
8326
  if (change_list)
 
8327
    *prev_ptr=0;
 
8328
  if (prev_ptr == &first_order)                 // Nothing to sort/group
 
8329
    *simple_order=1;
 
8330
  return(first_order);
 
8331
}
 
8332
 
 
8333
 
 
8334
static int
 
8335
return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables,
 
8336
                 List<Item> &fields, bool send_row, uint64_t select_options,
 
8337
                 const char *info, Item *having)
 
8338
{
 
8339
  if (select_options & SELECT_DESCRIBE)
 
8340
  {
 
8341
    select_describe(join, false, false, false, info);
 
8342
    return(0);
 
8343
  }
 
8344
 
 
8345
  join->join_free();
 
8346
 
 
8347
  if (send_row)
 
8348
  {
 
8349
    for (TABLE_LIST *table= tables; table; table= table->next_leaf)
 
8350
      mark_as_null_row(table->table);           // All fields are NULL
 
8351
    if (having && having->val_int() == 0)
 
8352
      send_row=0;
 
8353
  }
 
8354
  if (!(result->send_fields(fields,
 
8355
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
 
8356
  {
 
8357
    if (send_row)
 
8358
    {
 
8359
      List_iterator_fast<Item> it(fields);
 
8360
      Item *item;
 
8361
      while ((item= it++))
 
8362
        item->no_rows_in_result();
 
8363
      result->send_data(fields);
 
8364
    }
 
8365
    result->send_eof();                         // Should be safe
 
8366
  }
 
8367
  /* Update results for FOUND_ROWS */
 
8368
  join->thd->limit_found_rows= join->thd->examined_row_count= 0;
 
8369
  return(0);
 
8370
}
 
8371
 
 
8372
/*
 
8373
  used only in JOIN::clear
 
8374
*/
 
8375
static void clear_tables(JOIN *join)
 
8376
{
 
8377
  /* 
 
8378
    must clear only the non-const tables, as const tables
 
8379
    are not re-calculated.
 
8380
  */
 
8381
  for (uint i=join->const_tables ; i < join->tables ; i++)
 
8382
    mark_as_null_row(join->table[i]);           // All fields are NULL
 
8383
}
 
8384
 
 
8385
/*****************************************************************************
 
8386
  Make som simple condition optimization:
 
8387
  If there is a test 'field = const' change all refs to 'field' to 'const'
 
8388
  Remove all dummy tests 'item = item', 'const op const'.
 
8389
  Remove all 'item is NULL', when item can never be null!
 
8390
  item->marker should be 0 for all items on entry
 
8391
  Return in cond_value false if condition is impossible (1 = 2)
 
8392
*****************************************************************************/
 
8393
 
 
8394
class COND_CMP :public ilink {
 
8395
public:
 
8396
  static void *operator new(size_t size)
 
8397
  {
 
8398
    return (void*) sql_alloc((uint) size);
 
8399
  }
 
8400
  static void operator delete(void *ptr __attribute__((unused)),
 
8401
                              size_t size __attribute__((unused)))
 
8402
  { TRASH(ptr, size); }
 
8403
 
 
8404
  Item *and_level;
 
8405
  Item_func *cmp_func;
 
8406
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
 
8407
};
 
8408
 
 
8409
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
8410
template class I_List<COND_CMP>;
 
8411
template class I_List_iterator<COND_CMP>;
 
8412
#endif
 
8413
 
1330
8414
 
1331
8415
/**
1332
8416
  Find the multiple equality predicate containing a field.
1346
8430
    - Item_equal for the found multiple equality predicate if a success;
1347
8431
    - NULL otherwise.
1348
8432
*/
1349
 
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
 
8433
 
 
8434
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
 
8435
                            bool *inherited_fl)
1350
8436
{
1351
8437
  Item_equal *item= 0;
1352
8438
  bool in_upper_level= false;
1353
8439
  while (cond_equal)
1354
8440
  {
1355
 
    List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
8441
    List_iterator_fast<Item_equal> li(cond_equal->current_level);
1356
8442
    while ((item= li++))
1357
8443
    {
1358
8444
      if (item->contains(field))
1367
8453
  return item;
1368
8454
}
1369
8455
 
 
8456
  
1370
8457
/**
1371
8458
  Check whether an equality can be used to build multiple equalities.
1372
8459
 
1393
8480
    the check_equality will be called for the following equality
1394
8481
    predicates a=b, b=c, b=2 and f=e.
1395
8482
    - For a=b it will be called with *cond_equal=(0,[]) and will transform
1396
 
      *cond_equal into (0,[Item_equal(a,b)]).
 
8483
      *cond_equal into (0,[Item_equal(a,b)]). 
1397
8484
    - For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)])
1398
8485
      and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]).
1399
8486
    - For b=2 it will be called with *cond_equal=(ptr(CE),[])
1406
8493
    the Field::eq_def method) are placed to the same multiple equalities.
1407
8494
    Because of this some equality predicates are not eliminated and
1408
8495
    can be used in the constant propagation procedure.
1409
 
    We could weeken the equlity test as soon as at least one of the
1410
 
    equal fields is to be equal to a constant. It would require a
 
8496
    We could weeken the equlity test as soon as at least one of the 
 
8497
    equal fields is to be equal to a constant. It would require a 
1411
8498
    more complicated implementation: we would have to store, in
1412
8499
    general case, its own constant for each fields from the multiple
1413
8500
    equality. But at the same time it would allow us to get rid
1425
8512
    containing just field1 and field2 is added to the existing
1426
8513
    multiple equalities.
1427
8514
    If the function processes the predicate of the form field1=const,
1428
 
    it looks for a multiple equality containing field1. If found, the
 
8515
    it looks for a multiple equality containing field1. If found, the 
1429
8516
    function checks the constant of the multiple equality. If the value
1430
8517
    is unknown, it is setup to const. Otherwise the value is compared with
1431
8518
    const and the evaluation of the equality predicate is performed.
1448
8535
  @retval
1449
8536
    false   otherwise
1450
8537
*/
1451
 
static bool check_simple_equality(Item *left_item,
1452
 
                                  Item *right_item,
1453
 
                                  Item *item,
1454
 
                                  COND_EQUAL *cond_equal)
 
8538
 
 
8539
static bool check_simple_equality(Item *left_item, Item *right_item,
 
8540
                                  Item *item, COND_EQUAL *cond_equal)
1455
8541
{
 
8542
  if (left_item->type() == Item::REF_ITEM &&
 
8543
      ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
 
8544
  {
 
8545
    if (((Item_ref*)left_item)->depended_from)
 
8546
      return false;
 
8547
    left_item= left_item->real_item();
 
8548
  }
 
8549
  if (right_item->type() == Item::REF_ITEM &&
 
8550
      ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
 
8551
  {
 
8552
    if (((Item_ref*)right_item)->depended_from)
 
8553
      return false;
 
8554
    right_item= right_item->real_item();
 
8555
  }
1456
8556
  if (left_item->type() == Item::FIELD_ITEM &&
1457
8557
      right_item->type() == Item::FIELD_ITEM &&
1458
8558
      !((Item_field*)left_item)->depended_from &&
1470
8570
    bool left_copyfl, right_copyfl;
1471
8571
    Item_equal *left_item_equal=
1472
8572
               find_item_equal(cond_equal, left_field, &left_copyfl);
1473
 
    Item_equal *right_item_equal=
 
8573
    Item_equal *right_item_equal= 
1474
8574
               find_item_equal(cond_equal, right_field, &right_copyfl);
1475
8575
 
1476
8576
    /* As (NULL=NULL) != true we can't just remove the predicate f=f */
1477
8577
    if (left_field->eq(right_field)) /* f = f */
1478
 
      return (!(left_field->maybe_null() && !left_item_equal));
 
8578
      return (!(left_field->maybe_null() && !left_item_equal)); 
1479
8579
 
1480
8580
    if (left_item_equal && left_item_equal == right_item_equal)
1481
8581
    {
1482
 
      /*
 
8582
      /* 
1483
8583
        The equality predicate is inference of one of the existing
1484
8584
        multiple equalities, i.e the condition is already covered
1485
8585
        by upper level equalities
1486
8586
      */
1487
8587
       return true;
1488
8588
    }
1489
 
 
1490
 
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name &&
 
8589
    
 
8590
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name && 
1491
8591
                              item->name < subq_sj_cond_name + 64);
1492
8592
    /* Copy the found multiple equalities at the current level if needed */
1493
8593
    if (left_copyfl)
1508
8608
    }
1509
8609
 
1510
8610
    if (left_item_equal)
1511
 
    {
 
8611
    { 
1512
8612
      /* left item was found in the current or one of the upper levels */
1513
8613
      if (! right_item_equal)
1514
8614
        left_item_equal->add((Item_field *) right_item);
1517
8617
        /* Merge two multiple equalities forming a new one */
1518
8618
        left_item_equal->merge(right_item_equal);
1519
8619
        /* Remove the merged multiple equality from the list */
1520
 
        List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
8620
        List_iterator<Item_equal> li(cond_equal->current_level);
1521
8621
        while ((li++) != right_item_equal) {};
1522
8622
        li.remove();
1523
8623
      }
1524
8624
    }
1525
8625
    else
1526
 
    {
 
8626
    { 
1527
8627
      /* left item was not found neither the current nor in upper levels  */
1528
8628
      if (right_item_equal)
1529
8629
      {
1531
8631
        if (copy_item_name)
1532
8632
          right_item_equal->name = item->name;
1533
8633
      }
1534
 
      else
 
8634
      else 
1535
8635
      {
1536
8636
        /* None of the fields was found in multiple equalities */
1537
8637
        Item_equal *item_equal= new Item_equal((Item_field *) left_item,
1570
8670
 
1571
8671
      if (field_item->result_type() == STRING_RESULT)
1572
8672
      {
1573
 
        const CHARSET_INFO * const cs= ((Field_str*) field_item->field)->charset();
 
8673
        CHARSET_INFO *cs= ((Field_str*) field_item->field)->charset();
1574
8674
        if (!item)
1575
8675
        {
1576
8676
          Item_func_eq *eq_item;
1579
8679
          eq_item->set_cmp_func();
1580
8680
          eq_item->quick_fix_field();
1581
8681
          item= eq_item;
1582
 
        }
 
8682
        }  
1583
8683
        if ((cs != ((Item_func *) item)->compare_collation()) ||
1584
8684
            !cs->coll->propagate(cs, 0, 0))
1585
8685
          return false;
1594
8694
      }
1595
8695
      if (item_equal)
1596
8696
      {
1597
 
        /*
 
8697
        /* 
1598
8698
          The flag cond_false will be set to 1 after this, if item_equal
1599
8699
          already contains a constant and its value is  not equal to
1600
8700
          the value of const_item.
1612
8712
  return false;
1613
8713
}
1614
8714
 
 
8715
 
1615
8716
/**
1616
8717
  Convert row equalities into a conjunction of regular equalities.
1617
8718
 
1624
8725
    simple equality nor a row equality the item for this predicate is added
1625
8726
    to eq_list.
1626
8727
 
1627
 
  @param session        thread handle
 
8728
  @param thd        thread handle
1628
8729
  @param left_row   left term of the row equality to be processed
1629
8730
  @param right_row  right term of the row equality to be processed
1630
8731
  @param cond_equal multiple equalities that must hold together with the
1637
8738
  @retval
1638
8739
    false   otherwise
1639
8740
*/
1640
 
static bool check_row_equality(Session *session,
1641
 
                               Item *left_row, 
1642
 
                               Item_row *right_row,
1643
 
                               COND_EQUAL *cond_equal,
1644
 
                               List<Item>* eq_list)
1645
 
{
1646
 
  uint32_t n= left_row->cols();
1647
 
  for (uint32_t i= 0 ; i < n; i++)
 
8741
 
 
8742
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
 
8743
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
 
8744
 
8745
  uint n= left_row->cols();
 
8746
  for (uint i= 0 ; i < n; i++)
1648
8747
  {
1649
8748
    bool is_converted;
1650
8749
    Item *left_item= left_row->element_index(i);
1652
8751
    if (left_item->type() == Item::ROW_ITEM &&
1653
8752
        right_item->type() == Item::ROW_ITEM)
1654
8753
    {
1655
 
      is_converted= check_row_equality(session,
 
8754
      is_converted= check_row_equality(thd, 
1656
8755
                                       (Item_row *) left_item,
1657
8756
                                       (Item_row *) right_item,
1658
8757
                                       cond_equal, eq_list);
1659
8758
      if (!is_converted)
1660
 
        session->getLex()->current_select->cond_count++;
 
8759
        thd->lex->current_select->cond_count++;      
1661
8760
    }
1662
8761
    else
1663
 
    {
 
8762
    { 
1664
8763
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1665
 
      session->getLex()->current_select->cond_count++;
1666
 
    }
1667
 
 
 
8764
      thd->lex->current_select->cond_count++;
 
8765
    }  
 
8766
 
1668
8767
    if (!is_converted)
1669
8768
    {
1670
8769
      Item_func_eq *eq_item;
1678
8777
  return true;
1679
8778
}
1680
8779
 
 
8780
 
1681
8781
/**
1682
8782
  Eliminate row equalities and form multiple equalities predicates.
1683
8783
 
1692
8792
    equalities which are treated in the same way as original equality
1693
8793
    predicates.
1694
8794
 
1695
 
  @param session        thread handle
 
8795
  @param thd        thread handle
1696
8796
  @param item       predicate to process
1697
8797
  @param cond_equal multiple equalities that must hold together with the
1698
8798
                    predicate
1707
8807
           or, if the equality is neither a simple one nor a row equality,
1708
8808
           or, if the procedure fails by a fatal error.
1709
8809
*/
1710
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
 
8810
 
 
8811
static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal,
 
8812
                           List<Item> *eq_list)
1711
8813
{
1712
8814
  if (item->type() == Item::FUNC_ITEM &&
1713
8815
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
1718
8820
    if (left_item->type() == Item::ROW_ITEM &&
1719
8821
        right_item->type() == Item::ROW_ITEM)
1720
8822
    {
1721
 
      session->getLex()->current_select->cond_count--;
1722
 
      return check_row_equality(session,
 
8823
      thd->lex->current_select->cond_count--;
 
8824
      return check_row_equality(thd,
1723
8825
                                (Item_row *) left_item,
1724
8826
                                (Item_row *) right_item,
1725
8827
                                cond_equal, eq_list);
1726
8828
    }
1727
 
    else
 
8829
    else 
1728
8830
      return check_simple_equality(left_item, right_item, item, cond_equal);
1729
 
  }
 
8831
  } 
1730
8832
  return false;
1731
8833
}
1732
8834
 
 
8835
                          
1733
8836
/**
1734
8837
  Replace all equality predicates in a condition by multiple equality items.
1735
8838
 
1736
8839
    At each 'and' level the function detects items for equality predicates
1737
8840
    and replaced them by a set of multiple equality items of class Item_equal,
1738
 
    taking into account inherited equalities from upper levels.
 
8841
    taking into account inherited equalities from upper levels. 
1739
8842
    If an equality predicate is used not in a conjunction it's just
1740
8843
    replaced by a multiple equality predicate.
1741
8844
    For each 'and' level the function set a pointer to the inherited
1742
8845
    multiple equalities in the cond_equal field of the associated
1743
 
    object of the type Item_cond_and.
 
8846
    object of the type Item_cond_and.   
1744
8847
    The function also traverses the cond tree and and for each field reference
1745
8848
    sets a pointer to the multiple equality item containing the field, if there
1746
8849
    is any. If this multiple equality equates fields to a constant the
1747
 
    function replaces the field reference by the constant in the cases
 
8850
    function replaces the field reference by the constant in the cases 
1748
8851
    when the field is not of a string type or when the field reference is
1749
8852
    just an argument of a comparison predicate.
1750
 
    The function also determines the maximum number of members in
 
8853
    The function also determines the maximum number of members in 
1751
8854
    equality lists of each Item_cond_and object assigning it to
1752
 
    session->getLex()->current_select->max_equal_elems.
 
8855
    thd->lex->current_select->max_equal_elems.
1753
8856
 
1754
8857
  @note
1755
8858
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
1761
8864
    in a conjuction for a minimal set of multiple equality predicates.
1762
8865
    This set can be considered as a canonical representation of the
1763
8866
    sub-conjunction of the equality predicates.
1764
 
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by
 
8867
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by 
1765
8868
    (=(t1.a,t2.b,t3.c) AND t2.b>5), not by
1766
8869
    (=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5);
1767
8870
    while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by
1772
8875
    The function performs the substitution in a recursive descent by
1773
8876
    the condtion tree, passing to the next AND level a chain of multiple
1774
8877
    equality predicates which have been built at the upper levels.
1775
 
    The Item_equal items built at the level are attached to other
 
8878
    The Item_equal items built at the level are attached to other 
1776
8879
    non-equality conjucts as a sublist. The pointer to the inherited
1777
8880
    multiple equalities is saved in the and condition object (Item_cond_and).
1778
 
    This chain allows us for any field reference occurence easyly to find a
 
8881
    This chain allows us for any field reference occurence easyly to find a 
1779
8882
    multiple equality that must be held for this occurence.
1780
8883
    For each AND level we do the following:
1781
8884
    - scan it for all equality predicate (=) items
1782
8885
    - join them into disjoint Item_equal() groups
1783
 
    - process the included OR conditions recursively to do the same for
1784
 
      lower AND levels.
 
8886
    - process the included OR conditions recursively to do the same for 
 
8887
      lower AND levels. 
1785
8888
 
1786
8889
    We need to do things in this order as lower AND levels need to know about
1787
8890
    all possible Item_equal objects in upper levels.
1788
8891
 
1789
 
  @param session        thread handle
 
8892
  @param thd        thread handle
1790
8893
  @param cond       condition(expression) where to make replacement
1791
8894
  @param inherited  path to all inherited multiple equality items
1792
8895
 
1793
8896
  @return
1794
8897
    pointer to the transformed condition
1795
8898
*/
1796
 
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
 
8899
 
 
8900
static COND *build_equal_items_for_cond(THD *thd, COND *cond,
 
8901
                                        COND_EQUAL *inherited)
1797
8902
{
1798
8903
  Item_equal *item_equal;
1799
8904
  COND_EQUAL cond_equal;
1805
8910
    bool and_level= ((Item_cond*) cond)->functype() ==
1806
8911
      Item_func::COND_AND_FUNC;
1807
8912
    List<Item> *args= ((Item_cond*) cond)->argument_list();
1808
 
 
1809
 
    List<Item>::iterator li(args->begin());
 
8913
    
 
8914
    List_iterator<Item> li(*args);
1810
8915
    Item *item;
1811
8916
 
1812
8917
    if (and_level)
1814
8919
      /*
1815
8920
         Retrieve all conjucts of this level detecting the equality
1816
8921
         that are subject to substitution by multiple equality items and
1817
 
         removing each such predicate from the conjunction after having
 
8922
         removing each such predicate from the conjunction after having 
1818
8923
         found/created a multiple equality whose inference the predicate is.
1819
 
     */
 
8924
     */      
1820
8925
      while ((item= li++))
1821
8926
      {
1822
8927
        /*
1824
8929
          structure here because it's restored before each
1825
8930
          re-execution of any prepared statement/stored procedure.
1826
8931
        */
1827
 
        if (check_equality(session, item, &cond_equal, &eq_list))
 
8932
        if (check_equality(thd, item, &cond_equal, &eq_list))
1828
8933
          li.remove();
1829
8934
      }
1830
8935
 
1831
 
      List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
8936
      List_iterator_fast<Item_equal> it(cond_equal.current_level);
1832
8937
      while ((item_equal= it++))
1833
8938
      {
1834
8939
        item_equal->fix_length_and_dec();
1835
8940
        item_equal->update_used_tables();
1836
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
1837
 
                      item_equal->members());
 
8941
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8942
                      item_equal->members());  
1838
8943
      }
1839
8944
 
1840
8945
      ((Item_cond_and*)cond)->cond_equal= cond_equal;
1844
8949
       Make replacement of equality predicates for lower levels
1845
8950
       of the condition expression.
1846
8951
    */
1847
 
    li= args->begin();
 
8952
    li.rewind();
1848
8953
    while ((item= li++))
1849
 
    {
 
8954
    { 
1850
8955
      Item *new_item;
1851
 
      if ((new_item= build_equal_items_for_cond(session, item, inherited)) != item)
 
8956
      if ((new_item= build_equal_items_for_cond(thd, item, inherited)) != item)
1852
8957
      {
1853
8958
        /* This replacement happens only for standalone equalities */
1854
8959
        /*
1876
8981
      (b=5) and (a=c) are standalone equalities.
1877
8982
      In general we can't leave alone standalone eqalities:
1878
8983
      for WHERE a=b AND c=d AND (b=c OR d=5)
1879
 
      b=c is replaced by =(a,b,c,d).
 
8984
      b=c is replaced by =(a,b,c,d).  
1880
8985
     */
1881
 
    if (check_equality(session, cond, &cond_equal, &eq_list))
 
8986
    if (check_equality(thd, cond, &cond_equal, &eq_list))
1882
8987
    {
1883
8988
      int n= cond_equal.current_level.elements + eq_list.elements;
1884
8989
      if (n == 0)
1889
8994
        {
1890
8995
          item_equal->fix_length_and_dec();
1891
8996
          item_equal->update_used_tables();
1892
 
        }
 
8997
        }
1893
8998
        else
1894
8999
          item_equal= (Item_equal *) eq_list.pop();
1895
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
1896
 
                      item_equal->members());
 
9000
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
9001
                      item_equal->members());  
1897
9002
        return item_equal;
1898
9003
      }
1899
9004
      else
1900
9005
      {
1901
 
        /*
 
9006
        /* 
1902
9007
          Here a new AND level must be created. It can happen only
1903
9008
          when a row equality is processed as a standalone predicate.
1904
 
        */
 
9009
        */
1905
9010
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
1906
9011
        and_cond->quick_fix_field();
1907
9012
        List<Item> *args= and_cond->argument_list();
1908
 
        List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
9013
        List_iterator_fast<Item_equal> it(cond_equal.current_level);
1909
9014
        while ((item_equal= it++))
1910
9015
        {
1911
9016
          item_equal->fix_length_and_dec();
1912
9017
          item_equal->update_used_tables();
1913
 
          set_if_bigger(session->getLex()->current_select->max_equal_elems,
1914
 
                        item_equal->members());
 
9018
          set_if_bigger(thd->lex->current_select->max_equal_elems,
 
9019
                        item_equal->members());  
1915
9020
        }
1916
9021
        and_cond->cond_equal= cond_equal;
1917
9022
        args->concat((List<Item> *)&cond_equal.current_level);
1918
 
 
 
9023
        
1919
9024
        return and_cond;
1920
9025
      }
1921
9026
    }
1922
 
    /*
 
9027
    /* 
1923
9028
      For each field reference in cond, not from equal item predicates,
1924
9029
      set a pointer to the multiple equality it belongs to (if there is any)
1925
9030
      as soon the field is not of a string type or the field reference is
1926
 
      an argument of a comparison predicate.
1927
 
    */
1928
 
    unsigned char *is_subst_valid= (unsigned char *) 1;
 
9031
      an argument of a comparison predicate. 
 
9032
    */ 
 
9033
    uchar *is_subst_valid= (uchar *) 1;
1929
9034
    cond= cond->compile(&Item::subst_argument_checker,
1930
 
                        &is_subst_valid,
 
9035
                        &is_subst_valid, 
1931
9036
                        &Item::equal_fields_propagator,
1932
 
                        (unsigned char *) inherited);
 
9037
                        (uchar *) inherited);
1933
9038
    cond->update_used_tables();
1934
9039
  }
1935
9040
  return cond;
1936
9041
}
1937
9042
 
 
9043
 
1938
9044
/**
1939
9045
  Build multiple equalities for a condition and all on expressions that
1940
9046
  inherit these multiple equalities.
1980
9086
      SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a
1981
9087
        WHERE t1.a=t2.a
1982
9088
    @endcode
1983
 
    that is equivalent to:
 
9089
    that is equivalent to:   
1984
9090
    @code
1985
9091
      SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1
1986
9092
        WHERE t1.a=t2.a
1987
9093
    @endcode
1988
9094
    Thus, applying equalities from the where condition we basically
1989
9095
    can get more freedom in performing join operations.
1990
 
    Althogh we don't use this property now, it probably makes sense to use
1991
 
    it in the future.
1992
 
  @param session                      Thread Cursor
 
9096
    Althogh we don't use this property now, it probably makes sense to use 
 
9097
    it in the future.    
 
9098
  @param thd                  Thread handler
1993
9099
  @param cond                condition to build the multiple equalities for
1994
9100
  @param inherited           path to all inherited multiple equality items
1995
9101
  @param join_list           list of join tables to which the condition
2000
9106
  @return
2001
9107
    pointer to the transformed condition containing multiple equalities
2002
9108
*/
2003
 
static COND *build_equal_items(Session *session, COND *cond,
 
9109
   
 
9110
static COND *build_equal_items(THD *thd, COND *cond,
2004
9111
                               COND_EQUAL *inherited,
2005
 
                               List<TableList> *join_list,
 
9112
                               List<TABLE_LIST> *join_list,
2006
9113
                               COND_EQUAL **cond_equal_ref)
2007
9114
{
2008
9115
  COND_EQUAL *cond_equal= 0;
2009
9116
 
2010
 
  if (cond)
 
9117
  if (cond) 
2011
9118
  {
2012
 
    cond= build_equal_items_for_cond(session, cond, inherited);
 
9119
    cond= build_equal_items_for_cond(thd, cond, inherited);
2013
9120
    cond->update_used_tables();
2014
9121
    if (cond->type() == Item::COND_ITEM &&
2015
9122
        ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
2030
9137
 
2031
9138
  if (join_list)
2032
9139
  {
2033
 
    TableList *table;
2034
 
    List<TableList>::iterator li(join_list->begin());
 
9140
    TABLE_LIST *table;
 
9141
    List_iterator<TABLE_LIST> li(*join_list);
2035
9142
 
2036
9143
    while ((table= li++))
2037
9144
    {
2038
9145
      if (table->on_expr)
2039
9146
      {
2040
 
        List<TableList> *nested_join_list= table->getNestedJoin() ?
2041
 
          &table->getNestedJoin()->join_list : NULL;
 
9147
        List<TABLE_LIST> *nested_join_list= table->nested_join ?
 
9148
          &table->nested_join->join_list : NULL;
2042
9149
        /*
2043
9150
          We can modify table->on_expr because its old value will
2044
9151
          be restored before re-execution of PS/SP.
2045
9152
        */
2046
 
        table->on_expr= build_equal_items(session, table->on_expr, inherited,
 
9153
        table->on_expr= build_equal_items(thd, table->on_expr, inherited,
2047
9154
                                          nested_join_list,
2048
9155
                                          &table->cond_equal);
2049
9156
      }
2051
9158
  }
2052
9159
 
2053
9160
  return cond;
2054
 
}
 
9161
}    
 
9162
 
2055
9163
 
2056
9164
/**
2057
9165
  Compare field items by table order in the execution plan.
2058
9166
 
2059
9167
    field1 considered as better than field2 if the table containing
2060
 
    field1 is accessed earlier than the table containing field2.
 
9168
    field1 is accessed earlier than the table containing field2.   
2061
9169
    The function finds out what of two fields is better according
2062
9170
    this criteria.
2063
9171
 
2072
9180
  @retval
2073
9181
    0  otherwise
2074
9182
*/
 
9183
 
2075
9184
static int compare_fields_by_table_order(Item_field *field1,
2076
 
                                         Item_field *field2,
2077
 
                                         void *table_join_idx)
 
9185
                                  Item_field *field2,
 
9186
                                  void *table_join_idx)
2078
9187
{
2079
9188
  int cmp= 0;
2080
9189
  bool outer_ref= 0;
2081
9190
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
2082
 
  {
 
9191
  {  
2083
9192
    outer_ref= 1;
2084
9193
    cmp= -1;
2085
9194
  }
2090
9199
  }
2091
9200
  if (outer_ref)
2092
9201
    return cmp;
2093
 
  JoinTable **idx= (JoinTable **) table_join_idx;
2094
 
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
 
9202
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
 
9203
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
2095
9204
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2096
9205
}
2097
9206
 
 
9207
 
2098
9208
/**
2099
9209
  Generate minimal set of simple equalities equivalent to a multiple equality.
2100
9210
 
2134
9244
    a pointer to the simple generated equality, if success.
2135
9245
    - 0, otherwise.
2136
9246
*/
2137
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
 
9247
 
 
9248
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
 
9249
                                  Item_equal *item_equal)
2138
9250
{
2139
9251
  List<Item> eq_list;
2140
9252
  Item_func_eq *eq_item= 0;
2141
9253
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
2142
 
    return new Item_int((int64_t) 0,1);
 
9254
    return new Item_int((int64_t) 0,1); 
2143
9255
  Item *item_const= item_equal->get_const();
2144
9256
  Item_equal_iterator it(*item_equal);
2145
9257
  Item *head;
2156
9268
    Item_equal *upper= item_field->find_item_equal(upper_levels);
2157
9269
    Item_field *item= item_field;
2158
9270
    if (upper)
2159
 
    {
 
9271
    { 
2160
9272
      if (item_const && upper->get_const())
2161
9273
        item= 0;
2162
9274
      else
2200
9312
 
2201
9313
  cond->quick_fix_field();
2202
9314
  cond->update_used_tables();
2203
 
 
 
9315
   
2204
9316
  return cond;
2205
9317
}
2206
9318
 
 
9319
 
2207
9320
/**
2208
9321
  Substitute every field reference in a condition by the best equal field
2209
9322
  and eliminate all multiple equality predicates.
2212
9325
    multiple equality predicate it sorts the field references in it
2213
9326
    according to the order of tables specified by the table_join_idx
2214
9327
    parameter. Then it eliminates the multiple equality predicate it
2215
 
    replacing it by the conjunction of simple equality predicates
 
9328
    replacing it by the conjunction of simple equality predicates 
2216
9329
    equating every field from the multiple equality to the first
2217
9330
    field in it, or to the constant, if there is any.
2218
9331
    After this the function retrieves all other conjuncted
2231
9344
  @return
2232
9345
    The transformed condition
2233
9346
*/
2234
 
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
 
9347
 
 
9348
static COND* substitute_for_best_equal_field(COND *cond,
 
9349
                                             COND_EQUAL *cond_equal,
 
9350
                                             void *table_join_idx)
2235
9351
{
2236
9352
  Item_equal *item_equal;
2237
9353
 
2246
9362
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
2247
9363
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2248
9364
 
2249
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
9365
      List_iterator_fast<Item_equal> it(cond_equal->current_level);      
2250
9366
      while ((item_equal= it++))
2251
9367
      {
2252
9368
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2253
9369
      }
2254
9370
    }
2255
 
 
2256
 
    List<Item>::iterator li(cond_list->begin());
 
9371
    
 
9372
    List_iterator<Item> li(*cond_list);
2257
9373
    Item *item;
2258
9374
    while ((item= li++))
2259
9375
    {
2269
9385
 
2270
9386
    if (and_level)
2271
9387
    {
2272
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
9388
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2273
9389
      while ((item_equal= it++))
2274
9390
      {
2275
9391
        cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
2282
9398
    }
2283
9399
    if (cond->type() == Item::COND_ITEM &&
2284
9400
        !((Item_cond*)cond)->argument_list()->elements)
2285
 
      cond= new Item_int((int32_t)cond->val_bool());
 
9401
      cond= new Item_int((int32)cond->val_bool());
2286
9402
 
2287
9403
  }
2288
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9404
  else if (cond->type() == Item::FUNC_ITEM && 
2289
9405
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2290
9406
  {
2291
9407
    item_equal= (Item_equal *) cond;
2299
9415
  return cond;
2300
9416
}
2301
9417
 
 
9418
 
2302
9419
/**
2303
9420
  Check appearance of new constant items in multiple equalities
2304
9421
  of a condition after reading a constant table.
2311
9428
  @param cond       condition whose multiple equalities are to be checked
2312
9429
  @param table      constant table that has been read
2313
9430
*/
2314
 
void update_const_equal_items(COND *cond, JoinTable *tab)
 
9431
 
 
9432
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
2315
9433
{
2316
9434
  if (!(cond->used_tables() & tab->table->map))
2317
9435
    return;
2318
9436
 
2319
9437
  if (cond->type() == Item::COND_ITEM)
2320
9438
  {
2321
 
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
2322
 
    List<Item>::iterator li(cond_list->begin());
 
9439
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); 
 
9440
    List_iterator_fast<Item> li(*cond_list);
2323
9441
    Item *item;
2324
9442
    while ((item= li++))
2325
9443
      update_const_equal_items(item, tab);
2326
9444
  }
2327
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9445
  else if (cond->type() == Item::FUNC_ITEM && 
2328
9446
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2329
9447
  {
2330
9448
    Item_equal *item_equal= (Item_equal *) cond;
2338
9456
      while ((item_field= it++))
2339
9457
      {
2340
9458
        Field *field= item_field->field;
2341
 
        JoinTable *stat= field->getTable()->reginfo.join_tab;
 
9459
        JOIN_TAB *stat= field->table->reginfo.join_tab;
2342
9460
        key_map possible_keys= field->key_start;
2343
 
        possible_keys&= field->getTable()->keys_in_use_for_query;
2344
 
        stat[0].const_keys|= possible_keys;
 
9461
        possible_keys.intersect(field->table->keys_in_use_for_query);
 
9462
        stat[0].const_keys.merge(possible_keys);
2345
9463
 
2346
9464
        /*
2347
 
          For each field in the multiple equality (for which we know that it
2348
 
          is a constant) we have to find its corresponding key part, and set
 
9465
          For each field in the multiple equality (for which we know that it 
 
9466
          is a constant) we have to find its corresponding key part, and set 
2349
9467
          that key part in const_key_parts.
2350
 
        */
2351
 
        if (possible_keys.any())
 
9468
        */  
 
9469
        if (!possible_keys.is_clear_all())
2352
9470
        {
2353
 
          Table *field_tab= field->getTable();
2354
 
          optimizer::KeyUse *use;
2355
 
          for (use= stat->keyuse; use && use->getTable() == field_tab; use++)
2356
 
            if (possible_keys.test(use->getKey()) &&
2357
 
                field_tab->key_info[use->getKey()].key_part[use->getKeypart()].field ==
 
9471
          TABLE *tab= field->table;
 
9472
          KEYUSE *use;
 
9473
          for (use= stat->keyuse; use && use->table == tab; use++)
 
9474
            if (possible_keys.is_set(use->key) && 
 
9475
                tab->key_info[use->key].key_part[use->keypart].field ==
2358
9476
                field)
2359
 
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
9477
              tab->const_key_parts[use->key]|= use->keypart_map;
2360
9478
        }
2361
9479
      }
2362
9480
    }
2363
9481
  }
2364
9482
}
2365
9483
 
 
9484
 
2366
9485
/*
2367
9486
  change field = field to field = const for each found field = const in the
2368
9487
  and_level
2369
9488
*/
2370
 
static void change_cond_ref_to_const(Session *session,
2371
 
                                     list<COND_CMP>& save_list,
2372
 
                                     Item *and_father,
2373
 
                                     Item *cond,
2374
 
                                     Item *field,
2375
 
                                     Item *value)
 
9489
 
 
9490
static void
 
9491
change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
 
9492
                         Item *and_father, Item *cond,
 
9493
                         Item *field, Item *value)
2376
9494
{
2377
9495
  if (cond->type() == Item::COND_ITEM)
2378
9496
  {
2379
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2380
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
9497
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9498
      Item_func::COND_AND_FUNC;
 
9499
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2381
9500
    Item *item;
2382
9501
    while ((item=li++))
2383
 
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2384
 
 
 
9502
      change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item,
 
9503
                               field, value);
2385
9504
    return;
2386
9505
  }
2387
9506
  if (cond->eq_cmp_result() == Item::COND_OK)
2400
9519
       left_item->collation.collation == value->collation.collation))
2401
9520
  {
2402
9521
    Item *tmp=value->clone_item();
 
9522
    tmp->collation.set(right_item->collation);
 
9523
    
2403
9524
    if (tmp)
2404
9525
    {
2405
 
      tmp->collation.set(right_item->collation);
2406
 
      session->change_item_tree(args + 1, tmp);
 
9526
      thd->change_item_tree(args + 1, tmp);
2407
9527
      func->update_used_tables();
2408
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2409
 
                and_father != cond && 
2410
 
          ! left_item->const_item())
 
9528
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9529
          && and_father != cond && !left_item->const_item())
2411
9530
      {
2412
 
        cond->marker=1;
2413
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9531
        cond->marker=1;
 
9532
        COND_CMP *tmp2;
 
9533
        if ((tmp2=new COND_CMP(and_father,func)))
 
9534
          save_list->push_back(tmp2);
2414
9535
      }
2415
9536
      func->set_cmp_func();
2416
9537
    }
2422
9543
            right_item->collation.collation == value->collation.collation))
2423
9544
  {
2424
9545
    Item *tmp= value->clone_item();
 
9546
    tmp->collation.set(left_item->collation);
 
9547
    
2425
9548
    if (tmp)
2426
9549
    {
2427
 
      tmp->collation.set(left_item->collation);
2428
 
      session->change_item_tree(args, tmp);
 
9550
      thd->change_item_tree(args, tmp);
2429
9551
      value= tmp;
2430
9552
      func->update_used_tables();
2431
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2432
 
          and_father != cond && 
2433
 
          ! right_item->const_item())
 
9553
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9554
          && and_father != cond && !right_item->const_item())
2434
9555
      {
2435
9556
        args[0]= args[1];                       // For easy check
2436
 
        session->change_item_tree(args + 1, value);
2437
 
        cond->marker=1;
2438
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9557
        thd->change_item_tree(args + 1, value);
 
9558
        cond->marker=1;
 
9559
        COND_CMP *tmp2;
 
9560
        if ((tmp2=new COND_CMP(and_father,func)))
 
9561
          save_list->push_back(tmp2);
2439
9562
      }
2440
9563
      func->set_cmp_func();
2441
9564
    }
2450
9573
  @return
2451
9574
    new conditions
2452
9575
*/
2453
 
Item *remove_additional_cond(Item* conds)
 
9576
 
 
9577
static Item *remove_additional_cond(Item* conds)
2454
9578
{
2455
9579
  if (conds->name == in_additional_cond)
2456
9580
    return 0;
2457
9581
  if (conds->type() == Item::COND_ITEM)
2458
9582
  {
2459
9583
    Item_cond *cnd= (Item_cond*) conds;
2460
 
    List<Item>::iterator li(cnd->argument_list()->begin());
 
9584
    List_iterator<Item> li(*(cnd->argument_list()));
2461
9585
    Item *item;
2462
9586
    while ((item= li++))
2463
9587
    {
2473
9597
  return conds;
2474
9598
}
2475
9599
 
2476
 
static void propagate_cond_constants(Session *session, 
2477
 
                                     list<COND_CMP>& save_list, 
2478
 
                                     COND *and_father, 
2479
 
                                     COND *cond)
 
9600
static void
 
9601
propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list,
 
9602
                         COND *and_father, COND *cond)
2480
9603
{
2481
9604
  if (cond->type() == Item::COND_ITEM)
2482
9605
  {
2483
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2484
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
9606
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9607
      Item_func::COND_AND_FUNC;
 
9608
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2485
9609
    Item *item;
2486
 
    list<COND_CMP> save;
 
9610
    I_List<COND_CMP> save;
2487
9611
    while ((item=li++))
2488
9612
    {
2489
 
      propagate_cond_constants(session, save, and_level ? cond : item, item);
 
9613
      propagate_cond_constants(thd, &save,and_level ? cond : item, item);
2490
9614
    }
2491
9615
    if (and_level)
2492
 
    {
2493
 
      // Handle other found items
2494
 
      for (list<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
 
9616
    {                                           // Handle other found items
 
9617
      I_List_iterator<COND_CMP> cond_itr(save);
 
9618
      COND_CMP *cond_cmp;
 
9619
      while ((cond_cmp=cond_itr++))
2495
9620
      {
2496
 
        Item **args= iter->second->arguments();
2497
 
        if (not args[0]->const_item())
2498
 
        {
2499
 
          change_cond_ref_to_const(session, save, iter->first,
2500
 
                                   iter->first, args[0], args[1] );
2501
 
        }
 
9621
        Item **args= cond_cmp->cmp_func->arguments();
 
9622
        if (!args[0]->const_item())
 
9623
          change_cond_ref_to_const(thd, &save,cond_cmp->and_level,
 
9624
                                   cond_cmp->and_level, args[0], args[1]);
2502
9625
      }
2503
9626
    }
2504
9627
  }
2505
9628
  else if (and_father != cond && !cond->marker)         // In a AND group
2506
9629
  {
2507
9630
    if (cond->type() == Item::FUNC_ITEM &&
2508
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2509
 
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
9631
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
9632
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2510
9633
    {
2511
9634
      Item_func_eq *func=(Item_func_eq*) cond;
2512
9635
      Item **args= func->arguments();
2515
9638
      if (!(left_const && right_const) &&
2516
9639
          args[0]->result_type() == args[1]->result_type())
2517
9640
      {
2518
 
        if (right_const)
2519
 
        {
2520
 
                resolve_const_item(session, &args[1], args[0]);
2521
 
          func->update_used_tables();
2522
 
                change_cond_ref_to_const(session, save_list, and_father, and_father,
2523
 
                                        args[0], args[1]);
2524
 
        }
2525
 
        else if (left_const)
2526
 
        {
2527
 
                resolve_const_item(session, &args[0], args[1]);
2528
 
          func->update_used_tables();
2529
 
                change_cond_ref_to_const(session, save_list, and_father, and_father,
2530
 
                                        args[1], args[0]);
2531
 
        }
2532
 
      }
2533
 
    }
2534
 
  }
2535
 
}
 
9641
        if (right_const)
 
9642
        {
 
9643
          resolve_const_item(thd, &args[1], args[0]);
 
9644
          func->update_used_tables();
 
9645
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9646
                                   args[0], args[1]);
 
9647
        }
 
9648
        else if (left_const)
 
9649
        {
 
9650
          resolve_const_item(thd, &args[0], args[1]);
 
9651
          func->update_used_tables();
 
9652
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9653
                                   args[1], args[0]);
 
9654
        }
 
9655
      }
 
9656
    }
 
9657
  }
 
9658
}
 
9659
 
 
9660
 
 
9661
/**
 
9662
  Simplify joins replacing outer joins by inner joins whenever it's
 
9663
  possible.
 
9664
 
 
9665
    The function, during a retrieval of join_list,  eliminates those
 
9666
    outer joins that can be converted into inner join, possibly nested.
 
9667
    It also moves the on expressions for the converted outer joins
 
9668
    and from inner joins to conds.
 
9669
    The function also calculates some attributes for nested joins:
 
9670
    - used_tables    
 
9671
    - not_null_tables
 
9672
    - dep_tables.
 
9673
    - on_expr_dep_tables
 
9674
    The first two attributes are used to test whether an outer join can
 
9675
    be substituted for an inner join. The third attribute represents the
 
9676
    relation 'to be dependent on' for tables. If table t2 is dependent
 
9677
    on table t1, then in any evaluated execution plan table access to
 
9678
    table t2 must precede access to table t2. This relation is used also
 
9679
    to check whether the query contains  invalid cross-references.
 
9680
    The forth attribute is an auxiliary one and is used to calculate
 
9681
    dep_tables.
 
9682
    As the attribute dep_tables qualifies possibles orders of tables in the
 
9683
    execution plan, the dependencies required by the straight join
 
9684
    modifiers are reflected in this attribute as well.
 
9685
    The function also removes all braces that can be removed from the join
 
9686
    expression without changing its meaning.
 
9687
 
 
9688
  @note
 
9689
    An outer join can be replaced by an inner join if the where condition
 
9690
    or the on expression for an embedding nested join contains a conjunctive
 
9691
    predicate rejecting null values for some attribute of the inner tables.
 
9692
 
 
9693
    E.g. in the query:    
 
9694
    @code
 
9695
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9696
    @endcode
 
9697
    the predicate t2.b < 5 rejects nulls.
 
9698
    The query is converted first to:
 
9699
    @code
 
9700
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9701
    @endcode
 
9702
    then to the equivalent form:
 
9703
    @code
 
9704
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
 
9705
    @endcode
 
9706
 
 
9707
 
 
9708
    Similarly the following query:
 
9709
    @code
 
9710
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
 
9711
        WHERE t2.c < 5  
 
9712
    @endcode
 
9713
    is converted to:
 
9714
    @code
 
9715
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b 
 
9716
 
 
9717
    @endcode
 
9718
 
 
9719
    One conversion might trigger another:
 
9720
    @code
 
9721
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
 
9722
                       LEFT JOIN t3 ON t3.b=t2.b
 
9723
        WHERE t3 IS NOT NULL =>
 
9724
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
 
9725
        WHERE t3 IS NOT NULL AND t3.b=t2.b => 
 
9726
      SELECT * FROM t1, t2, t3
 
9727
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
 
9728
  @endcode
 
9729
 
 
9730
    The function removes all unnecessary braces from the expression
 
9731
    produced by the conversions.
 
9732
    E.g.
 
9733
    @code
 
9734
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9735
    @endcode
 
9736
    finally is converted to: 
 
9737
    @code
 
9738
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9739
 
 
9740
    @endcode
 
9741
 
 
9742
 
 
9743
    It also will remove braces from the following queries:
 
9744
    @code
 
9745
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
 
9746
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
 
9747
    @endcode
 
9748
 
 
9749
    The benefit of this simplification procedure is that it might return 
 
9750
    a query for which the optimizer can evaluate execution plan with more
 
9751
    join orders. With a left join operation the optimizer does not
 
9752
    consider any plan where one of the inner tables is before some of outer
 
9753
    tables.
 
9754
 
 
9755
  IMPLEMENTATION
 
9756
    The function is implemented by a recursive procedure.  On the recursive
 
9757
    ascent all attributes are calculated, all outer joins that can be
 
9758
    converted are replaced and then all unnecessary braces are removed.
 
9759
    As join list contains join tables in the reverse order sequential
 
9760
    elimination of outer joins does not require extra recursive calls.
 
9761
 
 
9762
  SEMI-JOIN NOTES
 
9763
    Remove all semi-joins that have are within another semi-join (i.e. have
 
9764
    an "ancestor" semi-join nest)
 
9765
 
 
9766
  EXAMPLES
 
9767
    Here is an example of a join query with invalid cross references:
 
9768
    @code
 
9769
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b 
 
9770
    @endcode
 
9771
 
 
9772
  @param join        reference to the query info
 
9773
  @param join_list   list representation of the join to be converted
 
9774
  @param conds       conditions to add on expressions for converted joins
 
9775
  @param top         true <=> conds is the where condition
 
9776
 
 
9777
  @return
 
9778
    - The new condition, if success
 
9779
    - 0, otherwise
 
9780
*/
 
9781
 
 
9782
static COND *
 
9783
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
 
9784
               bool in_sj)
 
9785
{
 
9786
  TABLE_LIST *table;
 
9787
  NESTED_JOIN *nested_join;
 
9788
  TABLE_LIST *prev_table= 0;
 
9789
  List_iterator<TABLE_LIST> li(*join_list);
 
9790
 
 
9791
  /* 
 
9792
    Try to simplify join operations from join_list.
 
9793
    The most outer join operation is checked for conversion first. 
 
9794
  */
 
9795
  while ((table= li++))
 
9796
  {
 
9797
    table_map used_tables;
 
9798
    table_map not_null_tables= (table_map) 0;
 
9799
 
 
9800
    if ((nested_join= table->nested_join))
 
9801
    {
 
9802
      /* 
 
9803
         If the element of join_list is a nested join apply
 
9804
         the procedure to its nested join list first.
 
9805
      */
 
9806
      if (table->on_expr)
 
9807
      {
 
9808
        Item *expr= table->on_expr;
 
9809
        /* 
 
9810
           If an on expression E is attached to the table, 
 
9811
           check all null rejected predicates in this expression.
 
9812
           If such a predicate over an attribute belonging to
 
9813
           an inner table of an embedded outer join is found,
 
9814
           the outer join is converted to an inner join and
 
9815
           the corresponding on expression is added to E. 
 
9816
        */ 
 
9817
        expr= simplify_joins(join, &nested_join->join_list,
 
9818
                             expr, false, in_sj || table->sj_on_expr);
 
9819
 
 
9820
        if (!table->prep_on_expr || expr != table->on_expr)
 
9821
        {
 
9822
          assert(expr);
 
9823
 
 
9824
          table->on_expr= expr;
 
9825
          table->prep_on_expr= expr->copy_andor_structure(join->thd);
 
9826
        }
 
9827
      }
 
9828
      nested_join->used_tables= (table_map) 0;
 
9829
      nested_join->not_null_tables=(table_map) 0;
 
9830
      conds= simplify_joins(join, &nested_join->join_list, conds, top, 
 
9831
                            in_sj || table->sj_on_expr);
 
9832
      used_tables= nested_join->used_tables;
 
9833
      not_null_tables= nested_join->not_null_tables;  
 
9834
    }
 
9835
    else
 
9836
    {
 
9837
      if (!table->prep_on_expr)
 
9838
        table->prep_on_expr= table->on_expr;
 
9839
      used_tables= table->table->map;
 
9840
      if (conds)
 
9841
        not_null_tables= conds->not_null_tables();
 
9842
    }
 
9843
      
 
9844
    if (table->embedding)
 
9845
    {
 
9846
      table->embedding->nested_join->used_tables|= used_tables;
 
9847
      table->embedding->nested_join->not_null_tables|= not_null_tables;
 
9848
    }
 
9849
 
 
9850
    if (!table->outer_join || (used_tables & not_null_tables))
 
9851
    {
 
9852
      /* 
 
9853
        For some of the inner tables there are conjunctive predicates
 
9854
        that reject nulls => the outer join can be replaced by an inner join.
 
9855
      */
 
9856
      table->outer_join= 0;
 
9857
      if (table->on_expr)
 
9858
      {
 
9859
        /* Add ON expression to the WHERE or upper-level ON condition. */
 
9860
        if (conds)
 
9861
        {
 
9862
          conds= and_conds(conds, table->on_expr);
 
9863
          conds->top_level_item();
 
9864
          /* conds is always a new item as both cond and on_expr existed */
 
9865
          assert(!conds->fixed);
 
9866
          conds->fix_fields(join->thd, &conds);
 
9867
        }
 
9868
        else
 
9869
          conds= table->on_expr; 
 
9870
        table->prep_on_expr= table->on_expr= 0;
 
9871
      }
 
9872
    }
 
9873
    
 
9874
    if (!top)
 
9875
      continue;
 
9876
 
 
9877
    /* 
 
9878
      Only inner tables of non-convertible outer joins
 
9879
      remain with on_expr.
 
9880
    */ 
 
9881
    if (table->on_expr)
 
9882
    {
 
9883
      table->dep_tables|= table->on_expr->used_tables(); 
 
9884
      if (table->embedding)
 
9885
      {
 
9886
        table->dep_tables&= ~table->embedding->nested_join->used_tables;   
 
9887
        /*
 
9888
           Embedding table depends on tables used
 
9889
           in embedded on expressions. 
 
9890
        */
 
9891
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
 
9892
      }
 
9893
      else
 
9894
        table->dep_tables&= ~table->table->map;
 
9895
    }
 
9896
 
 
9897
    if (prev_table)
 
9898
    {
 
9899
      /* The order of tables is reverse: prev_table follows table */
 
9900
      if (prev_table->straight)
 
9901
        prev_table->dep_tables|= used_tables;
 
9902
      if (prev_table->on_expr)
 
9903
      {
 
9904
        prev_table->dep_tables|= table->on_expr_dep_tables;
 
9905
        table_map prev_used_tables= prev_table->nested_join ?
 
9906
                                    prev_table->nested_join->used_tables :
 
9907
                                    prev_table->table->map;
 
9908
        /* 
 
9909
          If on expression contains only references to inner tables
 
9910
          we still make the inner tables dependent on the outer tables.
 
9911
          It would be enough to set dependency only on one outer table
 
9912
          for them. Yet this is really a rare case.
 
9913
        */  
 
9914
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
 
9915
          prev_table->dep_tables|= used_tables;
 
9916
      }
 
9917
    }
 
9918
    prev_table= table;
 
9919
  }
 
9920
    
 
9921
  /* 
 
9922
    Flatten nested joins that can be flattened.
 
9923
    no ON expression and not a semi-join => can be flattened.
 
9924
  */
 
9925
  li.rewind();
 
9926
  while ((table= li++))
 
9927
  {
 
9928
    nested_join= table->nested_join;
 
9929
    if (table->sj_on_expr && !in_sj)
 
9930
    {
 
9931
       /*
 
9932
         If this is a semi-join that is not contained within another semi-join, 
 
9933
         leave it intact (otherwise it is flattened)
 
9934
       */
 
9935
      join->select_lex->sj_nests.push_back(table);
 
9936
    }
 
9937
    else if (nested_join && !table->on_expr)
 
9938
    {
 
9939
      TABLE_LIST *tbl;
 
9940
      List_iterator<TABLE_LIST> it(nested_join->join_list);
 
9941
      while ((tbl= it++))
 
9942
      {
 
9943
        tbl->embedding= table->embedding;
 
9944
        tbl->join_list= table->join_list;
 
9945
      }      
 
9946
      li.replace(nested_join->join_list);
 
9947
    }
 
9948
  }
 
9949
  return(conds); 
 
9950
}
 
9951
 
 
9952
 
 
9953
/**
 
9954
  Assign each nested join structure a bit in nested_join_map.
 
9955
 
 
9956
    Assign each nested join structure (except "confluent" ones - those that
 
9957
    embed only one element) a bit in nested_join_map.
 
9958
 
 
9959
  @param join          Join being processed
 
9960
  @param join_list     List of tables
 
9961
  @param first_unused  Number of first unused bit in nested_join_map before the
 
9962
                       call
 
9963
 
 
9964
  @note
 
9965
    This function is called after simplify_joins(), when there are no
 
9966
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
 
9967
    we will not run out of bits in nested_join_map.
 
9968
 
 
9969
  @return
 
9970
    First unused bit in nested_join_map after the call.
 
9971
*/
 
9972
 
 
9973
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, 
 
9974
                                          uint first_unused)
 
9975
{
 
9976
  List_iterator<TABLE_LIST> li(*join_list);
 
9977
  TABLE_LIST *table;
 
9978
  while ((table= li++))
 
9979
  {
 
9980
    NESTED_JOIN *nested_join;
 
9981
    if ((nested_join= table->nested_join))
 
9982
    {
 
9983
      /*
 
9984
        It is guaranteed by simplify_joins() function that a nested join
 
9985
        that has only one child is either
 
9986
         - a single-table view (the child is the underlying table), or 
 
9987
         - a single-table semi-join nest
 
9988
 
 
9989
        We don't assign bits to such sj-nests because 
 
9990
        1. it is redundant (a "sequence" of one table cannot be interleaved 
 
9991
            with anything)
 
9992
        2. we could run out bits in nested_join_map otherwise.
 
9993
      */
 
9994
      if (nested_join->join_list.elements != 1)
 
9995
      {
 
9996
        /* Don't assign bits to sj-nests */
 
9997
        if (table->on_expr)
 
9998
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
 
9999
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
 
10000
                                                    first_unused);
 
10001
      }
 
10002
    }
 
10003
  }
 
10004
  return(first_unused);
 
10005
}
 
10006
 
 
10007
 
 
10008
/**
 
10009
  Set NESTED_JOIN::counter=0 in all nested joins in passed list.
 
10010
 
 
10011
    Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
 
10012
    the passed join_list.
 
10013
 
 
10014
  @param join_list  List of nested joins to process. It may also contain base
 
10015
                    tables which will be ignored.
 
10016
*/
 
10017
 
 
10018
static void reset_nj_counters(List<TABLE_LIST> *join_list)
 
10019
{
 
10020
  List_iterator<TABLE_LIST> li(*join_list);
 
10021
  TABLE_LIST *table;
 
10022
  while ((table= li++))
 
10023
  {
 
10024
    NESTED_JOIN *nested_join;
 
10025
    if ((nested_join= table->nested_join))
 
10026
    {
 
10027
      nested_join->counter_= 0;
 
10028
      reset_nj_counters(&nested_join->join_list);
 
10029
    }
 
10030
  }
 
10031
  return;
 
10032
}
 
10033
 
2536
10034
 
2537
10035
/**
2538
10036
  Check interleaving with an inner tables of an outer join for
2539
10037
  extension table.
2540
10038
 
2541
 
    Check if table next_tab can be added to current partial join order, and
 
10039
    Check if table next_tab can be added to current partial join order, and 
2542
10040
    if yes, record that it has been added.
2543
10041
 
2544
10042
    The function assumes that both current partial join order and its
2545
10043
    extension with next_tab are valid wrt table dependencies.
2546
10044
 
2547
10045
  @verbatim
2548
 
     IMPLEMENTATION
2549
 
       LIMITATIONS ON JOIN order_st
 
10046
     IMPLEMENTATION 
 
10047
       LIMITATIONS ON JOIN ORDER
2550
10048
         The nested [outer] joins executioner algorithm imposes these limitations
2551
10049
         on join order:
2552
 
         1. "Outer tables first" -  any "outer" table must be before any
 
10050
         1. "Outer tables first" -  any "outer" table must be before any 
2553
10051
             corresponding "inner" table.
2554
10052
         2. "No interleaving" - tables inside a nested join must form a continuous
2555
 
            sequence in join order (i.e. the sequence must not be interrupted by
 
10053
            sequence in join order (i.e. the sequence must not be interrupted by 
2556
10054
            tables that are outside of this nested join).
2557
10055
 
2558
10056
         #1 is checked elsewhere, this function checks #2 provided that #1 has
2559
10057
         been already checked.
2560
10058
 
2561
10059
       WHY NEED NON-INTERLEAVING
2562
 
         Consider an example:
 
10060
         Consider an example: 
2563
10061
 
2564
10062
           select * from t0 join t1 left join (t2 join t3) on cond1
2565
10063
 
2583
10081
         The limitations on join order can be rephrased as follows: for valid
2584
10082
         join order one must be able to:
2585
10083
           1. write down the used tables in the join order on one line.
2586
 
           2. for each nested join, put one '(' and one ')' on the said line
 
10084
           2. for each nested join, put one '(' and one ')' on the said line        
2587
10085
           3. write "LEFT JOIN" and "ON (...)" where appropriate
2588
10086
           4. get a query equivalent to the query we're trying to execute.
2589
10087
 
2590
10088
         Calls to check_interleaving_with_nj() are equivalent to writing the
2591
 
         above described line from left to right.
2592
 
         A single check_interleaving_with_nj(A,B) call is equivalent to writing
 
10089
         above described line from left to right. 
 
10090
         A single check_interleaving_with_nj(A,B) call is equivalent to writing 
2593
10091
         table B and appropriate brackets on condition that table A and
2594
10092
         appropriate brackets is the last what was written. Graphically the
2595
10093
         transition is as follows:
2602
10100
                                                      position.
2603
10101
 
2604
10102
         Notes about the position:
2605
 
           The caller guarantees that there is no more then one X-bracket by
2606
 
           checking "!(remaining_tables & s->dependent)" before calling this
 
10103
           The caller guarantees that there is no more then one X-bracket by 
 
10104
           checking "!(remaining_tables & s->dependent)" before calling this 
2607
10105
           function. X-bracket may have a pair in Y-bracket.
2608
10106
 
2609
10107
         When "writing" we store/update this auxilary info about the current
2610
10108
         position:
2611
10109
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
2612
10110
             joins) we've opened but didn't close.
2613
 
          2. {each NestedJoin class not simplified away}->counter - number
 
10111
          2. {each NESTED_JOIN structure not simplified away}->counter - number
2614
10112
             of this nested join's children that have already been added to to
2615
10113
             the partial join order.
2616
10114
  @endverbatim
2617
10115
 
2618
10116
  @param join       Join being processed
 
10117
  @param last_tab   Last table in current partial join order (this function is
 
10118
                    not called for empty partial join orders)
2619
10119
  @param next_tab   Table we're going to extend the current partial join with
2620
10120
 
2621
10121
  @retval
2624
10124
  @retval
2625
10125
    true   Requested join order extension not allowed.
2626
10126
*/
2627
 
bool check_interleaving_with_nj(JoinTable *next_tab)
 
10127
 
 
10128
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
2628
10129
{
2629
 
  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
2630
 
  Join *join= next_tab->join;
 
10130
  TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding;
 
10131
  JOIN *join= last_tab->join;
2631
10132
 
2632
 
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
 
10133
  if (join->cur_embedding_map & ~next_tab->embedding_map)
2633
10134
  {
2634
 
    /*
 
10135
    /* 
2635
10136
      next_tab is outside of the "pair of brackets" we're currently in.
2636
10137
      Cannot add it.
2637
10138
    */
2638
10139
    return true;
2639
10140
  }
2640
 
 
 
10141
   
2641
10142
  /*
2642
10143
    Do update counters for "pairs of brackets" that we've left (marked as
2643
10144
    X,Y,Z in the above picture)
2644
10145
  */
2645
 
  for (;next_emb; next_emb= next_emb->getEmbedding())
 
10146
  for (;next_emb; next_emb= next_emb->embedding)
2646
10147
  {
2647
 
    next_emb->getNestedJoin()->counter_++;
2648
 
    if (next_emb->getNestedJoin()->counter_ == 1)
 
10148
    next_emb->nested_join->counter_++;
 
10149
    if (next_emb->nested_join->counter_ == 1)
2649
10150
    {
2650
 
      /*
 
10151
      /* 
2651
10152
        next_emb is the first table inside a nested join we've "entered". In
2652
10153
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
2653
10154
        X bracket might have Y pair bracket.
2654
10155
      */
2655
 
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
 
10156
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
2656
10157
    }
2657
 
 
2658
 
    if (next_emb->getNestedJoin()->join_list.elements !=
2659
 
        next_emb->getNestedJoin()->counter_)
 
10158
    
 
10159
    if (next_emb->nested_join->join_list.elements !=
 
10160
        next_emb->nested_join->counter_)
2660
10161
      break;
2661
10162
 
2662
10163
    /*
2663
10164
      We're currently at Y or Z-bracket as depicted in the above picture.
2664
10165
      Mark that we've left it and continue walking up the brackets hierarchy.
2665
10166
    */
2666
 
    join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
 
10167
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
2667
10168
  }
2668
10169
  return false;
2669
10170
}
2670
10171
 
2671
 
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
2672
 
{
2673
 
  Session *session= join->session;
 
10172
 
 
10173
/**
 
10174
  Nested joins perspective: Remove the last table from the join order.
 
10175
 
 
10176
    Remove the last table from the partial join order and update the nested
 
10177
    joins counters and join->cur_embedding_map. It is ok to call this 
 
10178
    function for the first table in join order (for which 
 
10179
    check_interleaving_with_nj has not been called)
 
10180
 
 
10181
  @param last  join table to remove, it is assumed to be the last in current
 
10182
               partial join order.
 
10183
*/
 
10184
 
 
10185
static void restore_prev_nj_state(JOIN_TAB *last)
 
10186
{
 
10187
  TABLE_LIST *last_emb= last->table->pos_in_table_list->embedding;
 
10188
  JOIN *join= last->join;
 
10189
  while (last_emb)
 
10190
  {
 
10191
    if (last_emb->on_expr)
 
10192
    {
 
10193
      if (!(--last_emb->nested_join->counter_))
 
10194
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
 
10195
      else if (last_emb->nested_join->join_list.elements-1 ==
 
10196
               last_emb->nested_join->counter_) 
 
10197
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
 
10198
      else
 
10199
        break;
 
10200
    }
 
10201
    last_emb= last_emb->embedding;
 
10202
  }
 
10203
}
 
10204
 
 
10205
 
 
10206
 
 
10207
static 
 
10208
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
 
10209
{
 
10210
  TABLE_LIST *emb_sj_nest;
 
10211
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10212
  {
 
10213
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
 
10214
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
 
10215
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
 
10216
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10217
  }
 
10218
}
 
10219
 
 
10220
 
 
10221
/*
 
10222
  we assume remaining_tables doesnt contain @tab.
 
10223
*/
 
10224
 
 
10225
static void restore_prev_sj_state(const table_map remaining_tables, 
 
10226
                                  const JOIN_TAB *tab)
 
10227
{
 
10228
  TABLE_LIST *emb_sj_nest;
 
10229
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10230
  {
 
10231
    /* If we're removing the last SJ-inner table, remove the sj-nest */
 
10232
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) == 
 
10233
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
 
10234
    {
 
10235
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10236
    }
 
10237
  }
 
10238
}
 
10239
 
 
10240
 
 
10241
static COND *
 
10242
optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list,
 
10243
              Item::cond_result *cond_value)
 
10244
{
 
10245
  THD *thd= join->thd;
2674
10246
 
2675
10247
  if (!conds)
2676
10248
    *cond_value= Item::COND_TRUE;
2677
10249
  else
2678
10250
  {
2679
 
    /*
 
10251
    /* 
2680
10252
      Build all multiple equality predicates and eliminate equality
2681
10253
      predicates that can be inferred from these multiple equalities.
2682
10254
      For each reference of a field included into a multiple equality
2683
10255
      that occurs in a function set a pointer to the multiple equality
2684
10256
      predicate. Substitute a constant instead of this field if the
2685
10257
      multiple equality contains a constant.
2686
 
    */
2687
 
    conds= build_equal_items(join->session, conds, NULL, join_list,
 
10258
    */ 
 
10259
    conds= build_equal_items(join->thd, conds, NULL, join_list,
2688
10260
                             &join->cond_equal);
2689
10261
 
2690
10262
    /* change field = field to field = const for each found field = const */
2691
 
    list<COND_CMP> temp;
2692
 
    propagate_cond_constants(session, temp, conds, conds);
 
10263
    propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
2693
10264
    /*
2694
10265
      Remove all instances of item == item
2695
10266
      Remove all and-levels where CONST item != CONST item
2696
10267
    */
2697
 
    conds= remove_eq_conds(session, conds, cond_value) ;
 
10268
    conds= remove_eq_conds(thd, conds, cond_value) ;
2698
10269
  }
2699
10270
  return(conds);
2700
10271
}
2701
10272
 
 
10273
 
2702
10274
/**
2703
10275
  Remove const and eq items.
2704
10276
 
2709
10281
    - COND_TRUE   : always true ( 1 = 1 )
2710
10282
    - COND_FALSE  : always false        ( 1 = 2 )
2711
10283
*/
2712
 
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
10284
 
 
10285
COND *
 
10286
remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value)
2713
10287
{
2714
10288
  if (cond->type() == Item::COND_ITEM)
2715
10289
  {
2716
 
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
2717
 
 
2718
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
10290
    bool and_level= ((Item_cond*) cond)->functype()
 
10291
      == Item_func::COND_AND_FUNC;
 
10292
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2719
10293
    Item::cond_result tmp_cond_value;
2720
 
    bool should_fix_fields= false;
 
10294
    bool should_fix_fields=0;
2721
10295
 
2722
 
    *cond_value= Item::COND_UNDEF;
 
10296
    *cond_value=Item::COND_UNDEF;
2723
10297
    Item *item;
2724
 
    while ((item= li++))
 
10298
    while ((item=li++))
2725
10299
    {
2726
 
      Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
2727
 
      if (! new_item)
2728
 
              li.remove();
 
10300
      Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value);
 
10301
      if (!new_item)
 
10302
        li.remove();
2729
10303
      else if (item != new_item)
2730
10304
      {
2731
 
        li.replace(new_item);
2732
 
        should_fix_fields= true;
 
10305
        VOID(li.replace(new_item));
 
10306
        should_fix_fields=1;
2733
10307
      }
2734
10308
      if (*cond_value == Item::COND_UNDEF)
2735
 
              *cond_value= tmp_cond_value;
2736
 
 
2737
 
      switch (tmp_cond_value) 
2738
 
      {
2739
 
        case Item::COND_OK:                     /* Not true or false */
2740
 
          if (and_level || (*cond_value == Item::COND_FALSE))
2741
 
            *cond_value= tmp_cond_value;
2742
 
          break;
2743
 
        case Item::COND_FALSE:
2744
 
          if (and_level)
2745
 
          {
2746
 
            *cond_value= tmp_cond_value;
2747
 
            return (COND *) NULL;                       /* Always false */
2748
 
          }
2749
 
          break;
2750
 
        case Item::COND_TRUE:
2751
 
          if (! and_level)
2752
 
          {
2753
 
            *cond_value= tmp_cond_value;
2754
 
            return (COND *) NULL;                       /* Always true */
2755
 
          }
2756
 
          break;
2757
 
        case Item::COND_UNDEF:                  /* Impossible */
2758
 
          break;
 
10309
        *cond_value=tmp_cond_value;
 
10310
      switch (tmp_cond_value) {
 
10311
      case Item::COND_OK:                       // Not true or false
 
10312
        if (and_level || *cond_value == Item::COND_FALSE)
 
10313
          *cond_value=tmp_cond_value;
 
10314
        break;
 
10315
      case Item::COND_FALSE:
 
10316
        if (and_level)
 
10317
        {
 
10318
          *cond_value=tmp_cond_value;
 
10319
          return (COND*) 0;                     // Always false
 
10320
        }
 
10321
        break;
 
10322
      case Item::COND_TRUE:
 
10323
        if (!and_level)
 
10324
        {
 
10325
          *cond_value= tmp_cond_value;
 
10326
          return (COND*) 0;                     // Always true
 
10327
        }
 
10328
        break;
 
10329
      case Item::COND_UNDEF:                    // Impossible
 
10330
        break; /* purecov: deadcode */
2759
10331
      }
2760
10332
    }
2761
 
 
2762
10333
    if (should_fix_fields)
2763
10334
      cond->update_used_tables();
2764
10335
 
2765
 
    if (! ((Item_cond*) cond)->argument_list()->elements || *cond_value != Item::COND_OK)
2766
 
      return (COND*) NULL;
2767
 
 
 
10336
    if (!((Item_cond*) cond)->argument_list()->elements ||
 
10337
        *cond_value != Item::COND_OK)
 
10338
      return (COND*) 0;
2768
10339
    if (((Item_cond*) cond)->argument_list()->elements == 1)
2769
 
    {                                           
2770
 
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
 
10340
    {                                           // Remove list
2771
10341
      item= ((Item_cond*) cond)->argument_list()->head();
2772
 
      ((Item_cond*) cond)->argument_list()->clear();
 
10342
      ((Item_cond*) cond)->argument_list()->empty();
2773
10343
      return item;
2774
10344
    }
2775
10345
  }
2776
 
  else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
 
10346
  else if (cond->type() == Item::FUNC_ITEM &&
 
10347
           ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
2777
10348
  {
2778
10349
    /*
2779
10350
      Handles this special case for some ODBC applications:
2785
10356
      SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
2786
10357
    */
2787
10358
 
2788
 
    Item_func_isnull *func= (Item_func_isnull*) cond;
 
10359
    Item_func_isnull *func=(Item_func_isnull*) cond;
2789
10360
    Item **args= func->arguments();
2790
10361
    if (args[0]->type() == Item::FIELD_ITEM)
2791
10362
    {
2792
 
      Field *field= ((Item_field*) args[0])->field;
2793
 
      if (field->flags & AUTO_INCREMENT_FLAG 
2794
 
          && ! field->getTable()->maybe_null 
2795
 
          && session->options & OPTION_AUTO_IS_NULL
2796
 
          && (
2797
 
            session->first_successful_insert_id_in_prev_stmt > 0 
2798
 
            && session->substitute_null_with_insert_id
2799
 
            )
2800
 
          )
 
10363
      Field *field=((Item_field*) args[0])->field;
 
10364
      if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
 
10365
          (thd->options & OPTION_AUTO_IS_NULL) &&
 
10366
          (thd->first_successful_insert_id_in_prev_stmt > 0 &&
 
10367
           thd->substitute_null_with_insert_id))
2801
10368
      {
2802
 
        COND *new_cond;
2803
 
        if ((new_cond= new Item_func_eq(args[0], new Item_int("last_insert_id()",
2804
 
                                                          session->read_first_successful_insert_id_in_prev_stmt(),
2805
 
                                                          MY_INT64_NUM_DECIMAL_DIGITS))))
2806
 
        {
2807
 
          cond= new_cond;
 
10369
        COND *new_cond;
 
10370
        if ((new_cond= new Item_func_eq(args[0],
 
10371
                                        new Item_int("last_insert_id()",
 
10372
                                                     thd->read_first_successful_insert_id_in_prev_stmt(),
 
10373
                                                     MY_INT64_NUM_DECIMAL_DIGITS))))
 
10374
        {
 
10375
          cond=new_cond;
2808
10376
          /*
2809
10377
            Item_func_eq can't be fixed after creation so we do not check
2810
10378
            cond->fixed, also it do not need tables so we use 0 as second
2811
10379
            argument.
2812
10380
          */
2813
 
          cond->fix_fields(session, &cond);
2814
 
        }
 
10381
          cond->fix_fields(thd, &cond);
 
10382
        }
2815
10383
        /*
2816
10384
          IS NULL should be mapped to LAST_INSERT_ID only for first row, so
2817
10385
          clear for next row
2818
10386
        */
2819
 
        session->substitute_null_with_insert_id= false;
 
10387
        thd->substitute_null_with_insert_id= false;
2820
10388
      }
2821
 
#ifdef NOTDEFINED
2822
10389
      /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
2823
 
      else if (
2824
 
          ((field->type() == DRIZZLE_TYPE_DATE) || (field->type() == DRIZZLE_TYPE_DATETIME)) 
2825
 
          && (field->flags & NOT_NULL_FLAG) 
2826
 
          && ! field->table->maybe_null)
 
10390
      else if (((field->type() == MYSQL_TYPE_NEWDATE) ||
 
10391
                (field->type() == MYSQL_TYPE_DATETIME)) &&
 
10392
                (field->flags & NOT_NULL_FLAG) &&
 
10393
               !field->table->maybe_null)
2827
10394
      {
2828
 
        COND *new_cond;
2829
 
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
2830
 
        {
2831
 
          cond= new_cond;
 
10395
        COND *new_cond;
 
10396
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
 
10397
        {
 
10398
          cond=new_cond;
2832
10399
          /*
2833
10400
            Item_func_eq can't be fixed after creation so we do not check
2834
10401
            cond->fixed, also it do not need tables so we use 0 as second
2835
10402
            argument.
2836
10403
          */
2837
 
          cond->fix_fields(session, &cond);
2838
 
        }
 
10404
          cond->fix_fields(thd, &cond);
 
10405
        }
2839
10406
      }
2840
 
#endif /* NOTDEFINED */
2841
10407
    }
2842
10408
    if (cond->const_item())
2843
10409
    {
2844
10410
      *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2845
 
      return (COND *) NULL;
 
10411
      return (COND*) 0;
2846
10412
    }
2847
10413
  }
2848
10414
  else if (cond->const_item() && !cond->is_expensive())
2849
10415
  /*
2850
 
    @todo
 
10416
    TODO:
2851
10417
    Excluding all expensive functions is too restritive we should exclude only
2852
 
    materialized IN subquery predicates because they can't yet be evaluated
2853
 
    here (they need additional initialization that is done later on).
2854
 
 
2855
 
    The proper way to exclude the subqueries would be to walk the cond tree and
2856
 
    check for materialized subqueries there.
2857
 
 
 
10418
    materialized IN because it is created later than this phase, and cannot be
 
10419
    evaluated at this point.
 
10420
    The condition should be something as (need to fix member access):
 
10421
      !(cond->type() == Item::FUNC_ITEM &&
 
10422
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
 
10423
        ((Item_in_optimizer*)cond)->is_expensive()))
2858
10424
  */
2859
10425
  {
2860
10426
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2861
 
    return (COND *) NULL;
 
10427
    return (COND*) 0;
2862
10428
  }
2863
10429
  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
2864
 
  {                                             
2865
 
    /* boolan compare function */
 
10430
  {                                             // boolan compare function
2866
10431
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2867
10432
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2868
10433
    if (left_item->eq(right_item,1))
2869
10434
    {
2870
 
      if (!left_item->maybe_null || ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
2871
 
              return (COND*) NULL;                      /* Comparison of identical items */
 
10435
      if (!left_item->maybe_null ||
 
10436
          ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
 
10437
        return (COND*) 0;                       // Compare of identical items
2872
10438
    }
2873
10439
  }
2874
 
  *cond_value= Item::COND_OK;
2875
 
  return cond;                                  /* Point at next and return into recursion */
 
10440
  *cond_value=Item::COND_OK;
 
10441
  return cond;                                  // Point at next and level
2876
10442
}
2877
10443
 
2878
 
/*
 
10444
/* 
2879
10445
  Check if equality can be used in removing components of GROUP BY/DISTINCT
2880
 
 
 
10446
  
2881
10447
  SYNOPSIS
2882
10448
    test_if_equality_guarantees_uniqueness()
2883
10449
      l          the left comparison argument (a field if any)
2884
10450
      r          the right comparison argument (a const of any)
2885
 
 
2886
 
  DESCRIPTION
2887
 
    Checks if an equality predicate can be used to take away
2888
 
    DISTINCT/GROUP BY because it is known to be true for exactly one
 
10451
  
 
10452
  DESCRIPTION    
 
10453
    Checks if an equality predicate can be used to take away 
 
10454
    DISTINCT/GROUP BY because it is known to be true for exactly one 
2889
10455
    distinct value (e.g. <expr> == <const>).
2890
 
    Arguments must be of the same type because e.g.
2891
 
    <string_field> = <int_const> may match more than 1 distinct value from
2892
 
    the column.
2893
 
    We must take into consideration and the optimization done for various
 
10456
    Arguments must be of the same type because e.g. 
 
10457
    <string_field> = <int_const> may match more than 1 distinct value from 
 
10458
    the column. 
 
10459
    We must take into consideration and the optimization done for various 
2894
10460
    string constants when compared to dates etc (see Item_int_with_ref) as
2895
10461
    well as the collation of the arguments.
2896
 
 
2897
 
  RETURN VALUE
 
10462
  
 
10463
  RETURN VALUE  
2898
10464
    true    can be used
2899
10465
    false   cannot be used
2900
10466
*/
2901
 
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
10467
static bool
 
10468
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
2902
10469
{
2903
10470
  return r->const_item() &&
2904
10471
    /* elements must be compared as dates */
2913
10480
/**
2914
10481
  Return true if the item is a const value in all the WHERE clause.
2915
10482
*/
2916
 
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
10483
 
 
10484
static bool
 
10485
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
2917
10486
{
2918
10487
  if (cond->type() == Item::COND_ITEM)
2919
10488
  {
2920
10489
    bool and_level= (((Item_cond*) cond)->functype()
2921
10490
                     == Item_func::COND_AND_FUNC);
2922
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
10491
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2923
10492
    Item *item;
2924
10493
    while ((item=li++))
2925
10494
    {
2926
10495
      bool res=const_expression_in_where(item, comp_item, const_item);
2927
10496
      if (res)                                  // Is a const value
2928
10497
      {
2929
 
        if (and_level)
2930
 
          return 1;
 
10498
        if (and_level)
 
10499
          return 1;
2931
10500
      }
2932
10501
      else if (!and_level)
2933
 
        return 0;
 
10502
        return 0;
2934
10503
    }
2935
10504
    return and_level ? 0 : 1;
2936
10505
  }
2938
10507
  {                                             // boolan compare function
2939
10508
    Item_func* func= (Item_func*) cond;
2940
10509
    if (func->functype() != Item_func::EQUAL_FUNC &&
2941
 
              func->functype() != Item_func::EQ_FUNC)
 
10510
        func->functype() != Item_func::EQ_FUNC)
2942
10511
      return 0;
2943
10512
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2944
10513
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2946
10515
    {
2947
10516
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
2948
10517
      {
2949
 
        if (*const_item)
2950
 
          return right_item->eq(*const_item, 1);
2951
 
        *const_item=right_item;
2952
 
        return 1;
 
10518
        if (*const_item)
 
10519
          return right_item->eq(*const_item, 1);
 
10520
        *const_item=right_item;
 
10521
        return 1;
2953
10522
      }
2954
10523
    }
2955
10524
    else if (right_item->eq(comp_item,1))
2956
10525
    {
2957
10526
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
2958
10527
      {
2959
 
        if (*const_item)
2960
 
          return left_item->eq(*const_item, 1);
2961
 
        *const_item=left_item;
2962
 
        return 1;
2963
 
      }
2964
 
    }
2965
 
  }
2966
 
  return 0;
2967
 
}
 
10528
        if (*const_item)
 
10529
          return left_item->eq(*const_item, 1);
 
10530
        *const_item=left_item;
 
10531
        return 1;
 
10532
      }
 
10533
    }
 
10534
  }
 
10535
  return 0;
 
10536
}
 
10537
 
 
10538
/****************************************************************************
 
10539
  Create internal temporary table
 
10540
****************************************************************************/
 
10541
 
 
10542
/**
 
10543
  Create field for temporary table from given field.
 
10544
 
 
10545
  @param thd           Thread handler
 
10546
  @param org_field    field from which new field will be created
 
10547
  @param name         New field name
 
10548
  @param table         Temporary table
 
10549
  @param item          !=NULL if item->result_field should point to new field.
 
10550
                      This is relevant for how fill_record() is going to work:
 
10551
                      If item != NULL then fill_record() will update
 
10552
                      the record in the original table.
 
10553
                      If item == NULL then fill_record() will update
 
10554
                      the temporary table
 
10555
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
 
10556
                               field instead of blob.
 
10557
 
 
10558
  @retval
 
10559
    NULL                on error
 
10560
  @retval
 
10561
    new_created field
 
10562
*/
 
10563
 
 
10564
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
 
10565
                                   const char *name, TABLE *table,
 
10566
                                   Item_field *item, uint convert_blob_length)
 
10567
{
 
10568
  Field *new_field;
 
10569
 
 
10570
  /* 
 
10571
    Make sure that the blob fits into a Field_varstring which has 
 
10572
    2-byte lenght. 
 
10573
  */
 
10574
  if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
 
10575
      (org_field->flags & BLOB_FLAG))
 
10576
    new_field= new Field_varstring(convert_blob_length,
 
10577
                                   org_field->maybe_null(),
 
10578
                                   org_field->field_name, table->s,
 
10579
                                   org_field->charset());
 
10580
  else
 
10581
    new_field= org_field->new_field(thd->mem_root, table,
 
10582
                                    table == org_field->table);
 
10583
  if (new_field)
 
10584
  {
 
10585
    new_field->init(table);
 
10586
    new_field->orig_table= org_field->orig_table;
 
10587
    if (item)
 
10588
      item->result_field= new_field;
 
10589
    else
 
10590
      new_field->field_name= name;
 
10591
    new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
 
10592
    if (org_field->maybe_null() || (item && item->maybe_null))
 
10593
      new_field->flags&= ~NOT_NULL_FLAG;        // Because of outer join
 
10594
    if (org_field->type() == MYSQL_TYPE_VAR_STRING ||
 
10595
        org_field->type() == MYSQL_TYPE_VARCHAR)
 
10596
      table->s->db_create_options|= HA_OPTION_PACK_RECORD;
 
10597
    else if (org_field->type() == FIELD_TYPE_DOUBLE)
 
10598
      ((Field_double *) new_field)->not_fixed= true;
 
10599
  }
 
10600
  return new_field;
 
10601
}
 
10602
 
 
10603
/**
 
10604
  Create field for temporary table using type of given item.
 
10605
 
 
10606
  @param thd                   Thread handler
 
10607
  @param item                  Item to create a field for
 
10608
  @param table                 Temporary table
 
10609
  @param copy_func             If set and item is a function, store copy of
 
10610
                               item in this array
 
10611
  @param modify_item           1 if item->result_field should point to new
 
10612
                               item. This is relevent for how fill_record()
 
10613
                               is going to work:
 
10614
                               If modify_item is 1 then fill_record() will
 
10615
                               update the record in the original table.
 
10616
                               If modify_item is 0 then fill_record() will
 
10617
                               update the temporary table
 
10618
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
 
10619
                               field instead of blob.
 
10620
 
 
10621
  @retval
 
10622
    0  on error
 
10623
  @retval
 
10624
    new_created field
 
10625
*/
 
10626
 
 
10627
static Field *create_tmp_field_from_item(THD *thd __attribute__((__unused__)),
 
10628
                                         Item *item, TABLE *table,
 
10629
                                         Item ***copy_func, bool modify_item,
 
10630
                                         uint convert_blob_length)
 
10631
{
 
10632
  bool maybe_null= item->maybe_null;
 
10633
  Field *new_field;
 
10634
 
 
10635
  switch (item->result_type()) {
 
10636
  case REAL_RESULT:
 
10637
    new_field= new Field_double(item->max_length, maybe_null,
 
10638
                                item->name, item->decimals, true);
 
10639
    break;
 
10640
  case INT_RESULT:
 
10641
    /* 
 
10642
      Select an integer type with the minimal fit precision.
 
10643
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
 
10644
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into 
 
10645
      Field_long : make them Field_int64_t.  
 
10646
    */
 
10647
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
10648
      new_field=new Field_int64_t(item->max_length, maybe_null,
 
10649
                                   item->name, item->unsigned_flag);
 
10650
    else
 
10651
      new_field=new Field_long(item->max_length, maybe_null,
 
10652
                               item->name, item->unsigned_flag);
 
10653
    break;
 
10654
  case STRING_RESULT:
 
10655
    assert(item->collation.collation);
 
10656
  
 
10657
    enum enum_field_types type;
 
10658
    /*
 
10659
      DATE/TIME fields have STRING_RESULT result type. 
 
10660
      To preserve type they needed to be handled separately.
 
10661
    */
 
10662
    if ((type= item->field_type()) == MYSQL_TYPE_DATETIME ||
 
10663
        type == MYSQL_TYPE_TIME || type == MYSQL_TYPE_NEWDATE ||
 
10664
        type == MYSQL_TYPE_TIMESTAMP)
 
10665
      new_field= item->tmp_table_field_from_field_type(table, 1);
 
10666
    /* 
 
10667
      Make sure that the blob fits into a Field_varstring which has 
 
10668
      2-byte lenght. 
 
10669
    */
 
10670
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
 
10671
             convert_blob_length <= Field_varstring::MAX_SIZE && 
 
10672
             convert_blob_length)
 
10673
      new_field= new Field_varstring(convert_blob_length, maybe_null,
 
10674
                                     item->name, table->s,
 
10675
                                     item->collation.collation);
 
10676
    else
 
10677
      new_field= item->make_string_field(table);
 
10678
    new_field->set_derivation(item->collation.derivation);
 
10679
    break;
 
10680
  case DECIMAL_RESULT:
 
10681
  {
 
10682
    uint8 dec= item->decimals;
 
10683
    uint8 intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
10684
    uint32 len= item->max_length;
 
10685
 
 
10686
    /*
 
10687
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
10688
      will always throw a warning. We must limit dec to
 
10689
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
10690
    */
 
10691
 
 
10692
    if (dec > 0)
 
10693
    {
 
10694
      signed int overflow;
 
10695
 
 
10696
      dec= min(dec, DECIMAL_MAX_SCALE);
 
10697
 
 
10698
      /*
 
10699
        If the value still overflows the field with the corrected dec,
 
10700
        we'll throw out decimals rather than integers. This is still
 
10701
        bad and of course throws a truncation warning.
 
10702
        +1: for decimal point
 
10703
      */
 
10704
 
 
10705
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
10706
                                               item->unsigned_flag) - len;
 
10707
 
 
10708
      if (overflow > 0)
 
10709
        dec= max(0, dec - overflow);            // too long, discard fract
 
10710
      else
 
10711
        len -= item->decimals - dec;            // corrected value fits
 
10712
    }
 
10713
 
 
10714
    new_field= new Field_new_decimal(len, maybe_null, item->name,
 
10715
                                     dec, item->unsigned_flag);
 
10716
    break;
 
10717
  }
 
10718
  case ROW_RESULT:
 
10719
  default:
 
10720
    // This case should never be choosen
 
10721
    assert(0);
 
10722
    new_field= 0;
 
10723
    break;
 
10724
  }
 
10725
  if (new_field)
 
10726
    new_field->init(table);
 
10727
    
 
10728
  if (copy_func && item->is_result_field())
 
10729
    *((*copy_func)++) = item;                   // Save for copy_funcs
 
10730
  if (modify_item)
 
10731
    item->set_result_field(new_field);
 
10732
  if (item->type() == Item::NULL_ITEM)
 
10733
    new_field->is_created_from_null_item= true;
 
10734
  return new_field;
 
10735
}
 
10736
 
 
10737
 
 
10738
/**
 
10739
  Create field for information schema table.
 
10740
 
 
10741
  @param thd            Thread handler
 
10742
  @param table          Temporary table
 
10743
  @param item           Item to create a field for
 
10744
 
 
10745
  @retval
 
10746
    0                   on error
 
10747
  @retval
 
10748
    new_created field
 
10749
*/
 
10750
 
 
10751
Field *create_tmp_field_for_schema(THD *thd __attribute__((__unused__)),
 
10752
                                   Item *item, TABLE *table)
 
10753
{
 
10754
  if (item->field_type() == MYSQL_TYPE_VARCHAR)
 
10755
  {
 
10756
    Field *field;
 
10757
    if (item->max_length > MAX_FIELD_VARCHARLENGTH)
 
10758
      field= new Field_blob(item->max_length, item->maybe_null,
 
10759
                            item->name, item->collation.collation);
 
10760
    else
 
10761
      field= new Field_varstring(item->max_length, item->maybe_null,
 
10762
                                 item->name,
 
10763
                                 table->s, item->collation.collation);
 
10764
    if (field)
 
10765
      field->init(table);
 
10766
    return field;
 
10767
  }
 
10768
  return item->tmp_table_field_from_field_type(table, 0);
 
10769
}
 
10770
 
 
10771
 
 
10772
/**
 
10773
  Create field for temporary table.
 
10774
 
 
10775
  @param thd            Thread handler
 
10776
  @param table          Temporary table
 
10777
  @param item           Item to create a field for
 
10778
  @param type           Type of item (normally item->type)
 
10779
  @param copy_func      If set and item is a function, store copy of item
 
10780
                       in this array
 
10781
  @param from_field    if field will be created using other field as example,
 
10782
                       pointer example field will be written here
 
10783
  @param default_field  If field has a default value field, store it here
 
10784
  @param group          1 if we are going to do a relative group by on result
 
10785
  @param modify_item    1 if item->result_field should point to new item.
 
10786
                       This is relevent for how fill_record() is going to
 
10787
                       work:
 
10788
                       If modify_item is 1 then fill_record() will update
 
10789
                       the record in the original table.
 
10790
                       If modify_item is 0 then fill_record() will update
 
10791
                       the temporary table
 
10792
  @param convert_blob_length If >0 create a varstring(convert_blob_length)
 
10793
                             field instead of blob.
 
10794
 
 
10795
  @retval
 
10796
    0                   on error
 
10797
  @retval
 
10798
    new_created field
 
10799
*/
 
10800
 
 
10801
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
 
10802
                        Item ***copy_func, Field **from_field,
 
10803
                        Field **default_field,
 
10804
                        bool group, bool modify_item,
 
10805
                        bool table_cant_handle_bit_fields __attribute__((__unused__)),
 
10806
                        bool make_copy_field,
 
10807
                        uint convert_blob_length)
 
10808
{
 
10809
  Field *result;
 
10810
  Item::Type orig_type= type;
 
10811
  Item *orig_item= 0;
 
10812
 
 
10813
  if (type != Item::FIELD_ITEM &&
 
10814
      item->real_item()->type() == Item::FIELD_ITEM)
 
10815
  {
 
10816
    orig_item= item;
 
10817
    item= item->real_item();
 
10818
    type= Item::FIELD_ITEM;
 
10819
  }
 
10820
 
 
10821
  switch (type) {
 
10822
  case Item::SUM_FUNC_ITEM:
 
10823
  {
 
10824
    Item_sum *item_sum=(Item_sum*) item;
 
10825
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
 
10826
    if (!result)
 
10827
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
10828
    return result;
 
10829
  }
 
10830
  case Item::FIELD_ITEM:
 
10831
  case Item::DEFAULT_VALUE_ITEM:
 
10832
  {
 
10833
    Item_field *field= (Item_field*) item;
 
10834
    bool orig_modify= modify_item;
 
10835
    if (orig_type == Item::REF_ITEM)
 
10836
      modify_item= 0;
 
10837
    /*
 
10838
      If item have to be able to store NULLs but underlaid field can't do it,
 
10839
      create_tmp_field_from_field() can't be used for tmp field creation.
 
10840
    */
 
10841
    if (field->maybe_null && !field->field->maybe_null())
 
10842
    {
 
10843
      result= create_tmp_field_from_item(thd, item, table, NULL,
 
10844
                                         modify_item, convert_blob_length);
 
10845
      *from_field= field->field;
 
10846
      if (result && modify_item)
 
10847
        field->result_field= result;
 
10848
    } 
 
10849
    else
 
10850
      result= create_tmp_field_from_field(thd, (*from_field= field->field),
 
10851
                                          orig_item ? orig_item->name :
 
10852
                                          item->name,
 
10853
                                          table,
 
10854
                                          modify_item ? field :
 
10855
                                          NULL,
 
10856
                                          convert_blob_length);
 
10857
    if (orig_type == Item::REF_ITEM && orig_modify)
 
10858
      ((Item_ref*)orig_item)->set_result_field(result);
 
10859
    if (field->field->eq_def(result))
 
10860
      *default_field= field->field;
 
10861
    return result;
 
10862
  }
 
10863
  /* Fall through */
 
10864
  case Item::FUNC_ITEM:
 
10865
    /* Fall through */
 
10866
  case Item::COND_ITEM:
 
10867
  case Item::FIELD_AVG_ITEM:
 
10868
  case Item::FIELD_STD_ITEM:
 
10869
  case Item::SUBSELECT_ITEM:
 
10870
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
 
10871
  case Item::PROC_ITEM:
 
10872
  case Item::INT_ITEM:
 
10873
  case Item::REAL_ITEM:
 
10874
  case Item::DECIMAL_ITEM:
 
10875
  case Item::STRING_ITEM:
 
10876
  case Item::REF_ITEM:
 
10877
  case Item::NULL_ITEM:
 
10878
  case Item::VARBIN_ITEM:
 
10879
    if (make_copy_field)
 
10880
    {
 
10881
      assert(((Item_result_field*)item)->result_field);
 
10882
      *from_field= ((Item_result_field*)item)->result_field;
 
10883
    }
 
10884
    return create_tmp_field_from_item(thd, item, table,
 
10885
                                      (make_copy_field ? 0 : copy_func),
 
10886
                                       modify_item, convert_blob_length);
 
10887
  case Item::TYPE_HOLDER:  
 
10888
    result= ((Item_type_holder *)item)->make_field_by_type(table);
 
10889
    result->set_derivation(item->collation.derivation);
 
10890
    return result;
 
10891
  default:                                      // Dosen't have to be stored
 
10892
    return 0;
 
10893
  }
 
10894
}
 
10895
 
 
10896
/*
 
10897
  Set up column usage bitmaps for a temporary table
 
10898
 
 
10899
  IMPLEMENTATION
 
10900
    For temporary tables, we need one bitmap with all columns set and
 
10901
    a tmp_set bitmap to be used by things like filesort.
 
10902
*/
 
10903
 
 
10904
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
 
10905
{
 
10906
  uint field_count= table->s->fields;
 
10907
  bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
 
10908
              false);
 
10909
  bitmap_init(&table->tmp_set,
 
10910
              (my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
 
10911
              field_count, false);
 
10912
  /* write_set and all_set are copies of read_set */
 
10913
  table->def_write_set= table->def_read_set;
 
10914
  table->s->all_set= table->def_read_set;
 
10915
  bitmap_set_all(&table->s->all_set);
 
10916
  table->default_column_bitmaps();
 
10917
}
 
10918
 
 
10919
 
 
10920
/**
 
10921
  Create a temp table according to a field list.
 
10922
 
 
10923
  Given field pointers are changed to point at tmp_table for
 
10924
  send_fields. The table object is self contained: it's
 
10925
  allocated in its own memory root, as well as Field objects
 
10926
  created for table columns.
 
10927
  This function will replace Item_sum items in 'fields' list with
 
10928
  corresponding Item_field items, pointing at the fields in the
 
10929
  temporary table, unless this was prohibited by true
 
10930
  value of argument save_sum_fields. The Item_field objects
 
10931
  are created in THD memory root.
 
10932
 
 
10933
  @param thd                  thread handle
 
10934
  @param param                a description used as input to create the table
 
10935
  @param fields               list of items that will be used to define
 
10936
                              column types of the table (also see NOTES)
 
10937
  @param group                TODO document
 
10938
  @param distinct             should table rows be distinct
 
10939
  @param save_sum_fields      see NOTES
 
10940
  @param select_options
 
10941
  @param rows_limit
 
10942
  @param table_alias          possible name of the temporary table that can
 
10943
                              be used for name resolving; can be "".
 
10944
*/
 
10945
 
 
10946
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
 
10947
#define AVG_STRING_LENGTH_TO_PACK_ROWS   64
 
10948
#define RATIO_TO_PACK_ROWS             2
 
10949
#define MIN_STRING_LENGTH_TO_PACK_ROWS   10
 
10950
 
 
10951
TABLE *
 
10952
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
 
10953
                 ORDER *group, bool distinct, bool save_sum_fields,
 
10954
                 uint64_t select_options, ha_rows rows_limit,
 
10955
                 char *table_alias)
 
10956
{
 
10957
  MEM_ROOT *mem_root_save, own_root;
 
10958
  TABLE *table;
 
10959
  TABLE_SHARE *share;
 
10960
  uint  i,field_count,null_count,null_pack_length;
 
10961
  uint  copy_func_count= param->func_count;
 
10962
  uint  hidden_null_count, hidden_null_pack_length, hidden_field_count;
 
10963
  uint  blob_count,group_null_items, string_count;
 
10964
  uint  temp_pool_slot=MY_BIT_NONE;
 
10965
  uint fieldnr= 0;
 
10966
  ulong reclength, string_total_length;
 
10967
  bool  using_unique_constraint= 0;
 
10968
  bool  use_packed_rows= 0;
 
10969
  bool  not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
 
10970
  char  *tmpname,path[FN_REFLEN];
 
10971
  uchar *pos, *group_buff, *bitmaps;
 
10972
  uchar *null_flags;
 
10973
  Field **reg_field, **from_field, **default_field;
 
10974
  uint *blob_field;
 
10975
  Copy_field *copy=0;
 
10976
  KEY *keyinfo;
 
10977
  KEY_PART_INFO *key_part_info;
 
10978
  Item **copy_func;
 
10979
  MI_COLUMNDEF *recinfo;
 
10980
  uint total_uneven_bit_length= 0;
 
10981
  bool force_copy_fields= param->force_copy_fields;
 
10982
 
 
10983
  status_var_increment(thd->status_var.created_tmp_tables);
 
10984
 
 
10985
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
 
10986
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
 
10987
 
 
10988
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
 
10989
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
 
10990
            current_pid, temp_pool_slot);
 
10991
  else
 
10992
  {
 
10993
    /* if we run out of slots or we are not using tempool */
 
10994
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
 
10995
            thd->thread_id, thd->tmp_table++);
 
10996
  }
 
10997
 
 
10998
  /*
 
10999
    No need to change table name to lower case as we are only creating
 
11000
    MyISAM or HEAP tables here
 
11001
  */
 
11002
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
 
11003
 
 
11004
 
 
11005
  if (group)
 
11006
  {
 
11007
    if (!param->quick_group)
 
11008
      group=0;                                  // Can't use group key
 
11009
    else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
 
11010
    {
 
11011
      /*
 
11012
        marker == 4 means two things:
 
11013
        - store NULLs in the key, and
 
11014
        - convert BIT fields to 64-bit long, needed because MEMORY tables
 
11015
          can't index BIT fields.
 
11016
      */
 
11017
      (*tmp->item)->marker= 4;
 
11018
      if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
 
11019
        using_unique_constraint=1;
 
11020
    }
 
11021
    if (param->group_length >= MAX_BLOB_WIDTH)
 
11022
      using_unique_constraint=1;
 
11023
    if (group)
 
11024
      distinct=0;                               // Can't use distinct
 
11025
  }
 
11026
 
 
11027
  field_count=param->field_count+param->func_count+param->sum_func_count;
 
11028
  hidden_field_count=param->hidden_field_count;
 
11029
 
 
11030
  /*
 
11031
    When loose index scan is employed as access method, it already
 
11032
    computes all groups and the result of all aggregate functions. We
 
11033
    make space for the items of the aggregate function in the list of
 
11034
    functions TMP_TABLE_PARAM::items_to_copy, so that the values of
 
11035
    these items are stored in the temporary table.
 
11036
  */
 
11037
  if (param->precomputed_group_by)
 
11038
    copy_func_count+= param->sum_func_count;
 
11039
  
 
11040
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
11041
 
 
11042
  if (!multi_alloc_root(&own_root,
 
11043
                        &table, sizeof(*table),
 
11044
                        &share, sizeof(*share),
 
11045
                        &reg_field, sizeof(Field*) * (field_count+1),
 
11046
                        &default_field, sizeof(Field*) * (field_count),
 
11047
                        &blob_field, sizeof(uint)*(field_count+1),
 
11048
                        &from_field, sizeof(Field*)*field_count,
 
11049
                        &copy_func, sizeof(*copy_func)*(copy_func_count+1),
 
11050
                        &param->keyinfo, sizeof(*param->keyinfo),
 
11051
                        &key_part_info,
 
11052
                        sizeof(*key_part_info)*(param->group_parts+1),
 
11053
                        &param->start_recinfo,
 
11054
                        sizeof(*param->recinfo)*(field_count*2+4),
 
11055
                        &tmpname, (uint) strlen(path)+1,
 
11056
                        &group_buff, (group && ! using_unique_constraint ?
 
11057
                                      param->group_length : 0),
 
11058
                        &bitmaps, bitmap_buffer_size(field_count)*2,
 
11059
                        NullS))
 
11060
  {
 
11061
    if (temp_pool_slot != MY_BIT_NONE)
 
11062
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11063
    return(NULL);                               /* purecov: inspected */
 
11064
  }
 
11065
  /* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
 
11066
  if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
 
11067
  {
 
11068
    if (temp_pool_slot != MY_BIT_NONE)
 
11069
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11070
    free_root(&own_root, MYF(0));               /* purecov: inspected */
 
11071
    return(NULL);                               /* purecov: inspected */
 
11072
  }
 
11073
  param->items_to_copy= copy_func;
 
11074
  strmov(tmpname,path);
 
11075
  /* make table according to fields */
 
11076
 
 
11077
  bzero((char*) table,sizeof(*table));
 
11078
  bzero((char*) reg_field,sizeof(Field*)*(field_count+1));
 
11079
  bzero((char*) default_field, sizeof(Field*) * (field_count));
 
11080
  bzero((char*) from_field,sizeof(Field*)*field_count);
 
11081
 
 
11082
  table->mem_root= own_root;
 
11083
  mem_root_save= thd->mem_root;
 
11084
  thd->mem_root= &table->mem_root;
 
11085
 
 
11086
  table->field=reg_field;
 
11087
  table->alias= table_alias;
 
11088
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
 
11089
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
 
11090
  table->map=1;
 
11091
  table->temp_pool_slot = temp_pool_slot;
 
11092
  table->copy_blobs= 1;
 
11093
  table->in_use= thd;
 
11094
  table->quick_keys.init();
 
11095
  table->covering_keys.init();
 
11096
  table->keys_in_use_for_query.init();
 
11097
 
 
11098
  table->s= share;
 
11099
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
 
11100
  share->blob_field= blob_field;
 
11101
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
11102
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
 
11103
  share->table_charset= param->table_charset;
 
11104
  share->primary_key= MAX_KEY;               // Indicate no primary key
 
11105
  share->keys_for_keyread.init();
 
11106
  share->keys_in_use.init();
 
11107
 
 
11108
  /* Calculate which type of fields we will store in the temporary table */
 
11109
 
 
11110
  reclength= string_total_length= 0;
 
11111
  blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
 
11112
  param->using_indirect_summary_function=0;
 
11113
 
 
11114
  List_iterator_fast<Item> li(fields);
 
11115
  Item *item;
 
11116
  Field **tmp_from_field=from_field;
 
11117
  while ((item=li++))
 
11118
  {
 
11119
    Item::Type type=item->type();
 
11120
    if (not_all_columns)
 
11121
    {
 
11122
      if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
 
11123
      {
 
11124
        if (item->used_tables() & OUTER_REF_TABLE_BIT)
 
11125
          item->update_used_tables();
 
11126
        if (type == Item::SUBSELECT_ITEM ||
 
11127
            (item->used_tables() & ~OUTER_REF_TABLE_BIT))
 
11128
        {
 
11129
          /*
 
11130
            Mark that the we have ignored an item that refers to a summary
 
11131
            function. We need to know this if someone is going to use
 
11132
            DISTINCT on the result.
 
11133
          */
 
11134
          param->using_indirect_summary_function=1;
 
11135
          continue;
 
11136
        }
 
11137
      }
 
11138
      if (item->const_item() && (int) hidden_field_count <= 0)
 
11139
        continue; // We don't have to store this
 
11140
    }
 
11141
    if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
 
11142
    {                                           /* Can't calc group yet */
 
11143
      ((Item_sum*) item)->result_field=0;
 
11144
      for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
 
11145
      {
 
11146
        Item **argp= ((Item_sum*) item)->args + i;
 
11147
        Item *arg= *argp;
 
11148
        if (!arg->const_item())
 
11149
        {
 
11150
          Field *new_field=
 
11151
            create_tmp_field(thd, table, arg, arg->type(), &copy_func,
 
11152
                             tmp_from_field, &default_field[fieldnr],
 
11153
                             group != 0,not_all_columns,
 
11154
                             distinct, 0,
 
11155
                             param->convert_blob_length);
 
11156
          if (!new_field)
 
11157
            goto err;                                   // Should be OOM
 
11158
          tmp_from_field++;
 
11159
          reclength+=new_field->pack_length();
 
11160
          if (new_field->flags & BLOB_FLAG)
 
11161
          {
 
11162
            *blob_field++= fieldnr;
 
11163
            blob_count++;
 
11164
          }
 
11165
          *(reg_field++)= new_field;
 
11166
          if (new_field->real_type() == MYSQL_TYPE_STRING ||
 
11167
              new_field->real_type() == MYSQL_TYPE_VARCHAR)
 
11168
          {
 
11169
            string_count++;
 
11170
            string_total_length+= new_field->pack_length();
 
11171
          }
 
11172
          thd->mem_root= mem_root_save;
 
11173
          thd->change_item_tree(argp, new Item_field(new_field));
 
11174
          thd->mem_root= &table->mem_root;
 
11175
          if (!(new_field->flags & NOT_NULL_FLAG))
 
11176
          {
 
11177
            null_count++;
 
11178
            /*
 
11179
              new_field->maybe_null() is still false, it will be
 
11180
              changed below. But we have to setup Item_field correctly
 
11181
            */
 
11182
            (*argp)->maybe_null=1;
 
11183
          }
 
11184
          new_field->field_index= fieldnr++;
 
11185
        }
 
11186
      }
 
11187
    }
 
11188
    else
 
11189
    {
 
11190
      /*
 
11191
        The last parameter to create_tmp_field() is a bit tricky:
 
11192
 
 
11193
        We need to set it to 0 in union, to get fill_record() to modify the
 
11194
        temporary table.
 
11195
        We need to set it to 1 on multi-table-update and in select to
 
11196
        write rows to the temporary table.
 
11197
        We here distinguish between UNION and multi-table-updates by the fact
 
11198
        that in the later case group is set to the row pointer.
 
11199
      */
 
11200
      Field *new_field= (param->schema_table) ?
 
11201
        create_tmp_field_for_schema(thd, item, table) :
 
11202
        create_tmp_field(thd, table, item, type, &copy_func,
 
11203
                         tmp_from_field, &default_field[fieldnr],
 
11204
                         group != 0,
 
11205
                         !force_copy_fields &&
 
11206
                           (not_all_columns || group !=0),
 
11207
                         /*
 
11208
                           If item->marker == 4 then we force create_tmp_field
 
11209
                           to create a 64-bit longs for BIT fields because HEAP
 
11210
                           tables can't index BIT fields directly. We do the same
 
11211
                           for distinct, as we want the distinct index to be
 
11212
                           usable in this case too.
 
11213
                         */
 
11214
                         item->marker == 4 || param->bit_fields_as_long,
 
11215
                         force_copy_fields,
 
11216
                         param->convert_blob_length);
 
11217
 
 
11218
      if (!new_field)
 
11219
      {
 
11220
        if (thd->is_fatal_error)
 
11221
          goto err;                             // Got OOM
 
11222
        continue;                               // Some kindf of const item
 
11223
      }
 
11224
      if (type == Item::SUM_FUNC_ITEM)
 
11225
        ((Item_sum *) item)->result_field= new_field;
 
11226
      tmp_from_field++;
 
11227
      reclength+=new_field->pack_length();
 
11228
      if (!(new_field->flags & NOT_NULL_FLAG))
 
11229
        null_count++;
 
11230
      if (new_field->flags & BLOB_FLAG)
 
11231
      {
 
11232
        *blob_field++= fieldnr;
 
11233
        blob_count++;
 
11234
      }
 
11235
      if (item->marker == 4 && item->maybe_null)
 
11236
      {
 
11237
        group_null_items++;
 
11238
        new_field->flags|= GROUP_FLAG;
 
11239
      }
 
11240
      new_field->field_index= fieldnr++;
 
11241
      *(reg_field++)= new_field;
 
11242
    }
 
11243
    if (!--hidden_field_count)
 
11244
    {
 
11245
      /*
 
11246
        This was the last hidden field; Remember how many hidden fields could
 
11247
        have null
 
11248
      */
 
11249
      hidden_null_count=null_count;
 
11250
      /*
 
11251
        We need to update hidden_field_count as we may have stored group
 
11252
        functions with constant arguments
 
11253
      */
 
11254
      param->hidden_field_count= fieldnr;
 
11255
      null_count= 0;
 
11256
    }
 
11257
  }
 
11258
  assert(fieldnr == (uint) (reg_field - table->field));
 
11259
  assert(field_count >= (uint) (reg_field - table->field));
 
11260
  field_count= fieldnr;
 
11261
  *reg_field= 0;
 
11262
  *blob_field= 0;                               // End marker
 
11263
  share->fields= field_count;
 
11264
 
 
11265
  /* If result table is small; use a heap */
 
11266
  /* future: storage engine selection can be made dynamic? */
 
11267
  if (blob_count || using_unique_constraint ||
 
11268
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
 
11269
      OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
 
11270
  {
 
11271
    share->db_plugin= ha_lock_engine(0, myisam_hton);
 
11272
    table->file= get_new_handler(share, &table->mem_root,
 
11273
                                 share->db_type());
 
11274
    if (group &&
 
11275
        (param->group_parts > table->file->max_key_parts() ||
 
11276
         param->group_length > table->file->max_key_length()))
 
11277
      using_unique_constraint=1;
 
11278
  }
 
11279
  else
 
11280
  {
 
11281
    share->db_plugin= ha_lock_engine(0, heap_hton);
 
11282
    table->file= get_new_handler(share, &table->mem_root,
 
11283
                                 share->db_type());
 
11284
  }
 
11285
  if (!table->file)
 
11286
    goto err;
 
11287
 
 
11288
 
 
11289
  if (!using_unique_constraint)
 
11290
    reclength+= group_null_items;       // null flag is stored separately
 
11291
 
 
11292
  share->blob_fields= blob_count;
 
11293
  if (blob_count == 0)
 
11294
  {
 
11295
    /* We need to ensure that first byte is not 0 for the delete link */
 
11296
    if (param->hidden_field_count)
 
11297
      hidden_null_count++;
 
11298
    else
 
11299
      null_count++;
 
11300
  }
 
11301
  hidden_null_pack_length=(hidden_null_count+7)/8;
 
11302
  null_pack_length= (hidden_null_pack_length +
 
11303
                     (null_count + total_uneven_bit_length + 7) / 8);
 
11304
  reclength+=null_pack_length;
 
11305
  if (!reclength)
 
11306
    reclength=1;                                // Dummy select
 
11307
  /* Use packed rows if there is blobs or a lot of space to gain */
 
11308
  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)))
 
11309
    use_packed_rows= 1;
 
11310
 
 
11311
  share->reclength= reclength;
 
11312
  {
 
11313
    uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
 
11314
    share->rec_buff_length= alloc_length;
 
11315
    if (!(table->record[0]= (uchar*)
 
11316
                            alloc_root(&table->mem_root, alloc_length*3)))
 
11317
      goto err;
 
11318
    table->record[1]= table->record[0]+alloc_length;
 
11319
    share->default_values= table->record[1]+alloc_length;
 
11320
  }
 
11321
  copy_func[0]=0;                               // End marker
 
11322
  param->func_count= copy_func - param->items_to_copy; 
 
11323
 
 
11324
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
11325
 
 
11326
  recinfo=param->start_recinfo;
 
11327
  null_flags=(uchar*) table->record[0];
 
11328
  pos=table->record[0]+ null_pack_length;
 
11329
  if (null_pack_length)
 
11330
  {
 
11331
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11332
    recinfo->type=FIELD_NORMAL;
 
11333
    recinfo->length=null_pack_length;
 
11334
    recinfo++;
 
11335
    bfill(null_flags,null_pack_length,255);     // Set null fields
 
11336
 
 
11337
    table->null_flags= (uchar*) table->record[0];
 
11338
    share->null_fields= null_count+ hidden_null_count;
 
11339
    share->null_bytes= null_pack_length;
 
11340
  }
 
11341
  null_count= (blob_count == 0) ? 1 : 0;
 
11342
  hidden_field_count=param->hidden_field_count;
 
11343
  for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
 
11344
  {
 
11345
    Field *field= *reg_field;
 
11346
    uint length;
 
11347
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11348
 
 
11349
    if (!(field->flags & NOT_NULL_FLAG))
 
11350
    {
 
11351
      if (field->flags & GROUP_FLAG && !using_unique_constraint)
 
11352
      {
 
11353
        /*
 
11354
          We have to reserve one byte here for NULL bits,
 
11355
          as this is updated by 'end_update()'
 
11356
        */
 
11357
        *pos++=0;                               // Null is stored here
 
11358
        recinfo->length=1;
 
11359
        recinfo->type=FIELD_NORMAL;
 
11360
        recinfo++;
 
11361
        bzero((uchar*) recinfo,sizeof(*recinfo));
 
11362
      }
 
11363
      else
 
11364
      {
 
11365
        recinfo->null_bit= 1 << (null_count & 7);
 
11366
        recinfo->null_pos= null_count/8;
 
11367
      }
 
11368
      field->move_field(pos,null_flags+null_count/8,
 
11369
                        1 << (null_count & 7));
 
11370
      null_count++;
 
11371
    }
 
11372
    else
 
11373
      field->move_field(pos,(uchar*) 0,0);
 
11374
    field->reset();
 
11375
 
 
11376
    /*
 
11377
      Test if there is a default field value. The test for ->ptr is to skip
 
11378
      'offset' fields generated by initalize_tables
 
11379
    */
 
11380
    if (default_field[i] && default_field[i]->ptr)
 
11381
    {
 
11382
      /* 
 
11383
         default_field[i] is set only in the cases  when 'field' can
 
11384
         inherit the default value that is defined for the field referred
 
11385
         by the Item_field object from which 'field' has been created.
 
11386
      */
 
11387
      my_ptrdiff_t diff;
 
11388
      Field *orig_field= default_field[i];
 
11389
      /* Get the value from default_values */
 
11390
      diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
 
11391
                            orig_field->table->record[0]);
 
11392
      orig_field->move_field_offset(diff);      // Points now at default_values
 
11393
      if (orig_field->is_real_null())
 
11394
        field->set_null();
 
11395
      else
 
11396
      {
 
11397
        field->set_notnull();
 
11398
        memcpy(field->ptr, orig_field->ptr, field->pack_length());
 
11399
      }
 
11400
      orig_field->move_field_offset(-diff);     // Back to record[0]
 
11401
    } 
 
11402
 
 
11403
    if (from_field[i])
 
11404
    {                                           /* Not a table Item */
 
11405
      copy->set(field,from_field[i],save_sum_fields);
 
11406
      copy++;
 
11407
    }
 
11408
    length=field->pack_length();
 
11409
    pos+= length;
 
11410
 
 
11411
    /* Make entry for create table */
 
11412
    recinfo->length=length;
 
11413
    if (field->flags & BLOB_FLAG)
 
11414
      recinfo->type= (int) FIELD_BLOB;
 
11415
    else if (use_packed_rows &&
 
11416
             field->real_type() == MYSQL_TYPE_STRING &&
 
11417
             length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
 
11418
      recinfo->type=FIELD_SKIP_ENDSPACE;
 
11419
    else
 
11420
      recinfo->type=FIELD_NORMAL;
 
11421
    if (!--hidden_field_count)
 
11422
      null_count=(null_count+7) & ~7;           // move to next byte
 
11423
 
 
11424
    // fix table name in field entry
 
11425
    field->table_name= &table->alias;
 
11426
  }
 
11427
 
 
11428
  param->copy_field_end=copy;
 
11429
  param->recinfo=recinfo;
 
11430
  store_record(table,s->default_values);        // Make empty default record
 
11431
 
 
11432
  if (thd->variables.tmp_table_size == ~ (uint64_t) 0)          // No limit
 
11433
    share->max_rows= ~(ha_rows) 0;
 
11434
  else
 
11435
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
 
11436
                                 min(thd->variables.tmp_table_size,
 
11437
                                     thd->variables.max_heap_table_size) :
 
11438
                                 thd->variables.tmp_table_size) /
 
11439
                                 share->reclength);
 
11440
  set_if_bigger(share->max_rows,1);             // For dummy start options
 
11441
  /*
 
11442
    Push the LIMIT clause to the temporary table creation, so that we
 
11443
    materialize only up to 'rows_limit' records instead of all result records.
 
11444
  */
 
11445
  set_if_smaller(share->max_rows, rows_limit);
 
11446
  param->end_write_records= rows_limit;
 
11447
 
 
11448
  keyinfo= param->keyinfo;
 
11449
 
 
11450
  if (group)
 
11451
  {
 
11452
    table->group=group;                         /* Table is grouped by key */
 
11453
    param->group_buff=group_buff;
 
11454
    share->keys=1;
 
11455
    share->uniques= test(using_unique_constraint);
 
11456
    table->key_info=keyinfo;
 
11457
    keyinfo->key_part=key_part_info;
 
11458
    keyinfo->flags=HA_NOSAME;
 
11459
    keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
 
11460
    keyinfo->key_length=0;
 
11461
    keyinfo->rec_per_key=0;
 
11462
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11463
    keyinfo->name= (char*) "group_key";
 
11464
    ORDER *cur_group= group;
 
11465
    for (; cur_group ; cur_group= cur_group->next, key_part_info++)
 
11466
    {
 
11467
      Field *field=(*cur_group->item)->get_tmp_table_field();
 
11468
      bool maybe_null=(*cur_group->item)->maybe_null;
 
11469
      key_part_info->null_bit=0;
 
11470
      key_part_info->field=  field;
 
11471
      key_part_info->offset= field->offset(table->record[0]);
 
11472
      key_part_info->length= (uint16) field->key_length();
 
11473
      key_part_info->type=   (uint8) field->key_type();
 
11474
      key_part_info->key_type =
 
11475
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
 
11476
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
 
11477
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
 
11478
        0 : FIELDFLAG_BINARY;
 
11479
      if (!using_unique_constraint)
 
11480
      {
 
11481
        cur_group->buff=(char*) group_buff;
 
11482
        if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
 
11483
                                                     group_buff +
 
11484
                                                     test(maybe_null),
 
11485
                                                     field->null_ptr,
 
11486
                                                     field->null_bit)))
 
11487
          goto err; /* purecov: inspected */
 
11488
        if (maybe_null)
 
11489
        {
 
11490
          /*
 
11491
            To be able to group on NULL, we reserved place in group_buff
 
11492
            for the NULL flag just before the column. (see above).
 
11493
            The field data is after this flag.
 
11494
            The NULL flag is updated in 'end_update()' and 'end_write()'
 
11495
          */
 
11496
          keyinfo->flags|= HA_NULL_ARE_EQUAL;   // def. that NULL == NULL
 
11497
          key_part_info->null_bit=field->null_bit;
 
11498
          key_part_info->null_offset= (uint) (field->null_ptr -
 
11499
                                              (uchar*) table->record[0]);
 
11500
          cur_group->buff++;                        // Pointer to field data
 
11501
          group_buff++;                         // Skipp null flag
 
11502
        }
 
11503
        /* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
 
11504
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
 
11505
        group_buff+= cur_group->field->pack_length();
 
11506
      }
 
11507
      keyinfo->key_length+=  key_part_info->length;
 
11508
    }
 
11509
  }
 
11510
 
 
11511
  if (distinct && field_count != param->hidden_field_count)
 
11512
  {
 
11513
    /*
 
11514
      Create an unique key or an unique constraint over all columns
 
11515
      that should be in the result.  In the temporary table, there are
 
11516
      'param->hidden_field_count' extra columns, whose null bits are stored
 
11517
      in the first 'hidden_null_pack_length' bytes of the row.
 
11518
    */
 
11519
    if (blob_count)
 
11520
    {
 
11521
      /*
 
11522
        Special mode for index creation in MyISAM used to support unique
 
11523
        indexes on blobs with arbitrary length. Such indexes cannot be
 
11524
        used for lookups.
 
11525
      */
 
11526
      share->uniques= 1;
 
11527
    }
 
11528
    null_pack_length-=hidden_null_pack_length;
 
11529
    keyinfo->key_parts= ((field_count-param->hidden_field_count)+
 
11530
                         (share->uniques ? test(null_pack_length) : 0));
 
11531
    table->distinct= 1;
 
11532
    share->keys= 1;
 
11533
    if (!(key_part_info= (KEY_PART_INFO*)
 
11534
          alloc_root(&table->mem_root,
 
11535
                     keyinfo->key_parts * sizeof(KEY_PART_INFO))))
 
11536
      goto err;
 
11537
    bzero((void*) key_part_info, keyinfo->key_parts * sizeof(KEY_PART_INFO));
 
11538
    table->key_info=keyinfo;
 
11539
    keyinfo->key_part=key_part_info;
 
11540
    keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
 
11541
    keyinfo->key_length=(uint16) reclength;
 
11542
    keyinfo->name= (char*) "distinct_key";
 
11543
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11544
    keyinfo->rec_per_key=0;
 
11545
 
 
11546
    /*
 
11547
      Create an extra field to hold NULL bits so that unique indexes on
 
11548
      blobs can distinguish NULL from 0. This extra field is not needed
 
11549
      when we do not use UNIQUE indexes for blobs.
 
11550
    */
 
11551
    if (null_pack_length && share->uniques)
 
11552
    {
 
11553
      key_part_info->null_bit=0;
 
11554
      key_part_info->offset=hidden_null_pack_length;
 
11555
      key_part_info->length=null_pack_length;
 
11556
      key_part_info->field= new Field_string(table->record[0],
 
11557
                                             (uint32) key_part_info->length,
 
11558
                                             (uchar*) 0,
 
11559
                                             (uint) 0,
 
11560
                                             Field::NONE,
 
11561
                                             NullS, &my_charset_bin);
 
11562
      if (!key_part_info->field)
 
11563
        goto err;
 
11564
      key_part_info->field->init(table);
 
11565
      key_part_info->key_type=FIELDFLAG_BINARY;
 
11566
      key_part_info->type=    HA_KEYTYPE_BINARY;
 
11567
      key_part_info++;
 
11568
    }
 
11569
    /* Create a distinct key over the columns we are going to return */
 
11570
    for (i=param->hidden_field_count, reg_field=table->field + i ;
 
11571
         i < field_count;
 
11572
         i++, reg_field++, key_part_info++)
 
11573
    {
 
11574
      key_part_info->null_bit=0;
 
11575
      key_part_info->field=    *reg_field;
 
11576
      key_part_info->offset=   (*reg_field)->offset(table->record[0]);
 
11577
      key_part_info->length=   (uint16) (*reg_field)->pack_length();
 
11578
      /* TODO:
 
11579
        The below method of computing the key format length of the
 
11580
        key part is a copy/paste from opt_range.cc, and table.cc.
 
11581
        This should be factored out, e.g. as a method of Field.
 
11582
        In addition it is not clear if any of the Field::*_length
 
11583
        methods is supposed to compute the same length. If so, it
 
11584
        might be reused.
 
11585
      */
 
11586
      key_part_info->store_length= key_part_info->length;
 
11587
 
 
11588
      if ((*reg_field)->real_maybe_null())
 
11589
        key_part_info->store_length+= HA_KEY_NULL_LENGTH;
 
11590
      if ((*reg_field)->type() == MYSQL_TYPE_BLOB || 
 
11591
          (*reg_field)->real_type() == MYSQL_TYPE_VARCHAR)
 
11592
        key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
 
11593
 
 
11594
      key_part_info->type=     (uint8) (*reg_field)->key_type();
 
11595
      key_part_info->key_type =
 
11596
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
 
11597
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
 
11598
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
 
11599
        0 : FIELDFLAG_BINARY;
 
11600
    }
 
11601
  }
 
11602
 
 
11603
  if (thd->is_fatal_error)                              // If end of memory
 
11604
    goto err;                                    /* purecov: inspected */
 
11605
  share->db_record_offset= 1;
 
11606
  if (share->db_type() == myisam_hton)
 
11607
  {
 
11608
    if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
 
11609
                                &param->recinfo, select_options))
 
11610
      goto err;
 
11611
  }
 
11612
  if (open_tmp_table(table))
 
11613
    goto err;
 
11614
 
 
11615
  thd->mem_root= mem_root_save;
 
11616
 
 
11617
  return(table);
 
11618
 
 
11619
err:
 
11620
  thd->mem_root= mem_root_save;
 
11621
  free_tmp_table(thd,table);                    /* purecov: inspected */
 
11622
  if (temp_pool_slot != MY_BIT_NONE)
 
11623
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11624
  return(NULL);                         /* purecov: inspected */
 
11625
}
 
11626
 
 
11627
 
 
11628
 
 
11629
 
 
11630
/*
 
11631
  Create a temporary table to weed out duplicate rowid combinations
 
11632
 
 
11633
  SYNOPSIS
 
11634
 
 
11635
    create_duplicate_weedout_tmp_table()
 
11636
      thd
 
11637
      uniq_tuple_length_arg
 
11638
      SJ_TMP_TABLE 
 
11639
 
 
11640
  DESCRIPTION
 
11641
    Create a temporary table to weed out duplicate rowid combinations. The
 
11642
    table has a single column that is a concatenation of all rowids in the
 
11643
    combination. 
 
11644
 
 
11645
    Depending on the needed length, there are two cases:
 
11646
 
 
11647
    1. When the length of the column < max_key_length:
 
11648
 
 
11649
      CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
 
11650
 
 
11651
    2. Otherwise (not a valid SQL syntax but internally supported):
 
11652
 
 
11653
      CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
 
11654
 
 
11655
    The code in this function was produced by extraction of relevant parts
 
11656
    from create_tmp_table().
 
11657
 
 
11658
  RETURN
 
11659
    created table
 
11660
    NULL on error
 
11661
*/
 
11662
 
 
11663
TABLE *create_duplicate_weedout_tmp_table(THD *thd, 
 
11664
                                          uint uniq_tuple_length_arg,
 
11665
                                          SJ_TMP_TABLE *sjtbl)
 
11666
{
 
11667
  MEM_ROOT *mem_root_save, own_root;
 
11668
  TABLE *table;
 
11669
  TABLE_SHARE *share;
 
11670
  uint  temp_pool_slot=MY_BIT_NONE;
 
11671
  char  *tmpname,path[FN_REFLEN];
 
11672
  Field **reg_field;
 
11673
  KEY_PART_INFO *key_part_info;
 
11674
  KEY *keyinfo;
 
11675
  uchar *group_buff;
 
11676
  uchar *bitmaps;
 
11677
  uint *blob_field;
 
11678
  MI_COLUMNDEF *recinfo, *start_recinfo;
 
11679
  bool using_unique_constraint=false;
 
11680
  bool use_packed_rows= false;
 
11681
  Field *field, *key_field;
 
11682
  uint blob_count, null_pack_length, null_count;
 
11683
  uchar *null_flags;
 
11684
  uchar *pos;
 
11685
  
 
11686
  /*
 
11687
    STEP 1: Get temporary table name
 
11688
  */
 
11689
  statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
 
11690
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
 
11691
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
 
11692
 
 
11693
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
 
11694
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
 
11695
            current_pid, temp_pool_slot);
 
11696
  else
 
11697
  {
 
11698
    /* if we run out of slots or we are not using tempool */
 
11699
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
 
11700
            thd->thread_id, thd->tmp_table++);
 
11701
  }
 
11702
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
 
11703
 
 
11704
  /* STEP 2: Figure if we'll be using a key or blob+constraint */
 
11705
  if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
 
11706
    using_unique_constraint= true;
 
11707
 
 
11708
  /* STEP 3: Allocate memory for temptable description */
 
11709
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
11710
  if (!multi_alloc_root(&own_root,
 
11711
                        &table, sizeof(*table),
 
11712
                        &share, sizeof(*share),
 
11713
                        &reg_field, sizeof(Field*) * (1+1),
 
11714
                        &blob_field, sizeof(uint)*2,
 
11715
                        &keyinfo, sizeof(*keyinfo),
 
11716
                        &key_part_info, sizeof(*key_part_info) * 2,
 
11717
                        &start_recinfo,
 
11718
                        sizeof(*recinfo)*(1*2+4),
 
11719
                        &tmpname, (uint) strlen(path)+1,
 
11720
                        &group_buff, (!using_unique_constraint ?
 
11721
                                      uniq_tuple_length_arg : 0),
 
11722
                        &bitmaps, bitmap_buffer_size(1)*2,
 
11723
                        NullS))
 
11724
  {
 
11725
    if (temp_pool_slot != MY_BIT_NONE)
 
11726
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11727
    return(NULL);
 
11728
  }
 
11729
  strmov(tmpname,path);
 
11730
  
 
11731
 
 
11732
  /* STEP 4: Create TABLE description */
 
11733
  bzero((char*) table,sizeof(*table));
 
11734
  bzero((char*) reg_field,sizeof(Field*)*2);
 
11735
 
 
11736
  table->mem_root= own_root;
 
11737
  mem_root_save= thd->mem_root;
 
11738
  thd->mem_root= &table->mem_root;
 
11739
 
 
11740
  table->field=reg_field;
 
11741
  table->alias= "weedout-tmp";
 
11742
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
 
11743
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
 
11744
  table->map=1;
 
11745
  table->temp_pool_slot = temp_pool_slot;
 
11746
  table->copy_blobs= 1;
 
11747
  table->in_use= thd;
 
11748
  table->quick_keys.init();
 
11749
  table->covering_keys.init();
 
11750
  table->keys_in_use_for_query.init();
 
11751
 
 
11752
  table->s= share;
 
11753
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
 
11754
  share->blob_field= blob_field;
 
11755
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
11756
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
 
11757
  share->table_charset= NULL;
 
11758
  share->primary_key= MAX_KEY;               // Indicate no primary key
 
11759
  share->keys_for_keyread.init();
 
11760
  share->keys_in_use.init();
 
11761
 
 
11762
  blob_count= 0;
 
11763
 
 
11764
  /* Create the field */
 
11765
  {
 
11766
    /*
 
11767
      For the sake of uniformity, always use Field_varstring (altough we could
 
11768
      use Field_string for shorter keys)
 
11769
    */
 
11770
    field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
 
11771
                               &my_charset_bin);
 
11772
    if (!field)
 
11773
      return(0);
 
11774
    field->table= table;
 
11775
    field->key_start.init(0);
 
11776
    field->part_of_key.init(0);
 
11777
    field->part_of_sortkey.init(0);
 
11778
    field->unireg_check= Field::NONE;
 
11779
    field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
 
11780
    field->reset_fields();
 
11781
    field->init(table);
 
11782
    field->orig_table= NULL;
 
11783
     
 
11784
    field->field_index= 0;
 
11785
    
 
11786
    *(reg_field++)= field;
 
11787
    *blob_field= 0;
 
11788
    *reg_field= 0;
 
11789
 
 
11790
    share->fields= 1;
 
11791
    share->blob_fields= 0;
 
11792
  }
 
11793
 
 
11794
  uint reclength= field->pack_length();
 
11795
  if (using_unique_constraint)
 
11796
  { 
 
11797
    share->db_plugin= ha_lock_engine(0, myisam_hton);
 
11798
    table->file= get_new_handler(share, &table->mem_root,
 
11799
                                 share->db_type());
 
11800
    assert(uniq_tuple_length_arg <= table->file->max_key_length());
 
11801
  }
 
11802
  else
 
11803
  {
 
11804
    share->db_plugin= ha_lock_engine(0, heap_hton);
 
11805
    table->file= get_new_handler(share, &table->mem_root,
 
11806
                                 share->db_type());
 
11807
  }
 
11808
  if (!table->file)
 
11809
    goto err;
 
11810
 
 
11811
  null_count=1;
 
11812
  
 
11813
  null_pack_length= 1;
 
11814
  reclength += null_pack_length;
 
11815
 
 
11816
  share->reclength= reclength;
 
11817
  {
 
11818
    uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
 
11819
    share->rec_buff_length= alloc_length;
 
11820
    if (!(table->record[0]= (uchar*)
 
11821
                            alloc_root(&table->mem_root, alloc_length*3)))
 
11822
      goto err;
 
11823
    table->record[1]= table->record[0]+alloc_length;
 
11824
    share->default_values= table->record[1]+alloc_length;
 
11825
  }
 
11826
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
11827
 
 
11828
  recinfo= start_recinfo;
 
11829
  null_flags=(uchar*) table->record[0];
 
11830
  pos=table->record[0]+ null_pack_length;
 
11831
  if (null_pack_length)
 
11832
  {
 
11833
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11834
    recinfo->type=FIELD_NORMAL;
 
11835
    recinfo->length=null_pack_length;
 
11836
    recinfo++;
 
11837
    bfill(null_flags,null_pack_length,255);     // Set null fields
 
11838
 
 
11839
    table->null_flags= (uchar*) table->record[0];
 
11840
    share->null_fields= null_count;
 
11841
    share->null_bytes= null_pack_length;
 
11842
  }
 
11843
  null_count=1;
 
11844
 
 
11845
  {
 
11846
    //Field *field= *reg_field;
 
11847
    uint length;
 
11848
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11849
    field->move_field(pos,(uchar*) 0,0);
 
11850
 
 
11851
    field->reset();
 
11852
    /*
 
11853
      Test if there is a default field value. The test for ->ptr is to skip
 
11854
      'offset' fields generated by initalize_tables
 
11855
    */
 
11856
    // Initialize the table field:
 
11857
    bzero(field->ptr, field->pack_length());
 
11858
 
 
11859
    length=field->pack_length();
 
11860
    pos+= length;
 
11861
 
 
11862
    /* Make entry for create table */
 
11863
    recinfo->length=length;
 
11864
    if (field->flags & BLOB_FLAG)
 
11865
      recinfo->type= (int) FIELD_BLOB;
 
11866
    else if (use_packed_rows &&
 
11867
             field->real_type() == MYSQL_TYPE_STRING &&
 
11868
             length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
 
11869
      recinfo->type=FIELD_SKIP_ENDSPACE;
 
11870
    else
 
11871
      recinfo->type=FIELD_NORMAL;
 
11872
 
 
11873
    field->table_name= &table->alias;
 
11874
  }
 
11875
 
 
11876
  //param->recinfo=recinfo;
 
11877
  //store_record(table,s->default_values);        // Make empty default record
 
11878
 
 
11879
  if (thd->variables.tmp_table_size == ~ (uint64_t) 0)          // No limit
 
11880
    share->max_rows= ~(ha_rows) 0;
 
11881
  else
 
11882
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
 
11883
                                 min(thd->variables.tmp_table_size,
 
11884
                                     thd->variables.max_heap_table_size) :
 
11885
                                 thd->variables.tmp_table_size) /
 
11886
                                 share->reclength);
 
11887
  set_if_bigger(share->max_rows,1);             // For dummy start options
 
11888
 
 
11889
 
 
11890
  //// keyinfo= param->keyinfo;
 
11891
  if (true)
 
11892
  {
 
11893
    share->keys=1;
 
11894
    share->uniques= test(using_unique_constraint);
 
11895
    table->key_info=keyinfo;
 
11896
    keyinfo->key_part=key_part_info;
 
11897
    keyinfo->flags=HA_NOSAME;
 
11898
    keyinfo->usable_key_parts= keyinfo->key_parts= 1;
 
11899
    keyinfo->key_length=0;
 
11900
    keyinfo->rec_per_key=0;
 
11901
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11902
    keyinfo->name= (char*) "weedout_key";
 
11903
    {
 
11904
      key_part_info->null_bit=0;
 
11905
      key_part_info->field=  field;
 
11906
      key_part_info->offset= field->offset(table->record[0]);
 
11907
      key_part_info->length= (uint16) field->key_length();
 
11908
      key_part_info->type=   (uint8) field->key_type();
 
11909
      key_part_info->key_type = FIELDFLAG_BINARY;
 
11910
      if (!using_unique_constraint)
 
11911
      {
 
11912
        if (!(key_field= field->new_key_field(thd->mem_root, table,
 
11913
                                              group_buff,
 
11914
                                              field->null_ptr,
 
11915
                                              field->null_bit)))
 
11916
          goto err;
 
11917
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
 
11918
      }
 
11919
      keyinfo->key_length+=  key_part_info->length;
 
11920
    }
 
11921
  }
 
11922
 
 
11923
  if (thd->is_fatal_error)                              // If end of memory
 
11924
    goto err;
 
11925
  share->db_record_offset= 1;
 
11926
  if (share->db_type() == myisam_hton)
 
11927
  {
 
11928
    recinfo++;
 
11929
    if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
 
11930
      goto err;
 
11931
  }
 
11932
  sjtbl->start_recinfo= start_recinfo;
 
11933
  sjtbl->recinfo=       recinfo;
 
11934
  if (open_tmp_table(table))
 
11935
    goto err;
 
11936
 
 
11937
  thd->mem_root= mem_root_save;
 
11938
  return(table);
 
11939
 
 
11940
err:
 
11941
  thd->mem_root= mem_root_save;
 
11942
  free_tmp_table(thd,table);                    /* purecov: inspected */
 
11943
  if (temp_pool_slot != MY_BIT_NONE)
 
11944
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11945
  return(NULL);                         /* purecov: inspected */
 
11946
}
 
11947
 
 
11948
/****************************************************************************/
 
11949
 
 
11950
/**
 
11951
  Create a reduced TABLE object with properly set up Field list from a
 
11952
  list of field definitions.
 
11953
 
 
11954
    The created table doesn't have a table handler associated with
 
11955
    it, has no keys, no group/distinct, no copy_funcs array.
 
11956
    The sole purpose of this TABLE object is to use the power of Field
 
11957
    class to read/write data to/from table->record[0]. Then one can store
 
11958
    the record in any container (RB tree, hash, etc).
 
11959
    The table is created in THD mem_root, so are the table's fields.
 
11960
    Consequently, if you don't BLOB fields, you don't need to free it.
 
11961
 
 
11962
  @param thd         connection handle
 
11963
  @param field_list  list of column definitions
 
11964
 
 
11965
  @return
 
11966
    0 if out of memory, TABLE object in case of success
 
11967
*/
 
11968
 
 
11969
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
 
11970
{
 
11971
  uint field_count= field_list.elements;
 
11972
  uint blob_count= 0;
 
11973
  Field **field;
 
11974
  Create_field *cdef;                           /* column definition */
 
11975
  uint record_length= 0;
 
11976
  uint null_count= 0;                 /* number of columns which may be null */
 
11977
  uint null_pack_length;              /* NULL representation array length */
 
11978
  uint *blob_field;
 
11979
  uchar *bitmaps;
 
11980
  TABLE *table;
 
11981
  TABLE_SHARE *share;
 
11982
 
 
11983
  if (!multi_alloc_root(thd->mem_root,
 
11984
                        &table, sizeof(*table),
 
11985
                        &share, sizeof(*share),
 
11986
                        &field, (field_count + 1) * sizeof(Field*),
 
11987
                        &blob_field, (field_count+1) *sizeof(uint),
 
11988
                        &bitmaps, bitmap_buffer_size(field_count)*2,
 
11989
                        NullS))
 
11990
    return 0;
 
11991
 
 
11992
  bzero(table, sizeof(*table));
 
11993
  bzero(share, sizeof(*share));
 
11994
  table->field= field;
 
11995
  table->s= share;
 
11996
  share->blob_field= blob_field;
 
11997
  share->fields= field_count;
 
11998
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
11999
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
12000
 
 
12001
  /* Create all fields and calculate the total length of record */
 
12002
  List_iterator_fast<Create_field> it(field_list);
 
12003
  while ((cdef= it++))
 
12004
  {
 
12005
    *field= make_field(share, 0, cdef->length,
 
12006
                       (uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
 
12007
                       f_maybe_null(cdef->pack_flag) ? 1 : 0,
 
12008
                       cdef->pack_flag, cdef->sql_type, cdef->charset,
 
12009
                       cdef->unireg_check,
 
12010
                       cdef->interval, cdef->field_name);
 
12011
    if (!*field)
 
12012
      goto error;
 
12013
    (*field)->init(table);
 
12014
    record_length+= (*field)->pack_length();
 
12015
    if (! ((*field)->flags & NOT_NULL_FLAG))
 
12016
      null_count++;
 
12017
 
 
12018
    if ((*field)->flags & BLOB_FLAG)
 
12019
      share->blob_field[blob_count++]= (uint) (field - table->field);
 
12020
 
 
12021
    field++;
 
12022
  }
 
12023
  *field= NULL;                             /* mark the end of the list */
 
12024
  share->blob_field[blob_count]= 0;            /* mark the end of the list */
 
12025
  share->blob_fields= blob_count;
 
12026
 
 
12027
  null_pack_length= (null_count + 7)/8;
 
12028
  share->reclength= record_length + null_pack_length;
 
12029
  share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
 
12030
  table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
 
12031
  if (!table->record[0])
 
12032
    goto error;
 
12033
 
 
12034
  if (null_pack_length)
 
12035
  {
 
12036
    table->null_flags= (uchar*) table->record[0];
 
12037
    share->null_fields= null_count;
 
12038
    share->null_bytes= null_pack_length;
 
12039
  }
 
12040
 
 
12041
  table->in_use= thd;           /* field->reset() may access table->in_use */
 
12042
  {
 
12043
    /* Set up field pointers */
 
12044
    uchar *null_pos= table->record[0];
 
12045
    uchar *field_pos= null_pos + share->null_bytes;
 
12046
    uint null_bit= 1;
 
12047
 
 
12048
    for (field= table->field; *field; ++field)
 
12049
    {
 
12050
      Field *cur_field= *field;
 
12051
      if ((cur_field->flags & NOT_NULL_FLAG))
 
12052
        cur_field->move_field(field_pos);
 
12053
      else
 
12054
      {
 
12055
        cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
 
12056
        null_bit<<= 1;
 
12057
        if (null_bit == (1 << 8))
 
12058
        {
 
12059
          ++null_pos;
 
12060
          null_bit= 1;
 
12061
        }
 
12062
      }
 
12063
      cur_field->reset();
 
12064
 
 
12065
      field_pos+= cur_field->pack_length();
 
12066
    }
 
12067
  }
 
12068
  return table;
 
12069
error:
 
12070
  for (field= table->field; *field; ++field)
 
12071
    delete *field;                         /* just invokes field destructor */
 
12072
  return 0;
 
12073
}
 
12074
 
 
12075
 
 
12076
static bool open_tmp_table(TABLE *table)
 
12077
{
 
12078
  int error;
 
12079
  if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
 
12080
                                  HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
 
12081
  {
 
12082
    table->file->print_error(error,MYF(0)); /* purecov: inspected */
 
12083
    table->db_stat=0;
 
12084
    return(1);
 
12085
  }
 
12086
  (void) table->file->extra(HA_EXTRA_QUICK);            /* Faster */
 
12087
  return(0);
 
12088
}
 
12089
 
 
12090
 
 
12091
/*
 
12092
  Create MyISAM temporary table
 
12093
 
 
12094
  SYNOPSIS
 
12095
    create_myisam_tmp_table()
 
12096
      table           Table object that descrimes the table to be created
 
12097
      keyinfo         Description of the index (there is always one index)
 
12098
      start_recinfo   MyISAM's column descriptions
 
12099
      recinfo INOUT   End of MyISAM's column descriptions
 
12100
      options         Option bits
 
12101
   
 
12102
  DESCRIPTION
 
12103
    Create a MyISAM temporary table according to passed description. The is
 
12104
    assumed to have one unique index or constraint.
 
12105
 
 
12106
    The passed array or MI_COLUMNDEF structures must have this form:
 
12107
 
 
12108
      1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
 
12109
         when there are many nullable columns)
 
12110
      2. Table columns
 
12111
      3. One free MI_COLUMNDEF element (*recinfo points here)
 
12112
   
 
12113
    This function may use the free element to create hash column for unique
 
12114
    constraint.
 
12115
 
 
12116
   RETURN
 
12117
     false - OK
 
12118
     true  - Error
 
12119
*/
 
12120
 
 
12121
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
 
12122
                                    MI_COLUMNDEF *start_recinfo,
 
12123
                                    MI_COLUMNDEF **recinfo, 
 
12124
                                    uint64_t options)
 
12125
{
 
12126
  int error;
 
12127
  MI_KEYDEF keydef;
 
12128
  MI_UNIQUEDEF uniquedef;
 
12129
  TABLE_SHARE *share= table->s;
 
12130
 
 
12131
  if (share->keys)
 
12132
  {                                             // Get keys for ni_create
 
12133
    bool using_unique_constraint=0;
 
12134
    HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
 
12135
                                            sizeof(*seg) * keyinfo->key_parts);
 
12136
    if (!seg)
 
12137
      goto err;
 
12138
 
 
12139
    bzero(seg, sizeof(*seg) * keyinfo->key_parts);
 
12140
    if (keyinfo->key_length >= table->file->max_key_length() ||
 
12141
        keyinfo->key_parts > table->file->max_key_parts() ||
 
12142
        share->uniques)
 
12143
    {
 
12144
      /* Can't create a key; Make a unique constraint instead of a key */
 
12145
      share->keys=    0;
 
12146
      share->uniques= 1;
 
12147
      using_unique_constraint=1;
 
12148
      bzero((char*) &uniquedef,sizeof(uniquedef));
 
12149
      uniquedef.keysegs=keyinfo->key_parts;
 
12150
      uniquedef.seg=seg;
 
12151
      uniquedef.null_are_equal=1;
 
12152
 
 
12153
      /* Create extra column for hash value */
 
12154
      bzero((uchar*) *recinfo,sizeof(**recinfo));
 
12155
      (*recinfo)->type= FIELD_CHECK;
 
12156
      (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
 
12157
      (*recinfo)++;
 
12158
      share->reclength+=MI_UNIQUE_HASH_LENGTH;
 
12159
    }
 
12160
    else
 
12161
    {
 
12162
      /* Create an unique key */
 
12163
      bzero((char*) &keydef,sizeof(keydef));
 
12164
      keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
 
12165
      keydef.keysegs=  keyinfo->key_parts;
 
12166
      keydef.seg= seg;
 
12167
    }
 
12168
    for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
 
12169
    {
 
12170
      Field *field=keyinfo->key_part[i].field;
 
12171
      seg->flag=     0;
 
12172
      seg->language= field->charset()->number;
 
12173
      seg->length=   keyinfo->key_part[i].length;
 
12174
      seg->start=    keyinfo->key_part[i].offset;
 
12175
      if (field->flags & BLOB_FLAG)
 
12176
      {
 
12177
        seg->type=
 
12178
        ((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
 
12179
         HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
 
12180
        seg->bit_start= (uint8)(field->pack_length() - share->blob_ptr_size);
 
12181
        seg->flag= HA_BLOB_PART;
 
12182
        seg->length=0;                  // Whole blob in unique constraint
 
12183
      }
 
12184
      else
 
12185
      {
 
12186
        seg->type= keyinfo->key_part[i].type;
 
12187
        /* Tell handler if it can do suffic space compression */
 
12188
        if (field->real_type() == MYSQL_TYPE_STRING &&
 
12189
            keyinfo->key_part[i].length > 4)
 
12190
          seg->flag|= HA_SPACE_PACK;
 
12191
      }
 
12192
      if (!(field->flags & NOT_NULL_FLAG))
 
12193
      {
 
12194
        seg->null_bit= field->null_bit;
 
12195
        seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
 
12196
        /*
 
12197
          We are using a GROUP BY on something that contains NULL
 
12198
          In this case we have to tell MyISAM that two NULL should
 
12199
          on INSERT be regarded at the same value
 
12200
        */
 
12201
        if (!using_unique_constraint)
 
12202
          keydef.flag|= HA_NULL_ARE_EQUAL;
 
12203
      }
 
12204
    }
 
12205
  }
 
12206
  MI_CREATE_INFO create_info;
 
12207
  bzero((char*) &create_info,sizeof(create_info));
 
12208
 
 
12209
  if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
 
12210
      OPTION_BIG_TABLES)
 
12211
    create_info.data_file_length= ~(uint64_t) 0;
 
12212
 
 
12213
  if ((error=mi_create(share->table_name.str, share->keys, &keydef,
 
12214
                       (uint) (*recinfo-start_recinfo),
 
12215
                       start_recinfo,
 
12216
                       share->uniques, &uniquedef,
 
12217
                       &create_info,
 
12218
                       HA_CREATE_TMP_TABLE)))
 
12219
  {
 
12220
    table->file->print_error(error,MYF(0));     /* purecov: inspected */
 
12221
    table->db_stat=0;
 
12222
    goto err;
 
12223
  }
 
12224
  status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
 
12225
  share->db_record_offset= 1;
 
12226
  return(0);
 
12227
 err:
 
12228
  return(1);
 
12229
}
 
12230
 
 
12231
 
 
12232
void
 
12233
free_tmp_table(THD *thd, TABLE *entry)
 
12234
{
 
12235
  MEM_ROOT own_root= entry->mem_root;
 
12236
  const char *save_proc_info;
 
12237
 
 
12238
  save_proc_info=thd->proc_info;
 
12239
  thd_proc_info(thd, "removing tmp table");
 
12240
 
 
12241
  if (entry->file)
 
12242
  {
 
12243
    if (entry->db_stat)
 
12244
      entry->file->ha_drop_table(entry->s->table_name.str);
 
12245
    else
 
12246
      entry->file->ha_delete_table(entry->s->table_name.str);
 
12247
    delete entry->file;
 
12248
  }
 
12249
 
 
12250
  /* free blobs */
 
12251
  for (Field **ptr=entry->field ; *ptr ; ptr++)
 
12252
    (*ptr)->free();
 
12253
  free_io_cache(entry);
 
12254
 
 
12255
  if (entry->temp_pool_slot != MY_BIT_NONE)
 
12256
    bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
 
12257
 
 
12258
  plugin_unlock(0, entry->s->db_plugin);
 
12259
 
 
12260
  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
 
12261
  thd_proc_info(thd, save_proc_info);
 
12262
 
 
12263
  return;
 
12264
}
 
12265
 
 
12266
/**
 
12267
  If a HEAP table gets full, create a MyISAM table and copy all rows
 
12268
  to this.
 
12269
*/
 
12270
 
 
12271
bool create_myisam_from_heap(THD *thd, TABLE *table,
 
12272
                             MI_COLUMNDEF *start_recinfo,
 
12273
                             MI_COLUMNDEF **recinfo, 
 
12274
                             int error, bool ignore_last_dupp_key_error)
 
12275
{
 
12276
  TABLE new_table;
 
12277
  TABLE_SHARE share;
 
12278
  const char *save_proc_info;
 
12279
  int write_err;
 
12280
 
 
12281
  if (table->s->db_type() != heap_hton || 
 
12282
      error != HA_ERR_RECORD_FILE_FULL)
 
12283
  {
 
12284
    table->file->print_error(error,MYF(0));
 
12285
    return(1);
 
12286
  }
 
12287
  new_table= *table;
 
12288
  share= *table->s;
 
12289
  new_table.s= &share;
 
12290
  new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
 
12291
  if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
 
12292
                                        new_table.s->db_type())))
 
12293
    return(1);                          // End of memory
 
12294
 
 
12295
  save_proc_info=thd->proc_info;
 
12296
  thd_proc_info(thd, "converting HEAP to MyISAM");
 
12297
 
 
12298
  if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
 
12299
                              recinfo, thd->lex->select_lex.options | 
 
12300
                                               thd->options))
 
12301
    goto err2;
 
12302
  if (open_tmp_table(&new_table))
 
12303
    goto err1;
 
12304
  if (table->file->indexes_are_disabled())
 
12305
    new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
 
12306
  table->file->ha_index_or_rnd_end();
 
12307
  table->file->ha_rnd_init(1);
 
12308
  if (table->no_rows)
 
12309
  {
 
12310
    new_table.file->extra(HA_EXTRA_NO_ROWS);
 
12311
    new_table.no_rows=1;
 
12312
  }
 
12313
 
 
12314
#ifdef TO_BE_DONE_LATER_IN_4_1
 
12315
  /*
 
12316
    To use start_bulk_insert() (which is new in 4.1) we need to find
 
12317
    all places where a corresponding end_bulk_insert() should be put.
 
12318
  */
 
12319
  table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
 
12320
  new_table.file->ha_start_bulk_insert(table->file->stats.records);
 
12321
#else
 
12322
  /* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
 
12323
  new_table.file->extra(HA_EXTRA_WRITE_CACHE);
 
12324
#endif
 
12325
 
 
12326
  /*
 
12327
    copy all old rows from heap table to MyISAM table
 
12328
    This is the only code that uses record[1] to read/write but this
 
12329
    is safe as this is a temporary MyISAM table without timestamp/autoincrement.
 
12330
  */
 
12331
  while (!table->file->rnd_next(new_table.record[1]))
 
12332
  {
 
12333
    write_err= new_table.file->ha_write_row(new_table.record[1]);
 
12334
    if (write_err)
 
12335
      goto err;
 
12336
  }
 
12337
  /* copy row that filled HEAP table */
 
12338
  if ((write_err=new_table.file->ha_write_row(table->record[0])))
 
12339
  {
 
12340
    if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
 
12341
        !ignore_last_dupp_key_error)
 
12342
      goto err;
 
12343
  }
 
12344
 
 
12345
  /* remove heap table and change to use myisam table */
 
12346
  (void) table->file->ha_rnd_end();
 
12347
  (void) table->file->close();                  // This deletes the table !
 
12348
  delete table->file;
 
12349
  table->file=0;
 
12350
  plugin_unlock(0, table->s->db_plugin);
 
12351
  share.db_plugin= my_plugin_lock(0, &share.db_plugin);
 
12352
  new_table.s= table->s;                       // Keep old share
 
12353
  *table= new_table;
 
12354
  *table->s= share;
 
12355
  
 
12356
  table->file->change_table_ptr(table, table->s);
 
12357
  table->use_all_columns();
 
12358
  if (save_proc_info)
 
12359
  {
 
12360
    const char *new_proc_info=
 
12361
      (!strcmp(save_proc_info,"Copying to tmp table") ?
 
12362
      "Copying to tmp table on disk" : save_proc_info);
 
12363
    thd_proc_info(thd, new_proc_info);
 
12364
  }
 
12365
  return(0);
 
12366
 
 
12367
 err:
 
12368
  table->file->print_error(write_err, MYF(0));
 
12369
  (void) table->file->ha_rnd_end();
 
12370
  (void) new_table.file->close();
 
12371
 err1:
 
12372
  new_table.file->ha_delete_table(new_table.s->table_name.str);
 
12373
 err2:
 
12374
  delete new_table.file;
 
12375
  thd_proc_info(thd, save_proc_info);
 
12376
  table->mem_root= new_table.mem_root;
 
12377
  return(1);
 
12378
}
 
12379
 
2968
12380
 
2969
12381
/**
2970
12382
  @details
2977
12389
  @return
2978
12390
    end_select function to use. This function can't fail.
2979
12391
*/
2980
 
Next_select_func setup_end_select_func(Join *join)
 
12392
 
 
12393
Next_select_func setup_end_select_func(JOIN *join)
2981
12394
{
2982
 
  Table *table= join->tmp_table;
2983
 
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
 
12395
  TABLE *table= join->tmp_table;
 
12396
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
2984
12397
  Next_select_func end_select;
2985
12398
 
2986
12399
  /* Set up select_end */
2987
12400
  if (table)
2988
12401
  {
2989
 
    if (table->group && tmp_tbl->sum_func_count &&
 
12402
    if (table->group && tmp_tbl->sum_func_count && 
2990
12403
        !tmp_tbl->precomputed_group_by)
2991
12404
    {
2992
 
      if (table->getShare()->sizeKeys())
 
12405
      if (table->s->keys)
2993
12406
      {
2994
 
        end_select= end_update;
 
12407
        end_select=end_update;
2995
12408
      }
2996
12409
      else
2997
12410
      {
2998
 
        end_select= end_unique_update;
 
12411
        end_select=end_unique_update;
2999
12412
      }
3000
12413
    }
3001
12414
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
3002
12415
    {
3003
 
      end_select= end_write_group;
 
12416
      end_select=end_write_group;
3004
12417
    }
3005
12418
    else
3006
12419
    {
3007
 
      end_select= end_write;
 
12420
      end_select=end_write;
3008
12421
      if (tmp_tbl->precomputed_group_by)
3009
12422
      {
3010
12423
        /*
3011
12424
          A preceding call to create_tmp_table in the case when loose
3012
12425
          index scan is used guarantees that
3013
 
          Tmp_Table_Param::items_to_copy has enough space for the group
 
12426
          TMP_TABLE_PARAM::items_to_copy has enough space for the group
3014
12427
          by functions. It is OK here to use memcpy since we copy
3015
12428
          Item_sum pointers into an array of Item pointers.
3016
12429
        */
3032
12445
  return end_select;
3033
12446
}
3034
12447
 
 
12448
 
3035
12449
/**
3036
12450
  Make a join of all tables and write it on socket or to table.
3037
12451
 
3042
12456
  @retval
3043
12457
    -1  if error should be sent
3044
12458
*/
3045
 
int do_select(Join *join, List<Item> *fields, Table *table)
 
12459
 
 
12460
static int
 
12461
do_select(JOIN *join,List<Item> *fields,TABLE *table)
3046
12462
{
3047
12463
  int rc= 0;
3048
12464
  enum_nested_loop_state error= NESTED_LOOP_OK;
3049
 
  JoinTable *join_tab= NULL;
3050
 
 
 
12465
  JOIN_TAB *join_tab= NULL;
 
12466
  
3051
12467
  join->tmp_table= table;                       /* Save for easy recursion */
3052
12468
  join->fields= fields;
3053
12469
 
3054
12470
  if (table)
3055
12471
  {
3056
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
3057
 
    table->emptyRecord();
 
12472
    VOID(table->file->extra(HA_EXTRA_WRITE_CACHE));
 
12473
    empty_record(table);
3058
12474
    if (table->group && join->tmp_table_param.sum_func_count &&
3059
 
        table->getShare()->sizeKeys() && !table->cursor->inited)
3060
 
    {
3061
 
      int tmp_error;
3062
 
      tmp_error= table->cursor->startIndexScan(0, 0);
3063
 
      if (tmp_error != 0)
3064
 
      {
3065
 
        table->print_error(tmp_error, MYF(0));
3066
 
        return -1;
3067
 
      }
3068
 
    }
 
12475
        table->s->keys && !table->file->inited)
 
12476
      table->file->ha_index_init(0, 0);
3069
12477
  }
3070
12478
  /* Set up select_end */
3071
12479
  Next_select_func end_select= setup_end_select_func(join);
3086
12494
    {
3087
12495
      error= (*end_select)(join, 0, 0);
3088
12496
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
3089
 
              error= (*end_select)(join, 0, 1);
 
12497
        error= (*end_select)(join, 0, 1);
3090
12498
 
3091
12499
      /*
3092
12500
        If we don't go through evaluate_join_record(), do the counting
3094
12502
        so we don't touch it here.
3095
12503
      */
3096
12504
      join->examined_rows++;
3097
 
      join->session->row_count++;
 
12505
      join->thd->row_count++;
3098
12506
      assert(join->examined_rows <= 1);
3099
12507
    }
3100
12508
    else if (join->send_row_on_empty_set())
3124
12532
    if (!table)                                 // If sending data to client
3125
12533
    {
3126
12534
      /*
3127
 
        The following will unlock all cursors if the command wasn't an
3128
 
        update command
 
12535
        The following will unlock all cursors if the command wasn't an
 
12536
        update command
3129
12537
      */
3130
12538
      join->join_free();                        // Unlock all cursors
3131
12539
      if (join->result->send_eof())
3132
 
        rc= 1;                                  // Don't send error
 
12540
        rc= 1;                                  // Don't send error
3133
12541
    }
3134
12542
  }
3135
12543
  else
3137
12545
  if (table)
3138
12546
  {
3139
12547
    int tmp, new_errno= 0;
3140
 
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
12548
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
3141
12549
    {
3142
12550
      new_errno= tmp;
3143
12551
    }
3144
 
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
 
12552
    if ((tmp=table->file->ha_index_or_rnd_end()))
3145
12553
    {
3146
12554
      new_errno= tmp;
3147
12555
    }
3148
12556
    if (new_errno)
3149
 
      table->print_error(new_errno,MYF(0));
 
12557
      table->file->print_error(new_errno,MYF(0));
3150
12558
  }
3151
 
  return(join->session->is_error() ? -1 : rc);
 
12559
  return(join->thd->is_error() ? -1 : rc);
3152
12560
}
3153
12561
 
3154
 
enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records)
 
12562
 
 
12563
enum_nested_loop_state
 
12564
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3155
12565
{
3156
12566
  enum_nested_loop_state rc;
3157
12567
 
3162
12572
      rc= sub_select(join,join_tab,end_of_records);
3163
12573
    return rc;
3164
12574
  }
3165
 
  if (join->session->getKilled())               // If aborted by user
 
12575
  if (join->thd->killed)                // If aborted by user
3166
12576
  {
3167
 
    join->session->send_kill_message();
3168
 
    return NESTED_LOOP_KILLED;
 
12577
    join->thd->send_kill_message();
 
12578
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
3169
12579
  }
3170
12580
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3171
12581
  {
3172
 
    if (! join_tab->cache.store_record_in_cache())
 
12582
    if (!store_record_in_cache(&join_tab->cache))
3173
12583
      return NESTED_LOOP_OK;                     // There is more room in cache
3174
12584
    return flush_cached_records(join,join_tab,false);
3175
12585
  }
3182
12592
/**
3183
12593
  Retrieve records ends with a given beginning from the result of a join.
3184
12594
 
3185
 
    For a given partial join record consisting of records from the tables
 
12595
    For a given partial join record consisting of records from the tables 
3186
12596
    preceding the table join_tab in the execution plan, the function
3187
12597
    retrieves all matching full records from the result set and
3188
 
    send them to the result set stream.
 
12598
    send them to the result set stream. 
3189
12599
 
3190
12600
  @note
3191
12601
    The function effectively implements the  final (n-k) nested loops
3225
12635
    first row with t3.a=t1.a has been encountered.
3226
12636
    Thus, the second predicate P2 is supplied with a guarded value that are
3227
12637
    stored in the field 'found' of the first inner table for the outer join
3228
 
    (table t2). When the first row with t3.a=t1.a for the  current row
 
12638
    (table t2). When the first row with t3.a=t1.a for the  current row 
3229
12639
    of table t1  appears, the value becomes true. For now on the predicate
3230
12640
    is evaluated immediately after the row of table t2 has been read.
3231
12641
    When the first row with t3.a=t1.a has been encountered all
3233
12643
    Only when all of them are true the row is sent to the output stream.
3234
12644
    If not, the function returns to the lowest nest level that has a false
3235
12645
    attached condition.
3236
 
    The predicates from on expressions are also pushed down. If in the
 
12646
    The predicates from on expressions are also pushed down. If in the 
3237
12647
    the above example the on expression were (t3.a=t1.a AND t2.a=t1.a),
3238
12648
    then t1.a=t2.a would be pushed down to table t2, and without any
3239
12649
    guard.
3243
12653
    is complemented by nulls  for t2 and t3. Then the pushed down predicates
3244
12654
    are checked for the composed row almost in the same way as it had
3245
12655
    been done for the first row with a match. The only difference is
3246
 
    the predicates from on expressions are not checked.
 
12656
    the predicates from on expressions are not checked. 
3247
12657
 
3248
12658
  @par
3249
12659
  @b IMPLEMENTATION
3259
12669
    and a pointer to a guarding boolean variable.
3260
12670
    When the value of the guard variable is true the value of the object
3261
12671
    is the same as the value of the predicate, otherwise it's just returns
3262
 
    true.
3263
 
    To carry out a return to a nested loop level of join table t the pointer
 
12672
    true. 
 
12673
    To carry out a return to a nested loop level of join table t the pointer 
3264
12674
    to t is remembered in the field 'return_tab' of the join structure.
3265
12675
    Consider the following query:
3266
12676
    @code
3277
12687
    t5.a=t3.a is found, the pushed down predicate t4.b=2 OR t4.b IS NULL
3278
12688
    becomes 'activated', as well the predicate t4.a=t2.a. But
3279
12689
    the predicate (t2.b=5 OR t2.b IS NULL) can not be checked until
3280
 
    t4.a=t2.a becomes true.
 
12690
    t4.a=t2.a becomes true. 
3281
12691
    In order not to re-evaluate the predicates that were already evaluated
3282
12692
    as attached pushed down predicates, a pointer to the the first
3283
12693
    most inner unmatched table is maintained in join_tab->first_unmatched.
3284
12694
    Thus, when the first row from t5 with t5.a=t3.a is found
3285
 
    this pointer for t5 is changed from t4 to t2.
 
12695
    this pointer for t5 is changed from t4 to t2.             
3286
12696
 
3287
12697
    @par
3288
12698
    @b STRUCTURE @b NOTES
3293
12703
  @param join      pointer to the structure providing all context info for
3294
12704
                   the query
3295
12705
  @param join_tab  the first next table of the execution plan to be retrieved
3296
 
  @param end_records  true when we need to perform final steps of retrival
 
12706
  @param end_records  true when we need to perform final steps of retrival   
3297
12707
 
3298
12708
  @return
3299
12709
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
3300
12710
*/
3301
 
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
 
12711
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
 
12712
 
 
12713
enum_nested_loop_state
 
12714
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3302
12715
{
3303
12716
  join_tab->table->null_row=0;
3304
12717
  if (end_of_records)
3306
12719
 
3307
12720
  int error;
3308
12721
  enum_nested_loop_state rc;
3309
 
  ReadRecord *info= &join_tab->read_record;
 
12722
  READ_RECORD *info= &join_tab->read_record;
 
12723
 
 
12724
  if (join_tab->flush_weedout_table)
 
12725
  {
 
12726
    do_sj_reset(join_tab->flush_weedout_table);
 
12727
  }
3310
12728
 
3311
12729
  if (join->resume_nested_loop)
3312
12730
  {
3334
12752
      /* Set first_unmatched for the last inner table of this group */
3335
12753
      join_tab->last_inner->first_unmatched= join_tab;
3336
12754
    }
3337
 
    join->session->row_count= 0;
 
12755
    join->thd->row_count= 0;
3338
12756
 
3339
12757
    error= (*join_tab->read_first_record)(join_tab);
3340
12758
    rc= evaluate_join_record(join, join_tab, error);
3341
12759
  }
3342
 
 
3343
 
  /*
3344
 
    Note: psergey has added the 2nd part of the following condition; the
 
12760
  
 
12761
  /* 
 
12762
    Note: psergey has added the 2nd part of the following condition; the 
3345
12763
    change should probably be made in 5.1, too.
3346
12764
  */
3347
12765
  while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
3359
12777
  return rc;
3360
12778
}
3361
12779
 
3362
 
int safe_index_read(JoinTable *tab)
3363
 
{
3364
 
  int error;
3365
 
  Table *table= tab->table;
3366
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
12780
 
 
12781
 
 
12782
 
 
12783
/*
 
12784
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
 
12785
 
 
12786
  SYNPOSIS
 
12787
    do_sj_dups_weedout()
 
12788
      
 
12789
  RETURN
 
12790
    -1  Error
 
12791
    1   The row combination is a duplicate (discard it)
 
12792
    0   The row combination is not a duplicate (continue)
 
12793
*/
 
12794
 
 
12795
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl) 
 
12796
{
 
12797
  int error;
 
12798
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
 
12799
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
 
12800
  uchar *ptr= sjtbl->tmp_table->record[0] + 1;
 
12801
  uchar *nulls_ptr= ptr;
 
12802
  
 
12803
  /* Put the the rowids tuple into table->record[0]: */
 
12804
 
 
12805
  // 1. Store the length 
 
12806
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
 
12807
  {
 
12808
    *ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
 
12809
    ptr++;
 
12810
  }
 
12811
  else
 
12812
  {
 
12813
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
 
12814
    ptr += 2;
 
12815
  }
 
12816
 
 
12817
  // 2. Zero the null bytes 
 
12818
  if (sjtbl->null_bytes)
 
12819
  {
 
12820
    bzero(ptr, sjtbl->null_bytes);
 
12821
    ptr += sjtbl->null_bytes; 
 
12822
  }
 
12823
 
 
12824
  // 3. Put the rowids
 
12825
  for (uint i=0; tab != tab_end; tab++, i++)
 
12826
  {
 
12827
    handler *h= tab->join_tab->table->file;
 
12828
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
 
12829
    {
 
12830
      /* It's a NULL-complemented row */
 
12831
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
 
12832
      bzero(ptr + tab->rowid_offset, h->ref_length);
 
12833
    }
 
12834
    else
 
12835
    {
 
12836
      /* Copy the rowid value */
 
12837
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
12838
        h->position(tab->join_tab->table->record[0]);
 
12839
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
 
12840
    }
 
12841
  }
 
12842
 
 
12843
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
 
12844
  if (error)
 
12845
  {
 
12846
    /* create_myisam_from_heap will generate error if needed */
 
12847
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
12848
        create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo, 
 
12849
                                &sjtbl->recinfo, error, 1))
 
12850
      return -1;
 
12851
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
 
12852
    return 1;
 
12853
  }
 
12854
  return 0;
 
12855
}
 
12856
 
 
12857
 
 
12858
/*
 
12859
  SemiJoinDuplicateElimination: Reset the temporary table
 
12860
*/
 
12861
 
 
12862
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
 
12863
{
 
12864
  if (sj_tbl->tmp_table)
 
12865
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
 
12866
  return 0;
 
12867
}
 
12868
 
 
12869
/*
 
12870
  Process one record of the nested loop join.
 
12871
 
 
12872
    This function will evaluate parts of WHERE/ON clauses that are
 
12873
    applicable to the partial record on hand and in case of success
 
12874
    submit this record to the next level of the nested loop.
 
12875
*/
 
12876
 
 
12877
static enum_nested_loop_state
 
12878
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
12879
                     int error)
 
12880
{
 
12881
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
 
12882
  ha_rows found_records=join->found_records;
 
12883
  COND *select_cond= join_tab->select_cond;
 
12884
 
 
12885
  if (error > 0 || (join->thd->is_error()))     // Fatal error
 
12886
    return NESTED_LOOP_ERROR;
 
12887
  if (error < 0)
 
12888
    return NESTED_LOOP_NO_MORE_ROWS;
 
12889
  if (join->thd->killed)                        // Aborted by user
 
12890
  {
 
12891
    join->thd->send_kill_message();
 
12892
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
 
12893
  }
 
12894
  if (!select_cond || select_cond->val_int())
 
12895
  {
 
12896
    /*
 
12897
      There is no select condition or the attached pushed down
 
12898
      condition is true => a match is found.
 
12899
    */
 
12900
    bool found= 1;
 
12901
    while (join_tab->first_unmatched && found)
 
12902
    {
 
12903
      /*
 
12904
        The while condition is always false if join_tab is not
 
12905
        the last inner join table of an outer join operation.
 
12906
      */
 
12907
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
12908
      /*
 
12909
        Mark that a match for current outer table is found.
 
12910
        This activates push down conditional predicates attached
 
12911
        to the all inner tables of the outer join.
 
12912
      */
 
12913
      first_unmatched->found= 1;
 
12914
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
12915
      {
 
12916
        if (tab->table->reginfo.not_exists_optimize)
 
12917
          return NESTED_LOOP_NO_MORE_ROWS;
 
12918
        /* Check all predicates that has just been activated. */
 
12919
        /*
 
12920
          Actually all predicates non-guarded by first_unmatched->found
 
12921
          will be re-evaluated again. It could be fixed, but, probably,
 
12922
          it's not worth doing now.
 
12923
        */
 
12924
        if (tab->select_cond && !tab->select_cond->val_int())
 
12925
        {
 
12926
          /* The condition attached to table tab is false */
 
12927
          if (tab == join_tab)
 
12928
            found= 0;
 
12929
          else
 
12930
          {
 
12931
            /*
 
12932
              Set a return point if rejected predicate is attached
 
12933
              not to the last table of the current nest level.
 
12934
            */
 
12935
            join->return_tab= tab;
 
12936
            return NESTED_LOOP_OK;
 
12937
          }
 
12938
        }
 
12939
      }
 
12940
      /*
 
12941
        Check whether join_tab is not the last inner table
 
12942
        for another embedding outer join.
 
12943
      */
 
12944
      if ((first_unmatched= first_unmatched->first_upper) &&
 
12945
          first_unmatched->last_inner != join_tab)
 
12946
        first_unmatched= 0;
 
12947
      join_tab->first_unmatched= first_unmatched;
 
12948
    }
 
12949
 
 
12950
    JOIN_TAB *return_tab= join->return_tab;
 
12951
    join_tab->found_match= true;
 
12952
    if (join_tab->check_weed_out_table)
 
12953
    {
 
12954
      int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
 
12955
      if (res == -1)
 
12956
        return NESTED_LOOP_ERROR;
 
12957
      if (res == 1)
 
12958
        return NESTED_LOOP_OK;
 
12959
    }
 
12960
    else if (join_tab->do_firstmatch)
 
12961
    {
 
12962
      /* 
 
12963
        We should return to the join_tab->do_firstmatch after we have 
 
12964
        enumerated all the suffixes for current prefix row combination
 
12965
      */
 
12966
      return_tab= join_tab->do_firstmatch;
 
12967
    }
 
12968
 
 
12969
    /*
 
12970
      It was not just a return to lower loop level when one
 
12971
      of the newly activated predicates is evaluated as false
 
12972
      (See above join->return_tab= tab).
 
12973
    */
 
12974
    join->examined_rows++;
 
12975
    join->thd->row_count++;
 
12976
 
 
12977
    if (found)
 
12978
    {
 
12979
      enum enum_nested_loop_state rc;
 
12980
      /* A match from join_tab is found for the current partial join. */
 
12981
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
 
12982
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
12983
        return rc;
 
12984
      if (return_tab < join->return_tab)
 
12985
        join->return_tab= return_tab;
 
12986
 
 
12987
      if (join->return_tab < join_tab)
 
12988
        return NESTED_LOOP_OK;
 
12989
      /*
 
12990
        Test if this was a SELECT DISTINCT query on a table that
 
12991
        was not in the field list;  In this case we can abort if
 
12992
        we found a row, as no new rows can be added to the result.
 
12993
      */
 
12994
      if (not_used_in_distinct && found_records != join->found_records)
 
12995
        return NESTED_LOOP_NO_MORE_ROWS;
 
12996
    }
 
12997
    else
 
12998
      join_tab->read_record.file->unlock_row();
 
12999
  }
 
13000
  else
 
13001
  {
 
13002
    /*
 
13003
      The condition pushed down to the table join_tab rejects all rows
 
13004
      with the beginning coinciding with the current partial join.
 
13005
    */
 
13006
    join->examined_rows++;
 
13007
    join->thd->row_count++;
 
13008
    join_tab->read_record.file->unlock_row();
 
13009
  }
 
13010
  return NESTED_LOOP_OK;
 
13011
}
 
13012
 
 
13013
 
 
13014
/**
 
13015
 
 
13016
  @details
 
13017
    Construct a NULL complimented partial join record and feed it to the next
 
13018
    level of the nested loop. This function is used in case we have
 
13019
    an OUTER join and no matching record was found.
 
13020
*/
 
13021
 
 
13022
static enum_nested_loop_state
 
13023
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
 
13024
{
 
13025
  /*
 
13026
    The table join_tab is the first inner table of a outer join operation
 
13027
    and no matches has been found for the current outer row.
 
13028
  */
 
13029
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
 
13030
  /* Cache variables for faster loop */
 
13031
  COND *select_cond;
 
13032
  for ( ; join_tab <= last_inner_tab ; join_tab++)
 
13033
  {
 
13034
    /* Change the the values of guard predicate variables. */
 
13035
    join_tab->found= 1;
 
13036
    join_tab->not_null_compl= 0;
 
13037
    /* The outer row is complemented by nulls for each inner tables */
 
13038
    restore_record(join_tab->table,s->default_values);  // Make empty record
 
13039
    mark_as_null_row(join_tab->table);       // For group by without error
 
13040
    select_cond= join_tab->select_cond;
 
13041
    /* Check all attached conditions for inner table rows. */
 
13042
    if (select_cond && !select_cond->val_int())
 
13043
      return NESTED_LOOP_OK;
 
13044
  }
 
13045
  join_tab--;
 
13046
  /*
 
13047
    The row complemented by nulls might be the first row
 
13048
    of embedding outer joins.
 
13049
    If so, perform the same actions as in the code
 
13050
    for the first regular outer join row above.
 
13051
  */
 
13052
  for ( ; ; )
 
13053
  {
 
13054
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
13055
    if ((first_unmatched= first_unmatched->first_upper) &&
 
13056
        first_unmatched->last_inner != join_tab)
 
13057
      first_unmatched= 0;
 
13058
    join_tab->first_unmatched= first_unmatched;
 
13059
    if (!first_unmatched)
 
13060
      break;
 
13061
    first_unmatched->found= 1;
 
13062
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
13063
    {
 
13064
      if (tab->select_cond && !tab->select_cond->val_int())
 
13065
      {
 
13066
        join->return_tab= tab;
 
13067
        return NESTED_LOOP_OK;
 
13068
      }
 
13069
    }
 
13070
  }
 
13071
  /*
 
13072
    The row complemented by nulls satisfies all conditions
 
13073
    attached to inner tables.
 
13074
    Send the row complemented by nulls to be joined with the
 
13075
    remaining tables.
 
13076
  */
 
13077
  return (*join_tab->next_select)(join, join_tab+1, 0);
 
13078
}
 
13079
 
 
13080
 
 
13081
static enum_nested_loop_state
 
13082
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
 
13083
{
 
13084
  enum_nested_loop_state rc= NESTED_LOOP_OK;
 
13085
  int error;
 
13086
  READ_RECORD *info;
 
13087
 
 
13088
  join_tab->table->null_row= 0;
 
13089
  if (!join_tab->cache.records)
 
13090
    return NESTED_LOOP_OK;                      /* Nothing to do */
 
13091
  if (skip_last)
 
13092
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
 
13093
  if (join_tab->use_quick == 2)
 
13094
  {
 
13095
    if (join_tab->select->quick)
 
13096
    {                                   /* Used quick select last. reset it */
 
13097
      delete join_tab->select->quick;
 
13098
      join_tab->select->quick=0;
 
13099
    }
 
13100
  }
 
13101
 /* read through all records */
 
13102
  if ((error=join_init_read_record(join_tab)))
 
13103
  {
 
13104
    reset_cache_write(&join_tab->cache);
 
13105
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
 
13106
  }
 
13107
 
 
13108
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
 
13109
  {
 
13110
    tmp->status=tmp->table->status;
 
13111
    tmp->table->status=0;
 
13112
  }
 
13113
 
 
13114
  info= &join_tab->read_record;
 
13115
  do
 
13116
  {
 
13117
    if (join->thd->killed)
 
13118
    {
 
13119
      join->thd->send_kill_message();
 
13120
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
 
13121
    }
 
13122
    SQL_SELECT *select=join_tab->select;
 
13123
    if (rc == NESTED_LOOP_OK &&
 
13124
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
 
13125
    {
 
13126
      uint i;
 
13127
      reset_cache_read(&join_tab->cache);
 
13128
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
 
13129
      {
 
13130
        read_cached_record(join_tab);
 
13131
        if (!select || !select->skip_record())
 
13132
        {
 
13133
          int res= 0;
 
13134
          if (!join_tab->check_weed_out_table || 
 
13135
              !(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
 
13136
          {
 
13137
            rc= (join_tab->next_select)(join,join_tab+1,0);
 
13138
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
13139
            {
 
13140
              reset_cache_write(&join_tab->cache);
 
13141
              return rc;
 
13142
            }
 
13143
          }
 
13144
          if (res == -1)
 
13145
            return NESTED_LOOP_ERROR;
 
13146
        }
 
13147
      }
 
13148
    }
 
13149
  } while (!(error=info->read_record(info)));
 
13150
 
 
13151
  if (skip_last)
 
13152
    read_cached_record(join_tab);               // Restore current record
 
13153
  reset_cache_write(&join_tab->cache);
 
13154
  if (error > 0)                                // Fatal error
 
13155
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
 
13156
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
 
13157
    tmp2->table->status=tmp2->status;
 
13158
  return NESTED_LOOP_OK;
 
13159
}
 
13160
 
 
13161
 
 
13162
/*****************************************************************************
 
13163
  The different ways to read a record
 
13164
  Returns -1 if row was not found, 0 if row was found and 1 on errors
 
13165
*****************************************************************************/
 
13166
 
 
13167
/** Help function when we get some an error from the table handler. */
 
13168
 
 
13169
int report_error(TABLE *table, int error)
 
13170
{
 
13171
  if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
 
13172
  {
 
13173
    table->status= STATUS_GARBAGE;
 
13174
    return -1;                                  // key not found; ok
 
13175
  }
 
13176
  /*
 
13177
    Locking reads can legally return also these errors, do not
 
13178
    print them to the .err log
 
13179
  */
 
13180
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
 
13181
    sql_print_error("Got error %d when reading table '%s'",
 
13182
                    error, table->s->path.str);
 
13183
  table->file->print_error(error,MYF(0));
 
13184
  return 1;
 
13185
}
 
13186
 
 
13187
 
 
13188
int safe_index_read(JOIN_TAB *tab)
 
13189
{
 
13190
  int error;
 
13191
  TABLE *table= tab->table;
 
13192
  if ((error=table->file->index_read_map(table->record[0],
3367
13193
                                         tab->ref.key_buff,
3368
13194
                                         make_prev_keypart_map(tab->ref.key_parts),
3369
13195
                                         HA_READ_KEY_EXACT)))
3370
 
    return table->report_error(error);
 
13196
    return report_error(table, error);
3371
13197
  return 0;
3372
13198
}
3373
13199
 
 
13200
 
 
13201
static int
 
13202
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
 
13203
{
 
13204
  int error;
 
13205
  TABLE *table=tab->table;
 
13206
  table->const_table=1;
 
13207
  table->null_row=0;
 
13208
  table->status=STATUS_NO_RECORD;
 
13209
  
 
13210
  if (tab->type == JT_SYSTEM)
 
13211
  {
 
13212
    if ((error=join_read_system(tab)))
 
13213
    {                                           // Info for DESCRIBE
 
13214
      tab->info="const row not found";
 
13215
      /* Mark for EXPLAIN that the row was not found */
 
13216
      pos->records_read=0.0;
 
13217
      pos->ref_depend_map= 0;
 
13218
      if (!table->maybe_null || error > 0)
 
13219
        return(error);
 
13220
    }
 
13221
  }
 
13222
  else
 
13223
  {
 
13224
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
 
13225
        !table->no_keyread &&
 
13226
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
 
13227
    {
 
13228
      table->key_read=1;
 
13229
      table->file->extra(HA_EXTRA_KEYREAD);
 
13230
      tab->index= tab->ref.key;
 
13231
    }
 
13232
    error=join_read_const(tab);
 
13233
    if (table->key_read)
 
13234
    {
 
13235
      table->key_read=0;
 
13236
      table->file->extra(HA_EXTRA_NO_KEYREAD);
 
13237
    }
 
13238
    if (error)
 
13239
    {
 
13240
      tab->info="unique row not found";
 
13241
      /* Mark for EXPLAIN that the row was not found */
 
13242
      pos->records_read=0.0;
 
13243
      pos->ref_depend_map= 0;
 
13244
      if (!table->maybe_null || error > 0)
 
13245
        return(error);
 
13246
    }
 
13247
  }
 
13248
  if (*tab->on_expr_ref && !table->null_row)
 
13249
  {
 
13250
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
 
13251
      mark_as_null_row(table);  
 
13252
  }
 
13253
  if (!table->null_row)
 
13254
    table->maybe_null=0;
 
13255
 
 
13256
  /* Check appearance of new constant items in Item_equal objects */
 
13257
  JOIN *join= tab->join;
 
13258
  if (join->conds)
 
13259
    update_const_equal_items(join->conds, tab);
 
13260
  TABLE_LIST *tbl;
 
13261
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
 
13262
  {
 
13263
    TABLE_LIST *embedded;
 
13264
    TABLE_LIST *embedding= tbl;
 
13265
    do
 
13266
    {
 
13267
      embedded= embedding;
 
13268
      if (embedded->on_expr)
 
13269
         update_const_equal_items(embedded->on_expr, tab);
 
13270
      embedding= embedded->embedding;
 
13271
    }
 
13272
    while (embedding &&
 
13273
           embedding->nested_join->join_list.head() == embedded);
 
13274
  }
 
13275
 
 
13276
  return(0);
 
13277
}
 
13278
 
 
13279
 
 
13280
static int
 
13281
join_read_system(JOIN_TAB *tab)
 
13282
{
 
13283
  TABLE *table= tab->table;
 
13284
  int error;
 
13285
  if (table->status & STATUS_GARBAGE)           // If first read
 
13286
  {
 
13287
    if ((error=table->file->read_first_row(table->record[0],
 
13288
                                           table->s->primary_key)))
 
13289
    {
 
13290
      if (error != HA_ERR_END_OF_FILE)
 
13291
        return report_error(table, error);
 
13292
      mark_as_null_row(tab->table);
 
13293
      empty_record(table);                      // Make empty record
 
13294
      return -1;
 
13295
    }
 
13296
    store_record(table,record[1]);
 
13297
  }
 
13298
  else if (!table->status)                      // Only happens with left join
 
13299
    restore_record(table,record[1]);                    // restore old record
 
13300
  table->null_row=0;
 
13301
  return table->status ? -1 : 0;
 
13302
}
 
13303
 
 
13304
 
3374
13305
/**
3375
13306
  Read a (constant) table when there is at most one matching row.
3376
13307
 
3383
13314
  @retval
3384
13315
    1   Got an error (other than row not found) during read
3385
13316
*/
3386
 
int join_read_const(JoinTable *tab)
 
13317
 
 
13318
static int
 
13319
join_read_const(JOIN_TAB *tab)
3387
13320
{
3388
13321
  int error;
3389
 
  Table *table= tab->table;
 
13322
  TABLE *table= tab->table;
3390
13323
  if (table->status & STATUS_GARBAGE)           // If first read
3391
13324
  {
3392
13325
    table->status= 0;
3393
 
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3394
 
      error= HA_ERR_KEY_NOT_FOUND;
 
13326
    if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
 
13327
      error=HA_ERR_KEY_NOT_FOUND;
3395
13328
    else
3396
13329
    {
3397
 
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
3398
 
                                            (unsigned char*) tab->ref.key_buff,
 
13330
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
 
13331
                                            (uchar*) tab->ref.key_buff,
3399
13332
                                            make_prev_keypart_map(tab->ref.key_parts),
3400
13333
                                            HA_READ_KEY_EXACT);
3401
13334
    }
3402
13335
    if (error)
3403
13336
    {
3404
13337
      table->status= STATUS_NOT_FOUND;
3405
 
      tab->table->mark_as_null_row();
3406
 
      table->emptyRecord();
 
13338
      mark_as_null_row(tab->table);
 
13339
      empty_record(table);
3407
13340
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3408
 
        return table->report_error(error);
 
13341
        return report_error(table, error);
3409
13342
      return -1;
3410
13343
    }
3411
 
    table->storeRecord();
 
13344
    store_record(table,record[1]);
3412
13345
  }
3413
13346
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
3414
13347
  {
3415
13348
    table->status=0;
3416
 
    table->restoreRecord();                     // restore old record
 
13349
    restore_record(table,record[1]);                    // restore old record
3417
13350
  }
3418
13351
  table->null_row=0;
3419
13352
  return table->status ? -1 : 0;
3420
13353
}
3421
13354
 
 
13355
 
3422
13356
/*
3423
13357
  eq_ref access method implementation: "read_first" function
3424
13358
 
3425
13359
  SYNOPSIS
3426
13360
    join_read_key()
3427
 
      tab  JoinTable of the accessed table
 
13361
      tab  JOIN_TAB of the accessed table
3428
13362
 
3429
13363
  DESCRIPTION
3430
13364
    This is "read_fist" function for the "ref" access method. The difference
3432
13366
 
3433
13367
  RETURN
3434
13368
    0  - Ok
3435
 
   -1  - Row not found
 
13369
   -1  - Row not found 
3436
13370
    1  - Error
3437
13371
*/
3438
 
int join_read_key(JoinTable *tab)
 
13372
 
 
13373
static int
 
13374
join_read_key(JOIN_TAB *tab)
3439
13375
{
3440
13376
  int error;
3441
 
  Table *table= tab->table;
 
13377
  TABLE *table= tab->table;
3442
13378
 
3443
 
  if (!table->cursor->inited)
 
13379
  if (!table->file->inited)
3444
13380
  {
3445
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3446
 
    if (error != 0)
3447
 
    {
3448
 
      table->print_error(error, MYF(0));
3449
 
    }
 
13381
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3450
13382
  }
3451
13383
 
3452
 
  /* @todo Why don't we do "Late NULLs Filtering" here? */
 
13384
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3453
13385
  if (cmp_buffer_with_ref(tab) ||
3454
13386
      (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
3455
13387
  {
3458
13390
      table->status=STATUS_NOT_FOUND;
3459
13391
      return -1;
3460
13392
    }
3461
 
    error=table->cursor->index_read_map(table->getInsertRecord(),
 
13393
    error=table->file->index_read_map(table->record[0],
3462
13394
                                      tab->ref.key_buff,
3463
13395
                                      make_prev_keypart_map(tab->ref.key_parts),
3464
13396
                                      HA_READ_KEY_EXACT);
3465
13397
    if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3466
 
      return table->report_error(error);
 
13398
      return report_error(table, error);
3467
13399
  }
3468
13400
  table->null_row=0;
3469
13401
  return table->status ? -1 : 0;
3470
13402
}
3471
13403
 
 
13404
 
3472
13405
/*
3473
13406
  ref access method implementation: "read_first" function
3474
13407
 
3475
13408
  SYNOPSIS
3476
13409
    join_read_always_key()
3477
 
      tab  JoinTable of the accessed table
 
13410
      tab  JOIN_TAB of the accessed table
3478
13411
 
3479
13412
  DESCRIPTION
3480
 
    This is "read_first" function for the "ref" access method.
3481
 
 
 
13413
    This is "read_fist" function for the "ref" access method.
 
13414
   
3482
13415
    The functon must leave the index initialized when it returns.
3483
13416
    ref_or_null access implementation depends on that.
3484
13417
 
3485
13418
  RETURN
3486
13419
    0  - Ok
3487
 
   -1  - Row not found
 
13420
   -1  - Row not found 
3488
13421
    1  - Error
3489
13422
*/
3490
 
int join_read_always_key(JoinTable *tab)
 
13423
 
 
13424
static int
 
13425
join_read_always_key(JOIN_TAB *tab)
3491
13426
{
3492
13427
  int error;
3493
 
  Table *table= tab->table;
 
13428
  TABLE *table= tab->table;
3494
13429
 
3495
13430
  /* Initialize the index first */
3496
 
  if (!table->cursor->inited)
3497
 
  {
3498
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3499
 
    if (error != 0)
3500
 
      return table->report_error(error);
3501
 
  }
3502
 
 
 
13431
  if (!table->file->inited)
 
13432
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
13433
 
3503
13434
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3504
 
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
 
13435
  for (uint i= 0 ; i < tab->ref.key_parts ; i++)
3505
13436
  {
3506
13437
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
3507
13438
        return -1;
3508
13439
  }
3509
13440
 
3510
 
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
13441
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
3511
13442
    return -1;
3512
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
13443
  if ((error=table->file->index_read_map(table->record[0],
3513
13444
                                         tab->ref.key_buff,
3514
13445
                                         make_prev_keypart_map(tab->ref.key_parts),
3515
13446
                                         HA_READ_KEY_EXACT)))
3516
13447
  {
3517
13448
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3518
 
      return table->report_error(error);
3519
 
    return -1;
 
13449
      return report_error(table, error);
 
13450
    return -1; /* purecov: inspected */
3520
13451
  }
3521
 
 
3522
13452
  return 0;
3523
13453
}
3524
13454
 
 
13455
 
3525
13456
/**
3526
 
  This function is used when optimizing away ORDER BY in
 
13457
  This function is used when optimizing away ORDER BY in 
3527
13458
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
3528
13459
*/
3529
 
int join_read_last_key(JoinTable *tab)
 
13460
  
 
13461
static int
 
13462
join_read_last_key(JOIN_TAB *tab)
3530
13463
{
3531
13464
  int error;
3532
 
  Table *table= tab->table;
 
13465
  TABLE *table= tab->table;
3533
13466
 
3534
 
  if (!table->cursor->inited)
3535
 
  {
3536
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3537
 
    if (error != 0)
3538
 
      return table->report_error(error);
3539
 
  }
3540
 
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
13467
  if (!table->file->inited)
 
13468
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
13469
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
3541
13470
    return -1;
3542
 
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
 
13471
  if ((error=table->file->index_read_last_map(table->record[0],
3543
13472
                                              tab->ref.key_buff,
3544
13473
                                              make_prev_keypart_map(tab->ref.key_parts))))
3545
13474
  {
3546
13475
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3547
 
      return table->report_error(error);
3548
 
    return -1;
 
13476
      return report_error(table, error);
 
13477
    return -1; /* purecov: inspected */
3549
13478
  }
3550
13479
  return 0;
3551
13480
}
3552
13481
 
3553
 
int join_no_more_records(ReadRecord *)
 
13482
 
 
13483
        /* ARGSUSED */
 
13484
static int
 
13485
join_no_more_records(READ_RECORD *info __attribute__((unused)))
3554
13486
{
3555
13487
  return -1;
3556
13488
}
3557
13489
 
3558
 
int join_read_next_same_diff(ReadRecord *info)
 
13490
static int
 
13491
join_read_next_same_diff(READ_RECORD *info)
3559
13492
{
3560
 
  Table *table= info->table;
3561
 
  JoinTable *tab=table->reginfo.join_tab;
 
13493
  TABLE *table= info->table;
 
13494
  JOIN_TAB *tab=table->reginfo.join_tab;
3562
13495
  if (tab->insideout_match_tab->found_match)
3563
13496
  {
3564
 
    KeyInfo *key= tab->table->key_info + tab->index;
3565
 
    do
 
13497
    KEY *key= tab->table->key_info + tab->index;
 
13498
    do 
3566
13499
    {
3567
13500
      int error;
3568
13501
      /* Save index tuple from record to the buffer */
3569
13502
      key_copy(tab->insideout_buf, info->record, key, 0);
3570
13503
 
3571
 
      if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
13504
      if ((error=table->file->index_next_same(table->record[0],
3572
13505
                                              tab->ref.key_buff,
3573
13506
                                              tab->ref.key_length)))
3574
13507
      {
3575
13508
        if (error != HA_ERR_END_OF_FILE)
3576
 
          return table->report_error(error);
 
13509
          return report_error(table, error);
3577
13510
        table->status= STATUS_GARBAGE;
3578
13511
        return -1;
3579
13512
      }
3580
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
13513
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3581
13514
                      tab->insideout_buf, key->key_length));
3582
13515
    tab->insideout_match_tab->found_match= 0;
3583
13516
    return 0;
3586
13519
    return join_read_next_same(info);
3587
13520
}
3588
13521
 
3589
 
int join_read_next_same(ReadRecord *info)
 
13522
static int
 
13523
join_read_next_same(READ_RECORD *info)
3590
13524
{
3591
13525
  int error;
3592
 
  Table *table= info->table;
3593
 
  JoinTable *tab=table->reginfo.join_tab;
 
13526
  TABLE *table= info->table;
 
13527
  JOIN_TAB *tab=table->reginfo.join_tab;
3594
13528
 
3595
 
  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
13529
  if ((error=table->file->index_next_same(table->record[0],
3596
13530
                                          tab->ref.key_buff,
3597
13531
                                          tab->ref.key_length)))
3598
13532
  {
3599
13533
    if (error != HA_ERR_END_OF_FILE)
3600
 
      return table->report_error(error);
 
13534
      return report_error(table, error);
3601
13535
    table->status= STATUS_GARBAGE;
3602
13536
    return -1;
3603
13537
  }
3604
 
 
3605
13538
  return 0;
3606
13539
}
3607
13540
 
3608
 
int join_read_prev_same(ReadRecord *info)
 
13541
 
 
13542
static int
 
13543
join_read_prev_same(READ_RECORD *info)
3609
13544
{
3610
13545
  int error;
3611
 
  Table *table= info->table;
3612
 
  JoinTable *tab=table->reginfo.join_tab;
 
13546
  TABLE *table= info->table;
 
13547
  JOIN_TAB *tab=table->reginfo.join_tab;
3613
13548
 
3614
 
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
3615
 
    return table->report_error(error);
 
13549
  if ((error=table->file->index_prev(table->record[0])))
 
13550
    return report_error(table, error);
3616
13551
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3617
13552
                      tab->ref.key_length))
3618
13553
  {
3622
13557
  return error;
3623
13558
}
3624
13559
 
3625
 
int join_init_quick_read_record(JoinTable *tab)
 
13560
 
 
13561
static int
 
13562
join_init_quick_read_record(JOIN_TAB *tab)
3626
13563
{
3627
13564
  if (test_if_quick_select(tab) == -1)
3628
13565
    return -1;                                  /* No possible records */
3629
13566
  return join_init_read_record(tab);
3630
13567
}
3631
13568
 
3632
 
int init_read_record_seq(JoinTable *tab)
 
13569
 
 
13570
int rr_sequential(READ_RECORD *info);
 
13571
int init_read_record_seq(JOIN_TAB *tab)
3633
13572
{
3634
 
  tab->read_record.init_reard_record_sequential();
3635
 
 
3636
 
  if (tab->read_record.cursor->startTableScan(1))
 
13573
  tab->read_record.read_record= rr_sequential;
 
13574
  if (tab->read_record.file->ha_rnd_init(1))
3637
13575
    return 1;
3638
13576
  return (*tab->read_record.read_record)(&tab->read_record);
3639
13577
}
3640
13578
 
3641
 
int test_if_quick_select(JoinTable *tab)
 
13579
static int
 
13580
test_if_quick_select(JOIN_TAB *tab)
3642
13581
{
3643
 
  safe_delete(tab->select->quick);
3644
 
 
3645
 
  return tab->select->test_quick_select(tab->join->session, tab->keys,
3646
 
                                        (table_map) 0, HA_POS_ERROR, 0, false);
 
13582
  delete tab->select->quick;
 
13583
  tab->select->quick=0;
 
13584
  return tab->select->test_quick_select(tab->join->thd, tab->keys,
 
13585
                                        (table_map) 0, HA_POS_ERROR, 0,
 
13586
                                        false);
3647
13587
}
3648
13588
 
3649
 
int join_init_read_record(JoinTable *tab)
 
13589
 
 
13590
static int
 
13591
join_init_read_record(JOIN_TAB *tab)
3650
13592
{
3651
13593
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3652
13594
    return 1;
3653
 
 
3654
 
  if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true))
3655
 
    return 1;
3656
 
 
 
13595
  init_read_record(&tab->read_record, tab->join->thd, tab->table,
 
13596
                   tab->select,1,1);
3657
13597
  return (*tab->read_record.read_record)(&tab->read_record);
3658
13598
}
3659
13599
 
3660
 
int join_read_first(JoinTable *tab)
 
13600
 
 
13601
static int
 
13602
join_read_first(JOIN_TAB *tab)
3661
13603
{
3662
13604
  int error;
3663
 
  Table *table=tab->table;
3664
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
13605
  TABLE *table=tab->table;
 
13606
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3665
13607
      !table->no_keyread)
3666
13608
  {
3667
 
    table->key_read= 1;
3668
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
13609
    table->key_read=1;
 
13610
    table->file->extra(HA_EXTRA_KEYREAD);
3669
13611
  }
3670
 
  tab->table->status= 0;
 
13612
  tab->table->status=0;
3671
13613
  tab->read_record.table=table;
3672
 
  tab->read_record.cursor=table->cursor;
 
13614
  tab->read_record.file=table->file;
3673
13615
  tab->read_record.index=tab->index;
3674
 
  tab->read_record.record=table->getInsertRecord();
 
13616
  tab->read_record.record=table->record[0];
3675
13617
  if (tab->insideout_match_tab)
3676
13618
  {
3677
13619
    tab->read_record.do_insideout_scan= tab;
3684
13626
    tab->read_record.do_insideout_scan= 0;
3685
13627
  }
3686
13628
 
3687
 
  if (!table->cursor->inited)
3688
 
  {
3689
 
    error= table->cursor->startIndexScan(tab->index, tab->sorted);
3690
 
    if (error != 0)
3691
 
    {
3692
 
      table->report_error(error);
3693
 
      return -1;
3694
 
    }
3695
 
  }
3696
 
  if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
 
13629
  if (!table->file->inited)
 
13630
    table->file->ha_index_init(tab->index, tab->sorted);
 
13631
  if ((error=tab->table->file->index_first(tab->table->record[0])))
3697
13632
  {
3698
13633
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3699
 
      table->report_error(error);
 
13634
      report_error(table, error);
3700
13635
    return -1;
3701
13636
  }
3702
 
 
3703
13637
  return 0;
3704
13638
}
3705
13639
 
3706
 
int join_read_next_different(ReadRecord *info)
 
13640
 
 
13641
static int
 
13642
join_read_next_different(READ_RECORD *info)
3707
13643
{
3708
 
  JoinTable *tab= info->do_insideout_scan;
 
13644
  JOIN_TAB *tab= info->do_insideout_scan;
3709
13645
  if (tab->insideout_match_tab->found_match)
3710
13646
  {
3711
 
    KeyInfo *key= tab->table->key_info + tab->index;
3712
 
    do
 
13647
    KEY *key= tab->table->key_info + tab->index;
 
13648
    do 
3713
13649
    {
3714
13650
      int error;
3715
13651
      /* Save index tuple from record to the buffer */
3716
13652
      key_copy(tab->insideout_buf, info->record, key, 0);
3717
13653
 
3718
 
      if ((error=info->cursor->index_next(info->record)))
3719
 
        return info->table->report_error(error);
3720
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
13654
      if ((error=info->file->index_next(info->record)))
 
13655
        return report_error(info->table, error);
 
13656
      
 
13657
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3721
13658
                      tab->insideout_buf, key->key_length));
3722
13659
    tab->insideout_match_tab->found_match= 0;
3723
13660
    return 0;
3726
13663
    return join_read_next(info);
3727
13664
}
3728
13665
 
3729
 
int join_read_next(ReadRecord *info)
 
13666
 
 
13667
static int
 
13668
join_read_next(READ_RECORD *info)
3730
13669
{
3731
13670
  int error;
3732
 
  if ((error=info->cursor->index_next(info->record)))
3733
 
    return info->table->report_error(error);
 
13671
  if ((error=info->file->index_next(info->record)))
 
13672
    return report_error(info->table, error);
3734
13673
  return 0;
3735
13674
}
3736
13675
 
3737
 
int join_read_last(JoinTable *tab)
 
13676
 
 
13677
static int
 
13678
join_read_last(JOIN_TAB *tab)
3738
13679
{
3739
 
  Table *table=tab->table;
 
13680
  TABLE *table=tab->table;
3740
13681
  int error;
3741
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
13682
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3742
13683
      !table->no_keyread)
3743
13684
  {
3744
13685
    table->key_read=1;
3745
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
13686
    table->file->extra(HA_EXTRA_KEYREAD);
3746
13687
  }
3747
13688
  tab->table->status=0;
3748
13689
  tab->read_record.read_record=join_read_prev;
3749
13690
  tab->read_record.table=table;
3750
 
  tab->read_record.cursor=table->cursor;
 
13691
  tab->read_record.file=table->file;
3751
13692
  tab->read_record.index=tab->index;
3752
 
  tab->read_record.record=table->getInsertRecord();
3753
 
  if (!table->cursor->inited)
3754
 
  {
3755
 
    error= table->cursor->startIndexScan(tab->index, 1);
3756
 
    if (error != 0)
3757
 
      return table->report_error(error);
3758
 
  }
3759
 
  if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
3760
 
    return table->report_error(error);
3761
 
 
 
13693
  tab->read_record.record=table->record[0];
 
13694
  if (!table->file->inited)
 
13695
    table->file->ha_index_init(tab->index, 1);
 
13696
  if ((error= tab->table->file->index_last(tab->table->record[0])))
 
13697
    return report_error(table, error);
3762
13698
  return 0;
3763
13699
}
3764
13700
 
3765
 
int join_read_prev(ReadRecord *info)
 
13701
 
 
13702
static int
 
13703
join_read_prev(READ_RECORD *info)
3766
13704
{
3767
13705
  int error;
3768
 
  if ((error= info->cursor->index_prev(info->record)))
3769
 
    return info->table->report_error(error);
3770
 
 
 
13706
  if ((error= info->file->index_prev(info->record)))
 
13707
    return report_error(info->table, error);
3771
13708
  return 0;
3772
13709
}
3773
13710
 
3774
13711
/**
3775
13712
  Reading of key with key reference and one part that may be NULL.
3776
13713
*/
3777
 
int join_read_always_key_or_null(JoinTable *tab)
 
13714
 
 
13715
int
 
13716
join_read_always_key_or_null(JOIN_TAB *tab)
3778
13717
{
3779
13718
  int res;
3780
13719
 
3788
13727
  return safe_index_read(tab);
3789
13728
}
3790
13729
 
3791
 
int join_read_next_same_or_null(ReadRecord *info)
 
13730
 
 
13731
int
 
13732
join_read_next_same_or_null(READ_RECORD *info)
3792
13733
{
3793
13734
  int error;
3794
13735
  if ((error= join_read_next_same(info)) >= 0)
3795
13736
    return error;
3796
 
  JoinTable *tab= info->table->reginfo.join_tab;
 
13737
  JOIN_TAB *tab= info->table->reginfo.join_tab;
3797
13738
 
3798
13739
  /* Test if we have already done a read after null key */
3799
13740
  if (*tab->ref.null_ref_key)
3802
13743
  return safe_index_read(tab);                  // then read null keys
3803
13744
}
3804
13745
 
3805
 
enum_nested_loop_state end_send_group(Join *join, JoinTable *, bool end_of_records)
 
13746
 
 
13747
/*****************************************************************************
 
13748
  DESCRIPTION
 
13749
    Functions that end one nested loop iteration. Different functions
 
13750
    are used to support GROUP BY clause and to redirect records
 
13751
    to a table (e.g. in case of SELECT into a temporary table) or to the
 
13752
    network client.
 
13753
 
 
13754
  RETURN VALUES
 
13755
    NESTED_LOOP_OK           - the record has been successfully handled
 
13756
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
 
13757
                               was detected
 
13758
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
 
13759
                               the record
 
13760
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
 
13761
                               additionally, the nested loop produced the
 
13762
                               number of rows specified in the LIMIT clause
 
13763
                               for the query
 
13764
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
 
13765
                               additionally, there is a cursor and the nested
 
13766
                               loop algorithm produced the number of rows
 
13767
                               that is specified for current cursor fetch
 
13768
                               operation.
 
13769
   All return values except NESTED_LOOP_OK abort the nested loop.
 
13770
*****************************************************************************/
 
13771
 
 
13772
/* ARGSUSED */
 
13773
static enum_nested_loop_state
 
13774
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
13775
         bool end_of_records)
 
13776
{
 
13777
  if (!end_of_records)
 
13778
  {
 
13779
    int error;
 
13780
    if (join->having && join->having->val_int() == 0)
 
13781
      return(NESTED_LOOP_OK);               // Didn't match having
 
13782
    error=0;
 
13783
    if (join->do_send_rows)
 
13784
      error=join->result->send_data(*join->fields);
 
13785
    if (error)
 
13786
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
 
13787
    if (++join->send_records >= join->unit->select_limit_cnt &&
 
13788
        join->do_send_rows)
 
13789
    {
 
13790
      if (join->select_options & OPTION_FOUND_ROWS)
 
13791
      {
 
13792
        JOIN_TAB *jt=join->join_tab;
 
13793
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
 
13794
            && !join->send_group_parts && !join->having && !jt->select_cond &&
 
13795
            !(jt->select && jt->select->quick) &&
 
13796
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
13797
            (jt->ref.key < 0))
 
13798
        {
 
13799
          /* Join over all rows in table;  Return number of found rows */
 
13800
          TABLE *table=jt->table;
 
13801
 
 
13802
          join->select_options ^= OPTION_FOUND_ROWS;
 
13803
          if (table->sort.record_pointers ||
 
13804
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
 
13805
          {
 
13806
            /* Using filesort */
 
13807
            join->send_records= table->sort.found_records;
 
13808
          }
 
13809
          else
 
13810
          {
 
13811
            table->file->info(HA_STATUS_VARIABLE);
 
13812
            join->send_records= table->file->stats.records;
 
13813
          }
 
13814
        }
 
13815
        else 
 
13816
        {
 
13817
          join->do_send_rows= 0;
 
13818
          if (join->unit->fake_select_lex)
 
13819
            join->unit->fake_select_lex->select_limit= 0;
 
13820
          return(NESTED_LOOP_OK);
 
13821
        }
 
13822
      }
 
13823
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
 
13824
    }
 
13825
    else if (join->send_records >= join->fetch_limit)
 
13826
    {
 
13827
      /*
 
13828
        There is a server side cursor and all rows for
 
13829
        this fetch request are sent.
 
13830
      */
 
13831
      return(NESTED_LOOP_CURSOR_LIMIT);
 
13832
    }
 
13833
  }
 
13834
 
 
13835
  return(NESTED_LOOP_OK);
 
13836
}
 
13837
 
 
13838
 
 
13839
        /* ARGSUSED */
 
13840
enum_nested_loop_state
 
13841
end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
13842
               bool end_of_records)
3806
13843
{
3807
13844
  int idx= -1;
3808
13845
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
3810
13847
  if (!join->first_record || end_of_records ||
3811
13848
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3812
13849
  {
3813
 
    if (join->first_record ||
 
13850
    if (join->first_record || 
3814
13851
        (end_of_records && !join->group && !join->group_optimized_away))
3815
13852
    {
3816
13853
      if (idx < (int) join->send_group_parts)
3817
13854
      {
3818
 
        int error=0;
3819
 
        {
3820
 
          if (!join->first_record)
3821
 
          {
3822
 
                  List<Item>::iterator it(join->fields->begin());
3823
 
                  Item *item;
3824
 
            /* No matching rows for group function */
3825
 
            join->clear();
 
13855
        int error=0;
 
13856
        {
 
13857
          if (!join->first_record)
 
13858
          {
 
13859
            List_iterator_fast<Item> it(*join->fields);
 
13860
            Item *item;
 
13861
            /* No matching rows for group function */
 
13862
            join->clear();
3826
13863
 
3827
13864
            while ((item= it++))
3828
13865
              item->no_rows_in_result();
3829
 
          }
3830
 
          if (join->having && join->having->val_int() == 0)
3831
 
            error= -1;                          // Didn't satisfy having
3832
 
          else
3833
 
          {
3834
 
            if (join->do_send_rows)
3835
 
              error=join->result->send_data(*join->fields) ? 1 : 0;
3836
 
            join->send_records++;
3837
 
          }
3838
 
          if (join->rollup.getState() != Rollup::STATE_NONE && error <= 0)
3839
 
          {
3840
 
            if (join->rollup_send_data((uint32_t) (idx+1)))
3841
 
              error= 1;
3842
 
          }
3843
 
        }
3844
 
        if (error > 0)
3845
 
          return(NESTED_LOOP_ERROR);
3846
 
        if (end_of_records)
3847
 
          return(NESTED_LOOP_OK);
3848
 
        if (join->send_records >= join->unit->select_limit_cnt &&
3849
 
            join->do_send_rows)
3850
 
        {
3851
 
          if (!(join->select_options & OPTION_FOUND_ROWS))
3852
 
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
3853
 
          join->do_send_rows=0;
3854
 
          join->unit->select_limit_cnt = HA_POS_ERROR;
 
13866
          }
 
13867
          if (join->having && join->having->val_int() == 0)
 
13868
            error= -1;                          // Didn't satisfy having
 
13869
          else
 
13870
          {
 
13871
            if (join->do_send_rows)
 
13872
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
13873
            join->send_records++;
 
13874
          }
 
13875
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
13876
          {
 
13877
            if (join->rollup_send_data((uint) (idx+1)))
 
13878
              error= 1;
 
13879
          }
 
13880
        }
 
13881
        if (error > 0)
 
13882
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
 
13883
        if (end_of_records)
 
13884
          return(NESTED_LOOP_OK);
 
13885
        if (join->send_records >= join->unit->select_limit_cnt &&
 
13886
            join->do_send_rows)
 
13887
        {
 
13888
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
13889
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
13890
          join->do_send_rows=0;
 
13891
          join->unit->select_limit_cnt = HA_POS_ERROR;
3855
13892
        }
3856
13893
        else if (join->send_records >= join->fetch_limit)
3857
13894
        {
3870
13907
    else
3871
13908
    {
3872
13909
      if (end_of_records)
3873
 
        return(NESTED_LOOP_OK);
 
13910
        return(NESTED_LOOP_OK);
3874
13911
      join->first_record=1;
3875
 
      test_if_item_cache_changed(join->group_fields);
 
13912
      VOID(test_if_item_cache_changed(join->group_fields));
3876
13913
    }
3877
13914
    if (idx < (int) join->send_group_parts)
3878
13915
    {
3882
13919
      */
3883
13920
      copy_fields(&join->tmp_table_param);
3884
13921
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3885
 
        return(NESTED_LOOP_ERROR);
 
13922
        return(NESTED_LOOP_ERROR);
3886
13923
      return(ok_code);
3887
13924
    }
3888
13925
  }
3891
13928
  return(NESTED_LOOP_OK);
3892
13929
}
3893
13930
 
3894
 
enum_nested_loop_state end_write_group(Join *join, JoinTable *, bool end_of_records)
3895
 
{
3896
 
  Table *table=join->tmp_table;
 
13931
 
 
13932
        /* ARGSUSED */
 
13933
enum_nested_loop_state
 
13934
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
13935
          bool end_of_records)
 
13936
{
 
13937
  TABLE *table=join->tmp_table;
 
13938
 
 
13939
  if (join->thd->killed)                        // Aborted by user
 
13940
  {
 
13941
    join->thd->send_kill_message();
 
13942
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
13943
  }
 
13944
  if (!end_of_records)
 
13945
  {
 
13946
    copy_fields(&join->tmp_table_param);
 
13947
    copy_funcs(join->tmp_table_param.items_to_copy);
 
13948
#ifdef TO_BE_DELETED
 
13949
    if (!table->uniques)                        // If not unique handling
 
13950
    {
 
13951
      /* Copy null values from group to row */
 
13952
      ORDER   *group;
 
13953
      for (group=table->group ; group ; group=group->next)
 
13954
      {
 
13955
        Item *item= *group->item;
 
13956
        if (item->maybe_null)
 
13957
        {
 
13958
          Field *field=item->get_tmp_table_field();
 
13959
          field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0);
 
13960
        }
 
13961
      }
 
13962
    }
 
13963
#endif
 
13964
    if (!join->having || join->having->val_int())
 
13965
    {
 
13966
      int error;
 
13967
      join->found_records++;
 
13968
      if ((error=table->file->ha_write_row(table->record[0])))
 
13969
      {
 
13970
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
 
13971
          goto end;
 
13972
        if (create_myisam_from_heap(join->thd, table,
 
13973
                                    join->tmp_table_param.start_recinfo,
 
13974
                                    &join->tmp_table_param.recinfo,
 
13975
                                    error, 1))
 
13976
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
 
13977
        table->s->uniques=0;                    // To ensure rows are the same
 
13978
      }
 
13979
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
 
13980
          join->do_send_rows)
 
13981
      {
 
13982
        if (!(join->select_options & OPTION_FOUND_ROWS))
 
13983
          return(NESTED_LOOP_QUERY_LIMIT);
 
13984
        join->do_send_rows=0;
 
13985
        join->unit->select_limit_cnt = HA_POS_ERROR;
 
13986
        return(NESTED_LOOP_OK);
 
13987
      }
 
13988
    }
 
13989
  }
 
13990
end:
 
13991
  return(NESTED_LOOP_OK);
 
13992
}
 
13993
 
 
13994
/* ARGSUSED */
 
13995
/** Group by searching after group record and updating it if possible. */
 
13996
 
 
13997
static enum_nested_loop_state
 
13998
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
13999
           bool end_of_records)
 
14000
{
 
14001
  TABLE *table=join->tmp_table;
 
14002
  ORDER   *group;
 
14003
  int     error;
 
14004
 
 
14005
  if (end_of_records)
 
14006
    return(NESTED_LOOP_OK);
 
14007
  if (join->thd->killed)                        // Aborted by user
 
14008
  {
 
14009
    join->thd->send_kill_message();
 
14010
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
14011
  }
 
14012
 
 
14013
  join->found_records++;
 
14014
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
14015
  /* Make a key of group index */
 
14016
  for (group=table->group ; group ; group=group->next)
 
14017
  {
 
14018
    Item *item= *group->item;
 
14019
    item->save_org_in_field(group->field);
 
14020
    /* Store in the used key if the field was 0 */
 
14021
    if (item->maybe_null)
 
14022
      group->buff[-1]= (char) group->field->is_null();
 
14023
  }
 
14024
  if (!table->file->index_read_map(table->record[1],
 
14025
                                   join->tmp_table_param.group_buff,
 
14026
                                   HA_WHOLE_KEY,
 
14027
                                   HA_READ_KEY_EXACT))
 
14028
  {                                             /* Update old record */
 
14029
    restore_record(table,record[1]);
 
14030
    update_tmptable_sum_func(join->sum_funcs,table);
 
14031
    if ((error=table->file->ha_update_row(table->record[1],
 
14032
                                          table->record[0])))
 
14033
    {
 
14034
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
14035
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14036
    }
 
14037
    return(NESTED_LOOP_OK);
 
14038
  }
 
14039
 
 
14040
  /*
 
14041
    Copy null bits from group key to table
 
14042
    We can't copy all data as the key may have different format
 
14043
    as the row data (for example as with VARCHAR keys)
 
14044
  */
 
14045
  KEY_PART_INFO *key_part;
 
14046
  for (group=table->group,key_part=table->key_info[0].key_part;
 
14047
       group ;
 
14048
       group=group->next,key_part++)
 
14049
  {
 
14050
    if (key_part->null_bit)
 
14051
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
 
14052
  }
 
14053
  init_tmptable_sum_functions(join->sum_funcs);
 
14054
  copy_funcs(join->tmp_table_param.items_to_copy);
 
14055
  if ((error=table->file->ha_write_row(table->record[0])))
 
14056
  {
 
14057
    if (create_myisam_from_heap(join->thd, table,
 
14058
                                join->tmp_table_param.start_recinfo,
 
14059
                                &join->tmp_table_param.recinfo,
 
14060
                                error, 0))
 
14061
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
 
14062
    /* Change method to update rows */
 
14063
    table->file->ha_index_init(0, 0);
 
14064
    join->join_tab[join->tables-1].next_select=end_unique_update;
 
14065
  }
 
14066
  join->send_records++;
 
14067
  return(NESTED_LOOP_OK);
 
14068
}
 
14069
 
 
14070
 
 
14071
/** Like end_update, but this is done with unique constraints instead of keys.  */
 
14072
 
 
14073
static enum_nested_loop_state
 
14074
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
14075
                  bool end_of_records)
 
14076
{
 
14077
  TABLE *table=join->tmp_table;
 
14078
  int     error;
 
14079
 
 
14080
  if (end_of_records)
 
14081
    return(NESTED_LOOP_OK);
 
14082
  if (join->thd->killed)                        // Aborted by user
 
14083
  {
 
14084
    join->thd->send_kill_message();
 
14085
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
14086
  }
 
14087
 
 
14088
  init_tmptable_sum_functions(join->sum_funcs);
 
14089
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
14090
  copy_funcs(join->tmp_table_param.items_to_copy);
 
14091
 
 
14092
  if (!(error=table->file->ha_write_row(table->record[0])))
 
14093
    join->send_records++;                       // New group
 
14094
  else
 
14095
  {
 
14096
    if ((int) table->file->get_dup_key(error) < 0)
 
14097
    {
 
14098
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
14099
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14100
    }
 
14101
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
14102
    {
 
14103
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
14104
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14105
    }
 
14106
    restore_record(table,record[1]);
 
14107
    update_tmptable_sum_func(join->sum_funcs,table);
 
14108
    if ((error=table->file->ha_update_row(table->record[1],
 
14109
                                          table->record[0])))
 
14110
    {
 
14111
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
14112
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14113
    }
 
14114
  }
 
14115
  return(NESTED_LOOP_OK);
 
14116
}
 
14117
 
 
14118
 
 
14119
        /* ARGSUSED */
 
14120
enum_nested_loop_state
 
14121
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
14122
                bool end_of_records)
 
14123
{
 
14124
  TABLE *table=join->tmp_table;
3897
14125
  int     idx= -1;
3898
14126
 
3899
 
  if (join->session->getKilled())
 
14127
  if (join->thd->killed)
3900
14128
  {                                             // Aborted by user
3901
 
    join->session->send_kill_message();
3902
 
    return NESTED_LOOP_KILLED;
 
14129
    join->thd->send_kill_message();
 
14130
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
3903
14131
  }
3904
14132
  if (!join->first_record || end_of_records ||
3905
14133
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3909
14137
      int send_group_parts= join->send_group_parts;
3910
14138
      if (idx < send_group_parts)
3911
14139
      {
3912
 
        if (!join->first_record)
3913
 
        {
3914
 
          /* No matching rows for group function */
3915
 
          join->clear();
3916
 
        }
3917
 
        copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
3918
 
        if (!join->having || join->having->val_int())
3919
 
        {
3920
 
          int error= table->cursor->insertRecord(table->getInsertRecord());
3921
 
 
3922
 
          if (error)
3923
 
          {
3924
 
            my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
3925
 
            return NESTED_LOOP_ERROR;
3926
 
          }
3927
 
        }
3928
 
        if (join->rollup.getState() != Rollup::STATE_NONE)
3929
 
        {
3930
 
          if (join->rollup_write_data((uint32_t) (idx+1), table))
3931
 
            return NESTED_LOOP_ERROR;
3932
 
        }
3933
 
        if (end_of_records)
3934
 
          return NESTED_LOOP_OK;
 
14140
        if (!join->first_record)
 
14141
        {
 
14142
          /* No matching rows for group function */
 
14143
          join->clear();
 
14144
        }
 
14145
        copy_sum_funcs(join->sum_funcs,
 
14146
                       join->sum_funcs_end[send_group_parts]);
 
14147
        if (!join->having || join->having->val_int())
 
14148
        {
 
14149
          int error= table->file->ha_write_row(table->record[0]);
 
14150
          if (error && create_myisam_from_heap(join->thd, table,
 
14151
                                               join->tmp_table_param.start_recinfo,
 
14152
                                                &join->tmp_table_param.recinfo,
 
14153
                                               error, 0))
 
14154
            return(NESTED_LOOP_ERROR);
 
14155
        }
 
14156
        if (join->rollup.state != ROLLUP::STATE_NONE)
 
14157
        {
 
14158
          if (join->rollup_write_data((uint) (idx+1), table))
 
14159
            return(NESTED_LOOP_ERROR);
 
14160
        }
 
14161
        if (end_of_records)
 
14162
          return(NESTED_LOOP_OK);
3935
14163
      }
3936
14164
    }
3937
14165
    else
3938
14166
    {
3939
14167
      if (end_of_records)
3940
 
        return NESTED_LOOP_OK;
 
14168
        return(NESTED_LOOP_OK);
3941
14169
      join->first_record=1;
3942
 
      test_if_item_cache_changed(join->group_fields);
 
14170
      VOID(test_if_item_cache_changed(join->group_fields));
3943
14171
    }
3944
14172
    if (idx < (int) join->send_group_parts)
3945
14173
    {
3946
14174
      copy_fields(&join->tmp_table_param);
3947
 
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3948
 
        return NESTED_LOOP_ERROR;
 
14175
      copy_funcs(join->tmp_table_param.items_to_copy);
3949
14176
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3950
 
        return NESTED_LOOP_ERROR;
3951
 
      return NESTED_LOOP_OK;
 
14177
        return(NESTED_LOOP_ERROR);
 
14178
      return(NESTED_LOOP_OK);
3952
14179
    }
3953
14180
  }
3954
14181
  if (update_sum_func(join->sum_funcs))
3955
 
    return NESTED_LOOP_ERROR;
3956
 
  return NESTED_LOOP_OK;
 
14182
    return(NESTED_LOOP_ERROR);
 
14183
  return(NESTED_LOOP_OK);
3957
14184
}
3958
14185
 
 
14186
 
3959
14187
/*****************************************************************************
3960
14188
  Remove calculation with tables that aren't yet read. Remove also tests
3961
14189
  against fields that are read through key where the table is not a
3962
14190
  outer join table.
3963
14191
  We can't remove tests that are made against columns which are stored
3964
14192
  in sorted order.
 
14193
*****************************************************************************/
 
14194
 
 
14195
/**
3965
14196
  @return
3966
 
    1 if right_item used is a removable reference key on left_item
3967
 
    0 otherwise.
3968
 
****************************************************************************/
3969
 
bool test_if_ref(Item_field *left_item,Item *right_item)
 
14197
    1 if right_item is used removable reference key on left_item
 
14198
*/
 
14199
 
 
14200
static bool test_if_ref(Item_field *left_item,Item *right_item)
3970
14201
{
3971
14202
  Field *field=left_item->field;
3972
14203
  // No need to change const test. We also have to keep tests on LEFT JOIN
3973
 
  if (not field->getTable()->const_table && !field->getTable()->maybe_null)
 
14204
  if (!field->table->const_table && !field->table->maybe_null)
3974
14205
  {
3975
 
    Item *ref_item=part_of_refkey(field->getTable(),field);
 
14206
    Item *ref_item=part_of_refkey(field->table,field);
3976
14207
    if (ref_item && ref_item->eq(right_item,1))
3977
14208
    {
3978
14209
      right_item= right_item->real_item();
3979
14210
      if (right_item->type() == Item::FIELD_ITEM)
3980
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
14211
        return (field->eq_def(((Item_field *) right_item)->field));
3981
14212
      /* remove equalities injected by IN->EXISTS transformation */
3982
14213
      else if (right_item->type() == Item::CACHE_ITEM)
3983
14214
        return ((Item_cache *)right_item)->eq_def (field);
3984
14215
      if (right_item->const_item() && !(right_item->is_null()))
3985
14216
      {
3986
 
        /*
3987
 
          We can remove binary fields and numerical fields except float,
3988
 
          as float comparison isn't 100 % secure
3989
 
          We have to keep normal strings to be able to check for end spaces
 
14217
        /*
 
14218
          We can remove binary fields and numerical fields except float,
 
14219
          as float comparison isn't 100 % secure
 
14220
          We have to keep normal strings to be able to check for end spaces
3990
14221
 
3991
 
                sergefp: the above seems to be too restrictive. Counterexample:
3992
 
                  create table t100 (v varchar(10), key(v)) default charset=latin1;
3993
 
                  insert into t100 values ('a'),('a ');
3994
 
                  explain select * from t100 where v='a';
3995
 
                The EXPLAIN shows 'using Where'. Running the query returns both
3996
 
                rows, so it seems there are no problems with endspace in the most
3997
 
                frequent case?
3998
 
        */
3999
 
        if (field->binary() &&
4000
 
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
4001
 
            field->decimals() == 0)
4002
 
        {
4003
 
          return ! store_val_in_field(field, right_item, CHECK_FIELD_WARN);
4004
 
        }
 
14222
          sergefp: the above seems to be too restrictive. Counterexample:
 
14223
            create table t100 (v varchar(10), key(v)) default charset=latin1;
 
14224
            insert into t100 values ('a'),('a ');
 
14225
            explain select * from t100 where v='a';
 
14226
          The EXPLAIN shows 'using Where'. Running the query returns both
 
14227
          rows, so it seems there are no problems with endspace in the most
 
14228
          frequent case?
 
14229
        */
 
14230
        if (field->binary() &&
 
14231
            field->real_type() != MYSQL_TYPE_STRING &&
 
14232
            field->real_type() != MYSQL_TYPE_VARCHAR &&
 
14233
            field->decimals() == 0)
 
14234
        {
 
14235
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
14236
        }
4005
14237
      }
4006
14238
    }
4007
14239
  }
4008
 
  return 0;
 
14240
  return 0;                                     // keep test
 
14241
}
 
14242
 
 
14243
/**
 
14244
   @brief Replaces an expression destructively inside the expression tree of
 
14245
   the WHERE clase.
 
14246
 
 
14247
   @note Because of current requirements for semijoin flattening, we do not
 
14248
   need to recurse here, hence this function will only examine the top-level
 
14249
   AND conditions. (see JOIN::prepare, comment above the line 
 
14250
   'if (do_materialize)'
 
14251
   
 
14252
   @param join The top-level query.
 
14253
   @param old_cond The expression to be replaced.
 
14254
   @param new_cond The expression to be substituted.
 
14255
   @param do_fix_fields If true, Item::fix_fields(THD*, Item**) is called for
 
14256
   the new expression.
 
14257
   @return <code>true</code> if there was an error, <code>false</code> if
 
14258
   successful.
 
14259
*/
 
14260
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
14261
                                       Item *new_cond, bool do_fix_fields)
 
14262
{
 
14263
  if (join->conds == old_cond) {
 
14264
    join->conds= new_cond;
 
14265
    if (do_fix_fields)
 
14266
      new_cond->fix_fields(join->thd, &join->conds);
 
14267
    return false;
 
14268
  }
 
14269
  
 
14270
  if (join->conds->type() == Item::COND_ITEM) {
 
14271
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
 
14272
    Item *item;
 
14273
    while ((item= li++))
 
14274
      if (item == old_cond) 
 
14275
      {
 
14276
        li.replace(new_cond);
 
14277
        if (do_fix_fields)
 
14278
          new_cond->fix_fields(join->thd, li.ref());
 
14279
        return false;
 
14280
      }
 
14281
  }
 
14282
 
 
14283
  return true;
4009
14284
}
4010
14285
 
4011
14286
/*
4012
14287
  Extract a condition that can be checked after reading given table
4013
 
 
 
14288
  
4014
14289
  SYNOPSIS
4015
14290
    make_cond_for_table()
4016
14291
      cond         Condition to analyze
4017
14292
      tables       Tables for which "current field values" are available
4018
 
      used_table   Table that we're extracting the condition for (may
 
14293
      used_table   Table that we're extracting the condition for (may 
4019
14294
                   also include PSEUDO_TABLE_BITS
4020
14295
 
4021
14296
  DESCRIPTION
4025
14300
 
4026
14301
    The function assumes that
4027
14302
      - Constant parts of the condition has already been checked.
4028
 
      - Condition that could be checked for tables in 'tables' has already
 
14303
      - Condition that could be checked for tables in 'tables' has already 
4029
14304
        been checked.
4030
 
 
 
14305
        
4031
14306
    The function takes into account that some parts of the condition are
4032
14307
    guaranteed to be true by employed 'ref' access methods (the code that
4033
14308
    does this is located at the end, search down for "EQ_FUNC").
4034
14309
 
4035
14310
 
4036
 
  SEE ALSO
 
14311
  SEE ALSO 
4037
14312
    make_cond_for_info_schema uses similar algorithm
4038
14313
 
4039
14314
  RETURN
4040
14315
    Extracted condition
4041
14316
*/
4042
 
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
 
14317
 
 
14318
static COND *
 
14319
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
 
14320
                    bool exclude_expensive_cond)
4043
14321
{
4044
14322
  if (used_table && !(cond->used_tables() & used_table) &&
4045
 
    /*
4046
 
      Exclude constant conditions not checked at optimization time if
4047
 
      the table we are pushing conditions to is the first one.
4048
 
      As a result, such conditions are not considered as already checked
4049
 
      and will be checked at execution time, attached to the first table.
4050
 
    */
4051
 
    !((used_table & 1) && cond->is_expensive()))
 
14323
      /*
 
14324
        Exclude constant conditions not checked at optimization time if
 
14325
        the table we are pushing conditions to is the first one.
 
14326
        As a result, such conditions are not considered as already checked
 
14327
        and will be checked at execution time, attached to the first table.
 
14328
      */
 
14329
      !((used_table & 1) && cond->is_expensive()))
4052
14330
    return (COND*) 0;                           // Already checked
4053
14331
  if (cond->type() == Item::COND_ITEM)
4054
14332
  {
4057
14335
      /* Create new top level AND item */
4058
14336
      Item_cond_and *new_cond=new Item_cond_and;
4059
14337
      if (!new_cond)
4060
 
        return (COND*) 0;
4061
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
14338
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
14339
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4062
14340
      Item *item;
4063
14341
      while ((item=li++))
4064
14342
      {
4065
 
        Item *fix= make_cond_for_table(item,tables,used_table,
4066
 
                                            exclude_expensive_cond);
4067
 
        if (fix)
4068
 
          new_cond->argument_list()->push_back(fix);
 
14343
        Item *fix=make_cond_for_table(item,tables,used_table,
 
14344
                                      exclude_expensive_cond);
 
14345
        if (fix)
 
14346
          new_cond->argument_list()->push_back(fix);
4069
14347
      }
4070
 
      switch (new_cond->argument_list()->elements) 
4071
 
      {
4072
 
        case 0:
4073
 
          return (COND*) 0;                     // Always true
4074
 
        case 1:
4075
 
          return new_cond->argument_list()->head();
4076
 
        default:
4077
 
          /*
4078
 
            Item_cond_and do not need fix_fields for execution, its parameters
4079
 
            are fixed or do not need fix_fields, too
4080
 
          */
4081
 
          new_cond->quick_fix_field();
4082
 
          new_cond->used_tables_cache= ((Item_cond_and*) cond)->used_tables_cache & tables;
4083
 
          return new_cond;
 
14348
      switch (new_cond->argument_list()->elements) {
 
14349
      case 0:
 
14350
        return (COND*) 0;                       // Always true
 
14351
      case 1:
 
14352
        return new_cond->argument_list()->head();
 
14353
      default:
 
14354
        /*
 
14355
          Item_cond_and do not need fix_fields for execution, its parameters
 
14356
          are fixed or do not need fix_fields, too
 
14357
        */
 
14358
        new_cond->quick_fix_field();
 
14359
        new_cond->used_tables_cache=
 
14360
          ((Item_cond_and*) cond)->used_tables_cache &
 
14361
          tables;
 
14362
        return new_cond;
4084
14363
      }
4085
14364
    }
4086
14365
    else
4087
14366
    {                                           // Or list
4088
14367
      Item_cond_or *new_cond=new Item_cond_or;
4089
14368
      if (!new_cond)
4090
 
        return (COND*) 0;
4091
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
14369
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
14370
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4092
14371
      Item *item;
4093
14372
      while ((item=li++))
4094
14373
      {
4095
 
        Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
4096
 
        if (!fix)
4097
 
          return (COND*) 0;                     // Always true
4098
 
        new_cond->argument_list()->push_back(fix);
 
14374
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
14375
        if (!fix)
 
14376
          return (COND*) 0;                     // Always true
 
14377
        new_cond->argument_list()->push_back(fix);
4099
14378
      }
4100
14379
      /*
4101
 
        Item_cond_and do not need fix_fields for execution, its parameters
4102
 
        are fixed or do not need fix_fields, too
 
14380
        Item_cond_and do not need fix_fields for execution, its parameters
 
14381
        are fixed or do not need fix_fields, too
4103
14382
      */
4104
14383
      new_cond->quick_fix_field();
4105
14384
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4124
14403
  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
4125
14404
    return cond;                                // Not boolean op
4126
14405
 
4127
 
  /*
 
14406
  /* 
4128
14407
    Remove equalities that are guaranteed to be true by use of 'ref' access
4129
14408
    method
4130
14409
  */
4132
14411
  {
4133
14412
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
4134
14413
    Item *right_item= ((Item_func*) cond)->arguments()[1];
4135
 
    if (left_item->type() == Item::FIELD_ITEM && test_if_ref((Item_field*) left_item,right_item))
 
14414
    if (left_item->type() == Item::FIELD_ITEM &&
 
14415
        test_if_ref((Item_field*) left_item,right_item))
4136
14416
    {
4137
14417
      cond->marker=3;                   // Checked when read
4138
14418
      return (COND*) 0;
4139
14419
    }
4140
 
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
 
14420
    if (right_item->type() == Item::FIELD_ITEM &&
 
14421
        test_if_ref((Item_field*) right_item,left_item))
4141
14422
    {
4142
14423
      cond->marker=3;                   // Checked when read
4143
14424
      return (COND*) 0;
4147
14428
  return cond;
4148
14429
}
4149
14430
 
4150
 
static Item *part_of_refkey(Table *table,Field *field)
 
14431
 
 
14432
static Item *
 
14433
part_of_refkey(TABLE *table,Field *field)
4151
14434
{
4152
14435
  if (!table->reginfo.join_tab)
4153
14436
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
4154
14437
 
4155
 
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
 
14438
  uint ref_parts=table->reginfo.join_tab->ref.key_parts;
4156
14439
  if (ref_parts)
4157
14440
  {
4158
 
    KeyPartInfo *key_part=
 
14441
    KEY_PART_INFO *key_part=
4159
14442
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
4160
 
    uint32_t part;
 
14443
    uint part;
4161
14444
 
4162
14445
    for (part=0 ; part < ref_parts ; part++)
4163
14446
    {
4166
14449
    }
4167
14450
 
4168
14451
    for (part=0 ; part < ref_parts ; part++,key_part++)
4169
 
    {
4170
14452
      if (field->eq(key_part->field) &&
4171
 
          !(key_part->key_part_flag & HA_PART_KEY_SEG) &&
4172
 
          //If field can be NULL, we should not remove this predicate, as
4173
 
          //it may lead to non-rejection of NULL values. 
4174
 
          !(field->real_maybe_null()))
4175
 
      {
 
14453
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
4176
14454
        return table->reginfo.join_tab->ref.items[part];
4177
 
      }
4178
 
    }
4179
14455
  }
4180
14456
  return (Item*) 0;
4181
14457
}
4182
14458
 
 
14459
 
4183
14460
/**
4184
 
  Test if one can use the key to resolve order_st BY.
 
14461
  Test if one can use the key to resolve ORDER BY.
4185
14462
 
4186
14463
  @param order                 Sort order
4187
14464
  @param table                 Table to sort
4200
14477
  @retval
4201
14478
    -1   Reverse key can be used
4202
14479
*/
4203
 
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
14480
 
 
14481
static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
 
14482
                                uint *used_key_parts)
4204
14483
{
4205
 
  KeyPartInfo *key_part= NULL;
4206
 
  KeyPartInfo *key_part_end= NULL;
4207
 
  key_part= table->key_info[idx].key_part;
4208
 
  key_part_end= key_part + table->key_info[idx].key_parts;
 
14484
  KEY_PART_INFO *key_part,*key_part_end;
 
14485
  key_part=table->key_info[idx].key_part;
 
14486
  key_part_end=key_part+table->key_info[idx].key_parts;
4209
14487
  key_part_map const_key_parts=table->const_key_parts[idx];
4210
 
  int reverse= 0;
 
14488
  int reverse=0;
4211
14489
  bool on_primary_key= false;
4212
14490
 
4213
14491
  for (; order ; order=order->next, const_key_parts>>=1)
4220
14498
      These are already skipped in the ORDER BY by const_expression_in_where()
4221
14499
    */
4222
14500
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
4223
 
      key_part++;
 
14501
      key_part++; 
4224
14502
 
4225
14503
    if (key_part == key_part_end)
4226
14504
    {
4227
 
      /*
 
14505
      /* 
4228
14506
        We are at the end of the key. Check if the engine has the primary
4229
14507
        key as a suffix to the secondary keys. If it has continue to check
4230
14508
        the primary key as a suffix.
4231
14509
      */
4232
14510
      if (!on_primary_key &&
4233
 
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4234
 
          table->getShare()->hasPrimaryKey())
 
14511
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
 
14512
          table->s->primary_key != MAX_KEY)
4235
14513
      {
4236
14514
        on_primary_key= true;
4237
 
        key_part= table->key_info[table->getShare()->getPrimaryKey()].key_part;
4238
 
        key_part_end=key_part+table->key_info[table->getShare()->getPrimaryKey()].key_parts;
4239
 
        const_key_parts=table->const_key_parts[table->getShare()->getPrimaryKey()];
 
14515
        key_part= table->key_info[table->s->primary_key].key_part;
 
14516
        key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
 
14517
        const_key_parts=table->const_key_parts[table->s->primary_key];
4240
14518
 
4241
14519
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4242
 
          key_part++;
 
14520
          key_part++; 
4243
14521
        /*
4244
14522
         The primary and secondary key parts were all const (i.e. there's
4245
14523
         one row).  The sorting doesn't matter.
4263
14541
    key_part++;
4264
14542
  }
4265
14543
  *used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
4266
 
    (uint32_t) (key_part - table->key_info[idx].key_part);
4267
 
  if (reverse == -1 && !(table->index_flags(idx) &
 
14544
    (uint) (key_part - table->key_info[idx].key_part);
 
14545
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
4268
14546
                         HA_READ_PREV))
4269
14547
    reverse= 0;                                 // Index can't be used
4270
14548
  return(reverse);
4271
14549
}
4272
14550
 
 
14551
 
 
14552
uint find_shortest_key(TABLE *table, const key_map *usable_keys)
 
14553
{
 
14554
  uint min_length= (uint) ~0;
 
14555
  uint best= MAX_KEY;
 
14556
  if (!usable_keys->is_clear_all())
 
14557
  {
 
14558
    for (uint nr=0; nr < table->s->keys ; nr++)
 
14559
    {
 
14560
      if (usable_keys->is_set(nr))
 
14561
      {
 
14562
        if (table->key_info[nr].key_length < min_length)
 
14563
        {
 
14564
          min_length=table->key_info[nr].key_length;
 
14565
          best=nr;
 
14566
        }
 
14567
      }
 
14568
    }
 
14569
  }
 
14570
  return best;
 
14571
}
 
14572
 
4273
14573
/**
4274
14574
  Test if a second key is the subkey of the first one.
4275
14575
 
4285
14585
  @retval
4286
14586
    0   no sub key
4287
14587
*/
4288
 
inline bool is_subkey(KeyPartInfo *key_part,
4289
 
                      KeyPartInfo *ref_key_part,
4290
 
                      KeyPartInfo *ref_key_part_end)
 
14588
 
 
14589
inline bool 
 
14590
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
 
14591
          KEY_PART_INFO *ref_key_part_end)
4291
14592
{
4292
14593
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4293
 
    if (! key_part->field->eq(ref_key_part->field))
 
14594
    if (!key_part->field->eq(ref_key_part->field))
4294
14595
      return 0;
4295
14596
  return 1;
4296
14597
}
4306
14607
    - MAX_KEY                   If we can't use other key
4307
14608
    - the number of found key   Otherwise
4308
14609
*/
4309
 
static uint32_t test_if_subkey(Order *order,
4310
 
                               Table *table,
4311
 
                               uint32_t ref,
4312
 
                               uint32_t ref_key_parts,
4313
 
                               const key_map *usable_keys)
 
14610
 
 
14611
static uint
 
14612
test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts,
 
14613
               const key_map *usable_keys)
4314
14614
{
4315
 
  uint32_t nr;
4316
 
  uint32_t min_length= UINT32_MAX;
4317
 
  uint32_t best= MAX_KEY;
4318
 
  uint32_t not_used;
4319
 
  KeyPartInfo *ref_key_part= table->key_info[ref].key_part;
4320
 
  KeyPartInfo *ref_key_part_end= ref_key_part + ref_key_parts;
 
14615
  uint nr;
 
14616
  uint min_length= (uint) ~0;
 
14617
  uint best= MAX_KEY;
 
14618
  uint not_used;
 
14619
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
 
14620
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
4321
14621
 
4322
 
  for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
 
14622
  for (nr= 0 ; nr < table->s->keys ; nr++)
4323
14623
  {
4324
 
    if (usable_keys->test(nr) &&
 
14624
    if (usable_keys->is_set(nr) &&
4325
14625
        table->key_info[nr].key_length < min_length &&
4326
14626
        table->key_info[nr].key_parts >= ref_key_parts &&
4327
14627
        is_subkey(table->key_info[nr].key_part, ref_key_part,
4335
14635
  return best;
4336
14636
}
4337
14637
 
 
14638
 
4338
14639
/**
4339
14640
  Check if GROUP BY/DISTINCT can be optimized away because the set is
4340
14641
  already known to be distinct.
4353
14654
    of the table are referenced by a list : either the select list
4354
14655
    through find_field_in_item_list or GROUP BY list through
4355
14656
    find_field_in_order_list.
4356
 
    If the above holds and the key parts cannot contain NULLs then we
 
14657
    If the above holds and the key parts cannot contain NULLs then we 
4357
14658
    can safely remove the GROUP BY/DISTINCT,
4358
14659
    as no result set can be more distinct than an unique key.
4359
14660
 
4366
14667
  @retval
4367
14668
    0                    not found.
4368
14669
*/
4369
 
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
 
14670
 
 
14671
static bool
 
14672
list_contains_unique_index(TABLE *table,
 
14673
                          bool (*find_func) (Field *, void *), void *data)
4370
14674
{
4371
 
  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
 
14675
  for (uint keynr= 0; keynr < table->s->keys; keynr++)
4372
14676
  {
4373
 
    if (keynr == table->getShare()->getPrimaryKey() ||
 
14677
    if (keynr == table->s->primary_key ||
4374
14678
         (table->key_info[keynr].flags & HA_NOSAME))
4375
14679
    {
4376
 
      KeyInfo *keyinfo= table->key_info + keynr;
4377
 
      KeyPartInfo *key_part= NULL;
4378
 
      KeyPartInfo *key_part_end= NULL;
 
14680
      KEY *keyinfo= table->key_info + keynr;
 
14681
      KEY_PART_INFO *key_part, *key_part_end;
4379
14682
 
4380
14683
      for (key_part=keyinfo->key_part,
4381
14684
           key_part_end=key_part+ keyinfo->key_parts;
4382
14685
           key_part < key_part_end;
4383
14686
           key_part++)
4384
14687
      {
4385
 
        if (key_part->field->maybe_null() ||
4386
 
            ! find_func(key_part->field, data))
 
14688
        if (key_part->field->maybe_null() || 
 
14689
            !find_func(key_part->field, data))
4387
14690
          break;
4388
14691
      }
4389
14692
      if (key_part == key_part_end)
4393
14696
  return 0;
4394
14697
}
4395
14698
 
 
14699
 
4396
14700
/**
4397
14701
  Helper function for list_contains_unique_index.
4398
 
  Find a field reference in a list of order_st structures.
 
14702
  Find a field reference in a list of ORDER structures.
4399
14703
  Finds a direct reference of the Field in the list.
4400
14704
 
4401
14705
  @param field                The field to search for.
4402
 
  @param data                 order_st *.The list to search in
 
14706
  @param data                 ORDER *.The list to search in
4403
14707
 
4404
14708
  @retval
4405
14709
    1                    found
4406
14710
  @retval
4407
14711
    0                    not found.
4408
14712
*/
4409
 
bool find_field_in_order_list (Field *field, void *data)
 
14713
 
 
14714
static bool
 
14715
find_field_in_order_list (Field *field, void *data)
4410
14716
{
4411
 
  Order *group= (Order *) data;
 
14717
  ORDER *group= (ORDER *) data;
4412
14718
  bool part_found= 0;
4413
 
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
14719
  for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4414
14720
  {
4415
14721
    Item *item= (*tmp_group->item)->real_item();
4416
14722
    if (item->type() == Item::FIELD_ITEM &&
4423
14729
  return part_found;
4424
14730
}
4425
14731
 
 
14732
 
4426
14733
/**
4427
14734
  Helper function for list_contains_unique_index.
4428
14735
  Find a field reference in a dynamic list of Items.
4436
14743
  @retval
4437
14744
    0                    not found.
4438
14745
*/
4439
 
bool find_field_in_item_list (Field *field, void *data)
 
14746
 
 
14747
static bool
 
14748
find_field_in_item_list (Field *field, void *data)
4440
14749
{
4441
14750
  List<Item> *fields= (List<Item> *) data;
4442
14751
  bool part_found= 0;
4443
 
  List<Item>::iterator li(fields->begin());
 
14752
  List_iterator<Item> li(*fields);
4444
14753
  Item *item;
4445
14754
 
4446
14755
  while ((item= li++))
4455
14764
  return part_found;
4456
14765
}
4457
14766
 
 
14767
 
4458
14768
/**
4459
14769
  Test if we can skip the ORDER BY by using an index.
4460
14770
 
4466
14776
      no_changes
4467
14777
      map
4468
14778
 
4469
 
  If we can use an index, the JoinTable / tab->select struct
 
14779
  If we can use an index, the JOIN_TAB / tab->select struct
4470
14780
  is changed to use the index.
4471
14781
 
4472
14782
  The index must cover all fields in <order>, or it will not be considered.
4473
14783
 
4474
14784
  @todo
4475
 
    - sergeyp: Results of all index merge selects actually are ordered
 
14785
    - sergeyp: Results of all index merge selects actually are ordered 
4476
14786
    by clustered PK values.
4477
14787
 
4478
14788
  @retval
4480
14790
  @retval
4481
14791
    1    We can use an index.
4482
14792
*/
4483
 
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
14793
 
 
14794
static bool
 
14795
test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
 
14796
                        bool no_changes, const key_map *map)
4484
14797
{
4485
 
  int32_t ref_key;
4486
 
  uint32_t ref_key_parts;
 
14798
  int ref_key;
 
14799
  uint ref_key_parts;
4487
14800
  int order_direction;
4488
 
  uint32_t used_key_parts;
4489
 
  Table *table=tab->table;
4490
 
  optimizer::SqlSelect *select= tab->select;
 
14801
  uint used_key_parts;
 
14802
  TABLE *table=tab->table;
 
14803
  SQL_SELECT *select=tab->select;
4491
14804
  key_map usable_keys;
4492
 
  optimizer::QuickSelectInterface *save_quick= NULL;
 
14805
  QUICK_SELECT_I *save_quick= 0;
4493
14806
 
4494
14807
  /*
4495
 
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
 
14808
    Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
4496
14809
    been taken into account.
4497
14810
  */
4498
14811
  usable_keys= *map;
4499
14812
 
4500
 
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
14813
  for (ORDER *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4501
14814
  {
4502
14815
    Item *item= (*tmp_order->item)->real_item();
4503
14816
    if (item->type() != Item::FIELD_ITEM)
4504
14817
    {
4505
 
      usable_keys.reset();
 
14818
      usable_keys.clear_all();
4506
14819
      return(0);
4507
14820
    }
4508
 
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
4509
 
    if (usable_keys.none())
 
14821
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
 
14822
    if (usable_keys.is_clear_all())
4510
14823
      return(0);                                        // No usable keys
4511
14824
  }
4512
14825
 
4516
14829
  {
4517
14830
    ref_key=       tab->ref.key;
4518
14831
    ref_key_parts= tab->ref.key_parts;
4519
 
    if (tab->type == AM_REF_OR_NULL)
 
14832
    if (tab->type == JT_REF_OR_NULL)
4520
14833
      return(0);
4521
14834
  }
4522
 
  else if (select && select->quick)             // Range found by optimizer/range
 
14835
  else if (select && select->quick)             // Range found by opt_range
4523
14836
  {
4524
14837
    int quick_type= select->quick->get_type();
4525
14838
    save_quick= select->quick;
4526
 
    /*
4527
 
      assume results are not ordered when index merge is used
4528
 
      @todo sergeyp: Results of all index merge selects actually are ordered
 
14839
    /* 
 
14840
      assume results are not ordered when index merge is used 
 
14841
      TODO: sergeyp: Results of all index merge selects actually are ordered 
4529
14842
      by clustered PK values.
4530
14843
    */
4531
 
 
4532
 
    if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
4533
 
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
4534
 
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT)
 
14844
  
 
14845
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || 
 
14846
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
 
14847
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
4535
14848
      return(0);
4536
14849
    ref_key=       select->quick->index;
4537
14850
    ref_key_parts= select->quick->used_key_parts;
4542
14855
    /*
4543
14856
      We come here when there is a REF key.
4544
14857
    */
4545
 
    if (! usable_keys.test(ref_key))
 
14858
    if (!usable_keys.is_set(ref_key))
4546
14859
    {
4547
14860
      /*
4548
 
        We come here when ref_key is not among usable_keys
 
14861
        We come here when ref_key is not among usable_keys
4549
14862
      */
4550
 
      uint32_t new_ref_key;
 
14863
      uint new_ref_key;
4551
14864
      /*
4552
 
        If using index only read, only consider other possible index only
4553
 
        keys
 
14865
        If using index only read, only consider other possible index only
 
14866
        keys
4554
14867
      */
4555
 
      if (table->covering_keys.test(ref_key))
4556
 
        usable_keys&= table->covering_keys;
 
14868
      if (table->covering_keys.is_set(ref_key))
 
14869
        usable_keys.intersect(table->covering_keys);
4557
14870
      if (tab->pre_idx_push_select_cond)
4558
14871
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
4559
14872
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
4560
14873
                                       &usable_keys)) < MAX_KEY)
4561
14874
      {
4562
 
        /* Found key that can be used to retrieve data in sorted order */
4563
 
        if (tab->ref.key >= 0)
4564
 
        {
 
14875
        /* Found key that can be used to retrieve data in sorted order */
 
14876
        if (tab->ref.key >= 0)
 
14877
        {
4565
14878
          /*
4566
 
            We'll use ref access method on key new_ref_key. In general case
 
14879
            We'll use ref access method on key new_ref_key. In general case 
4567
14880
            the index search tuple for new_ref_key will be different (e.g.
4568
14881
            when one index is defined as (part1, part2, ...) and another as
4569
 
            (part1, part2(N), ...) and the WHERE clause contains
4570
 
            "part1 = const1 AND part2=const2".
 
14882
            (part1, part2(N), ...) and the WHERE clause contains 
 
14883
            "part1 = const1 AND part2=const2". 
4571
14884
            So we build tab->ref from scratch here.
4572
14885
          */
4573
 
          optimizer::KeyUse *keyuse= tab->keyuse;
4574
 
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
 
14886
          KEYUSE *keyuse= tab->keyuse;
 
14887
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
4575
14888
            keyuse++;
4576
14889
 
4577
 
          if (create_ref_for_key(tab->join, tab, keyuse,
 
14890
          if (create_ref_for_key(tab->join, tab, keyuse, 
4578
14891
                                 tab->join->const_table_map))
4579
14892
            return(0);
4580
 
        }
4581
 
        else
4582
 
        {
 
14893
        }
 
14894
        else
 
14895
        {
4583
14896
          /*
4584
 
            The range optimizer constructed QuickRange for ref_key, and
 
14897
            The range optimizer constructed QUICK_RANGE for ref_key, and
4585
14898
            we want to use instead new_ref_key as the index. We can't
4586
14899
            just change the index of the quick select, because this may
4587
14900
            result in an incosistent QUICK_SELECT object. Below we
4589
14902
            parameres are set correctly by the range optimizer.
4590
14903
           */
4591
14904
          key_map new_ref_key_map;
4592
 
          new_ref_key_map.reset();  // Force the creation of quick select
4593
 
          new_ref_key_map.set(new_ref_key); // only for new_ref_key.
 
14905
          new_ref_key_map.clear_all();  // Force the creation of quick select
 
14906
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
4594
14907
 
4595
 
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
 
14908
          if (select->test_quick_select(tab->join->thd, new_ref_key_map, 0,
4596
14909
                                        (tab->join->select_options &
4597
14910
                                         OPTION_FOUND_ROWS) ?
4598
14911
                                        HA_POS_ERROR :
4600
14913
                                        true) <=
4601
14914
              0)
4602
14915
            return(0);
4603
 
        }
 
14916
        }
4604
14917
        ref_key= new_ref_key;
4605
14918
      }
4606
14919
    }
4607
14920
    /* Check if we get the rows in requested sorted order by using the key */
4608
 
    if (usable_keys.test(ref_key) &&
 
14921
    if (usable_keys.is_set(ref_key) &&
4609
14922
        (order_direction= test_if_order_by_key(order,table,ref_key,
4610
14923
                                               &used_key_parts)))
4611
14924
      goto check_reverse_order;
4615
14928
      Check whether there is an index compatible with the given order
4616
14929
      usage of which is cheaper than usage of the ref_key index (ref_key>=0)
4617
14930
      or a table scan.
4618
 
      It may be the case if order_st/GROUP BY is used with LIMIT.
 
14931
      It may be the case if ORDER/GROUP BY is used with LIMIT.
4619
14932
    */
4620
 
    uint32_t nr;
 
14933
    uint nr;
4621
14934
    key_map keys;
4622
 
    uint32_t best_key_parts= 0;
 
14935
    uint best_key_parts= 0;
4623
14936
    int best_key_direction= 0;
4624
14937
    ha_rows best_records= 0;
4625
14938
    double read_time;
4626
14939
    int best_key= -1;
4627
14940
    bool is_best_covering= false;
4628
14941
    double fanout= 1;
4629
 
    Join *join= tab->join;
4630
 
    uint32_t tablenr= tab - join->join_tab;
4631
 
    ha_rows table_records= table->cursor->stats.records;
 
14942
    JOIN *join= tab->join;
 
14943
    uint tablenr= tab - join->join_tab;
 
14944
    ha_rows table_records= table->file->stats.records;
4632
14945
    bool group= join->group && order == join->group_list;
4633
 
    optimizer::Position cur_pos;
4634
14946
 
4635
14947
    /*
4636
14948
      If not used with LIMIT, only use keys if the whole query can be
4639
14951
    */
4640
14952
    if (select_limit >= table_records)
4641
14953
    {
4642
 
      /*
4643
 
        filesort() and join cache are usually faster than reading in
 
14954
      /* 
 
14955
        filesort() and join cache are usually faster than reading in 
4644
14956
        index order and not using join cache
4645
14957
        */
4646
 
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
 
14958
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
4647
14959
        return(0);
4648
 
      keys= *table->cursor->keys_to_use_for_scanning();
4649
 
      keys|= table->covering_keys;
 
14960
      keys= *table->file->keys_to_use_for_scanning();
 
14961
      keys.merge(table->covering_keys);
4650
14962
 
4651
14963
      /*
4652
 
        We are adding here also the index specified in FORCE INDEX clause,
4653
 
        if any.
4654
 
        This is to allow users to use index in order_st BY.
 
14964
        We are adding here also the index specified in FORCE INDEX clause, 
 
14965
        if any.
 
14966
        This is to allow users to use index in ORDER BY.
4655
14967
      */
4656
 
      if (table->force_index)
4657
 
        keys|= (group ? table->keys_in_use_for_group_by :
4658
 
                                table->keys_in_use_for_order_by);
4659
 
      keys&= usable_keys;
 
14968
      if (table->force_index) 
 
14969
        keys.merge(group ? table->keys_in_use_for_group_by :
 
14970
                           table->keys_in_use_for_order_by);
 
14971
      keys.intersect(usable_keys);
4660
14972
    }
4661
14973
    else
4662
14974
      keys= usable_keys;
4663
14975
 
4664
 
    cur_pos= join->getPosFromOptimalPlan(tablenr);
4665
 
    read_time= cur_pos.getCost();
4666
 
    for (uint32_t i= tablenr+1; i < join->tables; i++)
4667
 
    {
4668
 
      cur_pos= join->getPosFromOptimalPlan(i);
4669
 
      fanout*= cur_pos.getFanout(); // fanout is always >= 1
4670
 
    }
 
14976
    read_time= join->best_positions[tablenr].read_time;
 
14977
    for (uint i= tablenr+1; i < join->tables; i++)
 
14978
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
4671
14979
 
4672
 
    for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
 
14980
    for (nr=0; nr < table->s->keys ; nr++)
4673
14981
    {
4674
14982
      int direction;
4675
 
      if (keys.test(nr) &&
 
14983
      if (keys.is_set(nr) &&
4676
14984
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
4677
14985
      {
4678
 
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
4679
 
 
4680
 
        /*
 
14986
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
 
14987
        
 
14988
        /* 
4681
14989
          Don't use an index scan with ORDER BY without limit.
4682
14990
          For GROUP BY without limit always use index scan
4683
 
          if there is a suitable index.
 
14991
          if there is a suitable index. 
4684
14992
          Why we hold to this asymmetry hardly can be explained
4685
14993
          rationally. It's easy to demonstrate that using
4686
14994
          temporary table + filesort could be cheaper for grouping
4687
14995
          queries too.
4688
 
        */
 
14996
        */ 
4689
14997
        if (is_covering ||
4690
 
            select_limit != HA_POS_ERROR ||
 
14998
            select_limit != HA_POS_ERROR || 
4691
14999
            (ref_key < 0 && (group || table->force_index)))
4692
 
        {
 
15000
        { 
4693
15001
          double rec_per_key;
4694
15002
          double index_scan_time;
4695
 
          KeyInfo *keyinfo= tab->table->key_info+nr;
 
15003
          KEY *keyinfo= tab->table->key_info+nr;
4696
15004
          if (select_limit == HA_POS_ERROR)
4697
15005
            select_limit= table_records;
4698
15006
          if (group)
4699
15007
          {
4700
15008
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
4701
 
            set_if_bigger(rec_per_key, 1.0);
 
15009
            set_if_bigger(rec_per_key, 1);
4702
15010
            /*
4703
15011
              With a grouping query each group containing on average
4704
15012
              rec_per_key records produces only one row that will
4705
15013
              be included into the result set.
4706
 
            */
 
15014
            */  
4707
15015
            if (select_limit > table_records/rec_per_key)
4708
15016
                select_limit= table_records;
4709
15017
            else
4710
15018
              select_limit= (ha_rows) (select_limit*rec_per_key);
4711
15019
          }
4712
 
          /*
 
15020
          /* 
4713
15021
            If tab=tk is not the last joined table tn then to get first
4714
15022
            L records from the result set we can expect to retrieve
4715
15023
            only L/fanout(tk,tn) where fanout(tk,tn) says how many
4718
15026
            So the estimate for L/fanout(tk,tn) will be too optimistic
4719
15027
            and as result we'll choose an index scan when using ref/range
4720
15028
            access + filesort will be cheaper.
4721
 
          */
 
15029
          */
4722
15030
          select_limit= (ha_rows) (select_limit < fanout ?
4723
15031
                                   1 : select_limit/fanout);
4724
15032
          /*
4725
15033
            We assume that each of the tested indexes is not correlated
4726
15034
            with ref_key. Thus, to select first N records we have to scan
4727
 
            N/selectivity(ref_key) index entries.
 
15035
            N/selectivity(ref_key) index entries. 
4728
15036
            selectivity(ref_key) = #scanned_records/#table_records =
4729
15037
            table->quick_condition_rows/table_records.
4730
15038
            In any case we can't select more than #table_records.
4731
 
            N/(table->quick_condition_rows/table_records) > table_records
 
15039
            N/(table->quick_condition_rows/table_records) > table_records 
4732
15040
            <=> N > table->quick_condition_rows.
4733
 
          */
 
15041
          */ 
4734
15042
          if (select_limit > table->quick_condition_rows)
4735
15043
            select_limit= table_records;
4736
15044
          else
4738
15046
                                     (double) table_records /
4739
15047
                                      table->quick_condition_rows);
4740
15048
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
4741
 
          set_if_bigger(rec_per_key, 1.0);
 
15049
          set_if_bigger(rec_per_key, 1);
4742
15050
          /*
4743
15051
            Here we take into account the fact that rows are
4744
15052
            accessed in sequences rec_per_key records in each.
4745
15053
            Rows in such a sequence are supposed to be ordered
4746
15054
            by rowid/primary key. When reading the data
4747
15055
            in a sequence we'll touch not more pages than the
4748
 
            table cursor contains.
 
15056
            table file contains.
4749
15057
            TODO. Use the formula for a disk sweep sequential access
4750
 
            to calculate the cost of accessing data rows for one
 
15058
            to calculate the cost of accessing data rows for one 
4751
15059
            index entry.
4752
 
          */
 
15060
          */
4753
15061
          index_scan_time= select_limit/rec_per_key *
4754
 
                           min(rec_per_key, table->cursor->scan_time());
 
15062
                           min(rec_per_key, table->file->scan_time());
4755
15063
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
4756
15064
              index_scan_time < read_time)
4757
15065
          {
4758
15066
            ha_rows quick_records= table_records;
4759
15067
            if (is_best_covering && !is_covering)
4760
15068
              continue;
4761
 
            if (table->quick_keys.test(nr))
 
15069
            if (table->quick_keys.is_set(nr))
4762
15070
              quick_records= table->quick_rows[nr];
4763
15071
            if (best_key < 0 ||
4764
15072
                (select_limit <= min(quick_records,best_records) ?
4769
15077
              best_key_parts= keyinfo->key_parts;
4770
15078
              best_records= quick_records;
4771
15079
              is_best_covering= is_covering;
4772
 
              best_key_direction= direction;
 
15080
              best_key_direction= direction; 
4773
15081
            }
4774
 
          }
4775
 
        }
 
15082
          }   
 
15083
        }      
4776
15084
      }
4777
15085
    }
4778
15086
    if (best_key >= 0)
4779
15087
    {
4780
15088
      bool quick_created= false;
4781
 
      if (table->quick_keys.test(best_key) && best_key != ref_key)
 
15089
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
4782
15090
      {
4783
 
        key_map test_map;
4784
 
        test_map.reset();       // Force the creation of quick select
4785
 
        test_map.set(best_key); // only best_key.
4786
 
        quick_created=
4787
 
          select->test_quick_select(join->session, test_map, 0,
 
15091
        key_map map;
 
15092
        map.clear_all();       // Force the creation of quick select
 
15093
        map.set_bit(best_key); // only best_key.
 
15094
        quick_created=         
 
15095
          select->test_quick_select(join->thd, map, 0,
4788
15096
                                    join->select_options & OPTION_FOUND_ROWS ?
4789
15097
                                    HA_POS_ERROR :
4790
15098
                                    join->unit->select_limit_cnt,
4793
15101
      if (!no_changes)
4794
15102
      {
4795
15103
        if (!quick_created)
4796
 
        {
 
15104
        {
4797
15105
          tab->index= best_key;
4798
15106
          tab->read_first_record= best_key_direction > 0 ?
4799
15107
                                  join_read_first:join_read_last;
4800
 
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
 
15108
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
4801
15109
          if (select && select->quick)
4802
15110
          {
4803
 
            safe_delete(select->quick);
 
15111
            delete select->quick;
 
15112
            select->quick= 0;
4804
15113
          }
4805
 
          if (table->covering_keys.test(best_key))
 
15114
          if (table->covering_keys.is_set(best_key))
4806
15115
          {
4807
15116
            table->key_read=1;
4808
 
            table->cursor->extra(HA_EXTRA_KEYREAD);
 
15117
            table->file->extra(HA_EXTRA_KEYREAD);
4809
15118
          }
4810
 
          table->cursor->ha_index_or_rnd_end();
 
15119
          table->file->ha_index_or_rnd_end();
4811
15120
          if (join->select_options & SELECT_DESCRIBE)
4812
15121
          {
4813
15122
            tab->ref.key= -1;
4814
15123
            tab->ref.key_parts= 0;
4815
 
            if (select_limit < table_records)
 
15124
            if (select_limit < table_records) 
4816
15125
              tab->limit= select_limit;
4817
15126
          }
4818
15127
        }
4819
 
        else if (tab->type != AM_ALL)
 
15128
        else if (tab->type != JT_ALL)
4820
15129
        {
4821
15130
          /*
4822
15131
            We're about to use a quick access to the table.
4824
15133
            method is actually used.
4825
15134
          */
4826
15135
          assert(tab->select->quick);
4827
 
          tab->type= AM_ALL;
 
15136
          tab->type=JT_ALL;
4828
15137
          tab->use_quick=1;
4829
15138
          tab->ref.key= -1;
4830
15139
          tab->ref.key_parts=0;         // Don't use ref key.
4831
15140
          tab->read_first_record= join_init_read_record;
 
15141
          /*
 
15142
            TODO: update the number of records in join->best_positions[tablenr]
 
15143
          */
4832
15144
        }
4833
15145
      }
4834
15146
      used_key_parts= best_key_parts;
4835
15147
      order_direction= best_key_direction;
4836
15148
    }
4837
15149
    else
4838
 
      return(0);
4839
 
  }
 
15150
      return(0); 
 
15151
  } 
4840
15152
 
4841
 
check_reverse_order:
 
15153
check_reverse_order:                  
4842
15154
  if (order_direction == -1)            // If ORDER BY ... DESC
4843
15155
  {
4844
15156
    if (select && select->quick)
4845
15157
    {
4846
15158
      /*
4847
 
        Don't reverse the sort order, if it's already done.
 
15159
        Don't reverse the sort order, if it's already done.
4848
15160
        (In some cases test_if_order_by_key() can be called multiple times
4849
15161
      */
4850
 
      if (! select->quick->reverse_sorted())
 
15162
      if (!select->quick->reverse_sorted())
4851
15163
      {
4852
 
        optimizer::QuickSelectDescending *tmp= NULL;
 
15164
        QUICK_SELECT_DESC *tmp;
4853
15165
        bool error= false;
4854
15166
        int quick_type= select->quick->get_type();
4855
 
        if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
4856
 
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT ||
4857
 
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
4858
 
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX)
 
15167
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
 
15168
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
15169
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
15170
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
4859
15171
        {
4860
15172
          tab->limit= 0;
4861
15173
          select->quick= save_quick;
4862
 
          return 0; // Use filesort
 
15174
          return(0);                   // Use filesort
4863
15175
        }
4864
 
 
 
15176
            
4865
15177
        /* ORDER BY range_key DESC */
4866
 
        tmp= new optimizer::QuickSelectDescending((optimizer::QuickRangeSelect*)(select->quick),
4867
 
                                                  used_key_parts, 
4868
 
                                                  &error);
4869
 
        if (! tmp || error)
4870
 
        {
4871
 
          delete tmp;
 
15178
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
 
15179
                                    used_key_parts, &error);
 
15180
        if (!tmp || error)
 
15181
        {
 
15182
          delete tmp;
4872
15183
          select->quick= save_quick;
4873
15184
          tab->limit= 0;
4874
 
          return 0; // Reverse sort not supported
4875
 
        }
4876
 
        select->quick= tmp;
 
15185
          return(0);            // Reverse sort not supported
 
15186
        }
 
15187
        select->quick=tmp;
4877
15188
      }
4878
15189
    }
4879
 
    else if (tab->type != AM_NEXT &&
 
15190
    else if (tab->type != JT_NEXT && 
4880
15191
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
4881
15192
    {
4882
15193
      /*
4883
 
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
 
15194
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
4884
15195
 
4885
 
        Use a traversal function that starts by reading the last row
4886
 
        with key part (A) and then traverse the index backwards.
 
15196
        Use a traversal function that starts by reading the last row
 
15197
        with key part (A) and then traverse the index backwards.
4887
15198
      */
4888
15199
      tab->read_first_record= join_read_last_key;
4889
15200
      tab->read_record.read_record= join_read_prev_same;
4891
15202
  }
4892
15203
  else if (select && select->quick)
4893
15204
    select->quick->sorted= 1;
4894
 
  return 1;
 
15205
  return(1);
4895
15206
}
4896
15207
 
 
15208
 
4897
15209
/*
4898
15210
  If not selecting by given key, create an index how records should be read
4899
15211
 
4900
15212
  SYNOPSIS
4901
15213
   create_sort_index()
4902
 
     session            Thread Cursor
 
15214
     thd                Thread handler
4903
15215
     tab                Table to sort (in join structure)
4904
15216
     order              How table should be sorted
4905
15217
     filesort_limit     Max number of rows that needs to be sorted
4906
15218
     select_limit       Max number of rows in final output
4907
15219
                        Used to decide if we should use index or not
4908
 
     is_order_by        true if we are sorting on order_st BY, false if GROUP BY
4909
 
                        Used to decide if we should use index or not
 
15220
     is_order_by        true if we are sorting on ORDER BY, false if GROUP BY
 
15221
                        Used to decide if we should use index or not     
4910
15222
 
4911
15223
 
4912
15224
  IMPLEMENTATION
4913
15225
   - If there is an index that can be used, 'tab' is modified to use
4914
15226
     this index.
4915
 
   - If no index, create with filesort() an index cursor that can be used to
 
15227
   - If no index, create with filesort() an index file that can be used to
4916
15228
     retrieve rows in order (should be done with 'read_record').
4917
15229
     The sorted data is stored in tab->table and will be freed when calling
4918
 
     tab->table->free_io_cache().
 
15230
     free_io_cache(tab->table).
4919
15231
 
4920
15232
  RETURN VALUES
4921
15233
    0           ok
4922
15234
    -1          Some fatal error
4923
15235
    1           No records
4924
15236
*/
4925
 
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
15237
 
 
15238
static int
 
15239
create_sort_index(THD *thd, JOIN *join, ORDER *order,
 
15240
                  ha_rows filesort_limit, ha_rows select_limit,
 
15241
                  bool is_order_by)
4926
15242
{
4927
 
  uint32_t length= 0;
 
15243
  uint length= 0;
4928
15244
  ha_rows examined_rows;
4929
 
  Table *table;
4930
 
  optimizer::SqlSelect *select= NULL;
4931
 
  JoinTable *tab;
 
15245
  TABLE *table;
 
15246
  SQL_SELECT *select;
 
15247
  JOIN_TAB *tab;
4932
15248
 
4933
15249
  if (join->tables == join->const_tables)
4934
15250
    return(0);                          // One row, no need to sort
4942
15258
    is going to be used as it is applied now only for one table queries
4943
15259
    with covering indexes.
4944
15260
  */
4945
 
  if ((order != join->group_list ||
 
15261
  if ((order != join->group_list || 
4946
15262
       !(join->select_options & SELECT_BIG_RESULT) ||
4947
 
       (select && select->quick && (select->quick->get_type() == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX))) &&
4948
 
      test_if_skip_sort_order(tab,order,select_limit,0,
 
15263
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
 
15264
      test_if_skip_sort_order(tab,order,select_limit,0, 
4949
15265
                              is_order_by ?  &table->keys_in_use_for_order_by :
4950
15266
                              &table->keys_in_use_for_group_by))
4951
15267
    return(0);
4952
 
  for (Order *ord= join->order; ord; ord= ord->next)
 
15268
  for (ORDER *ord= join->order; ord; ord= ord->next)
4953
15269
    length++;
4954
 
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
4955
 
  {
4956
 
    return(-1);
4957
 
  }
 
15270
  if (!(join->sortorder= 
 
15271
        make_unireg_sortorder(order, &length, join->sortorder)))
 
15272
    goto err;                           /* purecov: inspected */
4958
15273
 
4959
 
  table->sort.io_cache= new internal::IO_CACHE;
 
15274
  table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
 
15275
                                             MYF(MY_WME | MY_ZEROFILL));
4960
15276
  table->status=0;                              // May be wrong if quick_select
4961
15277
 
4962
15278
  // If table has a range, move it to select
4966
15282
    {
4967
15283
      select->quick=tab->quick;
4968
15284
      tab->quick=0;
4969
 
      /*
 
15285
      /* 
4970
15286
        We can only use 'Only index' if quick key is same as ref_key
4971
15287
        and in index_merge 'Only index' cannot be used
4972
15288
      */
4973
 
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
 
15289
      if (table->key_read && ((uint) tab->ref.key != select->quick->index))
4974
15290
      {
4975
 
        table->key_read=0;
4976
 
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
15291
        table->key_read=0;
 
15292
        table->file->extra(HA_EXTRA_NO_KEYREAD);
4977
15293
      }
4978
15294
    }
4979
15295
    else
4980
15296
    {
4981
15297
      /*
4982
 
        We have a ref on a const;  Change this to a range that filesort
4983
 
        can use.
4984
 
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
4985
 
        field, quick will contain an empty record set.
 
15298
        We have a ref on a const;  Change this to a range that filesort
 
15299
        can use.
 
15300
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
15301
        field, quick will contain an empty record set.
4986
15302
      */
4987
 
      if (! (select->quick= (optimizer::get_quick_select_for_ref(session, 
4988
 
                                                                 table, 
4989
 
                                                                 &tab->ref,
4990
 
                                                                 tab->found_records))))
4991
 
      {
4992
 
        return(-1);
4993
 
      }
 
15303
      if (!(select->quick= (get_quick_select_for_ref(thd, table, &tab->ref, 
 
15304
                                                     tab->found_records))))
 
15305
        goto err;
4994
15306
    }
4995
15307
  }
4996
15308
 
4997
 
  if (table->getShare()->getType())
4998
 
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
 
15309
  /* Fill schema tables with data before filesort if it's necessary */
 
15310
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
15311
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
 
15312
    goto err;
4999
15313
 
5000
 
  FileSort filesort(*session);
5001
 
  table->sort.found_records=filesort.run(table,join->sortorder, length,
5002
 
                                         select, filesort_limit, 0,
5003
 
                                         examined_rows);
 
15314
  if (table->s->tmp_table)
 
15315
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
 
15316
  table->sort.found_records=filesort(thd, table,join->sortorder, length,
 
15317
                                     select, filesort_limit, 0,
 
15318
                                     &examined_rows);
5004
15319
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5005
15320
  if (select)
5006
15321
  {
5010
15325
  tab->select_cond=0;
5011
15326
  tab->last_inner= 0;
5012
15327
  tab->first_unmatched= 0;
5013
 
  tab->type= AM_ALL;                            // Read with normal read_record
 
15328
  tab->type=JT_ALL;                             // Read with normal read_record
5014
15329
  tab->read_first_record= join_init_read_record;
5015
15330
  tab->join->examined_rows+=examined_rows;
5016
15331
  if (table->key_read)                          // Restore if we used indexes
5017
15332
  {
5018
15333
    table->key_read=0;
5019
 
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
15334
    table->file->extra(HA_EXTRA_NO_KEYREAD);
5020
15335
  }
5021
 
 
5022
15336
  return(table->sort.found_records == HA_POS_ERROR);
5023
 
}
5024
 
 
5025
 
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
5026
 
{
5027
 
  Cursor *cursor=table->cursor;
 
15337
err:
 
15338
  return(-1);
 
15339
}
 
15340
 
 
15341
/*****************************************************************************
 
15342
  Remove duplicates from tmp table
 
15343
  This should be recoded to add a unique index to the table and remove
 
15344
  duplicates
 
15345
  Table is a locked single thread table
 
15346
  fields is the number of fields to check (from the end)
 
15347
*****************************************************************************/
 
15348
 
 
15349
static bool compare_record(TABLE *table, Field **ptr)
 
15350
{
 
15351
  for (; *ptr ; ptr++)
 
15352
  {
 
15353
    if ((*ptr)->cmp_offset(table->s->rec_buff_length))
 
15354
      return 1;
 
15355
  }
 
15356
  return 0;
 
15357
}
 
15358
 
 
15359
static bool copy_blobs(Field **ptr)
 
15360
{
 
15361
  for (; *ptr ; ptr++)
 
15362
  {
 
15363
    if ((*ptr)->flags & BLOB_FLAG)
 
15364
      if (((Field_blob *) (*ptr))->copy())
 
15365
        return 1;                               // Error
 
15366
  }
 
15367
  return 0;
 
15368
}
 
15369
 
 
15370
static void free_blobs(Field **ptr)
 
15371
{
 
15372
  for (; *ptr ; ptr++)
 
15373
  {
 
15374
    if ((*ptr)->flags & BLOB_FLAG)
 
15375
      ((Field_blob *) (*ptr))->free();
 
15376
  }
 
15377
}
 
15378
 
 
15379
 
 
15380
static int
 
15381
remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having)
 
15382
{
 
15383
  int error;
 
15384
  ulong reclength,offset;
 
15385
  uint field_count;
 
15386
  THD *thd= join->thd;
 
15387
 
 
15388
  entry->reginfo.lock_type=TL_WRITE;
 
15389
 
 
15390
  /* Calculate how many saved fields there is in list */
 
15391
  field_count=0;
 
15392
  List_iterator<Item> it(fields);
 
15393
  Item *item;
 
15394
  while ((item=it++))
 
15395
  {
 
15396
    if (item->get_tmp_table_field() && ! item->const_item())
 
15397
      field_count++;
 
15398
  }
 
15399
 
 
15400
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) 
 
15401
  {                    // only const items with no OPTION_FOUND_ROWS
 
15402
    join->unit->select_limit_cnt= 1;            // Only send first row
 
15403
    return(0);
 
15404
  }
 
15405
  Field **first_field=entry->field+entry->s->fields - field_count;
 
15406
  offset= (field_count ? 
 
15407
           entry->field[entry->s->fields - field_count]->
 
15408
           offset(entry->record[0]) : 0);
 
15409
  reclength=entry->s->reclength-offset;
 
15410
 
 
15411
  free_io_cache(entry);                         // Safety
 
15412
  entry->file->info(HA_STATUS_VARIABLE);
 
15413
  if (entry->s->db_type() == heap_hton ||
 
15414
      (!entry->s->blob_fields &&
 
15415
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
 
15416
        thd->variables.sortbuff_size)))
 
15417
    error=remove_dup_with_hash_index(join->thd, entry,
 
15418
                                     field_count, first_field,
 
15419
                                     reclength, having);
 
15420
  else
 
15421
    error=remove_dup_with_compare(join->thd, entry, first_field, offset,
 
15422
                                  having);
 
15423
 
 
15424
  free_blobs(first_field);
 
15425
  return(error);
 
15426
}
 
15427
 
 
15428
 
 
15429
static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field,
 
15430
                                   ulong offset, Item *having)
 
15431
{
 
15432
  handler *file=table->file;
5028
15433
  char *org_record,*new_record;
5029
 
  unsigned char *record;
 
15434
  uchar *record;
5030
15435
  int error;
5031
 
  uint32_t reclength= table->getShare()->getRecordLength() - offset;
5032
 
 
5033
 
  org_record=(char*) (record=table->getInsertRecord())+offset;
5034
 
  new_record=(char*) table->getUpdateRecord()+offset;
5035
 
 
5036
 
  if ((error= cursor->startTableScan(1)))
5037
 
    goto err;
5038
 
 
5039
 
  error=cursor->rnd_next(record);
 
15436
  ulong reclength= table->s->reclength-offset;
 
15437
 
 
15438
  org_record=(char*) (record=table->record[0])+offset;
 
15439
  new_record=(char*) table->record[1]+offset;
 
15440
 
 
15441
  file->ha_rnd_init(1);
 
15442
  error=file->rnd_next(record);
5040
15443
  for (;;)
5041
15444
  {
5042
 
    if (session->getKilled())
 
15445
    if (thd->killed)
5043
15446
    {
5044
 
      session->send_kill_message();
 
15447
      thd->send_kill_message();
5045
15448
      error=0;
5046
15449
      goto err;
5047
15450
    }
5048
15451
    if (error)
5049
15452
    {
5050
15453
      if (error == HA_ERR_RECORD_DELETED)
5051
 
        continue;
 
15454
        continue;
5052
15455
      if (error == HA_ERR_END_OF_FILE)
5053
 
        break;
 
15456
        break;
5054
15457
      goto err;
5055
15458
    }
5056
15459
    if (having && !having->val_int())
5057
15460
    {
5058
 
      if ((error=cursor->deleteRecord(record)))
5059
 
        goto err;
5060
 
      error=cursor->rnd_next(record);
 
15461
      if ((error=file->ha_delete_row(record)))
 
15462
        goto err;
 
15463
      error=file->rnd_next(record);
5061
15464
      continue;
5062
15465
    }
5063
15466
    if (copy_blobs(first_field))
5068
15471
    }
5069
15472
    memcpy(new_record,org_record,reclength);
5070
15473
 
5071
 
    /* Read through rest of cursor and mark duplicated rows deleted */
 
15474
    /* Read through rest of file and mark duplicated rows deleted */
5072
15475
    bool found=0;
5073
15476
    for (;;)
5074
15477
    {
5075
 
      if ((error=cursor->rnd_next(record)))
 
15478
      if ((error=file->rnd_next(record)))
5076
15479
      {
5077
 
        if (error == HA_ERR_RECORD_DELETED)
5078
 
          continue;
5079
 
        if (error == HA_ERR_END_OF_FILE)
5080
 
          break;
5081
 
        goto err;
 
15480
        if (error == HA_ERR_RECORD_DELETED)
 
15481
          continue;
 
15482
        if (error == HA_ERR_END_OF_FILE)
 
15483
          break;
 
15484
        goto err;
5082
15485
      }
5083
 
      if (table->compare_record(first_field) == 0)
 
15486
      if (compare_record(table, first_field) == 0)
5084
15487
      {
5085
 
        if ((error=cursor->deleteRecord(record)))
5086
 
          goto err;
 
15488
        if ((error=file->ha_delete_row(record)))
 
15489
          goto err;
5087
15490
      }
5088
15491
      else if (!found)
5089
15492
      {
5090
 
        found= 1;
5091
 
        cursor->position(record);       // Remember position
 
15493
        found=1;
 
15494
        file->position(record); // Remember position
5092
15495
      }
5093
15496
    }
5094
15497
    if (!found)
5095
 
      break;                                    // End of cursor
5096
 
    /* Move current position to the next row */
5097
 
    error= cursor->rnd_pos(record, cursor->ref);
 
15498
      break;                                    // End of file
 
15499
    /* Restart search on next row */
 
15500
    error=file->restart_rnd_next(record,file->ref);
5098
15501
  }
5099
15502
 
5100
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
15503
  file->extra(HA_EXTRA_NO_CACHE);
5101
15504
  return(0);
5102
15505
err:
5103
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
15506
  file->extra(HA_EXTRA_NO_CACHE);
5104
15507
  if (error)
5105
 
    table->print_error(error,MYF(0));
 
15508
    file->print_error(error,MYF(0));
5106
15509
  return(1);
5107
15510
}
5108
15511
 
 
15512
 
5109
15513
/**
5110
15514
  Generate a hash index for each row to quickly find duplicate rows.
5111
15515
 
5112
15516
  @note
5113
15517
    Note that this will not work on tables with blobs!
5114
15518
*/
5115
 
int remove_dup_with_hash_index(Session *session, 
5116
 
                               Table *table,
5117
 
                               uint32_t field_count,
5118
 
                               Field **first_field,
5119
 
                               uint32_t key_length,
5120
 
                               Item *having)
 
15519
 
 
15520
static int remove_dup_with_hash_index(THD *thd, TABLE *table,
 
15521
                                      uint field_count,
 
15522
                                      Field **first_field,
 
15523
                                      ulong key_length,
 
15524
                                      Item *having)
5121
15525
{
5122
 
  unsigned char *key_pos, *record=table->getInsertRecord();
 
15526
  uchar *key_buffer, *key_pos, *record=table->record[0];
5123
15527
  int error;
5124
 
  Cursor *cursor= table->cursor;
5125
 
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5126
 
  uint32_t *field_length;
 
15528
  handler *file= table->file;
 
15529
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
 
15530
  uint *field_lengths,*field_length;
5127
15531
  HASH hash;
5128
 
  std::vector<unsigned char> key_buffer;
5129
 
  std::vector<uint32_t> field_lengths;
5130
15532
 
5131
 
  key_buffer.resize((key_length + extra_length) * (long) cursor->stats.records);
5132
 
  field_lengths.resize(field_count);
 
15533
  if (!my_multi_malloc(MYF(MY_WME),
 
15534
                       &key_buffer,
 
15535
                       (uint) ((key_length + extra_length) *
 
15536
                               (long) file->stats.records),
 
15537
                       &field_lengths,
 
15538
                       (uint) (field_count*sizeof(*field_lengths)),
 
15539
                       NullS))
 
15540
    return(1);
5133
15541
 
5134
15542
  {
5135
15543
    Field **ptr;
5136
 
    uint32_t total_length= 0;
5137
 
 
5138
 
    for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
 
15544
    ulong total_length= 0;
 
15545
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
5139
15546
    {
5140
 
      uint32_t length= (*ptr)->sort_length();
 
15547
      uint length= (*ptr)->sort_length();
5141
15548
      (*field_length++)= length;
5142
15549
      total_length+= length;
5143
15550
    }
5146
15553
    extra_length= ALIGN_SIZE(key_length)-key_length;
5147
15554
  }
5148
15555
 
5149
 
  if (hash_init(&hash, &my_charset_bin, (uint32_t) cursor->stats.records, 0,
 
15556
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, 
5150
15557
                key_length, (hash_get_key) 0, 0, 0))
5151
15558
  {
 
15559
    my_free((char*) key_buffer,MYF(0));
5152
15560
    return(1);
5153
15561
  }
5154
15562
 
5155
 
  if ((error= cursor->startTableScan(1)))
5156
 
    goto err;
5157
 
 
5158
 
  key_pos= &key_buffer[0];
 
15563
  file->ha_rnd_init(1);
 
15564
  key_pos=key_buffer;
5159
15565
  for (;;)
5160
15566
  {
5161
 
    unsigned char *org_key_pos;
5162
 
    if (session->getKilled())
 
15567
    uchar *org_key_pos;
 
15568
    if (thd->killed)
5163
15569
    {
5164
 
      session->send_kill_message();
 
15570
      thd->send_kill_message();
5165
15571
      error=0;
5166
15572
      goto err;
5167
15573
    }
5168
 
    if ((error=cursor->rnd_next(record)))
 
15574
    if ((error=file->rnd_next(record)))
5169
15575
    {
5170
15576
      if (error == HA_ERR_RECORD_DELETED)
5171
 
        continue;
 
15577
        continue;
5172
15578
      if (error == HA_ERR_END_OF_FILE)
5173
 
        break;
 
15579
        break;
5174
15580
      goto err;
5175
15581
    }
5176
15582
    if (having && !having->val_int())
5177
15583
    {
5178
 
      if ((error=cursor->deleteRecord(record)))
5179
 
        goto err;
 
15584
      if ((error=file->ha_delete_row(record)))
 
15585
        goto err;
5180
15586
      continue;
5181
15587
    }
5182
15588
 
5183
15589
    /* copy fields to key buffer */
5184
15590
    org_key_pos= key_pos;
5185
 
    field_length= &field_lengths[0];
 
15591
    field_length=field_lengths;
5186
15592
    for (Field **ptr= first_field ; *ptr ; ptr++)
5187
15593
    {
5188
15594
      (*ptr)->sort_string(key_pos,*field_length);
5192
15598
    if (hash_search(&hash, org_key_pos, key_length))
5193
15599
    {
5194
15600
      /* Duplicated found ; Remove the row */
5195
 
      if ((error=cursor->deleteRecord(record)))
5196
 
        goto err;
 
15601
      if ((error=file->ha_delete_row(record)))
 
15602
        goto err;
5197
15603
    }
5198
15604
    else
5199
15605
      (void) my_hash_insert(&hash, org_key_pos);
5200
15606
    key_pos+=extra_length;
5201
15607
  }
 
15608
  my_free((char*) key_buffer,MYF(0));
5202
15609
  hash_free(&hash);
5203
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5204
 
  (void) cursor->endTableScan();
 
15610
  file->extra(HA_EXTRA_NO_CACHE);
 
15611
  (void) file->ha_rnd_end();
5205
15612
  return(0);
5206
15613
 
5207
15614
err:
 
15615
  my_free((char*) key_buffer,MYF(0));
5208
15616
  hash_free(&hash);
5209
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5210
 
  (void) cursor->endTableScan();
 
15617
  file->extra(HA_EXTRA_NO_CACHE);
 
15618
  (void) file->ha_rnd_end();
5211
15619
  if (error)
5212
 
    table->print_error(error,MYF(0));
 
15620
    file->print_error(error,MYF(0));
5213
15621
  return(1);
5214
15622
}
5215
15623
 
5216
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
 
15624
 
 
15625
SORT_FIELD *make_unireg_sortorder(ORDER *order, uint *length,
 
15626
                                  SORT_FIELD *sortorder)
5217
15627
{
5218
 
  uint32_t count;
5219
 
  SortField *sort,*pos;
 
15628
  uint count;
 
15629
  SORT_FIELD *sort,*pos;
5220
15630
 
5221
15631
  count=0;
5222
 
  for (Order *tmp = order; tmp; tmp=tmp->next)
 
15632
  for (ORDER *tmp = order; tmp; tmp=tmp->next)
5223
15633
    count++;
5224
15634
  if (!sortorder)
5225
 
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
 
15635
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
5226
15636
                                       (max(count, *length) + 1));
5227
15637
  pos= sort= sortorder;
5228
15638
 
5249
15659
  return(sort);
5250
15660
}
5251
15661
 
 
15662
 
 
15663
/*****************************************************************************
 
15664
  Fill join cache with packed records
 
15665
  Records are stored in tab->cache.buffer and last record in
 
15666
  last record is stored with pointers to blobs to support very big
 
15667
  records
 
15668
******************************************************************************/
 
15669
 
 
15670
static int
 
15671
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count)
 
15672
{
 
15673
  register unsigned int i;
 
15674
  unsigned int length, blobs;
 
15675
  size_t size;
 
15676
  CACHE_FIELD *copy,**blob_ptr;
 
15677
  JOIN_CACHE  *cache;
 
15678
  JOIN_TAB *join_tab;
 
15679
 
 
15680
  cache= &tables[table_count].cache;
 
15681
  cache->fields=blobs=0;
 
15682
 
 
15683
  join_tab=tables;
 
15684
  for (i=0 ; i < table_count ; i++,join_tab++)
 
15685
  {
 
15686
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
15687
      calc_used_field_length(thd, join_tab);
 
15688
    cache->fields+=join_tab->used_fields;
 
15689
    blobs+=join_tab->used_blobs;
 
15690
 
 
15691
    /* SemiJoinDuplicateElimination: reserve space for rowid */
 
15692
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
15693
    {
 
15694
      cache->fields++;
 
15695
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
 
15696
    }
 
15697
  }
 
15698
  if (!(cache->field=(CACHE_FIELD*)
 
15699
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
 
15700
 
 
15701
                  sizeof(CACHE_FIELD*))))
 
15702
  {
 
15703
    my_free((uchar*) cache->buff,MYF(0));               /* purecov: inspected */
 
15704
    cache->buff=0;                              /* purecov: inspected */
 
15705
    return(1);                          /* purecov: inspected */
 
15706
  }
 
15707
  copy=cache->field;
 
15708
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
 
15709
    (cache->field+cache->fields+table_count*2);
 
15710
 
 
15711
  length=0;
 
15712
  for (i=0 ; i < table_count ; i++)
 
15713
  {
 
15714
    uint null_fields=0,used_fields;
 
15715
    Field **f_ptr,*field;
 
15716
    MY_BITMAP *read_set= tables[i].table->read_set;
 
15717
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
 
15718
         used_fields ;
 
15719
         f_ptr++)
 
15720
    {
 
15721
      field= *f_ptr;
 
15722
      if (bitmap_is_set(read_set, field->field_index))
 
15723
      {
 
15724
        used_fields--;
 
15725
        length+=field->fill_cache_field(copy);
 
15726
        if (copy->blob_field)
 
15727
          (*blob_ptr++)=copy;
 
15728
        if (field->maybe_null())
 
15729
          null_fields++;
 
15730
        copy->get_rowid= NULL;
 
15731
        copy++;
 
15732
      }
 
15733
    }
 
15734
    /* Copy null bits from table */
 
15735
    if (null_fields && tables[i].table->s->null_fields)
 
15736
    {                                           /* must copy null bits */
 
15737
      copy->str= tables[i].table->null_flags;
 
15738
      copy->length= tables[i].table->s->null_bytes;
 
15739
      copy->strip=0;
 
15740
      copy->blob_field=0;
 
15741
      copy->get_rowid= NULL;
 
15742
      length+=copy->length;
 
15743
      copy++;
 
15744
      cache->fields++;
 
15745
    }
 
15746
    /* If outer join table, copy null_row flag */
 
15747
    if (tables[i].table->maybe_null)
 
15748
    {
 
15749
      copy->str= (uchar*) &tables[i].table->null_row;
 
15750
      copy->length=sizeof(tables[i].table->null_row);
 
15751
      copy->strip=0;
 
15752
      copy->blob_field=0;
 
15753
      copy->get_rowid= NULL;
 
15754
      length+=copy->length;
 
15755
      copy++;
 
15756
      cache->fields++;
 
15757
    }
 
15758
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
 
15759
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
15760
    {
 
15761
      copy->str= tables[i].table->file->ref;
 
15762
      copy->length= tables[i].table->file->ref_length;
 
15763
      copy->strip=0;
 
15764
      copy->blob_field=0;
 
15765
      copy->get_rowid= NULL;
 
15766
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
15767
      {
 
15768
        /* We will need to call h->position(): */
 
15769
        copy->get_rowid= tables[i].table;
 
15770
        /* And those after us won't have to: */
 
15771
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
 
15772
      }
 
15773
      copy++;
 
15774
    }
 
15775
  }
 
15776
 
 
15777
  cache->length=length+blobs*sizeof(char*);
 
15778
  cache->blobs=blobs;
 
15779
  *blob_ptr=0;                                  /* End sequentel */
 
15780
  size=max(thd->variables.join_buff_size, cache->length);
 
15781
  if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
 
15782
    return(1);                          /* Don't use cache */ /* purecov: inspected */
 
15783
  cache->end=cache->buff+size;
 
15784
  reset_cache_write(cache);
 
15785
  return(0);
 
15786
}
 
15787
 
 
15788
 
 
15789
static ulong
 
15790
used_blob_length(CACHE_FIELD **ptr)
 
15791
{
 
15792
  uint length,blob_length;
 
15793
  for (length=0 ; *ptr ; ptr++)
 
15794
  {
 
15795
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
 
15796
    length+=blob_length;
 
15797
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
 
15798
  }
 
15799
  return length;
 
15800
}
 
15801
 
 
15802
 
 
15803
static bool
 
15804
store_record_in_cache(JOIN_CACHE *cache)
 
15805
{
 
15806
  uint length;
 
15807
  uchar *pos;
 
15808
  CACHE_FIELD *copy,*end_field;
 
15809
  bool last_record;
 
15810
 
 
15811
  pos=cache->pos;
 
15812
  end_field=cache->field+cache->fields;
 
15813
 
 
15814
  length=cache->length;
 
15815
  if (cache->blobs)
 
15816
    length+=used_blob_length(cache->blob_ptr);
 
15817
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
15818
    cache->ptr_record=cache->records;
 
15819
  /*
 
15820
    There is room in cache. Put record there
 
15821
  */
 
15822
  cache->records++;
 
15823
  for (copy=cache->field ; copy < end_field; copy++)
 
15824
  {
 
15825
    if (copy->blob_field)
 
15826
    {
 
15827
      if (last_record)
 
15828
      {
 
15829
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), 
 
15830
                                    copy->blob_field->charset());
 
15831
        pos+=copy->length+sizeof(char*);
 
15832
      }
 
15833
      else
 
15834
      {
 
15835
        copy->blob_field->get_image(pos, copy->length, // blob length
 
15836
                                    copy->blob_field->charset());
 
15837
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
15838
        pos+=copy->length+copy->blob_length;
 
15839
      }
 
15840
    }
 
15841
    else
 
15842
    {
 
15843
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
 
15844
      if (copy->get_rowid)
 
15845
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
 
15846
 
 
15847
      if (copy->strip)
 
15848
      {
 
15849
        uchar *str,*end;
 
15850
        for (str=copy->str,end= str+copy->length;
 
15851
             end > str && end[-1] == ' ' ;
 
15852
             end--) ;
 
15853
        length=(uint) (end-str);
 
15854
        memcpy(pos+2, str, length);
 
15855
        int2store(pos, length);
 
15856
        pos+= length+2;
 
15857
      }
 
15858
      else
 
15859
      {
 
15860
        memcpy(pos,copy->str,copy->length);
 
15861
        pos+=copy->length;
 
15862
      }
 
15863
    }
 
15864
  }
 
15865
  cache->pos=pos;
 
15866
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
15867
}
 
15868
 
 
15869
 
 
15870
static void
 
15871
reset_cache_read(JOIN_CACHE *cache)
 
15872
{
 
15873
  cache->record_nr=0;
 
15874
  cache->pos=cache->buff;
 
15875
}
 
15876
 
 
15877
 
 
15878
static void reset_cache_write(JOIN_CACHE *cache)
 
15879
{
 
15880
  reset_cache_read(cache);
 
15881
  cache->records= 0;
 
15882
  cache->ptr_record= (uint) ~0;
 
15883
}
 
15884
 
 
15885
 
 
15886
static void
 
15887
read_cached_record(JOIN_TAB *tab)
 
15888
{
 
15889
  uchar *pos;
 
15890
  uint length;
 
15891
  bool last_record;
 
15892
  CACHE_FIELD *copy,*end_field;
 
15893
 
 
15894
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
 
15895
  pos=tab->cache.pos;
 
15896
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
 
15897
       copy < end_field;
 
15898
       copy++)
 
15899
  {
 
15900
    if (copy->blob_field)
 
15901
    {
 
15902
      if (last_record)
 
15903
      {
 
15904
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
 
15905
                                    copy->blob_field->charset());
 
15906
        pos+=copy->length+sizeof(char*);
 
15907
      }
 
15908
      else
 
15909
      {
 
15910
        copy->blob_field->set_ptr(pos, pos+copy->length);
 
15911
        pos+=copy->length+copy->blob_field->get_length();
 
15912
      }
 
15913
    }
 
15914
    else
 
15915
    {
 
15916
      if (copy->strip)
 
15917
      {
 
15918
        length= uint2korr(pos);
 
15919
        memcpy(copy->str, pos+2, length);
 
15920
        memset(copy->str+length, ' ', copy->length-length);
 
15921
        pos+= 2 + length;
 
15922
      }
 
15923
      else
 
15924
      {
 
15925
        memcpy(copy->str,pos,copy->length);
 
15926
        pos+=copy->length;
 
15927
      }
 
15928
    }
 
15929
  }
 
15930
  tab->cache.pos=pos;
 
15931
  return;
 
15932
}
 
15933
 
 
15934
 
5252
15935
/*
5253
15936
  eq_ref: Create the lookup key and check if it is the same as saved key
5254
15937
 
5255
15938
  SYNOPSIS
5256
15939
    cmp_buffer_with_ref()
5257
15940
      tab  Join tab of the accessed table
5258
 
 
5259
 
  DESCRIPTION
5260
 
    Used by eq_ref access method: create the index lookup key and check if
 
15941
 
 
15942
  DESCRIPTION 
 
15943
    Used by eq_ref access method: create the index lookup key and check if 
5261
15944
    we've used this key at previous lookup (If yes, we don't need to repeat
5262
15945
    the lookup - the record has been already fetched)
5263
15946
 
5264
 
  RETURN
 
15947
  RETURN 
5265
15948
    true   No cached record for the key, or failed to create the key (due to
5266
15949
           out-of-domain error)
5267
 
    false  The created key is the same as the previous one (and the record
 
15950
    false  The created key is the same as the previous one (and the record 
5268
15951
           is already in table->record)
5269
15952
*/
5270
 
static bool cmp_buffer_with_ref(JoinTable *tab)
 
15953
 
 
15954
static bool
 
15955
cmp_buffer_with_ref(JOIN_TAB *tab)
5271
15956
{
5272
15957
  bool no_prev_key;
5273
15958
  if (!tab->ref.disable_cache)
5278
15963
      memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
5279
15964
    }
5280
15965
  }
5281
 
  else
 
15966
  else 
5282
15967
    no_prev_key= true;
5283
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) ||
 
15968
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, tab->table,
 
15969
                                            &tab->ref)) ||
5284
15970
      no_prev_key)
5285
15971
    return 1;
5286
15972
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
5287
15973
    != 0;
5288
15974
}
5289
15975
 
5290
 
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
 
15976
 
 
15977
bool
 
15978
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
5291
15979
{
5292
 
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
5293
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
15980
  enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
 
15981
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
15982
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
5294
15983
  bool result= 0;
5295
15984
 
5296
 
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
 
15985
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
5297
15986
  {
5298
15987
    if ((*copy)->copy() & 1)
5299
15988
    {
5301
15990
      break;
5302
15991
    }
5303
15992
  }
5304
 
  session->count_cuted_fields= save_count_cuted_fields;
 
15993
  thd->count_cuted_fields= save_count_cuted_fields;
 
15994
  dbug_tmp_restore_column_map(table->write_set, old_map);
5305
15995
  return result;
5306
15996
}
5307
15997
 
 
15998
 
5308
15999
/*****************************************************************************
5309
16000
  Group and order functions
5310
16001
*****************************************************************************/
5312
16003
/**
5313
16004
  Resolve an ORDER BY or GROUP BY column reference.
5314
16005
 
5315
 
  Given a column reference (represented by 'order') from a GROUP BY or order_st
 
16006
  Given a column reference (represented by 'order') from a GROUP BY or ORDER
5316
16007
  BY clause, find the actual column it represents. If the column being
5317
16008
  resolved is from the GROUP BY clause, the procedure searches the SELECT
5318
16009
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
5325
16016
 
5326
16017
  ref_pointer_array and all_fields are updated.
5327
16018
 
5328
 
  @param[in] session                 Pointer to current thread structure
 
16019
  @param[in] thd                     Pointer to current thread structure
5329
16020
  @param[in,out] ref_pointer_array  All select, group and order by fields
5330
16021
  @param[in] tables                 List of tables to search in (usually
5331
16022
    FROM clause)
5334
16025
    SELECT list)
5335
16026
  @param[in,out] all_fields         All select, group and order by fields
5336
16027
  @param[in] is_group_field         True if order is a GROUP field, false if
5337
 
    order_st by field
 
16028
    ORDER by field
5338
16029
 
5339
16030
  @retval
5340
16031
    false if OK
5341
16032
  @retval
5342
16033
    true  if error occurred
5343
16034
*/
5344
 
static bool find_order_in_list(Session *session, 
5345
 
                               Item **ref_pointer_array, 
5346
 
                               TableList *tables,
5347
 
                               Order *order,
5348
 
                               List<Item> &fields,
5349
 
                               List<Item> &all_fields,
5350
 
                               bool is_group_field)
 
16035
 
 
16036
static bool
 
16037
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16038
                   ORDER *order, List<Item> &fields, List<Item> &all_fields,
 
16039
                   bool is_group_field)
5351
16040
{
5352
 
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
 
16041
  Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */
5353
16042
  Item::Type order_item_type;
5354
16043
  Item **select_item; /* The corresponding item from the SELECT clause. */
5355
16044
  Field *from_field;  /* The corresponding field from the FROM clause. */
5356
 
  uint32_t counter;
 
16045
  uint counter;
5357
16046
  enum_resolution_type resolution;
5358
16047
 
5359
16048
  /*
5362
16051
  */
5363
16052
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
5364
16053
  {                                             /* Order by position */
5365
 
    uint32_t count= (uint32_t) order_item->val_int();
 
16054
    uint count= (uint) order_item->val_int();
5366
16055
    if (!count || count > fields.elements)
5367
16056
    {
5368
16057
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
5369
 
               order_item->full_name(), session->where());
 
16058
               order_item->full_name(), thd->where);
5370
16059
      return true;
5371
16060
    }
5372
16061
    order->item= ref_pointer_array + count - 1;
5375
16064
    order->counter_used= 1;
5376
16065
    return false;
5377
16066
  }
5378
 
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5379
 
  select_item= find_item_in_list(session, order_item, fields, &counter,
 
16067
  /* Lookup the current GROUP/ORDER field in the SELECT clause. */
 
16068
  select_item= find_item_in_list(order_item, fields, &counter,
5380
16069
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
5381
16070
  if (!select_item)
5382
16071
    return true; /* The item is not unique, or some other error occured. */
5392
16081
      for this name (in case if we would perform lookup in all tables).
5393
16082
    */
5394
16083
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
5395
 
        order_item->fix_fields(session, order->item))
 
16084
        order_item->fix_fields(thd, order->item))
5396
16085
      return true;
5397
16086
 
5398
16087
    /* Lookup the current GROUP field in the FROM clause. */
5401
16090
    if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
5402
16091
        order_item_type == Item::REF_ITEM)
5403
16092
    {
5404
 
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
5405
 
                                       NULL, &view_ref, IGNORE_ERRORS, false);
 
16093
      from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables,
 
16094
                                       NULL, &view_ref, IGNORE_ERRORS, true,
 
16095
                                       false);
5406
16096
      if (!from_field)
5407
16097
        from_field= (Field*) not_found_field;
5408
16098
    }
5440
16130
        warning so the user knows that the field from the FROM clause
5441
16131
        overshadows the column reference from the SELECT list.
5442
16132
      */
5443
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
16133
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5444
16134
                          ER(ER_NON_UNIQ_ERROR),
5445
16135
                          ((Item_ident*) order_item)->field_name,
5446
 
                          session->where());
 
16136
                          current_thd->where);
5447
16137
    }
5448
16138
  }
5449
16139
 
5460
16150
    arguments for which fix_fields already was called.
5461
16151
  */
5462
16152
  if (!order_item->fixed &&
5463
 
      (order_item->fix_fields(session, order->item) ||
 
16153
      (order_item->fix_fields(thd, order->item) ||
5464
16154
       (order_item= *order->item)->check_cols(1) ||
5465
 
       session->is_fatal_error))
 
16155
       thd->is_fatal_error))
5466
16156
    return true; /* Wrong field. */
5467
16157
 
5468
 
  uint32_t el= all_fields.elements;
 
16158
  uint el= all_fields.elements;
5469
16159
  all_fields.push_front(order_item); /* Add new field to field list. */
5470
16160
  ref_pointer_array[el]= order_item;
5471
16161
  order->item= ref_pointer_array + el;
5472
16162
  return false;
5473
16163
}
5474
16164
 
 
16165
 
5475
16166
/**
5476
16167
  Change order to point at item in select list.
5477
16168
 
5478
16169
  If item isn't a number and doesn't exits in the select list, add it the
5479
16170
  the field list.
5480
16171
*/
5481
 
int setup_order(Session *session,
5482
 
                Item **ref_pointer_array,
5483
 
                TableList *tables,
5484
 
                            List<Item> &fields,
5485
 
                List<Item> &all_fields,
5486
 
                Order *order)
 
16172
 
 
16173
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16174
                List<Item> &fields, List<Item> &all_fields, ORDER *order)
5487
16175
{
5488
 
  session->setWhere("order clause");
 
16176
  thd->where="order clause";
5489
16177
  for (; order; order=order->next)
5490
16178
  {
5491
 
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
 
16179
    if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
5492
16180
                           all_fields, false))
5493
16181
      return 1;
5494
16182
  }
5495
16183
  return 0;
5496
16184
}
5497
16185
 
 
16186
 
5498
16187
/**
5499
16188
  Intitialize the GROUP BY list.
5500
16189
 
5501
 
  @param session                        Thread Cursor
 
16190
  @param thd                    Thread handler
5502
16191
  @param ref_pointer_array      We store references to all fields that was
5503
16192
                               not in 'fields' here.
5504
16193
  @param fields         All fields in the select part. Any item in
5520
16209
  @retval
5521
16210
    1  error (probably out of memory)
5522
16211
*/
5523
 
int setup_group(Session *session,
5524
 
                Item **ref_pointer_array,
5525
 
                TableList *tables,
5526
 
                      List<Item> &fields,
5527
 
                List<Item> &all_fields,
5528
 
                Order *order,
5529
 
                      bool *hidden_group_fields)
 
16212
 
 
16213
int
 
16214
setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16215
            List<Item> &fields, List<Item> &all_fields, ORDER *order,
 
16216
            bool *hidden_group_fields)
5530
16217
{
5531
16218
  *hidden_group_fields=0;
5532
 
  Order *ord;
 
16219
  ORDER *ord;
5533
16220
 
5534
16221
  if (!order)
5535
16222
    return 0;                           /* Everything is ok */
5536
16223
 
5537
 
  uint32_t org_fields=all_fields.elements;
 
16224
  uint org_fields=all_fields.elements;
5538
16225
 
5539
 
  session->setWhere("group statement");
 
16226
  thd->where="group statement";
5540
16227
  for (ord= order; ord; ord= ord->next)
5541
16228
  {
5542
 
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
 
16229
    if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields,
5543
16230
                           all_fields, true))
5544
16231
      return 1;
5545
16232
    (*ord->item)->marker= UNDEF_POS;            /* Mark found */
5569
16256
    Item *item;
5570
16257
    Item_field *field;
5571
16258
    int cur_pos_in_select_list= 0;
5572
 
    List<Item>::iterator li(fields.begin());
5573
 
    List<Item_field>::iterator naf_it(session->getLex()->current_select->non_agg_fields.begin());
 
16259
    List_iterator<Item> li(fields);
 
16260
    List_iterator<Item_field> naf_it(thd->lex->current_select->non_agg_fields);
5574
16261
 
5575
16262
    field= naf_it++;
5576
16263
    while (field && (item=li++))
5596
16283
            if ((*ord->item)->eq((Item*)field, 0))
5597
16284
              goto next_field;
5598
16285
          /*
5599
 
            @todo change ER_WRONG_FIELD_WITH_GROUP to more detailed ER_NON_GROUPING_FIELD_USED
 
16286
            TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed
 
16287
            ER_NON_GROUPING_FIELD_USED
5600
16288
          */
5601
16289
          my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
5602
16290
          return 1;
5618
16306
  Try to use the fields in the order given by 'order' to allow one to
5619
16307
  optimize away 'order by'.
5620
16308
*/
5621
 
Order *create_distinct_group(Session *session,
5622
 
                                Item **ref_pointer_array,
5623
 
                                Order *order_list,
5624
 
                                List<Item> &fields,
5625
 
                                List<Item> &,
5626
 
                                bool *all_order_by_fields_used)
 
16309
 
 
16310
static ORDER *
 
16311
create_distinct_group(THD *thd, Item **ref_pointer_array,
 
16312
                      ORDER *order_list, List<Item> &fields,
 
16313
                      List<Item> &all_fields __attribute__((__unused__)),
 
16314
                      bool *all_order_by_fields_used)
5627
16315
{
5628
 
  List<Item>::iterator li(fields.begin());
 
16316
  List_iterator<Item> li(fields);
5629
16317
  Item *item;
5630
 
  Order *order,*group,**prev;
 
16318
  ORDER *order,*group,**prev;
5631
16319
 
5632
16320
  *all_order_by_fields_used= 1;
5633
16321
  while ((item=li++))
5638
16326
  {
5639
16327
    if (order->in_field_list)
5640
16328
    {
5641
 
      Order *ord=(Order*) session->getMemRoot()->duplicate((char*) order,sizeof(Order));
 
16329
      ORDER *ord=(ORDER*) thd->memdup((char*) order,sizeof(ORDER));
5642
16330
      if (!ord)
5643
 
        return 0;
 
16331
        return 0;
5644
16332
      *prev=ord;
5645
16333
      prev= &ord->next;
5646
16334
      (*ord->item)->marker=1;
5649
16337
      *all_order_by_fields_used= 0;
5650
16338
  }
5651
16339
 
5652
 
  li= fields.begin();
 
16340
  li.rewind();
5653
16341
  while ((item=li++))
5654
16342
  {
5655
16343
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5656
16344
    {
5657
 
      /*
5658
 
        Don't put duplicate columns from the SELECT list into the
 
16345
      /* 
 
16346
        Don't put duplicate columns from the SELECT list into the 
5659
16347
        GROUP BY list.
5660
16348
      */
5661
 
      Order *ord_iter;
 
16349
      ORDER *ord_iter;
5662
16350
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5663
16351
        if ((*ord_iter->item)->eq(item, 1))
5664
16352
          goto next_item;
5665
 
 
5666
 
      Order *ord=(Order*) session->calloc(sizeof(Order));
 
16353
      
 
16354
      ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER));
5667
16355
      if (!ord)
5668
 
        return 0;
 
16356
        return 0;
5669
16357
 
5670
16358
      /*
5671
16359
        We have here only field_list (not all_field_list), so we can use
5684
16372
  return group;
5685
16373
}
5686
16374
 
 
16375
 
5687
16376
/**
5688
16377
  Update join with count of the different type of fields.
5689
16378
*/
5690
 
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
 
16379
 
 
16380
void
 
16381
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, 
 
16382
                  List<Item> &fields, bool reset_with_sum_func)
5691
16383
{
5692
 
  List<Item>::iterator li(fields.begin());
 
16384
  List_iterator<Item> li(fields);
5693
16385
  Item *field;
5694
16386
 
5695
16387
  param->field_count=param->sum_func_count=param->func_count=
5704
16396
    {
5705
16397
      if (! field->const_item())
5706
16398
      {
5707
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
16399
        Item_sum *sum_item=(Item_sum*) field->real_item();
5708
16400
        if (!sum_item->depended_from() ||
5709
16401
            sum_item->depended_from() == select_lex)
5710
16402
        {
5712
16404
            param->quick_group=0;                       // UDF SUM function
5713
16405
          param->sum_func_count++;
5714
16406
 
5715
 
          for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
 
16407
          for (uint i=0 ; i < sum_item->arg_count ; i++)
5716
16408
          {
5717
16409
            if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
5718
16410
              param->field_count++;
5727
16419
    {
5728
16420
      param->func_count++;
5729
16421
      if (reset_with_sum_func)
5730
 
        field->with_sum_func=0;
5731
 
    }
5732
 
  }
5733
 
}
 
16422
        field->with_sum_func=0;
 
16423
    }
 
16424
  }
 
16425
}
 
16426
 
 
16427
 
 
16428
/**
 
16429
  Return 1 if second is a subpart of first argument.
 
16430
 
 
16431
  If first parts has different direction, change it to second part
 
16432
  (group is sorted like order)
 
16433
*/
 
16434
 
 
16435
static bool
 
16436
test_if_subpart(ORDER *a,ORDER *b)
 
16437
{
 
16438
  for (; a && b; a=a->next,b=b->next)
 
16439
  {
 
16440
    if ((*a->item)->eq(*b->item,1))
 
16441
      a->asc=b->asc;
 
16442
    else
 
16443
      return 0;
 
16444
  }
 
16445
  return test(!b);
 
16446
}
 
16447
 
 
16448
/**
 
16449
  Return table number if there is only one table in sort order
 
16450
  and group and order is compatible, else return 0.
 
16451
*/
 
16452
 
 
16453
static TABLE *
 
16454
get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables)
 
16455
{
 
16456
  table_map map= (table_map) 0;
 
16457
 
 
16458
  if (!a)
 
16459
    a=b;                                        // Only one need to be given
 
16460
  else if (!b)
 
16461
    b=a;
 
16462
 
 
16463
  for (; a && b; a=a->next,b=b->next)
 
16464
  {
 
16465
    if (!(*a->item)->eq(*b->item,1))
 
16466
      return(0);
 
16467
    map|=a->item[0]->used_tables();
 
16468
  }
 
16469
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
 
16470
    return(0);
 
16471
 
 
16472
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
 
16473
  if (map != tables->table->map)
 
16474
    return(0);                          // More than one table
 
16475
  return(tables->table);
 
16476
}
 
16477
 
 
16478
 
 
16479
/**
 
16480
  calc how big buffer we need for comparing group entries.
 
16481
*/
 
16482
 
 
16483
static void
 
16484
calc_group_buffer(JOIN *join,ORDER *group)
 
16485
{
 
16486
  uint key_length=0, parts=0, null_parts=0;
 
16487
 
 
16488
  if (group)
 
16489
    join->group= 1;
 
16490
  for (; group ; group=group->next)
 
16491
  {
 
16492
    Item *group_item= *group->item;
 
16493
    Field *field= group_item->get_tmp_table_field();
 
16494
    if (field)
 
16495
    {
 
16496
      enum_field_types type;
 
16497
      if ((type= field->type()) == MYSQL_TYPE_BLOB)
 
16498
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
 
16499
      else if (type == MYSQL_TYPE_VARCHAR || type == MYSQL_TYPE_VAR_STRING)
 
16500
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
 
16501
      else
 
16502
        key_length+= field->pack_length();
 
16503
    }
 
16504
    else
 
16505
    { 
 
16506
      switch (group_item->result_type()) {
 
16507
      case REAL_RESULT:
 
16508
        key_length+= sizeof(double);
 
16509
        break;
 
16510
      case INT_RESULT:
 
16511
        key_length+= sizeof(int64_t);
 
16512
        break;
 
16513
      case DECIMAL_RESULT:
 
16514
        key_length+= my_decimal_get_binary_size(group_item->max_length - 
 
16515
                                                (group_item->decimals ? 1 : 0),
 
16516
                                                group_item->decimals);
 
16517
        break;
 
16518
      case STRING_RESULT:
 
16519
      {
 
16520
        enum enum_field_types type= group_item->field_type();
 
16521
        /*
 
16522
          As items represented as DATE/TIME fields in the group buffer
 
16523
          have STRING_RESULT result type, we increase the length 
 
16524
          by 8 as maximum pack length of such fields.
 
16525
        */
 
16526
        if (type == MYSQL_TYPE_TIME ||
 
16527
            type == MYSQL_TYPE_NEWDATE ||
 
16528
            type == MYSQL_TYPE_DATETIME ||
 
16529
            type == MYSQL_TYPE_TIMESTAMP)
 
16530
        {
 
16531
          key_length+= 8;
 
16532
        }
 
16533
        else
 
16534
        {
 
16535
          /*
 
16536
            Group strings are taken as varstrings and require an length field.
 
16537
            A field is not yet created by create_tmp_field()
 
16538
            and the sizes should match up.
 
16539
          */
 
16540
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
 
16541
        }
 
16542
        break;
 
16543
      }
 
16544
      default:
 
16545
        /* This case should never be choosen */
 
16546
        assert(0);
 
16547
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
16548
      }
 
16549
    }
 
16550
    parts++;
 
16551
    if (group_item->maybe_null)
 
16552
      null_parts++;
 
16553
  }
 
16554
  join->tmp_table_param.group_length=key_length+null_parts;
 
16555
  join->tmp_table_param.group_parts=parts;
 
16556
  join->tmp_table_param.group_null_parts=null_parts;
 
16557
}
 
16558
 
 
16559
 
 
16560
/**
 
16561
  allocate group fields or take prepared (cached).
 
16562
 
 
16563
  @param main_join   join of current select
 
16564
  @param curr_join   current join (join of current select or temporary copy
 
16565
                     of it)
 
16566
 
 
16567
  @retval
 
16568
    0   ok
 
16569
  @retval
 
16570
    1   failed
 
16571
*/
 
16572
 
 
16573
static bool
 
16574
make_group_fields(JOIN *main_join, JOIN *curr_join)
 
16575
{
 
16576
  if (main_join->group_fields_cache.elements)
 
16577
  {
 
16578
    curr_join->group_fields= main_join->group_fields_cache;
 
16579
    curr_join->sort_and_group= 1;
 
16580
  }
 
16581
  else
 
16582
  {
 
16583
    if (alloc_group_fields(curr_join, curr_join->group_list))
 
16584
      return (1);
 
16585
    main_join->group_fields_cache= curr_join->group_fields;
 
16586
  }
 
16587
  return (0);
 
16588
}
 
16589
 
 
16590
 
 
16591
/**
 
16592
  Get a list of buffers for saveing last group.
 
16593
 
 
16594
  Groups are saved in reverse order for easyer check loop.
 
16595
*/
 
16596
 
 
16597
static bool
 
16598
alloc_group_fields(JOIN *join,ORDER *group)
 
16599
{
 
16600
  if (group)
 
16601
  {
 
16602
    for (; group ; group=group->next)
 
16603
    {
 
16604
      Cached_item *tmp=new_Cached_item(join->thd, *group->item, false);
 
16605
      if (!tmp || join->group_fields.push_front(tmp))
 
16606
        return true;
 
16607
    }
 
16608
  }
 
16609
  join->sort_and_group=1;                       /* Mark for do_select */
 
16610
  return false;
 
16611
}
 
16612
 
5734
16613
 
5735
16614
/*
5736
16615
  Test if a single-row cache of items changed, and update the cache.
5738
16617
  @details Test if a list of items that typically represents a result
5739
16618
  row has changed. If the value of some item changed, update the cached
5740
16619
  value for this item.
5741
 
 
 
16620
  
5742
16621
  @param list list of <item, cached_value> pairs stored as Cached_item.
5743
16622
 
5744
16623
  @return -1 if no item changed
5745
16624
  @return index of the first item that changed
5746
16625
*/
 
16626
 
5747
16627
int test_if_item_cache_changed(List<Cached_item> &list)
5748
16628
{
5749
 
  List<Cached_item>::iterator li(list.begin());
 
16629
  List_iterator<Cached_item> li(list);
5750
16630
  int idx= -1,i;
5751
16631
  Cached_item *buff;
5752
16632
 
5758
16638
  return(idx);
5759
16639
}
5760
16640
 
 
16641
 
5761
16642
/**
5762
16643
  Setup copy_fields to save fields at start of new group.
5763
16644
 
5767
16648
  Change old item_field to use a new field with points at saved fieldvalue
5768
16649
  This function is only called before use of send_fields.
5769
16650
 
5770
 
  @param session                   Session pointer
 
16651
  @param thd                   THD pointer
5771
16652
  @param param                 temporary table parameters
5772
16653
  @param ref_pointer_array     array of pointers to top elements of filed list
5773
16654
  @param res_selected_fields   new list of items of select item list
5786
16667
  @retval
5787
16668
    !=0   error
5788
16669
*/
5789
 
bool setup_copy_fields(Session *session,
5790
 
                       Tmp_Table_Param *param,
5791
 
                       Item **ref_pointer_array,
5792
 
                       List<Item> &res_selected_fields,
5793
 
                       List<Item> &res_all_fields,
5794
 
                       uint32_t elements,
5795
 
                       List<Item> &all_fields)
 
16670
 
 
16671
bool
 
16672
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
 
16673
                  Item **ref_pointer_array,
 
16674
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
 
16675
                  uint elements, List<Item> &all_fields)
5796
16676
{
5797
16677
  Item *pos;
5798
 
  List<Item>::iterator li(all_fields.begin());
5799
 
  CopyField *copy= NULL;
5800
 
  res_selected_fields.clear();
5801
 
  res_all_fields.clear();
5802
 
  List<Item>::iterator itr(res_all_fields.begin());
 
16678
  List_iterator_fast<Item> li(all_fields);
 
16679
  Copy_field *copy= NULL;
 
16680
  res_selected_fields.empty();
 
16681
  res_all_fields.empty();
 
16682
  List_iterator_fast<Item> itr(res_all_fields);
5803
16683
  List<Item> extra_funcs;
5804
 
  uint32_t i, border= all_fields.elements - elements;
 
16684
  uint i, border= all_fields.elements - elements;
5805
16685
 
5806
 
  if (param->field_count &&
5807
 
      !(copy=param->copy_field= new CopyField[param->field_count]))
 
16686
  if (param->field_count && 
 
16687
      !(copy=param->copy_field= new Copy_field[param->field_count]))
5808
16688
    goto err2;
5809
16689
 
5810
 
  param->copy_funcs.clear();
 
16690
  param->copy_funcs.empty();
5811
16691
  for (i= 0; (pos= li++); i++)
5812
16692
  {
5813
16693
    Field *field;
5814
 
    unsigned char *tmp;
 
16694
    uchar *tmp;
5815
16695
    Item *real_pos= pos->real_item();
5816
16696
    if (real_pos->type() == Item::FIELD_ITEM)
5817
16697
    {
5818
16698
      Item_field *item;
5819
 
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
5820
 
        goto err;
 
16699
      if (!(item= new Item_field(thd, ((Item_field*) real_pos))))
 
16700
        goto err;
5821
16701
      if (pos->type() == Item::REF_ITEM)
5822
16702
      {
5823
16703
        /* preserve the names of the ref when dereferncing */
5829
16709
      pos= item;
5830
16710
      if (item->field->flags & BLOB_FLAG)
5831
16711
      {
5832
 
        if (!(pos= new Item_copy_string(pos)))
5833
 
          goto err;
5834
 
            /*
5835
 
              Item_copy_string::copy for function can call
5836
 
              Item_copy_string::val_int for blob via Item_ref.
5837
 
              But if Item_copy_string::copy for blob isn't called before,
5838
 
              it's value will be wrong
5839
 
              so let's insert Item_copy_string for blobs in the beginning of
5840
 
              copy_funcs
5841
 
              (to see full test case look at having.test, BUG #4358)
5842
 
            */
5843
 
        if (param->copy_funcs.push_front(pos))
5844
 
          goto err;
 
16712
        if (!(pos= new Item_copy_string(pos)))
 
16713
          goto err;
 
16714
       /*
 
16715
         Item_copy_string::copy for function can call 
 
16716
         Item_copy_string::val_int for blob via Item_ref.
 
16717
         But if Item_copy_string::copy for blob isn't called before,
 
16718
         it's value will be wrong
 
16719
         so let's insert Item_copy_string for blobs in the beginning of 
 
16720
         copy_funcs
 
16721
         (to see full test case look at having.test, BUG #4358) 
 
16722
       */
 
16723
        if (param->copy_funcs.push_front(pos))
 
16724
          goto err;
5845
16725
      }
5846
16726
      else
5847
16727
      {
 
16728
        /* 
 
16729
           set up save buffer and change result_field to point at 
 
16730
           saved value
 
16731
        */
 
16732
        field= item->field;
 
16733
        item->result_field=field->new_field(thd->mem_root,field->table, 1);
5848
16734
        /*
5849
 
          set up save buffer and change result_field to point at
5850
 
          saved value
 
16735
          We need to allocate one extra byte for null handling and
 
16736
          another extra byte to not get warnings from purify in
 
16737
          Field_string::val_int
5851
16738
        */
5852
 
        field= item->field;
5853
 
        item->result_field=field->new_field(session->mem_root,field->getTable(), 1);
5854
 
              /*
5855
 
                We need to allocate one extra byte for null handling and
5856
 
                another extra byte to not get warnings from purify in
5857
 
                Field_varstring::val_int
5858
 
              */
5859
 
        if (!(tmp= (unsigned char*) memory::sql_alloc(field->pack_length()+2)))
5860
 
          goto err;
 
16739
        if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
 
16740
          goto err;
5861
16741
        if (copy)
5862
16742
        {
5863
16743
          copy->set(tmp, item->result_field);
5864
16744
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
5865
 
#ifdef HAVE_VALGRIND
 
16745
#ifdef HAVE_purify
5866
16746
          copy->to_ptr[copy->from_length]= 0;
5867
16747
#endif
5868
16748
          copy++;
5876
16756
             !real_pos->with_sum_func)
5877
16757
    {                                           // Save for send fields
5878
16758
      pos= real_pos;
5879
 
      /* 
5880
 
        @todo In most cases this result will be sent to the user.
5881
 
        This should be changed to use copy_int or copy_real depending
5882
 
        on how the value is to be used: In some cases this may be an
5883
 
        argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
 
16759
      /* TODO:
 
16760
         In most cases this result will be sent to the user.
 
16761
         This should be changed to use copy_int or copy_real depending
 
16762
         on how the value is to be used: In some cases this may be an
 
16763
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
5884
16764
      */
5885
16765
      if (!(pos=new Item_copy_string(pos)))
5886
 
        goto err;
5887
 
      if (i < border)                           // HAVING, order_st and GROUP BY
 
16766
        goto err;
 
16767
      if (i < border)                           // HAVING, ORDER and GROUP BY
5888
16768
      {
5889
16769
        if (extra_funcs.push_back(pos))
5890
16770
          goto err;
5891
16771
      }
5892
16772
      else if (param->copy_funcs.push_back(pos))
5893
 
        goto err;
 
16773
        goto err;
5894
16774
    }
5895
16775
    res_all_fields.push_back(pos);
5896
16776
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
5909
16789
 
5910
16790
  return(0);
5911
16791
 
5912
 
err:
 
16792
 err:
5913
16793
  if (copy)
5914
16794
    delete [] param->copy_field;                        // This is never 0
5915
16795
  param->copy_field=0;
5917
16797
  return(true);
5918
16798
}
5919
16799
 
 
16800
 
5920
16801
/**
5921
16802
  Make a copy of all simple SELECT'ed items.
5922
16803
 
5923
16804
  This is done at the start of a new group so that we can retrieve
5924
16805
  these later when the group changes.
5925
16806
*/
5926
 
void copy_fields(Tmp_Table_Param *param)
 
16807
 
 
16808
void
 
16809
copy_fields(TMP_TABLE_PARAM *param)
5927
16810
{
5928
 
  CopyField *ptr= param->copy_field;
5929
 
  CopyField *end= param->copy_field_end;
 
16811
  Copy_field *ptr=param->copy_field;
 
16812
  Copy_field *end=param->copy_field_end;
5930
16813
 
5931
16814
  for (; ptr != end; ptr++)
5932
16815
    (*ptr->do_copy)(ptr);
5933
16816
 
5934
 
  List<Item>::iterator it(param->copy_funcs.begin());
 
16817
  List_iterator_fast<Item> it(param->copy_funcs);
5935
16818
  Item_copy_string *item;
5936
16819
  while ((item = (Item_copy_string*) it++))
5937
16820
    item->copy();
5938
16821
}
5939
16822
 
 
16823
 
 
16824
/**
 
16825
  Make an array of pointers to sum_functions to speed up
 
16826
  sum_func calculation.
 
16827
 
 
16828
  @retval
 
16829
    0   ok
 
16830
  @retval
 
16831
    1   Error
 
16832
*/
 
16833
 
 
16834
bool JOIN::alloc_func_list()
 
16835
{
 
16836
  uint func_count, group_parts;
 
16837
 
 
16838
  func_count= tmp_table_param.sum_func_count;
 
16839
  /*
 
16840
    If we are using rollup, we need a copy of the summary functions for
 
16841
    each level
 
16842
  */
 
16843
  if (rollup.state != ROLLUP::STATE_NONE)
 
16844
    func_count*= (send_group_parts+1);
 
16845
 
 
16846
  group_parts= send_group_parts;
 
16847
  /*
 
16848
    If distinct, reserve memory for possible
 
16849
    disctinct->group_by optimization
 
16850
  */
 
16851
  if (select_distinct)
 
16852
  {
 
16853
    group_parts+= fields_list.elements;
 
16854
    /*
 
16855
      If the ORDER clause is specified then it's possible that
 
16856
      it also will be optimized, so reserve space for it too
 
16857
    */
 
16858
    if (order)
 
16859
    {
 
16860
      ORDER *ord;
 
16861
      for (ord= order; ord; ord= ord->next)
 
16862
        group_parts++;
 
16863
    }
 
16864
  }
 
16865
 
 
16866
  /* This must use calloc() as rollup_make_fields depends on this */
 
16867
  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
 
16868
                                      sizeof(Item_sum***) * (group_parts+1));
 
16869
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
 
16870
  return(sum_funcs == 0);
 
16871
}
 
16872
 
 
16873
 
 
16874
/**
 
16875
  Initialize 'sum_funcs' array with all Item_sum objects.
 
16876
 
 
16877
  @param field_list        All items
 
16878
  @param send_fields       Items in select list
 
16879
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
 
16880
  @param recompute         Set to true if sum_funcs must be recomputed
 
16881
 
 
16882
  @retval
 
16883
    0  ok
 
16884
  @retval
 
16885
    1  error
 
16886
*/
 
16887
 
 
16888
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
 
16889
                              bool before_group_by, bool recompute)
 
16890
{
 
16891
  List_iterator_fast<Item> it(field_list);
 
16892
  Item_sum **func;
 
16893
  Item *item;
 
16894
 
 
16895
  if (*sum_funcs && !recompute)
 
16896
    return(false); /* We have already initialized sum_funcs. */
 
16897
 
 
16898
  func= sum_funcs;
 
16899
  while ((item=it++))
 
16900
  {
 
16901
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
16902
        (!((Item_sum*) item)->depended_from() ||
 
16903
         ((Item_sum *)item)->depended_from() == select_lex))
 
16904
      *func++= (Item_sum*) item;
 
16905
  }
 
16906
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
16907
  {
 
16908
    rollup.state= ROLLUP::STATE_READY;
 
16909
    if (rollup_make_fields(field_list, send_fields, &func))
 
16910
      return(true);                     // Should never happen
 
16911
  }
 
16912
  else if (rollup.state == ROLLUP::STATE_NONE)
 
16913
  {
 
16914
    for (uint i=0 ; i <= send_group_parts ;i++)
 
16915
      sum_funcs_end[i]= func;
 
16916
  }
 
16917
  else if (rollup.state == ROLLUP::STATE_READY)
 
16918
    return(false);                         // Don't put end marker
 
16919
  *func=0;                                      // End marker
 
16920
  return(false);
 
16921
}
 
16922
 
 
16923
 
5940
16924
/**
5941
16925
  Change all funcs and sum_funcs to fields in tmp table, and create
5942
16926
  new list of all items.
5943
16927
 
5944
 
  @param session                   Session pointer
 
16928
  @param thd                   THD pointer
5945
16929
  @param ref_pointer_array     array of pointers to top elements of filed list
5946
16930
  @param res_selected_fields   new list of items of select item list
5947
16931
  @param res_all_fields        new list of all items
5953
16937
  @retval
5954
16938
    !=0   error
5955
16939
*/
5956
 
bool change_to_use_tmp_fields(Session *session,
5957
 
                              Item **ref_pointer_array,
5958
 
                                                List<Item> &res_selected_fields,
5959
 
                                                List<Item> &res_all_fields,
5960
 
                                                uint32_t elements,
5961
 
                              List<Item> &all_fields)
 
16940
 
 
16941
static bool
 
16942
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
16943
                         List<Item> &res_selected_fields,
 
16944
                         List<Item> &res_all_fields,
 
16945
                         uint elements, List<Item> &all_fields)
5962
16946
{
5963
 
  List<Item>::iterator it(all_fields.begin());
 
16947
  List_iterator_fast<Item> it(all_fields);
5964
16948
  Item *item_field,*item;
5965
16949
 
5966
 
  res_selected_fields.clear();
5967
 
  res_all_fields.clear();
 
16950
  res_selected_fields.empty();
 
16951
  res_all_fields.empty();
5968
16952
 
5969
 
  uint32_t i, border= all_fields.elements - elements;
 
16953
  uint i, border= all_fields.elements - elements;
5970
16954
  for (i= 0; (item= it++); i++)
5971
16955
  {
5972
16956
    Field *field;
5979
16963
    {
5980
16964
      if (item->type() == Item::FIELD_ITEM)
5981
16965
      {
5982
 
        item_field= item->get_tmp_table_item(session);
 
16966
        item_field= item->get_tmp_table_item(thd);
5983
16967
      }
5984
16968
      else if ((field= item->get_tmp_table_field()))
5985
16969
      {
5986
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->getTable()->group)
5987
 
          item_field= ((Item_sum*) item)->result_item(field);
5988
 
        else
5989
 
          item_field= (Item*) new Item_field(field);
5990
 
        if (!item_field)
5991
 
          return(true);                    // Fatal error
 
16970
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
16971
          item_field= ((Item_sum*) item)->result_item(field);
 
16972
        else
 
16973
          item_field= (Item*) new Item_field(field);
 
16974
        if (!item_field)
 
16975
          return(true);                    // Fatal error
5992
16976
 
5993
16977
        if (item->real_item()->type() != Item::FIELD_ITEM)
5994
16978
          field->orig_table= 0;
5995
 
        item_field->name= item->name;
 
16979
        item_field->name= item->name;
5996
16980
        if (item->type() == Item::REF_ITEM)
5997
16981
        {
5998
16982
          Item_field *ifield= (Item_field *) item_field;
6002
16986
        }
6003
16987
      }
6004
16988
      else
6005
 
        item_field= item;
 
16989
        item_field= item;
6006
16990
    }
6007
16991
    res_all_fields.push_back(item_field);
6008
16992
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6009
16993
      item_field;
6010
16994
  }
6011
16995
 
6012
 
  List<Item>::iterator itr(res_all_fields.begin());
 
16996
  List_iterator_fast<Item> itr(res_all_fields);
6013
16997
  for (i= 0; i < border; i++)
6014
16998
    itr++;
6015
16999
  itr.sublist(res_selected_fields, elements);
6016
17000
  return(false);
6017
17001
}
6018
17002
 
 
17003
 
6019
17004
/**
6020
17005
  Change all sum_func refs to fields to point at fields in tmp table.
6021
17006
  Change all funcs to be fields in tmp table.
6022
17007
 
6023
 
  @param session                   Session pointer
 
17008
  @param thd                   THD pointer
6024
17009
  @param ref_pointer_array     array of pointers to top elements of filed list
6025
17010
  @param res_selected_fields   new list of items of select item list
6026
17011
  @param res_all_fields        new list of all items
6032
17017
  @retval
6033
17018
    1   error
6034
17019
*/
6035
 
bool change_refs_to_tmp_fields(Session *session,
6036
 
                               Item **ref_pointer_array,
6037
 
                               List<Item> &res_selected_fields,
6038
 
                               List<Item> &res_all_fields,
6039
 
                               uint32_t elements,
6040
 
                                                 List<Item> &all_fields)
 
17020
 
 
17021
static bool
 
17022
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
17023
                          List<Item> &res_selected_fields,
 
17024
                          List<Item> &res_all_fields, uint elements,
 
17025
                          List<Item> &all_fields)
6041
17026
{
6042
 
  List<Item>::iterator it(all_fields.begin());
 
17027
  List_iterator_fast<Item> it(all_fields);
6043
17028
  Item *item, *new_item;
6044
 
  res_selected_fields.clear();
6045
 
  res_all_fields.clear();
 
17029
  res_selected_fields.empty();
 
17030
  res_all_fields.empty();
6046
17031
 
6047
 
  uint32_t i, border= all_fields.elements - elements;
 
17032
  uint i, border= all_fields.elements - elements;
6048
17033
  for (i= 0; (item= it++); i++)
6049
17034
  {
6050
 
    res_all_fields.push_back(new_item= item->get_tmp_table_item(session));
 
17035
    res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
6051
17036
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6052
17037
      new_item;
6053
17038
  }
6054
17039
 
6055
 
  List<Item>::iterator itr(res_all_fields.begin());
 
17040
  List_iterator_fast<Item> itr(res_all_fields);
6056
17041
  for (i= 0; i < border; i++)
6057
17042
    itr++;
6058
17043
  itr.sublist(res_selected_fields, elements);
6059
17044
 
6060
 
  return session->is_fatal_error;
 
17045
  return thd->is_fatal_error;
6061
17046
}
6062
17047
 
 
17048
 
 
17049
 
6063
17050
/******************************************************************************
6064
17051
  Code for calculating functions
6065
17052
******************************************************************************/
6066
17053
 
 
17054
 
6067
17055
/**
6068
17056
  Call ::setup for all sum functions.
6069
17057
 
6070
 
  @param session           thread Cursor
 
17058
  @param thd           thread handler
6071
17059
  @param func_ptr      sum function list
6072
17060
 
6073
17061
  @retval
6075
17063
  @retval
6076
17064
    true   error
6077
17065
*/
6078
 
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
 
17066
 
 
17067
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
6079
17068
{
6080
17069
  Item_sum *func;
6081
17070
  while ((func= *(func_ptr++)))
6082
17071
  {
6083
 
    if (func->setup(session))
 
17072
    if (func->setup(thd))
6084
17073
      return(true);
6085
17074
  }
6086
17075
  return(false);
6087
17076
}
6088
17077
 
6089
 
void init_tmptable_sum_functions(Item_sum **func_ptr)
 
17078
 
 
17079
static void
 
17080
init_tmptable_sum_functions(Item_sum **func_ptr)
6090
17081
{
6091
17082
  Item_sum *func;
6092
17083
  while ((func= *(func_ptr++)))
6093
17084
    func->reset_field();
6094
17085
}
6095
17086
 
 
17087
 
6096
17088
/** Update record 0 in tmp_table from record 1. */
6097
 
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
 
17089
 
 
17090
static void
 
17091
update_tmptable_sum_func(Item_sum **func_ptr,
 
17092
                         TABLE *tmp_table __attribute__((unused)))
6098
17093
{
6099
17094
  Item_sum *func;
6100
17095
  while ((func= *(func_ptr++)))
6101
17096
    func->update_field();
6102
17097
}
6103
17098
 
 
17099
 
6104
17100
/** Copy result of sum functions to record in tmp_table. */
6105
 
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
17101
 
 
17102
static void
 
17103
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
6106
17104
{
6107
17105
  for (; func_ptr != end_ptr ; func_ptr++)
6108
17106
    (void) (*func_ptr)->save_in_result_field(1);
6109
17107
  return;
6110
17108
}
6111
17109
 
6112
 
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
17110
 
 
17111
static bool
 
17112
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6113
17113
{
6114
17114
  for (; func_ptr != end_ptr ;func_ptr++)
6115
17115
  {
6125
17125
  return 0;
6126
17126
}
6127
17127
 
6128
 
bool update_sum_func(Item_sum **func_ptr)
 
17128
 
 
17129
static bool
 
17130
update_sum_func(Item_sum **func_ptr)
6129
17131
{
6130
17132
  Item_sum *func;
6131
17133
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6135
17137
}
6136
17138
 
6137
17139
/** Copy result of functions to record in tmp_table. */
6138
 
bool copy_funcs(Item **func_ptr, const Session *session)
 
17140
 
 
17141
void
 
17142
copy_funcs(Item **func_ptr)
6139
17143
{
6140
17144
  Item *func;
6141
17145
  for (; (func = *func_ptr) ; func_ptr++)
6142
 
  {
6143
17146
    func->save_in_result_field(1);
6144
 
    /*
6145
 
      Need to check the THD error state because Item::val_xxx() don't
6146
 
      return error code, but can generate errors
6147
 
      @todo change it for a real status check when Item::val_xxx()
6148
 
      are extended to return status code.
6149
 
    */
6150
 
    if (session->is_error())
6151
 
      return true;
6152
 
  }
6153
 
  return false;
6154
 
}
 
17147
}
 
17148
 
 
17149
 
 
17150
/**
 
17151
  Create a condition for a const reference and add this to the
 
17152
  currenct select for the table.
 
17153
*/
 
17154
 
 
17155
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
 
17156
{
 
17157
  if (!join_tab->ref.key_parts)
 
17158
    return(false);
 
17159
 
 
17160
  Item_cond_and *cond=new Item_cond_and();
 
17161
  TABLE *table=join_tab->table;
 
17162
  int error;
 
17163
  if (!cond)
 
17164
    return(true);
 
17165
 
 
17166
  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
 
17167
  {
 
17168
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
 
17169
                              fieldnr-1];
 
17170
    Item *value=join_tab->ref.items[i];
 
17171
    cond->add(new Item_func_equal(new Item_field(field), value));
 
17172
  }
 
17173
  if (thd->is_fatal_error)
 
17174
    return(true);
 
17175
 
 
17176
  if (!cond->fixed)
 
17177
    cond->fix_fields(thd, (Item**)&cond);
 
17178
  if (join_tab->select)
 
17179
  {
 
17180
    error=(int) cond->add(join_tab->select->cond);
 
17181
    join_tab->select_cond=join_tab->select->cond=cond;
 
17182
  }
 
17183
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
 
17184
                                          &error)))
 
17185
    join_tab->select_cond=cond;
 
17186
 
 
17187
  return(error ? true : false);
 
17188
}
 
17189
 
6155
17190
 
6156
17191
/**
6157
17192
  Free joins of subselect of this select.
6158
17193
 
6159
 
  @param session      Session pointer
6160
 
  @param select   pointer to Select_Lex which subselects joins we will free
 
17194
  @param thd      THD pointer
 
17195
  @param select   pointer to st_select_lex which subselects joins we will free
6161
17196
*/
6162
 
void free_underlaid_joins(Session *, Select_Lex *select)
 
17197
 
 
17198
void free_underlaid_joins(THD *thd __attribute__((__unused__)),
 
17199
                          SELECT_LEX *select)
6163
17200
{
6164
 
  for (Select_Lex_Unit *unit= select->first_inner_unit();
 
17201
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
6165
17202
       unit;
6166
17203
       unit= unit->next_unit())
6167
17204
    unit->cleanup();
6183
17220
  @b EXAMPLES
6184
17221
    @code
6185
17222
      SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP
6186
 
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP
 
17223
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP 
6187
17224
  @endcode
6188
17225
 
6189
17226
  @b IMPLEMENTATION
6196
17233
    This substitution is needed GROUP BY queries with ROLLUP if
6197
17234
    SELECT list contains expressions over group by attributes.
6198
17235
 
6199
 
  @param session                  reference to the context
 
17236
  @param thd                  reference to the context
6200
17237
  @param expr                 expression to make replacement
6201
17238
  @param group_list           list of references to group by items
6202
17239
  @param changed        out:  returns 1 if item contains a replaced field item
6203
17240
 
6204
17241
  @todo
6205
 
    - @todo Some functions are not null-preserving. For those functions
6206
 
    updating of the maybe_null attribute is an overkill.
 
17242
    - TODO: Some functions are not null-preserving. For those functions
 
17243
    updating of the maybe_null attribute is an overkill. 
6207
17244
 
6208
17245
  @retval
6209
17246
    0   if ok
6210
17247
  @retval
6211
17248
    1   on error
6212
17249
*/
6213
 
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
 
17250
 
 
17251
static bool change_group_ref(THD *thd, Item_func *expr, ORDER *group_list,
 
17252
                             bool *changed)
6214
17253
{
6215
17254
  if (expr->arg_count)
6216
17255
  {
6217
 
    Name_resolution_context *context= &session->getLex()->current_select->context;
 
17256
    Name_resolution_context *context= &thd->lex->current_select->context;
6218
17257
    Item **arg,**arg_end;
6219
17258
    bool arg_changed= false;
6220
17259
    for (arg= expr->arguments(),
6224
17263
      Item *item= *arg;
6225
17264
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6226
17265
      {
6227
 
        Order *group_tmp;
 
17266
        ORDER *group_tmp;
6228
17267
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6229
17268
        {
6230
17269
          if (item->eq(*group_tmp->item,0))
6233
17272
            if (!(new_item= new Item_ref(context, group_tmp->item, 0,
6234
17273
                                        item->name)))
6235
17274
              return 1;                                 // fatal_error is set
6236
 
            session->change_item_tree(arg, new_item);
 
17275
            thd->change_item_tree(arg, new_item);
6237
17276
            arg_changed= true;
6238
17277
          }
6239
17278
        }
6240
17279
      }
6241
17280
      else if (item->type() == Item::FUNC_ITEM)
6242
17281
      {
6243
 
        if (change_group_ref(session, (Item_func *) item, group_list, &arg_changed))
 
17282
        if (change_group_ref(thd, (Item_func *) item, group_list, &arg_changed))
6244
17283
          return 1;
6245
17284
      }
6246
17285
    }
6254
17293
}
6255
17294
 
6256
17295
 
6257
 
static void print_table_array(Session *session, String *str, TableList **table,
6258
 
                              TableList **end)
6259
 
{
6260
 
  (*table)->print(session, str, QT_ORDINARY);
6261
 
 
6262
 
  for (TableList **tbl= table + 1; tbl < end; tbl++)
6263
 
  {
6264
 
    TableList *curr= *tbl;
 
17296
/** Allocate memory needed for other rollup functions. */
 
17297
 
 
17298
bool JOIN::rollup_init()
 
17299
{
 
17300
  uint i,j;
 
17301
  Item **ref_array;
 
17302
 
 
17303
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
 
17304
  rollup.state= ROLLUP::STATE_INITED;
 
17305
 
 
17306
  /*
 
17307
    Create pointers to the different sum function groups
 
17308
    These are updated by rollup_make_fields()
 
17309
  */
 
17310
  tmp_table_param.group_parts= send_group_parts;
 
17311
 
 
17312
  if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
 
17313
                                                sizeof(Item**) +
 
17314
                                                sizeof(List<Item>) +
 
17315
                                                ref_pointer_array_size)
 
17316
                                                * send_group_parts )))
 
17317
    return 1;
 
17318
  
 
17319
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
17320
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
17321
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
17322
 
 
17323
  /*
 
17324
    Prepare space for field list for the different levels
 
17325
    These will be filled up in rollup_make_fields()
 
17326
  */
 
17327
  for (i= 0 ; i < send_group_parts ; i++)
 
17328
  {
 
17329
    rollup.null_items[i]= new (thd->mem_root) Item_null_result();
 
17330
    List<Item> *rollup_fields= &rollup.fields[i];
 
17331
    rollup_fields->empty();
 
17332
    rollup.ref_pointer_arrays[i]= ref_array;
 
17333
    ref_array+= all_fields.elements;
 
17334
  }
 
17335
  for (i= 0 ; i < send_group_parts; i++)
 
17336
  {
 
17337
    for (j=0 ; j < fields_list.elements ; j++)
 
17338
      rollup.fields[i].push_back(rollup.null_items[i]);
 
17339
  }
 
17340
  List_iterator<Item> it(all_fields);
 
17341
  Item *item;
 
17342
  while ((item= it++))
 
17343
  {
 
17344
    ORDER *group_tmp;
 
17345
    bool found_in_group= 0;
 
17346
 
 
17347
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
 
17348
    {
 
17349
      if (*group_tmp->item == item)
 
17350
      {
 
17351
        item->maybe_null= 1;
 
17352
        found_in_group= 1;
 
17353
        if (item->const_item())
 
17354
        {
 
17355
          /*
 
17356
            For ROLLUP queries each constant item referenced in GROUP BY list
 
17357
            is wrapped up into an Item_func object yielding the same value
 
17358
            as the constant item. The objects of the wrapper class are never
 
17359
            considered as constant items and besides they inherit all
 
17360
            properties of the Item_result_field class.
 
17361
            This wrapping allows us to ensure writing constant items
 
17362
            into temporary tables whenever the result of the ROLLUP
 
17363
            operation has to be written into a temporary table, e.g. when
 
17364
            ROLLUP is used together with DISTINCT in the SELECT list.
 
17365
            Usually when creating temporary tables for a intermidiate
 
17366
            result we do not include fields for constant expressions.
 
17367
          */           
 
17368
          Item* new_item= new Item_func_rollup_const(item);
 
17369
          if (!new_item)
 
17370
            return 1;
 
17371
          new_item->fix_fields(thd, (Item **) 0);
 
17372
          thd->change_item_tree(it.ref(), new_item);
 
17373
          for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
 
17374
          { 
 
17375
            if (*tmp->item == item)
 
17376
              thd->change_item_tree(tmp->item, new_item);
 
17377
          }
 
17378
        }
 
17379
      }
 
17380
    }
 
17381
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
 
17382
    {
 
17383
      bool changed= false;
 
17384
      if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
 
17385
        return 1;
 
17386
      /*
 
17387
        We have to prevent creation of a field in a temporary table for
 
17388
        an expression that contains GROUP BY attributes.
 
17389
        Marking the expression item as 'with_sum_func' will ensure this.
 
17390
      */ 
 
17391
      if (changed)
 
17392
        item->with_sum_func= 1;
 
17393
    }
 
17394
  }
 
17395
  return 0;
 
17396
}
 
17397
  
 
17398
 
 
17399
/**
 
17400
  Fill up rollup structures with pointers to fields to use.
 
17401
 
 
17402
  Creates copies of item_sum items for each sum level.
 
17403
 
 
17404
  @param fields_arg             List of all fields (hidden and real ones)
 
17405
  @param sel_fields             Pointer to selected fields
 
17406
  @param func                   Store here a pointer to all fields
 
17407
 
 
17408
  @retval
 
17409
    0   if ok;
 
17410
    In this case func is pointing to next not used element.
 
17411
  @retval
 
17412
    1    on error
 
17413
*/
 
17414
 
 
17415
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
 
17416
                              Item_sum ***func)
 
17417
{
 
17418
  List_iterator_fast<Item> it(fields_arg);
 
17419
  Item *first_field= sel_fields.head();
 
17420
  uint level;
 
17421
 
 
17422
  /*
 
17423
    Create field lists for the different levels
 
17424
 
 
17425
    The idea here is to have a separate field list for each rollup level to
 
17426
    avoid all runtime checks of which columns should be NULL.
 
17427
 
 
17428
    The list is stored in reverse order to get sum function in such an order
 
17429
    in func that it makes it easy to reset them with init_sum_functions()
 
17430
 
 
17431
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
17432
 
 
17433
    rollup.fields[0] will contain list where a,b,c is NULL
 
17434
    rollup.fields[1] will contain list where b,c is NULL
 
17435
    ...
 
17436
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
 
17437
    ...
 
17438
    sum_funcs_end[0] points to all sum functions
 
17439
    sum_funcs_end[1] points to all sum functions, except grand totals
 
17440
    ...
 
17441
  */
 
17442
 
 
17443
  for (level=0 ; level < send_group_parts ; level++)
 
17444
  {
 
17445
    uint i;
 
17446
    uint pos= send_group_parts - level -1;
 
17447
    bool real_fields= 0;
 
17448
    Item *item;
 
17449
    List_iterator<Item> new_it(rollup.fields[pos]);
 
17450
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
17451
    ORDER *start_group;
 
17452
 
 
17453
    /* Point to first hidden field */
 
17454
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
17455
 
 
17456
    /* Remember where the sum functions ends for the previous level */
 
17457
    sum_funcs_end[pos+1]= *func;
 
17458
 
 
17459
    /* Find the start of the group for this level */
 
17460
    for (i= 0, start_group= group_list ;
 
17461
         i++ < pos ;
 
17462
         start_group= start_group->next)
 
17463
      ;
 
17464
 
 
17465
    it.rewind();
 
17466
    while ((item= it++))
 
17467
    {
 
17468
      if (item == first_field)
 
17469
      {
 
17470
        real_fields= 1;                         // End of hidden fields
 
17471
        ref_array= ref_array_start;
 
17472
      }
 
17473
 
 
17474
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
17475
          (!((Item_sum*) item)->depended_from() ||
 
17476
           ((Item_sum *)item)->depended_from() == select_lex))
 
17477
          
 
17478
      {
 
17479
        /*
 
17480
          This is a top level summary function that must be replaced with
 
17481
          a sum function that is reset for this level.
 
17482
 
 
17483
          NOTE: This code creates an object which is not that nice in a
 
17484
          sub select.  Fortunately it's not common to have rollup in
 
17485
          sub selects.
 
17486
        */
 
17487
        item= item->copy_or_same(thd);
 
17488
        ((Item_sum*) item)->make_unique();
 
17489
        *(*func)= (Item_sum*) item;
 
17490
        (*func)++;
 
17491
      }
 
17492
      else 
 
17493
      {
 
17494
        /* Check if this is something that is part of this group by */
 
17495
        ORDER *group_tmp;
 
17496
        for (group_tmp= start_group, i= pos ;
 
17497
             group_tmp ; group_tmp= group_tmp->next, i++)
 
17498
        {
 
17499
          if (*group_tmp->item == item)
 
17500
          {
 
17501
            /*
 
17502
              This is an element that is used by the GROUP BY and should be
 
17503
              set to NULL in this level
 
17504
            */
 
17505
            Item_null_result *null_item= new (thd->mem_root) Item_null_result();
 
17506
            if (!null_item)
 
17507
              return 1;
 
17508
            item->maybe_null= 1;                // Value will be null sometimes
 
17509
            null_item->result_field= item->get_tmp_table_field();
 
17510
            item= null_item;
 
17511
            break;
 
17512
          }
 
17513
        }
 
17514
      }
 
17515
      *ref_array= item;
 
17516
      if (real_fields)
 
17517
      {
 
17518
        (void) new_it++;                        // Point to next item
 
17519
        new_it.replace(item);                   // Replace previous
 
17520
        ref_array++;
 
17521
      }
 
17522
      else
 
17523
        ref_array--;
 
17524
    }
 
17525
  }
 
17526
  sum_funcs_end[0]= *func;                      // Point to last function
 
17527
  return 0;
 
17528
}
 
17529
 
 
17530
/**
 
17531
  Send all rollup levels higher than the current one to the client.
 
17532
 
 
17533
  @b SAMPLE
 
17534
    @code
 
17535
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
17536
  @endcode
 
17537
 
 
17538
  @param idx            Level we are on:
 
17539
                        - 0 = Total sum level
 
17540
                        - 1 = First group changed  (a)
 
17541
                        - 2 = Second group changed (a,b)
 
17542
 
 
17543
  @retval
 
17544
    0   ok
 
17545
  @retval
 
17546
    1   If send_data_failed()
 
17547
*/
 
17548
 
 
17549
int JOIN::rollup_send_data(uint idx)
 
17550
{
 
17551
  uint i;
 
17552
  for (i= send_group_parts ; i-- > idx ; )
 
17553
  {
 
17554
    /* Get reference pointers to sum functions in place */
 
17555
    memcpy((char*) ref_pointer_array,
 
17556
           (char*) rollup.ref_pointer_arrays[i],
 
17557
           ref_pointer_array_size);
 
17558
    if ((!having || having->val_int()))
 
17559
    {
 
17560
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
17561
          result->send_data(rollup.fields[i]))
 
17562
        return 1;
 
17563
      send_records++;
 
17564
    }
 
17565
  }
 
17566
  /* Restore ref_pointer_array */
 
17567
  set_items_ref_array(current_ref_pointer_array);
 
17568
  return 0;
 
17569
}
 
17570
 
 
17571
/**
 
17572
  Write all rollup levels higher than the current one to a temp table.
 
17573
 
 
17574
  @b SAMPLE
 
17575
    @code
 
17576
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
 
17577
  @endcode
 
17578
 
 
17579
  @param idx                 Level we are on:
 
17580
                               - 0 = Total sum level
 
17581
                               - 1 = First group changed  (a)
 
17582
                               - 2 = Second group changed (a,b)
 
17583
  @param table               reference to temp table
 
17584
 
 
17585
  @retval
 
17586
    0   ok
 
17587
  @retval
 
17588
    1   if write_data_failed()
 
17589
*/
 
17590
 
 
17591
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
 
17592
{
 
17593
  uint i;
 
17594
  for (i= send_group_parts ; i-- > idx ; )
 
17595
  {
 
17596
    /* Get reference pointers to sum functions in place */
 
17597
    memcpy((char*) ref_pointer_array,
 
17598
           (char*) rollup.ref_pointer_arrays[i],
 
17599
           ref_pointer_array_size);
 
17600
    if ((!having || having->val_int()))
 
17601
    {
 
17602
      int write_error;
 
17603
      Item *item;
 
17604
      List_iterator_fast<Item> it(rollup.fields[i]);
 
17605
      while ((item= it++))
 
17606
      {
 
17607
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
 
17608
          item->save_in_result_field(1);
 
17609
      }
 
17610
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
 
17611
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
 
17612
      {
 
17613
        if (create_myisam_from_heap(thd, table_arg, 
 
17614
                                    tmp_table_param.start_recinfo,
 
17615
                                    &tmp_table_param.recinfo,
 
17616
                                    write_error, 0))
 
17617
          return 1;                  
 
17618
      }
 
17619
    }
 
17620
  }
 
17621
  /* Restore ref_pointer_array */
 
17622
  set_items_ref_array(current_ref_pointer_array);
 
17623
  return 0;
 
17624
}
 
17625
 
 
17626
/**
 
17627
  clear results if there are not rows found for group
 
17628
  (end_send_group/end_write_group)
 
17629
*/
 
17630
 
 
17631
void JOIN::clear()
 
17632
{
 
17633
  clear_tables(this);
 
17634
  copy_fields(&tmp_table_param);
 
17635
 
 
17636
  if (sum_funcs)
 
17637
  {
 
17638
    Item_sum *func, **func_ptr= sum_funcs;
 
17639
    while ((func= *(func_ptr++)))
 
17640
      func->clear();
 
17641
  }
 
17642
}
 
17643
 
 
17644
/**
 
17645
  EXPLAIN handling.
 
17646
 
 
17647
  Send a description about what how the select will be done to stdout.
 
17648
*/
 
17649
 
 
17650
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
 
17651
                     bool distinct,const char *message)
 
17652
{
 
17653
  List<Item> field_list;
 
17654
  List<Item> item_list;
 
17655
  THD *thd=join->thd;
 
17656
  select_result *result=join->result;
 
17657
  Item *item_null= new Item_null();
 
17658
  CHARSET_INFO *cs= system_charset_info;
 
17659
  int quick_type;
 
17660
  /* Don't log this into the slow query log */
 
17661
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
 
17662
  join->unit->offset_limit_cnt= 0;
 
17663
 
 
17664
  /* 
 
17665
    NOTE: the number/types of items pushed into item_list must be in sync with
 
17666
    EXPLAIN column types as they're "defined" in THD::send_explain_fields()
 
17667
  */
 
17668
  if (message)
 
17669
  {
 
17670
    item_list.push_back(new Item_int((int32)
 
17671
                                     join->select_lex->select_number));
 
17672
    item_list.push_back(new Item_string(join->select_lex->type,
 
17673
                                        strlen(join->select_lex->type), cs));
 
17674
    for (uint i=0 ; i < 7; i++)
 
17675
      item_list.push_back(item_null);
 
17676
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
17677
      item_list.push_back(item_null);
 
17678
  
 
17679
    item_list.push_back(new Item_string(message,strlen(message),cs));
 
17680
    if (result->send_data(item_list))
 
17681
      join->error= 1;
 
17682
  }
 
17683
  else if (join->select_lex == join->unit->fake_select_lex)
 
17684
  {
 
17685
    /* 
 
17686
      here we assume that the query will return at least two rows, so we
 
17687
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
 
17688
      and no filesort will be actually done, but executing all selects in
 
17689
      the UNION to provide precise EXPLAIN information will hardly be
 
17690
      appreciated :)
 
17691
    */
 
17692
    char table_name_buffer[NAME_LEN];
 
17693
    item_list.empty();
 
17694
    /* id */
 
17695
    item_list.push_back(new Item_null);
 
17696
    /* select_type */
 
17697
    item_list.push_back(new Item_string(join->select_lex->type,
 
17698
                                        strlen(join->select_lex->type),
 
17699
                                        cs));
 
17700
    /* table */
 
17701
    {
 
17702
      SELECT_LEX *sl= join->unit->first_select();
 
17703
      uint len= 6, lastop= 0;
 
17704
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
 
17705
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
 
17706
      {
 
17707
        len+= lastop;
 
17708
        lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
 
17709
                         "%u,", sl->select_number);
 
17710
      }
 
17711
      if (sl || len + lastop >= NAME_LEN)
 
17712
      {
 
17713
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
 
17714
        len+= 4;
 
17715
      }
 
17716
      else
 
17717
      {
 
17718
        len+= lastop;
 
17719
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
 
17720
      }
 
17721
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
17722
    }
 
17723
    /* type */
 
17724
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
 
17725
                                          strlen(join_type_str[JT_ALL]),
 
17726
                                          cs));
 
17727
    /* possible_keys */
 
17728
    item_list.push_back(item_null);
 
17729
    /* key*/
 
17730
    item_list.push_back(item_null);
 
17731
    /* key_len */
 
17732
    item_list.push_back(item_null);
 
17733
    /* ref */
 
17734
    item_list.push_back(item_null);
 
17735
    /* in_rows */
 
17736
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
17737
      item_list.push_back(item_null);
 
17738
    /* rows */
 
17739
    item_list.push_back(item_null);
 
17740
    /* extra */
 
17741
    if (join->unit->global_parameters->order_list.first)
 
17742
      item_list.push_back(new Item_string("Using filesort",
 
17743
                                          14, cs));
 
17744
    else
 
17745
      item_list.push_back(new Item_string("", 0, cs));
 
17746
 
 
17747
    if (result->send_data(item_list))
 
17748
      join->error= 1;
 
17749
  }
 
17750
  else
 
17751
  {
 
17752
    table_map used_tables=0;
 
17753
    for (uint i=0 ; i < join->tables ; i++)
 
17754
    {
 
17755
      JOIN_TAB *tab=join->join_tab+i;
 
17756
      TABLE *table=tab->table;
 
17757
      TABLE_LIST *table_list= tab->table->pos_in_table_list;
 
17758
      char buff[512]; 
 
17759
      char buff1[512], buff2[512], buff3[512];
 
17760
      char keylen_str_buf[64];
 
17761
      String extra(buff, sizeof(buff),cs);
 
17762
      char table_name_buffer[NAME_LEN];
 
17763
      String tmp1(buff1,sizeof(buff1),cs);
 
17764
      String tmp2(buff2,sizeof(buff2),cs);
 
17765
      String tmp3(buff3,sizeof(buff3),cs);
 
17766
      extra.length(0);
 
17767
      tmp1.length(0);
 
17768
      tmp2.length(0);
 
17769
      tmp3.length(0);
 
17770
 
 
17771
      quick_type= -1;
 
17772
      item_list.empty();
 
17773
      /* id */
 
17774
      item_list.push_back(new Item_uint((uint32)
 
17775
                                       join->select_lex->select_number));
 
17776
      /* select_type */
 
17777
      item_list.push_back(new Item_string(join->select_lex->type,
 
17778
                                          strlen(join->select_lex->type),
 
17779
                                          cs));
 
17780
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
 
17781
      {
 
17782
        quick_type= tab->select->quick->get_type();
 
17783
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
 
17784
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
 
17785
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
 
17786
          tab->type = JT_INDEX_MERGE;
 
17787
        else
 
17788
          tab->type = JT_RANGE;
 
17789
      }
 
17790
      /* table */
 
17791
      if (table->derived_select_number)
 
17792
      {
 
17793
        /* Derived table name generation */
 
17794
        int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
 
17795
                          "<derived%u>",
 
17796
                          table->derived_select_number);
 
17797
        item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
17798
      }
 
17799
      else
 
17800
      {
 
17801
        TABLE_LIST *real_table= table->pos_in_table_list; 
 
17802
        item_list.push_back(new Item_string(real_table->alias,
 
17803
                                            strlen(real_table->alias),
 
17804
                                            cs));
 
17805
      }
 
17806
      /* "type" column */
 
17807
      item_list.push_back(new Item_string(join_type_str[tab->type],
 
17808
                                          strlen(join_type_str[tab->type]),
 
17809
                                          cs));
 
17810
      /* Build "possible_keys" value and add it to item_list */
 
17811
      if (!tab->keys.is_clear_all())
 
17812
      {
 
17813
        uint j;
 
17814
        for (j=0 ; j < table->s->keys ; j++)
 
17815
        {
 
17816
          if (tab->keys.is_set(j))
 
17817
          {
 
17818
            if (tmp1.length())
 
17819
              tmp1.append(',');
 
17820
            tmp1.append(table->key_info[j].name, 
 
17821
                        strlen(table->key_info[j].name),
 
17822
                        system_charset_info);
 
17823
          }
 
17824
        }
 
17825
      }
 
17826
      if (tmp1.length())
 
17827
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
 
17828
      else
 
17829
        item_list.push_back(item_null);
 
17830
 
 
17831
      /* Build "key", "key_len", and "ref" values and add them to item_list */
 
17832
      if (tab->ref.key_parts)
 
17833
      {
 
17834
        KEY *key_info=table->key_info+ tab->ref.key;
 
17835
        register uint length;
 
17836
        item_list.push_back(new Item_string(key_info->name,
 
17837
                                            strlen(key_info->name),
 
17838
                                            system_charset_info));
 
17839
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) - 
 
17840
                keylen_str_buf;
 
17841
        item_list.push_back(new Item_string(keylen_str_buf, length,
 
17842
                                            system_charset_info));
 
17843
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
 
17844
        {
 
17845
          if (tmp2.length())
 
17846
            tmp2.append(',');
 
17847
          tmp2.append((*ref)->name(), strlen((*ref)->name()),
 
17848
                      system_charset_info);
 
17849
        }
 
17850
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
17851
      }
 
17852
      else if (tab->type == JT_NEXT)
 
17853
      {
 
17854
        KEY *key_info=table->key_info+ tab->index;
 
17855
        register uint length;
 
17856
        item_list.push_back(new Item_string(key_info->name,
 
17857
                                            strlen(key_info->name),cs));
 
17858
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) - 
 
17859
                keylen_str_buf;
 
17860
        item_list.push_back(new Item_string(keylen_str_buf, 
 
17861
                                            length,
 
17862
                                            system_charset_info));
 
17863
        item_list.push_back(item_null);
 
17864
      }
 
17865
      else if (tab->select && tab->select->quick)
 
17866
      {
 
17867
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
 
17868
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
17869
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
 
17870
        item_list.push_back(item_null);
 
17871
      }
 
17872
      else
 
17873
      {
 
17874
        if (table_list->schema_table &&
 
17875
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
17876
        {
 
17877
          const char *tmp_buff;
 
17878
          int f_idx;
 
17879
          if (table_list->has_db_lookup_value)
 
17880
          {
 
17881
            f_idx= table_list->schema_table->idx_field1;
 
17882
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
17883
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
17884
          }          
 
17885
          if (table_list->has_table_lookup_value)
 
17886
          {
 
17887
            if (table_list->has_db_lookup_value)
 
17888
              tmp2.append(',');
 
17889
            f_idx= table_list->schema_table->idx_field2;
 
17890
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
17891
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
17892
          }
 
17893
          if (tmp2.length())
 
17894
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
17895
          else
 
17896
            item_list.push_back(item_null);
 
17897
        }
 
17898
        else
 
17899
          item_list.push_back(item_null);
 
17900
        item_list.push_back(item_null);
 
17901
        item_list.push_back(item_null);
 
17902
      }
 
17903
      
 
17904
      /* Add "rows" field to item_list. */
 
17905
      if (table_list->schema_table)
 
17906
      {
 
17907
        /* in_rows */
 
17908
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
17909
          item_list.push_back(item_null);
 
17910
        /* rows */
 
17911
        item_list.push_back(item_null);
 
17912
      }
 
17913
      else
 
17914
      {
 
17915
        double examined_rows;
 
17916
        if (tab->select && tab->select->quick)
 
17917
          examined_rows= rows2double(tab->select->quick->records);
 
17918
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
 
17919
          examined_rows= rows2double(tab->limit ? tab->limit : 
 
17920
                                     tab->table->file->records());
 
17921
        else
 
17922
          examined_rows= join->best_positions[i].records_read; 
 
17923
 
 
17924
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows, 
 
17925
                                         MY_INT64_NUM_DECIMAL_DIGITS));
 
17926
 
 
17927
        /* Add "filtered" field to item_list. */
 
17928
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
17929
        {
 
17930
          float f= 0.0; 
 
17931
          if (examined_rows)
 
17932
            f= (float) (100.0 * join->best_positions[i].records_read /
 
17933
                        examined_rows);
 
17934
          item_list.push_back(new Item_float(f, 2));
 
17935
        }
 
17936
      }
 
17937
 
 
17938
      /* Build "Extra" field and add it to item_list. */
 
17939
      bool key_read=table->key_read;
 
17940
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
 
17941
          table->covering_keys.is_set(tab->index))
 
17942
        key_read=1;
 
17943
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
 
17944
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
 
17945
        key_read=1;
 
17946
        
 
17947
      if (tab->info)
 
17948
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
 
17949
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
 
17950
      {
 
17951
        if (tab->packed_info & TAB_INFO_USING_INDEX)
 
17952
          extra.append(STRING_WITH_LEN("; Using index"));
 
17953
        if (tab->packed_info & TAB_INFO_USING_WHERE)
 
17954
          extra.append(STRING_WITH_LEN("; Using where"));
 
17955
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
 
17956
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
17957
        /* Skip initial "; "*/
 
17958
        const char *str= extra.ptr();
 
17959
        uint32 len= extra.length();
 
17960
        if (len)
 
17961
        {
 
17962
          str += 2;
 
17963
          len -= 2;
 
17964
        }
 
17965
        item_list.push_back(new Item_string(str, len, cs));
 
17966
      }
 
17967
      else
 
17968
      {
 
17969
        uint keyno= MAX_KEY;
 
17970
        if (tab->ref.key_parts)
 
17971
          keyno= tab->ref.key;
 
17972
        else if (tab->select && tab->select->quick)
 
17973
          keyno = tab->select->quick->index;
 
17974
 
 
17975
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
 
17976
            table->file->pushed_idx_cond)
 
17977
          extra.append(STRING_WITH_LEN("; Using index condition"));
 
17978
 
 
17979
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
 
17980
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
17981
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
 
17982
        {
 
17983
          extra.append(STRING_WITH_LEN("; Using "));
 
17984
          tab->select->quick->add_info_string(&extra);
 
17985
        }
 
17986
          if (tab->select)
 
17987
        {
 
17988
          if (tab->use_quick == 2)
 
17989
          {
 
17990
            /* 4 bits per 1 hex digit + terminating '\0' */
 
17991
            char buf[MAX_KEY / 4 + 1];
 
17992
            extra.append(STRING_WITH_LEN("; Range checked for each "
 
17993
                                         "record (index map: 0x"));
 
17994
            extra.append(tab->keys.print(buf));
 
17995
            extra.append(')');
 
17996
          }
 
17997
          else if (tab->select->cond)
 
17998
          {
 
17999
            const COND *pushed_cond= tab->table->file->pushed_cond;
 
18000
 
 
18001
            if (thd->variables.engine_condition_pushdown && pushed_cond)
 
18002
            {
 
18003
              extra.append(STRING_WITH_LEN("; Using where with pushed "
 
18004
                                           "condition"));
 
18005
              if (thd->lex->describe & DESCRIBE_EXTENDED)
 
18006
              {
 
18007
                extra.append(STRING_WITH_LEN(": "));
 
18008
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
 
18009
              }
 
18010
            }
 
18011
            else
 
18012
              extra.append(STRING_WITH_LEN("; Using where"));
 
18013
          }
 
18014
        }
 
18015
        if (key_read)
 
18016
        {
 
18017
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
18018
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
 
18019
          else
 
18020
            extra.append(STRING_WITH_LEN("; Using index"));
 
18021
        }
 
18022
        if (table->reginfo.not_exists_optimize)
 
18023
          extra.append(STRING_WITH_LEN("; Not exists"));
 
18024
          
 
18025
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
 
18026
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
 
18027
             HA_MRR_USE_DEFAULT_IMPL))
 
18028
        {
 
18029
          extra.append(STRING_WITH_LEN("; Using MRR"));
 
18030
        }
 
18031
 
 
18032
        if (table_list->schema_table &&
 
18033
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
18034
        {
 
18035
          if (!table_list->table_open_method)
 
18036
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
 
18037
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
 
18038
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
 
18039
          else
 
18040
            extra.append(STRING_WITH_LEN("; Open_full_table"));
 
18041
          if (table_list->has_db_lookup_value &&
 
18042
              table_list->has_table_lookup_value)
 
18043
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
 
18044
          else if (table_list->has_db_lookup_value ||
 
18045
                   table_list->has_table_lookup_value)
 
18046
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
 
18047
          else
 
18048
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
 
18049
        }
 
18050
        if (need_tmp_table)
 
18051
        {
 
18052
          need_tmp_table=0;
 
18053
          extra.append(STRING_WITH_LEN("; Using temporary"));
 
18054
        }
 
18055
        if (need_order)
 
18056
        {
 
18057
          need_order=0;
 
18058
          extra.append(STRING_WITH_LEN("; Using filesort"));
 
18059
        }
 
18060
        if (distinct & test_all_bits(used_tables,thd->used_tables))
 
18061
          extra.append(STRING_WITH_LEN("; Distinct"));
 
18062
 
 
18063
        if (tab->insideout_match_tab)
 
18064
        {
 
18065
          extra.append(STRING_WITH_LEN("; LooseScan"));
 
18066
        }
 
18067
 
 
18068
        if (tab->flush_weedout_table)
 
18069
          extra.append(STRING_WITH_LEN("; Start temporary"));
 
18070
        else if (tab->check_weed_out_table)
 
18071
          extra.append(STRING_WITH_LEN("; End temporary"));
 
18072
        else if (tab->do_firstmatch)
 
18073
        {
 
18074
          extra.append(STRING_WITH_LEN("; FirstMatch("));
 
18075
          TABLE *prev_table=tab->do_firstmatch->table;
 
18076
          if (prev_table->derived_select_number)
 
18077
          {
 
18078
            char namebuf[NAME_LEN];
 
18079
            /* Derived table name generation */
 
18080
            int len= snprintf(namebuf, sizeof(namebuf)-1,
 
18081
                              "<derived%u>",
 
18082
                              prev_table->derived_select_number);
 
18083
            extra.append(namebuf, len);
 
18084
          }
 
18085
          else
 
18086
            extra.append(prev_table->pos_in_table_list->alias);
 
18087
          extra.append(STRING_WITH_LEN(")"));
 
18088
        }
 
18089
 
 
18090
        for (uint part= 0; part < tab->ref.key_parts; part++)
 
18091
        {
 
18092
          if (tab->ref.cond_guards[part])
 
18093
          {
 
18094
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
18095
            break;
 
18096
          }
 
18097
        }
 
18098
 
 
18099
        if (i > 0 && tab[-1].next_select == sub_select_cache)
 
18100
          extra.append(STRING_WITH_LEN("; Using join buffer"));
 
18101
 
 
18102
        /* Skip initial "; "*/
 
18103
        const char *str= extra.ptr();
 
18104
        uint32 len= extra.length();
 
18105
        if (len)
 
18106
        {
 
18107
          str += 2;
 
18108
          len -= 2;
 
18109
        }
 
18110
        item_list.push_back(new Item_string(str, len, cs));
 
18111
      }
 
18112
      // For next iteration
 
18113
      used_tables|=table->map;
 
18114
      if (result->send_data(item_list))
 
18115
        join->error= 1;
 
18116
    }
 
18117
  }
 
18118
  for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
 
18119
       unit;
 
18120
       unit= unit->next_unit())
 
18121
  {
 
18122
    if (mysql_explain_union(thd, unit, result))
 
18123
      return;
 
18124
  }
 
18125
  return;
 
18126
}
 
18127
 
 
18128
 
 
18129
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
 
18130
{
 
18131
  bool res= 0;
 
18132
  SELECT_LEX *first= unit->first_select();
 
18133
 
 
18134
  for (SELECT_LEX *sl= first;
 
18135
       sl;
 
18136
       sl= sl->next_select())
 
18137
  {
 
18138
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
 
18139
    uint8 uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
 
18140
    sl->type= (((&thd->lex->select_lex)==sl)?
 
18141
               (sl->first_inner_unit() || sl->next_select() ? 
 
18142
                "PRIMARY" : "SIMPLE"):
 
18143
               ((sl == first)?
 
18144
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
 
18145
                 "DERIVED":
 
18146
                 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
18147
                  "DEPENDENT SUBQUERY":
 
18148
                  (uncacheable?"UNCACHEABLE SUBQUERY":
 
18149
                   "SUBQUERY"))):
 
18150
                ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
18151
                 "DEPENDENT UNION":
 
18152
                 uncacheable?"UNCACHEABLE UNION":
 
18153
                 "UNION")));
 
18154
    sl->options|= SELECT_DESCRIBE;
 
18155
  }
 
18156
  if (unit->is_union())
 
18157
  {
 
18158
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
 
18159
    unit->fake_select_lex->type= "UNION RESULT";
 
18160
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
 
18161
    if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
 
18162
      res= unit->exec();
 
18163
    res|= unit->cleanup();
 
18164
  }
 
18165
  else
 
18166
  {
 
18167
    thd->lex->current_select= first;
 
18168
    unit->set_limit(unit->global_parameters);
 
18169
    res= mysql_select(thd, &first->ref_pointer_array,
 
18170
                        (TABLE_LIST*) first->table_list.first,
 
18171
                        first->with_wild, first->item_list,
 
18172
                        first->where,
 
18173
                        first->order_list.elements +
 
18174
                        first->group_list.elements,
 
18175
                        (ORDER*) first->order_list.first,
 
18176
                        (ORDER*) first->group_list.first,
 
18177
                        first->having,
 
18178
                        (ORDER*) thd->lex->proc_list.first,
 
18179
                        first->options | thd->options | SELECT_DESCRIBE,
 
18180
                        result, unit, first);
 
18181
  }
 
18182
  return(res || thd->is_error());
 
18183
}
 
18184
 
 
18185
 
 
18186
static void print_table_array(THD *thd, String *str, TABLE_LIST **table, 
 
18187
                              TABLE_LIST **end)
 
18188
{
 
18189
  (*table)->print(thd, str, QT_ORDINARY);
 
18190
 
 
18191
  for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
 
18192
  {
 
18193
    TABLE_LIST *curr= *tbl;
6265
18194
    if (curr->outer_join)
6266
18195
    {
6267
18196
      /* MySQL converts right to left joins */
6269
18198
    }
6270
18199
    else if (curr->straight)
6271
18200
      str->append(STRING_WITH_LEN(" straight_join "));
 
18201
    else if (curr->sj_inner_tables)
 
18202
      str->append(STRING_WITH_LEN(" semi join "));
6272
18203
    else
6273
18204
      str->append(STRING_WITH_LEN(" join "));
6274
 
    curr->print(session, str, QT_ORDINARY);
 
18205
    curr->print(thd, str, QT_ORDINARY);
6275
18206
    if (curr->on_expr)
6276
18207
    {
6277
18208
      str->append(STRING_WITH_LEN(" on("));
6281
18212
  }
6282
18213
}
6283
18214
 
 
18215
 
6284
18216
/**
6285
18217
  Print joins from the FROM clause.
6286
 
  @param session     thread Cursor
 
18218
  @param thd     thread handler
6287
18219
  @param str     string where table should be printed
6288
18220
  @param tables  list of tables in join
6289
18221
  @query_type    type of the query is being generated
6290
18222
*/
6291
 
void print_join(Session *session, String *str,
6292
 
                List<TableList> *tables, enum_query_type)
 
18223
 
 
18224
static void print_join(THD *thd,
 
18225
                       String *str,
 
18226
                       List<TABLE_LIST> *tables,
 
18227
                       enum_query_type query_type __attribute__((__unused__)))
6293
18228
{
6294
18229
  /* List is reversed => we should reverse it before using */
6295
 
  List<TableList>::iterator ti(tables->begin());
6296
 
  TableList **table= (TableList **)session->getMemRoot()->allocate(sizeof(TableList*) *
 
18230
  List_iterator_fast<TABLE_LIST> ti(*tables);
 
18231
  TABLE_LIST **table= (TABLE_LIST **)thd->alloc(sizeof(TABLE_LIST*) *
6297
18232
                                                tables->elements);
6298
18233
  if (table == 0)
6299
18234
    return;  // out of memory
6300
18235
 
6301
 
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
 
18236
  for (TABLE_LIST **t= table + (tables->elements - 1); t >= table; t--)
6302
18237
    *t= ti++;
 
18238
  
 
18239
  /* 
 
18240
    If the first table is a semi-join nest, swap it with something that is
 
18241
    not a semi-join nest.
 
18242
  */
 
18243
  if ((*table)->sj_inner_tables)
 
18244
  {
 
18245
    TABLE_LIST **end= table + tables->elements;
 
18246
    for (TABLE_LIST **t2= table; t2!=end; t2++)
 
18247
    {
 
18248
      if (!(*t2)->sj_inner_tables)
 
18249
      {
 
18250
        TABLE_LIST *tmp= *t2;
 
18251
        *t2= *table;
 
18252
        *table= tmp;
 
18253
        break;
 
18254
      }
 
18255
    }
 
18256
  }
6303
18257
  assert(tables->elements >= 1);
6304
 
  print_table_array(session, str, table, table + tables->elements);
6305
 
}
6306
 
 
6307
 
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
6308
 
{
6309
 
  /* QQ: session may not be set for sub queries, but this should be fixed */
6310
 
  if(not session)
6311
 
    session= current_session;
6312
 
 
 
18258
  print_table_array(thd, str, table, table + tables->elements);
 
18259
}
 
18260
 
 
18261
 
 
18262
/**
 
18263
  @brief Print an index hint
 
18264
 
 
18265
  @details Prints out the USE|FORCE|IGNORE index hint.
 
18266
 
 
18267
  @param      thd         the current thread
 
18268
  @param[out] str         appends the index hint here
 
18269
  @param      hint        what the hint is (as string : "USE INDEX"|
 
18270
                          "FORCE INDEX"|"IGNORE INDEX")
 
18271
  @param      hint_length the length of the string in 'hint'
 
18272
  @param      indexes     a list of index names for the hint
 
18273
*/
 
18274
 
 
18275
void 
 
18276
Index_hint::print(THD *thd, String *str)
 
18277
{
 
18278
  switch (type)
 
18279
  {
 
18280
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
 
18281
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
 
18282
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
 
18283
  }
 
18284
  str->append (STRING_WITH_LEN(" ("));
 
18285
  if (key_name.length)
 
18286
  {
 
18287
    if (thd && !my_strnncoll(system_charset_info,
 
18288
                             (const uchar *)key_name.str, key_name.length, 
 
18289
                             (const uchar *)primary_key_name, 
 
18290
                             strlen(primary_key_name)))
 
18291
      str->append(primary_key_name);
 
18292
    else
 
18293
      append_identifier(thd, str, key_name.str, key_name.length);
 
18294
  }
 
18295
  str->append(')');
 
18296
}
 
18297
 
 
18298
 
 
18299
/**
 
18300
  Print table as it should be in join list.
 
18301
 
 
18302
  @param str   string where table should be printed
 
18303
*/
 
18304
 
 
18305
void TABLE_LIST::print(THD *thd, String *str, enum_query_type query_type)
 
18306
{
 
18307
  if (nested_join)
 
18308
  {
 
18309
    str->append('(');
 
18310
    print_join(thd, str, &nested_join->join_list, query_type);
 
18311
    str->append(')');
 
18312
  }
 
18313
  else
 
18314
  {
 
18315
    const char *cmp_name;                         // Name to compare with alias
 
18316
    if (derived)
 
18317
    {
 
18318
      // A derived table
 
18319
      str->append('(');
 
18320
      derived->print(str, query_type);
 
18321
      str->append(')');
 
18322
      cmp_name= "";                               // Force printing of alias
 
18323
    }
 
18324
    else
 
18325
    {
 
18326
      // A normal table
 
18327
      {
 
18328
        append_identifier(thd, str, db, db_length);
 
18329
        str->append('.');
 
18330
      }
 
18331
      if (schema_table)
 
18332
      {
 
18333
        append_identifier(thd, str, schema_table_name,
 
18334
                          strlen(schema_table_name));
 
18335
        cmp_name= schema_table_name;
 
18336
      }
 
18337
      else
 
18338
      {
 
18339
        append_identifier(thd, str, table_name, table_name_length);
 
18340
        cmp_name= table_name;
 
18341
      }
 
18342
    }
 
18343
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
 
18344
    {
 
18345
      char t_alias_buff[MAX_ALIAS_NAME];
 
18346
      const char *t_alias= alias;
 
18347
 
 
18348
      str->append(' ');
 
18349
      if (lower_case_table_names== 1)
 
18350
      {
 
18351
        if (alias && alias[0])
 
18352
        {
 
18353
          strmov(t_alias_buff, alias);
 
18354
          my_casedn_str(files_charset_info, t_alias_buff);
 
18355
          t_alias= t_alias_buff;
 
18356
        }
 
18357
      }
 
18358
 
 
18359
      append_identifier(thd, str, t_alias, strlen(t_alias));
 
18360
    }
 
18361
 
 
18362
    if (index_hints)
 
18363
    {
 
18364
      List_iterator<Index_hint> it(*index_hints);
 
18365
      Index_hint *hint;
 
18366
 
 
18367
      while ((hint= it++))
 
18368
      {
 
18369
        str->append (STRING_WITH_LEN(" "));
 
18370
        hint->print (thd, str);
 
18371
      }
 
18372
    }
 
18373
  }
 
18374
}
 
18375
 
 
18376
 
 
18377
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
 
18378
{
 
18379
  /* QQ: thd may not be set for sub queries, but this should be fixed */
 
18380
  if (!thd)
 
18381
    thd= current_thd;
6313
18382
 
6314
18383
  str->append(STRING_WITH_LEN("select "));
6315
18384
 
6316
18385
  /* First add options */
6317
18386
  if (options & SELECT_STRAIGHT_JOIN)
6318
18387
    str->append(STRING_WITH_LEN("straight_join "));
 
18388
  if ((thd->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
 
18389
      (this == &thd->lex->select_lex))
 
18390
    str->append(STRING_WITH_LEN("high_priority "));
6319
18391
  if (options & SELECT_DISTINCT)
6320
18392
    str->append(STRING_WITH_LEN("distinct "));
6321
18393
  if (options & SELECT_SMALL_RESULT)
6329
18401
 
6330
18402
  //Item List
6331
18403
  bool first= 1;
6332
 
  List<Item>::iterator it(item_list.begin());
 
18404
  List_iterator_fast<Item> it(item_list);
6333
18405
  Item *item;
6334
18406
  while ((item= it++))
6335
18407
  {
6342
18414
 
6343
18415
  /*
6344
18416
    from clause
6345
 
    @todo support USING/FORCE/IGNORE index
 
18417
    TODO: support USING/FORCE/IGNORE index
6346
18418
  */
6347
18419
  if (table_list.elements)
6348
18420
  {
6349
18421
    str->append(STRING_WITH_LEN(" from "));
6350
18422
    /* go through join tree */
6351
 
    print_join(session, str, &top_join_list, query_type);
 
18423
    print_join(thd, str, &top_join_list, query_type);
6352
18424
  }
6353
18425
  else if (where)
6354
18426
  {
6355
18427
    /*
6356
 
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as
 
18428
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as 
6357
18429
      "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
6358
18430
    */
6359
18431
    str->append(STRING_WITH_LEN(" from DUAL "));
6376
18448
  if (group_list.elements)
6377
18449
  {
6378
18450
    str->append(STRING_WITH_LEN(" group by "));
6379
 
    print_order(str, (Order *) group_list.first, query_type);
 
18451
    print_order(str, (ORDER *) group_list.first, query_type);
6380
18452
    switch (olap)
6381
18453
    {
6382
18454
      case CUBE_TYPE:
6407
18479
  if (order_list.elements)
6408
18480
  {
6409
18481
    str->append(STRING_WITH_LEN(" order by "));
6410
 
    print_order(str, (Order *) order_list.first, query_type);
 
18482
    print_order(str, (ORDER *) order_list.first, query_type);
6411
18483
  }
6412
18484
 
6413
18485
  // limit
6414
 
  print_limit(session, str, query_type);
 
18486
  print_limit(thd, str, query_type);
6415
18487
 
6416
18488
  // PROCEDURE unsupported here
6417
18489
}
6418
18490
 
 
18491
 
 
18492
/**
 
18493
  change select_result object of JOIN.
 
18494
 
 
18495
  @param res            new select_result object
 
18496
 
 
18497
  @retval
 
18498
    false   OK
 
18499
  @retval
 
18500
    true    error
 
18501
*/
 
18502
 
 
18503
bool JOIN::change_result(select_result *res)
 
18504
{
 
18505
  result= res;
 
18506
  if (result->prepare(fields_list, select_lex->master_unit()) ||
 
18507
                     result->prepare2())
 
18508
  {
 
18509
    return(true);
 
18510
  }
 
18511
  return(false);
 
18512
}
 
18513
 
6419
18514
/**
6420
18515
  @} (end of group Query_Optimizer)
6421
18516
*/
6422
 
 
6423
 
} /* namespace drizzled */