~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Monty Taylor
  • Date: 2008-10-14 21:20:42 UTC
  • mto: (511.1.4 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081014212042-tef3njx3368b6lwt
Override copy ctr and op= because we have pointer members.

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>
 
26
#include <drizzled/server_includes.h>
 
27
#include <drizzled/sql_select.h>
 
28
#include "sj_tmp_table.h"
 
29
 
 
30
#include <mysys/my_bit.h>
 
31
#include <drizzled/drizzled_error_messages.h>
 
32
#include <libdrizzle/gettext.h>
36
33
#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,
 
34
 
 
35
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
 
36
                              "MAYBE_REF","ALL","range","index",
 
37
                              "ref_or_null","unique_subquery","index_subquery",
 
38
                              "index_merge"
 
39
};
 
40
 
 
41
struct st_sargable_param;
 
42
 
 
43
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
 
44
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds,
 
45
                                 DYNAMIC_ARRAY *keyuse);
 
46
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
 
47
                                JOIN_TAB *join_tab,
 
48
                                uint32_t tables, COND *conds,
 
49
                                COND_EQUAL *cond_equal,
 
50
                                table_map table_map, SELECT_LEX *select_lex,
 
51
                                st_sargable_param **sargables);
 
52
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
 
53
static void set_position(JOIN *join,uint32_t index,JOIN_TAB *table,KEYUSE *key);
 
54
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
55
                               table_map used_tables);
 
56
static bool choose_plan(JOIN *join,table_map join_tables);
 
57
 
 
58
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
 
59
                             table_map remaining_tables, uint32_t idx,
 
60
                             double record_count, double read_time);
 
61
static void optimize_straight_join(JOIN *join, table_map join_tables);
 
62
static bool greedy_search(JOIN *join, table_map remaining_tables,
 
63
                             uint32_t depth, uint32_t prune_level);
 
64
static bool best_extension_by_limited_search(JOIN *join,
 
65
                                             table_map remaining_tables,
 
66
                                             uint32_t idx, double record_count,
 
67
                                             double read_time, uint32_t depth,
 
68
                                             uint32_t prune_level);
 
69
static uint32_t determine_search_depth(JOIN* join);
 
70
static int join_tab_cmp(const void* ptr1, const void* ptr2);
 
71
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
 
72
/*
 
73
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
 
74
  tested and approved.
 
75
*/
 
76
static bool find_best(JOIN *join,table_map rest_tables,uint32_t index,
 
77
                      double record_count,double read_time);
 
78
static uint32_t cache_record_length(JOIN *join,uint32_t index);
 
79
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
 
80
static bool get_best_combination(JOIN *join);
 
81
static store_key *get_store_key(THD *thd,
 
82
                                KEYUSE *keyuse, table_map used_tables,
 
83
                                KEY_PART_INFO *key_part, unsigned char *key_buff,
 
84
                                uint32_t maybe_null);
 
85
static bool make_simple_join(JOIN *join,Table *tmp_table);
 
86
static void make_outerjoin_info(JOIN *join);
 
87
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
 
88
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
 
89
static bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
 
90
static void update_depend_map(JOIN *join);
 
91
static void update_depend_map(JOIN *join, order_st *order);
 
92
static order_st *remove_const(JOIN *join,order_st *first_order,COND *cond,
 
93
                           bool change_list, bool *simple_order);
 
94
static int return_zero_rows(JOIN *join, select_result *res,TableList *tables,
 
95
                            List<Item> &fields, bool send_row,
 
96
                            uint64_t select_options, const char *info,
 
97
                            Item *having);
 
98
static COND *build_equal_items(THD *thd, COND *cond,
79
99
                               COND_EQUAL *inherited,
80
100
                               List<TableList> *join_list,
81
101
                               COND_EQUAL **cond_equal_ref);
82
 
 
 
102
static COND* substitute_for_best_equal_field(COND *cond,
 
103
                                             COND_EQUAL *cond_equal,
 
104
                                             void *table_join_idx);
 
105
static COND *simplify_joins(JOIN *join, List<TableList> *join_list,
 
106
                            COND *conds, bool top, bool in_sj);
 
107
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
 
108
static void restore_prev_nj_state(JOIN_TAB *last);
 
109
static void reset_nj_counters(List<TableList> *join_list);
 
110
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
 
111
                                          uint32_t first_unused);
 
112
 
 
113
static 
 
114
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
 
115
static void restore_prev_sj_state(const table_map remaining_tables, 
 
116
                                  const JOIN_TAB *tab);
 
117
 
 
118
static COND *optimize_cond(JOIN *join, COND *conds,
 
119
                           List<TableList> *join_list,
 
120
                           Item::cond_result *cond_value);
 
121
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
 
122
static int do_select(JOIN *join,List<Item> *fields,Table *tmp_table);
 
123
 
 
124
static enum_nested_loop_state
 
125
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
126
                     int error);
 
127
static enum_nested_loop_state
 
128
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
 
129
static enum_nested_loop_state
 
130
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
 
131
static enum_nested_loop_state
 
132
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
133
static enum_nested_loop_state
 
134
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
135
static enum_nested_loop_state
 
136
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
137
static enum_nested_loop_state
 
138
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
139
 
 
140
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
 
141
static int join_read_system(JOIN_TAB *tab);
 
142
static int join_read_const(JOIN_TAB *tab);
 
143
static int join_read_key(JOIN_TAB *tab);
 
144
static int join_read_always_key(JOIN_TAB *tab);
 
145
static int join_read_last_key(JOIN_TAB *tab);
 
146
static int join_no_more_records(READ_RECORD *info);
 
147
static int join_read_next(READ_RECORD *info);
 
148
static int join_read_next_different(READ_RECORD *info);
 
149
static int join_init_quick_read_record(JOIN_TAB *tab);
 
150
static int test_if_quick_select(JOIN_TAB *tab);
 
151
static int join_init_read_record(JOIN_TAB *tab);
 
152
static int join_read_first(JOIN_TAB *tab);
 
153
static int join_read_next_same(READ_RECORD *info);
 
154
static int join_read_next_same_diff(READ_RECORD *info);
 
155
static int join_read_last(JOIN_TAB *tab);
 
156
static int join_read_prev_same(READ_RECORD *info);
 
157
static int join_read_prev(READ_RECORD *info);
 
158
int join_read_always_key_or_null(JOIN_TAB *tab);
 
159
int join_read_next_same_or_null(READ_RECORD *info);
 
160
static COND *make_cond_for_table(COND *cond,table_map table,
 
161
                                 table_map used_table,
 
162
                                 bool exclude_expensive_cond);
83
163
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);
 
164
static bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,
 
165
                                    ha_rows select_limit, bool no_changes,
 
166
                                    const key_map *map);
 
167
static bool list_contains_unique_index(Table *table,
 
168
                          bool (*find_func) (Field *, void *), void *data);
 
169
static bool find_field_in_item_list (Field *field, void *data);
 
170
static bool find_field_in_order_list (Field *field, void *data);
 
171
static int create_sort_index(THD *thd, JOIN *join, order_st *order,
 
172
                             ha_rows filesort_limit, ha_rows select_limit,
 
173
                             bool is_order_by);
 
174
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields,
 
175
                             Item *having);
 
176
static int remove_dup_with_compare(THD *thd, Table *entry, Field **field,
 
177
                                   ulong offset,Item *having);
 
178
static int remove_dup_with_hash_index(THD *thd,Table *table,
 
179
                                      uint32_t field_count, Field **first_field,
92
180
 
93
 
static bool eval_const_cond(COND *cond)
94
 
{
95
 
    return ((Item_func*) cond)->val_int() ? true : false;
96
 
}
 
181
                                      ulong key_length,Item *having);
 
182
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint32_t table_count);
 
183
static ulong used_blob_length(CACHE_FIELD **ptr);
 
184
static bool store_record_in_cache(JOIN_CACHE *cache);
 
185
static void reset_cache_read(JOIN_CACHE *cache);
 
186
static void reset_cache_write(JOIN_CACHE *cache);
 
187
static void read_cached_record(JOIN_TAB *tab);
 
188
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
 
189
static order_st *create_distinct_group(THD *thd, Item **ref_pointer_array,
 
190
                                    order_st *order, List<Item> &fields,
 
191
                                    List<Item> &all_fields,
 
192
                                    bool *all_order_by_fields_used);
 
193
static bool test_if_subpart(order_st *a,order_st *b);
 
194
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
 
195
static void calc_group_buffer(JOIN *join,order_st *group);
 
196
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
 
197
static bool alloc_group_fields(JOIN *join,order_st *group);
 
198
// Create list for using with tempory table
 
199
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
200
                                     List<Item> &new_list1,
 
201
                                     List<Item> &new_list2,
 
202
                                     uint32_t elements, List<Item> &items);
 
203
// Create list for using with tempory table
 
204
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
205
                                      List<Item> &new_list1,
 
206
                                      List<Item> &new_list2,
 
207
                                      uint32_t elements, List<Item> &items);
 
208
static void init_tmptable_sum_functions(Item_sum **func);
 
209
static void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
 
210
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
 
211
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
 
212
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
 
213
static bool init_sum_functions(Item_sum **func, Item_sum **end);
 
214
static bool update_sum_func(Item_sum **func);
 
215
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
 
216
                            bool distinct, const char *message=NULL);
 
217
static Item *remove_additional_cond(Item* conds);
 
218
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
 
219
static bool test_if_ref(Item_field *left_item,Item *right_item);
 
220
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
221
                                       Item *new_cond, bool fix_fields);
97
222
 
98
223
/*
99
224
  This is used to mark equalities that were made from i-th IN-equality.
103
228
const char *subq_sj_cond_name=
104
229
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
105
230
 
106
 
static bool copy_blobs(Field **ptr)
 
231
static bool bitmap_covers(const table_map x, const table_map y)
107
232
{
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;
 
233
  return !test(y & ~x);
115
234
}
116
235
 
117
236
/**
118
237
  This handles SELECT with and without UNION.
119
238
*/
120
 
bool handle_select(Session *session, LEX *lex, select_result *result,
121
 
                   uint64_t setup_tables_done_option)
 
239
 
 
240
bool handle_select(THD *thd, LEX *lex, select_result *result,
 
241
                   ulong setup_tables_done_option)
122
242
{
123
243
  bool res;
124
 
  register Select_Lex *select_lex= &lex->select_lex;
125
 
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
 
244
  register SELECT_LEX *select_lex = &lex->select_lex;
 
245
  DRIZZLE_SELECT_START();
126
246
 
127
 
  if (select_lex->master_unit()->is_union() ||
 
247
  if (select_lex->master_unit()->is_union() || 
128
248
      select_lex->master_unit()->fake_select_lex)
129
 
  {
130
 
    res= drizzle_union(session, lex, result, &lex->unit,
131
 
                       setup_tables_done_option);
132
 
  }
 
249
    res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
133
250
  else
134
251
  {
135
 
    Select_Lex_Unit *unit= &lex->unit;
 
252
    SELECT_LEX_UNIT *unit= &lex->unit;
136
253
    unit->set_limit(unit->global_parameters);
137
 
    session->session_marker= 0;
 
254
    thd->thd_marker= 0;
138
255
    /*
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
 
256
      'options' of mysql_select will be set in JOIN, as far as JOIN for
 
257
      every PS/SP execution new, we will not need reset this flag if 
141
258
      setup_tables_done_option changed for next rexecution
142
259
    */
143
 
    res= select_query(session,
144
 
                      &select_lex->ref_pointer_array,
 
260
    res= mysql_select(thd, &select_lex->ref_pointer_array,
145
261
                      (TableList*) select_lex->table_list.first,
146
 
                      select_lex->with_wild,
147
 
                      select_lex->item_list,
 
262
                      select_lex->with_wild, select_lex->item_list,
148
263
                      select_lex->where,
149
264
                      select_lex->order_list.elements +
150
265
                      select_lex->group_list.elements,
151
 
                      (Order*) select_lex->order_list.first,
152
 
                      (Order*) select_lex->group_list.first,
 
266
                      (order_st*) select_lex->order_list.first,
 
267
                      (order_st*) select_lex->group_list.first,
153
268
                      select_lex->having,
154
 
                      select_lex->options | session->options |
 
269
                      (order_st*) lex->proc_list.first,
 
270
                      select_lex->options | thd->options |
155
271
                      setup_tables_done_option,
156
272
                      result, unit, select_lex);
157
273
  }
158
 
  res|= session->is_error();
 
274
  res|= thd->is_error();
159
275
  if (unlikely(res))
160
276
    result->abort();
161
277
 
162
 
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
163
 
  return res;
 
278
  DRIZZLE_SELECT_END();
 
279
  return(res);
164
280
}
165
281
 
 
282
 
166
283
/*
167
284
  Fix fields referenced from inner selects.
168
285
 
169
286
  SYNOPSIS
170
287
    fix_inner_refs()
171
 
    session               Thread handle
 
288
    thd               Thread handle
172
289
    all_fields        List of all fields used in select
173
290
    select            Current select
174
291
    ref_pointer_array Array of references to Items used in current select
203
320
    true  an error occured
204
321
    false ok
205
322
*/
206
 
bool fix_inner_refs(Session *session, 
207
 
                    List<Item> &all_fields, 
208
 
                    Select_Lex *select, 
209
 
                    Item **ref_pointer_array)
 
323
 
 
324
bool
 
325
fix_inner_refs(THD *thd, List<Item> &all_fields, SELECT_LEX *select,
 
326
                 Item **ref_pointer_array)
210
327
{
211
328
  Item_outer_ref *ref;
212
329
  bool res= false;
213
330
  bool direct_ref= false;
214
331
 
215
 
  List<Item_outer_ref>::iterator ref_it(select->inner_refs_list.begin());
 
332
  List_iterator<Item_outer_ref> ref_it(select->inner_refs_list);
216
333
  while ((ref= ref_it++))
217
334
  {
218
335
    Item *item= ref->outer_ref;
219
336
    Item **item_ref= ref->ref;
220
337
    Item_ref *new_ref;
221
338
    /*
222
 
      @todo this field item already might be present in the select list.
 
339
      TODO: this field item already might be present in the select list.
223
340
      In this case instead of adding new field item we could use an
224
341
      existing one. The change will lead to less operations for copying fields,
225
342
      smaller temporary tables and less data passed through filesort.
266
383
    ref->outer_ref= new_ref;
267
384
    ref->ref= &ref->outer_ref;
268
385
 
269
 
    if (!ref->fixed && ref->fix_fields(session, 0))
 
386
    if (!ref->fixed && ref->fix_fields(thd, 0))
270
387
      return true;
271
 
    session->used_tables|= item->used_tables();
 
388
    thd->used_tables|= item->used_tables();
272
389
  }
273
390
  return res;
274
391
}
275
392
 
 
393
#define MAGIC_IN_WHERE_TOP_LEVEL 10
 
394
/**
 
395
  Function to setup clauses without sum functions.
 
396
*/
 
397
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
 
398
                               TableList *tables,
 
399
                               TableList *leaves,
 
400
                               List<Item> &fields,
 
401
                               List<Item> &all_fields,
 
402
                               COND **conds,
 
403
                               order_st *order,
 
404
                               order_st *group, bool *hidden_group_fields)
 
405
{
 
406
  int res;
 
407
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
 
408
 
 
409
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
410
  res= setup_conds(thd, tables, leaves, conds);
 
411
 
 
412
  thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
 
413
  res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
 
414
                          order);
 
415
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
416
  res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
 
417
                          group, hidden_group_fields);
 
418
  thd->lex->allow_sum_func= save_allow_sum_func;
 
419
  return(res);
 
420
}
 
421
 
276
422
/*****************************************************************************
277
423
  Check fields, find best join, do the select and output fields.
278
 
  select_query assumes that all tables are already opened
 
424
  mysql_select assumes that all tables are already opened
279
425
*****************************************************************************/
280
426
 
 
427
/**
 
428
  Prepare of whole select (including sub queries in future).
 
429
 
 
430
  @todo
 
431
    Add check of calculation of GROUP functions and fields:
 
432
    SELECT COUNT(*)+table.col1 from table1;
 
433
 
 
434
  @retval
 
435
    -1   on error
 
436
  @retval
 
437
    0   on success
 
438
*/
 
439
int
 
440
JOIN::prepare(Item ***rref_pointer_array,
 
441
              TableList *tables_init,
 
442
              uint32_t wild_num, COND *conds_init, uint32_t og_num,
 
443
              order_st *order_init, order_st *group_init,
 
444
              Item *having_init,
 
445
              order_st *proc_param_init, SELECT_LEX *select_lex_arg,
 
446
              SELECT_LEX_UNIT *unit_arg)
 
447
{
 
448
  // to prevent double initialization on EXPLAIN
 
449
  if (optimized)
 
450
    return(0);
 
451
 
 
452
  conds= conds_init;
 
453
  order= order_init;
 
454
  group_list= group_init;
 
455
  having= having_init;
 
456
  proc_param= proc_param_init;
 
457
  tables_list= tables_init;
 
458
  select_lex= select_lex_arg;
 
459
  select_lex->join= this;
 
460
  join_list= &select_lex->top_join_list;
 
461
  union_part= unit_arg->is_union();
 
462
 
 
463
  thd->lex->current_select->is_item_list_lookup= 1;
 
464
  /*
 
465
    If we have already executed SELECT, then it have not sense to prevent
 
466
    its table from update (see unique_table())
 
467
  */
 
468
  if (thd->derived_tables_processing)
 
469
    select_lex->exclude_from_table_unique_test= true;
 
470
 
 
471
  /* Check that all tables, fields, conds and order are ok */
 
472
 
 
473
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
 
474
      setup_tables_and_check_access(thd, &select_lex->context, join_list,
 
475
                                    tables_list, &select_lex->leaf_tables,
 
476
                                    false))
 
477
      return(-1);
 
478
 
 
479
  TableList *table_ptr;
 
480
  for (table_ptr= select_lex->leaf_tables;
 
481
       table_ptr;
 
482
       table_ptr= table_ptr->next_leaf)
 
483
    tables++;
 
484
 
 
485
  if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
 
486
      select_lex->setup_ref_array(thd, og_num) ||
 
487
      setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
 
488
                   &all_fields, 1) ||
 
489
      setup_without_group(thd, (*rref_pointer_array), tables_list,
 
490
                          select_lex->leaf_tables, fields_list,
 
491
                          all_fields, &conds, order, group_list,
 
492
                          &hidden_group_fields))
 
493
    return(-1);                         /* purecov: inspected */
 
494
 
 
495
  ref_pointer_array= *rref_pointer_array;
 
496
  
 
497
  if (having)
 
498
  {
 
499
    nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
 
500
    thd->where="having clause";
 
501
    thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
502
    select_lex->having_fix_field= 1;
 
503
    bool having_fix_rc= (!having->fixed &&
 
504
                         (having->fix_fields(thd, &having) ||
 
505
                          having->check_cols(1)));
 
506
    select_lex->having_fix_field= 0;
 
507
    if (having_fix_rc || thd->is_error())
 
508
      return(-1);                               /* purecov: inspected */
 
509
    thd->lex->allow_sum_func= save_allow_sum_func;
 
510
  }
 
511
 
 
512
  {
 
513
    Item_subselect *subselect;
 
514
    Item_in_subselect *in_subs= NULL;
 
515
    /*
 
516
      Are we in a subquery predicate?
 
517
      TODO: the block below will be executed for every PS execution without need.
 
518
    */
 
519
    if ((subselect= select_lex->master_unit()->item))
 
520
    {
 
521
      bool do_semijoin= !test(thd->variables.optimizer_switch &
 
522
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
 
523
      if (subselect->substype() == Item_subselect::IN_SUBS)
 
524
        in_subs= (Item_in_subselect*)subselect;
 
525
 
 
526
      /*
 
527
        Check if we're in subquery that is a candidate for flattening into a
 
528
        semi-join (which is done done in flatten_subqueries()). The
 
529
        requirements are:
 
530
          1. Subquery predicate is an IN/=ANY subq predicate
 
531
          2. Subquery is a single SELECT (not a UNION)
 
532
          3. Subquery does not have GROUP BY or order_st BY
 
533
          4. Subquery does not use aggregate functions or HAVING
 
534
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
 
535
          6. No execution method was already chosen (by a prepared statement).
 
536
 
 
537
          (*). We are not in a subquery of a single table UPDATE/DELETE that 
 
538
               doesn't have a JOIN (TODO: We should handle this at some
 
539
               point by switching to multi-table UPDATE/DELETE)
 
540
 
 
541
          (**). We're not in a confluent table-less subquery, like
 
542
                "SELECT 1". 
 
543
      */
 
544
      if (in_subs &&                                                    // 1
 
545
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
546
          !select_lex->group_list.elements && !order &&                 // 3
 
547
          !having && !select_lex->with_sum_func &&                      // 4
 
548
          thd->thd_marker &&                                            // 5
 
549
          select_lex->outer_select()->join &&                           // (*)
 
550
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**) 
 
551
          do_semijoin &&
 
552
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
 
553
      {
 
554
        {
 
555
          if (!in_subs->left_expr->fixed &&
 
556
               in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
 
557
          {
 
558
            return(-1);
 
559
          }
 
560
          /*
 
561
            Check that the right part of the subselect contains no more than one
 
562
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
 
563
          */
 
564
          if (subselect->substype() == Item_subselect::IN_SUBS &&
 
565
             (select_lex->item_list.elements != 
 
566
              ((Item_in_subselect*)subselect)->left_expr->cols()))
 
567
          {
 
568
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
 
569
            return(-1);
 
570
          }
 
571
        }
 
572
 
 
573
        /* Register the subquery for further processing */
 
574
        select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
 
575
        in_subs->expr_join_nest= (TableList*)thd->thd_marker;
 
576
      }
 
577
      else
 
578
      {
 
579
        bool do_materialize= !test(thd->variables.optimizer_switch &
 
580
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
 
581
        /*
 
582
          Check if the subquery predicate can be executed via materialization.
 
583
          The required conditions are:
 
584
          1. Subquery predicate is an IN/=ANY subq predicate
 
585
          2. Subquery is a single SELECT (not a UNION)
 
586
          3. Subquery is not a table-less query. In this case there is no
 
587
             point in materializing.
 
588
          4. Subquery predicate is a top-level predicate
 
589
             (this implies it is not negated)
 
590
             TODO: this is a limitation that should be lifeted once we
 
591
             implement correct NULL semantics (WL#3830)
 
592
          5. Subquery is non-correlated
 
593
             TODO:
 
594
             This is an overly restrictive condition. It can be extended to:
 
595
             (Subquery is non-correlated ||
 
596
              Subquery is correlated to any query outer to IN predicate ||
 
597
              (Subquery is correlated to the immediate outer query &&
 
598
               Subquery !contains {GROUP BY, order_st BY [LIMIT],
 
599
               aggregate functions) && subquery predicate is not under "NOT IN"))
 
600
          6. No execution method was already chosen (by a prepared statement).
 
601
 
 
602
          (*) The subquery must be part of a SELECT statement. The current
 
603
               condition also excludes multi-table update statements.
 
604
 
 
605
          We have to determine whether we will perform subquery materialization
 
606
          before calling the IN=>EXISTS transformation, so that we know whether to
 
607
          perform the whole transformation or only that part of it which wraps
 
608
          Item_in_subselect in an Item_in_optimizer.
 
609
        */
 
610
        if (do_materialize && 
 
611
            in_subs  &&                                                   // 1
 
612
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
613
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
 
614
            thd->lex->sql_command == SQLCOM_SELECT)                       // *
 
615
        {
 
616
          if (in_subs->is_top_level_item() &&                             // 4
 
617
              !in_subs->is_correlated &&                                  // 5
 
618
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
 
619
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
 
620
        }
 
621
 
 
622
        Item_subselect::trans_res trans_res;
 
623
        if ((trans_res= subselect->select_transformer(this)) !=
 
624
            Item_subselect::RES_OK)
 
625
        {
 
626
          return((trans_res == Item_subselect::RES_ERROR));
 
627
        }
 
628
      }
 
629
    }
 
630
  }
 
631
 
 
632
  if (order)
 
633
  {
 
634
    order_st *ord;
 
635
    for (ord= order; ord; ord= ord->next)
 
636
    {
 
637
      Item *item= *ord->item;
 
638
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
 
639
        item->split_sum_func(thd, ref_pointer_array, all_fields);
 
640
    }
 
641
  }
 
642
 
 
643
  if (having && having->with_sum_func)
 
644
    having->split_sum_func2(thd, ref_pointer_array, all_fields,
 
645
                            &having, true);
 
646
  if (select_lex->inner_sum_func_list)
 
647
  {
 
648
    Item_sum *end=select_lex->inner_sum_func_list;
 
649
    Item_sum *item_sum= end;  
 
650
    do
 
651
    { 
 
652
      item_sum= item_sum->next;
 
653
      item_sum->split_sum_func2(thd, ref_pointer_array,
 
654
                                all_fields, item_sum->ref_by, false);
 
655
    } while (item_sum != end);
 
656
  }
 
657
 
 
658
  if (select_lex->inner_refs_list.elements &&
 
659
      fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
 
660
    return(-1);
 
661
 
 
662
  /*
 
663
    Check if there are references to un-aggregated columns when computing 
 
664
    aggregate functions with implicit grouping (there is no GROUP BY).
 
665
 
 
666
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
 
667
  */
 
668
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
 
669
  {
 
670
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
 
671
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
 
672
    return(-1);
 
673
  }
 
674
  {
 
675
    /* Caclulate the number of groups */
 
676
    send_group_parts= 0;
 
677
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
678
      send_group_parts++;
 
679
  }
 
680
  
 
681
  if (error)
 
682
    goto err;                                   /* purecov: inspected */
 
683
 
 
684
  if (result && result->prepare(fields_list, unit_arg))
 
685
    goto err;                                   /* purecov: inspected */
 
686
 
 
687
  /* Init join struct */
 
688
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
689
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
690
  this->group= group_list != 0;
 
691
  unit= unit_arg;
 
692
 
 
693
#ifdef RESTRICTED_GROUP
 
694
  if (sum_func_count && !group_list && (func_count || field_count))
 
695
  {
 
696
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
 
697
    goto err;
 
698
  }
 
699
#endif
 
700
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
 
701
    goto err;
 
702
  if (alloc_func_list())
 
703
    goto err;
 
704
 
 
705
  return(0); // All OK
 
706
 
 
707
err:
 
708
  return(-1);                           /* purecov: inspected */
 
709
}
 
710
 
 
711
 
 
712
/*
 
713
  Remove the predicates pushed down into the subquery
 
714
 
 
715
  SYNOPSIS
 
716
    JOIN::remove_subq_pushed_predicates()
 
717
      where   IN  Must be NULL
 
718
              OUT The remaining WHERE condition, or NULL
 
719
 
 
720
  DESCRIPTION
 
721
    Given that this join will be executed using (unique|index)_subquery,
 
722
    without "checking NULL", remove the predicates that were pushed down
 
723
    into the subquery.
 
724
 
 
725
    If the subquery compares scalar values, we can remove the condition that
 
726
    was wrapped into trig_cond (it will be checked when needed by the subquery
 
727
    engine)
 
728
 
 
729
    If the subquery compares row values, we need to keep the wrapped
 
730
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
 
731
    and non-NULL values, we'll do a full table scan and will rely on the
 
732
    equalities corresponding to non-NULL parts of left tuple to filter out
 
733
    non-matching records.
 
734
 
 
735
    TODO: We can remove the equalities that will be guaranteed to be true by the
 
736
    fact that subquery engine will be using index lookup. This must be done only
 
737
    for cases where there are no conversion errors of significance, e.g. 257
 
738
    that is searched in a byte. But this requires homogenization of the return 
 
739
    codes of all Field*::store() methods.
 
740
*/
 
741
 
 
742
void JOIN::remove_subq_pushed_predicates(Item **where)
 
743
{
 
744
  if (conds->type() == Item::FUNC_ITEM &&
 
745
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
 
746
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
 
747
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
 
748
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
 
749
                   ((Item_func *)conds)->arguments()[0]))
 
750
  {
 
751
    *where= 0;
 
752
    return;
 
753
  }
 
754
}
 
755
 
 
756
 
281
757
/*
282
758
  Index lookup-based subquery: save some flags for EXPLAIN output
283
759
 
290
766
  DESCRIPTION
291
767
    For index lookup-based subquery (i.e. one executed with
292
768
    subselect_uniquesubquery_engine or subselect_indexsubquery_engine),
293
 
    check its EXPLAIN output row should contain
294
 
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL)
 
769
    check its EXPLAIN output row should contain 
 
770
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL) 
295
771
      "Using Where" (TAB_INFO_USING_WHERE)
296
772
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
297
773
    and set appropriate flags in join_tab->packed_info.
298
774
*/
299
 
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
 
775
 
 
776
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
300
777
{
301
778
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
302
 
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
 
779
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
303
780
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
304
781
  if (where)
305
782
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
313
790
  }
314
791
}
315
792
 
 
793
 
 
794
 
 
795
 
 
796
/*
 
797
  Check if the table's rowid is included in the temptable
 
798
 
 
799
  SYNOPSIS
 
800
    sj_table_is_included()
 
801
      join      The join
 
802
      join_tab  The table to be checked
 
803
 
 
804
  DESCRIPTION
 
805
    SemiJoinDuplicateElimination: check the table's rowid should be included
 
806
    in the temptable. This is so if
 
807
 
 
808
    1. The table is not embedded within some semi-join nest
 
809
    2. The has been pulled out of a semi-join nest, or
 
810
 
 
811
    3. The table is functionally dependent on some previous table
 
812
 
 
813
    [4. This is also true for constant tables that can't be
 
814
        NULL-complemented but this function is not called for such tables]
 
815
 
 
816
  RETURN
 
817
    true  - Include table's rowid
 
818
    false - Don't
 
819
*/
 
820
 
 
821
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
 
822
{
 
823
  if (join_tab->emb_sj_nest)
 
824
    return false;
 
825
  
 
826
  /* Check if this table is functionally dependent on the tables that
 
827
     are within the same outer join nest
 
828
  */
 
829
  TableList *embedding= join_tab->table->pos_in_table_list->embedding;
 
830
  if (join_tab->type == JT_EQ_REF)
 
831
  {
 
832
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
 
833
    uint32_t idx;
 
834
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
 
835
    {
 
836
      JOIN_TAB *ref_tab= join->join_tab + idx;
 
837
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
 
838
        return true;
 
839
    }
 
840
    /* Ok, functionally dependent */
 
841
    return false;
 
842
  }
 
843
  /* Not functionally dependent => need to include*/
 
844
  return true;
 
845
}
 
846
 
 
847
 
 
848
/*
 
849
  Setup the strategies to eliminate semi-join duplicates.
 
850
  
 
851
  SYNOPSIS
 
852
    setup_semijoin_dups_elimination()
 
853
      join           Join to process
 
854
      options        Join options (needed to see if join buffering will be 
 
855
                     used or not)
 
856
      no_jbuf_after  Another bit of information re where join buffering will
 
857
                     be used.
 
858
 
 
859
  DESCRIPTION
 
860
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
 
861
    strategies:
 
862
 
 
863
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
 
864
                         of row combinations)
 
865
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
 
866
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
 
867
                  together and picking the 1st one)
 
868
    
 
869
    The join order has "duplicate-generating ranges", and every range is
 
870
    served by one strategy or a combination of FirstMatch with with some
 
871
    other strategy.
 
872
    
 
873
    "Duplicate-generating range" is defined as a range within the join order
 
874
    that contains all of the inner tables of a semi-join. All ranges must be
 
875
    disjoint, if tables of several semi-joins are interleaved, then the ranges
 
876
    are joined together, which is equivalent to converting
 
877
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
 
878
    to
 
879
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
 
880
    .
 
881
 
 
882
    Applicability conditions are as follows:
 
883
 
 
884
    DuplicateWeedout strategy
 
885
    ~~~~~~~~~~~~~~~~~~~~~~~~~
 
886
 
 
887
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
 
888
      +------+  +=========================+  +---+
 
889
        (1)                 (2)               (3)
 
890
 
 
891
       (1) - Prefix of OuterTables (those that participate in 
 
892
             IN-equality and/or are correlated with subquery) and outer 
 
893
             Noncorrelated Tables.
 
894
       (2) - The handled range. The range starts with the first sj-inner
 
895
             table, and covers all sj-inner and outer tables 
 
896
             Within the range,  Inner, Outer, outer Noncorrelated tables
 
897
             may follow in any order.
 
898
       (3) - The suffix of outer Noncorrelated tables.
 
899
    
 
900
    FirstMatch strategy
 
901
    ~~~~~~~~~~~~~~~~~~~
 
902
 
 
903
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
 
904
      +------+  +==================+  +---+
 
905
        (1)             (2)          (3)
 
906
 
 
907
      (1) - Prefix of outer and non-correlated tables
 
908
      (2) - The handled range, which may contain only inner and
 
909
            non-correlated tables.
 
910
      (3) - The suffix of outer Noncorrelated tables.
 
911
 
 
912
    InsideOut strategy 
 
913
    ~~~~~~~~~~~~~~~~~~
 
914
 
 
915
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
 
916
     +--------+   +===========+ +=============+   +------+
 
917
        (1)           (2)          (3)              (4)
 
918
     
 
919
      (1) - Prefix that may contain any outer tables. The prefix must contain
 
920
            all the non-trivially correlated outer tables. (non-trivially means
 
921
            that the correlation is not just through the IN-equality).
 
922
      
 
923
      (2) - Inner table for which the InsideOut scan is performed.
 
924
 
 
925
      (3) - The remainder of the duplicate-generating range. It is served by 
 
926
            application of FirstMatch strategy, with the exception that
 
927
            outer IN-correlated tables are considered to be non-correlated.
 
928
 
 
929
      (4) - THe suffix of outer and outer non-correlated tables.
 
930
 
 
931
    If several strategies are applicable, their relative priorities are:
 
932
      1. InsideOut
 
933
      2. FirstMatch 
 
934
      3. DuplicateWeedout
 
935
 
 
936
    This function walks over the join order and sets up the strategies by
 
937
    setting appropriate members in join_tab structures.
 
938
 
 
939
  RETURN
 
940
    false  OK 
 
941
    true   Out of memory error
 
942
*/
 
943
 
 
944
static
 
945
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
946
{
 
947
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
948
  struct {
 
949
    /* 
 
950
      0 - invalid (EOF marker), 
 
951
      1 - InsideOut, 
 
952
      2 - Temptable (maybe confluent),
 
953
      3 - Temptable with join buffering
 
954
    */
 
955
    uint32_t strategy;
 
956
    uint32_t start_idx; /* Left range bound */
 
957
    uint32_t end_idx;   /* Right range bound */
 
958
    /* 
 
959
      For Temptable strategy: Bitmap of all outer and correlated tables from 
 
960
      all involved join nests.
 
961
    */
 
962
    table_map outer_tables;
 
963
  } dups_ranges [MAX_TABLES];
 
964
 
 
965
  TableList *emb_insideout_nest= NULL;
 
966
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
 
967
                               tables) whose ranges we're in */
 
968
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
 
969
  table_map range_start_map= 0; /* table_map at current range start */
 
970
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
 
971
  int cur_range= 0;
 
972
  uint32_t i;
 
973
 
 
974
  /*
 
975
    First pass: locate the duplicate-generating ranges and pick the strategies.
 
976
  */
 
977
  for (i=join->const_tables ; i < join->tables ; i++)
 
978
  {
 
979
    JOIN_TAB *tab=join->join_tab+i;
 
980
    Table *table=tab->table;
 
981
    cur_map |= table->map;
 
982
 
 
983
    if (tab->emb_sj_nest) // Encountered an sj-inner table
 
984
    {
 
985
      if (!emb_sj_map)
 
986
      {
 
987
        dups_ranges[cur_range].start_idx= i;
 
988
        range_start_map= cur_map & ~table->map;
 
989
        /*
 
990
          Remember if this is a possible start of range that is covered by
 
991
          the InsideOut strategy (the reason that it is not covered could
 
992
          be that it overlaps with anther semi-join's range. we don't
 
993
          support InsideOut for joined ranges)
 
994
        */
 
995
        if (join->best_positions[i].use_insideout_scan)
 
996
          emb_insideout_nest= tab->emb_sj_nest;
 
997
      }
 
998
 
 
999
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
 
1000
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
 
1001
 
 
1002
      if (tab->emb_sj_nest != emb_insideout_nest)
 
1003
      {
 
1004
        /*
 
1005
          Two different semi-joins interleave. This cannot be handled by
 
1006
          InsideOut strategy.
 
1007
        */
 
1008
        emb_insideout_nest= NULL;
 
1009
      }
 
1010
    }
 
1011
 
 
1012
    if (emb_sj_map) /* We're in duplicate-generating range */
 
1013
    {
 
1014
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
1015
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
 
1016
          i <= no_jbuf_after && !dealing_with_jbuf)
 
1017
      {
 
1018
        /*
 
1019
          This table uses join buffering, which makes use of FirstMatch or 
 
1020
          InsideOut strategies impossible for the current and (we assume) 
 
1021
          preceding duplicate-producing ranges.
 
1022
          That is, for the join order:
 
1023
 
 
1024
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
 
1025
                  |     |     |     |          | \
 
1026
                  +-----+     +-----+          |  join buffering use
 
1027
                     r1          r2         we're here
 
1028
 
 
1029
          we'll have to remove r1 and r2 and use duplicate-elimination
 
1030
          strategy that spans all the tables, starting from the very 1st
 
1031
          one.
 
1032
        */
 
1033
        dealing_with_jbuf= true;
 
1034
        emb_insideout_nest= false;
 
1035
 
 
1036
        /* 
 
1037
          Absorb all preceding duplicate-eliminating ranges. Their strategies
 
1038
          do not matter: 
 
1039
        */
 
1040
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
 
1041
        {
 
1042
          dups_ranges[cur_range].outer_tables |= 
 
1043
            dups_ranges[prev_range].outer_tables;
 
1044
        }
 
1045
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
 
1046
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
 
1047
        cur_range=  0;
 
1048
      }
 
1049
 
 
1050
      /*
 
1051
        Check if we are at the end of duplicate-producing range. We are if
 
1052
 
 
1053
        1. It's an InsideOut range (which presumes all correlated tables are
 
1054
           in the prefix), and all inner tables are in the join order prefix,
 
1055
           or
 
1056
        2. It's a DuplicateElimination range (possibly covering several
 
1057
           SJ-nests), and all inner, outer, and correlated tables of all 
 
1058
           sj-nests are in the join order prefix.
 
1059
      */
 
1060
      bool end_of_range= false;
 
1061
      if (emb_insideout_nest && 
 
1062
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
 
1063
      {
 
1064
        /* Save that this range is handled with InsideOut: */
 
1065
        dups_ranges[cur_range].strategy= 1;
 
1066
        end_of_range= true;
 
1067
      }
 
1068
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
 
1069
      {
 
1070
        /*
 
1071
          This is a complete range to be handled with either DuplicateWeedout 
 
1072
          or FirstMatch
 
1073
        */
 
1074
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
 
1075
        /* 
 
1076
          This will hold tables from within the range that need to be put 
 
1077
          into the join buffer before we can use the FirstMatch on its tail.
 
1078
        */
 
1079
        dups_ranges[cur_range].outer_tables= emb_outer_tables & 
 
1080
                                             ~range_start_map;
 
1081
        end_of_range= true;
 
1082
      }
 
1083
 
 
1084
      if (end_of_range)
 
1085
      {
 
1086
        dups_ranges[cur_range].end_idx= i+1;
 
1087
        emb_sj_map= emb_outer_tables= 0;
 
1088
        emb_insideout_nest= NULL;
 
1089
        dealing_with_jbuf= false;
 
1090
        dups_ranges[++cur_range].strategy= 0;
 
1091
      }
 
1092
    }
 
1093
  }
 
1094
 
 
1095
  THD *thd= join->thd;
 
1096
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
 
1097
  /*
 
1098
    Second pass: setup the chosen strategies    
 
1099
  */
 
1100
  for (int j= 0; j < cur_range; j++)
 
1101
  {
 
1102
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
 
1103
    JOIN_TAB *jump_to;
 
1104
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
 
1105
    {
 
1106
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
 
1107
      jump_to= tab++;
 
1108
    }
 
1109
    else // DuplicateWeedout strategy
 
1110
    {
 
1111
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
 
1112
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
1113
      uint32_t jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
1114
      uint32_t jt_null_bits= 0;    // # null bits in tuple bytes
 
1115
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
 
1116
      uint32_t rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1117
      JOIN_TAB *last_outer_tab= tab - 1;
 
1118
      /*
 
1119
        Walk through the range and remember
 
1120
         - tables that need their rowids to be put into temptable
 
1121
         - the last outer table
 
1122
      */
 
1123
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1124
      {
 
1125
        if (sj_table_is_included(join, tab))
 
1126
        {
 
1127
          last_tab->join_tab= tab;
 
1128
          last_tab->rowid_offset= jt_rowid_offset;
 
1129
          jt_rowid_offset += tab->table->file->ref_length;
 
1130
          if (tab->table->maybe_null)
 
1131
          {
 
1132
            last_tab->null_byte= jt_null_bits / 8;
 
1133
            last_tab->null_bit= jt_null_bits++;
 
1134
          }
 
1135
          last_tab++;
 
1136
          tab->table->prepare_for_position();
 
1137
          tab->rowid_keep_flags= rowid_keep_flags;
 
1138
        }
 
1139
        cur_map |= tab->table->map;
 
1140
        if (!tab->emb_sj_nest && bitmap_covers(cur_map, 
 
1141
                                               dups_ranges[j].outer_tables))
 
1142
          last_outer_tab= tab;
 
1143
      }
 
1144
 
 
1145
      if (jt_rowid_offset) /* Temptable has at least one rowid */
 
1146
      {
 
1147
        SJ_TMP_TABLE *sjtbl;
 
1148
        uint32_t tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1149
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
 
1150
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
 
1151
          return(true);
 
1152
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
 
1153
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
 
1154
        sjtbl->rowid_len= jt_rowid_offset;
 
1155
        sjtbl->null_bits= jt_null_bits;
 
1156
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
 
1157
 
 
1158
        *next_sjtbl_ptr= sjtbl;
 
1159
        next_sjtbl_ptr= &(sjtbl->next);
 
1160
        sjtbl->next= NULL;
 
1161
 
 
1162
        sjtbl->tmp_table= 
 
1163
          create_duplicate_weedout_tmp_table(thd, 
 
1164
                                             sjtbl->rowid_len + 
 
1165
                                             sjtbl->null_bytes,
 
1166
                                             sjtbl);
 
1167
 
 
1168
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
 
1169
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
 
1170
      }
 
1171
      tab= last_outer_tab + 1;
 
1172
      jump_to= last_outer_tab;
 
1173
    }
 
1174
 
 
1175
    /* Create the FirstMatch tail */
 
1176
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1177
    {
 
1178
      if (tab->emb_sj_nest)
 
1179
        tab->do_firstmatch= jump_to; 
 
1180
      else
 
1181
        jump_to= tab;
 
1182
    }
 
1183
  }
 
1184
  return(false);
 
1185
}
 
1186
 
 
1187
 
 
1188
static void cleanup_sj_tmp_tables(JOIN *join)
 
1189
{
 
1190
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl; 
 
1191
       sj_tbl= sj_tbl->next)
 
1192
  {
 
1193
    if (sj_tbl->tmp_table)
 
1194
    {
 
1195
      sj_tbl->tmp_table->free_tmp_table(join->thd);
 
1196
    }
 
1197
  }
 
1198
  join->sj_tmp_tables= NULL;
 
1199
}
 
1200
 
 
1201
uint32_t make_join_orderinfo(JOIN *join);
 
1202
 
 
1203
/**
 
1204
  global select optimisation.
 
1205
 
 
1206
  @note
 
1207
    error code saved in field 'error'
 
1208
 
 
1209
  @retval
 
1210
    0   success
 
1211
  @retval
 
1212
    1   error
 
1213
*/
 
1214
 
 
1215
int
 
1216
JOIN::optimize()
 
1217
{
 
1218
  // to prevent double initialization on EXPLAIN
 
1219
  if (optimized)
 
1220
    return(0);
 
1221
  optimized= 1;
 
1222
 
 
1223
  thd->set_proc_info("optimizing");
 
1224
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
 
1225
              unit->select_limit_cnt);
 
1226
  /* select_limit is used to decide if we are likely to scan the whole table */
 
1227
  select_limit= unit->select_limit_cnt;
 
1228
  if (having || (select_options & OPTION_FOUND_ROWS))
 
1229
    select_limit= HA_POS_ERROR;
 
1230
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
 
1231
  // Ignore errors of execution if option IGNORE present
 
1232
  if (thd->lex->ignore)
 
1233
    thd->lex->current_select->no_error= 1;
 
1234
 
 
1235
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
 
1236
  /* Add HAVING to WHERE if possible */
 
1237
  if (having && !group_list && !sum_func_count)
 
1238
  {
 
1239
    if (!conds)
 
1240
    {
 
1241
      conds= having;
 
1242
      having= 0;
 
1243
    }
 
1244
    else if ((conds=new Item_cond_and(conds,having)))
 
1245
    {
 
1246
      /*
 
1247
        Item_cond_and can't be fixed after creation, so we do not check
 
1248
        conds->fixed
 
1249
      */
 
1250
      conds->fix_fields(thd, &conds);
 
1251
      conds->change_ref_to_fields(thd, tables_list);
 
1252
      conds->top_level_item();
 
1253
      having= 0;
 
1254
    }
 
1255
  }
 
1256
#endif
 
1257
 
 
1258
  /* Convert all outer joins to inner joins if possible */
 
1259
  conds= simplify_joins(this, join_list, conds, true, false);
 
1260
  build_bitmap_for_nested_joins(join_list, 0);
 
1261
 
 
1262
  conds= optimize_cond(this, conds, join_list, &cond_value);   
 
1263
  if (thd->is_error())
 
1264
  {
 
1265
    error= 1;
 
1266
    return(1);
 
1267
  }
 
1268
 
 
1269
  {
 
1270
    having= optimize_cond(this, having, join_list, &having_value);
 
1271
    if (thd->is_error())
 
1272
    {
 
1273
      error= 1;
 
1274
      return(1);
 
1275
    }
 
1276
    if (select_lex->where)
 
1277
      select_lex->cond_value= cond_value;
 
1278
    if (select_lex->having)
 
1279
      select_lex->having_value= having_value;
 
1280
 
 
1281
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 
 
1282
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
 
1283
    {                                           /* Impossible cond */
 
1284
      zero_result_cause=  having_value == Item::COND_FALSE ?
 
1285
                           "Impossible HAVING" : "Impossible WHERE";
 
1286
      error= 0;
 
1287
      return(0);
 
1288
    }
 
1289
  }
 
1290
 
 
1291
  /* Optimize count(*), cmin() and cmax() */
 
1292
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
 
1293
  {
 
1294
    int res;
 
1295
    /*
 
1296
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
 
1297
      to the WHERE conditions,
 
1298
      or 1 if all items were resolved,
 
1299
      or 0, or an error number HA_ERR_...
 
1300
    */
 
1301
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
 
1302
    {
 
1303
      if (res == HA_ERR_KEY_NOT_FOUND)
 
1304
      {
 
1305
        zero_result_cause= "No matching min/max row";
 
1306
        error=0;
 
1307
        return(0);
 
1308
      }
 
1309
      if (res > 1)
 
1310
      {
 
1311
        error= res;
 
1312
        return(1);
 
1313
      }
 
1314
      if (res < 0)
 
1315
      {
 
1316
        zero_result_cause= "No matching min/max row";
 
1317
        error=0;
 
1318
        return(0);
 
1319
      }
 
1320
      zero_result_cause= "Select tables optimized away";
 
1321
      tables_list= 0;                           // All tables resolved
 
1322
      /*
 
1323
        Extract all table-independent conditions and replace the WHERE
 
1324
        clause with them. All other conditions were computed by opt_sum_query
 
1325
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
 
1326
        so there is no need to compute the whole WHERE clause again.
 
1327
        Notice that make_cond_for_table() will always succeed to remove all
 
1328
        computed conditions, because opt_sum_query() is applicable only to
 
1329
        conjunctions.
 
1330
        Preserve conditions for EXPLAIN.
 
1331
      */
 
1332
      if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
 
1333
      {
 
1334
        COND *table_independent_conds=
 
1335
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
1336
        conds= table_independent_conds;
 
1337
      }
 
1338
    }
 
1339
  }
 
1340
  if (!tables_list)
 
1341
  {
 
1342
    error= 0;
 
1343
    return(0);
 
1344
  }
 
1345
  error= -1;                                    // Error is sent to client
 
1346
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
 
1347
 
 
1348
  /* Calculate how to do the join */
 
1349
  thd->set_proc_info("statistics");
 
1350
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
 
1351
      thd->is_fatal_error)
 
1352
  {
 
1353
    return(1);
 
1354
  }
 
1355
 
 
1356
  /* Remove distinct if only const tables */
 
1357
  select_distinct= select_distinct && (const_tables != tables);
 
1358
  thd->set_proc_info("preparing");
 
1359
  if (result->initialize_tables(this))
 
1360
  {
 
1361
    return(1);                          // error == -1
 
1362
  }
 
1363
  if (const_table_map != found_const_table_map &&
 
1364
      !(select_options & SELECT_DESCRIBE) &&
 
1365
      (!conds ||
 
1366
       !(conds->used_tables() & RAND_TABLE_BIT) ||
 
1367
       select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
 
1368
  {
 
1369
    zero_result_cause= "no matching row in const table";
 
1370
    error= 0;
 
1371
    return(0);
 
1372
  }
 
1373
  if (!(thd->options & OPTION_BIG_SELECTS) &&
 
1374
      best_read > (double) thd->variables.max_join_size &&
 
1375
      !(select_options & SELECT_DESCRIBE))
 
1376
  {                                             /* purecov: inspected */
 
1377
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
 
1378
    error= -1;
 
1379
    return(1);
 
1380
  }
 
1381
  if (const_tables && !thd->locked_tables &&
 
1382
      !(select_options & SELECT_NO_UNLOCK))
 
1383
    mysql_unlock_some_tables(thd, table, const_tables);
 
1384
  if (!conds && outer_join)
 
1385
  {
 
1386
    /* Handle the case where we have an OUTER JOIN without a WHERE */
 
1387
    conds=new Item_int((int64_t) 1,1);  // Always true
 
1388
  }
 
1389
  select= make_select(*table, const_table_map,
 
1390
                      const_table_map, conds, 1, &error);
 
1391
  if (error)
 
1392
  {                                             /* purecov: inspected */
 
1393
    error= -1;                                  /* purecov: inspected */
 
1394
    return(1);
 
1395
  }
 
1396
  
 
1397
  reset_nj_counters(join_list);
 
1398
  make_outerjoin_info(this);
 
1399
 
 
1400
  /*
 
1401
    Among the equal fields belonging to the same multiple equality
 
1402
    choose the one that is to be retrieved first and substitute
 
1403
    all references to these in where condition for a reference for
 
1404
    the selected field.
 
1405
  */
 
1406
  if (conds)
 
1407
  {
 
1408
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
 
1409
    conds->update_used_tables();
 
1410
  }
 
1411
 
 
1412
  /*
 
1413
    Permorm the the optimization on fields evaluation mentioned above
 
1414
    for all on expressions.
 
1415
  */ 
 
1416
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
 
1417
  {
 
1418
    if (*tab->on_expr_ref)
 
1419
    {
 
1420
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
 
1421
                                                         tab->cond_equal,
 
1422
                                                         map2table);
 
1423
      (*tab->on_expr_ref)->update_used_tables();
 
1424
    }
 
1425
  }
 
1426
 
 
1427
  if (conds &&!outer_join && const_table_map != found_const_table_map && 
 
1428
      (select_options & SELECT_DESCRIBE) &&
 
1429
      select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
 
1430
  {
 
1431
    conds=new Item_int((int64_t) 0,1);  // Always false
 
1432
  }
 
1433
  if (make_join_select(this, select, conds))
 
1434
  {
 
1435
    zero_result_cause=
 
1436
      "Impossible WHERE noticed after reading const tables";
 
1437
    return(0);                          // error == 0
 
1438
  }
 
1439
 
 
1440
  error= -1;                                    /* if goto err */
 
1441
 
 
1442
  /* Optimize distinct away if possible */
 
1443
  {
 
1444
    order_st *org_order= order;
 
1445
    order=remove_const(this, order,conds,1, &simple_order);
 
1446
    if (thd->is_error())
 
1447
    {
 
1448
      error= 1;
 
1449
      return(1);
 
1450
    }
 
1451
 
 
1452
    /*
 
1453
      If we are using order_st BY NULL or order_st BY const_expression,
 
1454
      return result in any order (even if we are using a GROUP BY)
 
1455
    */
 
1456
    if (!order && org_order)
 
1457
      skip_sort_order= 1;
 
1458
  }
 
1459
  /*
 
1460
     Check if we can optimize away GROUP BY/DISTINCT.
 
1461
     We can do that if there are no aggregate functions, the
 
1462
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
 
1463
     (if present) contain direct references to all key parts of
 
1464
     an unique index (in whatever order) and if the key parts of the
 
1465
     unique index cannot contain NULLs.
 
1466
     Note that the unique keys for DISTINCT and GROUP BY should not
 
1467
     be the same (as long as they are unique).
 
1468
 
 
1469
     The FROM clause must contain a single non-constant table.
 
1470
  */
 
1471
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
 
1472
      !tmp_table_param.sum_func_count &&
 
1473
      (!join_tab[const_tables].select ||
 
1474
       !join_tab[const_tables].select->quick ||
 
1475
       join_tab[const_tables].select->quick->get_type() != 
 
1476
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
 
1477
  {
 
1478
    if (group_list &&
 
1479
       list_contains_unique_index(join_tab[const_tables].table,
 
1480
                                 find_field_in_order_list,
 
1481
                                 (void *) group_list))
 
1482
    {
 
1483
      /*
 
1484
        We have found that grouping can be removed since groups correspond to
 
1485
        only one row anyway, but we still have to guarantee correct result
 
1486
        order. The line below effectively rewrites the query from GROUP BY
 
1487
        <fields> to order_st BY <fields>. There are two exceptions:
 
1488
        - if skip_sort_order is set (see above), then we can simply skip
 
1489
          GROUP BY;
 
1490
        - we can only rewrite order_st BY if the order_st BY fields are 'compatible'
 
1491
          with the GROUP BY ones, i.e. either one is a prefix of another.
 
1492
          We only check if the order_st BY is a prefix of GROUP BY. In this case
 
1493
          test_if_subpart() copies the ASC/DESC attributes from the original
 
1494
          order_st BY fields.
 
1495
          If GROUP BY is a prefix of order_st BY, then it is safe to leave
 
1496
          'order' as is.
 
1497
       */
 
1498
      if (!order || test_if_subpart(group_list, order))
 
1499
          order= skip_sort_order ? 0 : group_list;
 
1500
      /*
 
1501
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be 
 
1502
        rewritten to IGNORE INDEX FOR order_st BY(fields).
 
1503
      */
 
1504
      join_tab->table->keys_in_use_for_order_by=
 
1505
        join_tab->table->keys_in_use_for_group_by;
 
1506
      group_list= 0;
 
1507
      group= 0;
 
1508
    }
 
1509
    if (select_distinct &&
 
1510
       list_contains_unique_index(join_tab[const_tables].table,
 
1511
                                 find_field_in_item_list,
 
1512
                                 (void *) &fields_list))
 
1513
    {
 
1514
      select_distinct= 0;
 
1515
    }
 
1516
  }
 
1517
  if (group_list || tmp_table_param.sum_func_count)
 
1518
  {
 
1519
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
1520
      select_distinct=0;
 
1521
  }
 
1522
  else if (select_distinct && tables - const_tables == 1)
 
1523
  {
 
1524
    /*
 
1525
      We are only using one table. In this case we change DISTINCT to a
 
1526
      GROUP BY query if:
 
1527
      - The GROUP BY can be done through indexes (no sort) and the order_st
 
1528
        BY only uses selected fields.
 
1529
        (In this case we can later optimize away GROUP BY and order_st BY)
 
1530
      - We are scanning the whole table without LIMIT
 
1531
        This can happen if:
 
1532
        - We are using CALC_FOUND_ROWS
 
1533
        - We are using an order_st BY that can't be optimized away.
 
1534
 
 
1535
      We don't want to use this optimization when we are using LIMIT
 
1536
      because in this case we can just create a temporary table that
 
1537
      holds LIMIT rows and stop when this table is full.
 
1538
    */
 
1539
    JOIN_TAB *tab= &join_tab[const_tables];
 
1540
    bool all_order_fields_used;
 
1541
    if (order)
 
1542
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1, 
 
1543
        &tab->table->keys_in_use_for_order_by);
 
1544
    if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
 
1545
                                          order, fields_list, all_fields,
 
1546
                                          &all_order_fields_used)))
 
1547
    {
 
1548
      bool skip_group= (skip_sort_order &&
 
1549
        test_if_skip_sort_order(tab, group_list, select_limit, 1, 
 
1550
                                &tab->table->keys_in_use_for_group_by) != 0);
 
1551
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
1552
      if ((skip_group && all_order_fields_used) ||
 
1553
          select_limit == HA_POS_ERROR ||
 
1554
          (order && !skip_sort_order))
 
1555
      {
 
1556
        /*  Change DISTINCT to GROUP BY */
 
1557
        select_distinct= 0;
 
1558
        no_order= !order;
 
1559
        if (all_order_fields_used)
 
1560
        {
 
1561
          if (order && skip_sort_order)
 
1562
          {
 
1563
            /*
 
1564
              Force MySQL to read the table in sorted order to get result in
 
1565
              order_st BY order.
 
1566
            */
 
1567
            tmp_table_param.quick_group=0;
 
1568
          }
 
1569
          order=0;
 
1570
        }
 
1571
        group=1;                                // For end_write_group
 
1572
      }
 
1573
      else
 
1574
        group_list= 0;
 
1575
    }
 
1576
    else if (thd->is_fatal_error)                       // End of memory
 
1577
      return(1);
 
1578
  }
 
1579
  simple_group= 0;
 
1580
  {
 
1581
    order_st *old_group_list;
 
1582
    group_list= remove_const(this, (old_group_list= group_list), conds,
 
1583
                             rollup.state == ROLLUP::STATE_NONE,
 
1584
                             &simple_group);
 
1585
    if (thd->is_error())
 
1586
    {
 
1587
      error= 1;
 
1588
      return(1);
 
1589
    }
 
1590
    if (old_group_list && !group_list)
 
1591
      select_distinct= 0;
 
1592
  }
 
1593
  if (!group_list && group)
 
1594
  {
 
1595
    order=0;                                    // The output has only one row
 
1596
    simple_order=1;
 
1597
    select_distinct= 0;                       // No need in distinct for 1 row
 
1598
    group_optimized_away= 1;
 
1599
  }
 
1600
 
 
1601
  calc_group_buffer(this, group_list);
 
1602
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
 
1603
 
 
1604
  if (test_if_subpart(group_list, order) ||
 
1605
      (!group_list && tmp_table_param.sum_func_count))
 
1606
    order=0;
 
1607
 
 
1608
  // Can't use sort on head table if using row cache
 
1609
  if (full_join)
 
1610
  {
 
1611
    if (group_list)
 
1612
      simple_group=0;
 
1613
    if (order)
 
1614
      simple_order=0;
 
1615
  }
 
1616
 
 
1617
  /*
 
1618
    Check if we need to create a temporary table.
 
1619
    This has to be done if all tables are not already read (const tables)
 
1620
    and one of the following conditions holds:
 
1621
    - We are using DISTINCT (simple distinct's are already optimized away)
 
1622
    - We are using an order_st BY or GROUP BY on fields not in the first table
 
1623
    - We are using different order_st BY and GROUP BY orders
 
1624
    - The user wants us to buffer the result.
 
1625
  */
 
1626
  need_tmp= (const_tables != tables &&
 
1627
             ((select_distinct || !simple_order || !simple_group) ||
 
1628
              (group_list && order) ||
 
1629
              test(select_options & OPTION_BUFFER_RESULT)));
 
1630
 
 
1631
  uint32_t no_jbuf_after= make_join_orderinfo(this);
 
1632
  uint64_t select_opts_for_readinfo= 
 
1633
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
 
1634
 
 
1635
  sj_tmp_tables= NULL;
 
1636
  if (!select_lex->sj_nests.is_empty())
 
1637
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
 
1638
                                    no_jbuf_after);
 
1639
 
 
1640
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
 
1641
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
 
1642
    return(1);
 
1643
 
 
1644
  /* Create all structures needed for materialized subquery execution. */
 
1645
  if (setup_subquery_materialization())
 
1646
    return(1);
 
1647
 
 
1648
  /*
 
1649
    is this simple IN subquery?
 
1650
  */
 
1651
  if (!group_list && !order &&
 
1652
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
 
1653
      tables == 1 && conds &&
 
1654
      !unit->is_union())
 
1655
  {
 
1656
    if (!having)
 
1657
    {
 
1658
      Item *where= conds;
 
1659
      if (join_tab[0].type == JT_EQ_REF &&
 
1660
          join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1661
      {
 
1662
        remove_subq_pushed_predicates(&where);
 
1663
        save_index_subquery_explain_info(join_tab, where);
 
1664
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
 
1665
        error= 0;
 
1666
        return(unit->item->
 
1667
                    change_engine(new
 
1668
                                  subselect_uniquesubquery_engine(thd,
 
1669
                                                                  join_tab,
 
1670
                                                                  unit->item,
 
1671
                                                                  where)));
 
1672
      }
 
1673
      else if (join_tab[0].type == JT_REF &&
 
1674
               join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1675
      {
 
1676
        remove_subq_pushed_predicates(&where);
 
1677
        save_index_subquery_explain_info(join_tab, where);
 
1678
        join_tab[0].type= JT_INDEX_SUBQUERY;
 
1679
        error= 0;
 
1680
        return(unit->item->
 
1681
                    change_engine(new
 
1682
                                  subselect_indexsubquery_engine(thd,
 
1683
                                                                 join_tab,
 
1684
                                                                 unit->item,
 
1685
                                                                 where,
 
1686
                                                                 NULL,
 
1687
                                                                 0)));
 
1688
      }
 
1689
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
 
1690
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
 
1691
               having->name == in_having_cond)
 
1692
    {
 
1693
      join_tab[0].type= JT_INDEX_SUBQUERY;
 
1694
      error= 0;
 
1695
      conds= remove_additional_cond(conds);
 
1696
      save_index_subquery_explain_info(join_tab, conds);
 
1697
      return(unit->item->
 
1698
                  change_engine(new subselect_indexsubquery_engine(thd,
 
1699
                                                                   join_tab,
 
1700
                                                                   unit->item,
 
1701
                                                                   conds,
 
1702
                                                                   having,
 
1703
                                                                   1)));
 
1704
    }
 
1705
 
 
1706
  }
 
1707
  /*
 
1708
    Need to tell handlers that to play it safe, it should fetch all
 
1709
    columns of the primary key of the tables: this is because MySQL may
 
1710
    build row pointers for the rows, and for all columns of the primary key
 
1711
    the read set has not necessarily been set by the server code.
 
1712
  */
 
1713
  if (need_tmp || select_distinct || group_list || order)
 
1714
  {
 
1715
    for (uint32_t i = const_tables; i < tables; i++)
 
1716
      join_tab[i].table->prepare_for_position();
 
1717
  }
 
1718
 
 
1719
  if (const_tables != tables)
 
1720
  {
 
1721
    /*
 
1722
      Because filesort always does a full table scan or a quick range scan
 
1723
      we must add the removed reference to the select for the table.
 
1724
      We only need to do this when we have a simple_order or simple_group
 
1725
      as in other cases the join is done before the sort.
 
1726
    */
 
1727
    if ((order || group_list) &&
 
1728
        (join_tab[const_tables].type != JT_ALL) &&
 
1729
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
 
1730
        ((order && simple_order) || (group_list && simple_group)))
 
1731
    {
 
1732
      if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
 
1733
        return(1);
 
1734
      }
 
1735
    }
 
1736
    
 
1737
    if (!(select_options & SELECT_BIG_RESULT) &&
 
1738
        ((group_list &&
 
1739
          (!simple_group ||
 
1740
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
 
1741
                                    unit->select_limit_cnt, 0, 
 
1742
                                    &join_tab[const_tables].table->
 
1743
                                    keys_in_use_for_group_by))) ||
 
1744
         select_distinct) &&
 
1745
        tmp_table_param.quick_group)
 
1746
    {
 
1747
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
 
1748
    }
 
1749
    if (order)
 
1750
    {
 
1751
      /*
 
1752
        Force using of tmp table if sorting by a SP or UDF function due to
 
1753
        their expensive and probably non-deterministic nature.
 
1754
      */
 
1755
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
1756
      {
 
1757
        Item *item= *tmp_order->item;
 
1758
        if (item->is_expensive())
 
1759
        {
 
1760
          /* Force tmp table without sort */
 
1761
          need_tmp=1; simple_order=simple_group=0;
 
1762
          break;
 
1763
        }
 
1764
      }
 
1765
    }
 
1766
  }
 
1767
 
 
1768
  tmp_having= having;
 
1769
  if (select_options & SELECT_DESCRIBE)
 
1770
  {
 
1771
    error= 0;
 
1772
    return(0);
 
1773
  }
 
1774
  having= 0;
 
1775
 
 
1776
  /*
 
1777
    The loose index scan access method guarantees that all grouping or
 
1778
    duplicate row elimination (for distinct) is already performed
 
1779
    during data retrieval, and that all MIN/MAX functions are already
 
1780
    computed for each group. Thus all MIN/MAX functions should be
 
1781
    treated as regular functions, and there is no need to perform
 
1782
    grouping in the main execution loop.
 
1783
    Notice that currently loose index scan is applicable only for
 
1784
    single table queries, thus it is sufficient to test only the first
 
1785
    join_tab element of the plan for its access method.
 
1786
  */
 
1787
  if (join_tab->is_using_loose_index_scan())
 
1788
    tmp_table_param.precomputed_group_by= true;
 
1789
 
 
1790
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1791
  if (need_tmp)
 
1792
  {
 
1793
    thd->set_proc_info("Creating tmp table");
 
1794
 
 
1795
    init_items_ref_array();
 
1796
 
 
1797
    tmp_table_param.hidden_field_count= (all_fields.elements -
 
1798
                                         fields_list.elements);
 
1799
    order_st *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1800
                                                             (order_st*) 0);
 
1801
    /*
 
1802
      Pushing LIMIT to the temporary table creation is not applicable
 
1803
      when there is order_st BY or GROUP BY or there is no GROUP BY, but
 
1804
      there are aggregate functions, because in all these cases we need
 
1805
      all result rows.
 
1806
    */
 
1807
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
 
1808
                             !tmp_group &&
 
1809
                             !thd->lex->current_select->with_sum_func) ?
 
1810
                            select_limit : HA_POS_ERROR;
 
1811
 
 
1812
    if (!(exec_tmp_table1=
 
1813
          create_tmp_table(thd, &tmp_table_param, all_fields,
 
1814
                           tmp_group,
 
1815
                           group_list ? 0 : select_distinct,
 
1816
                           group_list && simple_group,
 
1817
                           select_options,
 
1818
                           tmp_rows_limit,
 
1819
                           (char *) "")))
 
1820
                {
 
1821
      return(1);
 
1822
    }
 
1823
 
 
1824
    /*
 
1825
      We don't have to store rows in temp table that doesn't match HAVING if:
 
1826
      - we are sorting the table and writing complete group rows to the
 
1827
        temp table.
 
1828
      - We are using DISTINCT without resolving the distinct as a GROUP BY
 
1829
        on all columns.
 
1830
      
 
1831
      If having is not handled here, it will be checked before the row
 
1832
      is sent to the client.
 
1833
    */    
 
1834
    if (tmp_having && 
 
1835
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
 
1836
      having= tmp_having;
 
1837
 
 
1838
    /* if group or order on first table, sort first */
 
1839
    if (group_list && simple_group)
 
1840
    {
 
1841
      thd->set_proc_info("Sorting for group");
 
1842
      if (create_sort_index(thd, this, group_list,
 
1843
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1844
          alloc_group_fields(this, group_list) ||
 
1845
          make_sum_func_list(all_fields, fields_list, 1) ||
 
1846
          setup_sum_funcs(thd, sum_funcs))
 
1847
      {
 
1848
        return(1);
 
1849
      }
 
1850
      group_list=0;
 
1851
    }
 
1852
    else
 
1853
    {
 
1854
      if (make_sum_func_list(all_fields, fields_list, 0) ||
 
1855
          setup_sum_funcs(thd, sum_funcs))
 
1856
      {
 
1857
        return(1);
 
1858
      }
 
1859
 
 
1860
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
 
1861
      {
 
1862
        thd->set_proc_info("Sorting for order");
 
1863
        if (create_sort_index(thd, this, order,
 
1864
                              HA_POS_ERROR, HA_POS_ERROR, true))
 
1865
        {
 
1866
          return(1);
 
1867
        }
 
1868
        order=0;
 
1869
      }
 
1870
    }
 
1871
    
 
1872
    /*
 
1873
      Optimize distinct when used on some of the tables
 
1874
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
 
1875
      In this case we can stop scanning t2 when we have found one t1.a
 
1876
    */
 
1877
 
 
1878
    if (exec_tmp_table1->distinct)
 
1879
    {
 
1880
      table_map used_tables= thd->used_tables;
 
1881
      JOIN_TAB *last_join_tab= join_tab+tables-1;
 
1882
      do
 
1883
      {
 
1884
        if (used_tables & last_join_tab->table->map)
 
1885
          break;
 
1886
        last_join_tab->not_used_in_distinct=1;
 
1887
      } while (last_join_tab-- != join_tab);
 
1888
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
 
1889
      if (order && skip_sort_order)
 
1890
      {
 
1891
        /* Should always succeed */
 
1892
        if (test_if_skip_sort_order(&join_tab[const_tables],
 
1893
                                    order, unit->select_limit_cnt, 0, 
 
1894
                                    &join_tab[const_tables].table->
 
1895
                                      keys_in_use_for_order_by))
 
1896
          order=0;
 
1897
      }
 
1898
    }
 
1899
 
 
1900
    /* 
 
1901
      If this join belongs to an uncacheable subquery save 
 
1902
      the original join 
 
1903
    */
 
1904
    if (select_lex->uncacheable && !is_top_level_join() &&
 
1905
        init_save_join_tab())
 
1906
      return(-1);                         /* purecov: inspected */
 
1907
  }
 
1908
 
 
1909
  error= 0;
 
1910
  return(0);
 
1911
}
 
1912
 
 
1913
 
 
1914
/**
 
1915
  Restore values in temporary join.
 
1916
*/
 
1917
void JOIN::restore_tmp()
 
1918
{
 
1919
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
 
1920
}
 
1921
 
 
1922
 
 
1923
int
 
1924
JOIN::reinit()
 
1925
{
 
1926
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
 
1927
                                    select_lex->offset_limit->val_uint() :
 
1928
                                    0UL);
 
1929
 
 
1930
  first_record= 0;
 
1931
 
 
1932
  if (exec_tmp_table1)
 
1933
  {
 
1934
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
 
1935
    exec_tmp_table1->file->ha_delete_all_rows();
 
1936
    free_io_cache(exec_tmp_table1);
 
1937
    filesort_free_buffers(exec_tmp_table1,0);
 
1938
  }
 
1939
  if (exec_tmp_table2)
 
1940
  {
 
1941
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
 
1942
    exec_tmp_table2->file->ha_delete_all_rows();
 
1943
    free_io_cache(exec_tmp_table2);
 
1944
    filesort_free_buffers(exec_tmp_table2,0);
 
1945
  }
 
1946
  if (items0)
 
1947
    set_items_ref_array(items0);
 
1948
 
 
1949
  if (join_tab_save)
 
1950
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
 
1951
 
 
1952
  if (tmp_join)
 
1953
    restore_tmp();
 
1954
 
 
1955
  /* Reset of sum functions */
 
1956
  if (sum_funcs)
 
1957
  {
 
1958
    Item_sum *func, **func_ptr= sum_funcs;
 
1959
    while ((func= *(func_ptr++)))
 
1960
      func->clear();
 
1961
  }
 
1962
 
 
1963
  return(0);
 
1964
}
 
1965
 
 
1966
/**
 
1967
   @brief Save the original join layout
 
1968
      
 
1969
   @details Saves the original join layout so it can be reused in 
 
1970
   re-execution and for EXPLAIN.
 
1971
             
 
1972
   @return Operation status
 
1973
   @retval 0      success.
 
1974
   @retval 1      error occurred.
 
1975
*/
 
1976
 
 
1977
bool
 
1978
JOIN::init_save_join_tab()
 
1979
{
 
1980
  if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
 
1981
    return 1;                                  /* purecov: inspected */
 
1982
  error= 0;                                    // Ensure that tmp_join.error= 0
 
1983
  restore_tmp();
 
1984
  return 0;
 
1985
}
 
1986
 
 
1987
 
 
1988
bool
 
1989
JOIN::save_join_tab()
 
1990
{
 
1991
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
 
1992
  {
 
1993
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((unsigned char*) join_tab,
 
1994
                                                sizeof(JOIN_TAB) * tables)))
 
1995
      return 1;
 
1996
  }
 
1997
  return 0;
 
1998
}
 
1999
 
 
2000
 
 
2001
/**
 
2002
  Exec select.
 
2003
 
 
2004
  @todo
 
2005
    Note, that create_sort_index calls test_if_skip_sort_order and may
 
2006
    finally replace sorting with index scan if there is a LIMIT clause in
 
2007
    the query.  It's never shown in EXPLAIN!
 
2008
 
 
2009
  @todo
 
2010
    When can we have here thd->net.report_error not zero?
 
2011
*/
 
2012
void
 
2013
JOIN::exec()
 
2014
{
 
2015
  List<Item> *columns_list= &fields_list;
 
2016
  int      tmp_error;
 
2017
 
 
2018
  thd->set_proc_info("executing");
 
2019
  error= 0;
 
2020
  (void) result->prepare2(); // Currently, this cannot fail.
 
2021
 
 
2022
  if (!tables_list && (tables || !select_lex->with_sum_func))
 
2023
  {                                           // Only test of functions
 
2024
    if (select_options & SELECT_DESCRIBE)
 
2025
      select_describe(this, false, false, false,
 
2026
                      (zero_result_cause?zero_result_cause:"No tables used"));
 
2027
    else
 
2028
    {
 
2029
      result->send_fields(*columns_list,
 
2030
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2031
      /*
 
2032
        We have to test for 'conds' here as the WHERE may not be constant
 
2033
        even if we don't have any tables for prepared statements or if
 
2034
        conds uses something like 'rand()'.
 
2035
      */
 
2036
      if (cond_value != Item::COND_FALSE &&
 
2037
          (!conds || conds->val_int()) &&
 
2038
          (!having || having->val_int()))
 
2039
      {
 
2040
        if (do_send_rows && result->send_data(fields_list))
 
2041
          error= 1;
 
2042
        else
 
2043
        {
 
2044
          error= (int) result->send_eof();
 
2045
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
 
2046
                         thd->sent_row_count);
 
2047
        }
 
2048
      }
 
2049
      else
 
2050
      {
 
2051
        error=(int) result->send_eof();
 
2052
        send_records= 0;
 
2053
      }
 
2054
    }
 
2055
    /* Single select (without union) always returns 0 or 1 row */
 
2056
    thd->limit_found_rows= send_records;
 
2057
    thd->examined_row_count= 0;
 
2058
    return;
 
2059
  }
 
2060
  /*
 
2061
    Don't reset the found rows count if there're no tables as
 
2062
    FOUND_ROWS() may be called. Never reset the examined row count here.
 
2063
    It must be accumulated from all join iterations of all join parts.
 
2064
  */
 
2065
  if (tables)
 
2066
    thd->limit_found_rows= 0;
 
2067
 
 
2068
  if (zero_result_cause)
 
2069
  {
 
2070
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
 
2071
                            *columns_list,
 
2072
                            send_row_on_empty_set(),
 
2073
                            select_options,
 
2074
                            zero_result_cause,
 
2075
                            having);
 
2076
    return;
 
2077
  }
 
2078
 
 
2079
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
2080
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
2081
    return;
 
2082
 
 
2083
  if (select_options & SELECT_DESCRIBE)
 
2084
  {
 
2085
    /*
 
2086
      Check if we managed to optimize order_st BY away and don't use temporary
 
2087
      table to resolve order_st BY: in that case, we only may need to do
 
2088
      filesort for GROUP BY.
 
2089
    */
 
2090
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
 
2091
    {
 
2092
      /*
 
2093
        Reset 'order' to 'group_list' and reinit variables describing
 
2094
        'order'
 
2095
      */
 
2096
      order= group_list;
 
2097
      simple_order= simple_group;
 
2098
      skip_sort_order= 0;
 
2099
    }
 
2100
    if (order && 
 
2101
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
 
2102
        (const_tables == tables ||
 
2103
         ((simple_order || skip_sort_order) &&
 
2104
          test_if_skip_sort_order(&join_tab[const_tables], order,
 
2105
                                  select_limit, 0, 
 
2106
                                  &join_tab[const_tables].table->
 
2107
                                    keys_in_use_for_query))))
 
2108
      order=0;
 
2109
    having= tmp_having;
 
2110
    select_describe(this, need_tmp,
 
2111
                    order != 0 && !skip_sort_order,
 
2112
                    select_distinct,
 
2113
                    !tables ? "No tables used" : NULL);
 
2114
    return;
 
2115
  }
 
2116
 
 
2117
  JOIN *curr_join= this;
 
2118
  List<Item> *curr_all_fields= &all_fields;
 
2119
  List<Item> *curr_fields_list= &fields_list;
 
2120
  Table *curr_tmp_table= 0;
 
2121
  /*
 
2122
    Initialize examined rows here because the values from all join parts
 
2123
    must be accumulated in examined_row_count. Hence every join
 
2124
    iteration must count from zero.
 
2125
  */
 
2126
  curr_join->examined_rows= 0;
 
2127
 
 
2128
  /* Create a tmp table if distinct or if the sort is too complicated */
 
2129
  if (need_tmp)
 
2130
  {
 
2131
    if (tmp_join)
 
2132
    {
 
2133
      /*
 
2134
        We are in a non cacheable sub query. Get the saved join structure
 
2135
        after optimization.
 
2136
        (curr_join may have been modified during last exection and we need
 
2137
        to reset it)
 
2138
      */
 
2139
      curr_join= tmp_join;
 
2140
    }
 
2141
    curr_tmp_table= exec_tmp_table1;
 
2142
 
 
2143
    /* Copy data to the temporary table */
 
2144
    thd->set_proc_info("Copying to tmp table");
 
2145
    if (!curr_join->sort_and_group &&
 
2146
        curr_join->const_tables != curr_join->tables)
 
2147
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2148
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2149
    {
 
2150
      error= tmp_error;
 
2151
      return;
 
2152
    }
 
2153
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
 
2154
    
 
2155
    if (curr_join->having)
 
2156
      curr_join->having= curr_join->tmp_having= 0; // Allready done
 
2157
    
 
2158
    /* Change sum_fields reference to calculated fields in tmp_table */
 
2159
    curr_join->all_fields= *curr_all_fields;
 
2160
    if (!items1)
 
2161
    {
 
2162
      items1= items0 + all_fields.elements;
 
2163
      if (sort_and_group || curr_tmp_table->group)
 
2164
      {
 
2165
        if (change_to_use_tmp_fields(thd, items1,
 
2166
                                     tmp_fields_list1, tmp_all_fields1,
 
2167
                                     fields_list.elements, all_fields))
 
2168
          return;
 
2169
      }
 
2170
      else
 
2171
      {
 
2172
        if (change_refs_to_tmp_fields(thd, items1,
 
2173
                                      tmp_fields_list1, tmp_all_fields1,
 
2174
                                      fields_list.elements, all_fields))
 
2175
          return;
 
2176
      }
 
2177
      curr_join->tmp_all_fields1= tmp_all_fields1;
 
2178
      curr_join->tmp_fields_list1= tmp_fields_list1;
 
2179
      curr_join->items1= items1;
 
2180
    }
 
2181
    curr_all_fields= &tmp_all_fields1;
 
2182
    curr_fields_list= &tmp_fields_list1;
 
2183
    curr_join->set_items_ref_array(items1);
 
2184
    
 
2185
    if (sort_and_group || curr_tmp_table->group)
 
2186
    {
 
2187
      curr_join->tmp_table_param.field_count+= 
 
2188
        curr_join->tmp_table_param.sum_func_count+
 
2189
        curr_join->tmp_table_param.func_count;
 
2190
      curr_join->tmp_table_param.sum_func_count= 
 
2191
        curr_join->tmp_table_param.func_count= 0;
 
2192
    }
 
2193
    else
 
2194
    {
 
2195
      curr_join->tmp_table_param.field_count+= 
 
2196
        curr_join->tmp_table_param.func_count;
 
2197
      curr_join->tmp_table_param.func_count= 0;
 
2198
    }
 
2199
    
 
2200
    if (curr_tmp_table->group)
 
2201
    {                                           // Already grouped
 
2202
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
 
2203
        curr_join->order= curr_join->group_list;  /* order by group */
 
2204
      curr_join->group_list= 0;
 
2205
    }
 
2206
    
 
2207
    /*
 
2208
      If we have different sort & group then we must sort the data by group
 
2209
      and copy it to another tmp table
 
2210
      This code is also used if we are using distinct something
 
2211
      we haven't been able to store in the temporary table yet
 
2212
      like SEC_TO_TIME(SUM(...)).
 
2213
    */
 
2214
 
 
2215
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
 
2216
    {                                   /* Must copy to another table */
 
2217
      /* Free first data from old join */
 
2218
      curr_join->join_free();
 
2219
      if (make_simple_join(curr_join, curr_tmp_table))
 
2220
        return;
 
2221
      calc_group_buffer(curr_join, group_list);
 
2222
      count_field_types(select_lex, &curr_join->tmp_table_param,
 
2223
                        curr_join->tmp_all_fields1,
 
2224
                        curr_join->select_distinct && !curr_join->group_list);
 
2225
      curr_join->tmp_table_param.hidden_field_count= 
 
2226
        (curr_join->tmp_all_fields1.elements-
 
2227
         curr_join->tmp_fields_list1.elements);
 
2228
      
 
2229
      
 
2230
      if (exec_tmp_table2)
 
2231
        curr_tmp_table= exec_tmp_table2;
 
2232
      else
 
2233
      {
 
2234
        /* group data to new table */
 
2235
 
 
2236
        /*
 
2237
          If the access method is loose index scan then all MIN/MAX
 
2238
          functions are precomputed, and should be treated as regular
 
2239
          functions. See extended comment in JOIN::exec.
 
2240
        */
 
2241
        if (curr_join->join_tab->is_using_loose_index_scan())
 
2242
          curr_join->tmp_table_param.precomputed_group_by= true;
 
2243
 
 
2244
        if (!(curr_tmp_table=
 
2245
              exec_tmp_table2= create_tmp_table(thd,
 
2246
                                                &curr_join->tmp_table_param,
 
2247
                                                *curr_all_fields,
 
2248
                                                (order_st*) 0,
 
2249
                                                curr_join->select_distinct && 
 
2250
                                                !curr_join->group_list,
 
2251
                                                1, curr_join->select_options,
 
2252
                                                HA_POS_ERROR,
 
2253
                                                (char *) "")))
 
2254
          return;
 
2255
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
2256
      }
 
2257
      if (curr_join->group_list)
 
2258
      {
 
2259
        thd->set_proc_info("Creating sort index");
 
2260
        if (curr_join->join_tab == join_tab && save_join_tab())
 
2261
        {
 
2262
          return;
 
2263
        }
 
2264
        if (create_sort_index(thd, curr_join, curr_join->group_list,
 
2265
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
 
2266
            make_group_fields(this, curr_join))
 
2267
        {
 
2268
          return;
 
2269
        }
 
2270
        sortorder= curr_join->sortorder;
 
2271
      }
 
2272
      
 
2273
      thd->set_proc_info("Copying to group table");
 
2274
      tmp_error= -1;
 
2275
      if (curr_join != this)
 
2276
      {
 
2277
        if (sum_funcs2)
 
2278
        {
 
2279
          curr_join->sum_funcs= sum_funcs2;
 
2280
          curr_join->sum_funcs_end= sum_funcs_end2; 
 
2281
        }
 
2282
        else
 
2283
        {
 
2284
          curr_join->alloc_func_list();
 
2285
          sum_funcs2= curr_join->sum_funcs;
 
2286
          sum_funcs_end2= curr_join->sum_funcs_end;
 
2287
        }
 
2288
      }
 
2289
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2290
                                        1, true))
 
2291
        return;
 
2292
      curr_join->group_list= 0;
 
2293
      if (!curr_join->sort_and_group &&
 
2294
          curr_join->const_tables != curr_join->tables)
 
2295
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2296
      if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2297
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2298
      {
 
2299
        error= tmp_error;
 
2300
        return;
 
2301
      }
 
2302
      end_read_record(&curr_join->join_tab->read_record);
 
2303
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
 
2304
      curr_join->join_tab[0].table= 0;           // Table is freed
 
2305
      
 
2306
      // No sum funcs anymore
 
2307
      if (!items2)
 
2308
      {
 
2309
        items2= items1 + all_fields.elements;
 
2310
        if (change_to_use_tmp_fields(thd, items2,
 
2311
                                     tmp_fields_list2, tmp_all_fields2, 
 
2312
                                     fields_list.elements, tmp_all_fields1))
 
2313
          return;
 
2314
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
2315
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
2316
      }
 
2317
      curr_fields_list= &curr_join->tmp_fields_list2;
 
2318
      curr_all_fields= &curr_join->tmp_all_fields2;
 
2319
      curr_join->set_items_ref_array(items2);
 
2320
      curr_join->tmp_table_param.field_count+= 
 
2321
        curr_join->tmp_table_param.sum_func_count;
 
2322
      curr_join->tmp_table_param.sum_func_count= 0;
 
2323
    }
 
2324
    if (curr_tmp_table->distinct)
 
2325
      curr_join->select_distinct=0;             /* Each row is unique */
 
2326
    
 
2327
    curr_join->join_free();                     /* Free quick selects */
 
2328
    if (curr_join->select_distinct && ! curr_join->group_list)
 
2329
    {
 
2330
      thd->set_proc_info("Removing duplicates");
 
2331
      if (curr_join->tmp_having)
 
2332
        curr_join->tmp_having->update_used_tables();
 
2333
      if (remove_duplicates(curr_join, curr_tmp_table,
 
2334
                            *curr_fields_list, curr_join->tmp_having))
 
2335
        return;
 
2336
      curr_join->tmp_having=0;
 
2337
      curr_join->select_distinct=0;
 
2338
    }
 
2339
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
 
2340
    if (make_simple_join(curr_join, curr_tmp_table))
 
2341
      return;
 
2342
    calc_group_buffer(curr_join, curr_join->group_list);
 
2343
    count_field_types(select_lex, &curr_join->tmp_table_param, 
 
2344
                      *curr_all_fields, 0);
 
2345
    
 
2346
  }
 
2347
  
 
2348
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
 
2349
  {
 
2350
    if (make_group_fields(this, curr_join))
 
2351
    {
 
2352
      return;
 
2353
    }
 
2354
    if (!items3)
 
2355
    {
 
2356
      if (!items0)
 
2357
        init_items_ref_array();
 
2358
      items3= ref_pointer_array + (all_fields.elements*4);
 
2359
      setup_copy_fields(thd, &curr_join->tmp_table_param,
 
2360
                        items3, tmp_fields_list3, tmp_all_fields3,
 
2361
                        curr_fields_list->elements, *curr_all_fields);
 
2362
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
 
2363
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
 
2364
      tmp_table_param.save_copy_field_end=
 
2365
        curr_join->tmp_table_param.copy_field_end;
 
2366
      curr_join->tmp_all_fields3= tmp_all_fields3;
 
2367
      curr_join->tmp_fields_list3= tmp_fields_list3;
 
2368
    }
 
2369
    else
 
2370
    {
 
2371
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
 
2372
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
 
2373
      curr_join->tmp_table_param.copy_field_end=
 
2374
        tmp_table_param.save_copy_field_end;
 
2375
    }
 
2376
    curr_fields_list= &tmp_fields_list3;
 
2377
    curr_all_fields= &tmp_all_fields3;
 
2378
    curr_join->set_items_ref_array(items3);
 
2379
 
 
2380
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2381
                                      1, true) || 
 
2382
        setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2383
        thd->is_fatal_error)
 
2384
      return;
 
2385
  }
 
2386
  if (curr_join->group_list || curr_join->order)
 
2387
  {
 
2388
    thd->set_proc_info("Sorting result");
 
2389
    /* If we have already done the group, add HAVING to sorted table */
 
2390
    if (curr_join->tmp_having && ! curr_join->group_list && 
 
2391
        ! curr_join->sort_and_group)
 
2392
    {
 
2393
      // Some tables may have been const
 
2394
      curr_join->tmp_having->update_used_tables();
 
2395
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
 
2396
      table_map used_tables= (curr_join->const_table_map |
 
2397
                              curr_table->table->map);
 
2398
 
 
2399
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
 
2400
                                                 used_tables,
 
2401
                                                 used_tables, 0);
 
2402
      if (sort_table_cond)
 
2403
      {
 
2404
        if (!curr_table->select)
 
2405
          if (!(curr_table->select= new SQL_SELECT))
 
2406
            return;
 
2407
        if (!curr_table->select->cond)
 
2408
          curr_table->select->cond= sort_table_cond;
 
2409
        else                                    // This should never happen
 
2410
        {
 
2411
          if (!(curr_table->select->cond=
 
2412
                new Item_cond_and(curr_table->select->cond,
 
2413
                                  sort_table_cond)))
 
2414
            return;
 
2415
          /*
 
2416
            Item_cond_and do not need fix_fields for execution, its parameters
 
2417
            are fixed or do not need fix_fields, too
 
2418
          */
 
2419
          curr_table->select->cond->quick_fix_field();
 
2420
        }
 
2421
        curr_table->select_cond= curr_table->select->cond;
 
2422
        curr_table->select_cond->top_level_item();
 
2423
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
2424
                                                   ~ (table_map) 0,
 
2425
                                                   ~used_tables, 0);
 
2426
      }
 
2427
    }
 
2428
    {
 
2429
      if (group)
 
2430
        curr_join->select_limit= HA_POS_ERROR;
 
2431
      else
 
2432
      {
 
2433
        /*
 
2434
          We can abort sorting after thd->select_limit rows if we there is no
 
2435
          WHERE clause for any tables after the sorted one.
 
2436
        */
 
2437
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
2438
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
2439
        for (; curr_table < end_table ; curr_table++)
 
2440
        {
 
2441
          /*
 
2442
            table->keyuse is set in the case there was an original WHERE clause
 
2443
            on the table that was optimized away.
 
2444
          */
 
2445
          if (curr_table->select_cond ||
 
2446
              (curr_table->keyuse && !curr_table->first_inner))
 
2447
          {
 
2448
            /* We have to sort all rows */
 
2449
            curr_join->select_limit= HA_POS_ERROR;
 
2450
            break;
 
2451
          }
 
2452
        }
 
2453
      }
 
2454
      if (curr_join->join_tab == join_tab && save_join_tab())
 
2455
      {
 
2456
        return;
 
2457
      }
 
2458
      /*
 
2459
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
 
2460
        chose FILESORT to be faster than INDEX SCAN or there is no 
 
2461
        suitable index present.
 
2462
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
2463
        finally replace sorting with index scan if there is a LIMIT clause in
 
2464
        the query. XXX: it's never shown in EXPLAIN!
 
2465
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
2466
      */
 
2467
      if (create_sort_index(thd, curr_join,
 
2468
                            curr_join->group_list ? 
 
2469
                            curr_join->group_list : curr_join->order,
 
2470
                            curr_join->select_limit,
 
2471
                            (select_options & OPTION_FOUND_ROWS ?
 
2472
                             HA_POS_ERROR : unit->select_limit_cnt),
 
2473
                            curr_join->group_list ? true : false))
 
2474
        return;
 
2475
      sortorder= curr_join->sortorder;
 
2476
      if (curr_join->const_tables != curr_join->tables &&
 
2477
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
 
2478
      {
 
2479
        /*
 
2480
          If no IO cache exists for the first table then we are using an
 
2481
          INDEX SCAN and no filesort. Thus we should not remove the sorted
 
2482
          attribute on the INDEX SCAN.
 
2483
        */
 
2484
        skip_sort_order= 1;
 
2485
      }
 
2486
    }
 
2487
  }
 
2488
  /* XXX: When can we have here thd->is_error() not zero? */
 
2489
  if (thd->is_error())
 
2490
  {
 
2491
    error= thd->is_error();
 
2492
    return;
 
2493
  }
 
2494
  curr_join->having= curr_join->tmp_having;
 
2495
  curr_join->fields= curr_fields_list;
 
2496
 
 
2497
  {
 
2498
    thd->set_proc_info("Sending data");
 
2499
    result->send_fields(*curr_fields_list,
 
2500
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2501
    error= do_select(curr_join, curr_fields_list, NULL);
 
2502
    thd->limit_found_rows= curr_join->send_records;
 
2503
  }
 
2504
 
 
2505
  /* Accumulate the counts from all join iterations of all join parts. */
 
2506
  thd->examined_row_count+= curr_join->examined_rows;
 
2507
 
 
2508
  /* 
 
2509
    With EXPLAIN EXTENDED we have to restore original ref_array
 
2510
    for a derived table which is always materialized.
 
2511
    Otherwise we would not be able to print the query  correctly.
 
2512
  */ 
 
2513
  if (items0 &&
 
2514
      (thd->lex->describe & DESCRIBE_EXTENDED) &&
 
2515
      select_lex->linkage == DERIVED_TABLE_TYPE)      
 
2516
    set_items_ref_array(items0);
 
2517
 
 
2518
  return;
 
2519
}
 
2520
 
 
2521
 
 
2522
/**
 
2523
  Clean up join.
 
2524
 
 
2525
  @return
 
2526
    Return error that hold JOIN.
 
2527
*/
 
2528
 
 
2529
int
 
2530
JOIN::destroy()
 
2531
{
 
2532
  select_lex->join= 0;
 
2533
 
 
2534
  if (tmp_join)
 
2535
  {
 
2536
    if (join_tab != tmp_join->join_tab)
 
2537
    {
 
2538
      JOIN_TAB *tab, *end;
 
2539
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
 
2540
        tab->cleanup();
 
2541
    }
 
2542
    tmp_join->tmp_join= 0;
 
2543
    tmp_table_param.copy_field=0;
 
2544
    return(tmp_join->destroy());
 
2545
  }
 
2546
  cond_equal= 0;
 
2547
 
 
2548
  cleanup(1);
 
2549
  if (exec_tmp_table1)
 
2550
    exec_tmp_table1->free_tmp_table(thd);
 
2551
  if (exec_tmp_table2)
 
2552
    exec_tmp_table2->free_tmp_table(thd);
 
2553
  delete select;
 
2554
  delete_dynamic(&keyuse);
 
2555
  return(error);
 
2556
}
 
2557
 
 
2558
 
 
2559
 
316
2560
/**
317
2561
  An entry point to single-unit select (a select without UNION).
318
2562
 
319
 
  @param session                  thread Cursor
 
2563
  @param thd                  thread handler
320
2564
  @param rref_pointer_array   a reference to ref_pointer_array of
321
2565
                              the top-level select_lex for this query
322
2566
  @param tables               list of all tables used in this query.
323
2567
                              The tables have been pre-opened.
324
 
  @param wild_num             number of wildcards used in the top level
 
2568
  @param wild_num             number of wildcards used in the top level 
325
2569
                              select of this query.
326
2570
                              For example statement
327
2571
                              SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
332
2576
                              for a, b and c in this list.
333
2577
  @param conds                top level item of an expression representing
334
2578
                              WHERE clause of the top level select
335
 
  @param og_num               total number of ORDER BY and GROUP BY clauses
 
2579
  @param og_num               total number of order_st BY and GROUP BY clauses
336
2580
                              arguments
337
 
  @param order                linked list of ORDER BY agruments
 
2581
  @param order                linked list of order_st BY agruments
338
2582
  @param group                linked list of GROUP BY arguments
339
2583
  @param having               top level item of HAVING expression
 
2584
  @param proc_param           list of PROCEDUREs
340
2585
  @param select_options       select options (BIG_RESULT, etc)
341
2586
  @param result               an instance of result set handling class.
342
2587
                              This object is responsible for send result
343
2588
                              set rows to the client or inserting them
344
2589
                              into a table.
345
 
  @param select_lex           the only Select_Lex of this query
 
2590
  @param select_lex           the only SELECT_LEX of this query
346
2591
  @param unit                 top-level UNIT of this query
347
2592
                              UNIT is an artificial object created by the
348
2593
                              parser for every SELECT clause.
355
2600
  @retval
356
2601
    true   an error
357
2602
*/
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)
 
2603
 
 
2604
bool
 
2605
mysql_select(THD *thd, Item ***rref_pointer_array,
 
2606
             TableList *tables, uint32_t wild_num, List<Item> &fields,
 
2607
             COND *conds, uint32_t og_num,  order_st *order, order_st *group,
 
2608
             Item *having, order_st *proc_param, uint64_t select_options,
 
2609
             select_result *result, SELECT_LEX_UNIT *unit,
 
2610
             SELECT_LEX *select_lex)
372
2611
{
373
2612
  bool err;
374
2613
  bool free_join= 1;
375
2614
 
376
2615
  select_lex->context.resolve_in_select_list= true;
377
 
  Join *join;
 
2616
  JOIN *join;
378
2617
  if (select_lex->join != 0)
379
2618
  {
380
2619
    join= select_lex->join;
383
2622
      creation
384
2623
    */
385
2624
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
386
 
        (select_options & SELECT_DESCRIBE))
 
2625
        (select_options & SELECT_DESCRIBE))
387
2626
    {
388
2627
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
389
2628
      {
390
 
        //here is EXPLAIN of subselect or derived table
391
 
        if (join->change_result(result))
392
 
        {
393
 
          return(true);
394
 
        }
 
2629
        //here is EXPLAIN of subselect or derived table
 
2630
        if (join->change_result(result))
 
2631
        {
 
2632
          return(true);
 
2633
        }
395
2634
      }
396
2635
      else
397
2636
      {
398
2637
        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
 
        }
 
2638
                               conds, og_num, order, group, having, proc_param,
 
2639
                               select_lex, unit)))
 
2640
        {
 
2641
          goto err;
 
2642
        }
403
2643
      }
404
2644
    }
405
2645
    free_join= 0;
407
2647
  }
408
2648
  else
409
2649
  {
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
 
2650
    if (!(join= new JOIN(thd, fields, select_options, result)))
 
2651
        return(true);
 
2652
    thd->set_proc_info("init");
 
2653
    thd->used_tables=0;                         // Updated by setup_fields
414
2654
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
415
 
                           conds, og_num, order, group, having,
 
2655
                           conds, og_num, order, group, having, proc_param,
416
2656
                           select_lex, unit)) == true)
417
2657
    {
418
2658
      goto err;
419
2659
    }
420
2660
  }
421
2661
 
422
 
  err= join->optimize();
423
 
  if (err)
424
 
  {
425
 
    goto err; // 1
426
 
  }
427
 
 
428
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
2662
  /* dump_TableList_graph(select_lex, select_lex->leaf_tables); */
 
2663
  if (join->flatten_subqueries())
 
2664
  {
 
2665
    err= 1;
 
2666
    goto err;
 
2667
  }
 
2668
  /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
 
2669
 
 
2670
  if ((err= join->optimize()))
 
2671
  {
 
2672
    goto err;                                   // 1
 
2673
  }
 
2674
 
 
2675
  if (thd->lex->describe & DESCRIBE_EXTENDED)
429
2676
  {
430
2677
    join->conds_history= join->conds;
431
2678
    join->having_history= (join->having?join->having:join->tmp_having);
432
2679
  }
433
2680
 
434
 
  if (session->is_error())
 
2681
  if (thd->is_error())
435
2682
    goto err;
436
2683
 
437
2684
  join->exec();
438
2685
 
439
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
2686
  if (thd->lex->describe & DESCRIBE_EXTENDED)
440
2687
  {
441
2688
    select_lex->where= join->conds_history;
442
2689
    select_lex->having= join->having_history;
445
2692
err:
446
2693
  if (free_join)
447
2694
  {
448
 
    session->set_proc_info("end");
 
2695
    thd->set_proc_info("end");
449
2696
    err|= select_lex->cleanup();
450
 
    return(err || session->is_error());
 
2697
    return(err || thd->is_error());
451
2698
  }
452
2699
  return(join->error);
453
2700
}
454
2701
 
455
 
inline Item *and_items(Item* cond, Item *item)
 
2702
 
 
2703
int subq_sj_candidate_cmp(Item_in_subselect* const *el1, 
 
2704
                          Item_in_subselect* const *el2)
 
2705
{
 
2706
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 : 
 
2707
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
 
2708
}
 
2709
 
 
2710
 
 
2711
inline Item * and_items(Item* cond, Item *item)
456
2712
{
457
2713
  return (cond? (new Item_cond_and(cond, item)) : item);
458
2714
}
459
2715
 
 
2716
 
 
2717
static TableList *alloc_join_nest(THD *thd)
 
2718
{
 
2719
  TableList *tbl;
 
2720
  if (!(tbl= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
 
2721
                                       sizeof(nested_join_st))))
 
2722
    return NULL;
 
2723
  tbl->nested_join= (nested_join_st*) ((unsigned char*)tbl + 
 
2724
                                    ALIGN_SIZE(sizeof(TableList)));
 
2725
  return tbl;
 
2726
}
 
2727
 
 
2728
 
 
2729
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TableList> *tlist)
 
2730
{
 
2731
  List_iterator<TableList> it(*tlist);
 
2732
  TableList *table;
 
2733
  while ((table= it++))
 
2734
  {
 
2735
    if (table->on_expr)
 
2736
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
 
2737
    if (table->nested_join)
 
2738
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
 
2739
  }
 
2740
}
 
2741
 
 
2742
 
 
2743
/*
 
2744
  Convert a subquery predicate into a TableList semi-join nest
 
2745
 
 
2746
  SYNOPSIS
 
2747
    convert_subq_to_sj()
 
2748
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON 
 
2749
                    clause
 
2750
       subq_pred    Subquery predicate to be converted
 
2751
  
 
2752
  DESCRIPTION
 
2753
    Convert a subquery predicate into a TableList semi-join nest. All the 
 
2754
    prerequisites are already checked, so the conversion is always successfull.
 
2755
 
 
2756
    Prepared Statements: the transformation is permanent:
 
2757
     - Changes in TableList structures are naturally permanent
 
2758
     - Item tree changes are performed on statement MEM_ROOT:
 
2759
        = we activate statement MEM_ROOT 
 
2760
        = this function is called before the first fix_prepare_information
 
2761
          call.
 
2762
 
 
2763
    This is intended because the criteria for subquery-to-sj conversion remain
 
2764
    constant for the lifetime of the Prepared Statement.
 
2765
 
 
2766
  RETURN
 
2767
    false  OK
 
2768
    true   Out of memory error
 
2769
*/
 
2770
 
 
2771
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
 
2772
{
 
2773
  SELECT_LEX *parent_lex= parent_join->select_lex;
 
2774
  TableList *emb_tbl_nest= NULL;
 
2775
  List<TableList> *emb_join_list= &parent_lex->top_join_list;
 
2776
  THD *thd= parent_join->thd;
 
2777
 
 
2778
  /*
 
2779
    1. Find out where to put the predicate into.
 
2780
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
 
2781
  */
 
2782
  if ((void*)subq_pred->expr_join_nest != (void*)1)
 
2783
  {
 
2784
    if (subq_pred->expr_join_nest->nested_join)
 
2785
    {
 
2786
      /*
 
2787
        We're dealing with
 
2788
 
 
2789
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
 
2790
 
 
2791
        The sj-nest will be inserted into the brackets nest.
 
2792
      */
 
2793
      emb_tbl_nest=  subq_pred->expr_join_nest;
 
2794
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2795
    }
 
2796
    else if (!subq_pred->expr_join_nest->outer_join)
 
2797
    {
 
2798
      /*
 
2799
        We're dealing with
 
2800
 
 
2801
          ... INNER JOIN tblX ON (subquery AND whatever) ...
 
2802
 
 
2803
        The sj-nest will be tblX's "sibling", i.e. another child of its
 
2804
        parent. This is ok because tblX is joined as an inner join.
 
2805
      */
 
2806
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
 
2807
      if (emb_tbl_nest)
 
2808
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2809
    }
 
2810
    else if (!subq_pred->expr_join_nest->nested_join)
 
2811
    {
 
2812
      TableList *outer_tbl= subq_pred->expr_join_nest;      
 
2813
      TableList *wrap_nest;
 
2814
      /*
 
2815
        We're dealing with
 
2816
 
 
2817
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
 
2818
 
 
2819
        we'll need to convert it into:
 
2820
 
 
2821
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
 
2822
                        |                      |
 
2823
                        |<----- wrap_nest ---->|
 
2824
        
 
2825
        Q:  other subqueries may be pointing to this element. What to do?
 
2826
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
 
2827
            But we'll need to fix other pointers.
 
2828
        A2: Another way: have TableList::next_ptr so the following
 
2829
            subqueries know the table has been nested.
 
2830
        A3: changes in the TableList::outer_join will make everything work
 
2831
            automatically.
 
2832
      */
 
2833
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
 
2834
      {
 
2835
        return(true);
 
2836
      }
 
2837
      wrap_nest->embedding= outer_tbl->embedding;
 
2838
      wrap_nest->join_list= outer_tbl->join_list;
 
2839
      wrap_nest->alias= (char*) "(sj-wrap)";
 
2840
 
 
2841
      wrap_nest->nested_join->join_list.empty();
 
2842
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
 
2843
 
 
2844
      outer_tbl->embedding= wrap_nest;
 
2845
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
 
2846
 
 
2847
      /*
 
2848
        wrap_nest will take place of outer_tbl, so move the outer join flag
 
2849
        and on_expr
 
2850
      */
 
2851
      wrap_nest->outer_join= outer_tbl->outer_join;
 
2852
      outer_tbl->outer_join= 0;
 
2853
 
 
2854
      wrap_nest->on_expr= outer_tbl->on_expr;
 
2855
      outer_tbl->on_expr= NULL;
 
2856
 
 
2857
      List_iterator<TableList> li(*wrap_nest->join_list);
 
2858
      TableList *tbl;
 
2859
      while ((tbl= li++))
 
2860
      {
 
2861
        if (tbl == outer_tbl)
 
2862
        {
 
2863
          li.replace(wrap_nest);
 
2864
          break;
 
2865
        }
 
2866
      }
 
2867
      /*
 
2868
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the 
 
2869
        semi-join nest into it
 
2870
      */
 
2871
      emb_join_list= &wrap_nest->nested_join->join_list;
 
2872
      emb_tbl_nest=  wrap_nest;
 
2873
    }
 
2874
  }
 
2875
 
 
2876
  TableList *sj_nest;
 
2877
  nested_join_st *nested_join;
 
2878
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
 
2879
  {
 
2880
    return(true);
 
2881
  }
 
2882
  nested_join= sj_nest->nested_join;
 
2883
 
 
2884
  sj_nest->join_list= emb_join_list;
 
2885
  sj_nest->embedding= emb_tbl_nest;
 
2886
  sj_nest->alias= (char*) "(sj-nest)";
 
2887
  /* Nests do not participate in those 'chains', so: */
 
2888
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
 
2889
  emb_join_list->push_back(sj_nest);
 
2890
 
 
2891
  /* 
 
2892
    nested_join->used_tables and nested_join->not_null_tables are
 
2893
    initialized in simplify_joins().
 
2894
  */
 
2895
  
 
2896
  /* 
 
2897
    2. Walk through subquery's top list and set 'embedding' to point to the
 
2898
       sj-nest.
 
2899
  */
 
2900
  st_select_lex *subq_lex= subq_pred->unit->first_select();
 
2901
  nested_join->join_list.empty();
 
2902
  List_iterator_fast<TableList> li(subq_lex->top_join_list);
 
2903
  TableList *tl, *last_leaf;
 
2904
  while ((tl= li++))
 
2905
  {
 
2906
    tl->embedding= sj_nest;
 
2907
    tl->join_list= &nested_join->join_list;
 
2908
    nested_join->join_list.push_back(tl);
 
2909
  }
 
2910
  
 
2911
  /*
 
2912
    Reconnect the next_leaf chain.
 
2913
    TODO: Do we have to put subquery's tables at the end of the chain?
 
2914
          Inserting them at the beginning would be a bit faster.
 
2915
    NOTE: We actually insert them at the front! That's because the order is
 
2916
          reversed in this list.
 
2917
  */
 
2918
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
 
2919
  tl->next_leaf= subq_lex->leaf_tables;
 
2920
  last_leaf= tl;
 
2921
 
 
2922
  /*
 
2923
    Same as above for next_local chain
 
2924
    (a theory: a next_local chain always starts with ::leaf_tables
 
2925
     because view's tables are inserted after the view)
 
2926
  */
 
2927
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
 
2928
  tl->next_local= subq_lex->leaf_tables;
 
2929
 
 
2930
  /* A theory: no need to re-connect the next_global chain */
 
2931
 
 
2932
  /* 3. Remove the original subquery predicate from the WHERE/ON */
 
2933
 
 
2934
  // The subqueries were replaced for Item_int(1) earlier
 
2935
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
 
2936
  /*TODO: also reset the 'with_subselect' there. */
 
2937
 
 
2938
  /* n. Adjust the parent_join->tables counter */
 
2939
  uint32_t table_no= parent_join->tables;
 
2940
  /* n. Walk through child's tables and adjust table->map */
 
2941
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
 
2942
  {
 
2943
    tl->table->tablenr= table_no;
 
2944
    tl->table->map= ((table_map)1) << table_no;
 
2945
    SELECT_LEX *old_sl= tl->select_lex;
 
2946
    tl->select_lex= parent_join->select_lex; 
 
2947
    for(TableList *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
 
2948
      emb->select_lex= parent_join->select_lex;
 
2949
  }
 
2950
  parent_join->tables += subq_lex->join->tables;
 
2951
 
 
2952
  /* 
 
2953
    Put the subquery's WHERE into semi-join's sj_on_expr
 
2954
    Add the subquery-induced equalities too.
 
2955
  */
 
2956
  SELECT_LEX *save_lex= thd->lex->current_select;
 
2957
  thd->lex->current_select=subq_lex;
 
2958
  if (!subq_pred->left_expr->fixed &&
 
2959
       subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
 
2960
    return(true);
 
2961
  thd->lex->current_select=save_lex;
 
2962
 
 
2963
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
 
2964
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
 
2965
                                        subq_pred->left_expr->used_tables();
 
2966
  sj_nest->sj_on_expr= subq_lex->where;
 
2967
 
 
2968
  /*
 
2969
    Create the IN-equalities and inject them into semi-join's ON expression.
 
2970
    Additionally, for InsideOut strategy
 
2971
     - Record the number of IN-equalities.
 
2972
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
 
2973
       see which of the expressions are bound and which are not (for those
 
2974
       we'll produce a distinct stream of (ie_i1,...ie_ik).
 
2975
 
 
2976
       (TODO: can we just create a list of pointers and hope the expressions
 
2977
       will not substitute themselves on fix_fields()? or we need to wrap
 
2978
       them into Item_direct_view_refs and store pointers to those. The
 
2979
       pointers to Item_direct_view_refs are guaranteed to be stable as 
 
2980
       Item_direct_view_refs doesn't substitute itself with anything in 
 
2981
       Item_direct_view_ref::fix_fields.
 
2982
  */
 
2983
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
 
2984
  sj_nest->nested_join->sj_outer_expr_list.empty();
 
2985
 
 
2986
  if (subq_pred->left_expr->cols() == 1)
 
2987
  {
 
2988
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
 
2989
 
 
2990
    Item *item_eq= new Item_func_eq(subq_pred->left_expr, 
 
2991
                                    subq_lex->ref_pointer_array[0]);
 
2992
    item_eq->name= (char*)subq_sj_cond_name;
 
2993
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
2994
  }
 
2995
  else
 
2996
  {
 
2997
    for (uint32_t i= 0; i < subq_pred->left_expr->cols(); i++)
 
2998
    {
 
2999
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
 
3000
                                                element_index(i));
 
3001
      Item *item_eq= 
 
3002
        new Item_func_eq(subq_pred->left_expr->element_index(i), 
 
3003
                         subq_lex->ref_pointer_array[i]);
 
3004
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
 
3005
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3006
    }
 
3007
  }
 
3008
  /* Fix the created equality and AND */
 
3009
  sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
 
3010
 
 
3011
  /*
 
3012
    Walk through sj nest's WHERE and ON expressions and call
 
3013
    item->fix_table_changes() for all items.
 
3014
  */
 
3015
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
 
3016
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
 
3017
 
 
3018
 
 
3019
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
 
3020
  subq_lex->master_unit()->exclude_level();
 
3021
 
 
3022
  /* Inject sj_on_expr into the parent's WHERE or ON */
 
3023
  if (emb_tbl_nest)
 
3024
  {
 
3025
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr, 
 
3026
                                     sj_nest->sj_on_expr);
 
3027
    emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
 
3028
  }
 
3029
  else
 
3030
  {
 
3031
    /* Inject into the WHERE */
 
3032
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
 
3033
    parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
 
3034
    parent_join->select_lex->where= parent_join->conds;
 
3035
  }
 
3036
 
 
3037
  return(false);
 
3038
}
 
3039
 
 
3040
 
 
3041
/*
 
3042
  Convert candidate subquery predicates to semi-joins
 
3043
 
 
3044
  SYNOPSIS
 
3045
    JOIN::flatten_subqueries()
 
3046
 
 
3047
  DESCRIPTION
 
3048
    Convert candidate subquery predicates to semi-joins.
 
3049
 
 
3050
  RETURN 
 
3051
    false  OK
 
3052
    true   Error
 
3053
*/
 
3054
 
 
3055
bool JOIN::flatten_subqueries()
 
3056
{
 
3057
  Item_in_subselect **in_subq;
 
3058
  Item_in_subselect **in_subq_end;
 
3059
 
 
3060
  if (sj_subselects.elements() == 0)
 
3061
    return(false);
 
3062
 
 
3063
  /* 1. Fix children subqueries */
 
3064
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); 
 
3065
       in_subq != in_subq_end; in_subq++)
 
3066
  {
 
3067
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3068
    child_join->outer_tables = child_join->tables;
 
3069
    if (child_join->flatten_subqueries())
 
3070
      return(true);
 
3071
    (*in_subq)->sj_convert_priority= 
 
3072
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
 
3073
  }
 
3074
 
 
3075
  //dump_TableList_struct(select_lex, select_lex->leaf_tables);
 
3076
  /* 
 
3077
    2. Pick which subqueries to convert:
 
3078
      sort the subquery array
 
3079
      - prefer correlated subqueries over uncorrelated;
 
3080
      - prefer subqueries that have greater number of outer tables;
 
3081
  */
 
3082
  sj_subselects.sort(subq_sj_candidate_cmp);
 
3083
  // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
 
3084
  /* Replace all subqueries to be flattened with Item_int(1) */
 
3085
  for (in_subq= sj_subselects.front(); 
 
3086
       in_subq != in_subq_end && 
 
3087
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3088
       in_subq++)
 
3089
  {
 
3090
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
 
3091
      return(true);
 
3092
  }
 
3093
 
 
3094
  for (in_subq= sj_subselects.front(); 
 
3095
       in_subq != in_subq_end && 
 
3096
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3097
       in_subq++)
 
3098
  {
 
3099
    if (convert_subq_to_sj(this, *in_subq))
 
3100
      return(true);
 
3101
  }
 
3102
 
 
3103
  /* 3. Finalize those we didn't convert */
 
3104
  for (; in_subq!= in_subq_end; in_subq++)
 
3105
  {
 
3106
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3107
    Item_subselect::trans_res res;
 
3108
    (*in_subq)->changed= 0;
 
3109
    (*in_subq)->fixed= 0;
 
3110
    res= (*in_subq)->select_transformer(child_join);
 
3111
    if (res == Item_subselect::RES_ERROR)
 
3112
      return(true);
 
3113
 
 
3114
    (*in_subq)->changed= 1;
 
3115
    (*in_subq)->fixed= 1;
 
3116
 
 
3117
    Item *substitute= (*in_subq)->substitution;
 
3118
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
 
3119
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
 
3120
      return(true);
 
3121
 
 
3122
    //if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
 
3123
    //  return(true);
 
3124
  }
 
3125
  sj_subselects.clear();
 
3126
  return(false);
 
3127
}
 
3128
 
 
3129
 
 
3130
/**
 
3131
  Setup for execution all subqueries of a query, for which the optimizer
 
3132
  chose hash semi-join.
 
3133
 
 
3134
  @details Iterate over all subqueries of the query, and if they are under an
 
3135
  IN predicate, and the optimizer chose to compute it via hash semi-join:
 
3136
  - try to initialize all data structures needed for the materialized execution
 
3137
    of the IN predicate,
 
3138
  - if this fails, then perform the IN=>EXISTS transformation which was
 
3139
    previously blocked during JOIN::prepare.
 
3140
 
 
3141
  This method is part of the "code generation" query processing phase.
 
3142
 
 
3143
  This phase must be called after substitute_for_best_equal_field() because
 
3144
  that function may replace items with other items from a multiple equality,
 
3145
  and we need to reference the correct items in the index access method of the
 
3146
  IN predicate.
 
3147
 
 
3148
  @return Operation status
 
3149
  @retval false     success.
 
3150
  @retval true      error occurred.
 
3151
*/
 
3152
 
 
3153
bool JOIN::setup_subquery_materialization()
 
3154
{
 
3155
  for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
 
3156
       un= un->next_unit())
 
3157
  {
 
3158
    for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
 
3159
    {
 
3160
      Item_subselect *subquery_predicate= sl->master_unit()->item;
 
3161
      if (subquery_predicate &&
 
3162
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
 
3163
      {
 
3164
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
 
3165
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
 
3166
            in_subs->setup_engine())
 
3167
          return true;
 
3168
      }
 
3169
    }
 
3170
  }
 
3171
  return false;
 
3172
}
 
3173
 
 
3174
 
 
3175
/*
 
3176
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3177
 
 
3178
  SYNOPSIS
 
3179
    find_eq_ref_candidate()
 
3180
      table             Table to be checked
 
3181
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
 
3182
                        count.
 
3183
 
 
3184
  DESCRIPTION
 
3185
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3186
 
 
3187
  TODO
 
3188
    Check again if it is feasible to factor common parts with constant table
 
3189
    search
 
3190
 
 
3191
  RETURN
 
3192
    true  - There exists an eq_ref(outer-tables) candidate
 
3193
    false - Otherwise
 
3194
*/
 
3195
 
 
3196
bool find_eq_ref_candidate(Table *table, table_map sj_inner_tables)
 
3197
{
 
3198
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
 
3199
  uint32_t key;
 
3200
 
 
3201
  if (keyuse)
 
3202
  {
 
3203
    while (1) /* For each key */
 
3204
    {
 
3205
      key= keyuse->key;
 
3206
      KEY *keyinfo= table->key_info + key;
 
3207
      key_part_map bound_parts= 0;
 
3208
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
 
3209
      {
 
3210
        do  /* For all equalities on all key parts */
 
3211
        {
 
3212
          /* Check if this is "t.keypart = expr(outer_tables) */
 
3213
          if (!(keyuse->used_tables & sj_inner_tables) &&
 
3214
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
 
3215
          {
 
3216
            bound_parts |= 1 << keyuse->keypart;
 
3217
          }
 
3218
          keyuse++;
 
3219
        } while (keyuse->key == key && keyuse->table == table);
 
3220
 
 
3221
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
 
3222
          return true;
 
3223
        if (keyuse->table != table)
 
3224
          return false;
 
3225
      }
 
3226
      else
 
3227
      {
 
3228
        do
 
3229
        {
 
3230
          keyuse++;
 
3231
          if (keyuse->table != table)
 
3232
            return false;
 
3233
        }
 
3234
        while (keyuse->key == key);
 
3235
      }
 
3236
    }
 
3237
  }
 
3238
  return false;
 
3239
}
 
3240
 
 
3241
 
 
3242
/*
 
3243
  Pull tables out of semi-join nests, if possible
 
3244
 
 
3245
  SYNOPSIS
 
3246
    pull_out_semijoin_tables()
 
3247
      join  The join where to do the semi-join flattening
 
3248
 
 
3249
  DESCRIPTION
 
3250
    Try to pull tables out of semi-join nests.
 
3251
     
 
3252
    PRECONDITIONS
 
3253
    When this function is called, the join may have several semi-join nests
 
3254
    (possibly within different semi-join nests), but it is guaranteed that
 
3255
    one semi-join nest does not contain another.
 
3256
   
 
3257
    ACTION
 
3258
    A table can be pulled out of the semi-join nest if
 
3259
     - It is a constant table
 
3260
     - It is accessed 
 
3261
 
 
3262
    POSTCONDITIONS
 
3263
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
 
3264
       tables)
 
3265
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
 
3266
     * Semi-join nests TableList::sj_inner_tables
 
3267
 
 
3268
    This operation is (and should be) performed at each PS execution since
 
3269
    tables may become/cease to be constant across PS reexecutions.
 
3270
 
 
3271
  RETURN 
 
3272
    0 - OK
 
3273
    1 - Out of memory error
 
3274
*/
 
3275
 
 
3276
int pull_out_semijoin_tables(JOIN *join)
 
3277
{
 
3278
  TableList *sj_nest;
 
3279
  List_iterator<TableList> sj_list_it(join->select_lex->sj_nests);
 
3280
   
 
3281
  /* Try pulling out of the each of the semi-joins */
 
3282
  while ((sj_nest= sj_list_it++))
 
3283
  {
 
3284
    /* Action #1: Mark the constant tables to be pulled out */
 
3285
    table_map pulled_tables= 0;
 
3286
     
 
3287
    List_iterator<TableList> child_li(sj_nest->nested_join->join_list);
 
3288
    TableList *tbl;
 
3289
    while ((tbl= child_li++))
 
3290
    {
 
3291
      if (tbl->table)
 
3292
      {
 
3293
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
 
3294
        if (tbl->table->map & join->const_table_map)
 
3295
        {
 
3296
          pulled_tables |= tbl->table->map;
 
3297
        }
 
3298
      }
 
3299
    }
 
3300
    
 
3301
    /*
 
3302
      Action #2: Find which tables we can pull out based on
 
3303
      update_ref_and_keys() data. Note that pulling one table out can allow
 
3304
      us to pull out some other tables too.
 
3305
    */
 
3306
    bool pulled_a_table;
 
3307
    do 
 
3308
    {
 
3309
      pulled_a_table= false;
 
3310
      child_li.rewind();
 
3311
      while ((tbl= child_li++))
 
3312
      {
 
3313
        if (tbl->table && !(pulled_tables & tbl->table->map))
 
3314
        {
 
3315
          if (find_eq_ref_candidate(tbl->table, 
 
3316
                                    sj_nest->nested_join->used_tables & 
 
3317
                                    ~pulled_tables))
 
3318
          {
 
3319
            pulled_a_table= true;
 
3320
            pulled_tables |= tbl->table->map;
 
3321
          }
 
3322
        }
 
3323
      }
 
3324
    } while (pulled_a_table);
 
3325
 
 
3326
    child_li.rewind();
 
3327
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
 
3328
    {
 
3329
      (sj_nest)->sj_inner_tables= 0;
 
3330
      while ((tbl= child_li++))
 
3331
      {
 
3332
        if (tbl->table)
 
3333
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3334
      }
 
3335
    }
 
3336
    else
 
3337
    {
 
3338
      /* Record the bitmap of inner tables, mark the inner tables */
 
3339
      table_map inner_tables=(sj_nest)->nested_join->used_tables & 
 
3340
                             ~pulled_tables;
 
3341
      (sj_nest)->sj_inner_tables= inner_tables;
 
3342
      while ((tbl= child_li++))
 
3343
      {
 
3344
        if (tbl->table)
 
3345
        {
 
3346
          if (inner_tables & tbl->table->map)
 
3347
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
 
3348
          else
 
3349
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3350
        }
 
3351
      }
 
3352
    }
 
3353
  }
 
3354
  return(0);
 
3355
}
 
3356
 
460
3357
/*****************************************************************************
461
 
  Create JoinTableS, make a guess about the table types,
 
3358
  Create JOIN_TABS, make a guess about the table types,
462
3359
  Approximate how many records will be used in each table
463
3360
*****************************************************************************/
464
 
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
 
3361
 
 
3362
 
 
3363
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
 
3364
                                      Table *table,
 
3365
                                      const key_map *keys,ha_rows limit)
465
3366
{
466
3367
  int error;
467
 
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
 
3368
  if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
468
3369
    return(0);                           // Fatal error flag is set
469
3370
  if (select)
470
3371
  {
471
3372
    select->head=table;
472
3373
    table->reginfo.impossible_range=0;
473
 
    if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
 
3374
    if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
474
3375
                                          limit, 0, false)) == 1)
475
3376
      return(select->quick->records);
476
3377
    if (error == -1)
482
3383
  return(HA_POS_ERROR);                 /* This shouldn't happend */
483
3384
}
484
3385
 
 
3386
/*
 
3387
   This structure is used to collect info on potentially sargable
 
3388
   predicates in order to check whether they become sargable after
 
3389
   reading const tables.
 
3390
   We form a bitmap of indexes that can be used for sargable predicates.
 
3391
   Only such indexes are involved in range analysis.
 
3392
*/
 
3393
typedef struct st_sargable_param
 
3394
{
 
3395
  Field *field;              /* field against which to check sargability */
 
3396
  Item **arg_value;          /* values of potential keys for lookups     */
 
3397
  uint32_t num_values;           /* number of values in the above array      */
 
3398
} SARGABLE_PARAM;  
 
3399
 
 
3400
/**
 
3401
  Calculate the best possible join and initialize the join structure.
 
3402
 
 
3403
  @retval
 
3404
    0   ok
 
3405
  @retval
 
3406
    1   Fatal error
 
3407
*/
 
3408
 
 
3409
static bool
 
3410
make_join_statistics(JOIN *join, TableList *tables, COND *conds,
 
3411
                     DYNAMIC_ARRAY *keyuse_array)
 
3412
{
 
3413
  int error;
 
3414
  Table *table;
 
3415
  uint32_t i,table_count,const_count,key;
 
3416
  table_map found_const_table_map, all_table_map, found_ref, refs;
 
3417
  key_map const_ref, eq_part;
 
3418
  Table **table_vector;
 
3419
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
 
3420
  KEYUSE *keyuse,*start_keyuse;
 
3421
  table_map outer_join=0;
 
3422
  SARGABLE_PARAM *sargables= 0;
 
3423
  JOIN_TAB *stat_vector[MAX_TABLES+1];
 
3424
 
 
3425
  table_count=join->tables;
 
3426
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
 
3427
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
 
3428
  table_vector=(Table**) join->thd->alloc(sizeof(Table*)*(table_count*2));
 
3429
  if (!stat || !stat_ref || !table_vector)
 
3430
    return(1);                          // Eom /* purecov: inspected */
 
3431
 
 
3432
  join->best_ref=stat_vector;
 
3433
 
 
3434
  stat_end=stat+table_count;
 
3435
  found_const_table_map= all_table_map=0;
 
3436
  const_count=0;
 
3437
 
 
3438
  for (s= stat, i= 0;
 
3439
       tables;
 
3440
       s++, tables= tables->next_leaf, i++)
 
3441
  {
 
3442
    TableList *embedding= tables->embedding;
 
3443
    stat_vector[i]=s;
 
3444
    s->keys.init();
 
3445
    s->const_keys.init();
 
3446
    s->checked_keys.init();
 
3447
    s->needed_reg.init();
 
3448
    table_vector[i]=s->table=table=tables->table;
 
3449
    table->pos_in_table_list= tables;
 
3450
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
3451
    if(error)
 
3452
    {
 
3453
        table->file->print_error(error, MYF(0));
 
3454
        return(1);
 
3455
    }
 
3456
    table->quick_keys.clear_all();
 
3457
    table->reginfo.join_tab=s;
 
3458
    table->reginfo.not_exists_optimize=0;
 
3459
    memset(table->const_key_parts, 0,
 
3460
           sizeof(key_part_map)*table->s->keys);
 
3461
    all_table_map|= table->map;
 
3462
    s->join=join;
 
3463
    s->info=0;                                  // For describe
 
3464
 
 
3465
    s->dependent= tables->dep_tables;
 
3466
    s->key_dependent= 0;
 
3467
    if (tables->schema_table)
 
3468
      table->file->stats.records= 2;
 
3469
    table->quick_condition_rows= table->file->stats.records;
 
3470
 
 
3471
    s->on_expr_ref= &tables->on_expr;
 
3472
    if (*s->on_expr_ref)
 
3473
    {
 
3474
      /* s is the only inner table of an outer join */
 
3475
      if (!table->file->stats.records && !embedding)
 
3476
      {                                         // Empty table
 
3477
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
 
3478
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3479
        continue;
 
3480
      }
 
3481
      outer_join|= table->map;
 
3482
      s->embedding_map= 0;
 
3483
      for (;embedding; embedding= embedding->embedding)
 
3484
        s->embedding_map|= embedding->nested_join->nj_map;
 
3485
      continue;
 
3486
    }
 
3487
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
 
3488
    {
 
3489
      /* s belongs to a nested join, maybe to several embedded joins */
 
3490
      s->embedding_map= 0;
 
3491
      do
 
3492
      {
 
3493
        nested_join_st *nested_join= embedding->nested_join;
 
3494
        s->embedding_map|=nested_join->nj_map;
 
3495
        s->dependent|= embedding->dep_tables;
 
3496
        embedding= embedding->embedding;
 
3497
        outer_join|= nested_join->used_tables;
 
3498
      }
 
3499
      while (embedding);
 
3500
      continue;
 
3501
    }
 
3502
    if ((table->file->stats.records <= 1) &&
 
3503
        !s->dependent &&
 
3504
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
 
3505
    {
 
3506
      set_position(join,const_count++,s,(KEYUSE*) 0);
 
3507
    }
 
3508
  }
 
3509
  stat_vector[i]=0;
 
3510
  join->outer_join=outer_join;
 
3511
 
 
3512
  if (join->outer_join)
 
3513
  {
 
3514
    /* 
 
3515
       Build transitive closure for relation 'to be dependent on'.
 
3516
       This will speed up the plan search for many cases with outer joins,
 
3517
       as well as allow us to catch illegal cross references/
 
3518
       Warshall's algorithm is used to build the transitive closure.
 
3519
       As we use bitmaps to represent the relation the complexity
 
3520
       of the algorithm is O((number of tables)^2). 
 
3521
    */
 
3522
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3523
    {
 
3524
      for (uint32_t j= 0 ; j < table_count ; j++)
 
3525
      {
 
3526
        table= stat[j].table;
 
3527
        if (s->dependent & table->map)
 
3528
          s->dependent |= table->reginfo.join_tab->dependent;
 
3529
      }
 
3530
      if (s->dependent)
 
3531
        s->table->maybe_null= 1;
 
3532
    }
 
3533
    /* Catch illegal cross references for outer joins */
 
3534
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3535
    {
 
3536
      if (s->dependent & s->table->map)
 
3537
      {
 
3538
        join->tables=0;                 // Don't use join->table
 
3539
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
 
3540
        return(1);
 
3541
      }
 
3542
      s->key_dependent= s->dependent;
 
3543
    }
 
3544
  }
 
3545
 
 
3546
  if (conds || outer_join)
 
3547
    if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
 
3548
                            conds, join->cond_equal,
 
3549
                            ~outer_join, join->select_lex, &sargables))
 
3550
      return(1);
 
3551
 
 
3552
  /* Read tables with 0 or 1 rows (system tables) */
 
3553
  join->const_table_map= 0;
 
3554
 
 
3555
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
 
3556
       p_pos < p_end ;
 
3557
       p_pos++)
 
3558
  {
 
3559
    int tmp;
 
3560
    s= p_pos->table;
 
3561
    s->type=JT_SYSTEM;
 
3562
    join->const_table_map|=s->table->map;
 
3563
    if ((tmp=join_read_const_table(s, p_pos)))
 
3564
    {
 
3565
      if (tmp > 0)
 
3566
        return(1);                      // Fatal error
 
3567
    }
 
3568
    else
 
3569
      found_const_table_map|= s->table->map;
 
3570
  }
 
3571
 
 
3572
  /* loop until no more const tables are found */
 
3573
  int ref_changed;
 
3574
  do
 
3575
  {
 
3576
  more_const_tables_found:
 
3577
    ref_changed = 0;
 
3578
    found_ref=0;
 
3579
 
 
3580
    /*
 
3581
      We only have to loop from stat_vector + const_count as
 
3582
      set_position() will move all const_tables first in stat_vector
 
3583
    */
 
3584
 
 
3585
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
 
3586
    {
 
3587
      table=s->table;
 
3588
 
 
3589
      /* 
 
3590
        If equi-join condition by a key is null rejecting and after a
 
3591
        substitution of a const table the key value happens to be null
 
3592
        then we can state that there are no matches for this equi-join.
 
3593
      */  
 
3594
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
 
3595
      {
 
3596
        /* 
 
3597
          When performing an outer join operation if there are no matching rows
 
3598
          for the single row of the outer table all the inner tables are to be
 
3599
          null complemented and thus considered as constant tables.
 
3600
          Here we apply this consideration to the case of outer join operations 
 
3601
          with a single inner table only because the case with nested tables
 
3602
          would require a more thorough analysis.
 
3603
          TODO. Apply single row substitution to null complemented inner tables
 
3604
          for nested outer join operations. 
 
3605
        */              
 
3606
        while (keyuse->table == table)
 
3607
        {
 
3608
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
 
3609
              keyuse->val->is_null() && keyuse->null_rejecting)
 
3610
          {
 
3611
            s->type= JT_CONST;
 
3612
            mark_as_null_row(table);
 
3613
            found_const_table_map|= table->map;
 
3614
            join->const_table_map|= table->map;
 
3615
            set_position(join,const_count++,s,(KEYUSE*) 0);
 
3616
            goto more_const_tables_found;
 
3617
           }
 
3618
          keyuse++;
 
3619
        }
 
3620
      }
 
3621
 
 
3622
      if (s->dependent)                         // If dependent on some table
 
3623
      {
 
3624
        // All dep. must be constants
 
3625
        if (s->dependent & ~(found_const_table_map))
 
3626
          continue;
 
3627
        if (table->file->stats.records <= 1L &&
 
3628
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3629
            !table->pos_in_table_list->embedding)
 
3630
        {                                       // system table
 
3631
          int tmp= 0;
 
3632
          s->type=JT_SYSTEM;
 
3633
          join->const_table_map|=table->map;
 
3634
          set_position(join,const_count++,s,(KEYUSE*) 0);
 
3635
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
 
3636
          {
 
3637
            if (tmp > 0)
 
3638
              return(1);                        // Fatal error
 
3639
          }
 
3640
          else
 
3641
            found_const_table_map|= table->map;
 
3642
          continue;
 
3643
        }
 
3644
      }
 
3645
      /* check if table can be read by key or table only uses const refs */
 
3646
      if ((keyuse=s->keyuse))
 
3647
      {
 
3648
        s->type= JT_REF;
 
3649
        while (keyuse->table == table)
 
3650
        {
 
3651
          start_keyuse=keyuse;
 
3652
          key=keyuse->key;
 
3653
          s->keys.set_bit(key);               // QQ: remove this ?
 
3654
 
 
3655
          refs=0;
 
3656
          const_ref.clear_all();
 
3657
          eq_part.clear_all();
 
3658
          do
 
3659
          {
 
3660
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
 
3661
            {
 
3662
              if (!((~found_const_table_map) & keyuse->used_tables))
 
3663
                const_ref.set_bit(keyuse->keypart);
 
3664
              else
 
3665
                refs|=keyuse->used_tables;
 
3666
              eq_part.set_bit(keyuse->keypart);
 
3667
            }
 
3668
            keyuse++;
 
3669
          } while (keyuse->table == table && keyuse->key == key);
 
3670
 
 
3671
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3672
              !table->pos_in_table_list->embedding)
 
3673
          {
 
3674
            if ((table->key_info[key].flags & (HA_NOSAME))
 
3675
                 == HA_NOSAME)
 
3676
            {
 
3677
              if (const_ref == eq_part)
 
3678
              {                                 // Found everything for ref.
 
3679
                int tmp;
 
3680
                ref_changed = 1;
 
3681
                s->type= JT_CONST;
 
3682
                join->const_table_map|=table->map;
 
3683
                set_position(join,const_count++,s,start_keyuse);
 
3684
                if (create_ref_for_key(join, s, start_keyuse,
 
3685
                                       found_const_table_map))
 
3686
                  return(1);
 
3687
                if ((tmp=join_read_const_table(s,
 
3688
                                               join->positions+const_count-1)))
 
3689
                {
 
3690
                  if (tmp > 0)
 
3691
                    return(1);                  // Fatal error
 
3692
                }
 
3693
                else
 
3694
                  found_const_table_map|= table->map;
 
3695
                break;
 
3696
              }
 
3697
              else
 
3698
                found_ref|= refs;      // Table is const if all refs are const
 
3699
            }
 
3700
            else if (const_ref == eq_part)
 
3701
              s->const_keys.set_bit(key);
 
3702
          }
 
3703
        }
 
3704
      }
 
3705
    }
 
3706
  } while (join->const_table_map & found_ref && ref_changed);
 
3707
 
 
3708
  /* 
 
3709
    Update info on indexes that can be used for search lookups as
 
3710
    reading const tables may has added new sargable predicates. 
 
3711
  */
 
3712
  if (const_count && sargables)
 
3713
  {
 
3714
    for( ; sargables->field ; sargables++)
 
3715
    {
 
3716
      Field *field= sargables->field;
 
3717
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
 
3718
      key_map possible_keys= field->key_start;
 
3719
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
3720
      bool is_const= 1;
 
3721
      for (uint32_t j=0; j < sargables->num_values; j++)
 
3722
        is_const&= sargables->arg_value[j]->const_item();
 
3723
      if (is_const)
 
3724
        join_tab[0].const_keys.merge(possible_keys);
 
3725
    }
 
3726
  }
 
3727
 
 
3728
  if (pull_out_semijoin_tables(join))
 
3729
    return(true);
 
3730
 
 
3731
  /* Calc how many (possible) matched records in each table */
 
3732
 
 
3733
  for (s=stat ; s < stat_end ; s++)
 
3734
  {
 
3735
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
 
3736
    {
 
3737
      /* Only one matching row */
 
3738
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
 
3739
      continue;
 
3740
    }
 
3741
    /* Approximate found rows and time to read them */
 
3742
    s->found_records=s->records=s->table->file->stats.records;
 
3743
    s->read_time=(ha_rows) s->table->file->scan_time();
 
3744
 
 
3745
    /*
 
3746
      Set a max range of how many seeks we can expect when using keys
 
3747
      This is can't be to high as otherwise we are likely to use
 
3748
      table scan.
 
3749
    */
 
3750
    s->worst_seeks= cmin((double) s->found_records / 10,
 
3751
                        (double) s->read_time*3);
 
3752
    if (s->worst_seeks < 2.0)                   // Fix for small tables
 
3753
      s->worst_seeks=2.0;
 
3754
 
 
3755
    /*
 
3756
      Add to stat->const_keys those indexes for which all group fields or
 
3757
      all select distinct fields participate in one index.
 
3758
    */
 
3759
    add_group_and_distinct_keys(join, s);
 
3760
 
 
3761
    if (!s->const_keys.is_clear_all() &&
 
3762
        !s->table->pos_in_table_list->embedding)
 
3763
    {
 
3764
      ha_rows records;
 
3765
      SQL_SELECT *select;
 
3766
      select= make_select(s->table, found_const_table_map,
 
3767
                          found_const_table_map,
 
3768
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
 
3769
                          1, &error);
 
3770
      if (!select)
 
3771
        return(1);
 
3772
      records= get_quick_record_count(join->thd, select, s->table,
 
3773
                                      &s->const_keys, join->row_limit);
 
3774
      s->quick=select->quick;
 
3775
      s->needed_reg=select->needed_reg;
 
3776
      select->quick=0;
 
3777
      if (records == 0 && s->table->reginfo.impossible_range)
 
3778
      {
 
3779
        /*
 
3780
          Impossible WHERE or ON expression
 
3781
          In case of ON, we mark that the we match one empty NULL row.
 
3782
          In case of WHERE, don't set found_const_table_map to get the
 
3783
          caller to abort with a zero row result.
 
3784
        */
 
3785
        join->const_table_map|= s->table->map;
 
3786
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3787
        s->type= JT_CONST;
 
3788
        if (*s->on_expr_ref)
 
3789
        {
 
3790
          /* Generate empty row */
 
3791
          s->info= "Impossible ON condition";
 
3792
          found_const_table_map|= s->table->map;
 
3793
          s->type= JT_CONST;
 
3794
          mark_as_null_row(s->table);           // All fields are NULL
 
3795
        }
 
3796
      }
 
3797
      if (records != HA_POS_ERROR)
 
3798
      {
 
3799
        s->found_records=records;
 
3800
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
3801
      }
 
3802
      delete select;
 
3803
    }
 
3804
  }
 
3805
 
 
3806
  join->join_tab=stat;
 
3807
  join->map2table=stat_ref;
 
3808
  join->table= join->all_tables=table_vector;
 
3809
  join->const_tables=const_count;
 
3810
  join->found_const_table_map=found_const_table_map;
 
3811
 
 
3812
  /* Find an optimal join order of the non-constant tables. */
 
3813
  if (join->const_tables != join->tables)
 
3814
  {
 
3815
    optimize_keyuse(join, keyuse_array);
 
3816
    if (choose_plan(join, all_table_map & ~join->const_table_map))
 
3817
      return(true);
 
3818
  }
 
3819
  else
 
3820
  {
 
3821
    memcpy(join->best_positions, join->positions,
 
3822
           sizeof(POSITION)*join->const_tables);
 
3823
    join->best_read=1.0;
 
3824
  }
 
3825
  /* Generate an execution plan from the found optimal join order. */
 
3826
  return(join->thd->killed || get_best_combination(join));
 
3827
}
 
3828
 
 
3829
 
485
3830
/*****************************************************************************
486
3831
  Check with keys are used and with tables references with tables
487
3832
  Updates in stat:
490
3835
          keyuse     Pointer to possible keys
491
3836
*****************************************************************************/
492
3837
 
 
3838
/// Used when finding key fields
 
3839
typedef struct key_field_t {
 
3840
  Field         *field;
 
3841
  Item          *val;                   ///< May be empty if diff constant
 
3842
  uint          level;
 
3843
  uint          optimize; // KEY_OPTIMIZE_*
 
3844
  bool          eq_func;
 
3845
  /**
 
3846
    If true, the condition this struct represents will not be satisfied
 
3847
    when val IS NULL.
 
3848
  */
 
3849
  bool          null_rejecting; 
 
3850
  bool          *cond_guard; /* See KEYUSE::cond_guard */
 
3851
  uint32_t          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3852
} KEY_FIELD;
 
3853
 
 
3854
/**
 
3855
  Merge new key definitions to old ones, remove those not used in both.
 
3856
 
 
3857
  This is called for OR between different levels.
 
3858
 
 
3859
  To be able to do 'ref_or_null' we merge a comparison of a column
 
3860
  and 'column IS NULL' to one test.  This is useful for sub select queries
 
3861
  that are internally transformed to something like:.
 
3862
 
 
3863
  @code
 
3864
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL 
 
3865
  @endcode
 
3866
 
 
3867
  KEY_FIELD::null_rejecting is processed as follows: @n
 
3868
  result has null_rejecting=true if it is set for both ORed references.
 
3869
  for example:
 
3870
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
 
3871
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
 
3872
 
 
3873
  @todo
 
3874
    The result of this is that we're missing some 'ref' accesses.
 
3875
    OptimizerTeam: Fix this
 
3876
*/
 
3877
 
 
3878
static KEY_FIELD *
 
3879
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
 
3880
                 uint32_t and_level)
 
3881
{
 
3882
  if (start == new_fields)
 
3883
    return start;                               // Impossible or
 
3884
  if (new_fields == end)
 
3885
    return start;                               // No new fields, skip all
 
3886
 
 
3887
  KEY_FIELD *first_free=new_fields;
 
3888
 
 
3889
  /* Mark all found fields in old array */
 
3890
  for (; new_fields != end ; new_fields++)
 
3891
  {
 
3892
    for (KEY_FIELD *old=start ; old != first_free ; old++)
 
3893
    {
 
3894
      if (old->field == new_fields->field)
 
3895
      {
 
3896
        /*
 
3897
          NOTE: below const_item() call really works as "!used_tables()", i.e.
 
3898
          it can return false where it is feasible to make it return true.
 
3899
          
 
3900
          The cause is as follows: Some of the tables are already known to be
 
3901
          const tables (the detection code is in make_join_statistics(),
 
3902
          above the update_ref_and_keys() call), but we didn't propagate 
 
3903
          information about this: Table::const_table is not set to true, and
 
3904
          Item::update_used_tables() hasn't been called for each item.
 
3905
          The result of this is that we're missing some 'ref' accesses.
 
3906
          TODO: OptimizerTeam: Fix this
 
3907
        */
 
3908
        if (!new_fields->val->const_item())
 
3909
        {
 
3910
          /*
 
3911
            If the value matches, we can use the key reference.
 
3912
            If not, we keep it until we have examined all new values
 
3913
          */
 
3914
          if (old->val->eq(new_fields->val, old->field->binary()))
 
3915
          {
 
3916
            old->level= and_level;
 
3917
            old->optimize= ((old->optimize & new_fields->optimize &
 
3918
                             KEY_OPTIMIZE_EXISTS) |
 
3919
                            ((old->optimize | new_fields->optimize) &
 
3920
                             KEY_OPTIMIZE_REF_OR_NULL));
 
3921
            old->null_rejecting= (old->null_rejecting &&
 
3922
                                  new_fields->null_rejecting);
 
3923
          }
 
3924
        }
 
3925
        else if (old->eq_func && new_fields->eq_func &&
 
3926
                 old->val->eq_by_collation(new_fields->val, 
 
3927
                                           old->field->binary(),
 
3928
                                           old->field->charset()))
 
3929
 
 
3930
        {
 
3931
          old->level= and_level;
 
3932
          old->optimize= ((old->optimize & new_fields->optimize &
 
3933
                           KEY_OPTIMIZE_EXISTS) |
 
3934
                          ((old->optimize | new_fields->optimize) &
 
3935
                           KEY_OPTIMIZE_REF_OR_NULL));
 
3936
          old->null_rejecting= (old->null_rejecting &&
 
3937
                                new_fields->null_rejecting);
 
3938
        }
 
3939
        else if (old->eq_func && new_fields->eq_func &&
 
3940
                 ((old->val->const_item() && old->val->is_null()) || 
 
3941
                  new_fields->val->is_null()))
 
3942
        {
 
3943
          /* field = expression OR field IS NULL */
 
3944
          old->level= and_level;
 
3945
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
 
3946
          /*
 
3947
            Remember the NOT NULL value unless the value does not depend
 
3948
            on other tables.
 
3949
          */
 
3950
          if (!old->val->used_tables() && old->val->is_null())
 
3951
            old->val= new_fields->val;
 
3952
          /* The referred expression can be NULL: */ 
 
3953
          old->null_rejecting= 0;
 
3954
        }
 
3955
        else
 
3956
        {
 
3957
          /*
 
3958
            We are comparing two different const.  In this case we can't
 
3959
            use a key-lookup on this so it's better to remove the value
 
3960
            and let the range optimzier handle it
 
3961
          */
 
3962
          if (old == --first_free)              // If last item
 
3963
            break;
 
3964
          *old= *first_free;                    // Remove old value
 
3965
          old--;                                // Retry this value
 
3966
        }
 
3967
      }
 
3968
    }
 
3969
  }
 
3970
  /* Remove all not used items */
 
3971
  for (KEY_FIELD *old=start ; old != first_free ;)
 
3972
  {
 
3973
    if (old->level != and_level)
 
3974
    {                                           // Not used in all levels
 
3975
      if (old == --first_free)
 
3976
        break;
 
3977
      *old= *first_free;                        // Remove old value
 
3978
      continue;
 
3979
    }
 
3980
    old++;
 
3981
  }
 
3982
  return first_free;
 
3983
}
 
3984
 
 
3985
 
 
3986
/**
 
3987
  Add a possible key to array of possible keys if it's usable as a key
 
3988
 
 
3989
    @param key_fields      Pointer to add key, if usable
 
3990
    @param and_level       And level, to be stored in KEY_FIELD
 
3991
    @param cond            Condition predicate
 
3992
    @param field           Field used in comparision
 
3993
    @param eq_func         True if we used =, <=> or IS NULL
 
3994
    @param value           Value used for comparison with field
 
3995
    @param usable_tables   Tables which can be used for key optimization
 
3996
    @param sargables       IN/OUT Array of found sargable candidates
 
3997
 
 
3998
  @note
 
3999
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
 
4000
    table, we store this to be able to do not exists optimization later.
 
4001
 
 
4002
  @returns
 
4003
    *key_fields is incremented if we stored a key in the array
 
4004
*/
 
4005
 
 
4006
static void
 
4007
add_key_field(KEY_FIELD **key_fields,uint32_t and_level, Item_func *cond,
 
4008
              Field *field, bool eq_func, Item **value, uint32_t num_values,
 
4009
              table_map usable_tables, SARGABLE_PARAM **sargables)
 
4010
{
 
4011
  uint32_t exists_optimize= 0;
 
4012
  if (!(field->flags & PART_KEY_FLAG))
 
4013
  {
 
4014
    // Don't remove column IS NULL on a LEFT JOIN table
 
4015
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4016
        !field->table->maybe_null || field->null_ptr)
 
4017
      return;                                   // Not a key. Skip it
 
4018
    exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4019
    assert(num_values == 1);
 
4020
  }
 
4021
  else
 
4022
  {
 
4023
    table_map used_tables=0;
 
4024
    bool optimizable=0;
 
4025
    for (uint32_t i=0; i<num_values; i++)
 
4026
    {
 
4027
      used_tables|=(value[i])->used_tables();
 
4028
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
 
4029
        optimizable=1;
 
4030
    }
 
4031
    if (!optimizable)
 
4032
      return;
 
4033
    if (!(usable_tables & field->table->map))
 
4034
    {
 
4035
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4036
          !field->table->maybe_null || field->null_ptr)
 
4037
        return;                                 // Can't use left join optimize
 
4038
      exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4039
    }
 
4040
    else
 
4041
    {
 
4042
      JOIN_TAB *stat=field->table->reginfo.join_tab;
 
4043
      key_map possible_keys=field->key_start;
 
4044
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
4045
      stat[0].keys.merge(possible_keys);             // Add possible keys
 
4046
 
 
4047
      /*
 
4048
        Save the following cases:
 
4049
        Field op constant
 
4050
        Field LIKE constant where constant doesn't start with a wildcard
 
4051
        Field = field2 where field2 is in a different table
 
4052
        Field op formula
 
4053
        Field IS NULL
 
4054
        Field IS NOT NULL
 
4055
         Field BETWEEN ...
 
4056
         Field IN ...
 
4057
      */
 
4058
      stat[0].key_dependent|=used_tables;
 
4059
 
 
4060
      bool is_const=1;
 
4061
      for (uint32_t i=0; i<num_values; i++)
 
4062
      {
 
4063
        if (!(is_const&= value[i]->const_item()))
 
4064
          break;
 
4065
      }
 
4066
      if (is_const)
 
4067
        stat[0].const_keys.merge(possible_keys);
 
4068
      else if (!eq_func)
 
4069
      {
 
4070
        /* 
 
4071
          Save info to be able check whether this predicate can be 
 
4072
          considered as sargable for range analisis after reading const tables.
 
4073
          We do not save info about equalities as update_const_equal_items
 
4074
          will take care of updating info on keys from sargable equalities. 
 
4075
        */
 
4076
        (*sargables)--;
 
4077
        (*sargables)->field= field;
 
4078
        (*sargables)->arg_value= value;
 
4079
        (*sargables)->num_values= num_values;
 
4080
      }
 
4081
      /*
 
4082
        We can't always use indexes when comparing a string index to a
 
4083
        number. cmp_type() is checked to allow compare of dates to numbers.
 
4084
        eq_func is NEVER true when num_values > 1
 
4085
       */
 
4086
      if (!eq_func)
 
4087
      {
 
4088
        /* 
 
4089
          Additional optimization: if we're processing
 
4090
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
 
4091
          "t.key = c1".
 
4092
          TODO: This is a very limited fix. A more generic fix is possible. 
 
4093
          There are 2 options:
 
4094
          A) Make equality propagation code be able to handle BETWEEN
 
4095
             (including cases like t1.key BETWEEN t2.key AND t3.key)
 
4096
          B) Make range optimizer to infer additional "t.key = c" equalities
 
4097
             and use them in equality propagation process (see details in
 
4098
             OptimizerKBAndTodo)
 
4099
        */
 
4100
        if ((cond->functype() != Item_func::BETWEEN) ||
 
4101
            ((Item_func_between*) cond)->negated ||
 
4102
            !value[0]->eq(value[1], field->binary()))
 
4103
          return;
 
4104
        eq_func= true;
 
4105
      }
 
4106
 
 
4107
      if (field->result_type() == STRING_RESULT)
 
4108
      {
 
4109
        if ((*value)->result_type() != STRING_RESULT)
 
4110
        {
 
4111
          if (field->cmp_type() != (*value)->result_type())
 
4112
            return;
 
4113
        }
 
4114
        else
 
4115
        {
 
4116
          /*
 
4117
            We can't use indexes if the effective collation
 
4118
            of the operation differ from the field collation.
 
4119
          */
 
4120
          if (field->cmp_type() == STRING_RESULT &&
 
4121
              ((Field_str*)field)->charset() != cond->compare_collation())
 
4122
            return;
 
4123
        }
 
4124
      }
 
4125
    }
 
4126
  }
 
4127
  /*
 
4128
    For the moment eq_func is always true. This slot is reserved for future
 
4129
    extensions where we want to remembers other things than just eq comparisons
 
4130
  */
 
4131
  assert(eq_func);
 
4132
  /* Store possible eq field */
 
4133
  (*key_fields)->field=         field;
 
4134
  (*key_fields)->eq_func=       eq_func;
 
4135
  (*key_fields)->val=           *value;
 
4136
  (*key_fields)->level=         and_level;
 
4137
  (*key_fields)->optimize=      exists_optimize;
 
4138
  /*
 
4139
    If the condition has form "tbl.keypart = othertbl.field" and 
 
4140
    othertbl.field can be NULL, there will be no matches if othertbl.field 
 
4141
    has NULL value.
 
4142
    We use null_rejecting in add_not_null_conds() to add
 
4143
    'othertbl.field IS NOT NULL' to tab->select_cond.
 
4144
  */
 
4145
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
 
4146
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
 
4147
                                  ((*value)->type() == Item::FIELD_ITEM) &&
 
4148
                                  ((Item_field*)*value)->field->maybe_null());
 
4149
  (*key_fields)->cond_guard= NULL;
 
4150
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name && 
 
4151
                              cond->name < subq_sj_cond_name + 64)? 
 
4152
                              cond->name - subq_sj_cond_name: UINT_MAX;
 
4153
  (*key_fields)++;
 
4154
}
 
4155
 
 
4156
/**
 
4157
  Add possible keys to array of possible keys originated from a simple
 
4158
  predicate.
 
4159
 
 
4160
    @param  key_fields     Pointer to add key, if usable
 
4161
    @param  and_level      And level, to be stored in KEY_FIELD
 
4162
    @param  cond           Condition predicate
 
4163
    @param  field          Field used in comparision
 
4164
    @param  eq_func        True if we used =, <=> or IS NULL
 
4165
    @param  value          Value used for comparison with field
 
4166
                           Is NULL for BETWEEN and IN    
 
4167
    @param  usable_tables  Tables which can be used for key optimization
 
4168
    @param  sargables      IN/OUT Array of found sargable candidates
 
4169
 
 
4170
  @note
 
4171
    If field items f1 and f2 belong to the same multiple equality and
 
4172
    a key is added for f1, the the same key is added for f2.
 
4173
 
 
4174
  @returns
 
4175
    *key_fields is incremented if we stored a key in the array
 
4176
*/
 
4177
 
 
4178
static void
 
4179
add_key_equal_fields(KEY_FIELD **key_fields, uint32_t and_level,
 
4180
                     Item_func *cond, Item_field *field_item,
 
4181
                     bool eq_func, Item **val,
 
4182
                     uint32_t num_values, table_map usable_tables,
 
4183
                     SARGABLE_PARAM **sargables)
 
4184
{
 
4185
  Field *field= field_item->field;
 
4186
  add_key_field(key_fields, and_level, cond, field,
 
4187
                eq_func, val, num_values, usable_tables, sargables);
 
4188
  Item_equal *item_equal= field_item->item_equal;
 
4189
  if (item_equal)
 
4190
  { 
 
4191
    /*
 
4192
      Add to the set of possible key values every substitution of
 
4193
      the field for an equal field included into item_equal
 
4194
    */
 
4195
    Item_equal_iterator it(*item_equal);
 
4196
    Item_field *item;
 
4197
    while ((item= it++))
 
4198
    {
 
4199
      if (!field->eq(item->field))
 
4200
      {
 
4201
        add_key_field(key_fields, and_level, cond, item->field,
 
4202
                      eq_func, val, num_values, usable_tables,
 
4203
                      sargables);
 
4204
      }
 
4205
    }
 
4206
  }
 
4207
}
 
4208
 
 
4209
static void
 
4210
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint32_t *and_level,
 
4211
               COND *cond, table_map usable_tables,
 
4212
               SARGABLE_PARAM **sargables)
 
4213
{
 
4214
  if (cond->type() == Item_func::COND_ITEM)
 
4215
  {
 
4216
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
 
4217
    KEY_FIELD *org_key_fields= *key_fields;
 
4218
 
 
4219
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
4220
    {
 
4221
      Item *item;
 
4222
      while ((item=li++))
 
4223
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4224
                       sargables);
 
4225
      for (; org_key_fields != *key_fields ; org_key_fields++)
 
4226
        org_key_fields->level= *and_level;
 
4227
    }
 
4228
    else
 
4229
    {
 
4230
      (*and_level)++;
 
4231
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
 
4232
                     sargables);
 
4233
      Item *item;
 
4234
      while ((item=li++))
 
4235
      {
 
4236
        KEY_FIELD *start_key_fields= *key_fields;
 
4237
        (*and_level)++;
 
4238
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4239
                       sargables);
 
4240
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
 
4241
                                     *key_fields,++(*and_level));
 
4242
      }
 
4243
    }
 
4244
    return;
 
4245
  }
 
4246
 
 
4247
  /* 
 
4248
    Subquery optimization: Conditions that are pushed down into subqueries
 
4249
    are wrapped into Item_func_trig_cond. We process the wrapped condition
 
4250
    but need to set cond_guard for KEYUSE elements generated from it.
 
4251
  */
 
4252
  {
 
4253
    if (cond->type() == Item::FUNC_ITEM &&
 
4254
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
 
4255
    {
 
4256
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
 
4257
      if (!join->group_list && !join->order &&
 
4258
          join->unit->item && 
 
4259
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
 
4260
          !join->unit->is_union())
 
4261
      {
 
4262
        KEY_FIELD *save= *key_fields;
 
4263
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
 
4264
                       sargables);
 
4265
        // Indicate that this ref access candidate is for subquery lookup:
 
4266
        for (; save != *key_fields; save++)
 
4267
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
 
4268
      }
 
4269
      return;
 
4270
    }
 
4271
  }
 
4272
 
 
4273
  /* If item is of type 'field op field/constant' add it to key_fields */
 
4274
  if (cond->type() != Item::FUNC_ITEM)
 
4275
    return;
 
4276
  Item_func *cond_func= (Item_func*) cond;
 
4277
  switch (cond_func->select_optimize()) {
 
4278
  case Item_func::OPTIMIZE_NONE:
 
4279
    break;
 
4280
  case Item_func::OPTIMIZE_KEY:
 
4281
  {
 
4282
    Item **values;
 
4283
    // BETWEEN, IN, NE
 
4284
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
 
4285
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4286
    {
 
4287
      values= cond_func->arguments()+1;
 
4288
      if (cond_func->functype() == Item_func::NE_FUNC &&
 
4289
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4290
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4291
        values--;
 
4292
      assert(cond_func->functype() != Item_func::IN_FUNC ||
 
4293
                  cond_func->argument_count() != 2);
 
4294
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4295
                           (Item_field*) (cond_func->key_item()->real_item()),
 
4296
                           0, values, 
 
4297
                           cond_func->argument_count()-1,
 
4298
                           usable_tables, sargables);
 
4299
    }
 
4300
    if (cond_func->functype() == Item_func::BETWEEN)
 
4301
    {
 
4302
      values= cond_func->arguments();
 
4303
      for (uint32_t i= 1 ; i < cond_func->argument_count() ; i++)
 
4304
      {
 
4305
        Item_field *field_item;
 
4306
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
 
4307
            &&
 
4308
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
 
4309
        {
 
4310
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
 
4311
          add_key_equal_fields(key_fields, *and_level, cond_func,
 
4312
                               field_item, 0, values, 1, usable_tables, 
 
4313
                               sargables);
 
4314
        }
 
4315
      }  
 
4316
    }
 
4317
    break;
 
4318
  }
 
4319
  case Item_func::OPTIMIZE_OP:
 
4320
  {
 
4321
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
 
4322
                     cond_func->functype() == Item_func::EQUAL_FUNC);
 
4323
 
 
4324
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4325
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4326
    {
 
4327
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4328
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4329
                           equal_func,
 
4330
                           cond_func->arguments()+1, 1, usable_tables,
 
4331
                           sargables);
 
4332
    }
 
4333
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4334
        cond_func->functype() != Item_func::LIKE_FUNC &&
 
4335
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
 
4336
    {
 
4337
      add_key_equal_fields(key_fields, *and_level, cond_func, 
 
4338
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
 
4339
                           equal_func,
 
4340
                           cond_func->arguments(),1,usable_tables,
 
4341
                           sargables);
 
4342
    }
 
4343
    break;
 
4344
  }
 
4345
  case Item_func::OPTIMIZE_NULL:
 
4346
    /* column_name IS [NOT] NULL */
 
4347
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4348
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4349
    {
 
4350
      Item *tmp=new Item_null;
 
4351
      if (unlikely(!tmp))                       // Should never be true
 
4352
        return;
 
4353
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4354
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4355
                    cond_func->functype() == Item_func::ISNULL_FUNC,
 
4356
                           &tmp, 1, usable_tables, sargables);
 
4357
    }
 
4358
    break;
 
4359
  case Item_func::OPTIMIZE_EQUAL:
 
4360
    Item_equal *item_equal= (Item_equal *) cond;
 
4361
    Item *const_item= item_equal->get_const();
 
4362
    Item_equal_iterator it(*item_equal);
 
4363
    Item_field *item;
 
4364
    if (const_item)
 
4365
    {
 
4366
      /*
 
4367
        For each field field1 from item_equal consider the equality 
 
4368
        field1=const_item as a condition allowing an index access of the table
 
4369
        with field1 by the keys value of field1.
 
4370
      */   
 
4371
      while ((item= it++))
 
4372
      {
 
4373
        add_key_field(key_fields, *and_level, cond_func, item->field,
 
4374
                      true, &const_item, 1, usable_tables, sargables);
 
4375
      }
 
4376
    }
 
4377
    else 
 
4378
    {
 
4379
      /*
 
4380
        Consider all pairs of different fields included into item_equal.
 
4381
        For each of them (field1, field1) consider the equality 
 
4382
        field1=field2 as a condition allowing an index access of the table
 
4383
        with field1 by the keys value of field2.
 
4384
      */   
 
4385
      Item_equal_iterator fi(*item_equal);
 
4386
      while ((item= fi++))
 
4387
      {
 
4388
        Field *field= item->field;
 
4389
        while ((item= it++))
 
4390
        {
 
4391
          if (!field->eq(item->field))
 
4392
          {
 
4393
            add_key_field(key_fields, *and_level, cond_func, field,
 
4394
                          true, (Item **) &item, 1, usable_tables,
 
4395
                          sargables);
 
4396
          }
 
4397
        }
 
4398
        it.rewind();
 
4399
      }
 
4400
    }
 
4401
    break;
 
4402
  }
 
4403
}
493
4404
 
494
4405
/**
495
4406
  Add all keys with uses 'field' for some keypart.
496
4407
 
497
4408
  If field->and_level != and_level then only mark key_part as const_part.
498
4409
*/
499
 
uint32_t max_part_bit(key_part_map bits)
 
4410
 
 
4411
static uint
 
4412
max_part_bit(key_part_map bits)
500
4413
{
501
4414
  uint32_t found;
502
4415
  for (found=0; bits & 1 ; found++,bits>>=1) ;
503
4416
  return found;
504
4417
}
505
4418
 
506
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
 
4419
static void
 
4420
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
 
4421
{
 
4422
  Field *field=key_field->field;
 
4423
  Table *form= field->table;
 
4424
  KEYUSE keyuse;
 
4425
 
 
4426
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
 
4427
  {
 
4428
    for (uint32_t key= 0 ; key < form->sizeKeys() ; key++)
 
4429
    {
 
4430
      if (!(form->keys_in_use_for_query.is_set(key)))
 
4431
        continue;
 
4432
 
 
4433
      uint32_t key_parts= (uint) form->key_info[key].key_parts;
 
4434
      for (uint32_t part=0 ; part <  key_parts ; part++)
 
4435
      {
 
4436
        if (field->eq(form->key_info[key].key_part[part].field))
 
4437
        {
 
4438
          keyuse.table= field->table;
 
4439
          keyuse.val =  key_field->val;
 
4440
          keyuse.key =  key;
 
4441
          keyuse.keypart=part;
 
4442
          keyuse.keypart_map= (key_part_map) 1 << part;
 
4443
          keyuse.used_tables=key_field->val->used_tables();
 
4444
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
 
4445
          keyuse.null_rejecting= key_field->null_rejecting;
 
4446
          keyuse.cond_guard= key_field->cond_guard;
 
4447
          keyuse.sj_pred_no= key_field->sj_pred_no;
 
4448
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
 
4449
        }
 
4450
      }
 
4451
    }
 
4452
  }
 
4453
}
 
4454
 
 
4455
static int
 
4456
sort_keyuse(KEYUSE *a,KEYUSE *b)
507
4457
{
508
4458
  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()));
 
4459
  if (a->table->tablenr != b->table->tablenr)
 
4460
    return (int) (a->table->tablenr - b->table->tablenr);
 
4461
  if (a->key != b->key)
 
4462
    return (int) (a->key - b->key);
 
4463
  if (a->keypart != b->keypart)
 
4464
    return (int) (a->keypart - b->keypart);
515
4465
  // Place const values before other ones
516
 
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
517
 
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
 
4466
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
 
4467
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
518
4468
    return res;
519
4469
  /* 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)));
 
4470
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
 
4471
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
 
4472
}
 
4473
 
 
4474
 
 
4475
/*
 
4476
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
 
4477
 
 
4478
    This function populates KEY_FIELD array with entries generated from the 
 
4479
    ON condition of the given nested join, and does the same for nested joins 
 
4480
    contained within this nested join.
 
4481
 
 
4482
  @param[in]      nested_join_table   Nested join pseudo-table to process
 
4483
  @param[in,out]  end                 End of the key field array
 
4484
  @param[in,out]  and_level           And-level
 
4485
  @param[in,out]  sargables           Array of found sargable candidates
 
4486
 
 
4487
 
 
4488
  @note
 
4489
    We can add accesses to the tables that are direct children of this nested 
 
4490
    join (1), and are not inner tables w.r.t their neighbours (2).
 
4491
    
 
4492
    Example for #1 (outer brackets pair denotes nested join this function is 
 
4493
    invoked for):
 
4494
    @code
 
4495
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
 
4496
    @endcode
 
4497
    Example for #2:
 
4498
    @code
 
4499
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
 
4500
    @endcode
 
4501
    In examples 1-2 for condition cond, we can add 'ref' access candidates to 
 
4502
    t1 only.
 
4503
    Example #3:
 
4504
    @code
 
4505
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
 
4506
    @endcode
 
4507
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
 
4508
*/
 
4509
 
 
4510
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
 
4511
                                  KEY_FIELD **end, uint32_t *and_level,
 
4512
                                  SARGABLE_PARAM **sargables)
 
4513
{
 
4514
  List_iterator<TableList> li(nested_join_table->nested_join->join_list);
 
4515
  List_iterator<TableList> li2(nested_join_table->nested_join->join_list);
 
4516
  bool have_another = false;
 
4517
  table_map tables= 0;
 
4518
  TableList *table;
 
4519
  assert(nested_join_table->nested_join);
 
4520
 
 
4521
  while ((table= li++) || (have_another && (li=li2, have_another=false,
 
4522
                                            (table= li++))))
 
4523
  {
 
4524
    if (table->nested_join)
 
4525
    {
 
4526
      if (!table->on_expr)
 
4527
      {
 
4528
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
 
4529
        have_another= true;
 
4530
        li2= li;
 
4531
        li= List_iterator<TableList>(table->nested_join->join_list); 
 
4532
      }
 
4533
      else
 
4534
        add_key_fields_for_nj(join, table, end, and_level, sargables);
 
4535
    }
 
4536
    else
 
4537
      if (!table->on_expr)
 
4538
        tables |= table->table->map;
 
4539
  }
 
4540
  if (nested_join_table->on_expr)
 
4541
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
 
4542
                   sargables);
522
4543
}
523
4544
 
524
4545
 
525
4546
/**
526
4547
  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
 
4548
  
 
4549
  @param       thd 
 
4550
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
530
4551
  @param       join_tab       Array in tablenr_order
531
4552
  @param       tables         Number of tables in join
532
4553
  @param       cond           WHERE condition (note that the function analyzes
535
4556
                              for which we can make ref access based the WHERE
536
4557
                              clause)
537
4558
  @param       select_lex     current SELECT
538
 
  @param[out]  sargables      std::vector of found sargable candidates
539
 
 
 
4559
  @param[out]  sargables      Array of found sargable candidates
 
4560
      
540
4561
   @retval
541
4562
     0  OK
542
4563
   @retval
543
4564
     1  Out of memory.
544
4565
*/
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)
 
4566
 
 
4567
static bool
 
4568
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
 
4569
                    uint32_t tables, COND *cond,
 
4570
                    COND_EQUAL *cond_equal __attribute__((unused)),
 
4571
                    table_map normal_tables, SELECT_LEX *select_lex,
 
4572
                    SARGABLE_PARAM **sargables)
554
4573
{
555
 
  uint  and_level,found_eq_constant;
556
 
  optimizer::KeyField *key_fields, *end, *field;
 
4574
  uint  and_level,i,found_eq_constant;
 
4575
  KEY_FIELD *key_fields, *end, *field;
557
4576
  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
 
4577
  uint32_t m= cmax(select_lex->max_equal_elems,(uint32_t)1);
 
4578
  
 
4579
  /* 
 
4580
    We use the same piece of memory to store both  KEY_FIELD 
 
4581
    and SARGABLE_PARAM structure.
 
4582
    KEY_FIELD values are placed at the beginning this memory
 
4583
    while  SARGABLE_PARAM values are put at the end.
 
4584
    All predicates that are used to fill arrays of KEY_FIELD
 
4585
    and SARGABLE_PARAM structures have at most 2 arguments
 
4586
    except BETWEEN predicates that have 3 arguments and 
564
4587
    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
 
4588
    This any predicate if it's not BETWEEN/IN can be used 
 
4589
    directly to fill at most 2 array elements, either of KEY_FIELD
 
4590
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
568
4591
    can be filled as this predicate is considered as
569
4592
    saragable with respect to each of its argument.
570
4593
    An IN predicate can require at most 1 element as currently
571
4594
    it is considered as sargable only for its first argument.
572
4595
    Multiple equality can add  elements that are filled after
573
4596
    substitution of field arguments by equal fields. There
574
 
    can be not more than select_lex->max_equal_elems such
 
4597
    can be not more than select_lex->max_equal_elems such 
575
4598
    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;
 
4599
  */ 
 
4600
  sz= cmax(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4601
      (((thd->lex->current_select->cond_count+1)*2 +
 
4602
        thd->lex->current_select->between_count)*m+1);
 
4603
  if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
 
4604
    return true; /* purecov: inspected */
582
4605
  and_level= 0;
583
4606
  field= end= key_fields;
 
4607
  *sargables= (SARGABLE_PARAM *) key_fields + 
 
4608
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
 
4609
  /* set a barrier for the array of SARGABLE_PARAM */
 
4610
  (*sargables)[0].field= 0; 
584
4611
 
585
 
  if (my_init_dynamic_array(keyuse, sizeof(optimizer::KeyUse), 20, 64))
 
4612
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
586
4613
    return true;
587
4614
  if (cond)
588
4615
  {
589
4616
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
590
4617
                   sargables);
591
 
    for (; field != end; field++)
 
4618
    for (; field != end ; field++)
592
4619
    {
593
 
      add_key_part(keyuse, field);
 
4620
      add_key_part(keyuse,field);
594
4621
      /* 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
 
      }
 
4622
      if (field->val->type() == Item::NULL_ITEM &&
 
4623
          !field->field->real_maybe_null())
 
4624
        field->field->table->reginfo.not_exists_optimize=1;
600
4625
    }
601
4626
  }
602
 
  for (uint32_t i= 0; i < tables; i++)
 
4627
  for (i=0 ; i < tables ; i++)
603
4628
  {
604
4629
    /*
605
4630
      Block the creation of keys for inner tables of outer joins.
609
4634
      In the future when we introduce conditional accesses
610
4635
      for inner tables in outer joins these keys will be taken
611
4636
      into account as well.
612
 
    */
 
4637
    */ 
613
4638
    if (*join_tab[i].on_expr_ref)
614
 
      add_key_fields(join_tab->join, &end, &and_level,
 
4639
      add_key_fields(join_tab->join, &end, &and_level, 
615
4640
                     *join_tab[i].on_expr_ref,
616
4641
                     join_tab[i].table->map, sargables);
617
4642
  }
618
4643
 
619
4644
  /* Process ON conditions for the nested joins */
620
4645
  {
621
 
    List<TableList>::iterator li(join_tab->join->join_list->begin());
 
4646
    List_iterator<TableList> li(*join_tab->join->join_list);
622
4647
    TableList *table;
623
4648
    while ((table= li++))
624
4649
    {
625
 
      if (table->getNestedJoin())
626
 
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
 
4650
      if (table->nested_join)
 
4651
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level, 
627
4652
                              sargables);
628
4653
    }
629
4654
  }
643
4668
  */
644
4669
  if (keyuse->elements)
645
4670
  {
646
 
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
 
4671
    KEYUSE key_end,*prev,*save_pos,*use;
647
4672
 
648
 
    internal::my_qsort(keyuse->buffer,keyuse->elements,sizeof(optimizer::KeyUse),
649
 
                       (qsort_cmp) sort_keyuse);
 
4673
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
 
4674
          (qsort_cmp) sort_keyuse);
650
4675
 
651
4676
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
652
4677
    insert_dynamic(keyuse,(unsigned char*) &key_end);
653
4678
 
654
 
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
 
4679
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
655
4680
    prev= &key_end;
656
 
    found_eq_constant= 0;
 
4681
    found_eq_constant=0;
 
4682
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
657
4683
    {
658
 
      uint32_t i;
659
 
 
660
 
      for (i= 0; i < keyuse->elements-1; i++, use++)
 
4684
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
 
4685
        use->table->const_key_parts[use->key]|= use->keypart_map;
661
4686
      {
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;
 
4687
        if (use->key == prev->key && use->table == prev->table)
 
4688
        {
 
4689
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
 
4690
            continue;                           /* remove */
 
4691
        }
 
4692
        else if (use->keypart != 0)             // First found must be 0
 
4693
          continue;
 
4694
      }
672
4695
 
673
 
#ifdef HAVE_VALGRIND
674
 
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
675
 
        if (save_pos != use)
 
4696
#ifdef HAVE_purify
 
4697
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
 
4698
      if (save_pos != use)
676
4699
#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;
 
4700
        *save_pos= *use;
 
4701
      prev=use;
 
4702
      found_eq_constant= !use->used_tables;
 
4703
      /* Save ptr to first use */
 
4704
      if (!use->table->reginfo.join_tab->keyuse)
 
4705
        use->table->reginfo.join_tab->keyuse=save_pos;
 
4706
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
 
4707
      save_pos++;
689
4708
    }
 
4709
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
 
4710
    set_dynamic(keyuse,(unsigned char*) &key_end,i);
 
4711
    keyuse->elements=i;
690
4712
  }
691
4713
  return false;
692
4714
}
694
4716
/**
695
4717
  Update some values in keyuse for faster choose_plan() loop.
696
4718
*/
697
 
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
 
4719
 
 
4720
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
698
4721
{
699
 
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
700
 
                                                  0, 
701
 
                                                  optimizer::KeyUse*);
 
4722
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
702
4723
 
703
4724
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
704
4725
  {
711
4732
      Constant tables are ignored.
712
4733
      To avoid bad matches, we don't make ref_table_rows less than 100.
713
4734
    */
714
 
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
715
 
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
 
4735
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
 
4736
    if (keyuse->used_tables &
 
4737
        (map= (keyuse->used_tables & ~join->const_table_map &
 
4738
               ~OUTER_REF_TABLE_BIT)))
716
4739
    {
717
4740
      uint32_t tablenr;
718
4741
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
719
4742
      if (map == 1)                     // Only one table
720
4743
      {
721
 
        Table *tmp_table=join->all_tables[tablenr];
722
 
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
 
4744
        Table *tmp_table=join->all_tables[tablenr];
 
4745
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
723
4746
      }
724
4747
    }
725
4748
    /*
726
4749
      Outer reference (external field) is constant for single executing
727
4750
      of subquery
728
4751
    */
729
 
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
730
 
      keyuse->setTableRows(1);
 
4752
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
 
4753
      keyuse->ref_table_rows= 1;
731
4754
  }
732
4755
}
733
4756
 
749
4772
  @return
750
4773
    None
751
4774
*/
752
 
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
 
4775
 
 
4776
static void
 
4777
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
753
4778
{
754
4779
  List<Item_field> indexed_fields;
755
 
  List<Item_field>::iterator indexed_fields_it(indexed_fields.begin());
756
 
  Order      *cur_group;
 
4780
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
 
4781
  order_st      *cur_group;
757
4782
  Item_field *cur_item;
758
4783
  key_map possible_keys(0);
759
4784
 
766
4791
  else if (join->select_distinct)
767
4792
  { /* Collect all query fields referenced in the SELECT clause. */
768
4793
    List<Item> &select_items= join->fields_list;
769
 
    List<Item>::iterator select_items_it(select_items.begin());
 
4794
    List_iterator<Item> select_items_it(select_items);
770
4795
    Item *item;
771
4796
    while ((item= select_items_it++))
772
4797
      item->walk(&Item::collect_item_field_processor, 0,
780
4805
 
781
4806
  /* Intersect the keys of all group fields. */
782
4807
  cur_item= indexed_fields_it++;
783
 
  possible_keys|= cur_item->field->part_of_key;
 
4808
  possible_keys.merge(cur_item->field->part_of_key);
784
4809
  while ((cur_item= indexed_fields_it++))
785
4810
  {
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
 
4811
    possible_keys.intersect(cur_item->field->part_of_key);
 
4812
  }
 
4813
 
 
4814
  if (!possible_keys.is_clear_all())
 
4815
    join_tab->const_keys.merge(possible_keys);
 
4816
}
 
4817
 
 
4818
 
 
4819
/*****************************************************************************
 
4820
  Go through all combinations of not marked tables and find the one
 
4821
  which uses least records
 
4822
*****************************************************************************/
 
4823
 
 
4824
/** Save const tables first as used tables. */
 
4825
 
 
4826
static void
 
4827
set_position(JOIN *join,uint32_t idx,JOIN_TAB *table,KEYUSE *key)
 
4828
{
 
4829
  join->positions[idx].table= table;
 
4830
  join->positions[idx].key=key;
 
4831
  join->positions[idx].records_read=1.0;        /* This is a const table */
 
4832
  join->positions[idx].ref_depend_map= 0;
 
4833
 
 
4834
  /* Move the const table as down as possible in best_ref */
 
4835
  JOIN_TAB **pos=join->best_ref+idx+1;
 
4836
  JOIN_TAB *next=join->best_ref[idx];
 
4837
  for (;next != table ; pos++)
 
4838
  {
 
4839
    JOIN_TAB *tmp=pos[0];
 
4840
    pos[0]=next;
 
4841
    next=tmp;
 
4842
  }
 
4843
  join->best_ref[idx]=table;
 
4844
}
 
4845
 
 
4846
 
 
4847
/*
 
4848
  Given a semi-join nest, find out which of the IN-equalities are bound
 
4849
 
 
4850
  SYNOPSIS
 
4851
    get_bound_sj_equalities()
 
4852
      sj_nest           Semi-join nest
 
4853
      remaining_tables  Tables that are not yet bound
 
4854
 
 
4855
  DESCRIPTION
 
4856
    Given a semi-join nest, find out which of the IN-equalities have their
 
4857
    left part expression bound (i.e. the said expression doesn't refer to
 
4858
    any of remaining_tables and can be evaluated).
 
4859
 
 
4860
  RETURN
 
4861
    Bitmap of bound IN-equalities.
 
4862
*/
 
4863
 
 
4864
uint64_t get_bound_sj_equalities(TableList *sj_nest, 
 
4865
                                  table_map remaining_tables)
 
4866
{
 
4867
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
 
4868
  Item *item;
 
4869
  uint32_t i= 0;
 
4870
  uint64_t res= 0;
 
4871
  while ((item= li++))
 
4872
  {
 
4873
    /*
 
4874
      Q: should this take into account equality propagation and how?
 
4875
      A: If e->outer_side is an Item_field, walk over the equality
 
4876
         class and see if there is an element that is bound?
 
4877
      (this is an optional feature)
 
4878
    */
 
4879
    if (!(item->used_tables() & remaining_tables))
 
4880
    {
 
4881
      res |= 1UL < i;
 
4882
    }
 
4883
  }
 
4884
  return res;
 
4885
}
 
4886
 
 
4887
 
 
4888
/**
 
4889
  Find the best access path for an extension of a partial execution
 
4890
  plan and add this path to the plan.
 
4891
 
 
4892
  The function finds the best access path to table 's' from the passed
 
4893
  partial plan where an access path is the general term for any means to
 
4894
  access the data in 's'. An access path may use either an index or a scan,
 
4895
  whichever is cheaper. The input partial plan is passed via the array
 
4896
  'join->positions' of length 'idx'. The chosen access method for 's' and its
 
4897
  cost are stored in 'join->positions[idx]'.
 
4898
 
 
4899
  @param join             pointer to the structure providing all context info
 
4900
                          for the query
 
4901
  @param s                the table to be joined by the function
 
4902
  @param thd              thread for the connection that submitted the query
 
4903
  @param remaining_tables set of tables not included into the partial plan yet
 
4904
  @param idx              the length of the partial plan
 
4905
  @param record_count     estimate for the number of records returned by the
 
4906
                          partial plan
 
4907
  @param read_time        the cost of the partial plan
 
4908
 
 
4909
  @return
 
4910
    None
 
4911
*/
 
4912
 
 
4913
static void
 
4914
best_access_path(JOIN      *join,
 
4915
                 JOIN_TAB  *s,
 
4916
                 THD       *thd,
 
4917
                 table_map remaining_tables,
 
4918
                 uint32_t      idx,
 
4919
                 double    record_count,
 
4920
                 double    read_time __attribute__((unused)))
 
4921
{
 
4922
  KEYUSE *best_key=         0;
 
4923
  uint32_t best_max_key_part=   0;
 
4924
  bool found_constraint= 0;
 
4925
  double best=              DBL_MAX;
 
4926
  double best_time=         DBL_MAX;
 
4927
  double records=           DBL_MAX;
 
4928
  table_map best_ref_depends_map= 0;
 
4929
  double tmp;
 
4930
  ha_rows rec;
 
4931
  uint32_t best_is_sj_inside_out=    0;
 
4932
 
 
4933
  if (s->keyuse)
 
4934
  {                                            /* Use key if possible */
 
4935
    Table *table= s->table;
 
4936
    KEYUSE *keyuse,*start_key=0;
 
4937
    double best_records= DBL_MAX;
 
4938
    uint32_t max_key_part=0;
 
4939
    uint64_t bound_sj_equalities= 0;
 
4940
    bool try_sj_inside_out= false;
 
4941
    /*
 
4942
      Discover the bound equalites. We need to do this, if
 
4943
        1. The next table is an SJ-inner table, and
 
4944
        2. It is the first table from that semijoin, and
 
4945
        3. We're not within a semi-join range (i.e. all semi-joins either have
 
4946
           all or none of their tables in join_table_map), except
 
4947
           s->emb_sj_nest (which we've just entered).
 
4948
        3. All correlation references from this sj-nest are bound
 
4949
    */
 
4950
    if (s->emb_sj_nest &&                                                 // (1)
 
4951
        s->emb_sj_nest->sj_in_exprs < 64 && 
 
4952
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
 
4953
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
 
4954
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
 
4955
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
 
4956
    {
 
4957
      /* This table is an InsideOut scan candidate */
 
4958
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest, 
 
4959
                                                   remaining_tables);
 
4960
      try_sj_inside_out= true;
 
4961
    }
 
4962
 
 
4963
    /* Test how we can use keys */
 
4964
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
 
4965
    for (keyuse=s->keyuse ; keyuse->table == table ;)
 
4966
    {
 
4967
      key_part_map found_part= 0;
 
4968
      table_map found_ref= 0;
 
4969
      uint32_t key= keyuse->key;
 
4970
      KEY *keyinfo= table->key_info+key;
 
4971
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
 
4972
      key_part_map const_part= 0;
 
4973
      /* The or-null keypart in ref-or-null access: */
 
4974
      key_part_map ref_or_null_part= 0;
 
4975
 
 
4976
      /* Calculate how many key segments of the current key we can use */
 
4977
      start_key= keyuse;
 
4978
      uint64_t handled_sj_equalities=0;
 
4979
      key_part_map sj_insideout_map= 0;
 
4980
 
 
4981
      do /* For each keypart */
 
4982
      {
 
4983
        uint32_t keypart= keyuse->keypart;
 
4984
        table_map best_part_found_ref= 0;
 
4985
        double best_prev_record_reads= DBL_MAX;
 
4986
        
 
4987
        do /* For each way to access the keypart */
 
4988
        {
 
4989
 
 
4990
          /*
 
4991
            if 1. expression doesn't refer to forward tables
 
4992
               2. we won't get two ref-or-null's
 
4993
          */
 
4994
          if (!(remaining_tables & keyuse->used_tables) &&
 
4995
              !(ref_or_null_part && (keyuse->optimize &
 
4996
                                     KEY_OPTIMIZE_REF_OR_NULL)))
 
4997
          {
 
4998
            found_part|= keyuse->keypart_map;
 
4999
            if (!(keyuse->used_tables & ~join->const_table_map))
 
5000
              const_part|= keyuse->keypart_map;
 
5001
 
 
5002
            double tmp2= prev_record_reads(join, idx, (found_ref |
 
5003
                                                      keyuse->used_tables));
 
5004
            if (tmp2 < best_prev_record_reads)
 
5005
            {
 
5006
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
 
5007
              best_prev_record_reads= tmp2;
 
5008
            }
 
5009
            if (rec > keyuse->ref_table_rows)
 
5010
              rec= keyuse->ref_table_rows;
 
5011
            /*
 
5012
              If there is one 'key_column IS NULL' expression, we can
 
5013
              use this ref_or_null optimisation of this field
 
5014
            */
 
5015
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
 
5016
              ref_or_null_part |= keyuse->keypart_map;
 
5017
          }
 
5018
 
 
5019
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
 
5020
          {
 
5021
            if (!(remaining_tables & keyuse->used_tables))
 
5022
              bound_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5023
            else
 
5024
            {
 
5025
              handled_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5026
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
 
5027
            }
 
5028
          }
 
5029
 
 
5030
          keyuse++;
 
5031
        } while (keyuse->table == table && keyuse->key == key &&
 
5032
                 keyuse->keypart == keypart);
 
5033
        found_ref|= best_part_found_ref;
 
5034
      } while (keyuse->table == table && keyuse->key == key);
 
5035
 
 
5036
      /*
 
5037
        Assume that that each key matches a proportional part of table.
 
5038
      */
 
5039
      if (!found_part && !handled_sj_equalities)
 
5040
        continue;                               // Nothing usable found
 
5041
 
 
5042
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
 
5043
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
 
5044
 
 
5045
      bool sj_inside_out_scan= false;
 
5046
      {
 
5047
        found_constraint= 1;
 
5048
        /*
 
5049
          Check if InsideOut scan is applicable:
 
5050
          1. All IN-equalities are either "bound" or "handled"
 
5051
          2. Index keyparts are 
 
5052
             ...
 
5053
        */
 
5054
        if (try_sj_inside_out && 
 
5055
            table->covering_keys.is_set(key) &&
 
5056
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
 
5057
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
 
5058
        {
 
5059
          uint32_t n_fixed_parts= max_part_bit(found_part);
 
5060
          if (n_fixed_parts != keyinfo->key_parts &&
 
5061
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
 
5062
               PREV_BITS(uint, keyinfo->key_parts))
 
5063
          {
 
5064
            /*
 
5065
              Not all parts are fixed. Produce bitmap of remaining bits and
 
5066
              check if all of them are covered.
 
5067
            */
 
5068
            sj_inside_out_scan= true;
 
5069
            if (!n_fixed_parts)
 
5070
            {
 
5071
              /*
 
5072
                It's a confluent ref scan.
 
5073
 
 
5074
                That is, all found KEYUSE elements refer to IN-equalities,
 
5075
                and there is really no ref access because there is no
 
5076
                  t.keypart0 = {bound expression}
 
5077
 
 
5078
                Calculate the cost of complete loose index scan.
 
5079
              */
 
5080
              records= (double)s->table->file->stats.records;
 
5081
 
 
5082
              /* The cost is entire index scan cost (divided by 2) */
 
5083
              best_time= s->table->file->index_only_read_time(key, records);
 
5084
 
 
5085
              /* Now figure how many different keys we will get */
 
5086
              ulong rpc;
 
5087
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5088
                records= records / rpc;
 
5089
              start_key= NULL;
 
5090
            }
 
5091
          }
 
5092
        }
 
5093
 
 
5094
        /*
 
5095
          Check if we found full key
 
5096
        */
 
5097
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
 
5098
            !ref_or_null_part)
 
5099
        {                                         /* use eq key */
 
5100
          max_key_part= UINT32_MAX;
 
5101
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
 
5102
          {
 
5103
            tmp = prev_record_reads(join, idx, found_ref);
 
5104
            records=1.0;
 
5105
          }
 
5106
          else
 
5107
          {
 
5108
            if (!found_ref)
 
5109
            {                                     /* We found a const key */
 
5110
              /*
 
5111
                ReuseRangeEstimateForRef-1:
 
5112
                We get here if we've found a ref(const) (c_i are constants):
 
5113
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
 
5114
                
 
5115
                If range optimizer was able to construct a "range" 
 
5116
                access on this index, then its condition "quick_cond" was
 
5117
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
 
5118
                from the range optimizer.
 
5119
                
 
5120
                Proof of (*): By properties of range and ref optimizers 
 
5121
                quick_cond will be equal or tighther than ref_const_cond. 
 
5122
                ref_const_cond already covers "smallest" possible interval - 
 
5123
                a singlepoint interval over all keyparts. Therefore, 
 
5124
                quick_cond is equivalent to ref_const_cond (if it was an 
 
5125
                empty interval we wouldn't have got here).
 
5126
              */
 
5127
              if (table->quick_keys.is_set(key))
 
5128
                records= (double) table->quick_rows[key];
 
5129
              else
 
5130
              {
 
5131
                /* quick_range couldn't use key! */
 
5132
                records= (double) s->records/rec;
 
5133
              }
 
5134
            }
 
5135
            else
 
5136
            {
 
5137
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5138
              {                                   /* Prefer longer keys */
 
5139
                records=
 
5140
                  ((double) s->records / (double) rec *
 
5141
                   (1.0 +
 
5142
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
 
5143
                     (double) table->s->max_key_length)));
 
5144
                if (records < 2.0)
 
5145
                  records=2.0;               /* Can't be as good as a unique */
 
5146
              }
 
5147
              /*
 
5148
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
 
5149
                E(#rows) from range optimizer. Make another try:
 
5150
                
 
5151
                If range optimizer produced E(#rows) for a prefix of the ref
 
5152
                access we're considering, and that E(#rows) is lower then our
 
5153
                current estimate, make an adjustment. The criteria of when we
 
5154
                can make an adjustment is a special case of the criteria used
 
5155
                in ReuseRangeEstimateForRef-3.
 
5156
              */
 
5157
              if (table->quick_keys.is_set(key) &&
 
5158
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5159
                  table->quick_n_ranges[key] == 1 &&
 
5160
                  records > (double) table->quick_rows[key])
 
5161
              {
 
5162
                records= (double) table->quick_rows[key];
 
5163
              }
 
5164
            }
 
5165
            /* Limit the number of matched rows */
 
5166
            tmp= records;
 
5167
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5168
            if (table->covering_keys.is_set(key))
 
5169
            {
 
5170
              /* we can use only index tree */
 
5171
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5172
            }
 
5173
            else
 
5174
              tmp= record_count*cmin(tmp,s->worst_seeks);
 
5175
          }
 
5176
        }
 
5177
        else
 
5178
        {
 
5179
          /*
 
5180
            Use as much key-parts as possible and a uniq key is better
 
5181
            than a not unique key
 
5182
            Set tmp to (previous record count) * (records / combination)
 
5183
          */
 
5184
          if ((found_part & 1) &&
 
5185
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
 
5186
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
 
5187
          {
 
5188
            max_key_part= max_part_bit(found_part);
 
5189
            /*
 
5190
              ReuseRangeEstimateForRef-3:
 
5191
              We're now considering a ref[or_null] access via
 
5192
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR  
 
5193
              (same-as-above but with one cond replaced 
 
5194
               with "t.keypart_i IS NULL")]  (**)
 
5195
              
 
5196
              Try re-using E(#rows) from "range" optimizer:
 
5197
              We can do so if "range" optimizer used the same intervals as
 
5198
              in (**). The intervals used by range optimizer may be not 
 
5199
              available at this point (as "range" access might have choosen to
 
5200
              create quick select over another index), so we can't compare
 
5201
              them to (**). We'll make indirect judgements instead.
 
5202
              The sufficient conditions for re-use are:
 
5203
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
 
5204
                   this is not satisfied we have no way to know which ranges
 
5205
                   will be actually scanned by 'ref' until we execute the 
 
5206
                   join)
 
5207
              (C2) max #key parts in 'range' access == K == max_key_part (this
 
5208
                   is apparently a necessary requirement)
 
5209
 
 
5210
              We also have a property that "range optimizer produces equal or 
 
5211
              tighter set of scan intervals than ref(const) optimizer". Each
 
5212
              of the intervals in (**) are "tightest possible" intervals when 
 
5213
              one limits itself to using keyparts 1..K (which we do in #2).              
 
5214
              From here it follows that range access used either one, or
 
5215
              both of the (I1) and (I2) intervals:
 
5216
              
 
5217
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1) 
 
5218
               (same-as-above but with one cond replaced  
 
5219
                with "t.keypart_i IS NULL")               (I2)
 
5220
 
 
5221
              The remaining part is to exclude the situation where range
 
5222
              optimizer used one interval while we're considering
 
5223
              ref-or-null and looking for estimate for two intervals. This
 
5224
              is done by last limitation:
 
5225
 
 
5226
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
 
5227
            */
 
5228
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
 
5229
                table->quick_key_parts[key] == max_key_part &&          //(C2)
 
5230
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
 
5231
            {
 
5232
              tmp= records= (double) table->quick_rows[key];
 
5233
            }
 
5234
            else
 
5235
            {
 
5236
              /* Check if we have statistic about the distribution */
 
5237
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
 
5238
              {
 
5239
                /* 
 
5240
                  Fix for the case where the index statistics is too
 
5241
                  optimistic: If 
 
5242
                  (1) We're considering ref(const) and there is quick select
 
5243
                      on the same index, 
 
5244
                  (2) and that quick select uses more keyparts (i.e. it will
 
5245
                      scan equal/smaller interval then this ref(const))
 
5246
                  (3) and E(#rows) for quick select is higher then our
 
5247
                      estimate,
 
5248
                  Then 
 
5249
                    We'll use E(#rows) from quick select.
 
5250
 
 
5251
                  Q: Why do we choose to use 'ref'? Won't quick select be
 
5252
                  cheaper in some cases ?
 
5253
                  TODO: figure this out and adjust the plan choice if needed.
 
5254
                */
 
5255
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
 
5256
                    table->quick_key_parts[key] > max_key_part &&     // (2)
 
5257
                    records < (double)table->quick_rows[key])         // (3)
 
5258
                  records= (double)table->quick_rows[key];
 
5259
 
 
5260
                tmp= records;
 
5261
              }
 
5262
              else
 
5263
              {
 
5264
                /*
 
5265
                  Assume that the first key part matches 1% of the file
 
5266
                  and that the whole key matches 10 (duplicates) or 1
 
5267
                  (unique) records.
 
5268
                  Assume also that more key matches proportionally more
 
5269
                  records
 
5270
                  This gives the formula:
 
5271
                  records = (x * (b-a) + a*c-b)/(c-1)
 
5272
 
 
5273
                  b = records matched by whole key
 
5274
                  a = records matched by first key part (1% of all records?)
 
5275
                  c = number of key parts in key
 
5276
                  x = used key parts (1 <= x <= c)
 
5277
                */
 
5278
                double rec_per_key;
 
5279
                if (!(rec_per_key=(double)
 
5280
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5281
                  rec_per_key=(double) s->records/rec+1;
 
5282
 
 
5283
                if (!s->records)
 
5284
                  tmp = 0;
 
5285
                else if (rec_per_key/(double) s->records >= 0.01)
 
5286
                  tmp = rec_per_key;
 
5287
                else
 
5288
                {
 
5289
                  double a=s->records*0.01;
 
5290
                  if (keyinfo->key_parts > 1)
 
5291
                    tmp= (max_key_part * (rec_per_key - a) +
 
5292
                          a*keyinfo->key_parts - rec_per_key)/
 
5293
                         (keyinfo->key_parts-1);
 
5294
                  else
 
5295
                    tmp= a;
 
5296
                  set_if_bigger(tmp,1.0);
 
5297
                }
 
5298
                records = (ulong) tmp;
 
5299
              }
 
5300
 
 
5301
              if (ref_or_null_part)
 
5302
              {
 
5303
                /* We need to do two key searches to find key */
 
5304
                tmp *= 2.0;
 
5305
                records *= 2.0;
 
5306
              }
 
5307
 
 
5308
              /*
 
5309
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
 
5310
                E(#rows) from range optimizer. Make another try:
 
5311
                
 
5312
                If range optimizer produced E(#rows) for a prefix of the ref 
 
5313
                access we're considering, and that E(#rows) is lower then our
 
5314
                current estimate, make the adjustment.
 
5315
 
 
5316
                The decision whether we can re-use the estimate from the range
 
5317
                optimizer is the same as in ReuseRangeEstimateForRef-3,
 
5318
                applied to first table->quick_key_parts[key] key parts.
 
5319
              */
 
5320
              if (table->quick_keys.is_set(key) &&
 
5321
                  table->quick_key_parts[key] <= max_key_part &&
 
5322
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5323
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
 
5324
                                                     const_part) ? 1 : 0) &&
 
5325
                  records > (double) table->quick_rows[key])
 
5326
              {
 
5327
                tmp= records= (double) table->quick_rows[key];
 
5328
              }
 
5329
            }
 
5330
 
 
5331
            /* Limit the number of matched rows */
 
5332
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5333
            if (table->covering_keys.is_set(key))
 
5334
            {
 
5335
              /* we can use only index tree */
 
5336
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5337
            }
 
5338
            else
 
5339
              tmp= record_count * cmin(tmp,s->worst_seeks);
 
5340
          }
 
5341
          else
 
5342
            tmp= best_time;                    // Do nothing
 
5343
        }
 
5344
 
 
5345
        if (sj_inside_out_scan && !start_key)
 
5346
        {
 
5347
          tmp= tmp/2;
 
5348
          if (records)
 
5349
            records= records/2;
 
5350
        }
 
5351
 
 
5352
      }
 
5353
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
 
5354
      {
 
5355
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
 
5356
        best= tmp;
 
5357
        best_records= records;
 
5358
        best_key= start_key;
 
5359
        best_max_key_part= max_key_part;
 
5360
        best_ref_depends_map= found_ref;
 
5361
        best_is_sj_inside_out= sj_inside_out_scan;
 
5362
      }
 
5363
    }
 
5364
    records= best_records;
 
5365
  }
 
5366
 
 
5367
  /*
 
5368
    Don't test table scan if it can't be better.
 
5369
    Prefer key lookup if we would use the same key for scanning.
 
5370
 
 
5371
    Don't do a table scan on InnoDB tables, if we can read the used
 
5372
    parts of the row from any of the used index.
 
5373
    This is because table scans uses index and we would not win
 
5374
    anything by using a table scan.
 
5375
 
 
5376
    A word for word translation of the below if-statement in sergefp's
 
5377
    understanding: we check if we should use table scan if:
 
5378
    (1) The found 'ref' access produces more records than a table scan
 
5379
        (or index scan, or quick select), or 'ref' is more expensive than
 
5380
        any of them.
 
5381
    (2) This doesn't hold: the best way to perform table scan is to to perform
 
5382
        'range' access using index IDX, and the best way to perform 'ref' 
 
5383
        access is to use the same index IDX, with the same or more key parts.
 
5384
        (note: it is not clear how this rule is/should be extended to 
 
5385
        index_merge quick selects)
 
5386
    (3) See above note about InnoDB.
 
5387
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
 
5388
             path, but there is no quick select)
 
5389
        If the condition in the above brackets holds, then the only possible
 
5390
        "table scan" access method is ALL/index (there is no quick select).
 
5391
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
 
5392
        choose it over ALL/index, there is no need to consider a full table
 
5393
        scan.
 
5394
  */
 
5395
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
 
5396
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
 
5397
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
 
5398
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
 
5399
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
 
5400
      !(s->table->force_index && best_key && !s->quick))                 // (4)
 
5401
  {                                             // Check full join
 
5402
    ha_rows rnd_records= s->found_records;
 
5403
    /*
 
5404
      If there is a filtering condition on the table (i.e. ref analyzer found
 
5405
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
 
5406
      preceding this table in the join order we're now considering), then 
 
5407
      assume that 25% of the rows will be filtered out by this condition.
 
5408
 
 
5409
      This heuristic is supposed to force tables used in exprZ to be before
 
5410
      this table in join order.
 
5411
    */
 
5412
    if (found_constraint)
 
5413
      rnd_records-= rnd_records/4;
 
5414
 
 
5415
    /*
 
5416
      If applicable, get a more accurate estimate. Don't use the two
 
5417
      heuristics at once.
 
5418
    */
 
5419
    if (s->table->quick_condition_rows != s->found_records)
 
5420
      rnd_records= s->table->quick_condition_rows;
 
5421
 
 
5422
    /*
 
5423
      Range optimizer never proposes a RANGE if it isn't better
 
5424
      than FULL: so if RANGE is present, it's always preferred to FULL.
 
5425
      Here we estimate its cost.
 
5426
    */
 
5427
    if (s->quick)
 
5428
    {
 
5429
      /*
 
5430
        For each record we:
 
5431
        - read record range through 'quick'
 
5432
        - skip rows which does not satisfy WHERE constraints
 
5433
        TODO: 
 
5434
        We take into account possible use of join cache for ALL/index
 
5435
        access (see first else-branch below), but we don't take it into 
 
5436
        account here for range/index_merge access. Find out why this is so.
 
5437
      */
 
5438
      tmp= record_count *
 
5439
        (s->quick->read_time +
 
5440
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5441
    }
 
5442
    else
 
5443
    {
 
5444
      /* Estimate cost of reading table. */
 
5445
      tmp= s->table->file->scan_time();
 
5446
      if (s->table->map & join->outer_join)     // Can't use join cache
 
5447
      {
 
5448
        /*
 
5449
          For each record we have to:
 
5450
          - read the whole table record 
 
5451
          - skip rows which does not satisfy join condition
 
5452
        */
 
5453
        tmp= record_count *
 
5454
          (tmp +
 
5455
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5456
      }
 
5457
      else
 
5458
      {
 
5459
        /* We read the table as many times as join buffer becomes full. */
 
5460
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
 
5461
                           record_count /
 
5462
                           (double) thd->variables.join_buff_size));
 
5463
        /* 
 
5464
            We don't make full cartesian product between rows in the scanned
 
5465
           table and existing records because we skip all rows from the
 
5466
           scanned table, which does not satisfy join condition when 
 
5467
           we read the table (see flush_cached_records for details). Here we
 
5468
           take into account cost to read and skip these records.
 
5469
        */
 
5470
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
 
5471
      }
 
5472
    }
 
5473
 
 
5474
    /*
 
5475
      We estimate the cost of evaluating WHERE clause for found records
 
5476
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
 
5477
      tmp give us total cost of using Table SCAN
 
5478
    */
 
5479
    if (best == DBL_MAX ||
 
5480
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
 
5481
         best + record_count/(double) TIME_FOR_COMPARE*records))
 
5482
    {
 
5483
      /*
 
5484
        If the table has a range (s->quick is set) make_join_select()
 
5485
        will ensure that this will be used
 
5486
      */
 
5487
      best= tmp;
 
5488
      records= rows2double(rnd_records);
 
5489
      best_key= 0;
 
5490
      /* range/index_merge/ALL/index access method are "independent", so: */
 
5491
      best_ref_depends_map= 0;
 
5492
      best_is_sj_inside_out= false;
 
5493
    }
 
5494
  }
 
5495
 
 
5496
  /* Update the cost information for the current partial plan */
 
5497
  join->positions[idx].records_read= records;
 
5498
  join->positions[idx].read_time=    best;
 
5499
  join->positions[idx].key=          best_key;
 
5500
  join->positions[idx].table=        s;
 
5501
  join->positions[idx].ref_depend_map= best_ref_depends_map;
 
5502
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
 
5503
 
 
5504
  if (!best_key &&
 
5505
      idx == join->const_tables &&
 
5506
      s->table == join->sort_by_table &&
 
5507
      join->unit->select_limit_cnt >= records)
 
5508
    join->sort_by_table= (Table*) 1;  // Must use temporary table
 
5509
 
 
5510
  return;
 
5511
}
 
5512
 
 
5513
 
 
5514
/**
 
5515
  Selects and invokes a search strategy for an optimal query plan.
 
5516
 
 
5517
  The function checks user-configurable parameters that control the search
 
5518
  strategy for an optimal plan, selects the search method and then invokes
 
5519
  it. Each specific optimization procedure stores the final optimal plan in
 
5520
  the array 'join->best_positions', and the cost of the plan in
 
5521
  'join->best_read'.
 
5522
 
 
5523
  @param join         pointer to the structure providing all context info for
 
5524
                      the query
 
5525
  @param join_tables  set of the tables in the query
 
5526
 
 
5527
  @todo
 
5528
    'MAX_TABLES+2' denotes the old implementation of find_best before
 
5529
    the greedy version. Will be removed when greedy_search is approved.
 
5530
 
 
5531
  @retval
 
5532
    false       ok
 
5533
  @retval
 
5534
    true        Fatal error
 
5535
*/
 
5536
 
 
5537
static bool
 
5538
choose_plan(JOIN *join, table_map join_tables)
 
5539
{
 
5540
  uint32_t search_depth= join->thd->variables.optimizer_search_depth;
 
5541
  uint32_t prune_level=  join->thd->variables.optimizer_prune_level;
 
5542
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
5543
 
 
5544
  join->cur_embedding_map= 0;
 
5545
  reset_nj_counters(join->join_list);
 
5546
  /*
 
5547
    if (SELECT_STRAIGHT_JOIN option is set)
 
5548
      reorder tables so dependent tables come after tables they depend 
 
5549
      on, otherwise keep tables in the order they were specified in the query 
 
5550
    else
 
5551
      Apply heuristic: pre-sort all access plans with respect to the number of
 
5552
      records accessed.
 
5553
  */
 
5554
  my_qsort(join->best_ref + join->const_tables,
 
5555
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
 
5556
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
 
5557
  join->cur_emb_sj_nests= 0;
 
5558
  if (straight_join)
 
5559
  {
 
5560
    optimize_straight_join(join, join_tables);
 
5561
  }
 
5562
  else
 
5563
  {
 
5564
    if (search_depth == MAX_TABLES+2)
 
5565
    { /*
 
5566
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
 
5567
        the greedy version. Will be removed when greedy_search is approved.
 
5568
      */
 
5569
      join->best_read= DBL_MAX;
 
5570
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
 
5571
        return(true);
 
5572
    } 
 
5573
    else
 
5574
    {
 
5575
      if (search_depth == 0)
 
5576
        /* Automatically determine a reasonable value for 'search_depth' */
 
5577
        search_depth= determine_search_depth(join);
 
5578
      if (greedy_search(join, join_tables, search_depth, prune_level))
 
5579
        return(true);
 
5580
    }
 
5581
  }
 
5582
 
 
5583
  /* 
 
5584
    Store the cost of this query into a user variable
 
5585
    Don't update last_query_cost for statements that are not "flat joins" :
 
5586
    i.e. they have subqueries, unions or call stored procedures.
 
5587
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
 
5588
  */
 
5589
  if (join->thd->lex->is_single_level_stmt())
 
5590
    join->thd->status_var.last_query_cost= join->best_read;
 
5591
  return(false);
 
5592
}
 
5593
 
 
5594
 
 
5595
/**
 
5596
  Compare two JOIN_TAB objects based on the number of accessed records.
 
5597
 
 
5598
  @param ptr1 pointer to first JOIN_TAB object
 
5599
  @param ptr2 pointer to second JOIN_TAB object
798
5600
 
799
5601
  NOTES
800
5602
    The order relation implemented by join_tab_cmp() is not transitive,
806
5608
      a: dependent = 0x0 table->map = 0x1 found_records = 3 ptr = 0x907e6b0
807
5609
      b: dependent = 0x0 table->map = 0x2 found_records = 3 ptr = 0x907e838
808
5610
      c: dependent = 0x6 table->map = 0x10 found_records = 2 ptr = 0x907ecd0
809
 
 
 
5611
     
810
5612
  @retval
811
5613
    1  if first is bigger
812
5614
  @retval
814
5616
  @retval
815
5617
    0  if equal
816
5618
*/
817
 
int join_tab_cmp(const void* ptr1, const void* ptr2)
 
5619
 
 
5620
static int
 
5621
join_tab_cmp(const void* ptr1, const void* ptr2)
818
5622
{
819
 
  JoinTable *jt1= *(JoinTable**) ptr1;
820
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5623
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5624
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
821
5625
 
822
5626
  if (jt1->dependent & jt2->table->map)
823
5627
    return 1;
824
5628
  if (jt2->dependent & jt1->table->map)
825
 
    return -1;
 
5629
    return -1;  
826
5630
  if (jt1->found_records > jt2->found_records)
827
5631
    return 1;
828
5632
  if (jt1->found_records < jt2->found_records)
829
 
    return -1;
 
5633
    return -1; 
830
5634
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
831
5635
}
832
5636
 
 
5637
 
833
5638
/**
834
5639
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
835
5640
*/
836
 
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
 
5641
 
 
5642
static int
 
5643
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
837
5644
{
838
 
  JoinTable *jt1= *(JoinTable**) ptr1;
839
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5645
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5646
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
840
5647
 
841
5648
  if (jt1->dependent & jt2->table->map)
842
5649
    return 1;
846
5653
}
847
5654
 
848
5655
/**
 
5656
  Heuristic procedure to automatically guess a reasonable degree of
 
5657
  exhaustiveness for the greedy search procedure.
 
5658
 
 
5659
  The procedure estimates the optimization time and selects a search depth
 
5660
  big enough to result in a near-optimal QEP, that doesn't take too long to
 
5661
  find. If the number of tables in the query exceeds some constant, then
 
5662
  search_depth is set to this constant.
 
5663
 
 
5664
  @param join   pointer to the structure providing all context info for
 
5665
                the query
 
5666
 
 
5667
  @note
 
5668
    This is an extremely simplistic implementation that serves as a stub for a
 
5669
    more advanced analysis of the join. Ideally the search depth should be
 
5670
    determined by learning from previous query optimizations, because it will
 
5671
    depend on the CPU power (and other factors).
 
5672
 
 
5673
  @todo
 
5674
    this value should be determined dynamically, based on statistics:
 
5675
    uint32_t max_tables_for_exhaustive_opt= 7;
 
5676
 
 
5677
  @todo
 
5678
    this value could be determined by some mapping of the form:
 
5679
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5680
 
 
5681
  @return
 
5682
    A positive integer that specifies the search depth (and thus the
 
5683
    exhaustiveness) of the depth-first search algorithm used by
 
5684
    'greedy_search'.
 
5685
*/
 
5686
 
 
5687
static uint
 
5688
determine_search_depth(JOIN *join)
 
5689
{
 
5690
  uint32_t table_count=  join->tables - join->const_tables;
 
5691
  uint32_t search_depth;
 
5692
  /* TODO: this value should be determined dynamically, based on statistics: */
 
5693
  uint32_t max_tables_for_exhaustive_opt= 7;
 
5694
 
 
5695
  if (table_count <= max_tables_for_exhaustive_opt)
 
5696
    search_depth= table_count+1; // use exhaustive for small number of tables
 
5697
  else
 
5698
    /*
 
5699
      TODO: this value could be determined by some mapping of the form:
 
5700
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5701
    */
 
5702
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
 
5703
 
 
5704
  return search_depth;
 
5705
}
 
5706
 
 
5707
 
 
5708
/**
 
5709
  Select the best ways to access the tables in a query without reordering them.
 
5710
 
 
5711
    Find the best access paths for each query table and compute their costs
 
5712
    according to their order in the array 'join->best_ref' (thus without
 
5713
    reordering the join tables). The function calls sequentially
 
5714
    'best_access_path' for each table in the query to select the best table
 
5715
    access method. The final optimal plan is stored in the array
 
5716
    'join->best_positions', and the corresponding cost in 'join->best_read'.
 
5717
 
 
5718
  @param join          pointer to the structure providing all context info for
 
5719
                       the query
 
5720
  @param join_tables   set of the tables in the query
 
5721
 
 
5722
  @note
 
5723
    This function can be applied to:
 
5724
    - queries with STRAIGHT_JOIN
 
5725
    - internally to compute the cost of an arbitrary QEP
 
5726
  @par
 
5727
    Thus 'optimize_straight_join' can be used at any stage of the query
 
5728
    optimization process to finalize a QEP as it is.
 
5729
*/
 
5730
 
 
5731
static void
 
5732
optimize_straight_join(JOIN *join, table_map join_tables)
 
5733
{
 
5734
  JOIN_TAB *s;
 
5735
  uint32_t idx= join->const_tables;
 
5736
  double    record_count= 1.0;
 
5737
  double    read_time=    0.0;
 
5738
 
 
5739
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
5740
  {
 
5741
    /* Find the best access method from 's' to the current partial plan */
 
5742
    advance_sj_state(join_tables, s);
 
5743
    best_access_path(join, s, join->thd, join_tables, idx,
 
5744
                     record_count, read_time);
 
5745
    /* compute the cost of the new plan extended with 's' */
 
5746
    record_count*= join->positions[idx].records_read;
 
5747
    read_time+=    join->positions[idx].read_time;
 
5748
    join_tables&= ~(s->table->map);
 
5749
    ++idx;
 
5750
  }
 
5751
 
 
5752
  read_time+= record_count / (double) TIME_FOR_COMPARE;
 
5753
  if (join->sort_by_table &&
 
5754
      join->sort_by_table != join->positions[join->const_tables].table->table)
 
5755
    read_time+= record_count;  // We have to make a temp table
 
5756
  memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
5757
  join->best_read= read_time;
 
5758
}
 
5759
 
 
5760
 
 
5761
/**
 
5762
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
 
5763
 
 
5764
    The search procedure uses a hybrid greedy/exhaustive search with controlled
 
5765
    exhaustiveness. The search is performed in N = card(remaining_tables)
 
5766
    steps. Each step evaluates how promising is each of the unoptimized tables,
 
5767
    selects the most promising table, and extends the current partial QEP with
 
5768
    that table.  Currenly the most 'promising' table is the one with least
 
5769
    expensive extension.\
 
5770
 
 
5771
    There are two extreme cases:
 
5772
    -# When (card(remaining_tables) < search_depth), the estimate finds the
 
5773
    best complete continuation of the partial QEP. This continuation can be
 
5774
    used directly as a result of the search.
 
5775
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
 
5776
    consideres the extension of the current QEP with each of the remaining
 
5777
    unoptimized tables.
 
5778
 
 
5779
    All other cases are in-between these two extremes. Thus the parameter
 
5780
    'search_depth' controlls the exhaustiveness of the search. The higher the
 
5781
    value, the longer the optimizaton time and possibly the better the
 
5782
    resulting plan. The lower the value, the fewer alternative plans are
 
5783
    estimated, but the more likely to get a bad QEP.
 
5784
 
 
5785
    All intermediate and final results of the procedure are stored in 'join':
 
5786
    - join->positions     : modified for every partial QEP that is explored
 
5787
    - join->best_positions: modified for the current best complete QEP
 
5788
    - join->best_read     : modified for the current best complete QEP
 
5789
    - join->best_ref      : might be partially reordered
 
5790
 
 
5791
    The final optimal plan is stored in 'join->best_positions', and its
 
5792
    corresponding cost in 'join->best_read'.
 
5793
 
 
5794
  @note
 
5795
    The following pseudocode describes the algorithm of 'greedy_search':
 
5796
 
 
5797
    @code
 
5798
    procedure greedy_search
 
5799
    input: remaining_tables
 
5800
    output: pplan;
 
5801
    {
 
5802
      pplan = <>;
 
5803
      do {
 
5804
        (t, a) = best_extension(pplan, remaining_tables);
 
5805
        pplan = concat(pplan, (t, a));
 
5806
        remaining_tables = remaining_tables - t;
 
5807
      } while (remaining_tables != {})
 
5808
      return pplan;
 
5809
    }
 
5810
 
 
5811
  @endcode
 
5812
    where 'best_extension' is a placeholder for a procedure that selects the
 
5813
    most "promising" of all tables in 'remaining_tables'.
 
5814
    Currently this estimate is performed by calling
 
5815
    'best_extension_by_limited_search' to evaluate all extensions of the
 
5816
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
 
5817
    mainly depends on that of 'best_extension_by_limited_search'.
 
5818
 
 
5819
  @par
 
5820
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
 
5821
    worst-case complexity of this algorithm is <=
 
5822
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
 
5823
    complexity of greedy_search is O(N!).
 
5824
 
 
5825
  @par
 
5826
    In the future, 'greedy_search' might be extended to support other
 
5827
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
 
5828
 
 
5829
  @param join             pointer to the structure providing all context info
 
5830
                          for the query
 
5831
  @param remaining_tables set of tables not included into the partial plan yet
 
5832
  @param search_depth     controlls the exhaustiveness of the search
 
5833
  @param prune_level      the pruning heuristics that should be applied during
 
5834
                          search
 
5835
 
 
5836
  @retval
 
5837
    false       ok
 
5838
  @retval
 
5839
    true        Fatal error
 
5840
*/
 
5841
 
 
5842
static bool
 
5843
greedy_search(JOIN      *join,
 
5844
              table_map remaining_tables,
 
5845
              uint32_t      search_depth,
 
5846
              uint32_t      prune_level)
 
5847
{
 
5848
  double    record_count= 1.0;
 
5849
  double    read_time=    0.0;
 
5850
  uint32_t      idx= join->const_tables; // index into 'join->best_ref'
 
5851
  uint32_t      best_idx;
 
5852
  uint32_t      size_remain;    // cardinality of remaining_tables
 
5853
  POSITION  best_pos;
 
5854
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
 
5855
 
 
5856
  /* number of tables that remain to be optimized */
 
5857
  size_remain= my_count_bits(remaining_tables);
 
5858
 
 
5859
  do {
 
5860
    /* Find the extension of the current QEP with the lowest cost */
 
5861
    join->best_read= DBL_MAX;
 
5862
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
 
5863
                                         read_time, search_depth, prune_level))
 
5864
      return(true);
 
5865
 
 
5866
    if (size_remain <= search_depth)
 
5867
    {
 
5868
      /*
 
5869
        'join->best_positions' contains a complete optimal extension of the
 
5870
        current partial QEP.
 
5871
      */
 
5872
      return(false);
 
5873
    }
 
5874
 
 
5875
    /* select the first table in the optimal extension as most promising */
 
5876
    best_pos= join->best_positions[idx];
 
5877
    best_table= best_pos.table;
 
5878
    /*
 
5879
      Each subsequent loop of 'best_extension_by_limited_search' uses
 
5880
      'join->positions' for cost estimates, therefore we have to update its
 
5881
      value.
 
5882
    */
 
5883
    join->positions[idx]= best_pos;
 
5884
 
 
5885
    /* find the position of 'best_table' in 'join->best_ref' */
 
5886
    best_idx= idx;
 
5887
    JOIN_TAB *pos= join->best_ref[best_idx];
 
5888
    while (pos && best_table != pos)
 
5889
      pos= join->best_ref[++best_idx];
 
5890
    assert((pos != NULL)); // should always find 'best_table'
 
5891
    /* move 'best_table' at the first free position in the array of joins */
 
5892
    std::swap(join->best_ref[idx], join->best_ref[best_idx]);
 
5893
 
 
5894
    /* compute the cost of the new plan extended with 'best_table' */
 
5895
    record_count*= join->positions[idx].records_read;
 
5896
    read_time+=    join->positions[idx].read_time;
 
5897
 
 
5898
    remaining_tables&= ~(best_table->table->map);
 
5899
    --size_remain;
 
5900
    ++idx;
 
5901
  } while (true);
 
5902
}
 
5903
 
 
5904
 
 
5905
/**
 
5906
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
 
5907
  exhaustive search.
 
5908
 
 
5909
    The procedure searches for the optimal ordering of the query tables in set
 
5910
    'remaining_tables' of size N, and the corresponding optimal access paths to
 
5911
    each table. The choice of a table order and an access path for each table
 
5912
    constitutes a query execution plan (QEP) that fully specifies how to
 
5913
    execute the query.
 
5914
   
 
5915
    The maximal size of the found plan is controlled by the parameter
 
5916
    'search_depth'. When search_depth == N, the resulting plan is complete and
 
5917
    can be used directly as a QEP. If search_depth < N, the found plan consists
 
5918
    of only some of the query tables. Such "partial" optimal plans are useful
 
5919
    only as input to query optimization procedures, and cannot be used directly
 
5920
    to execute a query.
 
5921
 
 
5922
    The algorithm begins with an empty partial plan stored in 'join->positions'
 
5923
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
 
5924
    evaluates the cost of the partial plan extended by all access plans for
 
5925
    each of the relations in 'remaining_tables', expands the current partial
 
5926
    plan with the access plan that results in lowest cost of the expanded
 
5927
    partial plan, and removes the corresponding relation from
 
5928
    'remaining_tables'. The algorithm continues until it either constructs a
 
5929
    complete optimal plan, or constructs an optimal plartial plan with size =
 
5930
    search_depth.
 
5931
 
 
5932
    The final optimal plan is stored in 'join->best_positions'. The
 
5933
    corresponding cost of the optimal plan is in 'join->best_read'.
 
5934
 
 
5935
  @note
 
5936
    The procedure uses a recursive depth-first search where the depth of the
 
5937
    recursion (and thus the exhaustiveness of the search) is controlled by the
 
5938
    parameter 'search_depth'.
 
5939
 
 
5940
  @note
 
5941
    The pseudocode below describes the algorithm of
 
5942
    'best_extension_by_limited_search'. The worst-case complexity of this
 
5943
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
 
5944
    the complexity of greedy_search is O(N!).
 
5945
 
 
5946
    @code
 
5947
    procedure best_extension_by_limited_search(
 
5948
      pplan in,             // in, partial plan of tables-joined-so-far
 
5949
      pplan_cost,           // in, cost of pplan
 
5950
      remaining_tables,     // in, set of tables not referenced in pplan
 
5951
      best_plan_so_far,     // in/out, best plan found so far
 
5952
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
 
5953
      search_depth)         // in, maximum size of the plans being considered
 
5954
    {
 
5955
      for each table T from remaining_tables
 
5956
      {
 
5957
        // Calculate the cost of using table T as above
 
5958
        cost = complex-series-of-calculations;
 
5959
 
 
5960
        // Add the cost to the cost so far.
 
5961
        pplan_cost+= cost;
 
5962
 
 
5963
        if (pplan_cost >= best_plan_so_far_cost)
 
5964
          // pplan_cost already too great, stop search
 
5965
          continue;
 
5966
 
 
5967
        pplan= expand pplan by best_access_method;
 
5968
        remaining_tables= remaining_tables - table T;
 
5969
        if (remaining_tables is not an empty set
 
5970
            and
 
5971
            search_depth > 1)
 
5972
        {
 
5973
          best_extension_by_limited_search(pplan, pplan_cost,
 
5974
                                           remaining_tables,
 
5975
                                           best_plan_so_far,
 
5976
                                           best_plan_so_far_cost,
 
5977
                                           search_depth - 1);
 
5978
        }
 
5979
        else
 
5980
        {
 
5981
          best_plan_so_far_cost= pplan_cost;
 
5982
          best_plan_so_far= pplan;
 
5983
        }
 
5984
      }
 
5985
    }
 
5986
    @endcode
 
5987
 
 
5988
  @note
 
5989
    When 'best_extension_by_limited_search' is called for the first time,
 
5990
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
 
5991
    The actual implementation provides a way to optionally use pruning
 
5992
    heuristic (controlled by the parameter 'prune_level') to reduce the search
 
5993
    space by skipping some partial plans.
 
5994
 
 
5995
  @note
 
5996
    The parameter 'search_depth' provides control over the recursion
 
5997
    depth, and thus the size of the resulting optimal plan.
 
5998
 
 
5999
  @param join             pointer to the structure providing all context info
 
6000
                          for the query
 
6001
  @param remaining_tables set of tables not included into the partial plan yet
 
6002
  @param idx              length of the partial QEP in 'join->positions';
 
6003
                          since a depth-first search is used, also corresponds
 
6004
                          to the current depth of the search tree;
 
6005
                          also an index in the array 'join->best_ref';
 
6006
  @param record_count     estimate for the number of records returned by the
 
6007
                          best partial plan
 
6008
  @param read_time        the cost of the best partial plan
 
6009
  @param search_depth     maximum depth of the recursion and thus size of the
 
6010
                          found optimal plan
 
6011
                          (0 < search_depth <= join->tables+1).
 
6012
  @param prune_level      pruning heuristics that should be applied during
 
6013
                          optimization
 
6014
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
 
6015
 
 
6016
  @retval
 
6017
    false       ok
 
6018
  @retval
 
6019
    true        Fatal error
 
6020
*/
 
6021
 
 
6022
static bool
 
6023
best_extension_by_limited_search(JOIN      *join,
 
6024
                                 table_map remaining_tables,
 
6025
                                 uint32_t      idx,
 
6026
                                 double    record_count,
 
6027
                                 double    read_time,
 
6028
                                 uint32_t      search_depth,
 
6029
                                 uint32_t      prune_level)
 
6030
{
 
6031
  THD *thd= join->thd;
 
6032
  if (thd->killed)  // Abort
 
6033
    return(true);
 
6034
 
 
6035
  /* 
 
6036
     'join' is a partial plan with lower cost than the best plan so far,
 
6037
     so continue expanding it further with the tables in 'remaining_tables'.
 
6038
  */
 
6039
  JOIN_TAB *s;
 
6040
  double best_record_count= DBL_MAX;
 
6041
  double best_read_time=    DBL_MAX;
 
6042
 
 
6043
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
6044
  {
 
6045
    table_map real_table_bit= s->table->map;
 
6046
    if ((remaining_tables & real_table_bit) && 
 
6047
        !(remaining_tables & s->dependent) && 
 
6048
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6049
    {
 
6050
      double current_record_count, current_read_time;
 
6051
      advance_sj_state(remaining_tables, s);
 
6052
 
 
6053
      /*
 
6054
        psergey-insideout-todo: 
 
6055
          when best_access_path() detects it could do an InsideOut scan or 
 
6056
          some other scan, have it return an insideout scan and a flag that 
 
6057
          requests to "fork" this loop iteration. (Q: how does that behave 
 
6058
          when the depth is insufficient??)
 
6059
      */
 
6060
      /* Find the best access method from 's' to the current partial plan */
 
6061
      best_access_path(join, s, thd, remaining_tables, idx,
 
6062
                       record_count, read_time);
 
6063
      /* Compute the cost of extending the plan with 's' */
 
6064
      current_record_count= record_count * join->positions[idx].records_read;
 
6065
      current_read_time=    read_time + join->positions[idx].read_time;
 
6066
 
 
6067
      /* Expand only partial plans with lower cost than the best QEP so far */
 
6068
      if ((current_read_time +
 
6069
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
 
6070
      {
 
6071
        restore_prev_nj_state(s);
 
6072
        restore_prev_sj_state(remaining_tables, s);
 
6073
        continue;
 
6074
      }
 
6075
 
 
6076
      /*
 
6077
        Prune some less promising partial plans. This heuristic may miss
 
6078
        the optimal QEPs, thus it results in a non-exhaustive search.
 
6079
      */
 
6080
      if (prune_level == 1)
 
6081
      {
 
6082
        if (best_record_count > current_record_count ||
 
6083
            best_read_time > current_read_time ||
 
6084
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
 
6085
        {
 
6086
          if (best_record_count >= current_record_count &&
 
6087
              best_read_time >= current_read_time &&
 
6088
              /* TODO: What is the reasoning behind this condition? */
 
6089
              (!(s->key_dependent & remaining_tables) ||
 
6090
               join->positions[idx].records_read < 2.0))
 
6091
          {
 
6092
            best_record_count= current_record_count;
 
6093
            best_read_time=    current_read_time;
 
6094
          }
 
6095
        }
 
6096
        else
 
6097
        {
 
6098
          restore_prev_nj_state(s);
 
6099
          restore_prev_sj_state(remaining_tables, s);
 
6100
          continue;
 
6101
        }
 
6102
      }
 
6103
 
 
6104
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
 
6105
      { /* Recursively expand the current partial plan */
 
6106
        std::swap(join->best_ref[idx], *pos);
 
6107
        if (best_extension_by_limited_search(join,
 
6108
                                             remaining_tables & ~real_table_bit,
 
6109
                                             idx + 1,
 
6110
                                             current_record_count,
 
6111
                                             current_read_time,
 
6112
                                             search_depth - 1,
 
6113
                                             prune_level))
 
6114
          return(true);
 
6115
        std::swap(join->best_ref[idx], *pos);
 
6116
      }
 
6117
      else
 
6118
      { /*
 
6119
          'join' is either the best partial QEP with 'search_depth' relations,
 
6120
          or the best complete QEP so far, whichever is smaller.
 
6121
        */
 
6122
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
 
6123
        if (join->sort_by_table &&
 
6124
            join->sort_by_table !=
 
6125
            join->positions[join->const_tables].table->table)
 
6126
          /* We have to make a temp table */
 
6127
          current_read_time+= current_record_count;
 
6128
        if ((search_depth == 1) || (current_read_time < join->best_read))
 
6129
        {
 
6130
          memcpy(join->best_positions, join->positions,
 
6131
                 sizeof(POSITION) * (idx + 1));
 
6132
          join->best_read= current_read_time - 0.001;
 
6133
        }
 
6134
      }
 
6135
      restore_prev_nj_state(s);
 
6136
      restore_prev_sj_state(remaining_tables, s);
 
6137
    }
 
6138
  }
 
6139
  return(false);
 
6140
}
 
6141
 
 
6142
 
 
6143
/**
 
6144
  @todo
 
6145
  - TODO: this function is here only temporarily until 'greedy_search' is
 
6146
  tested and accepted.
 
6147
 
 
6148
  RETURN VALUES
 
6149
    false       ok
 
6150
    true        Fatal error
 
6151
*/
 
6152
static bool
 
6153
find_best(JOIN *join,table_map rest_tables,uint32_t idx,double record_count,
 
6154
          double read_time)
 
6155
{
 
6156
  THD *thd= join->thd;
 
6157
  if (thd->killed)
 
6158
    return(true);
 
6159
  if (!rest_tables)
 
6160
  {
 
6161
    read_time+=record_count/(double) TIME_FOR_COMPARE;
 
6162
    if (join->sort_by_table &&
 
6163
        join->sort_by_table !=
 
6164
        join->positions[join->const_tables].table->table)
 
6165
      read_time+=record_count;                  // We have to make a temp table
 
6166
    if (read_time < join->best_read)
 
6167
    {
 
6168
      memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
6169
      join->best_read= read_time - 0.001;
 
6170
    }
 
6171
    return(false);
 
6172
  }
 
6173
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
 
6174
    return(false);                                      /* Found better before */
 
6175
 
 
6176
  JOIN_TAB *s;
 
6177
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
 
6178
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
 
6179
  {
 
6180
    table_map real_table_bit=s->table->map;
 
6181
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
 
6182
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6183
    {
 
6184
      double records, best;
 
6185
      advance_sj_state(rest_tables, s);
 
6186
      best_access_path(join, s, thd, rest_tables, idx, record_count, 
 
6187
                       read_time);
 
6188
      records= join->positions[idx].records_read;
 
6189
      best= join->positions[idx].read_time;
 
6190
      /*
 
6191
        Go to the next level only if there hasn't been a better key on
 
6192
        this level! This will cut down the search for a lot simple cases!
 
6193
      */
 
6194
      double current_record_count=record_count*records;
 
6195
      double current_read_time=read_time+best;
 
6196
      if (best_record_count > current_record_count ||
 
6197
          best_read_time > current_read_time ||
 
6198
          (idx == join->const_tables && s->table == join->sort_by_table))
 
6199
      {
 
6200
        if (best_record_count >= current_record_count &&
 
6201
            best_read_time >= current_read_time &&
 
6202
            (!(s->key_dependent & rest_tables) || records < 2.0))
 
6203
        {
 
6204
          best_record_count=current_record_count;
 
6205
          best_read_time=current_read_time;
 
6206
        }
 
6207
        std::swap(join->best_ref[idx], *pos);
 
6208
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
 
6209
                      current_record_count,current_read_time))
 
6210
          return(true);
 
6211
        std::swap(join->best_ref[idx], *pos);
 
6212
      }
 
6213
      restore_prev_nj_state(s);
 
6214
      restore_prev_sj_state(rest_tables, s);
 
6215
      if (join->select_options & SELECT_STRAIGHT_JOIN)
 
6216
        break;                          // Don't test all combinations
 
6217
    }
 
6218
  }
 
6219
  return(false);
 
6220
}
 
6221
 
 
6222
 
 
6223
/**
849
6224
  Find how much space the prevous read not const tables takes in cache.
850
6225
*/
851
 
void calc_used_field_length(Session *, JoinTable *join_tab)
 
6226
 
 
6227
static void calc_used_field_length(THD *thd __attribute__((unused)),
 
6228
                                   JOIN_TAB *join_tab)
852
6229
{
853
6230
  uint32_t null_fields,blobs,fields,rec_length;
854
6231
  Field **f_ptr,*field;
 
6232
  MY_BITMAP *read_set= join_tab->table->read_set;;
855
6233
 
856
6234
  null_fields= blobs= fields= rec_length=0;
857
 
  for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
 
6235
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
858
6236
  {
859
 
    if (field->isReadSet())
 
6237
    if (bitmap_is_set(read_set, field->field_index))
860
6238
    {
861
6239
      uint32_t flags=field->flags;
862
6240
      fields++;
863
6241
      rec_length+=field->pack_length();
864
6242
      if (flags & BLOB_FLAG)
865
 
        blobs++;
 
6243
        blobs++;
866
6244
      if (!(flags & NOT_NULL_FLAG))
867
 
        null_fields++;
 
6245
        null_fields++;
868
6246
    }
869
6247
  }
870
6248
  if (null_fields)
873
6251
    rec_length+=sizeof(bool);
874
6252
  if (blobs)
875
6253
  {
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
 
}
 
6254
    uint32_t blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
 
6255
                             (join_tab->table->getRecordLength()- rec_length));
 
6256
    rec_length+=(uint) cmax((uint)4,blob_length);
 
6257
  }
 
6258
  join_tab->used_fields=fields;
 
6259
  join_tab->used_fieldlength=rec_length;
 
6260
  join_tab->used_blobs=blobs;
 
6261
}
 
6262
 
 
6263
 
 
6264
static uint
 
6265
cache_record_length(JOIN *join,uint32_t idx)
 
6266
{
 
6267
  uint32_t length=0;
 
6268
  JOIN_TAB **pos,**end;
 
6269
  THD *thd=join->thd;
 
6270
 
 
6271
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
 
6272
       pos != end ;
 
6273
       pos++)
 
6274
  {
 
6275
    JOIN_TAB *join_tab= *pos;
 
6276
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
6277
      calc_used_field_length(thd, join_tab);
 
6278
    length+=join_tab->used_fieldlength;
 
6279
  }
 
6280
  return length;
 
6281
}
 
6282
 
 
6283
 
 
6284
/*
 
6285
  Get the number of different row combinations for subset of partial join
 
6286
 
 
6287
  SYNOPSIS
 
6288
    prev_record_reads()
 
6289
      join       The join structure
 
6290
      idx        Number of tables in the partial join order (i.e. the
 
6291
                 partial join order is in join->positions[0..idx-1])
 
6292
      found_ref  Bitmap of tables for which we need to find # of distinct
 
6293
                 row combinations.
 
6294
 
 
6295
  DESCRIPTION
 
6296
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
 
6297
    tables within that join order (specified in found_ref), find out how many
 
6298
    distinct row combinations of subset tables will be in the result of the
 
6299
    partial join order.
 
6300
     
 
6301
    This is used as follows: Suppose we have a table accessed with a ref-based
 
6302
    method. The ref access depends on current rows of tables in found_ref.
 
6303
    We want to count # of different ref accesses. We assume two ref accesses
 
6304
    will be different if at least one of access parameters is different.
 
6305
    Example: consider a query
 
6306
 
 
6307
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
 
6308
 
 
6309
    and a join order:
 
6310
      t1,  ref access on t1.key=c1
 
6311
      t2,  ref access on t2.key=c2       
 
6312
      t3,  ref access on t3.key=t1.field 
 
6313
    
 
6314
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
 
6315
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
 
6316
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
 
6317
            n_distinct_ref_scans = #records_read(t1)
 
6318
    
 
6319
    The reason for having this function (at least the latest version of it)
 
6320
    is that we need to account for buffering in join execution. 
 
6321
    
 
6322
    An edge-case example: if we have a non-first table in join accessed via
 
6323
    ref(const) or ref(param) where there is a small number of different
 
6324
    values of param, then the access will likely hit the disk cache and will
 
6325
    not require any disk seeks.
 
6326
    
 
6327
    The proper solution would be to assume an LRU disk cache of some size,
 
6328
    calculate probability of cache hits, etc. For now we just count
 
6329
    identical ref accesses as one.
 
6330
 
 
6331
  RETURN 
 
6332
    Expected number of row combinations
 
6333
*/
 
6334
 
 
6335
static double
 
6336
prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref)
 
6337
{
 
6338
  double found=1.0;
 
6339
  POSITION *pos_end= join->positions - 1;
 
6340
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
 
6341
  {
 
6342
    if (pos->table->table->map & found_ref)
 
6343
    {
 
6344
      found_ref|= pos->ref_depend_map;
 
6345
      /* 
 
6346
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table 
 
6347
        with no matching row we will get position[t2].records_read==0. 
 
6348
        Actually the size of output is one null-complemented row, therefore 
 
6349
        we will use value of 1 whenever we get records_read==0.
 
6350
 
 
6351
        Note
 
6352
        - the above case can't occur if inner part of outer join has more 
 
6353
          than one table: table with no matches will not be marked as const.
 
6354
 
 
6355
        - Ideally we should add 1 to records_read for every possible null-
 
6356
          complemented row. We're not doing it because: 1. it will require
 
6357
          non-trivial code and add overhead. 2. The value of records_read
 
6358
          is an inprecise estimate and adding 1 (or, in the worst case,
 
6359
          #max_nested_outer_joins=64-1) will not make it any more precise.
 
6360
      */
 
6361
      if (pos->records_read > DBL_EPSILON)
 
6362
        found*= pos->records_read;
 
6363
    }
 
6364
  }
 
6365
  return found;
 
6366
}
 
6367
 
923
6368
 
924
6369
/**
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.
 
6370
  Set up join struct according to best position.
929
6371
*/
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)
 
6372
 
 
6373
static bool
 
6374
get_best_combination(JOIN *join)
 
6375
{
 
6376
  uint32_t i,tablenr;
 
6377
  table_map used_tables;
 
6378
  JOIN_TAB *join_tab,*j;
 
6379
  KEYUSE *keyuse;
 
6380
  uint32_t table_count;
 
6381
  THD *thd=join->thd;
 
6382
 
 
6383
  table_count=join->tables;
 
6384
  if (!(join->join_tab=join_tab=
 
6385
        (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
 
6386
    return(true);
 
6387
 
 
6388
  join->full_join=0;
 
6389
 
 
6390
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
 
6391
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
952
6392
  {
953
 
    Item *res;
954
 
    if ((res= new Item_cond_and(*e1, e2)))
 
6393
    Table *form;
 
6394
    *j= *join->best_positions[tablenr].table;
 
6395
    form=join->table[tablenr]=j->table;
 
6396
    used_tables|= form->map;
 
6397
    form->reginfo.join_tab=j;
 
6398
    if (!*j->on_expr_ref)
 
6399
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
 
6400
    if (j->type == JT_CONST)
 
6401
      continue;                                 // Handled in make_join_stat..
 
6402
 
 
6403
    j->ref.key = -1;
 
6404
    j->ref.key_parts=0;
 
6405
 
 
6406
    if (j->type == JT_SYSTEM)
 
6407
      continue;
 
6408
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
955
6409
    {
956
 
      *e1= res;
957
 
      res->quick_fix_field();
 
6410
      j->type=JT_ALL;
 
6411
      if (tablenr != join->const_tables)
 
6412
        join->full_join=1;
958
6413
    }
 
6414
    else if (create_ref_for_key(join, j, keyuse, used_tables))
 
6415
      return(true);                        // Something went wrong
959
6416
  }
960
 
  else
961
 
    *e1= e2;
 
6417
 
 
6418
  for (i=0 ; i < table_count ; i++)
 
6419
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
 
6420
  update_depend_map(join);
 
6421
  return(0);
962
6422
}
963
6423
 
964
 
bool create_ref_for_key(Join *join, 
965
 
                        JoinTable *j, 
966
 
                        optimizer::KeyUse *org_keyuse,
967
 
                        table_map used_tables)
 
6424
 
 
6425
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
6426
                               table_map used_tables)
968
6427
{
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;
 
6428
  KEYUSE *keyuse=org_keyuse;
 
6429
  THD  *thd= join->thd;
 
6430
  uint32_t keyparts,length,key;
 
6431
  Table *table;
 
6432
  KEY *keyinfo;
976
6433
 
977
6434
  /*  Use best key from find_best */
978
 
  table= j->table;
979
 
  key= keyuse->getKey();
980
 
  keyinfo= table->key_info + key;
 
6435
  table=j->table;
 
6436
  key=keyuse->key;
 
6437
  keyinfo=table->key_info+key;
981
6438
 
982
6439
  {
983
 
    keyparts= length= 0;
 
6440
    keyparts=length=0;
984
6441
    uint32_t found_part_ref_or_null= 0;
985
6442
    /*
986
6443
      Calculate length for the used key
989
6446
    */
990
6447
    do
991
6448
    {
992
 
      if (! (~used_tables & keyuse->getUsedTables()))
 
6449
      if (!(~used_tables & keyuse->used_tables))
993
6450
      {
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
 
        }
 
6451
        if (keyparts == keyuse->keypart &&
 
6452
            !(found_part_ref_or_null & keyuse->optimize))
 
6453
        {
 
6454
          keyparts++;
 
6455
          length+= keyinfo->key_part[keyuse->keypart].store_length;
 
6456
          found_part_ref_or_null|= keyuse->optimize;
 
6457
        }
1001
6458
      }
1002
6459
      keyuse++;
1003
 
    } while (keyuse->getTable() == table && keyuse->getKey() == key);
 
6460
    } while (keyuse->table == table && keyuse->key == key);
1004
6461
  }
1005
6462
 
1006
6463
  /* set up fieldref */
1008
6465
  j->ref.key_parts=keyparts;
1009
6466
  j->ref.key_length=length;
1010
6467
  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)))
 
6468
  if (!(j->ref.key_buff= (unsigned char*) thd->calloc(ALIGN_SIZE(length)*2)) ||
 
6469
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
 
6470
                                                   (keyparts+1)))) ||
 
6471
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
 
6472
      !(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
1016
6473
  {
1017
6474
    return(true);
1018
6475
  }
1022
6479
  j->ref.disable_cache= false;
1023
6480
  keyuse=org_keyuse;
1024
6481
 
1025
 
  StoredKey **ref_key= j->ref.key_copy;
1026
 
  unsigned char *key_buff= j->ref.key_buff, *null_ref_key= 0;
 
6482
  store_key **ref_key= j->ref.key_copy;
 
6483
  unsigned char *key_buff=j->ref.key_buff, *null_ref_key= 0;
1027
6484
  bool keyuse_uses_no_tables= true;
1028
6485
  {
1029
 
    for (uint32_t i= 0; i < keyparts; keyuse++, i++)
 
6486
    uint32_t i;
 
6487
    for (i=0 ; i < keyparts ; keyuse++,i++)
1030
6488
    {
1031
 
      while (keyuse->getKeypart() != i ||
1032
 
             ((~used_tables) & keyuse->getUsedTables()))
1033
 
        keyuse++;       /* Skip other parts */
 
6489
      while (keyuse->keypart != i ||
 
6490
             ((~used_tables) & keyuse->used_tables))
 
6491
        keyuse++;                               /* Skip other parts */
1034
6492
 
1035
6493
      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())
 
6494
      j->ref.items[i]=keyuse->val;              // Save for cond removal
 
6495
      j->ref.cond_guards[i]= keyuse->cond_guard;
 
6496
      if (keyuse->null_rejecting) 
1039
6497
        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,
 
6498
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
 
6499
      if (!keyuse->used_tables &&
 
6500
          !(join->select_options & SELECT_DESCRIBE))
 
6501
      {                                 // Compare against constant
 
6502
        store_key_item tmp(thd, keyinfo->key_part[i].field,
1044
6503
                           key_buff + maybe_null,
1045
6504
                           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();
 
6505
                           keyinfo->key_part[i].length, keyuse->val);
 
6506
        if (thd->is_fatal_error)
 
6507
          return(true);
 
6508
        tmp.copy();
1050
6509
      }
1051
6510
      else
1052
 
        *ref_key++= get_store_key(session,
1053
 
          keyuse,join->const_table_map,
1054
 
          &keyinfo->key_part[i],
1055
 
          key_buff, maybe_null);
 
6511
        *ref_key++= get_store_key(thd,
 
6512
                                  keyuse,join->const_table_map,
 
6513
                                  &keyinfo->key_part[i],
 
6514
                                  key_buff, maybe_null);
1056
6515
      /*
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
 
6516
        Remember if we are going to use REF_OR_NULL
 
6517
        But only if field _really_ can be null i.e. we force JT_REF
 
6518
        instead of JT_REF_OR_NULL in case if field can't be null
1060
6519
      */
1061
 
      if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
1062
 
        null_ref_key= key_buff;
 
6520
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
6521
        null_ref_key= key_buff;
1063
6522
      key_buff+=keyinfo->key_part[i].store_length;
1064
6523
    }
1065
6524
  }
1066
 
  *ref_key= 0;       // end_marker
1067
 
  if (j->type == AM_CONST)
 
6525
  *ref_key=0;                           // end_marker
 
6526
  if (j->type == JT_CONST)
1068
6527
    j->table->const_table= 1;
1069
6528
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
1070
6529
           keyparts != keyinfo->key_parts || null_ref_key)
1071
6530
  {
1072
6531
    /* Must read with repeat */
1073
 
    j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
 
6532
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
1074
6533
    j->ref.null_ref_key= null_ref_key;
1075
6534
  }
1076
6535
  else if (keyuse_uses_no_tables)
1082
6541
      Here we should not mark the table as a 'const' as a field may
1083
6542
      have a 'normal' value or a NULL value.
1084
6543
    */
1085
 
    j->type= AM_CONST;
1086
 
  }
1087
 
  else
1088
 
    j->type= AM_EQ_REF;
1089
 
  return 0;
1090
 
}
 
6544
    j->type=JT_CONST;
 
6545
  }
 
6546
  else
 
6547
    j->type=JT_EQ_REF;
 
6548
  return(0);
 
6549
}
 
6550
 
 
6551
 
 
6552
 
 
6553
static store_key *
 
6554
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
 
6555
              KEY_PART_INFO *key_part, unsigned char *key_buff, uint32_t maybe_null)
 
6556
{
 
6557
  if (!((~used_tables) & keyuse->used_tables))          // if const item
 
6558
  {
 
6559
    return new store_key_const_item(thd,
 
6560
                                    key_part->field,
 
6561
                                    key_buff + maybe_null,
 
6562
                                    maybe_null ? key_buff : 0,
 
6563
                                    key_part->length,
 
6564
                                    keyuse->val);
 
6565
  }
 
6566
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
 
6567
           (keyuse->val->type() == Item::REF_ITEM &&
 
6568
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
 
6569
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
 
6570
             Item_ref::DIRECT_REF && 
 
6571
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
 
6572
    return new store_key_field(thd,
 
6573
                               key_part->field,
 
6574
                               key_buff + maybe_null,
 
6575
                               maybe_null ? key_buff : 0,
 
6576
                               key_part->length,
 
6577
                               ((Item_field*) keyuse->val->real_item())->field,
 
6578
                               keyuse->val->full_name());
 
6579
  return new store_key_item(thd,
 
6580
                            key_part->field,
 
6581
                            key_buff + maybe_null,
 
6582
                            maybe_null ? key_buff : 0,
 
6583
                            key_part->length,
 
6584
                            keyuse->val);
 
6585
}
 
6586
 
 
6587
/**
 
6588
  This function is only called for const items on fields which are keys.
 
6589
 
 
6590
  @return
 
6591
    returns 1 if there was some conversion made when the field was stored.
 
6592
*/
 
6593
 
 
6594
bool
 
6595
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
6596
{
 
6597
  bool error;
 
6598
  Table *table= field->table;
 
6599
  THD *thd= table->in_use;
 
6600
  ha_rows cuted_fields=thd->cuted_fields;
 
6601
 
 
6602
  /*
 
6603
    we should restore old value of count_cuted_fields because
 
6604
    store_val_in_field can be called from mysql_insert 
 
6605
    with select_insert, which make count_cuted_fields= 1
 
6606
   */
 
6607
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
 
6608
  thd->count_cuted_fields= check_flag;
 
6609
  error= item->save_in_field(field, 1);
 
6610
  thd->count_cuted_fields= old_count_cuted_fields;
 
6611
  return error || cuted_fields != thd->cuted_fields;
 
6612
}
 
6613
 
 
6614
 
 
6615
static bool
 
6616
make_simple_join(JOIN *join,Table *tmp_table)
 
6617
{
 
6618
  Table **tableptr;
 
6619
  JOIN_TAB *join_tab;
 
6620
 
 
6621
  /*
 
6622
    Reuse Table * and JOIN_TAB if already allocated by a previous call
 
6623
    to this function through JOIN::exec (may happen for sub-queries).
 
6624
  */
 
6625
  if (!join->table_reexec)
 
6626
  {
 
6627
    if (!(join->table_reexec= (Table**) join->thd->alloc(sizeof(Table*))))
 
6628
      return(true);                        /* purecov: inspected */
 
6629
    if (join->tmp_join)
 
6630
      join->tmp_join->table_reexec= join->table_reexec;
 
6631
  }
 
6632
  if (!join->join_tab_reexec)
 
6633
  {
 
6634
    if (!(join->join_tab_reexec=
 
6635
          (JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
 
6636
      return(true);                        /* purecov: inspected */
 
6637
    if (join->tmp_join)
 
6638
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
 
6639
  }
 
6640
  tableptr= join->table_reexec;
 
6641
  join_tab= join->join_tab_reexec;
 
6642
 
 
6643
  join->join_tab=join_tab;
 
6644
  join->table=tableptr; tableptr[0]=tmp_table;
 
6645
  join->tables=1;
 
6646
  join->const_tables=0;
 
6647
  join->const_table_map=0;
 
6648
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
 
6649
    join->tmp_table_param.func_count=0;
 
6650
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
 
6651
  join->first_record=join->sort_and_group=0;
 
6652
  join->send_records=(ha_rows) 0;
 
6653
  join->group=0;
 
6654
  join->row_limit=join->unit->select_limit_cnt;
 
6655
  join->do_send_rows = (join->row_limit) ? 1 : 0;
 
6656
 
 
6657
  join_tab->cache.buff=0;                       /* No caching */
 
6658
  join_tab->table=tmp_table;
 
6659
  join_tab->select=0;
 
6660
  join_tab->select_cond=0;
 
6661
  join_tab->quick=0;
 
6662
  join_tab->type= JT_ALL;                       /* Map through all records */
 
6663
  join_tab->keys.init();
 
6664
  join_tab->keys.set_all();                     /* test everything in quick */
 
6665
  join_tab->info=0;
 
6666
  join_tab->on_expr_ref=0;
 
6667
  join_tab->last_inner= 0;
 
6668
  join_tab->first_unmatched= 0;
 
6669
  join_tab->ref.key = -1;
 
6670
  join_tab->not_used_in_distinct=0;
 
6671
  join_tab->read_first_record= join_init_read_record;
 
6672
  join_tab->join=join;
 
6673
  join_tab->ref.key_parts= 0;
 
6674
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
 
6675
  join_tab->do_firstmatch= NULL;
 
6676
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
 
6677
  tmp_table->status=0;
 
6678
  tmp_table->null_row=0;
 
6679
  return(false);
 
6680
}
 
6681
 
 
6682
 
 
6683
inline void add_cond_and_fix(Item **e1, Item *e2)
 
6684
{
 
6685
  if (*e1)
 
6686
  {
 
6687
    Item *res;
 
6688
    if ((res= new Item_cond_and(*e1, e2)))
 
6689
    {
 
6690
      *e1= res;
 
6691
      res->quick_fix_field();
 
6692
    }
 
6693
  }
 
6694
  else
 
6695
    *e1= e2;
 
6696
}
 
6697
 
1091
6698
 
1092
6699
/**
1093
6700
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
1102
6709
    add "t1.field IS NOT NULL" to t1's table condition. @n
1103
6710
 
1104
6711
    Description of the optimization:
1105
 
 
 
6712
    
1106
6713
      We look through equalities choosen to perform ref/eq_ref access,
1107
6714
      pick equalities that have form "tbl.part_of_key = othertbl.field"
1108
6715
      (where othertbl is a non-const table and othertbl.field may be NULL)
1130
6737
      This optimization doesn't affect the choices that ref, range, or join
1131
6738
      optimizer make. This was intentional because this was added after 4.1
1132
6739
      was GA.
1133
 
 
 
6740
      
1134
6741
    Implementation overview
1135
6742
      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.
 
6743
         predicates in in KEY_FIELD::null_rejecting
 
6744
      1.1 add_key_part saves these to KEYUSE.
 
6745
      2. create_ref_for_key copies them to TABLE_REF.
1139
6746
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
1140
 
         appropiate JoinTable members.
 
6747
         appropiate JOIN_TAB members.
1141
6748
*/
1142
 
void add_not_null_conds(Join *join)
 
6749
 
 
6750
static void add_not_null_conds(JOIN *join)
1143
6751
{
1144
 
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
6752
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
1145
6753
  {
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) &&
 
6754
    JOIN_TAB *tab=join->join_tab+i;
 
6755
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
 
6756
         tab->type == JT_REF_OR_NULL) &&
1149
6757
        !tab->table->maybe_null)
1150
6758
    {
1151
6759
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
1156
6764
          Item *notnull;
1157
6765
          assert(item->type() == Item::FIELD_ITEM);
1158
6766
          Item_field *not_null_item= (Item_field*)item;
1159
 
          JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
 
6767
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
1160
6768
          /*
1161
6769
            For UPDATE queries such as:
1162
6770
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1168
6776
            return;
1169
6777
          /*
1170
6778
            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
 
6779
            notnull->const_item(). This is needed e.g. by test_quick_select 
 
6780
            when it is called from make_join_select after this function is 
1173
6781
            called.
1174
6782
          */
1175
 
          if (notnull->fix_fields(join->session, &notnull))
 
6783
          if (notnull->fix_fields(join->thd, &notnull))
1176
6784
            return;
1177
6785
          add_cond_and_fix(&referred_tab->select_cond, notnull);
1178
6786
        }
1197
6805
    -  pointer to the guarded predicate, if success
1198
6806
    -  0, otherwise
1199
6807
*/
1200
 
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
 
6808
 
 
6809
static COND*
 
6810
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
1201
6811
{
1202
6812
  COND *tmp;
1203
6813
  assert(cond != 0);
1213
6823
  return tmp;
1214
6824
}
1215
6825
 
 
6826
 
 
6827
/**
 
6828
  Fill in outer join related info for the execution plan structure.
 
6829
 
 
6830
    For each outer join operation left after simplification of the
 
6831
    original query the function set up the following pointers in the linear
 
6832
    structure join->join_tab representing the selected execution plan.
 
6833
    The first inner table t0 for the operation is set to refer to the last
 
6834
    inner table tk through the field t0->last_inner.
 
6835
    Any inner table ti for the operation are set to refer to the first
 
6836
    inner table ti->first_inner.
 
6837
    The first inner table t0 for the operation is set to refer to the
 
6838
    first inner table of the embedding outer join operation, if there is any,
 
6839
    through the field t0->first_upper.
 
6840
    The on expression for the outer join operation is attached to the
 
6841
    corresponding first inner table through the field t0->on_expr_ref.
 
6842
    Here ti are structures of the JOIN_TAB type.
 
6843
 
 
6844
  EXAMPLE. For the query: 
 
6845
  @code
 
6846
        SELECT * FROM t1
 
6847
                      LEFT JOIN
 
6848
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
 
6849
                      ON (t1.a=t2.a AND t1.b=t3.b)
 
6850
          WHERE t1.c > 5,
 
6851
  @endcode
 
6852
 
 
6853
    given the execution plan with the table order t1,t2,t3,t4
 
6854
    is selected, the following references will be set;
 
6855
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
 
6856
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
 
6857
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to 
 
6858
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
 
6859
 
 
6860
  @param join   reference to the info fully describing the query
 
6861
 
 
6862
  @note
 
6863
    The function assumes that the simplification procedure has been
 
6864
    already applied to the join query (see simplify_joins).
 
6865
    This function can be called only after the execution plan
 
6866
    has been chosen.
 
6867
*/
 
6868
 
 
6869
static void
 
6870
make_outerjoin_info(JOIN *join)
 
6871
{
 
6872
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6873
  {
 
6874
    JOIN_TAB *tab=join->join_tab+i;
 
6875
    Table *table=tab->table;
 
6876
    TableList *tbl= table->pos_in_table_list;
 
6877
    TableList *embedding= tbl->embedding;
 
6878
 
 
6879
    if (tbl->outer_join)
 
6880
    {
 
6881
      /* 
 
6882
        Table tab is the only one inner table for outer join.
 
6883
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
 
6884
        is in the query above.)
 
6885
      */
 
6886
      tab->last_inner= tab->first_inner= tab;
 
6887
      tab->on_expr_ref= &tbl->on_expr;
 
6888
      tab->cond_equal= tbl->cond_equal;
 
6889
      if (embedding)
 
6890
        tab->first_upper= embedding->nested_join->first_nested;
 
6891
    }    
 
6892
    for ( ; embedding ; embedding= embedding->embedding)
 
6893
    {
 
6894
      /* Ignore sj-nests: */
 
6895
      if (!embedding->on_expr)
 
6896
        continue;
 
6897
      nested_join_st *nested_join= embedding->nested_join;
 
6898
      if (!nested_join->counter_)
 
6899
      {
 
6900
        /* 
 
6901
          Table tab is the first inner table for nested_join.
 
6902
          Save reference to it in the nested join structure.
 
6903
        */ 
 
6904
        nested_join->first_nested= tab;
 
6905
        tab->on_expr_ref= &embedding->on_expr;
 
6906
        tab->cond_equal= tbl->cond_equal;
 
6907
        if (embedding->embedding)
 
6908
          tab->first_upper= embedding->embedding->nested_join->first_nested;
 
6909
      }
 
6910
      if (!tab->first_inner)  
 
6911
        tab->first_inner= nested_join->first_nested;
 
6912
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
6913
        break;
 
6914
      /* Table tab is the last inner table for nested join. */
 
6915
      nested_join->first_nested->last_inner= tab;
 
6916
    }
 
6917
  }
 
6918
  return;
 
6919
}
 
6920
 
 
6921
 
 
6922
static bool
 
6923
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
 
6924
{
 
6925
  THD *thd= join->thd;
 
6926
  if (select)
 
6927
  {
 
6928
    add_not_null_conds(join);
 
6929
    table_map used_tables;
 
6930
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
 
6931
    {                        /* there may be a select without a cond. */    
 
6932
      if (join->tables > 1)
 
6933
        cond->update_used_tables();             // Tablenr may have changed
 
6934
      if (join->const_tables == join->tables &&
 
6935
          thd->lex->current_select->master_unit() ==
 
6936
          &thd->lex->unit)              // not upper level SELECT
 
6937
        join->const_table_map|=RAND_TABLE_BIT;
 
6938
      {                                         // Check const tables
 
6939
        COND *const_cond=
 
6940
          make_cond_for_table(cond,
 
6941
                              join->const_table_map,
 
6942
                              (table_map) 0, 1);
 
6943
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
 
6944
             tab < join->join_tab+join->tables ; tab++)
 
6945
        {
 
6946
          if (*tab->on_expr_ref)
 
6947
          {
 
6948
            JOIN_TAB *cond_tab= tab->first_inner;
 
6949
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
 
6950
                                           join->const_table_map,
 
6951
                                           (  table_map) 0, 0);
 
6952
            if (!tmp)
 
6953
              continue;
 
6954
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
6955
            if (!tmp)
 
6956
              return(1);
 
6957
            tmp->quick_fix_field();
 
6958
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
6959
                                    new Item_cond_and(cond_tab->select_cond,
 
6960
                                                      tmp);
 
6961
            if (!cond_tab->select_cond)
 
6962
              return(1);
 
6963
            cond_tab->select_cond->quick_fix_field();
 
6964
          }       
 
6965
        }
 
6966
        if (const_cond && !const_cond->val_int())
 
6967
        {
 
6968
          return(1);     // Impossible const condition
 
6969
        }
 
6970
      }
 
6971
    }
 
6972
    used_tables=((select->const_tables=join->const_table_map) |
 
6973
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
6974
    for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6975
    {
 
6976
      JOIN_TAB *tab=join->join_tab+i;
 
6977
      /*
 
6978
        first_inner is the X in queries like:
 
6979
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
 
6980
      */
 
6981
      JOIN_TAB *first_inner_tab= tab->first_inner; 
 
6982
      table_map current_map= tab->table->map;
 
6983
      bool use_quick_range=0;
 
6984
      COND *tmp;
 
6985
 
 
6986
      /*
 
6987
        Following force including random expression in last table condition.
 
6988
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
 
6989
      */
 
6990
      if (i == join->tables-1)
 
6991
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
 
6992
      used_tables|=current_map;
 
6993
 
 
6994
      if (tab->type == JT_REF && tab->quick &&
 
6995
          (uint) tab->ref.key == tab->quick->index &&
 
6996
          tab->ref.key_length < tab->quick->max_used_key_length)
 
6997
      {
 
6998
        /* Range uses longer key;  Use this instead of ref on key */
 
6999
        tab->type=JT_ALL;
 
7000
        use_quick_range=1;
 
7001
        tab->use_quick=1;
 
7002
        tab->ref.key= -1;
 
7003
        tab->ref.key_parts=0;           // Don't use ref key.
 
7004
        join->best_positions[i].records_read= rows2double(tab->quick->records);
 
7005
        /* 
 
7006
          We will use join cache here : prevent sorting of the first
 
7007
          table only and sort at the end.
 
7008
        */
 
7009
        if (i != join->const_tables && join->tables > join->const_tables + 1)
 
7010
          join->full_join= 1;
 
7011
      }
 
7012
 
 
7013
      tmp= NULL;
 
7014
      if (cond)
 
7015
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
 
7016
      if (cond && !tmp && tab->quick)
 
7017
      {                                         // Outer join
 
7018
        if (tab->type != JT_ALL)
 
7019
        {
 
7020
          /*
 
7021
            Don't use the quick method
 
7022
            We come here in the case where we have 'key=constant' and
 
7023
            the test is removed by make_cond_for_table()
 
7024
          */
 
7025
          delete tab->quick;
 
7026
          tab->quick= 0;
 
7027
        }
 
7028
        else
 
7029
        {
 
7030
          /*
 
7031
            Hack to handle the case where we only refer to a table
 
7032
            in the ON part of an OUTER JOIN. In this case we want the code
 
7033
            below to check if we should use 'quick' instead.
 
7034
          */
 
7035
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
7036
        }
 
7037
 
 
7038
      }
 
7039
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
 
7040
          tab->type == JT_EQ_REF)
 
7041
      {
 
7042
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
 
7043
                                       thd->memdup((unsigned char*) select,
 
7044
                                                   sizeof(*select)));
 
7045
        if (!sel)
 
7046
          return(1);                    // End of memory
 
7047
        /*
 
7048
          If tab is an inner table of an outer join operation,
 
7049
          add a match guard to the pushed down predicate.
 
7050
          The guard will turn the predicate on only after
 
7051
          the first match for outer tables is encountered.
 
7052
        */        
 
7053
        if (cond && tmp)
 
7054
        {
 
7055
          /*
 
7056
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
 
7057
            a cond, so neutralize the hack above.
 
7058
          */
 
7059
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
 
7060
            return(1);
 
7061
          tab->select_cond=sel->cond=tmp;
 
7062
          /* Push condition to storage engine if this is enabled
 
7063
             and the condition is not guarded */
 
7064
          tab->table->file->pushed_cond= NULL;
 
7065
          if (thd->variables.engine_condition_pushdown)
 
7066
          {
 
7067
            COND *push_cond= 
 
7068
              make_cond_for_table(tmp, current_map, current_map, 0);
 
7069
            if (push_cond)
 
7070
            {
 
7071
              /* Push condition to handler */
 
7072
              if (!tab->table->file->cond_push(push_cond))
 
7073
                tab->table->file->pushed_cond= push_cond;
 
7074
            }
 
7075
          }
 
7076
        }
 
7077
        else
 
7078
          tab->select_cond= sel->cond= NULL;
 
7079
 
 
7080
        sel->head=tab->table;
 
7081
        if (tab->quick)
 
7082
        {
 
7083
          /* Use quick key read if it's a constant and it's not used
 
7084
             with key reading */
 
7085
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
 
7086
              && (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
 
7087
          {
 
7088
            sel->quick=tab->quick;              // Use value from get_quick_...
 
7089
            sel->quick_keys.clear_all();
 
7090
            sel->needed_reg.clear_all();
 
7091
          }
 
7092
          else
 
7093
          {
 
7094
            delete tab->quick;
 
7095
          }
 
7096
          tab->quick=0;
 
7097
        }
 
7098
        uint32_t ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
 
7099
        if (i == join->const_tables && ref_key)
 
7100
        {
 
7101
          if (!tab->const_keys.is_clear_all() &&
 
7102
              tab->table->reginfo.impossible_range)
 
7103
            return(1);
 
7104
        }
 
7105
        else if (tab->type == JT_ALL && ! use_quick_range)
 
7106
        {
 
7107
          if (!tab->const_keys.is_clear_all() &&
 
7108
              tab->table->reginfo.impossible_range)
 
7109
            return(1);                          // Impossible range
 
7110
          /*
 
7111
            We plan to scan all rows.
 
7112
            Check again if we should use an index.
 
7113
            We could have used an column from a previous table in
 
7114
            the index if we are using limit and this is the first table
 
7115
          */
 
7116
 
 
7117
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
 
7118
              (!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)))
 
7119
          {
 
7120
            /* Join with outer join condition */
 
7121
            COND *orig_cond=sel->cond;
 
7122
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
 
7123
 
 
7124
            /*
 
7125
              We can't call sel->cond->fix_fields,
 
7126
              as it will break tab->on_expr if it's AND condition
 
7127
              (fix_fields currently removes extra AND/OR levels).
 
7128
              Yet attributes of the just built condition are not needed.
 
7129
              Thus we call sel->cond->quick_fix_field for safety.
 
7130
            */
 
7131
            if (sel->cond && !sel->cond->fixed)
 
7132
              sel->cond->quick_fix_field();
 
7133
 
 
7134
            if (sel->test_quick_select(thd, tab->keys,
 
7135
                                       used_tables & ~ current_map,
 
7136
                                       (join->select_options &
 
7137
                                        OPTION_FOUND_ROWS ?
 
7138
                                        HA_POS_ERROR :
 
7139
                                        join->unit->select_limit_cnt), 0,
 
7140
                                        false) < 0)
 
7141
            {
 
7142
              /*
 
7143
                Before reporting "Impossible WHERE" for the whole query
 
7144
                we have to check isn't it only "impossible ON" instead
 
7145
              */
 
7146
              sel->cond=orig_cond;
 
7147
              if (!*tab->on_expr_ref ||
 
7148
                  sel->test_quick_select(thd, tab->keys,
 
7149
                                         used_tables & ~ current_map,
 
7150
                                         (join->select_options &
 
7151
                                          OPTION_FOUND_ROWS ?
 
7152
                                          HA_POS_ERROR :
 
7153
                                          join->unit->select_limit_cnt),0,
 
7154
                                          false) < 0)
 
7155
                return(1);                      // Impossible WHERE
 
7156
            }
 
7157
            else
 
7158
              sel->cond=orig_cond;
 
7159
 
 
7160
            /* Fix for EXPLAIN */
 
7161
            if (sel->quick)
 
7162
              join->best_positions[i].records_read= (double)sel->quick->records;
 
7163
          }
 
7164
          else
 
7165
          {
 
7166
            sel->needed_reg=tab->needed_reg;
 
7167
            sel->quick_keys.clear_all();
 
7168
          }
 
7169
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
 
7170
              !sel->needed_reg.is_subset(tab->checked_keys))
 
7171
          {
 
7172
            tab->keys=sel->quick_keys;
 
7173
            tab->keys.merge(sel->needed_reg);
 
7174
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
 
7175
                             (select->quick_keys.is_clear_all() ||
 
7176
                              (select->quick &&
 
7177
                               (select->quick->records >= 100L)))) ?
 
7178
              2 : 1;
 
7179
            sel->read_tables= used_tables & ~current_map;
 
7180
          }
 
7181
          if (i != join->const_tables && tab->use_quick != 2)
 
7182
          {                                     /* Read with cache */
 
7183
            if (cond &&
 
7184
                (tmp=make_cond_for_table(cond,
 
7185
                                         join->const_table_map |
 
7186
                                         current_map,
 
7187
                                         current_map, 0)))
 
7188
            {
 
7189
              tab->cache.select=(SQL_SELECT*)
 
7190
                thd->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
 
7191
              tab->cache.select->cond=tmp;
 
7192
              tab->cache.select->read_tables=join->const_table_map;
 
7193
            }
 
7194
          }
 
7195
        }
 
7196
      }
 
7197
      
 
7198
      /* 
 
7199
        Push down conditions from all on expressions.
 
7200
        Each of these conditions are guarded by a variable
 
7201
        that turns if off just before null complemented row for
 
7202
        outer joins is formed. Thus, the condition from an
 
7203
        'on expression' are guaranteed not to be checked for
 
7204
        the null complemented row.
 
7205
      */ 
 
7206
 
 
7207
      /* First push down constant conditions from on expressions */
 
7208
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
 
7209
           join_tab < join->join_tab+join->tables ; join_tab++)
 
7210
      {
 
7211
        if (*join_tab->on_expr_ref)
 
7212
        {
 
7213
          JOIN_TAB *cond_tab= join_tab->first_inner;
 
7214
          COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
 
7215
                                         join->const_table_map,
 
7216
                                         (table_map) 0, 0);
 
7217
          if (!tmp)
 
7218
            continue;
 
7219
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
7220
          if (!tmp)
 
7221
            return(1);
 
7222
          tmp->quick_fix_field();
 
7223
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
7224
                                    new Item_cond_and(cond_tab->select_cond,tmp);
 
7225
          if (!cond_tab->select_cond)
 
7226
            return(1);
 
7227
          cond_tab->select_cond->quick_fix_field();
 
7228
        }       
 
7229
      }
 
7230
 
 
7231
      /* Push down non-constant conditions from on expressions */
 
7232
      JOIN_TAB *last_tab= tab;
 
7233
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
 
7234
      {  
 
7235
        /* 
 
7236
          Table tab is the last inner table of an outer join.
 
7237
          An on expression is always attached to it.
 
7238
        */     
 
7239
        COND *on_expr= *first_inner_tab->on_expr_ref;
 
7240
 
 
7241
        table_map used_tables2= (join->const_table_map |
 
7242
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7243
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
 
7244
        {
 
7245
          current_map= tab->table->map;
 
7246
          used_tables2|= current_map;
 
7247
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
 
7248
                                              current_map, 0);
 
7249
          if (tmp_cond)
 
7250
          {
 
7251
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
 
7252
            /*
 
7253
              First add the guards for match variables of
 
7254
              all embedding outer join operations.
 
7255
            */
 
7256
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
 
7257
                                                     tmp_cond,
 
7258
                                                     first_inner_tab)))
 
7259
              return(1);
 
7260
            /* 
 
7261
              Now add the guard turning the predicate off for 
 
7262
              the null complemented row.
 
7263
            */ 
 
7264
            tmp_cond= new Item_func_trig_cond(tmp_cond,
 
7265
                                              &first_inner_tab->
 
7266
                                              not_null_compl);
 
7267
            if (tmp_cond)
 
7268
              tmp_cond->quick_fix_field();
 
7269
            /* Add the predicate to other pushed down predicates */
 
7270
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
 
7271
                                  new Item_cond_and(cond_tab->select_cond,
 
7272
                                                    tmp_cond);
 
7273
            if (!cond_tab->select_cond)
 
7274
              return(1);
 
7275
            cond_tab->select_cond->quick_fix_field();
 
7276
          }              
 
7277
        }
 
7278
        first_inner_tab= first_inner_tab->first_upper;       
 
7279
      }
 
7280
    }
 
7281
  }
 
7282
  return(0);
 
7283
}
 
7284
 
 
7285
 
 
7286
/* 
 
7287
  Check if given expression uses only table fields covered by the given index
 
7288
 
 
7289
  SYNOPSIS
 
7290
    uses_index_fields_only()
 
7291
      item           Expression to check
 
7292
      tbl            The table having the index
 
7293
      keyno          The index number
 
7294
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7295
 
 
7296
  DESCRIPTION
 
7297
    Check if given expression only uses fields covered by index #keyno in the
 
7298
    table tbl. The expression can use any fields in any other tables.
 
7299
    
 
7300
    The expression is guaranteed not to be AND or OR - those constructs are 
 
7301
    handled outside of this function.
 
7302
 
 
7303
  RETURN
 
7304
    true   Yes
 
7305
    false  No
 
7306
*/
 
7307
 
 
7308
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, 
 
7309
                            bool other_tbls_ok)
 
7310
{
 
7311
  if (item->const_item())
 
7312
    return true;
 
7313
 
 
7314
  /* 
 
7315
    Don't push down the triggered conditions. Nested outer joins execution 
 
7316
    code may need to evaluate a condition several times (both triggered and
 
7317
    untriggered), and there is no way to put thi
 
7318
    TODO: Consider cloning the triggered condition and using the copies for:
 
7319
      1. push the first copy down, to have most restrictive index condition
 
7320
         possible
 
7321
      2. Put the second copy into tab->select_cond. 
 
7322
  */
 
7323
  if (item->type() == Item::FUNC_ITEM && 
 
7324
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
 
7325
    return false;
 
7326
 
 
7327
  if (!(item->used_tables() & tbl->map))
 
7328
    return other_tbls_ok;
 
7329
 
 
7330
  Item::Type item_type= item->type();
 
7331
  switch (item_type) {
 
7332
  case Item::FUNC_ITEM:
 
7333
    {
 
7334
      /* This is a function, apply condition recursively to arguments */
 
7335
      Item_func *item_func= (Item_func*)item;
 
7336
      Item **child;
 
7337
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
 
7338
      for (child= item_func->arguments(); child != item_end; child++)
 
7339
      {
 
7340
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
 
7341
          return false;
 
7342
      }
 
7343
      return true;
 
7344
    }
 
7345
  case Item::COND_ITEM:
 
7346
    {
 
7347
      /* This is a function, apply condition recursively to arguments */
 
7348
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
 
7349
      Item *item;
 
7350
      while ((item=li++))
 
7351
      {
 
7352
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
 
7353
          return false;
 
7354
      }
 
7355
      return true;
 
7356
    }
 
7357
  case Item::FIELD_ITEM:
 
7358
    {
 
7359
      Item_field *item_field= (Item_field*)item;
 
7360
      if (item_field->field->table != tbl) 
 
7361
        return true;
 
7362
      return item_field->field->part_of_key.is_set(keyno);
 
7363
    }
 
7364
  case Item::REF_ITEM:
 
7365
    return uses_index_fields_only(item->real_item(), tbl, keyno,
 
7366
                                  other_tbls_ok);
 
7367
  default:
 
7368
    return false; /* Play it safe, don't push unknown non-const items */
 
7369
  }
 
7370
}
 
7371
 
 
7372
 
1216
7373
#define ICP_COND_USES_INDEX_ONLY 10
1217
7374
 
1218
 
 
1219
 
/**
1220
 
  cleanup JoinTable.
1221
 
*/
1222
 
void JoinTable::cleanup()
1223
 
{
1224
 
  safe_delete(select);
1225
 
  safe_delete(quick);
1226
 
 
 
7375
/*
 
7376
  Get a part of the condition that can be checked using only index fields
 
7377
 
 
7378
  SYNOPSIS
 
7379
    make_cond_for_index()
 
7380
      cond           The source condition
 
7381
      table          The table that is partially available
 
7382
      keyno          The index in the above table. Only fields covered by the index
 
7383
                     are available
 
7384
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7385
 
 
7386
  DESCRIPTION
 
7387
    Get a part of the condition that can be checked when for the given table 
 
7388
    we have values only of fields covered by some index. The condition may
 
7389
    refer to other tables, it is assumed that we have values of all of their 
 
7390
    fields.
 
7391
 
 
7392
    Example:
 
7393
      make_cond_for_index(
 
7394
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
 
7395
          t2, keyno(t2.key1)) 
 
7396
      will return
 
7397
        "cond(t1.field) AND cond(t2.key2)"
 
7398
 
 
7399
  RETURN
 
7400
    Index condition, or NULL if no condition could be inferred.
 
7401
*/
 
7402
 
 
7403
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno,
 
7404
                          bool other_tbls_ok)
 
7405
{
 
7406
  if (!cond)
 
7407
    return NULL;
 
7408
  if (cond->type() == Item::COND_ITEM)
 
7409
  {
 
7410
    uint32_t n_marked= 0;
 
7411
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7412
    {
 
7413
      Item_cond_and *new_cond=new Item_cond_and;
 
7414
      if (!new_cond)
 
7415
        return (COND*) 0;
 
7416
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7417
      Item *item;
 
7418
      while ((item=li++))
 
7419
      {
 
7420
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7421
        if (fix)
 
7422
          new_cond->argument_list()->push_back(fix);
 
7423
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7424
      }
 
7425
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7426
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7427
      switch (new_cond->argument_list()->elements) {
 
7428
      case 0:
 
7429
        return (COND*) 0;
 
7430
      case 1:
 
7431
        return new_cond->argument_list()->head();
 
7432
      default:
 
7433
        new_cond->quick_fix_field();
 
7434
        return new_cond;
 
7435
      }
 
7436
    }
 
7437
    else /* It's OR */
 
7438
    {
 
7439
      Item_cond_or *new_cond=new Item_cond_or;
 
7440
      if (!new_cond)
 
7441
        return (COND*) 0;
 
7442
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7443
      Item *item;
 
7444
      while ((item=li++))
 
7445
      {
 
7446
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7447
        if (!fix)
 
7448
          return (COND*) 0;
 
7449
        new_cond->argument_list()->push_back(fix);
 
7450
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7451
      }
 
7452
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7453
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7454
      new_cond->quick_fix_field();
 
7455
      new_cond->top_level_item();
 
7456
      return new_cond;
 
7457
    }
 
7458
  }
 
7459
 
 
7460
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
 
7461
    return (COND*) 0;
 
7462
  cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7463
  return cond;
 
7464
}
 
7465
 
 
7466
 
 
7467
Item *make_cond_remainder(Item *cond, bool exclude_index)
 
7468
{
 
7469
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
 
7470
    return 0; /* Already checked */
 
7471
 
 
7472
  if (cond->type() == Item::COND_ITEM)
 
7473
  {
 
7474
    table_map tbl_map= 0;
 
7475
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7476
    {
 
7477
      /* Create new top level AND item */
 
7478
      Item_cond_and *new_cond=new Item_cond_and;
 
7479
      if (!new_cond)
 
7480
        return (COND*) 0;
 
7481
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7482
      Item *item;
 
7483
      while ((item=li++))
 
7484
      {
 
7485
        Item *fix= make_cond_remainder(item, exclude_index);
 
7486
        if (fix)
 
7487
        {
 
7488
          new_cond->argument_list()->push_back(fix);
 
7489
          tbl_map |= fix->used_tables();
 
7490
        }
 
7491
      }
 
7492
      switch (new_cond->argument_list()->elements) {
 
7493
      case 0:
 
7494
        return (COND*) 0;
 
7495
      case 1:
 
7496
        return new_cond->argument_list()->head();
 
7497
      default:
 
7498
        new_cond->quick_fix_field();
 
7499
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7500
        return new_cond;
 
7501
      }
 
7502
    }
 
7503
    else /* It's OR */
 
7504
    {
 
7505
      Item_cond_or *new_cond=new Item_cond_or;
 
7506
      if (!new_cond)
 
7507
        return (COND*) 0;
 
7508
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7509
      Item *item;
 
7510
      while ((item=li++))
 
7511
      {
 
7512
        Item *fix= make_cond_remainder(item, false);
 
7513
        if (!fix)
 
7514
          return (COND*) 0;
 
7515
        new_cond->argument_list()->push_back(fix);
 
7516
        tbl_map |= fix->used_tables();
 
7517
      }
 
7518
      new_cond->quick_fix_field();
 
7519
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7520
      new_cond->top_level_item();
 
7521
      return new_cond;
 
7522
    }
 
7523
  }
 
7524
  return cond;
 
7525
}
 
7526
 
 
7527
 
 
7528
/*
 
7529
  Try to extract and push the index condition
 
7530
 
 
7531
  SYNOPSIS
 
7532
    push_index_cond()
 
7533
      tab            A join tab that has tab->table->file and its condition
 
7534
                     in tab->select_cond
 
7535
      keyno          Index for which extract and push the condition
 
7536
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7537
 
 
7538
  DESCRIPTION
 
7539
    Try to extract and push the index condition down to table handler
 
7540
*/
 
7541
 
 
7542
static void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok)
 
7543
{
 
7544
  Item *idx_cond;
 
7545
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
 
7546
      tab->join->thd->variables.engine_condition_pushdown)
 
7547
  {
 
7548
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
 
7549
                                  other_tbls_ok);
 
7550
 
 
7551
    if (idx_cond)
 
7552
    {
 
7553
      tab->pre_idx_push_select_cond= tab->select_cond;
 
7554
      Item *idx_remainder_cond= 
 
7555
        tab->table->file->idx_cond_push(keyno, idx_cond);
 
7556
 
 
7557
      /*
 
7558
        Disable eq_ref's "lookup cache" if we've pushed down an index
 
7559
        condition. 
 
7560
        TODO: This check happens to work on current ICP implementations, but
 
7561
        there may exist a compliant implementation that will not work 
 
7562
        correctly with it. Sort this out when we stabilize the condition
 
7563
        pushdown APIs.
 
7564
      */
 
7565
      if (idx_remainder_cond != idx_cond)
 
7566
        tab->ref.disable_cache= true;
 
7567
 
 
7568
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
 
7569
 
 
7570
      if (row_cond)
 
7571
      {
 
7572
        if (!idx_remainder_cond)
 
7573
          tab->select_cond= row_cond;
 
7574
        else
 
7575
        {
 
7576
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
 
7577
          tab->select_cond->quick_fix_field();
 
7578
          ((Item_cond_and*)tab->select_cond)->used_tables_cache= 
 
7579
            row_cond->used_tables() | idx_remainder_cond->used_tables();
 
7580
        }
 
7581
      }
 
7582
      else
 
7583
        tab->select_cond= idx_remainder_cond;
 
7584
      if (tab->select)
 
7585
      {
 
7586
        tab->select->cond= tab->select_cond;
 
7587
      }
 
7588
    }
 
7589
  }
 
7590
  return;
 
7591
}
 
7592
 
 
7593
 
 
7594
 
 
7595
    /*
 
7596
      Determine if the set is already ordered for order_st BY, so it can 
 
7597
      disable join cache because it will change the ordering of the results.
 
7598
      Code handles sort table that is at any location (not only first after 
 
7599
      the const tables) despite the fact that it's currently prohibited.
 
7600
      We must disable join cache if the first non-const table alone is
 
7601
      ordered. If there is a temp table the ordering is done as a last
 
7602
      operation and doesn't prevent join cache usage.
 
7603
    */
 
7604
uint32_t make_join_orderinfo(JOIN *join)
 
7605
{
 
7606
  uint32_t i;
 
7607
  if (join->need_tmp)
 
7608
    return join->tables;
 
7609
 
 
7610
  for (i=join->const_tables ; i < join->tables ; i++)
 
7611
  {
 
7612
    JOIN_TAB *tab=join->join_tab+i;
 
7613
    Table *table=tab->table;
 
7614
    if ((table == join->sort_by_table && 
 
7615
         (!join->order || join->skip_sort_order)) ||
 
7616
        (join->sort_by_table == (Table *) 1 && i != join->const_tables))
 
7617
    {
 
7618
      break;
 
7619
    }
 
7620
  }
 
7621
  return i;
 
7622
}
 
7623
 
 
7624
 
 
7625
/*
 
7626
  Plan refinement stage: do various set ups for the executioner
 
7627
 
 
7628
  SYNOPSIS
 
7629
    make_join_readinfo()
 
7630
      join           Join being processed
 
7631
      options        Join's options (checking for SELECT_DESCRIBE, 
 
7632
                     SELECT_NO_JOIN_CACHE)
 
7633
      no_jbuf_after  Don't use join buffering after table with this number.
 
7634
 
 
7635
  DESCRIPTION
 
7636
    Plan refinement stage: do various set ups for the executioner
 
7637
      - set up use of join buffering
 
7638
      - push index conditions
 
7639
      - increment counters
 
7640
      - etc
 
7641
 
 
7642
  RETURN 
 
7643
    false - OK
 
7644
    true  - Out of memory
 
7645
*/
 
7646
 
 
7647
static bool
 
7648
make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
7649
{
 
7650
  uint32_t i;
 
7651
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
 
7652
  bool sorted= 1;
 
7653
 
 
7654
  for (i=join->const_tables ; i < join->tables ; i++)
 
7655
  {
 
7656
    JOIN_TAB *tab=join->join_tab+i;
 
7657
    Table *table=tab->table;
 
7658
    bool using_join_cache;
 
7659
    tab->read_record.table= table;
 
7660
    tab->read_record.file=table->file;
 
7661
    tab->next_select=sub_select;                /* normal select */
 
7662
    /* 
 
7663
      TODO: don't always instruct first table's ref/range access method to 
 
7664
      produce sorted output.
 
7665
    */
 
7666
    tab->sorted= sorted;
 
7667
    sorted= 0;                                  // only first must be sorted
 
7668
    if (tab->insideout_match_tab)
 
7669
    {
 
7670
      if (!(tab->insideout_buf= (unsigned char*)join->thd->alloc(tab->table->key_info
 
7671
                                                         [tab->index].
 
7672
                                                         key_length)))
 
7673
        return true;
 
7674
    }
 
7675
    switch (tab->type) {
 
7676
    case JT_SYSTEM:                             // Only happens with left join
 
7677
      table->status=STATUS_NO_RECORD;
 
7678
      tab->read_first_record= join_read_system;
 
7679
      tab->read_record.read_record= join_no_more_records;
 
7680
      break;
 
7681
    case JT_CONST:                              // Only happens with left join
 
7682
      table->status=STATUS_NO_RECORD;
 
7683
      tab->read_first_record= join_read_const;
 
7684
      tab->read_record.read_record= join_no_more_records;
 
7685
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7686
          !table->no_keyread)
 
7687
      {
 
7688
        table->key_read=1;
 
7689
        table->file->extra(HA_EXTRA_KEYREAD);
 
7690
      }
 
7691
      break;
 
7692
    case JT_EQ_REF:
 
7693
      table->status=STATUS_NO_RECORD;
 
7694
      if (tab->select)
 
7695
      {
 
7696
        delete tab->select->quick;
 
7697
        tab->select->quick=0;
 
7698
      }
 
7699
      delete tab->quick;
 
7700
      tab->quick=0;
 
7701
      tab->read_first_record= join_read_key;
 
7702
      tab->read_record.read_record= join_no_more_records;
 
7703
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7704
          !table->no_keyread)
 
7705
      {
 
7706
        table->key_read=1;
 
7707
        table->file->extra(HA_EXTRA_KEYREAD);
 
7708
      }
 
7709
      else
 
7710
        push_index_cond(tab, tab->ref.key, true);
 
7711
      break;
 
7712
    case JT_REF_OR_NULL:
 
7713
    case JT_REF:
 
7714
      table->status=STATUS_NO_RECORD;
 
7715
      if (tab->select)
 
7716
      {
 
7717
        delete tab->select->quick;
 
7718
        tab->select->quick=0;
 
7719
      }
 
7720
      delete tab->quick;
 
7721
      tab->quick=0;
 
7722
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7723
          !table->no_keyread)
 
7724
      {
 
7725
        table->key_read=1;
 
7726
        table->file->extra(HA_EXTRA_KEYREAD);
 
7727
      }
 
7728
      else
 
7729
        push_index_cond(tab, tab->ref.key, true);
 
7730
      if (tab->type == JT_REF)
 
7731
      {
 
7732
        tab->read_first_record= join_read_always_key;
 
7733
        tab->read_record.read_record= tab->insideout_match_tab? 
 
7734
           join_read_next_same_diff : join_read_next_same;
 
7735
      }
 
7736
      else
 
7737
      {
 
7738
        tab->read_first_record= join_read_always_key_or_null;
 
7739
        tab->read_record.read_record= join_read_next_same_or_null;
 
7740
      }
 
7741
      break;
 
7742
    case JT_ALL:
 
7743
      /*
 
7744
        If previous table use cache
 
7745
        If the incoming data set is already sorted don't use cache.
 
7746
      */
 
7747
      table->status=STATUS_NO_RECORD;
 
7748
      using_join_cache= false;
 
7749
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
7750
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
 
7751
          !tab->insideout_match_tab)
 
7752
      {
 
7753
        if ((options & SELECT_DESCRIBE) ||
 
7754
            !join_init_cache(join->thd,join->join_tab+join->const_tables,
 
7755
                             i-join->const_tables))
 
7756
        {
 
7757
          using_join_cache= true;
 
7758
          tab[-1].next_select=sub_select_cache; /* Patch previous */
 
7759
        }
 
7760
      }
 
7761
      /* These init changes read_record */
 
7762
      if (tab->use_quick == 2)
 
7763
      {
 
7764
        join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
 
7765
        tab->read_first_record= join_init_quick_read_record;
 
7766
        if (statistics)
 
7767
          status_var_increment(join->thd->status_var.select_range_check_count);
 
7768
      }
 
7769
      else
 
7770
      {
 
7771
        tab->read_first_record= join_init_read_record;
 
7772
        if (i == join->const_tables)
 
7773
        {
 
7774
          if (tab->select && tab->select->quick)
 
7775
          {
 
7776
            if (statistics)
 
7777
              status_var_increment(join->thd->status_var.select_range_count);
 
7778
          }
 
7779
          else
 
7780
          {
 
7781
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7782
            if (statistics)
 
7783
              status_var_increment(join->thd->status_var.select_scan_count);
 
7784
          }
 
7785
        }
 
7786
        else
 
7787
        {
 
7788
          if (tab->select && tab->select->quick)
 
7789
          {
 
7790
            if (statistics)
 
7791
              status_var_increment(join->thd->status_var.select_full_range_join_count);
 
7792
          }
 
7793
          else
 
7794
          {
 
7795
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7796
            if (statistics)
 
7797
              status_var_increment(join->thd->status_var.select_full_join_count);
 
7798
          }
 
7799
        }
 
7800
        if (!table->no_keyread)
 
7801
        {
 
7802
          if (tab->select && tab->select->quick &&
 
7803
              tab->select->quick->index != MAX_KEY && //not index_merge
 
7804
              table->covering_keys.is_set(tab->select->quick->index))
 
7805
          {
 
7806
            table->key_read=1;
 
7807
            table->file->extra(HA_EXTRA_KEYREAD);
 
7808
          }
 
7809
          else if (!table->covering_keys.is_clear_all() &&
 
7810
                   !(tab->select && tab->select->quick))
 
7811
          {                                     // Only read index tree
 
7812
            if (!tab->insideout_match_tab)
 
7813
            {
 
7814
              /*
 
7815
                See bug #26447: "Using the clustered index for a table scan
 
7816
                is always faster than using a secondary index".
 
7817
              */
 
7818
              if (table->s->primary_key != MAX_KEY &&
 
7819
                  table->file->primary_key_is_clustered())
 
7820
                tab->index= table->s->primary_key;
 
7821
              else
 
7822
                tab->index= table->find_shortest_key(&table->covering_keys);
 
7823
            }
 
7824
            tab->read_first_record= join_read_first;
 
7825
            tab->type=JT_NEXT;          // Read with index_first / index_next
 
7826
          }
 
7827
        }
 
7828
        if (tab->select && tab->select->quick &&
 
7829
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
 
7830
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
 
7831
      }
 
7832
      break;
 
7833
    default:
 
7834
      break;                                    /* purecov: deadcode */
 
7835
    case JT_UNKNOWN:
 
7836
    case JT_MAYBE_REF:
 
7837
      abort();                                  /* purecov: deadcode */
 
7838
    }
 
7839
  }
 
7840
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
 
7841
  return(false);
 
7842
}
 
7843
 
 
7844
 
 
7845
/**
 
7846
  Give error if we some tables are done with a full join.
 
7847
 
 
7848
  This is used by multi_table_update and multi_table_delete when running
 
7849
  in safe mode.
 
7850
 
 
7851
  @param join           Join condition
 
7852
 
 
7853
  @retval
 
7854
    0   ok
 
7855
  @retval
 
7856
    1   Error (full join used)
 
7857
*/
 
7858
 
 
7859
bool error_if_full_join(JOIN *join)
 
7860
{
 
7861
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
 
7862
       tab < end;
 
7863
       tab++)
 
7864
  {
 
7865
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
 
7866
    {
 
7867
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
 
7868
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
 
7869
      return(1);
 
7870
    }
 
7871
  }
 
7872
  return(0);
 
7873
}
 
7874
 
 
7875
 
 
7876
/**
 
7877
  cleanup JOIN_TAB.
 
7878
*/
 
7879
 
 
7880
void JOIN_TAB::cleanup()
 
7881
{
 
7882
  delete select;
 
7883
  select= 0;
 
7884
  delete quick;
 
7885
  quick= 0;
1227
7886
  if (cache.buff)
1228
 
  {
1229
 
    size_t size= cache.end - cache.buff;
1230
 
    global_join_buffer.sub(size);
1231
7887
    free(cache.buff);
1232
 
  }
1233
7888
  cache.buff= 0;
1234
7889
  limit= 0;
1235
7890
  if (table)
1237
7892
    if (table->key_read)
1238
7893
    {
1239
7894
      table->key_read= 0;
1240
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
7895
      table->file->extra(HA_EXTRA_NO_KEYREAD);
1241
7896
    }
1242
 
    table->cursor->ha_index_or_rnd_end();
 
7897
    table->file->ha_index_or_rnd_end();
1243
7898
    /*
1244
7899
      We need to reset this for next select
1245
7900
      (Tested in part_of_refkey)
1246
7901
    */
1247
7902
    table->reginfo.join_tab= 0;
1248
7903
  }
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
 
}
1261
 
 
1262
 
/**
1263
 
  Remove the following expressions from ORDER BY and GROUP BY:
 
7904
  end_read_record(&read_record);
 
7905
}
 
7906
 
 
7907
 
 
7908
/**
 
7909
  Partially cleanup JOIN after it has executed: close index or rnd read
 
7910
  (table cursors), free quick selects.
 
7911
 
 
7912
    This function is called in the end of execution of a JOIN, before the used
 
7913
    tables are unlocked and closed.
 
7914
 
 
7915
    For a join that is resolved using a temporary table, the first sweep is
 
7916
    performed against actual tables and an intermediate result is inserted
 
7917
    into the temprorary table.
 
7918
    The last sweep is performed against the temporary table. Therefore,
 
7919
    the base tables and associated buffers used to fill the temporary table
 
7920
    are no longer needed, and this function is called to free them.
 
7921
 
 
7922
    For a join that is performed without a temporary table, this function
 
7923
    is called after all rows are sent, but before EOF packet is sent.
 
7924
 
 
7925
    For a simple SELECT with no subqueries this function performs a full
 
7926
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
 
7927
    tables.
 
7928
 
 
7929
    If a JOIN is executed for a subquery or if it has a subquery, we can't
 
7930
    do the full cleanup and need to do a partial cleanup only.
 
7931
    - If a JOIN is not the top level join, we must not unlock the tables
 
7932
    because the outer select may not have been evaluated yet, and we
 
7933
    can't unlock only selected tables of a query.
 
7934
    - Additionally, if this JOIN corresponds to a correlated subquery, we
 
7935
    should not free quick selects and join buffers because they will be
 
7936
    needed for the next execution of the correlated subquery.
 
7937
    - However, if this is a JOIN for a [sub]select, which is not
 
7938
    a correlated subquery itself, but has subqueries, we can free it
 
7939
    fully and also free JOINs of all its subqueries. The exception
 
7940
    is a subquery in SELECT list, e.g: @n
 
7941
    SELECT a, (select cmax(b) from t1) group by c @n
 
7942
    This subquery will not be evaluated at first sweep and its value will
 
7943
    not be inserted into the temporary table. Instead, it's evaluated
 
7944
    when selecting from the temporary table. Therefore, it can't be freed
 
7945
    here even though it's not correlated.
 
7946
 
 
7947
  @todo
 
7948
    Unlock tables even if the join isn't top level select in the tree
 
7949
*/
 
7950
 
 
7951
void JOIN::join_free()
 
7952
{
 
7953
  SELECT_LEX_UNIT *tmp_unit;
 
7954
  SELECT_LEX *sl;
 
7955
  /*
 
7956
    Optimization: if not EXPLAIN and we are done with the JOIN,
 
7957
    free all tables.
 
7958
  */
 
7959
  bool full= (!select_lex->uncacheable && !thd->lex->describe);
 
7960
  bool can_unlock= full;
 
7961
 
 
7962
  cleanup(full);
 
7963
 
 
7964
  for (tmp_unit= select_lex->first_inner_unit();
 
7965
       tmp_unit;
 
7966
       tmp_unit= tmp_unit->next_unit())
 
7967
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
 
7968
    {
 
7969
      Item_subselect *subselect= sl->master_unit()->item;
 
7970
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
7971
      /*
 
7972
        If this join is evaluated, we can fully clean it up and clean up all
 
7973
        its underlying joins even if they are correlated -- they will not be
 
7974
        used any more anyway.
 
7975
        If this join is not yet evaluated, we still must clean it up to
 
7976
        close its table cursors -- it may never get evaluated, as in case of
 
7977
        ... HAVING false OR a IN (SELECT ...))
 
7978
        but all table cursors must be closed before the unlock.
 
7979
      */
 
7980
      sl->cleanup_all_joins(full_local);
 
7981
      /* Can't unlock if at least one JOIN is still needed */
 
7982
      can_unlock= can_unlock && full_local;
 
7983
    }
 
7984
 
 
7985
  /*
 
7986
    We are not using tables anymore
 
7987
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
7988
  */
 
7989
  if (can_unlock && lock && thd->lock &&
 
7990
      !(select_options & SELECT_NO_UNLOCK) &&
 
7991
      !select_lex->subquery_in_having &&
 
7992
      (select_lex == (thd->lex->unit.fake_select_lex ?
 
7993
                      thd->lex->unit.fake_select_lex : &thd->lex->select_lex)))
 
7994
  {
 
7995
    /*
 
7996
      TODO: unlock tables even if the join isn't top level select in the
 
7997
      tree.
 
7998
    */
 
7999
    mysql_unlock_read_tables(thd, lock);           // Don't free join->lock
 
8000
    lock= 0;
 
8001
  }
 
8002
 
 
8003
  return;
 
8004
}
 
8005
 
 
8006
 
 
8007
/**
 
8008
  Free resources of given join.
 
8009
 
 
8010
  @param fill   true if we should free all resources, call with full==1
 
8011
                should be last, before it this function can be called with
 
8012
                full==0
 
8013
 
 
8014
  @note
 
8015
    With subquery this function definitely will be called several times,
 
8016
    but even for simple query it can be called several times.
 
8017
*/
 
8018
 
 
8019
void JOIN::cleanup(bool full)
 
8020
{
 
8021
  if (table)
 
8022
  {
 
8023
    JOIN_TAB *tab,*end;
 
8024
    /*
 
8025
      Only a sorted table may be cached.  This sorted table is always the
 
8026
      first non const table in join->table
 
8027
    */
 
8028
    if (tables > const_tables) // Test for not-const tables
 
8029
    {
 
8030
      free_io_cache(table[const_tables]);
 
8031
      filesort_free_buffers(table[const_tables],full);
 
8032
    }
 
8033
 
 
8034
    if (full)
 
8035
    {
 
8036
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8037
        tab->cleanup();
 
8038
      table= 0;
 
8039
    }
 
8040
    else
 
8041
    {
 
8042
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8043
      {
 
8044
        if (tab->table)
 
8045
          tab->table->file->ha_index_or_rnd_end();
 
8046
      }
 
8047
    }
 
8048
    cleanup_sj_tmp_tables(this);//
 
8049
  }
 
8050
  /*
 
8051
    We are not using tables anymore
 
8052
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8053
  */
 
8054
  if (full)
 
8055
  {
 
8056
    if (tmp_join)
 
8057
      tmp_table_param.copy_field= 0;
 
8058
    group_fields.delete_elements();
 
8059
    /*
 
8060
      We can't call delete_elements() on copy_funcs as this will cause
 
8061
      problems in free_elements() as some of the elements are then deleted.
 
8062
    */
 
8063
    tmp_table_param.copy_funcs.empty();
 
8064
    /*
 
8065
      If we have tmp_join and 'this' JOIN is not tmp_join and
 
8066
      tmp_table_param.copy_field's  of them are equal then we have to remove
 
8067
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
 
8068
      be removed in tmp_table_param.cleanup().
 
8069
    */
 
8070
    if (tmp_join &&
 
8071
        tmp_join != this &&
 
8072
        tmp_join->tmp_table_param.copy_field ==
 
8073
        tmp_table_param.copy_field)
 
8074
    {
 
8075
      tmp_join->tmp_table_param.copy_field=
 
8076
        tmp_join->tmp_table_param.save_copy_field= 0;
 
8077
    }
 
8078
    tmp_table_param.cleanup();
 
8079
  }
 
8080
  return;
 
8081
}
 
8082
 
 
8083
 
 
8084
/**
 
8085
  Remove the following expressions from order_st BY and GROUP BY:
1264
8086
  Constant expressions @n
1265
8087
  Expression that only uses tables that are of type EQ_REF and the reference
1266
8088
  is in the order_st list or if all refereed tables are of the above type.
1267
8089
 
1268
8090
  In the following, the X field can be removed:
1269
8091
  @code
1270
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
1271
 
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
 
8092
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
 
8093
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
1272
8094
  @endcode
1273
8095
 
1274
8096
  These can't be optimized:
1275
8097
  @code
1276
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
1277
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
1278
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
 
8098
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
 
8099
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
 
8100
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
1279
8101
  @endcode
1280
8102
*/
1281
 
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
 
8103
 
 
8104
static bool
 
8105
eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
1282
8106
{
1283
8107
  if (tab->cached_eq_ref_table)                 // If cached
1284
8108
    return tab->eq_ref_table;
1285
8109
  tab->cached_eq_ref_table=1;
1286
8110
  /* 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)
 
8111
  if (tab->type == JT_CONST && !tab->first_inner)
 
8112
    return (tab->eq_ref_table=1);               /* purecov: inspected */
 
8113
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
1290
8114
    return (tab->eq_ref_table=0);               // We must use this
1291
8115
  Item **ref_item=tab->ref.items;
1292
8116
  Item **end=ref_item+tab->ref.key_parts;
1297
8121
  {
1298
8122
    if (! (*ref_item)->const_item())
1299
8123
    {                                           // Not a const ref
1300
 
      Order *order;
 
8124
      order_st *order;
1301
8125
      for (order=start_order ; order ; order=order->next)
1302
8126
      {
1303
 
        if ((*ref_item)->eq(order->item[0],0))
1304
 
          break;
 
8127
        if ((*ref_item)->eq(order->item[0],0))
 
8128
          break;
1305
8129
      }
1306
8130
      if (order)
1307
8131
      {
1308
 
        found++;
1309
 
        assert(!(order->used & map));
1310
 
        order->used|=map;
1311
 
        continue;                               // Used in order_st BY
 
8132
        found++;
 
8133
        assert(!(order->used & map));
 
8134
        order->used|=map;
 
8135
        continue;                               // Used in order_st BY
1312
8136
      }
1313
8137
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1314
 
        return (tab->eq_ref_table= 0);
 
8138
        return (tab->eq_ref_table=0);
1315
8139
    }
1316
8140
  }
1317
8141
  /* Check that there was no reference to table before sort order */
1323
8147
      continue;
1324
8148
    }
1325
8149
    if (start_order->depend_map & map)
1326
 
      return (tab->eq_ref_table= 0);
1327
 
  }
1328
 
  return tab->eq_ref_table= 1;
1329
 
}
 
8150
      return (tab->eq_ref_table=0);
 
8151
  }
 
8152
  return tab->eq_ref_table=1;
 
8153
}
 
8154
 
 
8155
 
 
8156
static bool
 
8157
only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
 
8158
{
 
8159
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
 
8160
  {
 
8161
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
8162
      return 0;
 
8163
  }
 
8164
  return 1;
 
8165
}
 
8166
 
 
8167
 
 
8168
/** Update the dependency map for the tables. */
 
8169
 
 
8170
static void update_depend_map(JOIN *join)
 
8171
{
 
8172
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
 
8173
 
 
8174
  for (; join_tab != end ; join_tab++)
 
8175
  {
 
8176
    TABLE_REF *ref= &join_tab->ref;
 
8177
    table_map depend_map=0;
 
8178
    Item **item=ref->items;
 
8179
    uint32_t i;
 
8180
    for (i=0 ; i < ref->key_parts ; i++,item++)
 
8181
      depend_map|=(*item)->used_tables();
 
8182
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
 
8183
    depend_map&= ~OUTER_REF_TABLE_BIT;
 
8184
    for (JOIN_TAB **tab=join->map2table;
 
8185
         depend_map ;
 
8186
         tab++,depend_map>>=1 )
 
8187
    {
 
8188
      if (depend_map & 1)
 
8189
        ref->depend_map|=(*tab)->ref.depend_map;
 
8190
    }
 
8191
  }
 
8192
}
 
8193
 
 
8194
 
 
8195
/** Update the dependency map for the sort order. */
 
8196
 
 
8197
static void update_depend_map(JOIN *join, order_st *order)
 
8198
{
 
8199
  for (; order ; order=order->next)
 
8200
  {
 
8201
    table_map depend_map;
 
8202
    order->item[0]->update_used_tables();
 
8203
    order->depend_map=depend_map=order->item[0]->used_tables();
 
8204
    // Not item_sum(), RAND() and no reference to table outside of sub select
 
8205
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
 
8206
        && !order->item[0]->with_sum_func)
 
8207
    {
 
8208
      for (JOIN_TAB **tab=join->map2table;
 
8209
           depend_map ;
 
8210
           tab++, depend_map>>=1)
 
8211
      {
 
8212
        if (depend_map & 1)
 
8213
          order->depend_map|=(*tab)->ref.depend_map;
 
8214
      }
 
8215
    }
 
8216
  }
 
8217
}
 
8218
 
 
8219
 
 
8220
/**
 
8221
  Remove all constants and check if order_st only contains simple
 
8222
  expressions.
 
8223
 
 
8224
  simple_order is set to 1 if sort_order only uses fields from head table
 
8225
  and the head table is not a LEFT JOIN table.
 
8226
 
 
8227
  @param join                   Join handler
 
8228
  @param first_order            List of SORT or GROUP order
 
8229
  @param cond                   WHERE statement
 
8230
  @param change_list            Set to 1 if we should remove things from list.
 
8231
                               If this is not set, then only simple_order is
 
8232
                               calculated.
 
8233
  @param simple_order           Set to 1 if we are only using simple expressions
 
8234
 
 
8235
  @return
 
8236
    Returns new sort order
 
8237
*/
 
8238
 
 
8239
static order_st *
 
8240
remove_const(JOIN *join,order_st *first_order, COND *cond,
 
8241
             bool change_list, bool *simple_order)
 
8242
{
 
8243
  if (join->tables == join->const_tables)
 
8244
    return change_list ? 0 : first_order;               // No need to sort
 
8245
 
 
8246
  order_st *order,**prev_ptr;
 
8247
  table_map first_table= join->join_tab[join->const_tables].table->map;
 
8248
  table_map not_const_tables= ~join->const_table_map;
 
8249
  table_map ref;
 
8250
 
 
8251
  prev_ptr= &first_order;
 
8252
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
 
8253
 
 
8254
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
 
8255
 
 
8256
  update_depend_map(join, first_order);
 
8257
  for (order=first_order; order ; order=order->next)
 
8258
  {
 
8259
    table_map order_tables=order->item[0]->used_tables();
 
8260
    if (order->item[0]->with_sum_func)
 
8261
      *simple_order=0;                          // Must do a temp table to sort
 
8262
    else if (!(order_tables & not_const_tables))
 
8263
    {
 
8264
      if (order->item[0]->with_subselect)
 
8265
        order->item[0]->val_str(&order->item[0]->str_value);
 
8266
      continue;                                 // skip const item
 
8267
    }
 
8268
    else
 
8269
    {
 
8270
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
 
8271
        *simple_order=0;
 
8272
      else
 
8273
      {
 
8274
        Item *comp_item=0;
 
8275
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
 
8276
        {
 
8277
          continue;
 
8278
        }
 
8279
        if ((ref=order_tables & (not_const_tables ^ first_table)))
 
8280
        {
 
8281
          if (!(order_tables & first_table) &&
 
8282
              only_eq_ref_tables(join,first_order, ref))
 
8283
          {
 
8284
            continue;
 
8285
          }
 
8286
          *simple_order=0;                      // Must do a temp table to sort
 
8287
        }
 
8288
      }
 
8289
    }
 
8290
    if (change_list)
 
8291
      *prev_ptr= order;                         // use this entry
 
8292
    prev_ptr= &order->next;
 
8293
  }
 
8294
  if (change_list)
 
8295
    *prev_ptr=0;
 
8296
  if (prev_ptr == &first_order)                 // Nothing to sort/group
 
8297
    *simple_order=1;
 
8298
  return(first_order);
 
8299
}
 
8300
 
 
8301
 
 
8302
static int
 
8303
return_zero_rows(JOIN *join, select_result *result,TableList *tables,
 
8304
                 List<Item> &fields, bool send_row, uint64_t select_options,
 
8305
                 const char *info, Item *having)
 
8306
{
 
8307
  if (select_options & SELECT_DESCRIBE)
 
8308
  {
 
8309
    select_describe(join, false, false, false, info);
 
8310
    return(0);
 
8311
  }
 
8312
 
 
8313
  join->join_free();
 
8314
 
 
8315
  if (send_row)
 
8316
  {
 
8317
    for (TableList *table= tables; table; table= table->next_leaf)
 
8318
      mark_as_null_row(table->table);           // All fields are NULL
 
8319
    if (having && having->val_int() == 0)
 
8320
      send_row=0;
 
8321
  }
 
8322
  if (!(result->send_fields(fields,
 
8323
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
 
8324
  {
 
8325
    if (send_row)
 
8326
    {
 
8327
      List_iterator_fast<Item> it(fields);
 
8328
      Item *item;
 
8329
      while ((item= it++))
 
8330
        item->no_rows_in_result();
 
8331
      result->send_data(fields);
 
8332
    }
 
8333
    result->send_eof();                         // Should be safe
 
8334
  }
 
8335
  /* Update results for FOUND_ROWS */
 
8336
  join->thd->limit_found_rows= join->thd->examined_row_count= 0;
 
8337
  return(0);
 
8338
}
 
8339
 
 
8340
/*
 
8341
  used only in JOIN::clear
 
8342
*/
 
8343
static void clear_tables(JOIN *join)
 
8344
{
 
8345
  /* 
 
8346
    must clear only the non-const tables, as const tables
 
8347
    are not re-calculated.
 
8348
  */
 
8349
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
8350
    mark_as_null_row(join->table[i]);           // All fields are NULL
 
8351
}
 
8352
 
 
8353
/*****************************************************************************
 
8354
  Make som simple condition optimization:
 
8355
  If there is a test 'field = const' change all refs to 'field' to 'const'
 
8356
  Remove all dummy tests 'item = item', 'const op const'.
 
8357
  Remove all 'item is NULL', when item can never be null!
 
8358
  item->marker should be 0 for all items on entry
 
8359
  Return in cond_value false if condition is impossible (1 = 2)
 
8360
*****************************************************************************/
 
8361
 
 
8362
class COND_CMP :public ilink {
 
8363
public:
 
8364
  static void *operator new(size_t size)
 
8365
  {
 
8366
    return (void*) sql_alloc((uint) size);
 
8367
  }
 
8368
  static void operator delete(void *ptr __attribute__((unused)),
 
8369
                              size_t size __attribute__((unused)))
 
8370
  { TRASH(ptr, size); }
 
8371
 
 
8372
  Item *and_level;
 
8373
  Item_func *cmp_func;
 
8374
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
 
8375
};
 
8376
 
 
8377
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
8378
template class I_List<COND_CMP>;
 
8379
template class I_List_iterator<COND_CMP>;
 
8380
#endif
 
8381
 
1330
8382
 
1331
8383
/**
1332
8384
  Find the multiple equality predicate containing a field.
1346
8398
    - Item_equal for the found multiple equality predicate if a success;
1347
8399
    - NULL otherwise.
1348
8400
*/
1349
 
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
 
8401
 
 
8402
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
 
8403
                            bool *inherited_fl)
1350
8404
{
1351
8405
  Item_equal *item= 0;
1352
8406
  bool in_upper_level= false;
1353
8407
  while (cond_equal)
1354
8408
  {
1355
 
    List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
8409
    List_iterator_fast<Item_equal> li(cond_equal->current_level);
1356
8410
    while ((item= li++))
1357
8411
    {
1358
8412
      if (item->contains(field))
1367
8421
  return item;
1368
8422
}
1369
8423
 
 
8424
  
1370
8425
/**
1371
8426
  Check whether an equality can be used to build multiple equalities.
1372
8427
 
1393
8448
    the check_equality will be called for the following equality
1394
8449
    predicates a=b, b=c, b=2 and f=e.
1395
8450
    - For a=b it will be called with *cond_equal=(0,[]) and will transform
1396
 
      *cond_equal into (0,[Item_equal(a,b)]).
 
8451
      *cond_equal into (0,[Item_equal(a,b)]). 
1397
8452
    - For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)])
1398
8453
      and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]).
1399
8454
    - For b=2 it will be called with *cond_equal=(ptr(CE),[])
1406
8461
    the Field::eq_def method) are placed to the same multiple equalities.
1407
8462
    Because of this some equality predicates are not eliminated and
1408
8463
    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
 
8464
    We could weeken the equlity test as soon as at least one of the 
 
8465
    equal fields is to be equal to a constant. It would require a 
1411
8466
    more complicated implementation: we would have to store, in
1412
8467
    general case, its own constant for each fields from the multiple
1413
8468
    equality. But at the same time it would allow us to get rid
1425
8480
    containing just field1 and field2 is added to the existing
1426
8481
    multiple equalities.
1427
8482
    If the function processes the predicate of the form field1=const,
1428
 
    it looks for a multiple equality containing field1. If found, the
 
8483
    it looks for a multiple equality containing field1. If found, the 
1429
8484
    function checks the constant of the multiple equality. If the value
1430
8485
    is unknown, it is setup to const. Otherwise the value is compared with
1431
8486
    const and the evaluation of the equality predicate is performed.
1448
8503
  @retval
1449
8504
    false   otherwise
1450
8505
*/
1451
 
static bool check_simple_equality(Item *left_item,
1452
 
                                  Item *right_item,
1453
 
                                  Item *item,
1454
 
                                  COND_EQUAL *cond_equal)
 
8506
 
 
8507
static bool check_simple_equality(Item *left_item, Item *right_item,
 
8508
                                  Item *item, COND_EQUAL *cond_equal)
1455
8509
{
 
8510
  if (left_item->type() == Item::REF_ITEM &&
 
8511
      ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
 
8512
  {
 
8513
    if (((Item_ref*)left_item)->depended_from)
 
8514
      return false;
 
8515
    left_item= left_item->real_item();
 
8516
  }
 
8517
  if (right_item->type() == Item::REF_ITEM &&
 
8518
      ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
 
8519
  {
 
8520
    if (((Item_ref*)right_item)->depended_from)
 
8521
      return false;
 
8522
    right_item= right_item->real_item();
 
8523
  }
1456
8524
  if (left_item->type() == Item::FIELD_ITEM &&
1457
8525
      right_item->type() == Item::FIELD_ITEM &&
1458
8526
      !((Item_field*)left_item)->depended_from &&
1470
8538
    bool left_copyfl, right_copyfl;
1471
8539
    Item_equal *left_item_equal=
1472
8540
               find_item_equal(cond_equal, left_field, &left_copyfl);
1473
 
    Item_equal *right_item_equal=
 
8541
    Item_equal *right_item_equal= 
1474
8542
               find_item_equal(cond_equal, right_field, &right_copyfl);
1475
8543
 
1476
8544
    /* As (NULL=NULL) != true we can't just remove the predicate f=f */
1477
8545
    if (left_field->eq(right_field)) /* f = f */
1478
 
      return (!(left_field->maybe_null() && !left_item_equal));
 
8546
      return (!(left_field->maybe_null() && !left_item_equal)); 
1479
8547
 
1480
8548
    if (left_item_equal && left_item_equal == right_item_equal)
1481
8549
    {
1482
 
      /*
 
8550
      /* 
1483
8551
        The equality predicate is inference of one of the existing
1484
8552
        multiple equalities, i.e the condition is already covered
1485
8553
        by upper level equalities
1486
8554
      */
1487
8555
       return true;
1488
8556
    }
1489
 
 
1490
 
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name &&
 
8557
    
 
8558
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name && 
1491
8559
                              item->name < subq_sj_cond_name + 64);
1492
8560
    /* Copy the found multiple equalities at the current level if needed */
1493
8561
    if (left_copyfl)
1508
8576
    }
1509
8577
 
1510
8578
    if (left_item_equal)
1511
 
    {
 
8579
    { 
1512
8580
      /* left item was found in the current or one of the upper levels */
1513
8581
      if (! right_item_equal)
1514
8582
        left_item_equal->add((Item_field *) right_item);
1517
8585
        /* Merge two multiple equalities forming a new one */
1518
8586
        left_item_equal->merge(right_item_equal);
1519
8587
        /* Remove the merged multiple equality from the list */
1520
 
        List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
8588
        List_iterator<Item_equal> li(cond_equal->current_level);
1521
8589
        while ((li++) != right_item_equal) {};
1522
8590
        li.remove();
1523
8591
      }
1524
8592
    }
1525
8593
    else
1526
 
    {
 
8594
    { 
1527
8595
      /* left item was not found neither the current nor in upper levels  */
1528
8596
      if (right_item_equal)
1529
8597
      {
1531
8599
        if (copy_item_name)
1532
8600
          right_item_equal->name = item->name;
1533
8601
      }
1534
 
      else
 
8602
      else 
1535
8603
      {
1536
8604
        /* None of the fields was found in multiple equalities */
1537
8605
        Item_equal *item_equal= new Item_equal((Item_field *) left_item,
1579
8647
          eq_item->set_cmp_func();
1580
8648
          eq_item->quick_fix_field();
1581
8649
          item= eq_item;
1582
 
        }
 
8650
        }  
1583
8651
        if ((cs != ((Item_func *) item)->compare_collation()) ||
1584
8652
            !cs->coll->propagate(cs, 0, 0))
1585
8653
          return false;
1594
8662
      }
1595
8663
      if (item_equal)
1596
8664
      {
1597
 
        /*
 
8665
        /* 
1598
8666
          The flag cond_false will be set to 1 after this, if item_equal
1599
8667
          already contains a constant and its value is  not equal to
1600
8668
          the value of const_item.
1612
8680
  return false;
1613
8681
}
1614
8682
 
 
8683
 
1615
8684
/**
1616
8685
  Convert row equalities into a conjunction of regular equalities.
1617
8686
 
1624
8693
    simple equality nor a row equality the item for this predicate is added
1625
8694
    to eq_list.
1626
8695
 
1627
 
  @param session        thread handle
 
8696
  @param thd        thread handle
1628
8697
  @param left_row   left term of the row equality to be processed
1629
8698
  @param right_row  right term of the row equality to be processed
1630
8699
  @param cond_equal multiple equalities that must hold together with the
1637
8706
  @retval
1638
8707
    false   otherwise
1639
8708
*/
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
 
{
 
8709
 
 
8710
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
 
8711
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
 
8712
1646
8713
  uint32_t n= left_row->cols();
1647
8714
  for (uint32_t i= 0 ; i < n; i++)
1648
8715
  {
1652
8719
    if (left_item->type() == Item::ROW_ITEM &&
1653
8720
        right_item->type() == Item::ROW_ITEM)
1654
8721
    {
1655
 
      is_converted= check_row_equality(session,
 
8722
      is_converted= check_row_equality(thd, 
1656
8723
                                       (Item_row *) left_item,
1657
8724
                                       (Item_row *) right_item,
1658
8725
                                       cond_equal, eq_list);
1659
8726
      if (!is_converted)
1660
 
        session->getLex()->current_select->cond_count++;
 
8727
        thd->lex->current_select->cond_count++;      
1661
8728
    }
1662
8729
    else
1663
 
    {
 
8730
    { 
1664
8731
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1665
 
      session->getLex()->current_select->cond_count++;
1666
 
    }
1667
 
 
 
8732
      thd->lex->current_select->cond_count++;
 
8733
    }  
 
8734
 
1668
8735
    if (!is_converted)
1669
8736
    {
1670
8737
      Item_func_eq *eq_item;
1678
8745
  return true;
1679
8746
}
1680
8747
 
 
8748
 
1681
8749
/**
1682
8750
  Eliminate row equalities and form multiple equalities predicates.
1683
8751
 
1692
8760
    equalities which are treated in the same way as original equality
1693
8761
    predicates.
1694
8762
 
1695
 
  @param session        thread handle
 
8763
  @param thd        thread handle
1696
8764
  @param item       predicate to process
1697
8765
  @param cond_equal multiple equalities that must hold together with the
1698
8766
                    predicate
1707
8775
           or, if the equality is neither a simple one nor a row equality,
1708
8776
           or, if the procedure fails by a fatal error.
1709
8777
*/
1710
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
 
8778
 
 
8779
static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal,
 
8780
                           List<Item> *eq_list)
1711
8781
{
1712
8782
  if (item->type() == Item::FUNC_ITEM &&
1713
8783
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
1718
8788
    if (left_item->type() == Item::ROW_ITEM &&
1719
8789
        right_item->type() == Item::ROW_ITEM)
1720
8790
    {
1721
 
      session->getLex()->current_select->cond_count--;
1722
 
      return check_row_equality(session,
 
8791
      thd->lex->current_select->cond_count--;
 
8792
      return check_row_equality(thd,
1723
8793
                                (Item_row *) left_item,
1724
8794
                                (Item_row *) right_item,
1725
8795
                                cond_equal, eq_list);
1726
8796
    }
1727
 
    else
 
8797
    else 
1728
8798
      return check_simple_equality(left_item, right_item, item, cond_equal);
1729
 
  }
 
8799
  } 
1730
8800
  return false;
1731
8801
}
1732
8802
 
 
8803
                          
1733
8804
/**
1734
8805
  Replace all equality predicates in a condition by multiple equality items.
1735
8806
 
1736
8807
    At each 'and' level the function detects items for equality predicates
1737
8808
    and replaced them by a set of multiple equality items of class Item_equal,
1738
 
    taking into account inherited equalities from upper levels.
 
8809
    taking into account inherited equalities from upper levels. 
1739
8810
    If an equality predicate is used not in a conjunction it's just
1740
8811
    replaced by a multiple equality predicate.
1741
8812
    For each 'and' level the function set a pointer to the inherited
1742
8813
    multiple equalities in the cond_equal field of the associated
1743
 
    object of the type Item_cond_and.
 
8814
    object of the type Item_cond_and.   
1744
8815
    The function also traverses the cond tree and and for each field reference
1745
8816
    sets a pointer to the multiple equality item containing the field, if there
1746
8817
    is any. If this multiple equality equates fields to a constant the
1747
 
    function replaces the field reference by the constant in the cases
 
8818
    function replaces the field reference by the constant in the cases 
1748
8819
    when the field is not of a string type or when the field reference is
1749
8820
    just an argument of a comparison predicate.
1750
 
    The function also determines the maximum number of members in
 
8821
    The function also determines the maximum number of members in 
1751
8822
    equality lists of each Item_cond_and object assigning it to
1752
 
    session->getLex()->current_select->max_equal_elems.
 
8823
    thd->lex->current_select->max_equal_elems.
1753
8824
 
1754
8825
  @note
1755
8826
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
1761
8832
    in a conjuction for a minimal set of multiple equality predicates.
1762
8833
    This set can be considered as a canonical representation of the
1763
8834
    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
 
8835
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by 
1765
8836
    (=(t1.a,t2.b,t3.c) AND t2.b>5), not by
1766
8837
    (=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5);
1767
8838
    while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by
1772
8843
    The function performs the substitution in a recursive descent by
1773
8844
    the condtion tree, passing to the next AND level a chain of multiple
1774
8845
    equality predicates which have been built at the upper levels.
1775
 
    The Item_equal items built at the level are attached to other
 
8846
    The Item_equal items built at the level are attached to other 
1776
8847
    non-equality conjucts as a sublist. The pointer to the inherited
1777
8848
    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
 
8849
    This chain allows us for any field reference occurence easyly to find a 
1779
8850
    multiple equality that must be held for this occurence.
1780
8851
    For each AND level we do the following:
1781
8852
    - scan it for all equality predicate (=) items
1782
8853
    - join them into disjoint Item_equal() groups
1783
 
    - process the included OR conditions recursively to do the same for
1784
 
      lower AND levels.
 
8854
    - process the included OR conditions recursively to do the same for 
 
8855
      lower AND levels. 
1785
8856
 
1786
8857
    We need to do things in this order as lower AND levels need to know about
1787
8858
    all possible Item_equal objects in upper levels.
1788
8859
 
1789
 
  @param session        thread handle
 
8860
  @param thd        thread handle
1790
8861
  @param cond       condition(expression) where to make replacement
1791
8862
  @param inherited  path to all inherited multiple equality items
1792
8863
 
1793
8864
  @return
1794
8865
    pointer to the transformed condition
1795
8866
*/
1796
 
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
 
8867
 
 
8868
static COND *build_equal_items_for_cond(THD *thd, COND *cond,
 
8869
                                        COND_EQUAL *inherited)
1797
8870
{
1798
8871
  Item_equal *item_equal;
1799
8872
  COND_EQUAL cond_equal;
1805
8878
    bool and_level= ((Item_cond*) cond)->functype() ==
1806
8879
      Item_func::COND_AND_FUNC;
1807
8880
    List<Item> *args= ((Item_cond*) cond)->argument_list();
1808
 
 
1809
 
    List<Item>::iterator li(args->begin());
 
8881
    
 
8882
    List_iterator<Item> li(*args);
1810
8883
    Item *item;
1811
8884
 
1812
8885
    if (and_level)
1814
8887
      /*
1815
8888
         Retrieve all conjucts of this level detecting the equality
1816
8889
         that are subject to substitution by multiple equality items and
1817
 
         removing each such predicate from the conjunction after having
 
8890
         removing each such predicate from the conjunction after having 
1818
8891
         found/created a multiple equality whose inference the predicate is.
1819
 
     */
 
8892
     */      
1820
8893
      while ((item= li++))
1821
8894
      {
1822
8895
        /*
1824
8897
          structure here because it's restored before each
1825
8898
          re-execution of any prepared statement/stored procedure.
1826
8899
        */
1827
 
        if (check_equality(session, item, &cond_equal, &eq_list))
 
8900
        if (check_equality(thd, item, &cond_equal, &eq_list))
1828
8901
          li.remove();
1829
8902
      }
1830
8903
 
1831
 
      List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
8904
      List_iterator_fast<Item_equal> it(cond_equal.current_level);
1832
8905
      while ((item_equal= it++))
1833
8906
      {
1834
8907
        item_equal->fix_length_and_dec();
1835
8908
        item_equal->update_used_tables();
1836
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
1837
 
                      item_equal->members());
 
8909
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8910
                      item_equal->members());  
1838
8911
      }
1839
8912
 
1840
8913
      ((Item_cond_and*)cond)->cond_equal= cond_equal;
1844
8917
       Make replacement of equality predicates for lower levels
1845
8918
       of the condition expression.
1846
8919
    */
1847
 
    li= args->begin();
 
8920
    li.rewind();
1848
8921
    while ((item= li++))
1849
 
    {
 
8922
    { 
1850
8923
      Item *new_item;
1851
 
      if ((new_item= build_equal_items_for_cond(session, item, inherited)) != item)
 
8924
      if ((new_item= build_equal_items_for_cond(thd, item, inherited)) != item)
1852
8925
      {
1853
8926
        /* This replacement happens only for standalone equalities */
1854
8927
        /*
1876
8949
      (b=5) and (a=c) are standalone equalities.
1877
8950
      In general we can't leave alone standalone eqalities:
1878
8951
      for WHERE a=b AND c=d AND (b=c OR d=5)
1879
 
      b=c is replaced by =(a,b,c,d).
 
8952
      b=c is replaced by =(a,b,c,d).  
1880
8953
     */
1881
 
    if (check_equality(session, cond, &cond_equal, &eq_list))
 
8954
    if (check_equality(thd, cond, &cond_equal, &eq_list))
1882
8955
    {
1883
8956
      int n= cond_equal.current_level.elements + eq_list.elements;
1884
8957
      if (n == 0)
1889
8962
        {
1890
8963
          item_equal->fix_length_and_dec();
1891
8964
          item_equal->update_used_tables();
1892
 
        }
 
8965
        }
1893
8966
        else
1894
8967
          item_equal= (Item_equal *) eq_list.pop();
1895
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
1896
 
                      item_equal->members());
 
8968
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8969
                      item_equal->members());  
1897
8970
        return item_equal;
1898
8971
      }
1899
8972
      else
1900
8973
      {
1901
 
        /*
 
8974
        /* 
1902
8975
          Here a new AND level must be created. It can happen only
1903
8976
          when a row equality is processed as a standalone predicate.
1904
 
        */
 
8977
        */
1905
8978
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
1906
8979
        and_cond->quick_fix_field();
1907
8980
        List<Item> *args= and_cond->argument_list();
1908
 
        List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
8981
        List_iterator_fast<Item_equal> it(cond_equal.current_level);
1909
8982
        while ((item_equal= it++))
1910
8983
        {
1911
8984
          item_equal->fix_length_and_dec();
1912
8985
          item_equal->update_used_tables();
1913
 
          set_if_bigger(session->getLex()->current_select->max_equal_elems,
1914
 
                        item_equal->members());
 
8986
          set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8987
                        item_equal->members());  
1915
8988
        }
1916
8989
        and_cond->cond_equal= cond_equal;
1917
8990
        args->concat((List<Item> *)&cond_equal.current_level);
1918
 
 
 
8991
        
1919
8992
        return and_cond;
1920
8993
      }
1921
8994
    }
1922
 
    /*
 
8995
    /* 
1923
8996
      For each field reference in cond, not from equal item predicates,
1924
8997
      set a pointer to the multiple equality it belongs to (if there is any)
1925
8998
      as soon the field is not of a string type or the field reference is
1926
 
      an argument of a comparison predicate.
1927
 
    */
 
8999
      an argument of a comparison predicate. 
 
9000
    */ 
1928
9001
    unsigned char *is_subst_valid= (unsigned char *) 1;
1929
9002
    cond= cond->compile(&Item::subst_argument_checker,
1930
 
                        &is_subst_valid,
 
9003
                        &is_subst_valid, 
1931
9004
                        &Item::equal_fields_propagator,
1932
9005
                        (unsigned char *) inherited);
1933
9006
    cond->update_used_tables();
1935
9008
  return cond;
1936
9009
}
1937
9010
 
 
9011
 
1938
9012
/**
1939
9013
  Build multiple equalities for a condition and all on expressions that
1940
9014
  inherit these multiple equalities.
1980
9054
      SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a
1981
9055
        WHERE t1.a=t2.a
1982
9056
    @endcode
1983
 
    that is equivalent to:
 
9057
    that is equivalent to:   
1984
9058
    @code
1985
9059
      SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1
1986
9060
        WHERE t1.a=t2.a
1987
9061
    @endcode
1988
9062
    Thus, applying equalities from the where condition we basically
1989
9063
    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
 
9064
    Althogh we don't use this property now, it probably makes sense to use 
 
9065
    it in the future.    
 
9066
  @param thd                  Thread handler
1993
9067
  @param cond                condition to build the multiple equalities for
1994
9068
  @param inherited           path to all inherited multiple equality items
1995
9069
  @param join_list           list of join tables to which the condition
2000
9074
  @return
2001
9075
    pointer to the transformed condition containing multiple equalities
2002
9076
*/
2003
 
static COND *build_equal_items(Session *session, COND *cond,
 
9077
   
 
9078
static COND *build_equal_items(THD *thd, COND *cond,
2004
9079
                               COND_EQUAL *inherited,
2005
9080
                               List<TableList> *join_list,
2006
9081
                               COND_EQUAL **cond_equal_ref)
2007
9082
{
2008
9083
  COND_EQUAL *cond_equal= 0;
2009
9084
 
2010
 
  if (cond)
 
9085
  if (cond) 
2011
9086
  {
2012
 
    cond= build_equal_items_for_cond(session, cond, inherited);
 
9087
    cond= build_equal_items_for_cond(thd, cond, inherited);
2013
9088
    cond->update_used_tables();
2014
9089
    if (cond->type() == Item::COND_ITEM &&
2015
9090
        ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
2031
9106
  if (join_list)
2032
9107
  {
2033
9108
    TableList *table;
2034
 
    List<TableList>::iterator li(join_list->begin());
 
9109
    List_iterator<TableList> li(*join_list);
2035
9110
 
2036
9111
    while ((table= li++))
2037
9112
    {
2038
9113
      if (table->on_expr)
2039
9114
      {
2040
 
        List<TableList> *nested_join_list= table->getNestedJoin() ?
2041
 
          &table->getNestedJoin()->join_list : NULL;
 
9115
        List<TableList> *nested_join_list= table->nested_join ?
 
9116
          &table->nested_join->join_list : NULL;
2042
9117
        /*
2043
9118
          We can modify table->on_expr because its old value will
2044
9119
          be restored before re-execution of PS/SP.
2045
9120
        */
2046
 
        table->on_expr= build_equal_items(session, table->on_expr, inherited,
 
9121
        table->on_expr= build_equal_items(thd, table->on_expr, inherited,
2047
9122
                                          nested_join_list,
2048
9123
                                          &table->cond_equal);
2049
9124
      }
2051
9126
  }
2052
9127
 
2053
9128
  return cond;
2054
 
}
 
9129
}    
 
9130
 
2055
9131
 
2056
9132
/**
2057
9133
  Compare field items by table order in the execution plan.
2058
9134
 
2059
9135
    field1 considered as better than field2 if the table containing
2060
 
    field1 is accessed earlier than the table containing field2.
 
9136
    field1 is accessed earlier than the table containing field2.   
2061
9137
    The function finds out what of two fields is better according
2062
9138
    this criteria.
2063
9139
 
2072
9148
  @retval
2073
9149
    0  otherwise
2074
9150
*/
 
9151
 
2075
9152
static int compare_fields_by_table_order(Item_field *field1,
2076
 
                                         Item_field *field2,
2077
 
                                         void *table_join_idx)
 
9153
                                  Item_field *field2,
 
9154
                                  void *table_join_idx)
2078
9155
{
2079
9156
  int cmp= 0;
2080
9157
  bool outer_ref= 0;
2081
9158
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
2082
 
  {
 
9159
  {  
2083
9160
    outer_ref= 1;
2084
9161
    cmp= -1;
2085
9162
  }
2090
9167
  }
2091
9168
  if (outer_ref)
2092
9169
    return cmp;
2093
 
  JoinTable **idx= (JoinTable **) table_join_idx;
2094
 
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
 
9170
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
 
9171
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
2095
9172
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2096
9173
}
2097
9174
 
 
9175
 
2098
9176
/**
2099
9177
  Generate minimal set of simple equalities equivalent to a multiple equality.
2100
9178
 
2134
9212
    a pointer to the simple generated equality, if success.
2135
9213
    - 0, otherwise.
2136
9214
*/
2137
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
 
9215
 
 
9216
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
 
9217
                                  Item_equal *item_equal)
2138
9218
{
2139
9219
  List<Item> eq_list;
2140
9220
  Item_func_eq *eq_item= 0;
2141
9221
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
2142
 
    return new Item_int((int64_t) 0,1);
 
9222
    return new Item_int((int64_t) 0,1); 
2143
9223
  Item *item_const= item_equal->get_const();
2144
9224
  Item_equal_iterator it(*item_equal);
2145
9225
  Item *head;
2156
9236
    Item_equal *upper= item_field->find_item_equal(upper_levels);
2157
9237
    Item_field *item= item_field;
2158
9238
    if (upper)
2159
 
    {
 
9239
    { 
2160
9240
      if (item_const && upper->get_const())
2161
9241
        item= 0;
2162
9242
      else
2200
9280
 
2201
9281
  cond->quick_fix_field();
2202
9282
  cond->update_used_tables();
2203
 
 
 
9283
   
2204
9284
  return cond;
2205
9285
}
2206
9286
 
 
9287
 
2207
9288
/**
2208
9289
  Substitute every field reference in a condition by the best equal field
2209
9290
  and eliminate all multiple equality predicates.
2212
9293
    multiple equality predicate it sorts the field references in it
2213
9294
    according to the order of tables specified by the table_join_idx
2214
9295
    parameter. Then it eliminates the multiple equality predicate it
2215
 
    replacing it by the conjunction of simple equality predicates
 
9296
    replacing it by the conjunction of simple equality predicates 
2216
9297
    equating every field from the multiple equality to the first
2217
9298
    field in it, or to the constant, if there is any.
2218
9299
    After this the function retrieves all other conjuncted
2231
9312
  @return
2232
9313
    The transformed condition
2233
9314
*/
2234
 
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
 
9315
 
 
9316
static COND* substitute_for_best_equal_field(COND *cond,
 
9317
                                             COND_EQUAL *cond_equal,
 
9318
                                             void *table_join_idx)
2235
9319
{
2236
9320
  Item_equal *item_equal;
2237
9321
 
2246
9330
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
2247
9331
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2248
9332
 
2249
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
9333
      List_iterator_fast<Item_equal> it(cond_equal->current_level);      
2250
9334
      while ((item_equal= it++))
2251
9335
      {
2252
9336
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2253
9337
      }
2254
9338
    }
2255
 
 
2256
 
    List<Item>::iterator li(cond_list->begin());
 
9339
    
 
9340
    List_iterator<Item> li(*cond_list);
2257
9341
    Item *item;
2258
9342
    while ((item= li++))
2259
9343
    {
2269
9353
 
2270
9354
    if (and_level)
2271
9355
    {
2272
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
9356
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2273
9357
      while ((item_equal= it++))
2274
9358
      {
2275
9359
        cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
2285
9369
      cond= new Item_int((int32_t)cond->val_bool());
2286
9370
 
2287
9371
  }
2288
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9372
  else if (cond->type() == Item::FUNC_ITEM && 
2289
9373
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2290
9374
  {
2291
9375
    item_equal= (Item_equal *) cond;
2299
9383
  return cond;
2300
9384
}
2301
9385
 
 
9386
 
2302
9387
/**
2303
9388
  Check appearance of new constant items in multiple equalities
2304
9389
  of a condition after reading a constant table.
2311
9396
  @param cond       condition whose multiple equalities are to be checked
2312
9397
  @param table      constant table that has been read
2313
9398
*/
2314
 
void update_const_equal_items(COND *cond, JoinTable *tab)
 
9399
 
 
9400
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
2315
9401
{
2316
9402
  if (!(cond->used_tables() & tab->table->map))
2317
9403
    return;
2318
9404
 
2319
9405
  if (cond->type() == Item::COND_ITEM)
2320
9406
  {
2321
 
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
2322
 
    List<Item>::iterator li(cond_list->begin());
 
9407
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); 
 
9408
    List_iterator_fast<Item> li(*cond_list);
2323
9409
    Item *item;
2324
9410
    while ((item= li++))
2325
9411
      update_const_equal_items(item, tab);
2326
9412
  }
2327
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9413
  else if (cond->type() == Item::FUNC_ITEM && 
2328
9414
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2329
9415
  {
2330
9416
    Item_equal *item_equal= (Item_equal *) cond;
2338
9424
      while ((item_field= it++))
2339
9425
      {
2340
9426
        Field *field= item_field->field;
2341
 
        JoinTable *stat= field->getTable()->reginfo.join_tab;
 
9427
        JOIN_TAB *stat= field->table->reginfo.join_tab;
2342
9428
        key_map possible_keys= field->key_start;
2343
 
        possible_keys&= field->getTable()->keys_in_use_for_query;
2344
 
        stat[0].const_keys|= possible_keys;
 
9429
        possible_keys.intersect(field->table->keys_in_use_for_query);
 
9430
        stat[0].const_keys.merge(possible_keys);
2345
9431
 
2346
9432
        /*
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
 
9433
          For each field in the multiple equality (for which we know that it 
 
9434
          is a constant) we have to find its corresponding key part, and set 
2349
9435
          that key part in const_key_parts.
2350
 
        */
2351
 
        if (possible_keys.any())
 
9436
        */  
 
9437
        if (!possible_keys.is_clear_all())
2352
9438
        {
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 ==
 
9439
          Table *tab= field->table;
 
9440
          KEYUSE *use;
 
9441
          for (use= stat->keyuse; use && use->table == tab; use++)
 
9442
            if (possible_keys.is_set(use->key) && 
 
9443
                tab->key_info[use->key].key_part[use->keypart].field ==
2358
9444
                field)
2359
 
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
9445
              tab->const_key_parts[use->key]|= use->keypart_map;
2360
9446
        }
2361
9447
      }
2362
9448
    }
2363
9449
  }
2364
9450
}
2365
9451
 
 
9452
 
2366
9453
/*
2367
9454
  change field = field to field = const for each found field = const in the
2368
9455
  and_level
2369
9456
*/
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)
 
9457
 
 
9458
static void
 
9459
change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
 
9460
                         Item *and_father, Item *cond,
 
9461
                         Item *field, Item *value)
2376
9462
{
2377
9463
  if (cond->type() == Item::COND_ITEM)
2378
9464
  {
2379
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2380
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
9465
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9466
      Item_func::COND_AND_FUNC;
 
9467
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2381
9468
    Item *item;
2382
9469
    while ((item=li++))
2383
 
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2384
 
 
 
9470
      change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item,
 
9471
                               field, value);
2385
9472
    return;
2386
9473
  }
2387
9474
  if (cond->eq_cmp_result() == Item::COND_OK)
2400
9487
       left_item->collation.collation == value->collation.collation))
2401
9488
  {
2402
9489
    Item *tmp=value->clone_item();
 
9490
    tmp->collation.set(right_item->collation);
 
9491
    
2403
9492
    if (tmp)
2404
9493
    {
2405
 
      tmp->collation.set(right_item->collation);
2406
 
      session->change_item_tree(args + 1, tmp);
 
9494
      thd->change_item_tree(args + 1, tmp);
2407
9495
      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())
 
9496
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9497
          && and_father != cond && !left_item->const_item())
2411
9498
      {
2412
 
        cond->marker=1;
2413
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9499
        cond->marker=1;
 
9500
        COND_CMP *tmp2;
 
9501
        if ((tmp2=new COND_CMP(and_father,func)))
 
9502
          save_list->push_back(tmp2);
2414
9503
      }
2415
9504
      func->set_cmp_func();
2416
9505
    }
2422
9511
            right_item->collation.collation == value->collation.collation))
2423
9512
  {
2424
9513
    Item *tmp= value->clone_item();
 
9514
    tmp->collation.set(left_item->collation);
 
9515
    
2425
9516
    if (tmp)
2426
9517
    {
2427
 
      tmp->collation.set(left_item->collation);
2428
 
      session->change_item_tree(args, tmp);
 
9518
      thd->change_item_tree(args, tmp);
2429
9519
      value= tmp;
2430
9520
      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())
 
9521
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9522
          && and_father != cond && !right_item->const_item())
2434
9523
      {
2435
9524
        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) );
 
9525
        thd->change_item_tree(args + 1, value);
 
9526
        cond->marker=1;
 
9527
        COND_CMP *tmp2;
 
9528
        if ((tmp2=new COND_CMP(and_father,func)))
 
9529
          save_list->push_back(tmp2);
2439
9530
      }
2440
9531
      func->set_cmp_func();
2441
9532
    }
2450
9541
  @return
2451
9542
    new conditions
2452
9543
*/
2453
 
Item *remove_additional_cond(Item* conds)
 
9544
 
 
9545
static Item *remove_additional_cond(Item* conds)
2454
9546
{
2455
9547
  if (conds->name == in_additional_cond)
2456
9548
    return 0;
2457
9549
  if (conds->type() == Item::COND_ITEM)
2458
9550
  {
2459
9551
    Item_cond *cnd= (Item_cond*) conds;
2460
 
    List<Item>::iterator li(cnd->argument_list()->begin());
 
9552
    List_iterator<Item> li(*(cnd->argument_list()));
2461
9553
    Item *item;
2462
9554
    while ((item= li++))
2463
9555
    {
2473
9565
  return conds;
2474
9566
}
2475
9567
 
2476
 
static void propagate_cond_constants(Session *session, 
2477
 
                                     list<COND_CMP>& save_list, 
2478
 
                                     COND *and_father, 
2479
 
                                     COND *cond)
 
9568
static void
 
9569
propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list,
 
9570
                         COND *and_father, COND *cond)
2480
9571
{
2481
9572
  if (cond->type() == Item::COND_ITEM)
2482
9573
  {
2483
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2484
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
9574
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9575
      Item_func::COND_AND_FUNC;
 
9576
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2485
9577
    Item *item;
2486
 
    list<COND_CMP> save;
 
9578
    I_List<COND_CMP> save;
2487
9579
    while ((item=li++))
2488
9580
    {
2489
 
      propagate_cond_constants(session, save, and_level ? cond : item, item);
 
9581
      propagate_cond_constants(thd, &save,and_level ? cond : item, item);
2490
9582
    }
2491
9583
    if (and_level)
2492
 
    {
2493
 
      // Handle other found items
2494
 
      for (list<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
 
9584
    {                                           // Handle other found items
 
9585
      I_List_iterator<COND_CMP> cond_itr(save);
 
9586
      COND_CMP *cond_cmp;
 
9587
      while ((cond_cmp=cond_itr++))
2495
9588
      {
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
 
        }
 
9589
        Item **args= cond_cmp->cmp_func->arguments();
 
9590
        if (!args[0]->const_item())
 
9591
          change_cond_ref_to_const(thd, &save,cond_cmp->and_level,
 
9592
                                   cond_cmp->and_level, args[0], args[1]);
2502
9593
      }
2503
9594
    }
2504
9595
  }
2505
9596
  else if (and_father != cond && !cond->marker)         // In a AND group
2506
9597
  {
2507
9598
    if (cond->type() == Item::FUNC_ITEM &&
2508
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2509
 
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
9599
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
9600
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2510
9601
    {
2511
9602
      Item_func_eq *func=(Item_func_eq*) cond;
2512
9603
      Item **args= func->arguments();
2515
9606
      if (!(left_const && right_const) &&
2516
9607
          args[0]->result_type() == args[1]->result_type())
2517
9608
      {
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
 
}
 
9609
        if (right_const)
 
9610
        {
 
9611
          resolve_const_item(thd, &args[1], args[0]);
 
9612
          func->update_used_tables();
 
9613
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9614
                                   args[0], args[1]);
 
9615
        }
 
9616
        else if (left_const)
 
9617
        {
 
9618
          resolve_const_item(thd, &args[0], args[1]);
 
9619
          func->update_used_tables();
 
9620
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9621
                                   args[1], args[0]);
 
9622
        }
 
9623
      }
 
9624
    }
 
9625
  }
 
9626
}
 
9627
 
 
9628
 
 
9629
/**
 
9630
  Simplify joins replacing outer joins by inner joins whenever it's
 
9631
  possible.
 
9632
 
 
9633
    The function, during a retrieval of join_list,  eliminates those
 
9634
    outer joins that can be converted into inner join, possibly nested.
 
9635
    It also moves the on expressions for the converted outer joins
 
9636
    and from inner joins to conds.
 
9637
    The function also calculates some attributes for nested joins:
 
9638
    - used_tables    
 
9639
    - not_null_tables
 
9640
    - dep_tables.
 
9641
    - on_expr_dep_tables
 
9642
    The first two attributes are used to test whether an outer join can
 
9643
    be substituted for an inner join. The third attribute represents the
 
9644
    relation 'to be dependent on' for tables. If table t2 is dependent
 
9645
    on table t1, then in any evaluated execution plan table access to
 
9646
    table t2 must precede access to table t2. This relation is used also
 
9647
    to check whether the query contains  invalid cross-references.
 
9648
    The forth attribute is an auxiliary one and is used to calculate
 
9649
    dep_tables.
 
9650
    As the attribute dep_tables qualifies possibles orders of tables in the
 
9651
    execution plan, the dependencies required by the straight join
 
9652
    modifiers are reflected in this attribute as well.
 
9653
    The function also removes all braces that can be removed from the join
 
9654
    expression without changing its meaning.
 
9655
 
 
9656
  @note
 
9657
    An outer join can be replaced by an inner join if the where condition
 
9658
    or the on expression for an embedding nested join contains a conjunctive
 
9659
    predicate rejecting null values for some attribute of the inner tables.
 
9660
 
 
9661
    E.g. in the query:    
 
9662
    @code
 
9663
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9664
    @endcode
 
9665
    the predicate t2.b < 5 rejects nulls.
 
9666
    The query is converted first to:
 
9667
    @code
 
9668
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9669
    @endcode
 
9670
    then to the equivalent form:
 
9671
    @code
 
9672
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
 
9673
    @endcode
 
9674
 
 
9675
 
 
9676
    Similarly the following query:
 
9677
    @code
 
9678
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
 
9679
        WHERE t2.c < 5  
 
9680
    @endcode
 
9681
    is converted to:
 
9682
    @code
 
9683
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b 
 
9684
 
 
9685
    @endcode
 
9686
 
 
9687
    One conversion might trigger another:
 
9688
    @code
 
9689
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
 
9690
                       LEFT JOIN t3 ON t3.b=t2.b
 
9691
        WHERE t3 IS NOT NULL =>
 
9692
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
 
9693
        WHERE t3 IS NOT NULL AND t3.b=t2.b => 
 
9694
      SELECT * FROM t1, t2, t3
 
9695
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
 
9696
  @endcode
 
9697
 
 
9698
    The function removes all unnecessary braces from the expression
 
9699
    produced by the conversions.
 
9700
    E.g.
 
9701
    @code
 
9702
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9703
    @endcode
 
9704
    finally is converted to: 
 
9705
    @code
 
9706
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9707
 
 
9708
    @endcode
 
9709
 
 
9710
 
 
9711
    It also will remove braces from the following queries:
 
9712
    @code
 
9713
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
 
9714
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
 
9715
    @endcode
 
9716
 
 
9717
    The benefit of this simplification procedure is that it might return 
 
9718
    a query for which the optimizer can evaluate execution plan with more
 
9719
    join orders. With a left join operation the optimizer does not
 
9720
    consider any plan where one of the inner tables is before some of outer
 
9721
    tables.
 
9722
 
 
9723
  IMPLEMENTATION
 
9724
    The function is implemented by a recursive procedure.  On the recursive
 
9725
    ascent all attributes are calculated, all outer joins that can be
 
9726
    converted are replaced and then all unnecessary braces are removed.
 
9727
    As join list contains join tables in the reverse order sequential
 
9728
    elimination of outer joins does not require extra recursive calls.
 
9729
 
 
9730
  SEMI-JOIN NOTES
 
9731
    Remove all semi-joins that have are within another semi-join (i.e. have
 
9732
    an "ancestor" semi-join nest)
 
9733
 
 
9734
  EXAMPLES
 
9735
    Here is an example of a join query with invalid cross references:
 
9736
    @code
 
9737
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b 
 
9738
    @endcode
 
9739
 
 
9740
  @param join        reference to the query info
 
9741
  @param join_list   list representation of the join to be converted
 
9742
  @param conds       conditions to add on expressions for converted joins
 
9743
  @param top         true <=> conds is the where condition
 
9744
 
 
9745
  @return
 
9746
    - The new condition, if success
 
9747
    - 0, otherwise
 
9748
*/
 
9749
 
 
9750
static COND *
 
9751
simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top,
 
9752
               bool in_sj)
 
9753
{
 
9754
  TableList *table;
 
9755
  nested_join_st *nested_join;
 
9756
  TableList *prev_table= 0;
 
9757
  List_iterator<TableList> li(*join_list);
 
9758
 
 
9759
  /* 
 
9760
    Try to simplify join operations from join_list.
 
9761
    The most outer join operation is checked for conversion first. 
 
9762
  */
 
9763
  while ((table= li++))
 
9764
  {
 
9765
    table_map used_tables;
 
9766
    table_map not_null_tables= (table_map) 0;
 
9767
 
 
9768
    if ((nested_join= table->nested_join))
 
9769
    {
 
9770
      /* 
 
9771
         If the element of join_list is a nested join apply
 
9772
         the procedure to its nested join list first.
 
9773
      */
 
9774
      if (table->on_expr)
 
9775
      {
 
9776
        Item *expr= table->on_expr;
 
9777
        /* 
 
9778
           If an on expression E is attached to the table, 
 
9779
           check all null rejected predicates in this expression.
 
9780
           If such a predicate over an attribute belonging to
 
9781
           an inner table of an embedded outer join is found,
 
9782
           the outer join is converted to an inner join and
 
9783
           the corresponding on expression is added to E. 
 
9784
        */ 
 
9785
        expr= simplify_joins(join, &nested_join->join_list,
 
9786
                             expr, false, in_sj || table->sj_on_expr);
 
9787
 
 
9788
        if (!table->prep_on_expr || expr != table->on_expr)
 
9789
        {
 
9790
          assert(expr);
 
9791
 
 
9792
          table->on_expr= expr;
 
9793
          table->prep_on_expr= expr->copy_andor_structure(join->thd);
 
9794
        }
 
9795
      }
 
9796
      nested_join->used_tables= (table_map) 0;
 
9797
      nested_join->not_null_tables=(table_map) 0;
 
9798
      conds= simplify_joins(join, &nested_join->join_list, conds, top, 
 
9799
                            in_sj || table->sj_on_expr);
 
9800
      used_tables= nested_join->used_tables;
 
9801
      not_null_tables= nested_join->not_null_tables;  
 
9802
    }
 
9803
    else
 
9804
    {
 
9805
      if (!table->prep_on_expr)
 
9806
        table->prep_on_expr= table->on_expr;
 
9807
      used_tables= table->table->map;
 
9808
      if (conds)
 
9809
        not_null_tables= conds->not_null_tables();
 
9810
    }
 
9811
      
 
9812
    if (table->embedding)
 
9813
    {
 
9814
      table->embedding->nested_join->used_tables|= used_tables;
 
9815
      table->embedding->nested_join->not_null_tables|= not_null_tables;
 
9816
    }
 
9817
 
 
9818
    if (!table->outer_join || (used_tables & not_null_tables))
 
9819
    {
 
9820
      /* 
 
9821
        For some of the inner tables there are conjunctive predicates
 
9822
        that reject nulls => the outer join can be replaced by an inner join.
 
9823
      */
 
9824
      table->outer_join= 0;
 
9825
      if (table->on_expr)
 
9826
      {
 
9827
        /* Add ON expression to the WHERE or upper-level ON condition. */
 
9828
        if (conds)
 
9829
        {
 
9830
          conds= and_conds(conds, table->on_expr);
 
9831
          conds->top_level_item();
 
9832
          /* conds is always a new item as both cond and on_expr existed */
 
9833
          assert(!conds->fixed);
 
9834
          conds->fix_fields(join->thd, &conds);
 
9835
        }
 
9836
        else
 
9837
          conds= table->on_expr; 
 
9838
        table->prep_on_expr= table->on_expr= 0;
 
9839
      }
 
9840
    }
 
9841
    
 
9842
    if (!top)
 
9843
      continue;
 
9844
 
 
9845
    /* 
 
9846
      Only inner tables of non-convertible outer joins
 
9847
      remain with on_expr.
 
9848
    */ 
 
9849
    if (table->on_expr)
 
9850
    {
 
9851
      table->dep_tables|= table->on_expr->used_tables(); 
 
9852
      if (table->embedding)
 
9853
      {
 
9854
        table->dep_tables&= ~table->embedding->nested_join->used_tables;   
 
9855
        /*
 
9856
           Embedding table depends on tables used
 
9857
           in embedded on expressions. 
 
9858
        */
 
9859
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
 
9860
      }
 
9861
      else
 
9862
        table->dep_tables&= ~table->table->map;
 
9863
    }
 
9864
 
 
9865
    if (prev_table)
 
9866
    {
 
9867
      /* The order of tables is reverse: prev_table follows table */
 
9868
      if (prev_table->straight)
 
9869
        prev_table->dep_tables|= used_tables;
 
9870
      if (prev_table->on_expr)
 
9871
      {
 
9872
        prev_table->dep_tables|= table->on_expr_dep_tables;
 
9873
        table_map prev_used_tables= prev_table->nested_join ?
 
9874
                                    prev_table->nested_join->used_tables :
 
9875
                                    prev_table->table->map;
 
9876
        /* 
 
9877
          If on expression contains only references to inner tables
 
9878
          we still make the inner tables dependent on the outer tables.
 
9879
          It would be enough to set dependency only on one outer table
 
9880
          for them. Yet this is really a rare case.
 
9881
        */  
 
9882
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
 
9883
          prev_table->dep_tables|= used_tables;
 
9884
      }
 
9885
    }
 
9886
    prev_table= table;
 
9887
  }
 
9888
    
 
9889
  /* 
 
9890
    Flatten nested joins that can be flattened.
 
9891
    no ON expression and not a semi-join => can be flattened.
 
9892
  */
 
9893
  li.rewind();
 
9894
  while ((table= li++))
 
9895
  {
 
9896
    nested_join= table->nested_join;
 
9897
    if (table->sj_on_expr && !in_sj)
 
9898
    {
 
9899
       /*
 
9900
         If this is a semi-join that is not contained within another semi-join, 
 
9901
         leave it intact (otherwise it is flattened)
 
9902
       */
 
9903
      join->select_lex->sj_nests.push_back(table);
 
9904
    }
 
9905
    else if (nested_join && !table->on_expr)
 
9906
    {
 
9907
      TableList *tbl;
 
9908
      List_iterator<TableList> it(nested_join->join_list);
 
9909
      while ((tbl= it++))
 
9910
      {
 
9911
        tbl->embedding= table->embedding;
 
9912
        tbl->join_list= table->join_list;
 
9913
      }      
 
9914
      li.replace(nested_join->join_list);
 
9915
    }
 
9916
  }
 
9917
  return(conds); 
 
9918
}
 
9919
 
 
9920
 
 
9921
/**
 
9922
  Assign each nested join structure a bit in nested_join_map.
 
9923
 
 
9924
    Assign each nested join structure (except "confluent" ones - those that
 
9925
    embed only one element) a bit in nested_join_map.
 
9926
 
 
9927
  @param join          Join being processed
 
9928
  @param join_list     List of tables
 
9929
  @param first_unused  Number of first unused bit in nested_join_map before the
 
9930
                       call
 
9931
 
 
9932
  @note
 
9933
    This function is called after simplify_joins(), when there are no
 
9934
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
 
9935
    we will not run out of bits in nested_join_map.
 
9936
 
 
9937
  @return
 
9938
    First unused bit in nested_join_map after the call.
 
9939
*/
 
9940
 
 
9941
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, 
 
9942
                                          uint32_t first_unused)
 
9943
{
 
9944
  List_iterator<TableList> li(*join_list);
 
9945
  TableList *table;
 
9946
  while ((table= li++))
 
9947
  {
 
9948
    nested_join_st *nested_join;
 
9949
    if ((nested_join= table->nested_join))
 
9950
    {
 
9951
      /*
 
9952
        It is guaranteed by simplify_joins() function that a nested join
 
9953
        that has only one child is either
 
9954
         - a single-table view (the child is the underlying table), or 
 
9955
         - a single-table semi-join nest
 
9956
 
 
9957
        We don't assign bits to such sj-nests because 
 
9958
        1. it is redundant (a "sequence" of one table cannot be interleaved 
 
9959
            with anything)
 
9960
        2. we could run out bits in nested_join_map otherwise.
 
9961
      */
 
9962
      if (nested_join->join_list.elements != 1)
 
9963
      {
 
9964
        /* Don't assign bits to sj-nests */
 
9965
        if (table->on_expr)
 
9966
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
 
9967
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
 
9968
                                                    first_unused);
 
9969
      }
 
9970
    }
 
9971
  }
 
9972
  return(first_unused);
 
9973
}
 
9974
 
 
9975
 
 
9976
/**
 
9977
  Set nested_join_st::counter=0 in all nested joins in passed list.
 
9978
 
 
9979
    Recursively set nested_join_st::counter=0 for all nested joins contained in
 
9980
    the passed join_list.
 
9981
 
 
9982
  @param join_list  List of nested joins to process. It may also contain base
 
9983
                    tables which will be ignored.
 
9984
*/
 
9985
 
 
9986
static void reset_nj_counters(List<TableList> *join_list)
 
9987
{
 
9988
  List_iterator<TableList> li(*join_list);
 
9989
  TableList *table;
 
9990
  while ((table= li++))
 
9991
  {
 
9992
    nested_join_st *nested_join;
 
9993
    if ((nested_join= table->nested_join))
 
9994
    {
 
9995
      nested_join->counter_= 0;
 
9996
      reset_nj_counters(&nested_join->join_list);
 
9997
    }
 
9998
  }
 
9999
  return;
 
10000
}
 
10001
 
2536
10002
 
2537
10003
/**
2538
10004
  Check interleaving with an inner tables of an outer join for
2539
10005
  extension table.
2540
10006
 
2541
 
    Check if table next_tab can be added to current partial join order, and
 
10007
    Check if table next_tab can be added to current partial join order, and 
2542
10008
    if yes, record that it has been added.
2543
10009
 
2544
10010
    The function assumes that both current partial join order and its
2545
10011
    extension with next_tab are valid wrt table dependencies.
2546
10012
 
2547
10013
  @verbatim
2548
 
     IMPLEMENTATION
 
10014
     IMPLEMENTATION 
2549
10015
       LIMITATIONS ON JOIN order_st
2550
10016
         The nested [outer] joins executioner algorithm imposes these limitations
2551
10017
         on join order:
2552
 
         1. "Outer tables first" -  any "outer" table must be before any
 
10018
         1. "Outer tables first" -  any "outer" table must be before any 
2553
10019
             corresponding "inner" table.
2554
10020
         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
 
10021
            sequence in join order (i.e. the sequence must not be interrupted by 
2556
10022
            tables that are outside of this nested join).
2557
10023
 
2558
10024
         #1 is checked elsewhere, this function checks #2 provided that #1 has
2559
10025
         been already checked.
2560
10026
 
2561
10027
       WHY NEED NON-INTERLEAVING
2562
 
         Consider an example:
 
10028
         Consider an example: 
2563
10029
 
2564
10030
           select * from t0 join t1 left join (t2 join t3) on cond1
2565
10031
 
2583
10049
         The limitations on join order can be rephrased as follows: for valid
2584
10050
         join order one must be able to:
2585
10051
           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
 
10052
           2. for each nested join, put one '(' and one ')' on the said line        
2587
10053
           3. write "LEFT JOIN" and "ON (...)" where appropriate
2588
10054
           4. get a query equivalent to the query we're trying to execute.
2589
10055
 
2590
10056
         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
 
10057
         above described line from left to right. 
 
10058
         A single check_interleaving_with_nj(A,B) call is equivalent to writing 
2593
10059
         table B and appropriate brackets on condition that table A and
2594
10060
         appropriate brackets is the last what was written. Graphically the
2595
10061
         transition is as follows:
2602
10068
                                                      position.
2603
10069
 
2604
10070
         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
 
10071
           The caller guarantees that there is no more then one X-bracket by 
 
10072
           checking "!(remaining_tables & s->dependent)" before calling this 
2607
10073
           function. X-bracket may have a pair in Y-bracket.
2608
10074
 
2609
10075
         When "writing" we store/update this auxilary info about the current
2610
10076
         position:
2611
10077
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
2612
10078
             joins) we've opened but didn't close.
2613
 
          2. {each NestedJoin class not simplified away}->counter - number
 
10079
          2. {each nested_join_st structure not simplified away}->counter - number
2614
10080
             of this nested join's children that have already been added to to
2615
10081
             the partial join order.
2616
10082
  @endverbatim
2617
10083
 
2618
10084
  @param join       Join being processed
 
10085
  @param last_tab   Last table in current partial join order (this function is
 
10086
                    not called for empty partial join orders)
2619
10087
  @param next_tab   Table we're going to extend the current partial join with
2620
10088
 
2621
10089
  @retval
2624
10092
  @retval
2625
10093
    true   Requested join order extension not allowed.
2626
10094
*/
2627
 
bool check_interleaving_with_nj(JoinTable *next_tab)
 
10095
 
 
10096
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
2628
10097
{
2629
 
  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
2630
 
  Join *join= next_tab->join;
 
10098
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
 
10099
  JOIN *join= last_tab->join;
2631
10100
 
2632
 
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
 
10101
  if (join->cur_embedding_map & ~next_tab->embedding_map)
2633
10102
  {
2634
 
    /*
 
10103
    /* 
2635
10104
      next_tab is outside of the "pair of brackets" we're currently in.
2636
10105
      Cannot add it.
2637
10106
    */
2638
10107
    return true;
2639
10108
  }
2640
 
 
 
10109
   
2641
10110
  /*
2642
10111
    Do update counters for "pairs of brackets" that we've left (marked as
2643
10112
    X,Y,Z in the above picture)
2644
10113
  */
2645
 
  for (;next_emb; next_emb= next_emb->getEmbedding())
 
10114
  for (;next_emb; next_emb= next_emb->embedding)
2646
10115
  {
2647
 
    next_emb->getNestedJoin()->counter_++;
2648
 
    if (next_emb->getNestedJoin()->counter_ == 1)
 
10116
    next_emb->nested_join->counter_++;
 
10117
    if (next_emb->nested_join->counter_ == 1)
2649
10118
    {
2650
 
      /*
 
10119
      /* 
2651
10120
        next_emb is the first table inside a nested join we've "entered". In
2652
10121
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
2653
10122
        X bracket might have Y pair bracket.
2654
10123
      */
2655
 
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
 
10124
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
2656
10125
    }
2657
 
 
2658
 
    if (next_emb->getNestedJoin()->join_list.elements !=
2659
 
        next_emb->getNestedJoin()->counter_)
 
10126
    
 
10127
    if (next_emb->nested_join->join_list.elements !=
 
10128
        next_emb->nested_join->counter_)
2660
10129
      break;
2661
10130
 
2662
10131
    /*
2663
10132
      We're currently at Y or Z-bracket as depicted in the above picture.
2664
10133
      Mark that we've left it and continue walking up the brackets hierarchy.
2665
10134
    */
2666
 
    join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
 
10135
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
2667
10136
  }
2668
10137
  return false;
2669
10138
}
2670
10139
 
2671
 
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
2672
 
{
2673
 
  Session *session= join->session;
 
10140
 
 
10141
/**
 
10142
  Nested joins perspective: Remove the last table from the join order.
 
10143
 
 
10144
    Remove the last table from the partial join order and update the nested
 
10145
    joins counters and join->cur_embedding_map. It is ok to call this 
 
10146
    function for the first table in join order (for which 
 
10147
    check_interleaving_with_nj has not been called)
 
10148
 
 
10149
  @param last  join table to remove, it is assumed to be the last in current
 
10150
               partial join order.
 
10151
*/
 
10152
 
 
10153
static void restore_prev_nj_state(JOIN_TAB *last)
 
10154
{
 
10155
  TableList *last_emb= last->table->pos_in_table_list->embedding;
 
10156
  JOIN *join= last->join;
 
10157
  while (last_emb)
 
10158
  {
 
10159
    if (last_emb->on_expr)
 
10160
    {
 
10161
      if (!(--last_emb->nested_join->counter_))
 
10162
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
 
10163
      else if (last_emb->nested_join->join_list.elements-1 ==
 
10164
               last_emb->nested_join->counter_) 
 
10165
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
 
10166
      else
 
10167
        break;
 
10168
    }
 
10169
    last_emb= last_emb->embedding;
 
10170
  }
 
10171
}
 
10172
 
 
10173
 
 
10174
 
 
10175
static 
 
10176
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
 
10177
{
 
10178
  TableList *emb_sj_nest;
 
10179
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10180
  {
 
10181
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
 
10182
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
 
10183
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
 
10184
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10185
  }
 
10186
}
 
10187
 
 
10188
 
 
10189
/*
 
10190
  we assume remaining_tables doesnt contain @tab.
 
10191
*/
 
10192
 
 
10193
static void restore_prev_sj_state(const table_map remaining_tables, 
 
10194
                                  const JOIN_TAB *tab)
 
10195
{
 
10196
  TableList *emb_sj_nest;
 
10197
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10198
  {
 
10199
    /* If we're removing the last SJ-inner table, remove the sj-nest */
 
10200
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) == 
 
10201
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
 
10202
    {
 
10203
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10204
    }
 
10205
  }
 
10206
}
 
10207
 
 
10208
 
 
10209
static COND *
 
10210
optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list,
 
10211
              Item::cond_result *cond_value)
 
10212
{
 
10213
  THD *thd= join->thd;
2674
10214
 
2675
10215
  if (!conds)
2676
10216
    *cond_value= Item::COND_TRUE;
2677
10217
  else
2678
10218
  {
2679
 
    /*
 
10219
    /* 
2680
10220
      Build all multiple equality predicates and eliminate equality
2681
10221
      predicates that can be inferred from these multiple equalities.
2682
10222
      For each reference of a field included into a multiple equality
2683
10223
      that occurs in a function set a pointer to the multiple equality
2684
10224
      predicate. Substitute a constant instead of this field if the
2685
10225
      multiple equality contains a constant.
2686
 
    */
2687
 
    conds= build_equal_items(join->session, conds, NULL, join_list,
 
10226
    */ 
 
10227
    conds= build_equal_items(join->thd, conds, NULL, join_list,
2688
10228
                             &join->cond_equal);
2689
10229
 
2690
10230
    /* change field = field to field = const for each found field = const */
2691
 
    list<COND_CMP> temp;
2692
 
    propagate_cond_constants(session, temp, conds, conds);
 
10231
    propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
2693
10232
    /*
2694
10233
      Remove all instances of item == item
2695
10234
      Remove all and-levels where CONST item != CONST item
2696
10235
    */
2697
 
    conds= remove_eq_conds(session, conds, cond_value) ;
 
10236
    conds= remove_eq_conds(thd, conds, cond_value) ;
2698
10237
  }
2699
10238
  return(conds);
2700
10239
}
2701
10240
 
 
10241
 
2702
10242
/**
2703
10243
  Remove const and eq items.
2704
10244
 
2709
10249
    - COND_TRUE   : always true ( 1 = 1 )
2710
10250
    - COND_FALSE  : always false        ( 1 = 2 )
2711
10251
*/
2712
 
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
10252
 
 
10253
COND *
 
10254
remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value)
2713
10255
{
2714
10256
  if (cond->type() == Item::COND_ITEM)
2715
10257
  {
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());
 
10258
    bool and_level= ((Item_cond*) cond)->functype()
 
10259
      == Item_func::COND_AND_FUNC;
 
10260
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2719
10261
    Item::cond_result tmp_cond_value;
2720
 
    bool should_fix_fields= false;
 
10262
    bool should_fix_fields=0;
2721
10263
 
2722
 
    *cond_value= Item::COND_UNDEF;
 
10264
    *cond_value=Item::COND_UNDEF;
2723
10265
    Item *item;
2724
 
    while ((item= li++))
 
10266
    while ((item=li++))
2725
10267
    {
2726
 
      Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
2727
 
      if (! new_item)
2728
 
              li.remove();
 
10268
      Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value);
 
10269
      if (!new_item)
 
10270
        li.remove();
2729
10271
      else if (item != new_item)
2730
10272
      {
2731
 
        li.replace(new_item);
2732
 
        should_fix_fields= true;
 
10273
        li.replace(new_item);
 
10274
        should_fix_fields=1;
2733
10275
      }
2734
10276
      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;
 
10277
        *cond_value=tmp_cond_value;
 
10278
      switch (tmp_cond_value) {
 
10279
      case Item::COND_OK:                       // Not true or false
 
10280
        if (and_level || *cond_value == Item::COND_FALSE)
 
10281
          *cond_value=tmp_cond_value;
 
10282
        break;
 
10283
      case Item::COND_FALSE:
 
10284
        if (and_level)
 
10285
        {
 
10286
          *cond_value=tmp_cond_value;
 
10287
          return (COND*) 0;                     // Always false
 
10288
        }
 
10289
        break;
 
10290
      case Item::COND_TRUE:
 
10291
        if (!and_level)
 
10292
        {
 
10293
          *cond_value= tmp_cond_value;
 
10294
          return (COND*) 0;                     // Always true
 
10295
        }
 
10296
        break;
 
10297
      case Item::COND_UNDEF:                    // Impossible
 
10298
        break; /* purecov: deadcode */
2759
10299
      }
2760
10300
    }
2761
 
 
2762
10301
    if (should_fix_fields)
2763
10302
      cond->update_used_tables();
2764
10303
 
2765
 
    if (! ((Item_cond*) cond)->argument_list()->elements || *cond_value != Item::COND_OK)
2766
 
      return (COND*) NULL;
2767
 
 
 
10304
    if (!((Item_cond*) cond)->argument_list()->elements ||
 
10305
        *cond_value != Item::COND_OK)
 
10306
      return (COND*) 0;
2768
10307
    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 */
 
10308
    {                                           // Remove list
2771
10309
      item= ((Item_cond*) cond)->argument_list()->head();
2772
 
      ((Item_cond*) cond)->argument_list()->clear();
 
10310
      ((Item_cond*) cond)->argument_list()->empty();
2773
10311
      return item;
2774
10312
    }
2775
10313
  }
2776
 
  else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
 
10314
  else if (cond->type() == Item::FUNC_ITEM &&
 
10315
           ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
2777
10316
  {
2778
10317
    /*
2779
10318
      Handles this special case for some ODBC applications:
2785
10324
      SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
2786
10325
    */
2787
10326
 
2788
 
    Item_func_isnull *func= (Item_func_isnull*) cond;
 
10327
    Item_func_isnull *func=(Item_func_isnull*) cond;
2789
10328
    Item **args= func->arguments();
2790
10329
    if (args[0]->type() == Item::FIELD_ITEM)
2791
10330
    {
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
 
          )
 
10331
      Field *field=((Item_field*) args[0])->field;
 
10332
      if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
 
10333
          (thd->options & OPTION_AUTO_IS_NULL) &&
 
10334
          (thd->first_successful_insert_id_in_prev_stmt > 0 &&
 
10335
           thd->substitute_null_with_insert_id))
2801
10336
      {
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;
 
10337
        COND *new_cond;
 
10338
        if ((new_cond= new Item_func_eq(args[0],
 
10339
                                        new Item_int("last_insert_id()",
 
10340
                                                     thd->read_first_successful_insert_id_in_prev_stmt(),
 
10341
                                                     MY_INT64_NUM_DECIMAL_DIGITS))))
 
10342
        {
 
10343
          cond=new_cond;
2808
10344
          /*
2809
10345
            Item_func_eq can't be fixed after creation so we do not check
2810
10346
            cond->fixed, also it do not need tables so we use 0 as second
2811
10347
            argument.
2812
10348
          */
2813
 
          cond->fix_fields(session, &cond);
2814
 
        }
 
10349
          cond->fix_fields(thd, &cond);
 
10350
        }
2815
10351
        /*
2816
10352
          IS NULL should be mapped to LAST_INSERT_ID only for first row, so
2817
10353
          clear for next row
2818
10354
        */
2819
 
        session->substitute_null_with_insert_id= false;
 
10355
        thd->substitute_null_with_insert_id= false;
2820
10356
      }
2821
 
#ifdef NOTDEFINED
2822
10357
      /* 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)
 
10358
      else if (((field->type() == DRIZZLE_TYPE_NEWDATE) ||
 
10359
                (field->type() == DRIZZLE_TYPE_DATETIME)) &&
 
10360
                (field->flags & NOT_NULL_FLAG) &&
 
10361
               !field->table->maybe_null)
2827
10362
      {
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;
 
10363
        COND *new_cond;
 
10364
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
 
10365
        {
 
10366
          cond=new_cond;
2832
10367
          /*
2833
10368
            Item_func_eq can't be fixed after creation so we do not check
2834
10369
            cond->fixed, also it do not need tables so we use 0 as second
2835
10370
            argument.
2836
10371
          */
2837
 
          cond->fix_fields(session, &cond);
2838
 
        }
 
10372
          cond->fix_fields(thd, &cond);
 
10373
        }
2839
10374
      }
2840
 
#endif /* NOTDEFINED */
2841
10375
    }
2842
10376
    if (cond->const_item())
2843
10377
    {
2844
10378
      *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2845
 
      return (COND *) NULL;
 
10379
      return (COND*) 0;
2846
10380
    }
2847
10381
  }
2848
10382
  else if (cond->const_item() && !cond->is_expensive())
2849
10383
  /*
2850
 
    @todo
 
10384
    TODO:
2851
10385
    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
 
 
 
10386
    materialized IN because it is created later than this phase, and cannot be
 
10387
    evaluated at this point.
 
10388
    The condition should be something as (need to fix member access):
 
10389
      !(cond->type() == Item::FUNC_ITEM &&
 
10390
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
 
10391
        ((Item_in_optimizer*)cond)->is_expensive()))
2858
10392
  */
2859
10393
  {
2860
10394
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2861
 
    return (COND *) NULL;
 
10395
    return (COND*) 0;
2862
10396
  }
2863
10397
  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
2864
 
  {                                             
2865
 
    /* boolan compare function */
 
10398
  {                                             // boolan compare function
2866
10399
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2867
10400
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2868
10401
    if (left_item->eq(right_item,1))
2869
10402
    {
2870
 
      if (!left_item->maybe_null || ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
2871
 
              return (COND*) NULL;                      /* Comparison of identical items */
 
10403
      if (!left_item->maybe_null ||
 
10404
          ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
 
10405
        return (COND*) 0;                       // Compare of identical items
2872
10406
    }
2873
10407
  }
2874
 
  *cond_value= Item::COND_OK;
2875
 
  return cond;                                  /* Point at next and return into recursion */
 
10408
  *cond_value=Item::COND_OK;
 
10409
  return cond;                                  // Point at next and level
2876
10410
}
2877
10411
 
2878
 
/*
 
10412
/* 
2879
10413
  Check if equality can be used in removing components of GROUP BY/DISTINCT
2880
 
 
 
10414
  
2881
10415
  SYNOPSIS
2882
10416
    test_if_equality_guarantees_uniqueness()
2883
10417
      l          the left comparison argument (a field if any)
2884
10418
      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
 
10419
  
 
10420
  DESCRIPTION    
 
10421
    Checks if an equality predicate can be used to take away 
 
10422
    DISTINCT/GROUP BY because it is known to be true for exactly one 
2889
10423
    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
 
10424
    Arguments must be of the same type because e.g. 
 
10425
    <string_field> = <int_const> may match more than 1 distinct value from 
 
10426
    the column. 
 
10427
    We must take into consideration and the optimization done for various 
2894
10428
    string constants when compared to dates etc (see Item_int_with_ref) as
2895
10429
    well as the collation of the arguments.
2896
 
 
2897
 
  RETURN VALUE
 
10430
  
 
10431
  RETURN VALUE  
2898
10432
    true    can be used
2899
10433
    false   cannot be used
2900
10434
*/
2901
 
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
10435
static bool
 
10436
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
2902
10437
{
2903
10438
  return r->const_item() &&
2904
10439
    /* elements must be compared as dates */
2913
10448
/**
2914
10449
  Return true if the item is a const value in all the WHERE clause.
2915
10450
*/
2916
 
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
10451
 
 
10452
static bool
 
10453
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
2917
10454
{
2918
10455
  if (cond->type() == Item::COND_ITEM)
2919
10456
  {
2920
10457
    bool and_level= (((Item_cond*) cond)->functype()
2921
10458
                     == Item_func::COND_AND_FUNC);
2922
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
10459
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2923
10460
    Item *item;
2924
10461
    while ((item=li++))
2925
10462
    {
2926
10463
      bool res=const_expression_in_where(item, comp_item, const_item);
2927
10464
      if (res)                                  // Is a const value
2928
10465
      {
2929
 
        if (and_level)
2930
 
          return 1;
 
10466
        if (and_level)
 
10467
          return 1;
2931
10468
      }
2932
10469
      else if (!and_level)
2933
 
        return 0;
 
10470
        return 0;
2934
10471
    }
2935
10472
    return and_level ? 0 : 1;
2936
10473
  }
2938
10475
  {                                             // boolan compare function
2939
10476
    Item_func* func= (Item_func*) cond;
2940
10477
    if (func->functype() != Item_func::EQUAL_FUNC &&
2941
 
              func->functype() != Item_func::EQ_FUNC)
 
10478
        func->functype() != Item_func::EQ_FUNC)
2942
10479
      return 0;
2943
10480
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2944
10481
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2946
10483
    {
2947
10484
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
2948
10485
      {
2949
 
        if (*const_item)
2950
 
          return right_item->eq(*const_item, 1);
2951
 
        *const_item=right_item;
2952
 
        return 1;
 
10486
        if (*const_item)
 
10487
          return right_item->eq(*const_item, 1);
 
10488
        *const_item=right_item;
 
10489
        return 1;
2953
10490
      }
2954
10491
    }
2955
10492
    else if (right_item->eq(comp_item,1))
2956
10493
    {
2957
10494
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
2958
10495
      {
2959
 
        if (*const_item)
2960
 
          return left_item->eq(*const_item, 1);
2961
 
        *const_item=left_item;
2962
 
        return 1;
 
10496
        if (*const_item)
 
10497
          return left_item->eq(*const_item, 1);
 
10498
        *const_item=left_item;
 
10499
        return 1;
2963
10500
      }
2964
10501
    }
2965
10502
  }
2966
10503
  return 0;
2967
10504
}
2968
10505
 
 
10506
 
2969
10507
/**
2970
10508
  @details
2971
10509
  Rows produced by a join sweep may end up in a temporary table or be sent
2977
10515
  @return
2978
10516
    end_select function to use. This function can't fail.
2979
10517
*/
2980
 
Next_select_func setup_end_select_func(Join *join)
 
10518
 
 
10519
Next_select_func setup_end_select_func(JOIN *join)
2981
10520
{
2982
10521
  Table *table= join->tmp_table;
2983
 
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
 
10522
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
2984
10523
  Next_select_func end_select;
2985
10524
 
2986
10525
  /* Set up select_end */
2987
10526
  if (table)
2988
10527
  {
2989
 
    if (table->group && tmp_tbl->sum_func_count &&
 
10528
    if (table->group && tmp_tbl->sum_func_count && 
2990
10529
        !tmp_tbl->precomputed_group_by)
2991
10530
    {
2992
 
      if (table->getShare()->sizeKeys())
 
10531
      if (table->s->keys)
2993
10532
      {
2994
 
        end_select= end_update;
 
10533
        end_select=end_update;
2995
10534
      }
2996
10535
      else
2997
10536
      {
2998
 
        end_select= end_unique_update;
 
10537
        end_select=end_unique_update;
2999
10538
      }
3000
10539
    }
3001
10540
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
3002
10541
    {
3003
 
      end_select= end_write_group;
 
10542
      end_select=end_write_group;
3004
10543
    }
3005
10544
    else
3006
10545
    {
3007
 
      end_select= end_write;
 
10546
      end_select=end_write;
3008
10547
      if (tmp_tbl->precomputed_group_by)
3009
10548
      {
3010
10549
        /*
3011
10550
          A preceding call to create_tmp_table in the case when loose
3012
10551
          index scan is used guarantees that
3013
 
          Tmp_Table_Param::items_to_copy has enough space for the group
 
10552
          TMP_TABLE_PARAM::items_to_copy has enough space for the group
3014
10553
          by functions. It is OK here to use memcpy since we copy
3015
10554
          Item_sum pointers into an array of Item pointers.
3016
10555
        */
3032
10571
  return end_select;
3033
10572
}
3034
10573
 
 
10574
 
3035
10575
/**
3036
10576
  Make a join of all tables and write it on socket or to table.
3037
10577
 
3042
10582
  @retval
3043
10583
    -1  if error should be sent
3044
10584
*/
3045
 
int do_select(Join *join, List<Item> *fields, Table *table)
 
10585
 
 
10586
static int
 
10587
do_select(JOIN *join,List<Item> *fields,Table *table)
3046
10588
{
3047
10589
  int rc= 0;
3048
10590
  enum_nested_loop_state error= NESTED_LOOP_OK;
3049
 
  JoinTable *join_tab= NULL;
3050
 
 
 
10591
  JOIN_TAB *join_tab= NULL;
 
10592
  
3051
10593
  join->tmp_table= table;                       /* Save for easy recursion */
3052
10594
  join->fields= fields;
3053
10595
 
3054
10596
  if (table)
3055
10597
  {
3056
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
3057
 
    table->emptyRecord();
 
10598
    table->file->extra(HA_EXTRA_WRITE_CACHE);
 
10599
    empty_record(table);
3058
10600
    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
 
    }
 
10601
        table->s->keys && !table->file->inited)
 
10602
      table->file->ha_index_init(0, 0);
3069
10603
  }
3070
10604
  /* Set up select_end */
3071
10605
  Next_select_func end_select= setup_end_select_func(join);
3086
10620
    {
3087
10621
      error= (*end_select)(join, 0, 0);
3088
10622
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
3089
 
              error= (*end_select)(join, 0, 1);
 
10623
        error= (*end_select)(join, 0, 1);
3090
10624
 
3091
10625
      /*
3092
10626
        If we don't go through evaluate_join_record(), do the counting
3094
10628
        so we don't touch it here.
3095
10629
      */
3096
10630
      join->examined_rows++;
3097
 
      join->session->row_count++;
 
10631
      join->thd->row_count++;
3098
10632
      assert(join->examined_rows <= 1);
3099
10633
    }
3100
10634
    else if (join->send_row_on_empty_set())
3124
10658
    if (!table)                                 // If sending data to client
3125
10659
    {
3126
10660
      /*
3127
 
        The following will unlock all cursors if the command wasn't an
3128
 
        update command
 
10661
        The following will unlock all cursors if the command wasn't an
 
10662
        update command
3129
10663
      */
3130
10664
      join->join_free();                        // Unlock all cursors
3131
10665
      if (join->result->send_eof())
3132
 
        rc= 1;                                  // Don't send error
 
10666
        rc= 1;                                  // Don't send error
3133
10667
    }
3134
10668
  }
3135
10669
  else
3137
10671
  if (table)
3138
10672
  {
3139
10673
    int tmp, new_errno= 0;
3140
 
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
10674
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
3141
10675
    {
3142
10676
      new_errno= tmp;
3143
10677
    }
3144
 
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
 
10678
    if ((tmp=table->file->ha_index_or_rnd_end()))
3145
10679
    {
3146
10680
      new_errno= tmp;
3147
10681
    }
3148
10682
    if (new_errno)
3149
 
      table->print_error(new_errno,MYF(0));
 
10683
      table->file->print_error(new_errno,MYF(0));
3150
10684
  }
3151
 
  return(join->session->is_error() ? -1 : rc);
 
10685
  return(join->thd->is_error() ? -1 : rc);
3152
10686
}
3153
10687
 
3154
 
enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records)
 
10688
 
 
10689
enum_nested_loop_state
 
10690
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3155
10691
{
3156
10692
  enum_nested_loop_state rc;
3157
10693
 
3162
10698
      rc= sub_select(join,join_tab,end_of_records);
3163
10699
    return rc;
3164
10700
  }
3165
 
  if (join->session->getKilled())               // If aborted by user
 
10701
  if (join->thd->killed)                // If aborted by user
3166
10702
  {
3167
 
    join->session->send_kill_message();
3168
 
    return NESTED_LOOP_KILLED;
 
10703
    join->thd->send_kill_message();
 
10704
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
3169
10705
  }
3170
10706
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3171
10707
  {
3172
 
    if (! join_tab->cache.store_record_in_cache())
 
10708
    if (!store_record_in_cache(&join_tab->cache))
3173
10709
      return NESTED_LOOP_OK;                     // There is more room in cache
3174
10710
    return flush_cached_records(join,join_tab,false);
3175
10711
  }
3182
10718
/**
3183
10719
  Retrieve records ends with a given beginning from the result of a join.
3184
10720
 
3185
 
    For a given partial join record consisting of records from the tables
 
10721
    For a given partial join record consisting of records from the tables 
3186
10722
    preceding the table join_tab in the execution plan, the function
3187
10723
    retrieves all matching full records from the result set and
3188
 
    send them to the result set stream.
 
10724
    send them to the result set stream. 
3189
10725
 
3190
10726
  @note
3191
10727
    The function effectively implements the  final (n-k) nested loops
3225
10761
    first row with t3.a=t1.a has been encountered.
3226
10762
    Thus, the second predicate P2 is supplied with a guarded value that are
3227
10763
    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
 
10764
    (table t2). When the first row with t3.a=t1.a for the  current row 
3229
10765
    of table t1  appears, the value becomes true. For now on the predicate
3230
10766
    is evaluated immediately after the row of table t2 has been read.
3231
10767
    When the first row with t3.a=t1.a has been encountered all
3233
10769
    Only when all of them are true the row is sent to the output stream.
3234
10770
    If not, the function returns to the lowest nest level that has a false
3235
10771
    attached condition.
3236
 
    The predicates from on expressions are also pushed down. If in the
 
10772
    The predicates from on expressions are also pushed down. If in the 
3237
10773
    the above example the on expression were (t3.a=t1.a AND t2.a=t1.a),
3238
10774
    then t1.a=t2.a would be pushed down to table t2, and without any
3239
10775
    guard.
3243
10779
    is complemented by nulls  for t2 and t3. Then the pushed down predicates
3244
10780
    are checked for the composed row almost in the same way as it had
3245
10781
    been done for the first row with a match. The only difference is
3246
 
    the predicates from on expressions are not checked.
 
10782
    the predicates from on expressions are not checked. 
3247
10783
 
3248
10784
  @par
3249
10785
  @b IMPLEMENTATION
3259
10795
    and a pointer to a guarding boolean variable.
3260
10796
    When the value of the guard variable is true the value of the object
3261
10797
    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
 
10798
    true. 
 
10799
    To carry out a return to a nested loop level of join table t the pointer 
3264
10800
    to t is remembered in the field 'return_tab' of the join structure.
3265
10801
    Consider the following query:
3266
10802
    @code
3277
10813
    t5.a=t3.a is found, the pushed down predicate t4.b=2 OR t4.b IS NULL
3278
10814
    becomes 'activated', as well the predicate t4.a=t2.a. But
3279
10815
    the predicate (t2.b=5 OR t2.b IS NULL) can not be checked until
3280
 
    t4.a=t2.a becomes true.
 
10816
    t4.a=t2.a becomes true. 
3281
10817
    In order not to re-evaluate the predicates that were already evaluated
3282
10818
    as attached pushed down predicates, a pointer to the the first
3283
10819
    most inner unmatched table is maintained in join_tab->first_unmatched.
3284
10820
    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.
 
10821
    this pointer for t5 is changed from t4 to t2.             
3286
10822
 
3287
10823
    @par
3288
10824
    @b STRUCTURE @b NOTES
3293
10829
  @param join      pointer to the structure providing all context info for
3294
10830
                   the query
3295
10831
  @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
 
10832
  @param end_records  true when we need to perform final steps of retrival   
3297
10833
 
3298
10834
  @return
3299
10835
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
3300
10836
*/
3301
 
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
 
10837
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
 
10838
 
 
10839
enum_nested_loop_state
 
10840
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3302
10841
{
3303
10842
  join_tab->table->null_row=0;
3304
10843
  if (end_of_records)
3306
10845
 
3307
10846
  int error;
3308
10847
  enum_nested_loop_state rc;
3309
 
  ReadRecord *info= &join_tab->read_record;
 
10848
  READ_RECORD *info= &join_tab->read_record;
 
10849
 
 
10850
  if (join_tab->flush_weedout_table)
 
10851
  {
 
10852
    do_sj_reset(join_tab->flush_weedout_table);
 
10853
  }
3310
10854
 
3311
10855
  if (join->resume_nested_loop)
3312
10856
  {
3334
10878
      /* Set first_unmatched for the last inner table of this group */
3335
10879
      join_tab->last_inner->first_unmatched= join_tab;
3336
10880
    }
3337
 
    join->session->row_count= 0;
 
10881
    join->thd->row_count= 0;
3338
10882
 
3339
10883
    error= (*join_tab->read_first_record)(join_tab);
3340
10884
    rc= evaluate_join_record(join, join_tab, error);
3341
10885
  }
3342
 
 
3343
 
  /*
3344
 
    Note: psergey has added the 2nd part of the following condition; the
 
10886
  
 
10887
  /* 
 
10888
    Note: psergey has added the 2nd part of the following condition; the 
3345
10889
    change should probably be made in 5.1, too.
3346
10890
  */
3347
10891
  while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
3359
10903
  return rc;
3360
10904
}
3361
10905
 
3362
 
int safe_index_read(JoinTable *tab)
 
10906
 
 
10907
 
 
10908
 
 
10909
/*
 
10910
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
 
10911
 
 
10912
  SYNPOSIS
 
10913
    do_sj_dups_weedout()
 
10914
      
 
10915
  RETURN
 
10916
    -1  Error
 
10917
    1   The row combination is a duplicate (discard it)
 
10918
    0   The row combination is not a duplicate (continue)
 
10919
*/
 
10920
 
 
10921
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl) 
 
10922
{
 
10923
  int error;
 
10924
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
 
10925
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
 
10926
  unsigned char *ptr= sjtbl->tmp_table->record[0] + 1;
 
10927
  unsigned char *nulls_ptr= ptr;
 
10928
  
 
10929
  /* Put the the rowids tuple into table->record[0]: */
 
10930
 
 
10931
  // 1. Store the length 
 
10932
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
 
10933
  {
 
10934
    *ptr= (unsigned char)(sjtbl->rowid_len + sjtbl->null_bytes);
 
10935
    ptr++;
 
10936
  }
 
10937
  else
 
10938
  {
 
10939
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
 
10940
    ptr += 2;
 
10941
  }
 
10942
 
 
10943
  // 2. Zero the null bytes 
 
10944
  if (sjtbl->null_bytes)
 
10945
  {
 
10946
    memset(ptr, 0, sjtbl->null_bytes);
 
10947
    ptr += sjtbl->null_bytes; 
 
10948
  }
 
10949
 
 
10950
  // 3. Put the rowids
 
10951
  for (uint32_t i=0; tab != tab_end; tab++, i++)
 
10952
  {
 
10953
    handler *h= tab->join_tab->table->file;
 
10954
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
 
10955
    {
 
10956
      /* It's a NULL-complemented row */
 
10957
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
 
10958
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
 
10959
    }
 
10960
    else
 
10961
    {
 
10962
      /* Copy the rowid value */
 
10963
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
10964
        h->position(tab->join_tab->table->record[0]);
 
10965
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
 
10966
    }
 
10967
  }
 
10968
 
 
10969
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
 
10970
  if (error)
 
10971
  {
 
10972
    /* create_myisam_from_heap will generate error if needed */
 
10973
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
10974
        create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo, 
 
10975
                                &sjtbl->recinfo, error, 1))
 
10976
      return -1;
 
10977
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
 
10978
    return 1;
 
10979
  }
 
10980
  return 0;
 
10981
}
 
10982
 
 
10983
 
 
10984
/*
 
10985
  SemiJoinDuplicateElimination: Reset the temporary table
 
10986
*/
 
10987
 
 
10988
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
 
10989
{
 
10990
  if (sj_tbl->tmp_table)
 
10991
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
 
10992
  return 0;
 
10993
}
 
10994
 
 
10995
/*
 
10996
  Process one record of the nested loop join.
 
10997
 
 
10998
    This function will evaluate parts of WHERE/ON clauses that are
 
10999
    applicable to the partial record on hand and in case of success
 
11000
    submit this record to the next level of the nested loop.
 
11001
*/
 
11002
 
 
11003
static enum_nested_loop_state
 
11004
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
11005
                     int error)
 
11006
{
 
11007
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
 
11008
  ha_rows found_records=join->found_records;
 
11009
  COND *select_cond= join_tab->select_cond;
 
11010
 
 
11011
  if (error > 0 || (join->thd->is_error()))     // Fatal error
 
11012
    return NESTED_LOOP_ERROR;
 
11013
  if (error < 0)
 
11014
    return NESTED_LOOP_NO_MORE_ROWS;
 
11015
  if (join->thd->killed)                        // Aborted by user
 
11016
  {
 
11017
    join->thd->send_kill_message();
 
11018
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
 
11019
  }
 
11020
  if (!select_cond || select_cond->val_int())
 
11021
  {
 
11022
    /*
 
11023
      There is no select condition or the attached pushed down
 
11024
      condition is true => a match is found.
 
11025
    */
 
11026
    bool found= 1;
 
11027
    while (join_tab->first_unmatched && found)
 
11028
    {
 
11029
      /*
 
11030
        The while condition is always false if join_tab is not
 
11031
        the last inner join table of an outer join operation.
 
11032
      */
 
11033
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11034
      /*
 
11035
        Mark that a match for current outer table is found.
 
11036
        This activates push down conditional predicates attached
 
11037
        to the all inner tables of the outer join.
 
11038
      */
 
11039
      first_unmatched->found= 1;
 
11040
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11041
      {
 
11042
        if (tab->table->reginfo.not_exists_optimize)
 
11043
          return NESTED_LOOP_NO_MORE_ROWS;
 
11044
        /* Check all predicates that has just been activated. */
 
11045
        /*
 
11046
          Actually all predicates non-guarded by first_unmatched->found
 
11047
          will be re-evaluated again. It could be fixed, but, probably,
 
11048
          it's not worth doing now.
 
11049
        */
 
11050
        if (tab->select_cond && !tab->select_cond->val_int())
 
11051
        {
 
11052
          /* The condition attached to table tab is false */
 
11053
          if (tab == join_tab)
 
11054
            found= 0;
 
11055
          else
 
11056
          {
 
11057
            /*
 
11058
              Set a return point if rejected predicate is attached
 
11059
              not to the last table of the current nest level.
 
11060
            */
 
11061
            join->return_tab= tab;
 
11062
            return NESTED_LOOP_OK;
 
11063
          }
 
11064
        }
 
11065
      }
 
11066
      /*
 
11067
        Check whether join_tab is not the last inner table
 
11068
        for another embedding outer join.
 
11069
      */
 
11070
      if ((first_unmatched= first_unmatched->first_upper) &&
 
11071
          first_unmatched->last_inner != join_tab)
 
11072
        first_unmatched= 0;
 
11073
      join_tab->first_unmatched= first_unmatched;
 
11074
    }
 
11075
 
 
11076
    JOIN_TAB *return_tab= join->return_tab;
 
11077
    join_tab->found_match= true;
 
11078
    if (join_tab->check_weed_out_table)
 
11079
    {
 
11080
      int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
 
11081
      if (res == -1)
 
11082
        return NESTED_LOOP_ERROR;
 
11083
      if (res == 1)
 
11084
        return NESTED_LOOP_OK;
 
11085
    }
 
11086
    else if (join_tab->do_firstmatch)
 
11087
    {
 
11088
      /* 
 
11089
        We should return to the join_tab->do_firstmatch after we have 
 
11090
        enumerated all the suffixes for current prefix row combination
 
11091
      */
 
11092
      return_tab= join_tab->do_firstmatch;
 
11093
    }
 
11094
 
 
11095
    /*
 
11096
      It was not just a return to lower loop level when one
 
11097
      of the newly activated predicates is evaluated as false
 
11098
      (See above join->return_tab= tab).
 
11099
    */
 
11100
    join->examined_rows++;
 
11101
    join->thd->row_count++;
 
11102
 
 
11103
    if (found)
 
11104
    {
 
11105
      enum enum_nested_loop_state rc;
 
11106
      /* A match from join_tab is found for the current partial join. */
 
11107
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
 
11108
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11109
        return rc;
 
11110
      if (return_tab < join->return_tab)
 
11111
        join->return_tab= return_tab;
 
11112
 
 
11113
      if (join->return_tab < join_tab)
 
11114
        return NESTED_LOOP_OK;
 
11115
      /*
 
11116
        Test if this was a SELECT DISTINCT query on a table that
 
11117
        was not in the field list;  In this case we can abort if
 
11118
        we found a row, as no new rows can be added to the result.
 
11119
      */
 
11120
      if (not_used_in_distinct && found_records != join->found_records)
 
11121
        return NESTED_LOOP_NO_MORE_ROWS;
 
11122
    }
 
11123
    else
 
11124
      join_tab->read_record.file->unlock_row();
 
11125
  }
 
11126
  else
 
11127
  {
 
11128
    /*
 
11129
      The condition pushed down to the table join_tab rejects all rows
 
11130
      with the beginning coinciding with the current partial join.
 
11131
    */
 
11132
    join->examined_rows++;
 
11133
    join->thd->row_count++;
 
11134
    join_tab->read_record.file->unlock_row();
 
11135
  }
 
11136
  return NESTED_LOOP_OK;
 
11137
}
 
11138
 
 
11139
 
 
11140
/**
 
11141
 
 
11142
  @details
 
11143
    Construct a NULL complimented partial join record and feed it to the next
 
11144
    level of the nested loop. This function is used in case we have
 
11145
    an OUTER join and no matching record was found.
 
11146
*/
 
11147
 
 
11148
static enum_nested_loop_state
 
11149
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
 
11150
{
 
11151
  /*
 
11152
    The table join_tab is the first inner table of a outer join operation
 
11153
    and no matches has been found for the current outer row.
 
11154
  */
 
11155
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
 
11156
  /* Cache variables for faster loop */
 
11157
  COND *select_cond;
 
11158
  for ( ; join_tab <= last_inner_tab ; join_tab++)
 
11159
  {
 
11160
    /* Change the the values of guard predicate variables. */
 
11161
    join_tab->found= 1;
 
11162
    join_tab->not_null_compl= 0;
 
11163
    /* The outer row is complemented by nulls for each inner tables */
 
11164
    restore_record(join_tab->table,s->default_values);  // Make empty record
 
11165
    mark_as_null_row(join_tab->table);       // For group by without error
 
11166
    select_cond= join_tab->select_cond;
 
11167
    /* Check all attached conditions for inner table rows. */
 
11168
    if (select_cond && !select_cond->val_int())
 
11169
      return NESTED_LOOP_OK;
 
11170
  }
 
11171
  join_tab--;
 
11172
  /*
 
11173
    The row complemented by nulls might be the first row
 
11174
    of embedding outer joins.
 
11175
    If so, perform the same actions as in the code
 
11176
    for the first regular outer join row above.
 
11177
  */
 
11178
  for ( ; ; )
 
11179
  {
 
11180
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11181
    if ((first_unmatched= first_unmatched->first_upper) &&
 
11182
        first_unmatched->last_inner != join_tab)
 
11183
      first_unmatched= 0;
 
11184
    join_tab->first_unmatched= first_unmatched;
 
11185
    if (!first_unmatched)
 
11186
      break;
 
11187
    first_unmatched->found= 1;
 
11188
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11189
    {
 
11190
      if (tab->select_cond && !tab->select_cond->val_int())
 
11191
      {
 
11192
        join->return_tab= tab;
 
11193
        return NESTED_LOOP_OK;
 
11194
      }
 
11195
    }
 
11196
  }
 
11197
  /*
 
11198
    The row complemented by nulls satisfies all conditions
 
11199
    attached to inner tables.
 
11200
    Send the row complemented by nulls to be joined with the
 
11201
    remaining tables.
 
11202
  */
 
11203
  return (*join_tab->next_select)(join, join_tab+1, 0);
 
11204
}
 
11205
 
 
11206
 
 
11207
static enum_nested_loop_state
 
11208
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
 
11209
{
 
11210
  enum_nested_loop_state rc= NESTED_LOOP_OK;
 
11211
  int error;
 
11212
  READ_RECORD *info;
 
11213
 
 
11214
  join_tab->table->null_row= 0;
 
11215
  if (!join_tab->cache.records)
 
11216
    return NESTED_LOOP_OK;                      /* Nothing to do */
 
11217
  if (skip_last)
 
11218
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
 
11219
  if (join_tab->use_quick == 2)
 
11220
  {
 
11221
    if (join_tab->select->quick)
 
11222
    {                                   /* Used quick select last. reset it */
 
11223
      delete join_tab->select->quick;
 
11224
      join_tab->select->quick=0;
 
11225
    }
 
11226
  }
 
11227
 /* read through all records */
 
11228
  if ((error=join_init_read_record(join_tab)))
 
11229
  {
 
11230
    reset_cache_write(&join_tab->cache);
 
11231
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
 
11232
  }
 
11233
 
 
11234
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
 
11235
  {
 
11236
    tmp->status=tmp->table->status;
 
11237
    tmp->table->status=0;
 
11238
  }
 
11239
 
 
11240
  info= &join_tab->read_record;
 
11241
  do
 
11242
  {
 
11243
    if (join->thd->killed)
 
11244
    {
 
11245
      join->thd->send_kill_message();
 
11246
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
 
11247
    }
 
11248
    SQL_SELECT *select=join_tab->select;
 
11249
    if (rc == NESTED_LOOP_OK &&
 
11250
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
 
11251
    {
 
11252
      uint32_t i;
 
11253
      reset_cache_read(&join_tab->cache);
 
11254
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
 
11255
      {
 
11256
        read_cached_record(join_tab);
 
11257
        if (!select || !select->skip_record())
 
11258
        {
 
11259
          int res= 0;
 
11260
          if (!join_tab->check_weed_out_table || 
 
11261
              !(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
 
11262
          {
 
11263
            rc= (join_tab->next_select)(join,join_tab+1,0);
 
11264
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11265
            {
 
11266
              reset_cache_write(&join_tab->cache);
 
11267
              return rc;
 
11268
            }
 
11269
          }
 
11270
          if (res == -1)
 
11271
            return NESTED_LOOP_ERROR;
 
11272
        }
 
11273
      }
 
11274
    }
 
11275
  } while (!(error=info->read_record(info)));
 
11276
 
 
11277
  if (skip_last)
 
11278
    read_cached_record(join_tab);               // Restore current record
 
11279
  reset_cache_write(&join_tab->cache);
 
11280
  if (error > 0)                                // Fatal error
 
11281
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
 
11282
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
 
11283
    tmp2->table->status=tmp2->status;
 
11284
  return NESTED_LOOP_OK;
 
11285
}
 
11286
 
 
11287
int safe_index_read(JOIN_TAB *tab)
3363
11288
{
3364
11289
  int error;
3365
11290
  Table *table= tab->table;
3366
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
11291
  if ((error=table->file->index_read_map(table->record[0],
3367
11292
                                         tab->ref.key_buff,
3368
11293
                                         make_prev_keypart_map(tab->ref.key_parts),
3369
11294
                                         HA_READ_KEY_EXACT)))
3371
11296
  return 0;
3372
11297
}
3373
11298
 
 
11299
 
 
11300
static int
 
11301
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
 
11302
{
 
11303
  int error;
 
11304
  Table *table=tab->table;
 
11305
  table->const_table=1;
 
11306
  table->null_row=0;
 
11307
  table->status=STATUS_NO_RECORD;
 
11308
  
 
11309
  if (tab->type == JT_SYSTEM)
 
11310
  {
 
11311
    if ((error=join_read_system(tab)))
 
11312
    {                                           // Info for DESCRIBE
 
11313
      tab->info="const row not found";
 
11314
      /* Mark for EXPLAIN that the row was not found */
 
11315
      pos->records_read=0.0;
 
11316
      pos->ref_depend_map= 0;
 
11317
      if (!table->maybe_null || error > 0)
 
11318
        return(error);
 
11319
    }
 
11320
  }
 
11321
  else
 
11322
  {
 
11323
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
 
11324
        !table->no_keyread &&
 
11325
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
 
11326
    {
 
11327
      table->key_read=1;
 
11328
      table->file->extra(HA_EXTRA_KEYREAD);
 
11329
      tab->index= tab->ref.key;
 
11330
    }
 
11331
    error=join_read_const(tab);
 
11332
    if (table->key_read)
 
11333
    {
 
11334
      table->key_read=0;
 
11335
      table->file->extra(HA_EXTRA_NO_KEYREAD);
 
11336
    }
 
11337
    if (error)
 
11338
    {
 
11339
      tab->info="unique row not found";
 
11340
      /* Mark for EXPLAIN that the row was not found */
 
11341
      pos->records_read=0.0;
 
11342
      pos->ref_depend_map= 0;
 
11343
      if (!table->maybe_null || error > 0)
 
11344
        return(error);
 
11345
    }
 
11346
  }
 
11347
  if (*tab->on_expr_ref && !table->null_row)
 
11348
  {
 
11349
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
 
11350
      mark_as_null_row(table);  
 
11351
  }
 
11352
  if (!table->null_row)
 
11353
    table->maybe_null=0;
 
11354
 
 
11355
  /* Check appearance of new constant items in Item_equal objects */
 
11356
  JOIN *join= tab->join;
 
11357
  if (join->conds)
 
11358
    update_const_equal_items(join->conds, tab);
 
11359
  TableList *tbl;
 
11360
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
 
11361
  {
 
11362
    TableList *embedded;
 
11363
    TableList *embedding= tbl;
 
11364
    do
 
11365
    {
 
11366
      embedded= embedding;
 
11367
      if (embedded->on_expr)
 
11368
         update_const_equal_items(embedded->on_expr, tab);
 
11369
      embedding= embedded->embedding;
 
11370
    }
 
11371
    while (embedding &&
 
11372
           embedding->nested_join->join_list.head() == embedded);
 
11373
  }
 
11374
 
 
11375
  return(0);
 
11376
}
 
11377
 
 
11378
 
 
11379
static int
 
11380
join_read_system(JOIN_TAB *tab)
 
11381
{
 
11382
  Table *table= tab->table;
 
11383
  int error;
 
11384
  if (table->status & STATUS_GARBAGE)           // If first read
 
11385
  {
 
11386
    if ((error=table->file->read_first_row(table->record[0],
 
11387
                                           table->s->primary_key)))
 
11388
    {
 
11389
      if (error != HA_ERR_END_OF_FILE)
 
11390
        return table->report_error(error);
 
11391
      mark_as_null_row(tab->table);
 
11392
      empty_record(table);                      // Make empty record
 
11393
      return -1;
 
11394
    }
 
11395
    store_record(table,record[1]);
 
11396
  }
 
11397
  else if (!table->status)                      // Only happens with left join
 
11398
    restore_record(table,record[1]);                    // restore old record
 
11399
  table->null_row=0;
 
11400
  return table->status ? -1 : 0;
 
11401
}
 
11402
 
 
11403
 
3374
11404
/**
3375
11405
  Read a (constant) table when there is at most one matching row.
3376
11406
 
3383
11413
  @retval
3384
11414
    1   Got an error (other than row not found) during read
3385
11415
*/
3386
 
int join_read_const(JoinTable *tab)
 
11416
 
 
11417
static int
 
11418
join_read_const(JOIN_TAB *tab)
3387
11419
{
3388
11420
  int error;
3389
11421
  Table *table= tab->table;
3390
11422
  if (table->status & STATUS_GARBAGE)           // If first read
3391
11423
  {
3392
11424
    table->status= 0;
3393
 
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3394
 
      error= HA_ERR_KEY_NOT_FOUND;
 
11425
    if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
 
11426
      error=HA_ERR_KEY_NOT_FOUND;
3395
11427
    else
3396
11428
    {
3397
 
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
 
11429
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
3398
11430
                                            (unsigned char*) tab->ref.key_buff,
3399
11431
                                            make_prev_keypart_map(tab->ref.key_parts),
3400
11432
                                            HA_READ_KEY_EXACT);
3402
11434
    if (error)
3403
11435
    {
3404
11436
      table->status= STATUS_NOT_FOUND;
3405
 
      tab->table->mark_as_null_row();
3406
 
      table->emptyRecord();
 
11437
      mark_as_null_row(tab->table);
 
11438
      empty_record(table);
3407
11439
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3408
 
        return table->report_error(error);
 
11440
        return table->report_error(error);
3409
11441
      return -1;
3410
11442
    }
3411
 
    table->storeRecord();
 
11443
    store_record(table,record[1]);
3412
11444
  }
3413
11445
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
3414
11446
  {
3415
11447
    table->status=0;
3416
 
    table->restoreRecord();                     // restore old record
 
11448
    restore_record(table,record[1]);                    // restore old record
3417
11449
  }
3418
11450
  table->null_row=0;
3419
11451
  return table->status ? -1 : 0;
3420
11452
}
3421
11453
 
 
11454
 
3422
11455
/*
3423
11456
  eq_ref access method implementation: "read_first" function
3424
11457
 
3425
11458
  SYNOPSIS
3426
11459
    join_read_key()
3427
 
      tab  JoinTable of the accessed table
 
11460
      tab  JOIN_TAB of the accessed table
3428
11461
 
3429
11462
  DESCRIPTION
3430
11463
    This is "read_fist" function for the "ref" access method. The difference
3432
11465
 
3433
11466
  RETURN
3434
11467
    0  - Ok
3435
 
   -1  - Row not found
 
11468
   -1  - Row not found 
3436
11469
    1  - Error
3437
11470
*/
3438
 
int join_read_key(JoinTable *tab)
 
11471
 
 
11472
static int
 
11473
join_read_key(JOIN_TAB *tab)
3439
11474
{
3440
11475
  int error;
3441
11476
  Table *table= tab->table;
3442
11477
 
3443
 
  if (!table->cursor->inited)
 
11478
  if (!table->file->inited)
3444
11479
  {
3445
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3446
 
    if (error != 0)
3447
 
    {
3448
 
      table->print_error(error, MYF(0));
3449
 
    }
 
11480
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3450
11481
  }
3451
11482
 
3452
 
  /* @todo Why don't we do "Late NULLs Filtering" here? */
 
11483
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3453
11484
  if (cmp_buffer_with_ref(tab) ||
3454
11485
      (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
3455
11486
  {
3458
11489
      table->status=STATUS_NOT_FOUND;
3459
11490
      return -1;
3460
11491
    }
3461
 
    error=table->cursor->index_read_map(table->getInsertRecord(),
 
11492
    error=table->file->index_read_map(table->record[0],
3462
11493
                                      tab->ref.key_buff,
3463
11494
                                      make_prev_keypart_map(tab->ref.key_parts),
3464
11495
                                      HA_READ_KEY_EXACT);
3469
11500
  return table->status ? -1 : 0;
3470
11501
}
3471
11502
 
 
11503
 
3472
11504
/*
3473
11505
  ref access method implementation: "read_first" function
3474
11506
 
3475
11507
  SYNOPSIS
3476
11508
    join_read_always_key()
3477
 
      tab  JoinTable of the accessed table
 
11509
      tab  JOIN_TAB of the accessed table
3478
11510
 
3479
11511
  DESCRIPTION
3480
 
    This is "read_first" function for the "ref" access method.
3481
 
 
 
11512
    This is "read_fist" function for the "ref" access method.
 
11513
   
3482
11514
    The functon must leave the index initialized when it returns.
3483
11515
    ref_or_null access implementation depends on that.
3484
11516
 
3485
11517
  RETURN
3486
11518
    0  - Ok
3487
 
   -1  - Row not found
 
11519
   -1  - Row not found 
3488
11520
    1  - Error
3489
11521
*/
3490
 
int join_read_always_key(JoinTable *tab)
 
11522
 
 
11523
static int
 
11524
join_read_always_key(JOIN_TAB *tab)
3491
11525
{
3492
11526
  int error;
3493
11527
  Table *table= tab->table;
3494
11528
 
3495
11529
  /* 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
 
 
 
11530
  if (!table->file->inited)
 
11531
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
11532
 
3503
11533
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3504
11534
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
3505
11535
  {
3507
11537
        return -1;
3508
11538
  }
3509
11539
 
3510
 
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
11540
  if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
3511
11541
    return -1;
3512
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
11542
  if ((error=table->file->index_read_map(table->record[0],
3513
11543
                                         tab->ref.key_buff,
3514
11544
                                         make_prev_keypart_map(tab->ref.key_parts),
3515
11545
                                         HA_READ_KEY_EXACT)))
3516
11546
  {
3517
11547
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3518
11548
      return table->report_error(error);
3519
 
    return -1;
 
11549
    return -1; /* purecov: inspected */
3520
11550
  }
3521
 
 
3522
11551
  return 0;
3523
11552
}
3524
11553
 
 
11554
 
3525
11555
/**
3526
 
  This function is used when optimizing away ORDER BY in
3527
 
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
 
11556
  This function is used when optimizing away order_st BY in 
 
11557
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
3528
11558
*/
3529
 
int join_read_last_key(JoinTable *tab)
 
11559
  
 
11560
static int
 
11561
join_read_last_key(JOIN_TAB *tab)
3530
11562
{
3531
11563
  int error;
3532
11564
  Table *table= tab->table;
3533
11565
 
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))
 
11566
  if (!table->file->inited)
 
11567
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
11568
  if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
3541
11569
    return -1;
3542
 
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
 
11570
  if ((error=table->file->index_read_last_map(table->record[0],
3543
11571
                                              tab->ref.key_buff,
3544
11572
                                              make_prev_keypart_map(tab->ref.key_parts))))
3545
11573
  {
3546
11574
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3547
11575
      return table->report_error(error);
3548
 
    return -1;
 
11576
    return -1; /* purecov: inspected */
3549
11577
  }
3550
11578
  return 0;
3551
11579
}
3552
11580
 
3553
 
int join_no_more_records(ReadRecord *)
 
11581
 
 
11582
        /* ARGSUSED */
 
11583
static int
 
11584
join_no_more_records(READ_RECORD *info __attribute__((unused)))
3554
11585
{
3555
11586
  return -1;
3556
11587
}
3557
11588
 
3558
 
int join_read_next_same_diff(ReadRecord *info)
 
11589
static int
 
11590
join_read_next_same_diff(READ_RECORD *info)
3559
11591
{
3560
11592
  Table *table= info->table;
3561
 
  JoinTable *tab=table->reginfo.join_tab;
 
11593
  JOIN_TAB *tab=table->reginfo.join_tab;
3562
11594
  if (tab->insideout_match_tab->found_match)
3563
11595
  {
3564
 
    KeyInfo *key= tab->table->key_info + tab->index;
3565
 
    do
 
11596
    KEY *key= tab->table->key_info + tab->index;
 
11597
    do 
3566
11598
    {
3567
11599
      int error;
3568
11600
      /* Save index tuple from record to the buffer */
3569
11601
      key_copy(tab->insideout_buf, info->record, key, 0);
3570
11602
 
3571
 
      if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
11603
      if ((error=table->file->index_next_same(table->record[0],
3572
11604
                                              tab->ref.key_buff,
3573
11605
                                              tab->ref.key_length)))
3574
11606
      {
3577
11609
        table->status= STATUS_GARBAGE;
3578
11610
        return -1;
3579
11611
      }
3580
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
11612
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3581
11613
                      tab->insideout_buf, key->key_length));
3582
11614
    tab->insideout_match_tab->found_match= 0;
3583
11615
    return 0;
3586
11618
    return join_read_next_same(info);
3587
11619
}
3588
11620
 
3589
 
int join_read_next_same(ReadRecord *info)
 
11621
static int
 
11622
join_read_next_same(READ_RECORD *info)
3590
11623
{
3591
11624
  int error;
3592
11625
  Table *table= info->table;
3593
 
  JoinTable *tab=table->reginfo.join_tab;
 
11626
  JOIN_TAB *tab=table->reginfo.join_tab;
3594
11627
 
3595
 
  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
11628
  if ((error=table->file->index_next_same(table->record[0],
3596
11629
                                          tab->ref.key_buff,
3597
11630
                                          tab->ref.key_length)))
3598
11631
  {
3601
11634
    table->status= STATUS_GARBAGE;
3602
11635
    return -1;
3603
11636
  }
3604
 
 
3605
11637
  return 0;
3606
11638
}
3607
11639
 
3608
 
int join_read_prev_same(ReadRecord *info)
 
11640
 
 
11641
static int
 
11642
join_read_prev_same(READ_RECORD *info)
3609
11643
{
3610
11644
  int error;
3611
11645
  Table *table= info->table;
3612
 
  JoinTable *tab=table->reginfo.join_tab;
 
11646
  JOIN_TAB *tab=table->reginfo.join_tab;
3613
11647
 
3614
 
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
 
11648
  if ((error=table->file->index_prev(table->record[0])))
3615
11649
    return table->report_error(error);
3616
11650
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3617
11651
                      tab->ref.key_length))
3622
11656
  return error;
3623
11657
}
3624
11658
 
3625
 
int join_init_quick_read_record(JoinTable *tab)
 
11659
 
 
11660
static int
 
11661
join_init_quick_read_record(JOIN_TAB *tab)
3626
11662
{
3627
11663
  if (test_if_quick_select(tab) == -1)
3628
11664
    return -1;                                  /* No possible records */
3629
11665
  return join_init_read_record(tab);
3630
11666
}
3631
11667
 
3632
 
int init_read_record_seq(JoinTable *tab)
 
11668
 
 
11669
int rr_sequential(READ_RECORD *info);
 
11670
int init_read_record_seq(JOIN_TAB *tab)
3633
11671
{
3634
 
  tab->read_record.init_reard_record_sequential();
3635
 
 
3636
 
  if (tab->read_record.cursor->startTableScan(1))
 
11672
  tab->read_record.read_record= rr_sequential;
 
11673
  if (tab->read_record.file->ha_rnd_init(1))
3637
11674
    return 1;
3638
11675
  return (*tab->read_record.read_record)(&tab->read_record);
3639
11676
}
3640
11677
 
3641
 
int test_if_quick_select(JoinTable *tab)
 
11678
static int
 
11679
test_if_quick_select(JOIN_TAB *tab)
3642
11680
{
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);
 
11681
  delete tab->select->quick;
 
11682
  tab->select->quick=0;
 
11683
  return tab->select->test_quick_select(tab->join->thd, tab->keys,
 
11684
                                        (table_map) 0, HA_POS_ERROR, 0,
 
11685
                                        false);
3647
11686
}
3648
11687
 
3649
 
int join_init_read_record(JoinTable *tab)
 
11688
 
 
11689
static int
 
11690
join_init_read_record(JOIN_TAB *tab)
3650
11691
{
3651
11692
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3652
11693
    return 1;
3653
 
 
3654
 
  if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true))
3655
 
    return 1;
3656
 
 
 
11694
  init_read_record(&tab->read_record, tab->join->thd, tab->table,
 
11695
                   tab->select,1,1);
3657
11696
  return (*tab->read_record.read_record)(&tab->read_record);
3658
11697
}
3659
11698
 
3660
 
int join_read_first(JoinTable *tab)
 
11699
 
 
11700
static int
 
11701
join_read_first(JOIN_TAB *tab)
3661
11702
{
3662
11703
  int error;
3663
11704
  Table *table=tab->table;
3664
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11705
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3665
11706
      !table->no_keyread)
3666
11707
  {
3667
 
    table->key_read= 1;
3668
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11708
    table->key_read=1;
 
11709
    table->file->extra(HA_EXTRA_KEYREAD);
3669
11710
  }
3670
 
  tab->table->status= 0;
 
11711
  tab->table->status=0;
3671
11712
  tab->read_record.table=table;
3672
 
  tab->read_record.cursor=table->cursor;
 
11713
  tab->read_record.file=table->file;
3673
11714
  tab->read_record.index=tab->index;
3674
 
  tab->read_record.record=table->getInsertRecord();
 
11715
  tab->read_record.record=table->record[0];
3675
11716
  if (tab->insideout_match_tab)
3676
11717
  {
3677
11718
    tab->read_record.do_insideout_scan= tab;
3684
11725
    tab->read_record.do_insideout_scan= 0;
3685
11726
  }
3686
11727
 
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())))
 
11728
  if (!table->file->inited)
 
11729
    table->file->ha_index_init(tab->index, tab->sorted);
 
11730
  if ((error=tab->table->file->index_first(tab->table->record[0])))
3697
11731
  {
3698
11732
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3699
11733
      table->report_error(error);
3700
11734
    return -1;
3701
11735
  }
3702
 
 
3703
11736
  return 0;
3704
11737
}
3705
11738
 
3706
 
int join_read_next_different(ReadRecord *info)
 
11739
 
 
11740
static int
 
11741
join_read_next_different(READ_RECORD *info)
3707
11742
{
3708
 
  JoinTable *tab= info->do_insideout_scan;
 
11743
  JOIN_TAB *tab= info->do_insideout_scan;
3709
11744
  if (tab->insideout_match_tab->found_match)
3710
11745
  {
3711
 
    KeyInfo *key= tab->table->key_info + tab->index;
3712
 
    do
 
11746
    KEY *key= tab->table->key_info + tab->index;
 
11747
    do 
3713
11748
    {
3714
11749
      int error;
3715
11750
      /* Save index tuple from record to the buffer */
3716
11751
      key_copy(tab->insideout_buf, info->record, key, 0);
3717
11752
 
3718
 
      if ((error=info->cursor->index_next(info->record)))
 
11753
      if ((error=info->file->index_next(info->record)))
3719
11754
        return info->table->report_error(error);
3720
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
11755
      
 
11756
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3721
11757
                      tab->insideout_buf, key->key_length));
3722
11758
    tab->insideout_match_tab->found_match= 0;
3723
11759
    return 0;
3726
11762
    return join_read_next(info);
3727
11763
}
3728
11764
 
3729
 
int join_read_next(ReadRecord *info)
 
11765
 
 
11766
static int
 
11767
join_read_next(READ_RECORD *info)
3730
11768
{
3731
11769
  int error;
3732
 
  if ((error=info->cursor->index_next(info->record)))
 
11770
  if ((error=info->file->index_next(info->record)))
3733
11771
    return info->table->report_error(error);
3734
11772
  return 0;
3735
11773
}
3736
11774
 
3737
 
int join_read_last(JoinTable *tab)
 
11775
 
 
11776
static int
 
11777
join_read_last(JOIN_TAB *tab)
3738
11778
{
3739
11779
  Table *table=tab->table;
3740
11780
  int error;
3741
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11781
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3742
11782
      !table->no_keyread)
3743
11783
  {
3744
11784
    table->key_read=1;
3745
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11785
    table->file->extra(HA_EXTRA_KEYREAD);
3746
11786
  }
3747
11787
  tab->table->status=0;
3748
11788
  tab->read_record.read_record=join_read_prev;
3749
11789
  tab->read_record.table=table;
3750
 
  tab->read_record.cursor=table->cursor;
 
11790
  tab->read_record.file=table->file;
3751
11791
  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())))
 
11792
  tab->read_record.record=table->record[0];
 
11793
  if (!table->file->inited)
 
11794
    table->file->ha_index_init(tab->index, 1);
 
11795
  if ((error= tab->table->file->index_last(tab->table->record[0])))
3760
11796
    return table->report_error(error);
3761
 
 
3762
11797
  return 0;
3763
11798
}
3764
11799
 
3765
 
int join_read_prev(ReadRecord *info)
 
11800
 
 
11801
static int
 
11802
join_read_prev(READ_RECORD *info)
3766
11803
{
3767
11804
  int error;
3768
 
  if ((error= info->cursor->index_prev(info->record)))
 
11805
  if ((error= info->file->index_prev(info->record)))
3769
11806
    return info->table->report_error(error);
3770
 
 
3771
11807
  return 0;
3772
11808
}
3773
11809
 
3774
11810
/**
3775
11811
  Reading of key with key reference and one part that may be NULL.
3776
11812
*/
3777
 
int join_read_always_key_or_null(JoinTable *tab)
 
11813
 
 
11814
int
 
11815
join_read_always_key_or_null(JOIN_TAB *tab)
3778
11816
{
3779
11817
  int res;
3780
11818
 
3788
11826
  return safe_index_read(tab);
3789
11827
}
3790
11828
 
3791
 
int join_read_next_same_or_null(ReadRecord *info)
 
11829
 
 
11830
int
 
11831
join_read_next_same_or_null(READ_RECORD *info)
3792
11832
{
3793
11833
  int error;
3794
11834
  if ((error= join_read_next_same(info)) >= 0)
3795
11835
    return error;
3796
 
  JoinTable *tab= info->table->reginfo.join_tab;
 
11836
  JOIN_TAB *tab= info->table->reginfo.join_tab;
3797
11837
 
3798
11838
  /* Test if we have already done a read after null key */
3799
11839
  if (*tab->ref.null_ref_key)
3802
11842
  return safe_index_read(tab);                  // then read null keys
3803
11843
}
3804
11844
 
3805
 
enum_nested_loop_state end_send_group(Join *join, JoinTable *, bool end_of_records)
 
11845
 
 
11846
/*****************************************************************************
 
11847
  DESCRIPTION
 
11848
    Functions that end one nested loop iteration. Different functions
 
11849
    are used to support GROUP BY clause and to redirect records
 
11850
    to a table (e.g. in case of SELECT into a temporary table) or to the
 
11851
    network client.
 
11852
 
 
11853
  RETURN VALUES
 
11854
    NESTED_LOOP_OK           - the record has been successfully handled
 
11855
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
 
11856
                               was detected
 
11857
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
 
11858
                               the record
 
11859
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
 
11860
                               additionally, the nested loop produced the
 
11861
                               number of rows specified in the LIMIT clause
 
11862
                               for the query
 
11863
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
 
11864
                               additionally, there is a cursor and the nested
 
11865
                               loop algorithm produced the number of rows
 
11866
                               that is specified for current cursor fetch
 
11867
                               operation.
 
11868
   All return values except NESTED_LOOP_OK abort the nested loop.
 
11869
*****************************************************************************/
 
11870
 
 
11871
/* ARGSUSED */
 
11872
static enum_nested_loop_state
 
11873
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
11874
         bool end_of_records)
 
11875
{
 
11876
  if (!end_of_records)
 
11877
  {
 
11878
    int error;
 
11879
    if (join->having && join->having->val_int() == 0)
 
11880
      return(NESTED_LOOP_OK);               // Didn't match having
 
11881
    error=0;
 
11882
    if (join->do_send_rows)
 
11883
      error=join->result->send_data(*join->fields);
 
11884
    if (error)
 
11885
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
 
11886
    if (++join->send_records >= join->unit->select_limit_cnt &&
 
11887
        join->do_send_rows)
 
11888
    {
 
11889
      if (join->select_options & OPTION_FOUND_ROWS)
 
11890
      {
 
11891
        JOIN_TAB *jt=join->join_tab;
 
11892
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
 
11893
            && !join->send_group_parts && !join->having && !jt->select_cond &&
 
11894
            !(jt->select && jt->select->quick) &&
 
11895
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
11896
            (jt->ref.key < 0))
 
11897
        {
 
11898
          /* Join over all rows in table;  Return number of found rows */
 
11899
          Table *table=jt->table;
 
11900
 
 
11901
          join->select_options ^= OPTION_FOUND_ROWS;
 
11902
          if (table->sort.record_pointers ||
 
11903
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
 
11904
          {
 
11905
            /* Using filesort */
 
11906
            join->send_records= table->sort.found_records;
 
11907
          }
 
11908
          else
 
11909
          {
 
11910
            table->file->info(HA_STATUS_VARIABLE);
 
11911
            join->send_records= table->file->stats.records;
 
11912
          }
 
11913
        }
 
11914
        else 
 
11915
        {
 
11916
          join->do_send_rows= 0;
 
11917
          if (join->unit->fake_select_lex)
 
11918
            join->unit->fake_select_lex->select_limit= 0;
 
11919
          return(NESTED_LOOP_OK);
 
11920
        }
 
11921
      }
 
11922
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
 
11923
    }
 
11924
    else if (join->send_records >= join->fetch_limit)
 
11925
    {
 
11926
      /*
 
11927
        There is a server side cursor and all rows for
 
11928
        this fetch request are sent.
 
11929
      */
 
11930
      return(NESTED_LOOP_CURSOR_LIMIT);
 
11931
    }
 
11932
  }
 
11933
 
 
11934
  return(NESTED_LOOP_OK);
 
11935
}
 
11936
 
 
11937
 
 
11938
        /* ARGSUSED */
 
11939
enum_nested_loop_state
 
11940
end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
11941
               bool end_of_records)
3806
11942
{
3807
11943
  int idx= -1;
3808
11944
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
3810
11946
  if (!join->first_record || end_of_records ||
3811
11947
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3812
11948
  {
3813
 
    if (join->first_record ||
 
11949
    if (join->first_record || 
3814
11950
        (end_of_records && !join->group && !join->group_optimized_away))
3815
11951
    {
3816
11952
      if (idx < (int) join->send_group_parts)
3817
11953
      {
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();
 
11954
        int error=0;
 
11955
        {
 
11956
          if (!join->first_record)
 
11957
          {
 
11958
            List_iterator_fast<Item> it(*join->fields);
 
11959
            Item *item;
 
11960
            /* No matching rows for group function */
 
11961
            join->clear();
3826
11962
 
3827
11963
            while ((item= it++))
3828
11964
              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;
 
11965
          }
 
11966
          if (join->having && join->having->val_int() == 0)
 
11967
            error= -1;                          // Didn't satisfy having
 
11968
          else
 
11969
          {
 
11970
            if (join->do_send_rows)
 
11971
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
11972
            join->send_records++;
 
11973
          }
 
11974
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
11975
          {
 
11976
            if (join->rollup_send_data((uint) (idx+1)))
 
11977
              error= 1;
 
11978
          }
 
11979
        }
 
11980
        if (error > 0)
 
11981
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
 
11982
        if (end_of_records)
 
11983
          return(NESTED_LOOP_OK);
 
11984
        if (join->send_records >= join->unit->select_limit_cnt &&
 
11985
            join->do_send_rows)
 
11986
        {
 
11987
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
11988
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
11989
          join->do_send_rows=0;
 
11990
          join->unit->select_limit_cnt = HA_POS_ERROR;
3855
11991
        }
3856
11992
        else if (join->send_records >= join->fetch_limit)
3857
11993
        {
3870
12006
    else
3871
12007
    {
3872
12008
      if (end_of_records)
3873
 
        return(NESTED_LOOP_OK);
 
12009
        return(NESTED_LOOP_OK);
3874
12010
      join->first_record=1;
3875
12011
      test_if_item_cache_changed(join->group_fields);
3876
12012
    }
3882
12018
      */
3883
12019
      copy_fields(&join->tmp_table_param);
3884
12020
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3885
 
        return(NESTED_LOOP_ERROR);
 
12021
        return(NESTED_LOOP_ERROR);
3886
12022
      return(ok_code);
3887
12023
    }
3888
12024
  }
3891
12027
  return(NESTED_LOOP_OK);
3892
12028
}
3893
12029
 
3894
 
enum_nested_loop_state end_write_group(Join *join, JoinTable *, bool end_of_records)
 
12030
 
 
12031
        /* ARGSUSED */
 
12032
enum_nested_loop_state
 
12033
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12034
          bool end_of_records)
 
12035
{
 
12036
  Table *table=join->tmp_table;
 
12037
 
 
12038
  if (join->thd->killed)                        // Aborted by user
 
12039
  {
 
12040
    join->thd->send_kill_message();
 
12041
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12042
  }
 
12043
  if (!end_of_records)
 
12044
  {
 
12045
    copy_fields(&join->tmp_table_param);
 
12046
    copy_funcs(join->tmp_table_param.items_to_copy);
 
12047
#ifdef TO_BE_DELETED
 
12048
    if (!table->uniques)                        // If not unique handling
 
12049
    {
 
12050
      /* Copy null values from group to row */
 
12051
      order_st   *group;
 
12052
      for (group=table->group ; group ; group=group->next)
 
12053
      {
 
12054
        Item *item= *group->item;
 
12055
        if (item->maybe_null)
 
12056
        {
 
12057
          Field *field=item->get_tmp_table_field();
 
12058
          field->ptr[-1]= (unsigned char) (field->is_null() ? 1 : 0);
 
12059
        }
 
12060
      }
 
12061
    }
 
12062
#endif
 
12063
    if (!join->having || join->having->val_int())
 
12064
    {
 
12065
      int error;
 
12066
      join->found_records++;
 
12067
      if ((error=table->file->ha_write_row(table->record[0])))
 
12068
      {
 
12069
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
 
12070
          goto end;
 
12071
        if (create_myisam_from_heap(join->thd, table,
 
12072
                                    join->tmp_table_param.start_recinfo,
 
12073
                                    &join->tmp_table_param.recinfo,
 
12074
                                    error, 1))
 
12075
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
 
12076
        table->s->uniques=0;                    // To ensure rows are the same
 
12077
      }
 
12078
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
 
12079
          join->do_send_rows)
 
12080
      {
 
12081
        if (!(join->select_options & OPTION_FOUND_ROWS))
 
12082
          return(NESTED_LOOP_QUERY_LIMIT);
 
12083
        join->do_send_rows=0;
 
12084
        join->unit->select_limit_cnt = HA_POS_ERROR;
 
12085
        return(NESTED_LOOP_OK);
 
12086
      }
 
12087
    }
 
12088
  }
 
12089
end:
 
12090
  return(NESTED_LOOP_OK);
 
12091
}
 
12092
 
 
12093
/* ARGSUSED */
 
12094
/** Group by searching after group record and updating it if possible. */
 
12095
 
 
12096
static enum_nested_loop_state
 
12097
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12098
           bool end_of_records)
 
12099
{
 
12100
  Table *table=join->tmp_table;
 
12101
  order_st   *group;
 
12102
  int     error;
 
12103
 
 
12104
  if (end_of_records)
 
12105
    return(NESTED_LOOP_OK);
 
12106
  if (join->thd->killed)                        // Aborted by user
 
12107
  {
 
12108
    join->thd->send_kill_message();
 
12109
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12110
  }
 
12111
 
 
12112
  join->found_records++;
 
12113
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12114
  /* Make a key of group index */
 
12115
  for (group=table->group ; group ; group=group->next)
 
12116
  {
 
12117
    Item *item= *group->item;
 
12118
    item->save_org_in_field(group->field);
 
12119
    /* Store in the used key if the field was 0 */
 
12120
    if (item->maybe_null)
 
12121
      group->buff[-1]= (char) group->field->is_null();
 
12122
  }
 
12123
  if (!table->file->index_read_map(table->record[1],
 
12124
                                   join->tmp_table_param.group_buff,
 
12125
                                   HA_WHOLE_KEY,
 
12126
                                   HA_READ_KEY_EXACT))
 
12127
  {                                             /* Update old record */
 
12128
    restore_record(table,record[1]);
 
12129
    update_tmptable_sum_func(join->sum_funcs,table);
 
12130
    if ((error=table->file->ha_update_row(table->record[1],
 
12131
                                          table->record[0])))
 
12132
    {
 
12133
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12134
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12135
    }
 
12136
    return(NESTED_LOOP_OK);
 
12137
  }
 
12138
 
 
12139
  /*
 
12140
    Copy null bits from group key to table
 
12141
    We can't copy all data as the key may have different format
 
12142
    as the row data (for example as with VARCHAR keys)
 
12143
  */
 
12144
  KEY_PART_INFO *key_part;
 
12145
  for (group=table->group,key_part=table->key_info[0].key_part;
 
12146
       group ;
 
12147
       group=group->next,key_part++)
 
12148
  {
 
12149
    if (key_part->null_bit)
 
12150
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
 
12151
  }
 
12152
  init_tmptable_sum_functions(join->sum_funcs);
 
12153
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12154
  if ((error=table->file->ha_write_row(table->record[0])))
 
12155
  {
 
12156
    if (create_myisam_from_heap(join->thd, table,
 
12157
                                join->tmp_table_param.start_recinfo,
 
12158
                                &join->tmp_table_param.recinfo,
 
12159
                                error, 0))
 
12160
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
 
12161
    /* Change method to update rows */
 
12162
    table->file->ha_index_init(0, 0);
 
12163
    join->join_tab[join->tables-1].next_select=end_unique_update;
 
12164
  }
 
12165
  join->send_records++;
 
12166
  return(NESTED_LOOP_OK);
 
12167
}
 
12168
 
 
12169
 
 
12170
/** Like end_update, but this is done with unique constraints instead of keys.  */
 
12171
 
 
12172
static enum_nested_loop_state
 
12173
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12174
                  bool end_of_records)
 
12175
{
 
12176
  Table *table=join->tmp_table;
 
12177
  int     error;
 
12178
 
 
12179
  if (end_of_records)
 
12180
    return(NESTED_LOOP_OK);
 
12181
  if (join->thd->killed)                        // Aborted by user
 
12182
  {
 
12183
    join->thd->send_kill_message();
 
12184
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12185
  }
 
12186
 
 
12187
  init_tmptable_sum_functions(join->sum_funcs);
 
12188
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12189
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12190
 
 
12191
  if (!(error=table->file->ha_write_row(table->record[0])))
 
12192
    join->send_records++;                       // New group
 
12193
  else
 
12194
  {
 
12195
    if ((int) table->file->get_dup_key(error) < 0)
 
12196
    {
 
12197
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12198
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12199
    }
 
12200
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
12201
    {
 
12202
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12203
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12204
    }
 
12205
    restore_record(table,record[1]);
 
12206
    update_tmptable_sum_func(join->sum_funcs,table);
 
12207
    if ((error=table->file->ha_update_row(table->record[1],
 
12208
                                          table->record[0])))
 
12209
    {
 
12210
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12211
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12212
    }
 
12213
  }
 
12214
  return(NESTED_LOOP_OK);
 
12215
}
 
12216
 
 
12217
 
 
12218
        /* ARGSUSED */
 
12219
enum_nested_loop_state
 
12220
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12221
                bool end_of_records)
3895
12222
{
3896
12223
  Table *table=join->tmp_table;
3897
12224
  int     idx= -1;
3898
12225
 
3899
 
  if (join->session->getKilled())
 
12226
  if (join->thd->killed)
3900
12227
  {                                             // Aborted by user
3901
 
    join->session->send_kill_message();
3902
 
    return NESTED_LOOP_KILLED;
 
12228
    join->thd->send_kill_message();
 
12229
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
3903
12230
  }
3904
12231
  if (!join->first_record || end_of_records ||
3905
12232
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3909
12236
      int send_group_parts= join->send_group_parts;
3910
12237
      if (idx < send_group_parts)
3911
12238
      {
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;
 
12239
        if (!join->first_record)
 
12240
        {
 
12241
          /* No matching rows for group function */
 
12242
          join->clear();
 
12243
        }
 
12244
        copy_sum_funcs(join->sum_funcs,
 
12245
                       join->sum_funcs_end[send_group_parts]);
 
12246
        if (!join->having || join->having->val_int())
 
12247
        {
 
12248
          int error= table->file->ha_write_row(table->record[0]);
 
12249
          if (error && create_myisam_from_heap(join->thd, table,
 
12250
                                               join->tmp_table_param.start_recinfo,
 
12251
                                                &join->tmp_table_param.recinfo,
 
12252
                                               error, 0))
 
12253
            return(NESTED_LOOP_ERROR);
 
12254
        }
 
12255
        if (join->rollup.state != ROLLUP::STATE_NONE)
 
12256
        {
 
12257
          if (join->rollup_write_data((uint) (idx+1), table))
 
12258
            return(NESTED_LOOP_ERROR);
 
12259
        }
 
12260
        if (end_of_records)
 
12261
          return(NESTED_LOOP_OK);
3935
12262
      }
3936
12263
    }
3937
12264
    else
3938
12265
    {
3939
12266
      if (end_of_records)
3940
 
        return NESTED_LOOP_OK;
 
12267
        return(NESTED_LOOP_OK);
3941
12268
      join->first_record=1;
3942
12269
      test_if_item_cache_changed(join->group_fields);
3943
12270
    }
3944
12271
    if (idx < (int) join->send_group_parts)
3945
12272
    {
3946
12273
      copy_fields(&join->tmp_table_param);
3947
 
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3948
 
        return NESTED_LOOP_ERROR;
 
12274
      copy_funcs(join->tmp_table_param.items_to_copy);
3949
12275
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3950
 
        return NESTED_LOOP_ERROR;
3951
 
      return NESTED_LOOP_OK;
 
12276
        return(NESTED_LOOP_ERROR);
 
12277
      return(NESTED_LOOP_OK);
3952
12278
    }
3953
12279
  }
3954
12280
  if (update_sum_func(join->sum_funcs))
3955
 
    return NESTED_LOOP_ERROR;
3956
 
  return NESTED_LOOP_OK;
 
12281
    return(NESTED_LOOP_ERROR);
 
12282
  return(NESTED_LOOP_OK);
3957
12283
}
3958
12284
 
 
12285
 
3959
12286
/*****************************************************************************
3960
12287
  Remove calculation with tables that aren't yet read. Remove also tests
3961
12288
  against fields that are read through key where the table is not a
3962
12289
  outer join table.
3963
12290
  We can't remove tests that are made against columns which are stored
3964
12291
  in sorted order.
 
12292
*****************************************************************************/
 
12293
 
 
12294
/**
3965
12295
  @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)
 
12296
    1 if right_item is used removable reference key on left_item
 
12297
*/
 
12298
 
 
12299
static bool test_if_ref(Item_field *left_item,Item *right_item)
3970
12300
{
3971
12301
  Field *field=left_item->field;
3972
12302
  // 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)
 
12303
  if (!field->table->const_table && !field->table->maybe_null)
3974
12304
  {
3975
 
    Item *ref_item=part_of_refkey(field->getTable(),field);
 
12305
    Item *ref_item=part_of_refkey(field->table,field);
3976
12306
    if (ref_item && ref_item->eq(right_item,1))
3977
12307
    {
3978
12308
      right_item= right_item->real_item();
3979
12309
      if (right_item->type() == Item::FIELD_ITEM)
3980
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
12310
        return (field->eq_def(((Item_field *) right_item)->field));
3981
12311
      /* remove equalities injected by IN->EXISTS transformation */
3982
12312
      else if (right_item->type() == Item::CACHE_ITEM)
3983
12313
        return ((Item_cache *)right_item)->eq_def (field);
3984
12314
      if (right_item->const_item() && !(right_item->is_null()))
3985
12315
      {
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
 
12316
        /*
 
12317
          We can remove binary fields and numerical fields except float,
 
12318
          as float comparison isn't 100 % secure
 
12319
          We have to keep normal strings to be able to check for end spaces
3990
12320
 
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
 
        }
 
12321
          sergefp: the above seems to be too restrictive. Counterexample:
 
12322
            create table t100 (v varchar(10), key(v)) default charset=latin1;
 
12323
            insert into t100 values ('a'),('a ');
 
12324
            explain select * from t100 where v='a';
 
12325
          The EXPLAIN shows 'using Where'. Running the query returns both
 
12326
          rows, so it seems there are no problems with endspace in the most
 
12327
          frequent case?
 
12328
        */
 
12329
        if (field->binary() &&
 
12330
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
 
12331
            field->decimals() == 0)
 
12332
        {
 
12333
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
12334
        }
4005
12335
      }
4006
12336
    }
4007
12337
  }
4008
 
  return 0;
 
12338
  return 0;                                     // keep test
 
12339
}
 
12340
 
 
12341
/**
 
12342
   @brief Replaces an expression destructively inside the expression tree of
 
12343
   the WHERE clase.
 
12344
 
 
12345
   @note Because of current requirements for semijoin flattening, we do not
 
12346
   need to recurse here, hence this function will only examine the top-level
 
12347
   AND conditions. (see JOIN::prepare, comment above the line 
 
12348
   'if (do_materialize)'
 
12349
   
 
12350
   @param join The top-level query.
 
12351
   @param old_cond The expression to be replaced.
 
12352
   @param new_cond The expression to be substituted.
 
12353
   @param do_fix_fields If true, Item::fix_fields(THD*, Item**) is called for
 
12354
   the new expression.
 
12355
   @return <code>true</code> if there was an error, <code>false</code> if
 
12356
   successful.
 
12357
*/
 
12358
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
12359
                                       Item *new_cond, bool do_fix_fields)
 
12360
{
 
12361
  if (join->conds == old_cond) {
 
12362
    join->conds= new_cond;
 
12363
    if (do_fix_fields)
 
12364
      new_cond->fix_fields(join->thd, &join->conds);
 
12365
    return false;
 
12366
  }
 
12367
  
 
12368
  if (join->conds->type() == Item::COND_ITEM) {
 
12369
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
 
12370
    Item *item;
 
12371
    while ((item= li++))
 
12372
      if (item == old_cond) 
 
12373
      {
 
12374
        li.replace(new_cond);
 
12375
        if (do_fix_fields)
 
12376
          new_cond->fix_fields(join->thd, li.ref());
 
12377
        return false;
 
12378
      }
 
12379
  }
 
12380
 
 
12381
  return true;
4009
12382
}
4010
12383
 
4011
12384
/*
4012
12385
  Extract a condition that can be checked after reading given table
4013
 
 
 
12386
  
4014
12387
  SYNOPSIS
4015
12388
    make_cond_for_table()
4016
12389
      cond         Condition to analyze
4017
12390
      tables       Tables for which "current field values" are available
4018
 
      used_table   Table that we're extracting the condition for (may
 
12391
      used_table   Table that we're extracting the condition for (may 
4019
12392
                   also include PSEUDO_TABLE_BITS
4020
12393
 
4021
12394
  DESCRIPTION
4025
12398
 
4026
12399
    The function assumes that
4027
12400
      - Constant parts of the condition has already been checked.
4028
 
      - Condition that could be checked for tables in 'tables' has already
 
12401
      - Condition that could be checked for tables in 'tables' has already 
4029
12402
        been checked.
4030
 
 
 
12403
        
4031
12404
    The function takes into account that some parts of the condition are
4032
12405
    guaranteed to be true by employed 'ref' access methods (the code that
4033
12406
    does this is located at the end, search down for "EQ_FUNC").
4034
12407
 
4035
12408
 
4036
 
  SEE ALSO
 
12409
  SEE ALSO 
4037
12410
    make_cond_for_info_schema uses similar algorithm
4038
12411
 
4039
12412
  RETURN
4040
12413
    Extracted condition
4041
12414
*/
4042
 
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
 
12415
 
 
12416
static COND *
 
12417
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
 
12418
                    bool exclude_expensive_cond)
4043
12419
{
4044
12420
  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()))
 
12421
      /*
 
12422
        Exclude constant conditions not checked at optimization time if
 
12423
        the table we are pushing conditions to is the first one.
 
12424
        As a result, such conditions are not considered as already checked
 
12425
        and will be checked at execution time, attached to the first table.
 
12426
      */
 
12427
      !((used_table & 1) && cond->is_expensive()))
4052
12428
    return (COND*) 0;                           // Already checked
4053
12429
  if (cond->type() == Item::COND_ITEM)
4054
12430
  {
4057
12433
      /* Create new top level AND item */
4058
12434
      Item_cond_and *new_cond=new Item_cond_and;
4059
12435
      if (!new_cond)
4060
 
        return (COND*) 0;
4061
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
12436
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
12437
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4062
12438
      Item *item;
4063
12439
      while ((item=li++))
4064
12440
      {
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);
 
12441
        Item *fix=make_cond_for_table(item,tables,used_table,
 
12442
                                      exclude_expensive_cond);
 
12443
        if (fix)
 
12444
          new_cond->argument_list()->push_back(fix);
4069
12445
      }
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;
 
12446
      switch (new_cond->argument_list()->elements) {
 
12447
      case 0:
 
12448
        return (COND*) 0;                       // Always true
 
12449
      case 1:
 
12450
        return new_cond->argument_list()->head();
 
12451
      default:
 
12452
        /*
 
12453
          Item_cond_and do not need fix_fields for execution, its parameters
 
12454
          are fixed or do not need fix_fields, too
 
12455
        */
 
12456
        new_cond->quick_fix_field();
 
12457
        new_cond->used_tables_cache=
 
12458
          ((Item_cond_and*) cond)->used_tables_cache &
 
12459
          tables;
 
12460
        return new_cond;
4084
12461
      }
4085
12462
    }
4086
12463
    else
4087
12464
    {                                           // Or list
4088
12465
      Item_cond_or *new_cond=new Item_cond_or;
4089
12466
      if (!new_cond)
4090
 
        return (COND*) 0;
4091
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
12467
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
12468
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4092
12469
      Item *item;
4093
12470
      while ((item=li++))
4094
12471
      {
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);
 
12472
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
12473
        if (!fix)
 
12474
          return (COND*) 0;                     // Always true
 
12475
        new_cond->argument_list()->push_back(fix);
4099
12476
      }
4100
12477
      /*
4101
 
        Item_cond_and do not need fix_fields for execution, its parameters
4102
 
        are fixed or do not need fix_fields, too
 
12478
        Item_cond_and do not need fix_fields for execution, its parameters
 
12479
        are fixed or do not need fix_fields, too
4103
12480
      */
4104
12481
      new_cond->quick_fix_field();
4105
12482
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4124
12501
  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
4125
12502
    return cond;                                // Not boolean op
4126
12503
 
4127
 
  /*
 
12504
  /* 
4128
12505
    Remove equalities that are guaranteed to be true by use of 'ref' access
4129
12506
    method
4130
12507
  */
4132
12509
  {
4133
12510
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
4134
12511
    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))
 
12512
    if (left_item->type() == Item::FIELD_ITEM &&
 
12513
        test_if_ref((Item_field*) left_item,right_item))
4136
12514
    {
4137
12515
      cond->marker=3;                   // Checked when read
4138
12516
      return (COND*) 0;
4139
12517
    }
4140
 
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
 
12518
    if (right_item->type() == Item::FIELD_ITEM &&
 
12519
        test_if_ref((Item_field*) right_item,left_item))
4141
12520
    {
4142
12521
      cond->marker=3;                   // Checked when read
4143
12522
      return (COND*) 0;
4147
12526
  return cond;
4148
12527
}
4149
12528
 
4150
 
static Item *part_of_refkey(Table *table,Field *field)
 
12529
 
 
12530
static Item *
 
12531
part_of_refkey(Table *table,Field *field)
4151
12532
{
4152
12533
  if (!table->reginfo.join_tab)
4153
12534
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
4155
12536
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
4156
12537
  if (ref_parts)
4157
12538
  {
4158
 
    KeyPartInfo *key_part=
 
12539
    KEY_PART_INFO *key_part=
4159
12540
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
4160
12541
    uint32_t part;
4161
12542
 
4166
12547
    }
4167
12548
 
4168
12549
    for (part=0 ; part < ref_parts ; part++,key_part++)
4169
 
    {
4170
12550
      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
 
      {
 
12551
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
4176
12552
        return table->reginfo.join_tab->ref.items[part];
4177
 
      }
4178
 
    }
4179
12553
  }
4180
12554
  return (Item*) 0;
4181
12555
}
4182
12556
 
 
12557
 
4183
12558
/**
4184
12559
  Test if one can use the key to resolve order_st BY.
4185
12560
 
4200
12575
  @retval
4201
12576
    -1   Reverse key can be used
4202
12577
*/
4203
 
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
12578
 
 
12579
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx,
 
12580
                                uint32_t *used_key_parts)
4204
12581
{
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;
 
12582
  KEY_PART_INFO *key_part,*key_part_end;
 
12583
  key_part=table->key_info[idx].key_part;
 
12584
  key_part_end=key_part+table->key_info[idx].key_parts;
4209
12585
  key_part_map const_key_parts=table->const_key_parts[idx];
4210
 
  int reverse= 0;
 
12586
  int reverse=0;
4211
12587
  bool on_primary_key= false;
4212
12588
 
4213
12589
  for (; order ; order=order->next, const_key_parts>>=1)
4217
12593
 
4218
12594
    /*
4219
12595
      Skip key parts that are constants in the WHERE clause.
4220
 
      These are already skipped in the ORDER BY by const_expression_in_where()
 
12596
      These are already skipped in the order_st BY by const_expression_in_where()
4221
12597
    */
4222
12598
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
4223
 
      key_part++;
 
12599
      key_part++; 
4224
12600
 
4225
12601
    if (key_part == key_part_end)
4226
12602
    {
4227
 
      /*
 
12603
      /* 
4228
12604
        We are at the end of the key. Check if the engine has the primary
4229
12605
        key as a suffix to the secondary keys. If it has continue to check
4230
12606
        the primary key as a suffix.
4231
12607
      */
4232
12608
      if (!on_primary_key &&
4233
 
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4234
 
          table->getShare()->hasPrimaryKey())
 
12609
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
 
12610
          table->s->primary_key != MAX_KEY)
4235
12611
      {
4236
12612
        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()];
 
12613
        key_part= table->key_info[table->s->primary_key].key_part;
 
12614
        key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
 
12615
        const_key_parts=table->const_key_parts[table->s->primary_key];
4240
12616
 
4241
12617
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4242
 
          key_part++;
 
12618
          key_part++; 
4243
12619
        /*
4244
12620
         The primary and secondary key parts were all const (i.e. there's
4245
12621
         one row).  The sorting doesn't matter.
4263
12639
    key_part++;
4264
12640
  }
4265
12641
  *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) &
 
12642
    (uint) (key_part - table->key_info[idx].key_part);
 
12643
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
4268
12644
                         HA_READ_PREV))
4269
12645
    reverse= 0;                                 // Index can't be used
4270
12646
  return(reverse);
4271
12647
}
4272
12648
 
 
12649
 
4273
12650
/**
4274
12651
  Test if a second key is the subkey of the first one.
4275
12652
 
4285
12662
  @retval
4286
12663
    0   no sub key
4287
12664
*/
4288
 
inline bool is_subkey(KeyPartInfo *key_part,
4289
 
                      KeyPartInfo *ref_key_part,
4290
 
                      KeyPartInfo *ref_key_part_end)
 
12665
 
 
12666
inline bool 
 
12667
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
 
12668
          KEY_PART_INFO *ref_key_part_end)
4291
12669
{
4292
12670
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4293
 
    if (! key_part->field->eq(ref_key_part->field))
 
12671
    if (!key_part->field->eq(ref_key_part->field))
4294
12672
      return 0;
4295
12673
  return 1;
4296
12674
}
4306
12684
    - MAX_KEY                   If we can't use other key
4307
12685
    - the number of found key   Otherwise
4308
12686
*/
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)
 
12687
 
 
12688
static uint
 
12689
test_if_subkey(order_st *order, Table *table, uint32_t ref, uint32_t ref_key_parts,
 
12690
               const key_map *usable_keys)
4314
12691
{
4315
12692
  uint32_t nr;
4316
12693
  uint32_t min_length= UINT32_MAX;
4317
12694
  uint32_t best= MAX_KEY;
4318
12695
  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;
 
12696
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
 
12697
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
4321
12698
 
4322
 
  for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
 
12699
  for (nr= 0 ; nr < table->s->keys ; nr++)
4323
12700
  {
4324
 
    if (usable_keys->test(nr) &&
 
12701
    if (usable_keys->is_set(nr) &&
4325
12702
        table->key_info[nr].key_length < min_length &&
4326
12703
        table->key_info[nr].key_parts >= ref_key_parts &&
4327
12704
        is_subkey(table->key_info[nr].key_part, ref_key_part,
4335
12712
  return best;
4336
12713
}
4337
12714
 
 
12715
 
4338
12716
/**
4339
12717
  Check if GROUP BY/DISTINCT can be optimized away because the set is
4340
12718
  already known to be distinct.
4353
12731
    of the table are referenced by a list : either the select list
4354
12732
    through find_field_in_item_list or GROUP BY list through
4355
12733
    find_field_in_order_list.
4356
 
    If the above holds and the key parts cannot contain NULLs then we
 
12734
    If the above holds and the key parts cannot contain NULLs then we 
4357
12735
    can safely remove the GROUP BY/DISTINCT,
4358
12736
    as no result set can be more distinct than an unique key.
4359
12737
 
4366
12744
  @retval
4367
12745
    0                    not found.
4368
12746
*/
4369
 
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
 
12747
 
 
12748
static bool
 
12749
list_contains_unique_index(Table *table,
 
12750
                          bool (*find_func) (Field *, void *), void *data)
4370
12751
{
4371
 
  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
 
12752
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
4372
12753
  {
4373
 
    if (keynr == table->getShare()->getPrimaryKey() ||
 
12754
    if (keynr == table->s->primary_key ||
4374
12755
         (table->key_info[keynr].flags & HA_NOSAME))
4375
12756
    {
4376
 
      KeyInfo *keyinfo= table->key_info + keynr;
4377
 
      KeyPartInfo *key_part= NULL;
4378
 
      KeyPartInfo *key_part_end= NULL;
 
12757
      KEY *keyinfo= table->key_info + keynr;
 
12758
      KEY_PART_INFO *key_part, *key_part_end;
4379
12759
 
4380
12760
      for (key_part=keyinfo->key_part,
4381
12761
           key_part_end=key_part+ keyinfo->key_parts;
4382
12762
           key_part < key_part_end;
4383
12763
           key_part++)
4384
12764
      {
4385
 
        if (key_part->field->maybe_null() ||
4386
 
            ! find_func(key_part->field, data))
 
12765
        if (key_part->field->maybe_null() || 
 
12766
            !find_func(key_part->field, data))
4387
12767
          break;
4388
12768
      }
4389
12769
      if (key_part == key_part_end)
4393
12773
  return 0;
4394
12774
}
4395
12775
 
 
12776
 
4396
12777
/**
4397
12778
  Helper function for list_contains_unique_index.
4398
12779
  Find a field reference in a list of order_st structures.
4406
12787
  @retval
4407
12788
    0                    not found.
4408
12789
*/
4409
 
bool find_field_in_order_list (Field *field, void *data)
 
12790
 
 
12791
static bool
 
12792
find_field_in_order_list (Field *field, void *data)
4410
12793
{
4411
 
  Order *group= (Order *) data;
 
12794
  order_st *group= (order_st *) data;
4412
12795
  bool part_found= 0;
4413
 
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
12796
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4414
12797
  {
4415
12798
    Item *item= (*tmp_group->item)->real_item();
4416
12799
    if (item->type() == Item::FIELD_ITEM &&
4423
12806
  return part_found;
4424
12807
}
4425
12808
 
 
12809
 
4426
12810
/**
4427
12811
  Helper function for list_contains_unique_index.
4428
12812
  Find a field reference in a dynamic list of Items.
4436
12820
  @retval
4437
12821
    0                    not found.
4438
12822
*/
4439
 
bool find_field_in_item_list (Field *field, void *data)
 
12823
 
 
12824
static bool
 
12825
find_field_in_item_list (Field *field, void *data)
4440
12826
{
4441
12827
  List<Item> *fields= (List<Item> *) data;
4442
12828
  bool part_found= 0;
4443
 
  List<Item>::iterator li(fields->begin());
 
12829
  List_iterator<Item> li(*fields);
4444
12830
  Item *item;
4445
12831
 
4446
12832
  while ((item= li++))
4455
12841
  return part_found;
4456
12842
}
4457
12843
 
 
12844
 
4458
12845
/**
4459
 
  Test if we can skip the ORDER BY by using an index.
 
12846
  Test if we can skip the order_st BY by using an index.
4460
12847
 
4461
12848
  SYNOPSIS
4462
12849
    test_if_skip_sort_order()
4466
12853
      no_changes
4467
12854
      map
4468
12855
 
4469
 
  If we can use an index, the JoinTable / tab->select struct
 
12856
  If we can use an index, the JOIN_TAB / tab->select struct
4470
12857
  is changed to use the index.
4471
12858
 
4472
12859
  The index must cover all fields in <order>, or it will not be considered.
4473
12860
 
4474
12861
  @todo
4475
 
    - sergeyp: Results of all index merge selects actually are ordered
 
12862
    - sergeyp: Results of all index merge selects actually are ordered 
4476
12863
    by clustered PK values.
4477
12864
 
4478
12865
  @retval
4480
12867
  @retval
4481
12868
    1    We can use an index.
4482
12869
*/
4483
 
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
12870
 
 
12871
static bool
 
12872
test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,ha_rows select_limit,
 
12873
                        bool no_changes, const key_map *map)
4484
12874
{
4485
12875
  int32_t ref_key;
4486
12876
  uint32_t ref_key_parts;
4487
12877
  int order_direction;
4488
12878
  uint32_t used_key_parts;
4489
12879
  Table *table=tab->table;
4490
 
  optimizer::SqlSelect *select= tab->select;
 
12880
  SQL_SELECT *select=tab->select;
4491
12881
  key_map usable_keys;
4492
 
  optimizer::QuickSelectInterface *save_quick= NULL;
 
12882
  QUICK_SELECT_I *save_quick= 0;
4493
12883
 
4494
12884
  /*
4495
12885
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
4497
12887
  */
4498
12888
  usable_keys= *map;
4499
12889
 
4500
 
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
12890
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4501
12891
  {
4502
12892
    Item *item= (*tmp_order->item)->real_item();
4503
12893
    if (item->type() != Item::FIELD_ITEM)
4504
12894
    {
4505
 
      usable_keys.reset();
 
12895
      usable_keys.clear_all();
4506
12896
      return(0);
4507
12897
    }
4508
 
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
4509
 
    if (usable_keys.none())
 
12898
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
 
12899
    if (usable_keys.is_clear_all())
4510
12900
      return(0);                                        // No usable keys
4511
12901
  }
4512
12902
 
4516
12906
  {
4517
12907
    ref_key=       tab->ref.key;
4518
12908
    ref_key_parts= tab->ref.key_parts;
4519
 
    if (tab->type == AM_REF_OR_NULL)
 
12909
    if (tab->type == JT_REF_OR_NULL)
4520
12910
      return(0);
4521
12911
  }
4522
 
  else if (select && select->quick)             // Range found by optimizer/range
 
12912
  else if (select && select->quick)             // Range found by opt_range
4523
12913
  {
4524
12914
    int quick_type= select->quick->get_type();
4525
12915
    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
 
12916
    /* 
 
12917
      assume results are not ordered when index merge is used 
 
12918
      TODO: sergeyp: Results of all index merge selects actually are ordered 
4529
12919
      by clustered PK values.
4530
12920
    */
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)
 
12921
  
 
12922
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || 
 
12923
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
 
12924
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
4535
12925
      return(0);
4536
12926
    ref_key=       select->quick->index;
4537
12927
    ref_key_parts= select->quick->used_key_parts;
4542
12932
    /*
4543
12933
      We come here when there is a REF key.
4544
12934
    */
4545
 
    if (! usable_keys.test(ref_key))
 
12935
    if (!usable_keys.is_set(ref_key))
4546
12936
    {
4547
12937
      /*
4548
 
        We come here when ref_key is not among usable_keys
 
12938
        We come here when ref_key is not among usable_keys
4549
12939
      */
4550
12940
      uint32_t new_ref_key;
4551
12941
      /*
4552
 
        If using index only read, only consider other possible index only
4553
 
        keys
 
12942
        If using index only read, only consider other possible index only
 
12943
        keys
4554
12944
      */
4555
 
      if (table->covering_keys.test(ref_key))
4556
 
        usable_keys&= table->covering_keys;
 
12945
      if (table->covering_keys.is_set(ref_key))
 
12946
        usable_keys.intersect(table->covering_keys);
4557
12947
      if (tab->pre_idx_push_select_cond)
4558
12948
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
4559
12949
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
4560
12950
                                       &usable_keys)) < MAX_KEY)
4561
12951
      {
4562
 
        /* Found key that can be used to retrieve data in sorted order */
4563
 
        if (tab->ref.key >= 0)
4564
 
        {
 
12952
        /* Found key that can be used to retrieve data in sorted order */
 
12953
        if (tab->ref.key >= 0)
 
12954
        {
4565
12955
          /*
4566
 
            We'll use ref access method on key new_ref_key. In general case
 
12956
            We'll use ref access method on key new_ref_key. In general case 
4567
12957
            the index search tuple for new_ref_key will be different (e.g.
4568
12958
            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".
 
12959
            (part1, part2(N), ...) and the WHERE clause contains 
 
12960
            "part1 = const1 AND part2=const2". 
4571
12961
            So we build tab->ref from scratch here.
4572
12962
          */
4573
 
          optimizer::KeyUse *keyuse= tab->keyuse;
4574
 
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
 
12963
          KEYUSE *keyuse= tab->keyuse;
 
12964
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
4575
12965
            keyuse++;
4576
12966
 
4577
 
          if (create_ref_for_key(tab->join, tab, keyuse,
 
12967
          if (create_ref_for_key(tab->join, tab, keyuse, 
4578
12968
                                 tab->join->const_table_map))
4579
12969
            return(0);
4580
 
        }
4581
 
        else
4582
 
        {
 
12970
        }
 
12971
        else
 
12972
        {
4583
12973
          /*
4584
 
            The range optimizer constructed QuickRange for ref_key, and
 
12974
            The range optimizer constructed QUICK_RANGE for ref_key, and
4585
12975
            we want to use instead new_ref_key as the index. We can't
4586
12976
            just change the index of the quick select, because this may
4587
12977
            result in an incosistent QUICK_SELECT object. Below we
4589
12979
            parameres are set correctly by the range optimizer.
4590
12980
           */
4591
12981
          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.
 
12982
          new_ref_key_map.clear_all();  // Force the creation of quick select
 
12983
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
4594
12984
 
4595
 
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
 
12985
          if (select->test_quick_select(tab->join->thd, new_ref_key_map, 0,
4596
12986
                                        (tab->join->select_options &
4597
12987
                                         OPTION_FOUND_ROWS) ?
4598
12988
                                        HA_POS_ERROR :
4600
12990
                                        true) <=
4601
12991
              0)
4602
12992
            return(0);
4603
 
        }
 
12993
        }
4604
12994
        ref_key= new_ref_key;
4605
12995
      }
4606
12996
    }
4607
12997
    /* Check if we get the rows in requested sorted order by using the key */
4608
 
    if (usable_keys.test(ref_key) &&
 
12998
    if (usable_keys.is_set(ref_key) &&
4609
12999
        (order_direction= test_if_order_by_key(order,table,ref_key,
4610
13000
                                               &used_key_parts)))
4611
13001
      goto check_reverse_order;
4626
13016
    int best_key= -1;
4627
13017
    bool is_best_covering= false;
4628
13018
    double fanout= 1;
4629
 
    Join *join= tab->join;
 
13019
    JOIN *join= tab->join;
4630
13020
    uint32_t tablenr= tab - join->join_tab;
4631
 
    ha_rows table_records= table->cursor->stats.records;
 
13021
    ha_rows table_records= table->file->stats.records;
4632
13022
    bool group= join->group && order == join->group_list;
4633
 
    optimizer::Position cur_pos;
4634
13023
 
4635
13024
    /*
4636
13025
      If not used with LIMIT, only use keys if the whole query can be
4639
13028
    */
4640
13029
    if (select_limit >= table_records)
4641
13030
    {
4642
 
      /*
4643
 
        filesort() and join cache are usually faster than reading in
 
13031
      /* 
 
13032
        filesort() and join cache are usually faster than reading in 
4644
13033
        index order and not using join cache
4645
13034
        */
4646
 
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
 
13035
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
4647
13036
        return(0);
4648
 
      keys= *table->cursor->keys_to_use_for_scanning();
4649
 
      keys|= table->covering_keys;
 
13037
      keys= *table->file->keys_to_use_for_scanning();
 
13038
      keys.merge(table->covering_keys);
4650
13039
 
4651
13040
      /*
4652
 
        We are adding here also the index specified in FORCE INDEX clause,
4653
 
        if any.
 
13041
        We are adding here also the index specified in FORCE INDEX clause, 
 
13042
        if any.
4654
13043
        This is to allow users to use index in order_st BY.
4655
13044
      */
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;
 
13045
      if (table->force_index) 
 
13046
        keys.merge(group ? table->keys_in_use_for_group_by :
 
13047
                           table->keys_in_use_for_order_by);
 
13048
      keys.intersect(usable_keys);
4660
13049
    }
4661
13050
    else
4662
13051
      keys= usable_keys;
4663
13052
 
4664
 
    cur_pos= join->getPosFromOptimalPlan(tablenr);
4665
 
    read_time= cur_pos.getCost();
 
13053
    read_time= join->best_positions[tablenr].read_time;
4666
13054
    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
 
    }
 
13055
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
4671
13056
 
4672
 
    for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
 
13057
    for (nr=0; nr < table->s->keys ; nr++)
4673
13058
    {
4674
13059
      int direction;
4675
 
      if (keys.test(nr) &&
 
13060
      if (keys.is_set(nr) &&
4676
13061
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
4677
13062
      {
4678
 
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
4679
 
 
4680
 
        /*
4681
 
          Don't use an index scan with ORDER BY without limit.
 
13063
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
 
13064
        
 
13065
        /* 
 
13066
          Don't use an index scan with order_st BY without limit.
4682
13067
          For GROUP BY without limit always use index scan
4683
 
          if there is a suitable index.
 
13068
          if there is a suitable index. 
4684
13069
          Why we hold to this asymmetry hardly can be explained
4685
13070
          rationally. It's easy to demonstrate that using
4686
13071
          temporary table + filesort could be cheaper for grouping
4687
13072
          queries too.
4688
 
        */
 
13073
        */ 
4689
13074
        if (is_covering ||
4690
 
            select_limit != HA_POS_ERROR ||
 
13075
            select_limit != HA_POS_ERROR || 
4691
13076
            (ref_key < 0 && (group || table->force_index)))
4692
 
        {
 
13077
        { 
4693
13078
          double rec_per_key;
4694
13079
          double index_scan_time;
4695
 
          KeyInfo *keyinfo= tab->table->key_info+nr;
 
13080
          KEY *keyinfo= tab->table->key_info+nr;
4696
13081
          if (select_limit == HA_POS_ERROR)
4697
13082
            select_limit= table_records;
4698
13083
          if (group)
4699
13084
          {
4700
13085
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
4701
 
            set_if_bigger(rec_per_key, 1.0);
 
13086
            set_if_bigger(rec_per_key, 1);
4702
13087
            /*
4703
13088
              With a grouping query each group containing on average
4704
13089
              rec_per_key records produces only one row that will
4705
13090
              be included into the result set.
4706
 
            */
 
13091
            */  
4707
13092
            if (select_limit > table_records/rec_per_key)
4708
13093
                select_limit= table_records;
4709
13094
            else
4710
13095
              select_limit= (ha_rows) (select_limit*rec_per_key);
4711
13096
          }
4712
 
          /*
 
13097
          /* 
4713
13098
            If tab=tk is not the last joined table tn then to get first
4714
13099
            L records from the result set we can expect to retrieve
4715
13100
            only L/fanout(tk,tn) where fanout(tk,tn) says how many
4718
13103
            So the estimate for L/fanout(tk,tn) will be too optimistic
4719
13104
            and as result we'll choose an index scan when using ref/range
4720
13105
            access + filesort will be cheaper.
4721
 
          */
 
13106
          */
4722
13107
          select_limit= (ha_rows) (select_limit < fanout ?
4723
13108
                                   1 : select_limit/fanout);
4724
13109
          /*
4725
13110
            We assume that each of the tested indexes is not correlated
4726
13111
            with ref_key. Thus, to select first N records we have to scan
4727
 
            N/selectivity(ref_key) index entries.
 
13112
            N/selectivity(ref_key) index entries. 
4728
13113
            selectivity(ref_key) = #scanned_records/#table_records =
4729
13114
            table->quick_condition_rows/table_records.
4730
13115
            In any case we can't select more than #table_records.
4731
 
            N/(table->quick_condition_rows/table_records) > table_records
 
13116
            N/(table->quick_condition_rows/table_records) > table_records 
4732
13117
            <=> N > table->quick_condition_rows.
4733
 
          */
 
13118
          */ 
4734
13119
          if (select_limit > table->quick_condition_rows)
4735
13120
            select_limit= table_records;
4736
13121
          else
4738
13123
                                     (double) table_records /
4739
13124
                                      table->quick_condition_rows);
4740
13125
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
4741
 
          set_if_bigger(rec_per_key, 1.0);
 
13126
          set_if_bigger(rec_per_key, 1);
4742
13127
          /*
4743
13128
            Here we take into account the fact that rows are
4744
13129
            accessed in sequences rec_per_key records in each.
4745
13130
            Rows in such a sequence are supposed to be ordered
4746
13131
            by rowid/primary key. When reading the data
4747
13132
            in a sequence we'll touch not more pages than the
4748
 
            table cursor contains.
 
13133
            table file contains.
4749
13134
            TODO. Use the formula for a disk sweep sequential access
4750
 
            to calculate the cost of accessing data rows for one
 
13135
            to calculate the cost of accessing data rows for one 
4751
13136
            index entry.
4752
 
          */
 
13137
          */
4753
13138
          index_scan_time= select_limit/rec_per_key *
4754
 
                           min(rec_per_key, table->cursor->scan_time());
 
13139
                           cmin(rec_per_key, table->file->scan_time());
4755
13140
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
4756
13141
              index_scan_time < read_time)
4757
13142
          {
4758
13143
            ha_rows quick_records= table_records;
4759
13144
            if (is_best_covering && !is_covering)
4760
13145
              continue;
4761
 
            if (table->quick_keys.test(nr))
 
13146
            if (table->quick_keys.is_set(nr))
4762
13147
              quick_records= table->quick_rows[nr];
4763
13148
            if (best_key < 0 ||
4764
 
                (select_limit <= min(quick_records,best_records) ?
 
13149
                (select_limit <= cmin(quick_records,best_records) ?
4765
13150
                 keyinfo->key_parts < best_key_parts :
4766
13151
                 quick_records < best_records))
4767
13152
            {
4769
13154
              best_key_parts= keyinfo->key_parts;
4770
13155
              best_records= quick_records;
4771
13156
              is_best_covering= is_covering;
4772
 
              best_key_direction= direction;
 
13157
              best_key_direction= direction; 
4773
13158
            }
4774
 
          }
4775
 
        }
 
13159
          }   
 
13160
        }      
4776
13161
      }
4777
13162
    }
4778
13163
    if (best_key >= 0)
4779
13164
    {
4780
13165
      bool quick_created= false;
4781
 
      if (table->quick_keys.test(best_key) && best_key != ref_key)
 
13166
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
4782
13167
      {
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,
 
13168
        key_map map;
 
13169
        map.clear_all();       // Force the creation of quick select
 
13170
        map.set_bit(best_key); // only best_key.
 
13171
        quick_created=         
 
13172
          select->test_quick_select(join->thd, map, 0,
4788
13173
                                    join->select_options & OPTION_FOUND_ROWS ?
4789
13174
                                    HA_POS_ERROR :
4790
13175
                                    join->unit->select_limit_cnt,
4793
13178
      if (!no_changes)
4794
13179
      {
4795
13180
        if (!quick_created)
4796
 
        {
 
13181
        {
4797
13182
          tab->index= best_key;
4798
13183
          tab->read_first_record= best_key_direction > 0 ?
4799
13184
                                  join_read_first:join_read_last;
4800
 
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
 
13185
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
4801
13186
          if (select && select->quick)
4802
13187
          {
4803
 
            safe_delete(select->quick);
 
13188
            delete select->quick;
 
13189
            select->quick= 0;
4804
13190
          }
4805
 
          if (table->covering_keys.test(best_key))
 
13191
          if (table->covering_keys.is_set(best_key))
4806
13192
          {
4807
13193
            table->key_read=1;
4808
 
            table->cursor->extra(HA_EXTRA_KEYREAD);
 
13194
            table->file->extra(HA_EXTRA_KEYREAD);
4809
13195
          }
4810
 
          table->cursor->ha_index_or_rnd_end();
 
13196
          table->file->ha_index_or_rnd_end();
4811
13197
          if (join->select_options & SELECT_DESCRIBE)
4812
13198
          {
4813
13199
            tab->ref.key= -1;
4814
13200
            tab->ref.key_parts= 0;
4815
 
            if (select_limit < table_records)
 
13201
            if (select_limit < table_records) 
4816
13202
              tab->limit= select_limit;
4817
13203
          }
4818
13204
        }
4819
 
        else if (tab->type != AM_ALL)
 
13205
        else if (tab->type != JT_ALL)
4820
13206
        {
4821
13207
          /*
4822
13208
            We're about to use a quick access to the table.
4824
13210
            method is actually used.
4825
13211
          */
4826
13212
          assert(tab->select->quick);
4827
 
          tab->type= AM_ALL;
 
13213
          tab->type=JT_ALL;
4828
13214
          tab->use_quick=1;
4829
13215
          tab->ref.key= -1;
4830
13216
          tab->ref.key_parts=0;         // Don't use ref key.
4831
13217
          tab->read_first_record= join_init_read_record;
 
13218
          /*
 
13219
            TODO: update the number of records in join->best_positions[tablenr]
 
13220
          */
4832
13221
        }
4833
13222
      }
4834
13223
      used_key_parts= best_key_parts;
4835
13224
      order_direction= best_key_direction;
4836
13225
    }
4837
13226
    else
4838
 
      return(0);
4839
 
  }
 
13227
      return(0); 
 
13228
  } 
4840
13229
 
4841
 
check_reverse_order:
4842
 
  if (order_direction == -1)            // If ORDER BY ... DESC
 
13230
check_reverse_order:                  
 
13231
  if (order_direction == -1)            // If order_st BY ... DESC
4843
13232
  {
4844
13233
    if (select && select->quick)
4845
13234
    {
4846
13235
      /*
4847
 
        Don't reverse the sort order, if it's already done.
 
13236
        Don't reverse the sort order, if it's already done.
4848
13237
        (In some cases test_if_order_by_key() can be called multiple times
4849
13238
      */
4850
 
      if (! select->quick->reverse_sorted())
 
13239
      if (!select->quick->reverse_sorted())
4851
13240
      {
4852
 
        optimizer::QuickSelectDescending *tmp= NULL;
 
13241
        QUICK_SELECT_DESC *tmp;
4853
13242
        bool error= false;
4854
13243
        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)
4859
 
        {
4860
 
          tab->limit= 0;
4861
 
          select->quick= save_quick;
4862
 
          return 0; // Use filesort
4863
 
        }
4864
 
 
4865
 
        /* 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;
4872
 
          select->quick= save_quick;
4873
 
          tab->limit= 0;
4874
 
          return 0; // Reverse sort not supported
4875
 
        }
4876
 
        select->quick= tmp;
 
13244
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
 
13245
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
13246
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
13247
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
13248
        {
 
13249
          tab->limit= 0;
 
13250
          select->quick= save_quick;
 
13251
          return(0);                   // Use filesort
 
13252
        }
 
13253
            
 
13254
        /* order_st BY range_key DESC */
 
13255
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
 
13256
                                    used_key_parts, &error);
 
13257
        if (!tmp || error)
 
13258
        {
 
13259
          delete tmp;
 
13260
          select->quick= save_quick;
 
13261
          tab->limit= 0;
 
13262
          return(0);            // Reverse sort not supported
 
13263
        }
 
13264
        select->quick=tmp;
4877
13265
      }
4878
13266
    }
4879
 
    else if (tab->type != AM_NEXT &&
 
13267
    else if (tab->type != JT_NEXT && 
4880
13268
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
4881
13269
    {
4882
13270
      /*
4883
 
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
 
13271
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
4884
13272
 
4885
 
        Use a traversal function that starts by reading the last row
4886
 
        with key part (A) and then traverse the index backwards.
 
13273
        Use a traversal function that starts by reading the last row
 
13274
        with key part (A) and then traverse the index backwards.
4887
13275
      */
4888
13276
      tab->read_first_record= join_read_last_key;
4889
13277
      tab->read_record.read_record= join_read_prev_same;
4891
13279
  }
4892
13280
  else if (select && select->quick)
4893
13281
    select->quick->sorted= 1;
4894
 
  return 1;
 
13282
  return(1);
4895
13283
}
4896
13284
 
 
13285
 
4897
13286
/*
4898
13287
  If not selecting by given key, create an index how records should be read
4899
13288
 
4900
13289
  SYNOPSIS
4901
13290
   create_sort_index()
4902
 
     session            Thread Cursor
 
13291
     thd                Thread handler
4903
13292
     tab                Table to sort (in join structure)
4904
13293
     order              How table should be sorted
4905
13294
     filesort_limit     Max number of rows that needs to be sorted
4906
13295
     select_limit       Max number of rows in final output
4907
13296
                        Used to decide if we should use index or not
4908
13297
     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
 
13298
                        Used to decide if we should use index or not     
4910
13299
 
4911
13300
 
4912
13301
  IMPLEMENTATION
4913
13302
   - If there is an index that can be used, 'tab' is modified to use
4914
13303
     this index.
4915
 
   - If no index, create with filesort() an index cursor that can be used to
 
13304
   - If no index, create with filesort() an index file that can be used to
4916
13305
     retrieve rows in order (should be done with 'read_record').
4917
13306
     The sorted data is stored in tab->table and will be freed when calling
4918
 
     tab->table->free_io_cache().
 
13307
     free_io_cache(tab->table).
4919
13308
 
4920
13309
  RETURN VALUES
4921
13310
    0           ok
4922
13311
    -1          Some fatal error
4923
13312
    1           No records
4924
13313
*/
4925
 
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
13314
 
 
13315
static int
 
13316
create_sort_index(THD *thd, JOIN *join, order_st *order,
 
13317
                  ha_rows filesort_limit, ha_rows select_limit,
 
13318
                  bool is_order_by)
4926
13319
{
4927
13320
  uint32_t length= 0;
4928
13321
  ha_rows examined_rows;
4929
13322
  Table *table;
4930
 
  optimizer::SqlSelect *select= NULL;
4931
 
  JoinTable *tab;
 
13323
  SQL_SELECT *select;
 
13324
  JOIN_TAB *tab;
4932
13325
 
4933
13326
  if (join->tables == join->const_tables)
4934
13327
    return(0);                          // One row, no need to sort
4942
13335
    is going to be used as it is applied now only for one table queries
4943
13336
    with covering indexes.
4944
13337
  */
4945
 
  if ((order != join->group_list ||
 
13338
  if ((order != join->group_list || 
4946
13339
       !(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,
 
13340
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
 
13341
      test_if_skip_sort_order(tab,order,select_limit,0, 
4949
13342
                              is_order_by ?  &table->keys_in_use_for_order_by :
4950
13343
                              &table->keys_in_use_for_group_by))
4951
13344
    return(0);
4952
 
  for (Order *ord= join->order; ord; ord= ord->next)
 
13345
  for (order_st *ord= join->order; ord; ord= ord->next)
4953
13346
    length++;
4954
 
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
4955
 
  {
4956
 
    return(-1);
4957
 
  }
 
13347
  if (!(join->sortorder= 
 
13348
        make_unireg_sortorder(order, &length, join->sortorder)))
 
13349
    goto err;                           /* purecov: inspected */
4958
13350
 
4959
 
  table->sort.io_cache= new internal::IO_CACHE;
 
13351
  table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
 
13352
                                             MYF(MY_WME | MY_ZEROFILL));
4960
13353
  table->status=0;                              // May be wrong if quick_select
4961
13354
 
4962
13355
  // If table has a range, move it to select
4966
13359
    {
4967
13360
      select->quick=tab->quick;
4968
13361
      tab->quick=0;
4969
 
      /*
 
13362
      /* 
4970
13363
        We can only use 'Only index' if quick key is same as ref_key
4971
13364
        and in index_merge 'Only index' cannot be used
4972
13365
      */
4973
 
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
 
13366
      if (table->key_read && ((uint) tab->ref.key != select->quick->index))
4974
13367
      {
4975
 
        table->key_read=0;
4976
 
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13368
        table->key_read=0;
 
13369
        table->file->extra(HA_EXTRA_NO_KEYREAD);
4977
13370
      }
4978
13371
    }
4979
13372
    else
4980
13373
    {
4981
13374
      /*
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.
 
13375
        We have a ref on a const;  Change this to a range that filesort
 
13376
        can use.
 
13377
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
13378
        field, quick will contain an empty record set.
4986
13379
      */
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
 
      }
 
13380
      if (!(select->quick= (get_quick_select_for_ref(thd, table, &tab->ref, 
 
13381
                                                     tab->found_records))))
 
13382
        goto err;
4994
13383
    }
4995
13384
  }
4996
13385
 
4997
 
  if (table->getShare()->getType())
4998
 
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
 
13386
  /* Fill schema tables with data before filesort if it's necessary */
 
13387
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
13388
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
 
13389
    goto err;
4999
13390
 
5000
 
  FileSort filesort(*session);
5001
 
  table->sort.found_records=filesort.run(table,join->sortorder, length,
5002
 
                                         select, filesort_limit, 0,
5003
 
                                         examined_rows);
 
13391
  if (table->s->tmp_table)
 
13392
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
 
13393
  table->sort.found_records=filesort(thd, table,join->sortorder, length,
 
13394
                                     select, filesort_limit, 0,
 
13395
                                     &examined_rows);
5004
13396
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5005
13397
  if (select)
5006
13398
  {
5010
13402
  tab->select_cond=0;
5011
13403
  tab->last_inner= 0;
5012
13404
  tab->first_unmatched= 0;
5013
 
  tab->type= AM_ALL;                            // Read with normal read_record
 
13405
  tab->type=JT_ALL;                             // Read with normal read_record
5014
13406
  tab->read_first_record= join_init_read_record;
5015
13407
  tab->join->examined_rows+=examined_rows;
5016
13408
  if (table->key_read)                          // Restore if we used indexes
5017
13409
  {
5018
13410
    table->key_read=0;
5019
 
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13411
    table->file->extra(HA_EXTRA_NO_KEYREAD);
5020
13412
  }
5021
 
 
5022
13413
  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;
 
13414
err:
 
13415
  return(-1);
 
13416
}
 
13417
 
 
13418
static bool copy_blobs(Field **ptr)
 
13419
{
 
13420
  for (; *ptr ; ptr++)
 
13421
  {
 
13422
    if ((*ptr)->flags & BLOB_FLAG)
 
13423
      if (((Field_blob *) (*ptr))->copy())
 
13424
        return 1;                               // Error
 
13425
  }
 
13426
  return 0;
 
13427
}
 
13428
 
 
13429
static void free_blobs(Field **ptr)
 
13430
{
 
13431
  for (; *ptr ; ptr++)
 
13432
  {
 
13433
    if ((*ptr)->flags & BLOB_FLAG)
 
13434
      ((Field_blob *) (*ptr))->free();
 
13435
  }
 
13436
}
 
13437
 
 
13438
 
 
13439
static int
 
13440
remove_duplicates(JOIN *join, Table *entry,List<Item> &fields, Item *having)
 
13441
{
 
13442
  int error;
 
13443
  ulong reclength,offset;
 
13444
  uint32_t field_count;
 
13445
  THD *thd= join->thd;
 
13446
 
 
13447
  entry->reginfo.lock_type=TL_WRITE;
 
13448
 
 
13449
  /* Calculate how many saved fields there is in list */
 
13450
  field_count=0;
 
13451
  List_iterator<Item> it(fields);
 
13452
  Item *item;
 
13453
  while ((item=it++))
 
13454
  {
 
13455
    if (item->get_tmp_table_field() && ! item->const_item())
 
13456
      field_count++;
 
13457
  }
 
13458
 
 
13459
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) 
 
13460
  {                    // only const items with no OPTION_FOUND_ROWS
 
13461
    join->unit->select_limit_cnt= 1;            // Only send first row
 
13462
    return(0);
 
13463
  }
 
13464
  Field **first_field=entry->field+entry->s->fields - field_count;
 
13465
  offset= (field_count ? 
 
13466
           entry->field[entry->s->fields - field_count]->
 
13467
           offset(entry->record[0]) : 0);
 
13468
  reclength=entry->s->reclength-offset;
 
13469
 
 
13470
  free_io_cache(entry);                         // Safety
 
13471
  entry->file->info(HA_STATUS_VARIABLE);
 
13472
  if (entry->s->db_type() == heap_hton ||
 
13473
      (!entry->s->blob_fields &&
 
13474
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
 
13475
        thd->variables.sortbuff_size)))
 
13476
    error=remove_dup_with_hash_index(join->thd, entry,
 
13477
                                     field_count, first_field,
 
13478
                                     reclength, having);
 
13479
  else
 
13480
    error=remove_dup_with_compare(join->thd, entry, first_field, offset,
 
13481
                                  having);
 
13482
 
 
13483
  free_blobs(first_field);
 
13484
  return(error);
 
13485
}
 
13486
 
 
13487
 
 
13488
static int remove_dup_with_compare(THD *thd, Table *table, Field **first_field,
 
13489
                                   ulong offset, Item *having)
 
13490
{
 
13491
  handler *file=table->file;
5028
13492
  char *org_record,*new_record;
5029
13493
  unsigned char *record;
5030
13494
  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);
 
13495
  ulong reclength= table->s->reclength-offset;
 
13496
 
 
13497
  org_record=(char*) (record=table->record[0])+offset;
 
13498
  new_record=(char*) table->record[1]+offset;
 
13499
 
 
13500
  file->ha_rnd_init(1);
 
13501
  error=file->rnd_next(record);
5040
13502
  for (;;)
5041
13503
  {
5042
 
    if (session->getKilled())
 
13504
    if (thd->killed)
5043
13505
    {
5044
 
      session->send_kill_message();
 
13506
      thd->send_kill_message();
5045
13507
      error=0;
5046
13508
      goto err;
5047
13509
    }
5048
13510
    if (error)
5049
13511
    {
5050
13512
      if (error == HA_ERR_RECORD_DELETED)
5051
 
        continue;
 
13513
        continue;
5052
13514
      if (error == HA_ERR_END_OF_FILE)
5053
 
        break;
 
13515
        break;
5054
13516
      goto err;
5055
13517
    }
5056
13518
    if (having && !having->val_int())
5057
13519
    {
5058
 
      if ((error=cursor->deleteRecord(record)))
5059
 
        goto err;
5060
 
      error=cursor->rnd_next(record);
 
13520
      if ((error=file->ha_delete_row(record)))
 
13521
        goto err;
 
13522
      error=file->rnd_next(record);
5061
13523
      continue;
5062
13524
    }
5063
13525
    if (copy_blobs(first_field))
5068
13530
    }
5069
13531
    memcpy(new_record,org_record,reclength);
5070
13532
 
5071
 
    /* Read through rest of cursor and mark duplicated rows deleted */
 
13533
    /* Read through rest of file and mark duplicated rows deleted */
5072
13534
    bool found=0;
5073
13535
    for (;;)
5074
13536
    {
5075
 
      if ((error=cursor->rnd_next(record)))
 
13537
      if ((error=file->rnd_next(record)))
5076
13538
      {
5077
 
        if (error == HA_ERR_RECORD_DELETED)
5078
 
          continue;
5079
 
        if (error == HA_ERR_END_OF_FILE)
5080
 
          break;
5081
 
        goto err;
 
13539
        if (error == HA_ERR_RECORD_DELETED)
 
13540
          continue;
 
13541
        if (error == HA_ERR_END_OF_FILE)
 
13542
          break;
 
13543
        goto err;
5082
13544
      }
5083
13545
      if (table->compare_record(first_field) == 0)
5084
13546
      {
5085
 
        if ((error=cursor->deleteRecord(record)))
5086
 
          goto err;
 
13547
        if ((error=file->ha_delete_row(record)))
 
13548
          goto err;
5087
13549
      }
5088
13550
      else if (!found)
5089
13551
      {
5090
 
        found= 1;
5091
 
        cursor->position(record);       // Remember position
 
13552
        found=1;
 
13553
        file->position(record); // Remember position
5092
13554
      }
5093
13555
    }
5094
13556
    if (!found)
5095
 
      break;                                    // End of cursor
5096
 
    /* Move current position to the next row */
5097
 
    error= cursor->rnd_pos(record, cursor->ref);
 
13557
      break;                                    // End of file
 
13558
    /* Restart search on next row */
 
13559
    error=file->restart_rnd_next(record,file->ref);
5098
13560
  }
5099
13561
 
5100
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13562
  file->extra(HA_EXTRA_NO_CACHE);
5101
13563
  return(0);
5102
13564
err:
5103
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13565
  file->extra(HA_EXTRA_NO_CACHE);
5104
13566
  if (error)
5105
 
    table->print_error(error,MYF(0));
 
13567
    file->print_error(error,MYF(0));
5106
13568
  return(1);
5107
13569
}
5108
13570
 
 
13571
 
5109
13572
/**
5110
13573
  Generate a hash index for each row to quickly find duplicate rows.
5111
13574
 
5112
13575
  @note
5113
13576
    Note that this will not work on tables with blobs!
5114
13577
*/
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)
 
13578
 
 
13579
static int remove_dup_with_hash_index(THD *thd, Table *table,
 
13580
                                      uint32_t field_count,
 
13581
                                      Field **first_field,
 
13582
                                      ulong key_length,
 
13583
                                      Item *having)
5121
13584
{
5122
 
  unsigned char *key_pos, *record=table->getInsertRecord();
 
13585
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
5123
13586
  int error;
5124
 
  Cursor *cursor= table->cursor;
5125
 
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5126
 
  uint32_t *field_length;
 
13587
  handler *file= table->file;
 
13588
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
 
13589
  uint32_t *field_lengths,*field_length;
5127
13590
  HASH hash;
5128
 
  std::vector<unsigned char> key_buffer;
5129
 
  std::vector<uint32_t> field_lengths;
5130
13591
 
5131
 
  key_buffer.resize((key_length + extra_length) * (long) cursor->stats.records);
5132
 
  field_lengths.resize(field_count);
 
13592
  if (!my_multi_malloc(MYF(MY_WME),
 
13593
                       &key_buffer,
 
13594
                       (uint) ((key_length + extra_length) *
 
13595
                               (long) file->stats.records),
 
13596
                       &field_lengths,
 
13597
                       (uint) (field_count*sizeof(*field_lengths)),
 
13598
                       NULL))
 
13599
    return(1);
5133
13600
 
5134
13601
  {
5135
13602
    Field **ptr;
5136
 
    uint32_t total_length= 0;
5137
 
 
5138
 
    for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
 
13603
    ulong total_length= 0;
 
13604
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
5139
13605
    {
5140
13606
      uint32_t length= (*ptr)->sort_length();
5141
13607
      (*field_length++)= length;
5146
13612
    extra_length= ALIGN_SIZE(key_length)-key_length;
5147
13613
  }
5148
13614
 
5149
 
  if (hash_init(&hash, &my_charset_bin, (uint32_t) cursor->stats.records, 0,
 
13615
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, 
5150
13616
                key_length, (hash_get_key) 0, 0, 0))
5151
13617
  {
 
13618
    free((char*) key_buffer);
5152
13619
    return(1);
5153
13620
  }
5154
13621
 
5155
 
  if ((error= cursor->startTableScan(1)))
5156
 
    goto err;
5157
 
 
5158
 
  key_pos= &key_buffer[0];
 
13622
  file->ha_rnd_init(1);
 
13623
  key_pos=key_buffer;
5159
13624
  for (;;)
5160
13625
  {
5161
13626
    unsigned char *org_key_pos;
5162
 
    if (session->getKilled())
 
13627
    if (thd->killed)
5163
13628
    {
5164
 
      session->send_kill_message();
 
13629
      thd->send_kill_message();
5165
13630
      error=0;
5166
13631
      goto err;
5167
13632
    }
5168
 
    if ((error=cursor->rnd_next(record)))
 
13633
    if ((error=file->rnd_next(record)))
5169
13634
    {
5170
13635
      if (error == HA_ERR_RECORD_DELETED)
5171
 
        continue;
 
13636
        continue;
5172
13637
      if (error == HA_ERR_END_OF_FILE)
5173
 
        break;
 
13638
        break;
5174
13639
      goto err;
5175
13640
    }
5176
13641
    if (having && !having->val_int())
5177
13642
    {
5178
 
      if ((error=cursor->deleteRecord(record)))
5179
 
        goto err;
 
13643
      if ((error=file->ha_delete_row(record)))
 
13644
        goto err;
5180
13645
      continue;
5181
13646
    }
5182
13647
 
5183
13648
    /* copy fields to key buffer */
5184
13649
    org_key_pos= key_pos;
5185
 
    field_length= &field_lengths[0];
 
13650
    field_length=field_lengths;
5186
13651
    for (Field **ptr= first_field ; *ptr ; ptr++)
5187
13652
    {
5188
13653
      (*ptr)->sort_string(key_pos,*field_length);
5192
13657
    if (hash_search(&hash, org_key_pos, key_length))
5193
13658
    {
5194
13659
      /* Duplicated found ; Remove the row */
5195
 
      if ((error=cursor->deleteRecord(record)))
5196
 
        goto err;
 
13660
      if ((error=file->ha_delete_row(record)))
 
13661
        goto err;
5197
13662
    }
5198
13663
    else
5199
13664
      (void) my_hash_insert(&hash, org_key_pos);
5200
13665
    key_pos+=extra_length;
5201
13666
  }
 
13667
  free((char*) key_buffer);
5202
13668
  hash_free(&hash);
5203
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5204
 
  (void) cursor->endTableScan();
 
13669
  file->extra(HA_EXTRA_NO_CACHE);
 
13670
  (void) file->ha_rnd_end();
5205
13671
  return(0);
5206
13672
 
5207
13673
err:
 
13674
  free((char*) key_buffer);
5208
13675
  hash_free(&hash);
5209
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5210
 
  (void) cursor->endTableScan();
 
13676
  file->extra(HA_EXTRA_NO_CACHE);
 
13677
  (void) file->ha_rnd_end();
5211
13678
  if (error)
5212
 
    table->print_error(error,MYF(0));
 
13679
    file->print_error(error,MYF(0));
5213
13680
  return(1);
5214
13681
}
5215
13682
 
5216
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
 
13683
 
 
13684
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length,
 
13685
                                  SORT_FIELD *sortorder)
5217
13686
{
5218
13687
  uint32_t count;
5219
 
  SortField *sort,*pos;
 
13688
  SORT_FIELD *sort,*pos;
5220
13689
 
5221
13690
  count=0;
5222
 
  for (Order *tmp = order; tmp; tmp=tmp->next)
 
13691
  for (order_st *tmp = order; tmp; tmp=tmp->next)
5223
13692
    count++;
5224
13693
  if (!sortorder)
5225
 
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
5226
 
                                       (max(count, *length) + 1));
 
13694
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
 
13695
                                       (cmax(count, *length) + 1));
5227
13696
  pos= sort= sortorder;
5228
13697
 
5229
13698
  if (!pos)
5249
13718
  return(sort);
5250
13719
}
5251
13720
 
 
13721
 
 
13722
/*****************************************************************************
 
13723
  Fill join cache with packed records
 
13724
  Records are stored in tab->cache.buffer and last record in
 
13725
  last record is stored with pointers to blobs to support very big
 
13726
  records
 
13727
******************************************************************************/
 
13728
 
 
13729
static int
 
13730
join_init_cache(THD *thd,JOIN_TAB *tables,uint32_t table_count)
 
13731
{
 
13732
  register unsigned int i;
 
13733
  unsigned int length, blobs;
 
13734
  size_t size;
 
13735
  CACHE_FIELD *copy,**blob_ptr;
 
13736
  JOIN_CACHE  *cache;
 
13737
  JOIN_TAB *join_tab;
 
13738
 
 
13739
  cache= &tables[table_count].cache;
 
13740
  cache->fields=blobs=0;
 
13741
 
 
13742
  join_tab=tables;
 
13743
  for (i=0 ; i < table_count ; i++,join_tab++)
 
13744
  {
 
13745
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
13746
      calc_used_field_length(thd, join_tab);
 
13747
    cache->fields+=join_tab->used_fields;
 
13748
    blobs+=join_tab->used_blobs;
 
13749
 
 
13750
    /* SemiJoinDuplicateElimination: reserve space for rowid */
 
13751
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13752
    {
 
13753
      cache->fields++;
 
13754
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
 
13755
    }
 
13756
  }
 
13757
  if (!(cache->field=(CACHE_FIELD*)
 
13758
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
 
13759
 
 
13760
                  sizeof(CACHE_FIELD*))))
 
13761
  {
 
13762
    free((unsigned char*) cache->buff);         /* purecov: inspected */
 
13763
    cache->buff=0;                              /* purecov: inspected */
 
13764
    return(1);                          /* purecov: inspected */
 
13765
  }
 
13766
  copy=cache->field;
 
13767
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
 
13768
    (cache->field+cache->fields+table_count*2);
 
13769
 
 
13770
  length=0;
 
13771
  for (i=0 ; i < table_count ; i++)
 
13772
  {
 
13773
    uint32_t null_fields=0, used_fields;
 
13774
    Field **f_ptr,*field;
 
13775
    MY_BITMAP *read_set= tables[i].table->read_set;
 
13776
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
 
13777
         used_fields ;
 
13778
         f_ptr++)
 
13779
    {
 
13780
      field= *f_ptr;
 
13781
      if (bitmap_is_set(read_set, field->field_index))
 
13782
      {
 
13783
        used_fields--;
 
13784
        length+=field->fill_cache_field(copy);
 
13785
        if (copy->blob_field)
 
13786
          (*blob_ptr++)=copy;
 
13787
        if (field->maybe_null())
 
13788
          null_fields++;
 
13789
        copy->get_rowid= NULL;
 
13790
        copy++;
 
13791
      }
 
13792
    }
 
13793
    /* Copy null bits from table */
 
13794
    if (null_fields && tables[i].table->getNullFields())
 
13795
    {                                           /* must copy null bits */
 
13796
      copy->str= tables[i].table->null_flags;
 
13797
      copy->length= tables[i].table->s->null_bytes;
 
13798
      copy->strip=0;
 
13799
      copy->blob_field=0;
 
13800
      copy->get_rowid= NULL;
 
13801
      length+=copy->length;
 
13802
      copy++;
 
13803
      cache->fields++;
 
13804
    }
 
13805
    /* If outer join table, copy null_row flag */
 
13806
    if (tables[i].table->maybe_null)
 
13807
    {
 
13808
      copy->str= (unsigned char*) &tables[i].table->null_row;
 
13809
      copy->length=sizeof(tables[i].table->null_row);
 
13810
      copy->strip=0;
 
13811
      copy->blob_field=0;
 
13812
      copy->get_rowid= NULL;
 
13813
      length+=copy->length;
 
13814
      copy++;
 
13815
      cache->fields++;
 
13816
    }
 
13817
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
 
13818
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13819
    {
 
13820
      copy->str= tables[i].table->file->ref;
 
13821
      copy->length= tables[i].table->file->ref_length;
 
13822
      copy->strip=0;
 
13823
      copy->blob_field=0;
 
13824
      copy->get_rowid= NULL;
 
13825
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
13826
      {
 
13827
        /* We will need to call h->position(): */
 
13828
        copy->get_rowid= tables[i].table;
 
13829
        /* And those after us won't have to: */
 
13830
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
 
13831
      }
 
13832
      copy++;
 
13833
    }
 
13834
  }
 
13835
 
 
13836
  cache->length=length+blobs*sizeof(char*);
 
13837
  cache->blobs=blobs;
 
13838
  *blob_ptr=0;                                  /* End sequentel */
 
13839
  size=cmax(thd->variables.join_buff_size, (ulong)cache->length);
 
13840
  if (!(cache->buff=(unsigned char*) my_malloc(size,MYF(0))))
 
13841
    return(1);                          /* Don't use cache */ /* purecov: inspected */
 
13842
  cache->end=cache->buff+size;
 
13843
  reset_cache_write(cache);
 
13844
  return(0);
 
13845
}
 
13846
 
 
13847
 
 
13848
static ulong
 
13849
used_blob_length(CACHE_FIELD **ptr)
 
13850
{
 
13851
  uint32_t length,blob_length;
 
13852
  for (length=0 ; *ptr ; ptr++)
 
13853
  {
 
13854
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
 
13855
    length+=blob_length;
 
13856
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
 
13857
  }
 
13858
  return length;
 
13859
}
 
13860
 
 
13861
 
 
13862
static bool
 
13863
store_record_in_cache(JOIN_CACHE *cache)
 
13864
{
 
13865
  uint32_t length;
 
13866
  unsigned char *pos;
 
13867
  CACHE_FIELD *copy,*end_field;
 
13868
  bool last_record;
 
13869
 
 
13870
  pos=cache->pos;
 
13871
  end_field=cache->field+cache->fields;
 
13872
 
 
13873
  length=cache->length;
 
13874
  if (cache->blobs)
 
13875
    length+=used_blob_length(cache->blob_ptr);
 
13876
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
13877
    cache->ptr_record=cache->records;
 
13878
  /*
 
13879
    There is room in cache. Put record there
 
13880
  */
 
13881
  cache->records++;
 
13882
  for (copy=cache->field ; copy < end_field; copy++)
 
13883
  {
 
13884
    if (copy->blob_field)
 
13885
    {
 
13886
      if (last_record)
 
13887
      {
 
13888
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), 
 
13889
                                    copy->blob_field->charset());
 
13890
        pos+=copy->length+sizeof(char*);
 
13891
      }
 
13892
      else
 
13893
      {
 
13894
        copy->blob_field->get_image(pos, copy->length, // blob length
 
13895
                                    copy->blob_field->charset());
 
13896
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
13897
        pos+=copy->length+copy->blob_length;
 
13898
      }
 
13899
    }
 
13900
    else
 
13901
    {
 
13902
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
 
13903
      if (copy->get_rowid)
 
13904
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
 
13905
 
 
13906
      if (copy->strip)
 
13907
      {
 
13908
        unsigned char *str,*end;
 
13909
        for (str=copy->str,end= str+copy->length;
 
13910
             end > str && end[-1] == ' ' ;
 
13911
             end--) ;
 
13912
        length=(uint) (end-str);
 
13913
        memcpy(pos+2, str, length);
 
13914
        int2store(pos, length);
 
13915
        pos+= length+2;
 
13916
      }
 
13917
      else
 
13918
      {
 
13919
        memcpy(pos,copy->str,copy->length);
 
13920
        pos+=copy->length;
 
13921
      }
 
13922
    }
 
13923
  }
 
13924
  cache->pos=pos;
 
13925
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
13926
}
 
13927
 
 
13928
 
 
13929
static void
 
13930
reset_cache_read(JOIN_CACHE *cache)
 
13931
{
 
13932
  cache->record_nr=0;
 
13933
  cache->pos=cache->buff;
 
13934
}
 
13935
 
 
13936
 
 
13937
static void reset_cache_write(JOIN_CACHE *cache)
 
13938
{
 
13939
  reset_cache_read(cache);
 
13940
  cache->records= 0;
 
13941
  cache->ptr_record= UINT32_MAX;
 
13942
}
 
13943
 
 
13944
 
 
13945
static void
 
13946
read_cached_record(JOIN_TAB *tab)
 
13947
{
 
13948
  unsigned char *pos;
 
13949
  uint32_t length;
 
13950
  bool last_record;
 
13951
  CACHE_FIELD *copy,*end_field;
 
13952
 
 
13953
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
 
13954
  pos=tab->cache.pos;
 
13955
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
 
13956
       copy < end_field;
 
13957
       copy++)
 
13958
  {
 
13959
    if (copy->blob_field)
 
13960
    {
 
13961
      if (last_record)
 
13962
      {
 
13963
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
 
13964
                                    copy->blob_field->charset());
 
13965
        pos+=copy->length+sizeof(char*);
 
13966
      }
 
13967
      else
 
13968
      {
 
13969
        copy->blob_field->set_ptr(pos, pos+copy->length);
 
13970
        pos+=copy->length+copy->blob_field->get_length();
 
13971
      }
 
13972
    }
 
13973
    else
 
13974
    {
 
13975
      if (copy->strip)
 
13976
      {
 
13977
        length= uint2korr(pos);
 
13978
        memcpy(copy->str, pos+2, length);
 
13979
        memset(copy->str+length, ' ', copy->length-length);
 
13980
        pos+= 2 + length;
 
13981
      }
 
13982
      else
 
13983
      {
 
13984
        memcpy(copy->str,pos,copy->length);
 
13985
        pos+=copy->length;
 
13986
      }
 
13987
    }
 
13988
  }
 
13989
  tab->cache.pos=pos;
 
13990
  return;
 
13991
}
 
13992
 
 
13993
 
5252
13994
/*
5253
13995
  eq_ref: Create the lookup key and check if it is the same as saved key
5254
13996
 
5255
13997
  SYNOPSIS
5256
13998
    cmp_buffer_with_ref()
5257
13999
      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
 
14000
 
 
14001
  DESCRIPTION 
 
14002
    Used by eq_ref access method: create the index lookup key and check if 
5261
14003
    we've used this key at previous lookup (If yes, we don't need to repeat
5262
14004
    the lookup - the record has been already fetched)
5263
14005
 
5264
 
  RETURN
 
14006
  RETURN 
5265
14007
    true   No cached record for the key, or failed to create the key (due to
5266
14008
           out-of-domain error)
5267
 
    false  The created key is the same as the previous one (and the record
 
14009
    false  The created key is the same as the previous one (and the record 
5268
14010
           is already in table->record)
5269
14011
*/
5270
 
static bool cmp_buffer_with_ref(JoinTable *tab)
 
14012
 
 
14013
static bool
 
14014
cmp_buffer_with_ref(JOIN_TAB *tab)
5271
14015
{
5272
14016
  bool no_prev_key;
5273
14017
  if (!tab->ref.disable_cache)
5278
14022
      memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
5279
14023
    }
5280
14024
  }
5281
 
  else
 
14025
  else 
5282
14026
    no_prev_key= true;
5283
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) ||
 
14027
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, &tab->ref)) ||
5284
14028
      no_prev_key)
5285
14029
    return 1;
5286
14030
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
5287
14031
    != 0;
5288
14032
}
5289
14033
 
5290
 
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
 
14034
 
 
14035
bool
 
14036
cp_buffer_from_ref(THD *thd, TABLE_REF *ref)
5291
14037
{
5292
 
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
5293
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
14038
  enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
 
14039
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
5294
14040
  bool result= 0;
5295
14041
 
5296
 
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
 
14042
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
5297
14043
  {
5298
14044
    if ((*copy)->copy() & 1)
5299
14045
    {
5301
14047
      break;
5302
14048
    }
5303
14049
  }
5304
 
  session->count_cuted_fields= save_count_cuted_fields;
 
14050
  thd->count_cuted_fields= save_count_cuted_fields;
5305
14051
  return result;
5306
14052
}
5307
14053
 
 
14054
 
5308
14055
/*****************************************************************************
5309
14056
  Group and order functions
5310
14057
*****************************************************************************/
5311
14058
 
5312
14059
/**
5313
 
  Resolve an ORDER BY or GROUP BY column reference.
 
14060
  Resolve an order_st BY or GROUP BY column reference.
5314
14061
 
5315
14062
  Given a column reference (represented by 'order') from a GROUP BY or order_st
5316
14063
  BY clause, find the actual column it represents. If the column being
5317
14064
  resolved is from the GROUP BY clause, the procedure searches the SELECT
5318
14065
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
5319
 
  the ORDER BY clause, only the SELECT list is being searched.
 
14066
  the order_st BY clause, only the SELECT list is being searched.
5320
14067
 
5321
14068
  If 'order' is resolved to an Item, then order->item is set to the found
5322
14069
  Item. If there is no item for the found column (that is, it was resolved
5325
14072
 
5326
14073
  ref_pointer_array and all_fields are updated.
5327
14074
 
5328
 
  @param[in] session                 Pointer to current thread structure
 
14075
  @param[in] thd                     Pointer to current thread structure
5329
14076
  @param[in,out] ref_pointer_array  All select, group and order by fields
5330
14077
  @param[in] tables                 List of tables to search in (usually
5331
14078
    FROM clause)
5341
14088
  @retval
5342
14089
    true  if error occurred
5343
14090
*/
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)
 
14091
 
 
14092
static bool
 
14093
find_order_in_list(THD *thd, Item **ref_pointer_array, TableList *tables,
 
14094
                   order_st *order, List<Item> &fields, List<Item> &all_fields,
 
14095
                   bool is_group_field)
5351
14096
{
5352
14097
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
5353
14098
  Item::Type order_item_type;
5362
14107
  */
5363
14108
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
5364
14109
  {                                             /* Order by position */
5365
 
    uint32_t count= (uint32_t) order_item->val_int();
 
14110
    uint32_t count= (uint) order_item->val_int();
5366
14111
    if (!count || count > fields.elements)
5367
14112
    {
5368
14113
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
5369
 
               order_item->full_name(), session->where());
 
14114
               order_item->full_name(), thd->where);
5370
14115
      return true;
5371
14116
    }
5372
14117
    order->item= ref_pointer_array + count - 1;
5376
14121
    return false;
5377
14122
  }
5378
14123
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5379
 
  select_item= find_item_in_list(session, order_item, fields, &counter,
 
14124
  select_item= find_item_in_list(order_item, fields, &counter,
5380
14125
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
5381
14126
  if (!select_item)
5382
14127
    return true; /* The item is not unique, or some other error occured. */
5392
14137
      for this name (in case if we would perform lookup in all tables).
5393
14138
    */
5394
14139
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
5395
 
        order_item->fix_fields(session, order->item))
 
14140
        order_item->fix_fields(thd, order->item))
5396
14141
      return true;
5397
14142
 
5398
14143
    /* Lookup the current GROUP field in the FROM clause. */
5401
14146
    if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
5402
14147
        order_item_type == Item::REF_ITEM)
5403
14148
    {
5404
 
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
5405
 
                                       NULL, &view_ref, IGNORE_ERRORS, false);
 
14149
      from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables,
 
14150
                                       NULL, &view_ref, IGNORE_ERRORS, true,
 
14151
                                       false);
5406
14152
      if (!from_field)
5407
14153
        from_field= (Field*) not_found_field;
5408
14154
    }
5440
14186
        warning so the user knows that the field from the FROM clause
5441
14187
        overshadows the column reference from the SELECT list.
5442
14188
      */
5443
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
14189
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5444
14190
                          ER(ER_NON_UNIQ_ERROR),
5445
14191
                          ((Item_ident*) order_item)->field_name,
5446
 
                          session->where());
 
14192
                          current_thd->where);
5447
14193
    }
5448
14194
  }
5449
14195
 
5460
14206
    arguments for which fix_fields already was called.
5461
14207
  */
5462
14208
  if (!order_item->fixed &&
5463
 
      (order_item->fix_fields(session, order->item) ||
 
14209
      (order_item->fix_fields(thd, order->item) ||
5464
14210
       (order_item= *order->item)->check_cols(1) ||
5465
 
       session->is_fatal_error))
 
14211
       thd->is_fatal_error))
5466
14212
    return true; /* Wrong field. */
5467
14213
 
5468
14214
  uint32_t el= all_fields.elements;
5472
14218
  return false;
5473
14219
}
5474
14220
 
 
14221
 
5475
14222
/**
5476
14223
  Change order to point at item in select list.
5477
14224
 
5478
14225
  If item isn't a number and doesn't exits in the select list, add it the
5479
14226
  the field list.
5480
14227
*/
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)
 
14228
 
 
14229
int setup_order(THD *thd, Item **ref_pointer_array, TableList *tables,
 
14230
                List<Item> &fields, List<Item> &all_fields, order_st *order)
5487
14231
{
5488
 
  session->setWhere("order clause");
 
14232
  thd->where="order clause";
5489
14233
  for (; order; order=order->next)
5490
14234
  {
5491
 
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
 
14235
    if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
5492
14236
                           all_fields, false))
5493
14237
      return 1;
5494
14238
  }
5495
14239
  return 0;
5496
14240
}
5497
14241
 
 
14242
 
5498
14243
/**
5499
14244
  Intitialize the GROUP BY list.
5500
14245
 
5501
 
  @param session                        Thread Cursor
 
14246
  @param thd                    Thread handler
5502
14247
  @param ref_pointer_array      We store references to all fields that was
5503
14248
                               not in 'fields' here.
5504
14249
  @param fields         All fields in the select part. Any item in
5520
14265
  @retval
5521
14266
    1  error (probably out of memory)
5522
14267
*/
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)
 
14268
 
 
14269
int
 
14270
setup_group(THD *thd, Item **ref_pointer_array, TableList *tables,
 
14271
            List<Item> &fields, List<Item> &all_fields, order_st *order,
 
14272
            bool *hidden_group_fields)
5530
14273
{
5531
14274
  *hidden_group_fields=0;
5532
 
  Order *ord;
 
14275
  order_st *ord;
5533
14276
 
5534
14277
  if (!order)
5535
14278
    return 0;                           /* Everything is ok */
5536
14279
 
5537
14280
  uint32_t org_fields=all_fields.elements;
5538
14281
 
5539
 
  session->setWhere("group statement");
 
14282
  thd->where="group statement";
5540
14283
  for (ord= order; ord; ord= ord->next)
5541
14284
  {
5542
 
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
 
14285
    if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields,
5543
14286
                           all_fields, true))
5544
14287
      return 1;
5545
14288
    (*ord->item)->marker= UNDEF_POS;            /* Mark found */
5569
14312
    Item *item;
5570
14313
    Item_field *field;
5571
14314
    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());
 
14315
    List_iterator<Item> li(fields);
 
14316
    List_iterator<Item_field> naf_it(thd->lex->current_select->non_agg_fields);
5574
14317
 
5575
14318
    field= naf_it++;
5576
14319
    while (field && (item=li++))
5596
14339
            if ((*ord->item)->eq((Item*)field, 0))
5597
14340
              goto next_field;
5598
14341
          /*
5599
 
            @todo change ER_WRONG_FIELD_WITH_GROUP to more detailed ER_NON_GROUPING_FIELD_USED
 
14342
            TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed
 
14343
            ER_NON_GROUPING_FIELD_USED
5600
14344
          */
5601
14345
          my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
5602
14346
          return 1;
5618
14362
  Try to use the fields in the order given by 'order' to allow one to
5619
14363
  optimize away 'order by'.
5620
14364
*/
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)
 
14365
 
 
14366
static order_st *
 
14367
create_distinct_group(THD *thd, Item **ref_pointer_array,
 
14368
                      order_st *order_list, List<Item> &fields,
 
14369
                      List<Item> &all_fields __attribute__((unused)),
 
14370
                      bool *all_order_by_fields_used)
5627
14371
{
5628
 
  List<Item>::iterator li(fields.begin());
 
14372
  List_iterator<Item> li(fields);
5629
14373
  Item *item;
5630
 
  Order *order,*group,**prev;
 
14374
  order_st *order,*group,**prev;
5631
14375
 
5632
14376
  *all_order_by_fields_used= 1;
5633
14377
  while ((item=li++))
5638
14382
  {
5639
14383
    if (order->in_field_list)
5640
14384
    {
5641
 
      Order *ord=(Order*) session->getMemRoot()->duplicate((char*) order,sizeof(Order));
 
14385
      order_st *ord=(order_st*) thd->memdup((char*) order,sizeof(order_st));
5642
14386
      if (!ord)
5643
 
        return 0;
 
14387
        return 0;
5644
14388
      *prev=ord;
5645
14389
      prev= &ord->next;
5646
14390
      (*ord->item)->marker=1;
5649
14393
      *all_order_by_fields_used= 0;
5650
14394
  }
5651
14395
 
5652
 
  li= fields.begin();
 
14396
  li.rewind();
5653
14397
  while ((item=li++))
5654
14398
  {
5655
14399
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5656
14400
    {
5657
 
      /*
5658
 
        Don't put duplicate columns from the SELECT list into the
 
14401
      /* 
 
14402
        Don't put duplicate columns from the SELECT list into the 
5659
14403
        GROUP BY list.
5660
14404
      */
5661
 
      Order *ord_iter;
 
14405
      order_st *ord_iter;
5662
14406
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5663
14407
        if ((*ord_iter->item)->eq(item, 1))
5664
14408
          goto next_item;
5665
 
 
5666
 
      Order *ord=(Order*) session->calloc(sizeof(Order));
 
14409
      
 
14410
      order_st *ord=(order_st*) thd->calloc(sizeof(order_st));
5667
14411
      if (!ord)
5668
 
        return 0;
 
14412
        return 0;
5669
14413
 
5670
14414
      /*
5671
14415
        We have here only field_list (not all_field_list), so we can use
5684
14428
  return group;
5685
14429
}
5686
14430
 
 
14431
 
5687
14432
/**
5688
14433
  Update join with count of the different type of fields.
5689
14434
*/
5690
 
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
 
14435
 
 
14436
void
 
14437
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, 
 
14438
                  List<Item> &fields, bool reset_with_sum_func)
5691
14439
{
5692
 
  List<Item>::iterator li(fields.begin());
 
14440
  List_iterator<Item> li(fields);
5693
14441
  Item *field;
5694
14442
 
5695
14443
  param->field_count=param->sum_func_count=param->func_count=
5704
14452
    {
5705
14453
      if (! field->const_item())
5706
14454
      {
5707
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
14455
        Item_sum *sum_item=(Item_sum*) field->real_item();
5708
14456
        if (!sum_item->depended_from() ||
5709
14457
            sum_item->depended_from() == select_lex)
5710
14458
        {
5727
14475
    {
5728
14476
      param->func_count++;
5729
14477
      if (reset_with_sum_func)
5730
 
        field->with_sum_func=0;
5731
 
    }
5732
 
  }
5733
 
}
 
14478
        field->with_sum_func=0;
 
14479
    }
 
14480
  }
 
14481
}
 
14482
 
 
14483
 
 
14484
/**
 
14485
  Return 1 if second is a subpart of first argument.
 
14486
 
 
14487
  If first parts has different direction, change it to second part
 
14488
  (group is sorted like order)
 
14489
*/
 
14490
 
 
14491
static bool
 
14492
test_if_subpart(order_st *a,order_st *b)
 
14493
{
 
14494
  for (; a && b; a=a->next,b=b->next)
 
14495
  {
 
14496
    if ((*a->item)->eq(*b->item,1))
 
14497
      a->asc=b->asc;
 
14498
    else
 
14499
      return 0;
 
14500
  }
 
14501
  return test(!b);
 
14502
}
 
14503
 
 
14504
/**
 
14505
  Return table number if there is only one table in sort order
 
14506
  and group and order is compatible, else return 0.
 
14507
*/
 
14508
 
 
14509
static Table *
 
14510
get_sort_by_table(order_st *a,order_st *b,TableList *tables)
 
14511
{
 
14512
  table_map map= (table_map) 0;
 
14513
 
 
14514
  if (!a)
 
14515
    a=b;                                        // Only one need to be given
 
14516
  else if (!b)
 
14517
    b=a;
 
14518
 
 
14519
  for (; a && b; a=a->next,b=b->next)
 
14520
  {
 
14521
    if (!(*a->item)->eq(*b->item,1))
 
14522
      return(0);
 
14523
    map|=a->item[0]->used_tables();
 
14524
  }
 
14525
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
 
14526
    return(0);
 
14527
 
 
14528
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
 
14529
  if (map != tables->table->map)
 
14530
    return(0);                          // More than one table
 
14531
  return(tables->table);
 
14532
}
 
14533
 
 
14534
 
 
14535
/**
 
14536
  calc how big buffer we need for comparing group entries.
 
14537
*/
 
14538
 
 
14539
static void
 
14540
calc_group_buffer(JOIN *join,order_st *group)
 
14541
{
 
14542
  uint32_t key_length=0, parts=0, null_parts=0;
 
14543
 
 
14544
  if (group)
 
14545
    join->group= 1;
 
14546
  for (; group ; group=group->next)
 
14547
  {
 
14548
    Item *group_item= *group->item;
 
14549
    Field *field= group_item->get_tmp_table_field();
 
14550
    if (field)
 
14551
    {
 
14552
      enum_field_types type;
 
14553
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
 
14554
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
 
14555
      else if (type == DRIZZLE_TYPE_VARCHAR)
 
14556
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
 
14557
      else
 
14558
        key_length+= field->pack_length();
 
14559
    }
 
14560
    else
 
14561
    { 
 
14562
      switch (group_item->result_type()) {
 
14563
      case REAL_RESULT:
 
14564
        key_length+= sizeof(double);
 
14565
        break;
 
14566
      case INT_RESULT:
 
14567
        key_length+= sizeof(int64_t);
 
14568
        break;
 
14569
      case DECIMAL_RESULT:
 
14570
        key_length+= my_decimal_get_binary_size(group_item->max_length - 
 
14571
                                                (group_item->decimals ? 1 : 0),
 
14572
                                                group_item->decimals);
 
14573
        break;
 
14574
      case STRING_RESULT:
 
14575
      {
 
14576
        enum enum_field_types type= group_item->field_type();
 
14577
        /*
 
14578
          As items represented as DATE/TIME fields in the group buffer
 
14579
          have STRING_RESULT result type, we increase the length 
 
14580
          by 8 as maximum pack length of such fields.
 
14581
        */
 
14582
        if (type == DRIZZLE_TYPE_TIME ||
 
14583
            type == DRIZZLE_TYPE_NEWDATE ||
 
14584
            type == DRIZZLE_TYPE_DATETIME ||
 
14585
            type == DRIZZLE_TYPE_TIMESTAMP)
 
14586
        {
 
14587
          key_length+= 8;
 
14588
        }
 
14589
        else
 
14590
        {
 
14591
          /*
 
14592
            Group strings are taken as varstrings and require an length field.
 
14593
            A field is not yet created by create_tmp_field()
 
14594
            and the sizes should match up.
 
14595
          */
 
14596
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
 
14597
        }
 
14598
        break;
 
14599
      }
 
14600
      default:
 
14601
        /* This case should never be choosen */
 
14602
        assert(0);
 
14603
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
14604
      }
 
14605
    }
 
14606
    parts++;
 
14607
    if (group_item->maybe_null)
 
14608
      null_parts++;
 
14609
  }
 
14610
  join->tmp_table_param.group_length=key_length+null_parts;
 
14611
  join->tmp_table_param.group_parts=parts;
 
14612
  join->tmp_table_param.group_null_parts=null_parts;
 
14613
}
 
14614
 
 
14615
 
 
14616
/**
 
14617
  allocate group fields or take prepared (cached).
 
14618
 
 
14619
  @param main_join   join of current select
 
14620
  @param curr_join   current join (join of current select or temporary copy
 
14621
                     of it)
 
14622
 
 
14623
  @retval
 
14624
    0   ok
 
14625
  @retval
 
14626
    1   failed
 
14627
*/
 
14628
 
 
14629
static bool
 
14630
make_group_fields(JOIN *main_join, JOIN *curr_join)
 
14631
{
 
14632
  if (main_join->group_fields_cache.elements)
 
14633
  {
 
14634
    curr_join->group_fields= main_join->group_fields_cache;
 
14635
    curr_join->sort_and_group= 1;
 
14636
  }
 
14637
  else
 
14638
  {
 
14639
    if (alloc_group_fields(curr_join, curr_join->group_list))
 
14640
      return (1);
 
14641
    main_join->group_fields_cache= curr_join->group_fields;
 
14642
  }
 
14643
  return (0);
 
14644
}
 
14645
 
 
14646
 
 
14647
/**
 
14648
  Get a list of buffers for saveing last group.
 
14649
 
 
14650
  Groups are saved in reverse order for easyer check loop.
 
14651
*/
 
14652
 
 
14653
static bool
 
14654
alloc_group_fields(JOIN *join,order_st *group)
 
14655
{
 
14656
  if (group)
 
14657
  {
 
14658
    for (; group ; group=group->next)
 
14659
    {
 
14660
      Cached_item *tmp=new_Cached_item(join->thd, *group->item, false);
 
14661
      if (!tmp || join->group_fields.push_front(tmp))
 
14662
        return true;
 
14663
    }
 
14664
  }
 
14665
  join->sort_and_group=1;                       /* Mark for do_select */
 
14666
  return false;
 
14667
}
 
14668
 
5734
14669
 
5735
14670
/*
5736
14671
  Test if a single-row cache of items changed, and update the cache.
5738
14673
  @details Test if a list of items that typically represents a result
5739
14674
  row has changed. If the value of some item changed, update the cached
5740
14675
  value for this item.
5741
 
 
 
14676
  
5742
14677
  @param list list of <item, cached_value> pairs stored as Cached_item.
5743
14678
 
5744
14679
  @return -1 if no item changed
5745
14680
  @return index of the first item that changed
5746
14681
*/
 
14682
 
5747
14683
int test_if_item_cache_changed(List<Cached_item> &list)
5748
14684
{
5749
 
  List<Cached_item>::iterator li(list.begin());
 
14685
  List_iterator<Cached_item> li(list);
5750
14686
  int idx= -1,i;
5751
14687
  Cached_item *buff;
5752
14688
 
5758
14694
  return(idx);
5759
14695
}
5760
14696
 
 
14697
 
5761
14698
/**
5762
14699
  Setup copy_fields to save fields at start of new group.
5763
14700
 
5767
14704
  Change old item_field to use a new field with points at saved fieldvalue
5768
14705
  This function is only called before use of send_fields.
5769
14706
 
5770
 
  @param session                   Session pointer
 
14707
  @param thd                   THD pointer
5771
14708
  @param param                 temporary table parameters
5772
14709
  @param ref_pointer_array     array of pointers to top elements of filed list
5773
14710
  @param res_selected_fields   new list of items of select item list
5786
14723
  @retval
5787
14724
    !=0   error
5788
14725
*/
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)
 
14726
 
 
14727
bool
 
14728
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
 
14729
                  Item **ref_pointer_array,
 
14730
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
 
14731
                  uint32_t elements, List<Item> &all_fields)
5796
14732
{
5797
14733
  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());
 
14734
  List_iterator_fast<Item> li(all_fields);
 
14735
  Copy_field *copy= NULL;
 
14736
  res_selected_fields.empty();
 
14737
  res_all_fields.empty();
 
14738
  List_iterator_fast<Item> itr(res_all_fields);
5803
14739
  List<Item> extra_funcs;
5804
14740
  uint32_t i, border= all_fields.elements - elements;
5805
14741
 
5806
 
  if (param->field_count &&
5807
 
      !(copy=param->copy_field= new CopyField[param->field_count]))
 
14742
  if (param->field_count && 
 
14743
      !(copy=param->copy_field= new Copy_field[param->field_count]))
5808
14744
    goto err2;
5809
14745
 
5810
 
  param->copy_funcs.clear();
 
14746
  param->copy_funcs.empty();
5811
14747
  for (i= 0; (pos= li++); i++)
5812
14748
  {
5813
14749
    Field *field;
5816
14752
    if (real_pos->type() == Item::FIELD_ITEM)
5817
14753
    {
5818
14754
      Item_field *item;
5819
 
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
5820
 
        goto err;
 
14755
      if (!(item= new Item_field(thd, ((Item_field*) real_pos))))
 
14756
        goto err;
5821
14757
      if (pos->type() == Item::REF_ITEM)
5822
14758
      {
5823
14759
        /* preserve the names of the ref when dereferncing */
5829
14765
      pos= item;
5830
14766
      if (item->field->flags & BLOB_FLAG)
5831
14767
      {
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;
 
14768
        if (!(pos= new Item_copy_string(pos)))
 
14769
          goto err;
 
14770
       /*
 
14771
         Item_copy_string::copy for function can call 
 
14772
         Item_copy_string::val_int for blob via Item_ref.
 
14773
         But if Item_copy_string::copy for blob isn't called before,
 
14774
         it's value will be wrong
 
14775
         so let's insert Item_copy_string for blobs in the beginning of 
 
14776
         copy_funcs
 
14777
         (to see full test case look at having.test, BUG #4358) 
 
14778
       */
 
14779
        if (param->copy_funcs.push_front(pos))
 
14780
          goto err;
5845
14781
      }
5846
14782
      else
5847
14783
      {
 
14784
        /* 
 
14785
           set up save buffer and change result_field to point at 
 
14786
           saved value
 
14787
        */
 
14788
        field= item->field;
 
14789
        item->result_field=field->new_field(thd->mem_root,field->table, 1);
5848
14790
        /*
5849
 
          set up save buffer and change result_field to point at
5850
 
          saved value
 
14791
          We need to allocate one extra byte for null handling and
 
14792
          another extra byte to not get warnings from purify in
 
14793
          Field_varstring::val_int
5851
14794
        */
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;
 
14795
        if (!(tmp= (unsigned char*) sql_alloc(field->pack_length()+2)))
 
14796
          goto err;
5861
14797
        if (copy)
5862
14798
        {
5863
14799
          copy->set(tmp, item->result_field);
5864
14800
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
5865
 
#ifdef HAVE_VALGRIND
 
14801
#ifdef HAVE_purify
5866
14802
          copy->to_ptr[copy->from_length]= 0;
5867
14803
#endif
5868
14804
          copy++;
5876
14812
             !real_pos->with_sum_func)
5877
14813
    {                                           // Save for send fields
5878
14814
      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(*))
 
14815
      /* TODO:
 
14816
         In most cases this result will be sent to the user.
 
14817
         This should be changed to use copy_int or copy_real depending
 
14818
         on how the value is to be used: In some cases this may be an
 
14819
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
5884
14820
      */
5885
14821
      if (!(pos=new Item_copy_string(pos)))
5886
 
        goto err;
 
14822
        goto err;
5887
14823
      if (i < border)                           // HAVING, order_st and GROUP BY
5888
14824
      {
5889
14825
        if (extra_funcs.push_back(pos))
5890
14826
          goto err;
5891
14827
      }
5892
14828
      else if (param->copy_funcs.push_back(pos))
5893
 
        goto err;
 
14829
        goto err;
5894
14830
    }
5895
14831
    res_all_fields.push_back(pos);
5896
14832
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
5902
14838
    itr++;
5903
14839
  itr.sublist(res_selected_fields, elements);
5904
14840
  /*
5905
 
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
 
14841
    Put elements from HAVING, order_st BY and GROUP BY last to ensure that any
5906
14842
    reference used in these will resolve to a item that is already calculated
5907
14843
  */
5908
14844
  param->copy_funcs.concat(&extra_funcs);
5909
14845
 
5910
14846
  return(0);
5911
14847
 
5912
 
err:
 
14848
 err:
5913
14849
  if (copy)
5914
14850
    delete [] param->copy_field;                        // This is never 0
5915
14851
  param->copy_field=0;
5917
14853
  return(true);
5918
14854
}
5919
14855
 
 
14856
 
5920
14857
/**
5921
14858
  Make a copy of all simple SELECT'ed items.
5922
14859
 
5923
14860
  This is done at the start of a new group so that we can retrieve
5924
14861
  these later when the group changes.
5925
14862
*/
5926
 
void copy_fields(Tmp_Table_Param *param)
 
14863
 
 
14864
void
 
14865
copy_fields(TMP_TABLE_PARAM *param)
5927
14866
{
5928
 
  CopyField *ptr= param->copy_field;
5929
 
  CopyField *end= param->copy_field_end;
 
14867
  Copy_field *ptr=param->copy_field;
 
14868
  Copy_field *end=param->copy_field_end;
5930
14869
 
5931
14870
  for (; ptr != end; ptr++)
5932
14871
    (*ptr->do_copy)(ptr);
5933
14872
 
5934
 
  List<Item>::iterator it(param->copy_funcs.begin());
 
14873
  List_iterator_fast<Item> it(param->copy_funcs);
5935
14874
  Item_copy_string *item;
5936
14875
  while ((item = (Item_copy_string*) it++))
5937
14876
    item->copy();
5938
14877
}
5939
14878
 
 
14879
 
 
14880
/**
 
14881
  Make an array of pointers to sum_functions to speed up
 
14882
  sum_func calculation.
 
14883
 
 
14884
  @retval
 
14885
    0   ok
 
14886
  @retval
 
14887
    1   Error
 
14888
*/
 
14889
 
 
14890
bool JOIN::alloc_func_list()
 
14891
{
 
14892
  uint32_t func_count, group_parts;
 
14893
 
 
14894
  func_count= tmp_table_param.sum_func_count;
 
14895
  /*
 
14896
    If we are using rollup, we need a copy of the summary functions for
 
14897
    each level
 
14898
  */
 
14899
  if (rollup.state != ROLLUP::STATE_NONE)
 
14900
    func_count*= (send_group_parts+1);
 
14901
 
 
14902
  group_parts= send_group_parts;
 
14903
  /*
 
14904
    If distinct, reserve memory for possible
 
14905
    disctinct->group_by optimization
 
14906
  */
 
14907
  if (select_distinct)
 
14908
  {
 
14909
    group_parts+= fields_list.elements;
 
14910
    /*
 
14911
      If the order_st clause is specified then it's possible that
 
14912
      it also will be optimized, so reserve space for it too
 
14913
    */
 
14914
    if (order)
 
14915
    {
 
14916
      order_st *ord;
 
14917
      for (ord= order; ord; ord= ord->next)
 
14918
        group_parts++;
 
14919
    }
 
14920
  }
 
14921
 
 
14922
  /* This must use calloc() as rollup_make_fields depends on this */
 
14923
  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
 
14924
                                      sizeof(Item_sum***) * (group_parts+1));
 
14925
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
 
14926
  return(sum_funcs == 0);
 
14927
}
 
14928
 
 
14929
 
 
14930
/**
 
14931
  Initialize 'sum_funcs' array with all Item_sum objects.
 
14932
 
 
14933
  @param field_list        All items
 
14934
  @param send_fields       Items in select list
 
14935
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
 
14936
  @param recompute         Set to true if sum_funcs must be recomputed
 
14937
 
 
14938
  @retval
 
14939
    0  ok
 
14940
  @retval
 
14941
    1  error
 
14942
*/
 
14943
 
 
14944
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
 
14945
                              bool before_group_by, bool recompute)
 
14946
{
 
14947
  List_iterator_fast<Item> it(field_list);
 
14948
  Item_sum **func;
 
14949
  Item *item;
 
14950
 
 
14951
  if (*sum_funcs && !recompute)
 
14952
    return(false); /* We have already initialized sum_funcs. */
 
14953
 
 
14954
  func= sum_funcs;
 
14955
  while ((item=it++))
 
14956
  {
 
14957
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
14958
        (!((Item_sum*) item)->depended_from() ||
 
14959
         ((Item_sum *)item)->depended_from() == select_lex))
 
14960
      *func++= (Item_sum*) item;
 
14961
  }
 
14962
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
14963
  {
 
14964
    rollup.state= ROLLUP::STATE_READY;
 
14965
    if (rollup_make_fields(field_list, send_fields, &func))
 
14966
      return(true);                     // Should never happen
 
14967
  }
 
14968
  else if (rollup.state == ROLLUP::STATE_NONE)
 
14969
  {
 
14970
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
 
14971
      sum_funcs_end[i]= func;
 
14972
  }
 
14973
  else if (rollup.state == ROLLUP::STATE_READY)
 
14974
    return(false);                         // Don't put end marker
 
14975
  *func=0;                                      // End marker
 
14976
  return(false);
 
14977
}
 
14978
 
 
14979
 
5940
14980
/**
5941
14981
  Change all funcs and sum_funcs to fields in tmp table, and create
5942
14982
  new list of all items.
5943
14983
 
5944
 
  @param session                   Session pointer
 
14984
  @param thd                   THD pointer
5945
14985
  @param ref_pointer_array     array of pointers to top elements of filed list
5946
14986
  @param res_selected_fields   new list of items of select item list
5947
14987
  @param res_all_fields        new list of all items
5953
14993
  @retval
5954
14994
    !=0   error
5955
14995
*/
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)
 
14996
 
 
14997
static bool
 
14998
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
14999
                         List<Item> &res_selected_fields,
 
15000
                         List<Item> &res_all_fields,
 
15001
                         uint32_t elements, List<Item> &all_fields)
5962
15002
{
5963
 
  List<Item>::iterator it(all_fields.begin());
 
15003
  List_iterator_fast<Item> it(all_fields);
5964
15004
  Item *item_field,*item;
5965
15005
 
5966
 
  res_selected_fields.clear();
5967
 
  res_all_fields.clear();
 
15006
  res_selected_fields.empty();
 
15007
  res_all_fields.empty();
5968
15008
 
5969
15009
  uint32_t i, border= all_fields.elements - elements;
5970
15010
  for (i= 0; (item= it++); i++)
5979
15019
    {
5980
15020
      if (item->type() == Item::FIELD_ITEM)
5981
15021
      {
5982
 
        item_field= item->get_tmp_table_item(session);
 
15022
        item_field= item->get_tmp_table_item(thd);
5983
15023
      }
5984
15024
      else if ((field= item->get_tmp_table_field()))
5985
15025
      {
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
 
15026
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
15027
          item_field= ((Item_sum*) item)->result_item(field);
 
15028
        else
 
15029
          item_field= (Item*) new Item_field(field);
 
15030
        if (!item_field)
 
15031
          return(true);                    // Fatal error
5992
15032
 
5993
15033
        if (item->real_item()->type() != Item::FIELD_ITEM)
5994
15034
          field->orig_table= 0;
5995
 
        item_field->name= item->name;
 
15035
        item_field->name= item->name;
5996
15036
        if (item->type() == Item::REF_ITEM)
5997
15037
        {
5998
15038
          Item_field *ifield= (Item_field *) item_field;
6002
15042
        }
6003
15043
      }
6004
15044
      else
6005
 
        item_field= item;
 
15045
        item_field= item;
6006
15046
    }
6007
15047
    res_all_fields.push_back(item_field);
6008
15048
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6009
15049
      item_field;
6010
15050
  }
6011
15051
 
6012
 
  List<Item>::iterator itr(res_all_fields.begin());
 
15052
  List_iterator_fast<Item> itr(res_all_fields);
6013
15053
  for (i= 0; i < border; i++)
6014
15054
    itr++;
6015
15055
  itr.sublist(res_selected_fields, elements);
6016
15056
  return(false);
6017
15057
}
6018
15058
 
 
15059
 
6019
15060
/**
6020
15061
  Change all sum_func refs to fields to point at fields in tmp table.
6021
15062
  Change all funcs to be fields in tmp table.
6022
15063
 
6023
 
  @param session                   Session pointer
 
15064
  @param thd                   THD pointer
6024
15065
  @param ref_pointer_array     array of pointers to top elements of filed list
6025
15066
  @param res_selected_fields   new list of items of select item list
6026
15067
  @param res_all_fields        new list of all items
6032
15073
  @retval
6033
15074
    1   error
6034
15075
*/
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)
 
15076
 
 
15077
static bool
 
15078
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
15079
                          List<Item> &res_selected_fields,
 
15080
                          List<Item> &res_all_fields, uint32_t elements,
 
15081
                          List<Item> &all_fields)
6041
15082
{
6042
 
  List<Item>::iterator it(all_fields.begin());
 
15083
  List_iterator_fast<Item> it(all_fields);
6043
15084
  Item *item, *new_item;
6044
 
  res_selected_fields.clear();
6045
 
  res_all_fields.clear();
 
15085
  res_selected_fields.empty();
 
15086
  res_all_fields.empty();
6046
15087
 
6047
15088
  uint32_t i, border= all_fields.elements - elements;
6048
15089
  for (i= 0; (item= it++); i++)
6049
15090
  {
6050
 
    res_all_fields.push_back(new_item= item->get_tmp_table_item(session));
 
15091
    res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
6051
15092
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6052
15093
      new_item;
6053
15094
  }
6054
15095
 
6055
 
  List<Item>::iterator itr(res_all_fields.begin());
 
15096
  List_iterator_fast<Item> itr(res_all_fields);
6056
15097
  for (i= 0; i < border; i++)
6057
15098
    itr++;
6058
15099
  itr.sublist(res_selected_fields, elements);
6059
15100
 
6060
 
  return session->is_fatal_error;
 
15101
  return thd->is_fatal_error;
6061
15102
}
6062
15103
 
 
15104
 
 
15105
 
6063
15106
/******************************************************************************
6064
15107
  Code for calculating functions
6065
15108
******************************************************************************/
6066
15109
 
 
15110
 
6067
15111
/**
6068
15112
  Call ::setup for all sum functions.
6069
15113
 
6070
 
  @param session           thread Cursor
 
15114
  @param thd           thread handler
6071
15115
  @param func_ptr      sum function list
6072
15116
 
6073
15117
  @retval
6075
15119
  @retval
6076
15120
    true   error
6077
15121
*/
6078
 
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
 
15122
 
 
15123
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
6079
15124
{
6080
15125
  Item_sum *func;
6081
15126
  while ((func= *(func_ptr++)))
6082
15127
  {
6083
 
    if (func->setup(session))
 
15128
    if (func->setup(thd))
6084
15129
      return(true);
6085
15130
  }
6086
15131
  return(false);
6087
15132
}
6088
15133
 
6089
 
void init_tmptable_sum_functions(Item_sum **func_ptr)
 
15134
 
 
15135
static void
 
15136
init_tmptable_sum_functions(Item_sum **func_ptr)
6090
15137
{
6091
15138
  Item_sum *func;
6092
15139
  while ((func= *(func_ptr++)))
6093
15140
    func->reset_field();
6094
15141
}
6095
15142
 
 
15143
 
6096
15144
/** Update record 0 in tmp_table from record 1. */
6097
 
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
 
15145
 
 
15146
static void
 
15147
update_tmptable_sum_func(Item_sum **func_ptr,
 
15148
                         Table *tmp_table __attribute__((unused)))
6098
15149
{
6099
15150
  Item_sum *func;
6100
15151
  while ((func= *(func_ptr++)))
6101
15152
    func->update_field();
6102
15153
}
6103
15154
 
 
15155
 
6104
15156
/** Copy result of sum functions to record in tmp_table. */
6105
 
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
15157
 
 
15158
static void
 
15159
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
6106
15160
{
6107
15161
  for (; func_ptr != end_ptr ; func_ptr++)
6108
15162
    (void) (*func_ptr)->save_in_result_field(1);
6109
15163
  return;
6110
15164
}
6111
15165
 
6112
 
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
15166
 
 
15167
static bool
 
15168
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6113
15169
{
6114
15170
  for (; func_ptr != end_ptr ;func_ptr++)
6115
15171
  {
6125
15181
  return 0;
6126
15182
}
6127
15183
 
6128
 
bool update_sum_func(Item_sum **func_ptr)
 
15184
 
 
15185
static bool
 
15186
update_sum_func(Item_sum **func_ptr)
6129
15187
{
6130
15188
  Item_sum *func;
6131
15189
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6135
15193
}
6136
15194
 
6137
15195
/** Copy result of functions to record in tmp_table. */
6138
 
bool copy_funcs(Item **func_ptr, const Session *session)
 
15196
 
 
15197
void
 
15198
copy_funcs(Item **func_ptr)
6139
15199
{
6140
15200
  Item *func;
6141
15201
  for (; (func = *func_ptr) ; func_ptr++)
6142
 
  {
6143
15202
    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
 
}
 
15203
}
 
15204
 
 
15205
 
 
15206
/**
 
15207
  Create a condition for a const reference and add this to the
 
15208
  currenct select for the table.
 
15209
*/
 
15210
 
 
15211
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
 
15212
{
 
15213
  if (!join_tab->ref.key_parts)
 
15214
    return(false);
 
15215
 
 
15216
  Item_cond_and *cond=new Item_cond_and();
 
15217
  Table *table=join_tab->table;
 
15218
  int error;
 
15219
  if (!cond)
 
15220
    return(true);
 
15221
 
 
15222
  for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
 
15223
  {
 
15224
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
 
15225
                              fieldnr-1];
 
15226
    Item *value=join_tab->ref.items[i];
 
15227
    cond->add(new Item_func_equal(new Item_field(field), value));
 
15228
  }
 
15229
  if (thd->is_fatal_error)
 
15230
    return(true);
 
15231
 
 
15232
  if (!cond->fixed)
 
15233
    cond->fix_fields(thd, (Item**)&cond);
 
15234
  if (join_tab->select)
 
15235
  {
 
15236
    error=(int) cond->add(join_tab->select->cond);
 
15237
    join_tab->select_cond=join_tab->select->cond=cond;
 
15238
  }
 
15239
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
 
15240
                                          &error)))
 
15241
    join_tab->select_cond=cond;
 
15242
 
 
15243
  return(error ? true : false);
 
15244
}
 
15245
 
6155
15246
 
6156
15247
/**
6157
15248
  Free joins of subselect of this select.
6158
15249
 
6159
 
  @param session      Session pointer
6160
 
  @param select   pointer to Select_Lex which subselects joins we will free
 
15250
  @param thd      THD pointer
 
15251
  @param select   pointer to st_select_lex which subselects joins we will free
6161
15252
*/
6162
 
void free_underlaid_joins(Session *, Select_Lex *select)
 
15253
 
 
15254
void free_underlaid_joins(THD *thd __attribute__((unused)),
 
15255
                          SELECT_LEX *select)
6163
15256
{
6164
 
  for (Select_Lex_Unit *unit= select->first_inner_unit();
 
15257
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
6165
15258
       unit;
6166
15259
       unit= unit->next_unit())
6167
15260
    unit->cleanup();
6183
15276
  @b EXAMPLES
6184
15277
    @code
6185
15278
      SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP
6186
 
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP
 
15279
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP 
6187
15280
  @endcode
6188
15281
 
6189
15282
  @b IMPLEMENTATION
6196
15289
    This substitution is needed GROUP BY queries with ROLLUP if
6197
15290
    SELECT list contains expressions over group by attributes.
6198
15291
 
6199
 
  @param session                  reference to the context
 
15292
  @param thd                  reference to the context
6200
15293
  @param expr                 expression to make replacement
6201
15294
  @param group_list           list of references to group by items
6202
15295
  @param changed        out:  returns 1 if item contains a replaced field item
6203
15296
 
6204
15297
  @todo
6205
 
    - @todo Some functions are not null-preserving. For those functions
6206
 
    updating of the maybe_null attribute is an overkill.
 
15298
    - TODO: Some functions are not null-preserving. For those functions
 
15299
    updating of the maybe_null attribute is an overkill. 
6207
15300
 
6208
15301
  @retval
6209
15302
    0   if ok
6210
15303
  @retval
6211
15304
    1   on error
6212
15305
*/
6213
 
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
 
15306
 
 
15307
static bool change_group_ref(THD *thd, Item_func *expr, order_st *group_list,
 
15308
                             bool *changed)
6214
15309
{
6215
15310
  if (expr->arg_count)
6216
15311
  {
6217
 
    Name_resolution_context *context= &session->getLex()->current_select->context;
 
15312
    Name_resolution_context *context= &thd->lex->current_select->context;
6218
15313
    Item **arg,**arg_end;
6219
15314
    bool arg_changed= false;
6220
15315
    for (arg= expr->arguments(),
6224
15319
      Item *item= *arg;
6225
15320
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6226
15321
      {
6227
 
        Order *group_tmp;
 
15322
        order_st *group_tmp;
6228
15323
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6229
15324
        {
6230
15325
          if (item->eq(*group_tmp->item,0))
6233
15328
            if (!(new_item= new Item_ref(context, group_tmp->item, 0,
6234
15329
                                        item->name)))
6235
15330
              return 1;                                 // fatal_error is set
6236
 
            session->change_item_tree(arg, new_item);
 
15331
            thd->change_item_tree(arg, new_item);
6237
15332
            arg_changed= true;
6238
15333
          }
6239
15334
        }
6240
15335
      }
6241
15336
      else if (item->type() == Item::FUNC_ITEM)
6242
15337
      {
6243
 
        if (change_group_ref(session, (Item_func *) item, group_list, &arg_changed))
 
15338
        if (change_group_ref(thd, (Item_func *) item, group_list, &arg_changed))
6244
15339
          return 1;
6245
15340
      }
6246
15341
    }
6254
15349
}
6255
15350
 
6256
15351
 
6257
 
static void print_table_array(Session *session, String *str, TableList **table,
 
15352
/** Allocate memory needed for other rollup functions. */
 
15353
 
 
15354
bool JOIN::rollup_init()
 
15355
{
 
15356
  uint32_t i,j;
 
15357
  Item **ref_array;
 
15358
 
 
15359
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
 
15360
  rollup.state= ROLLUP::STATE_INITED;
 
15361
 
 
15362
  /*
 
15363
    Create pointers to the different sum function groups
 
15364
    These are updated by rollup_make_fields()
 
15365
  */
 
15366
  tmp_table_param.group_parts= send_group_parts;
 
15367
 
 
15368
  if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
 
15369
                                                sizeof(Item**) +
 
15370
                                                sizeof(List<Item>) +
 
15371
                                                ref_pointer_array_size)
 
15372
                                                * send_group_parts )))
 
15373
    return 1;
 
15374
  
 
15375
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
15376
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
15377
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
15378
 
 
15379
  /*
 
15380
    Prepare space for field list for the different levels
 
15381
    These will be filled up in rollup_make_fields()
 
15382
  */
 
15383
  for (i= 0 ; i < send_group_parts ; i++)
 
15384
  {
 
15385
    rollup.null_items[i]= new (thd->mem_root) Item_null_result();
 
15386
    List<Item> *rollup_fields= &rollup.fields[i];
 
15387
    rollup_fields->empty();
 
15388
    rollup.ref_pointer_arrays[i]= ref_array;
 
15389
    ref_array+= all_fields.elements;
 
15390
  }
 
15391
  for (i= 0 ; i < send_group_parts; i++)
 
15392
  {
 
15393
    for (j=0 ; j < fields_list.elements ; j++)
 
15394
      rollup.fields[i].push_back(rollup.null_items[i]);
 
15395
  }
 
15396
  List_iterator<Item> it(all_fields);
 
15397
  Item *item;
 
15398
  while ((item= it++))
 
15399
  {
 
15400
    order_st *group_tmp;
 
15401
    bool found_in_group= 0;
 
15402
 
 
15403
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
 
15404
    {
 
15405
      if (*group_tmp->item == item)
 
15406
      {
 
15407
        item->maybe_null= 1;
 
15408
        found_in_group= 1;
 
15409
        if (item->const_item())
 
15410
        {
 
15411
          /*
 
15412
            For ROLLUP queries each constant item referenced in GROUP BY list
 
15413
            is wrapped up into an Item_func object yielding the same value
 
15414
            as the constant item. The objects of the wrapper class are never
 
15415
            considered as constant items and besides they inherit all
 
15416
            properties of the Item_result_field class.
 
15417
            This wrapping allows us to ensure writing constant items
 
15418
            into temporary tables whenever the result of the ROLLUP
 
15419
            operation has to be written into a temporary table, e.g. when
 
15420
            ROLLUP is used together with DISTINCT in the SELECT list.
 
15421
            Usually when creating temporary tables for a intermidiate
 
15422
            result we do not include fields for constant expressions.
 
15423
          */           
 
15424
          Item* new_item= new Item_func_rollup_const(item);
 
15425
          if (!new_item)
 
15426
            return 1;
 
15427
          new_item->fix_fields(thd, (Item **) 0);
 
15428
          thd->change_item_tree(it.ref(), new_item);
 
15429
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
 
15430
          { 
 
15431
            if (*tmp->item == item)
 
15432
              thd->change_item_tree(tmp->item, new_item);
 
15433
          }
 
15434
        }
 
15435
      }
 
15436
    }
 
15437
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
 
15438
    {
 
15439
      bool changed= false;
 
15440
      if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
 
15441
        return 1;
 
15442
      /*
 
15443
        We have to prevent creation of a field in a temporary table for
 
15444
        an expression that contains GROUP BY attributes.
 
15445
        Marking the expression item as 'with_sum_func' will ensure this.
 
15446
      */ 
 
15447
      if (changed)
 
15448
        item->with_sum_func= 1;
 
15449
    }
 
15450
  }
 
15451
  return 0;
 
15452
}
 
15453
  
 
15454
 
 
15455
/**
 
15456
  Fill up rollup structures with pointers to fields to use.
 
15457
 
 
15458
  Creates copies of item_sum items for each sum level.
 
15459
 
 
15460
  @param fields_arg             List of all fields (hidden and real ones)
 
15461
  @param sel_fields             Pointer to selected fields
 
15462
  @param func                   Store here a pointer to all fields
 
15463
 
 
15464
  @retval
 
15465
    0   if ok;
 
15466
    In this case func is pointing to next not used element.
 
15467
  @retval
 
15468
    1    on error
 
15469
*/
 
15470
 
 
15471
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
 
15472
                              Item_sum ***func)
 
15473
{
 
15474
  List_iterator_fast<Item> it(fields_arg);
 
15475
  Item *first_field= sel_fields.head();
 
15476
  uint32_t level;
 
15477
 
 
15478
  /*
 
15479
    Create field lists for the different levels
 
15480
 
 
15481
    The idea here is to have a separate field list for each rollup level to
 
15482
    avoid all runtime checks of which columns should be NULL.
 
15483
 
 
15484
    The list is stored in reverse order to get sum function in such an order
 
15485
    in func that it makes it easy to reset them with init_sum_functions()
 
15486
 
 
15487
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15488
 
 
15489
    rollup.fields[0] will contain list where a,b,c is NULL
 
15490
    rollup.fields[1] will contain list where b,c is NULL
 
15491
    ...
 
15492
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
 
15493
    ...
 
15494
    sum_funcs_end[0] points to all sum functions
 
15495
    sum_funcs_end[1] points to all sum functions, except grand totals
 
15496
    ...
 
15497
  */
 
15498
 
 
15499
  for (level=0 ; level < send_group_parts ; level++)
 
15500
  {
 
15501
    uint32_t i;
 
15502
    uint32_t pos= send_group_parts - level -1;
 
15503
    bool real_fields= 0;
 
15504
    Item *item;
 
15505
    List_iterator<Item> new_it(rollup.fields[pos]);
 
15506
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
15507
    order_st *start_group;
 
15508
 
 
15509
    /* Point to first hidden field */
 
15510
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
15511
 
 
15512
    /* Remember where the sum functions ends for the previous level */
 
15513
    sum_funcs_end[pos+1]= *func;
 
15514
 
 
15515
    /* Find the start of the group for this level */
 
15516
    for (i= 0, start_group= group_list ;
 
15517
         i++ < pos ;
 
15518
         start_group= start_group->next)
 
15519
      ;
 
15520
 
 
15521
    it.rewind();
 
15522
    while ((item= it++))
 
15523
    {
 
15524
      if (item == first_field)
 
15525
      {
 
15526
        real_fields= 1;                         // End of hidden fields
 
15527
        ref_array= ref_array_start;
 
15528
      }
 
15529
 
 
15530
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
15531
          (!((Item_sum*) item)->depended_from() ||
 
15532
           ((Item_sum *)item)->depended_from() == select_lex))
 
15533
          
 
15534
      {
 
15535
        /*
 
15536
          This is a top level summary function that must be replaced with
 
15537
          a sum function that is reset for this level.
 
15538
 
 
15539
          NOTE: This code creates an object which is not that nice in a
 
15540
          sub select.  Fortunately it's not common to have rollup in
 
15541
          sub selects.
 
15542
        */
 
15543
        item= item->copy_or_same(thd);
 
15544
        ((Item_sum*) item)->make_unique();
 
15545
        *(*func)= (Item_sum*) item;
 
15546
        (*func)++;
 
15547
      }
 
15548
      else 
 
15549
      {
 
15550
        /* Check if this is something that is part of this group by */
 
15551
        order_st *group_tmp;
 
15552
        for (group_tmp= start_group, i= pos ;
 
15553
             group_tmp ; group_tmp= group_tmp->next, i++)
 
15554
        {
 
15555
          if (*group_tmp->item == item)
 
15556
          {
 
15557
            /*
 
15558
              This is an element that is used by the GROUP BY and should be
 
15559
              set to NULL in this level
 
15560
            */
 
15561
            Item_null_result *null_item= new (thd->mem_root) Item_null_result();
 
15562
            if (!null_item)
 
15563
              return 1;
 
15564
            item->maybe_null= 1;                // Value will be null sometimes
 
15565
            null_item->result_field= item->get_tmp_table_field();
 
15566
            item= null_item;
 
15567
            break;
 
15568
          }
 
15569
        }
 
15570
      }
 
15571
      *ref_array= item;
 
15572
      if (real_fields)
 
15573
      {
 
15574
        (void) new_it++;                        // Point to next item
 
15575
        new_it.replace(item);                   // Replace previous
 
15576
        ref_array++;
 
15577
      }
 
15578
      else
 
15579
        ref_array--;
 
15580
    }
 
15581
  }
 
15582
  sum_funcs_end[0]= *func;                      // Point to last function
 
15583
  return 0;
 
15584
}
 
15585
 
 
15586
/**
 
15587
  Send all rollup levels higher than the current one to the client.
 
15588
 
 
15589
  @b SAMPLE
 
15590
    @code
 
15591
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15592
  @endcode
 
15593
 
 
15594
  @param idx            Level we are on:
 
15595
                        - 0 = Total sum level
 
15596
                        - 1 = First group changed  (a)
 
15597
                        - 2 = Second group changed (a,b)
 
15598
 
 
15599
  @retval
 
15600
    0   ok
 
15601
  @retval
 
15602
    1   If send_data_failed()
 
15603
*/
 
15604
 
 
15605
int JOIN::rollup_send_data(uint32_t idx)
 
15606
{
 
15607
  uint32_t i;
 
15608
  for (i= send_group_parts ; i-- > idx ; )
 
15609
  {
 
15610
    /* Get reference pointers to sum functions in place */
 
15611
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15612
           ref_pointer_array_size);
 
15613
    if ((!having || having->val_int()))
 
15614
    {
 
15615
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
15616
          result->send_data(rollup.fields[i]))
 
15617
        return 1;
 
15618
      send_records++;
 
15619
    }
 
15620
  }
 
15621
  /* Restore ref_pointer_array */
 
15622
  set_items_ref_array(current_ref_pointer_array);
 
15623
  return 0;
 
15624
}
 
15625
 
 
15626
/**
 
15627
  Write all rollup levels higher than the current one to a temp table.
 
15628
 
 
15629
  @b SAMPLE
 
15630
    @code
 
15631
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
 
15632
  @endcode
 
15633
 
 
15634
  @param idx                 Level we are on:
 
15635
                               - 0 = Total sum level
 
15636
                               - 1 = First group changed  (a)
 
15637
                               - 2 = Second group changed (a,b)
 
15638
  @param table               reference to temp table
 
15639
 
 
15640
  @retval
 
15641
    0   ok
 
15642
  @retval
 
15643
    1   if write_data_failed()
 
15644
*/
 
15645
 
 
15646
int JOIN::rollup_write_data(uint32_t idx, Table *table_arg)
 
15647
{
 
15648
  uint32_t i;
 
15649
  for (i= send_group_parts ; i-- > idx ; )
 
15650
  {
 
15651
    /* Get reference pointers to sum functions in place */
 
15652
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15653
           ref_pointer_array_size);
 
15654
    if ((!having || having->val_int()))
 
15655
    {
 
15656
      int write_error;
 
15657
      Item *item;
 
15658
      List_iterator_fast<Item> it(rollup.fields[i]);
 
15659
      while ((item= it++))
 
15660
      {
 
15661
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
 
15662
          item->save_in_result_field(1);
 
15663
      }
 
15664
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
 
15665
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
 
15666
      {
 
15667
        if (create_myisam_from_heap(thd, table_arg, 
 
15668
                                    tmp_table_param.start_recinfo,
 
15669
                                    &tmp_table_param.recinfo,
 
15670
                                    write_error, 0))
 
15671
          return 1;                  
 
15672
      }
 
15673
    }
 
15674
  }
 
15675
  /* Restore ref_pointer_array */
 
15676
  set_items_ref_array(current_ref_pointer_array);
 
15677
  return 0;
 
15678
}
 
15679
 
 
15680
/**
 
15681
  clear results if there are not rows found for group
 
15682
  (end_send_group/end_write_group)
 
15683
*/
 
15684
 
 
15685
void JOIN::clear()
 
15686
{
 
15687
  clear_tables(this);
 
15688
  copy_fields(&tmp_table_param);
 
15689
 
 
15690
  if (sum_funcs)
 
15691
  {
 
15692
    Item_sum *func, **func_ptr= sum_funcs;
 
15693
    while ((func= *(func_ptr++)))
 
15694
      func->clear();
 
15695
  }
 
15696
}
 
15697
 
 
15698
/**
 
15699
  EXPLAIN handling.
 
15700
 
 
15701
  Send a description about what how the select will be done to stdout.
 
15702
*/
 
15703
 
 
15704
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
 
15705
                     bool distinct,const char *message)
 
15706
{
 
15707
  List<Item> field_list;
 
15708
  List<Item> item_list;
 
15709
  THD *thd=join->thd;
 
15710
  select_result *result=join->result;
 
15711
  Item *item_null= new Item_null();
 
15712
  const CHARSET_INFO * const cs= system_charset_info;
 
15713
  int quick_type;
 
15714
  /* Don't log this into the slow query log */
 
15715
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
 
15716
  join->unit->offset_limit_cnt= 0;
 
15717
 
 
15718
  /* 
 
15719
    NOTE: the number/types of items pushed into item_list must be in sync with
 
15720
    EXPLAIN column types as they're "defined" in THD::send_explain_fields()
 
15721
  */
 
15722
  if (message)
 
15723
  {
 
15724
    item_list.push_back(new Item_int((int32_t)
 
15725
                                     join->select_lex->select_number));
 
15726
    item_list.push_back(new Item_string(join->select_lex->type,
 
15727
                                        strlen(join->select_lex->type), cs));
 
15728
    for (uint32_t i=0 ; i < 7; i++)
 
15729
      item_list.push_back(item_null);
 
15730
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15731
      item_list.push_back(item_null);
 
15732
  
 
15733
    item_list.push_back(new Item_string(message,strlen(message),cs));
 
15734
    if (result->send_data(item_list))
 
15735
      join->error= 1;
 
15736
  }
 
15737
  else if (join->select_lex == join->unit->fake_select_lex)
 
15738
  {
 
15739
    /* 
 
15740
      here we assume that the query will return at least two rows, so we
 
15741
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
 
15742
      and no filesort will be actually done, but executing all selects in
 
15743
      the UNION to provide precise EXPLAIN information will hardly be
 
15744
      appreciated :)
 
15745
    */
 
15746
    char table_name_buffer[NAME_LEN];
 
15747
    item_list.empty();
 
15748
    /* id */
 
15749
    item_list.push_back(new Item_null);
 
15750
    /* select_type */
 
15751
    item_list.push_back(new Item_string(join->select_lex->type,
 
15752
                                        strlen(join->select_lex->type),
 
15753
                                        cs));
 
15754
    /* table */
 
15755
    {
 
15756
      SELECT_LEX *sl= join->unit->first_select();
 
15757
      uint32_t len= 6, lastop= 0;
 
15758
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
 
15759
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
 
15760
      {
 
15761
        len+= lastop;
 
15762
        lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
 
15763
                         "%u,", sl->select_number);
 
15764
      }
 
15765
      if (sl || len + lastop >= NAME_LEN)
 
15766
      {
 
15767
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
 
15768
        len+= 4;
 
15769
      }
 
15770
      else
 
15771
      {
 
15772
        len+= lastop;
 
15773
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
 
15774
      }
 
15775
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
15776
    }
 
15777
    /* type */
 
15778
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
 
15779
                                          strlen(join_type_str[JT_ALL]),
 
15780
                                          cs));
 
15781
    /* possible_keys */
 
15782
    item_list.push_back(item_null);
 
15783
    /* key*/
 
15784
    item_list.push_back(item_null);
 
15785
    /* key_len */
 
15786
    item_list.push_back(item_null);
 
15787
    /* ref */
 
15788
    item_list.push_back(item_null);
 
15789
    /* in_rows */
 
15790
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15791
      item_list.push_back(item_null);
 
15792
    /* rows */
 
15793
    item_list.push_back(item_null);
 
15794
    /* extra */
 
15795
    if (join->unit->global_parameters->order_list.first)
 
15796
      item_list.push_back(new Item_string("Using filesort",
 
15797
                                          14, cs));
 
15798
    else
 
15799
      item_list.push_back(new Item_string("", 0, cs));
 
15800
 
 
15801
    if (result->send_data(item_list))
 
15802
      join->error= 1;
 
15803
  }
 
15804
  else
 
15805
  {
 
15806
    table_map used_tables=0;
 
15807
    for (uint32_t i=0 ; i < join->tables ; i++)
 
15808
    {
 
15809
      JOIN_TAB *tab=join->join_tab+i;
 
15810
      Table *table=tab->table;
 
15811
      TableList *table_list= tab->table->pos_in_table_list;
 
15812
      char buff[512]; 
 
15813
      char buff1[512], buff2[512], buff3[512];
 
15814
      char keylen_str_buf[64];
 
15815
      String extra(buff, sizeof(buff),cs);
 
15816
      char table_name_buffer[NAME_LEN];
 
15817
      String tmp1(buff1,sizeof(buff1),cs);
 
15818
      String tmp2(buff2,sizeof(buff2),cs);
 
15819
      String tmp3(buff3,sizeof(buff3),cs);
 
15820
      extra.length(0);
 
15821
      tmp1.length(0);
 
15822
      tmp2.length(0);
 
15823
      tmp3.length(0);
 
15824
 
 
15825
      quick_type= -1;
 
15826
      item_list.empty();
 
15827
      /* id */
 
15828
      item_list.push_back(new Item_uint((uint32_t)
 
15829
                                       join->select_lex->select_number));
 
15830
      /* select_type */
 
15831
      item_list.push_back(new Item_string(join->select_lex->type,
 
15832
                                          strlen(join->select_lex->type),
 
15833
                                          cs));
 
15834
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
 
15835
      {
 
15836
        quick_type= tab->select->quick->get_type();
 
15837
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
 
15838
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
 
15839
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
 
15840
          tab->type = JT_INDEX_MERGE;
 
15841
        else
 
15842
          tab->type = JT_RANGE;
 
15843
      }
 
15844
      /* table */
 
15845
      if (table->derived_select_number)
 
15846
      {
 
15847
        /* Derived table name generation */
 
15848
        int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
 
15849
                          "<derived%u>",
 
15850
                          table->derived_select_number);
 
15851
        item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
15852
      }
 
15853
      else
 
15854
      {
 
15855
        TableList *real_table= table->pos_in_table_list; 
 
15856
        item_list.push_back(new Item_string(real_table->alias,
 
15857
                                            strlen(real_table->alias),
 
15858
                                            cs));
 
15859
      }
 
15860
      /* "type" column */
 
15861
      item_list.push_back(new Item_string(join_type_str[tab->type],
 
15862
                                          strlen(join_type_str[tab->type]),
 
15863
                                          cs));
 
15864
      /* Build "possible_keys" value and add it to item_list */
 
15865
      if (!tab->keys.is_clear_all())
 
15866
      {
 
15867
        uint32_t j;
 
15868
        for (j=0 ; j < table->s->keys ; j++)
 
15869
        {
 
15870
          if (tab->keys.is_set(j))
 
15871
          {
 
15872
            if (tmp1.length())
 
15873
              tmp1.append(',');
 
15874
            tmp1.append(table->key_info[j].name, 
 
15875
                        strlen(table->key_info[j].name),
 
15876
                        system_charset_info);
 
15877
          }
 
15878
        }
 
15879
      }
 
15880
      if (tmp1.length())
 
15881
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
 
15882
      else
 
15883
        item_list.push_back(item_null);
 
15884
 
 
15885
      /* Build "key", "key_len", and "ref" values and add them to item_list */
 
15886
      if (tab->ref.key_parts)
 
15887
      {
 
15888
        KEY *key_info=table->key_info+ tab->ref.key;
 
15889
        register uint32_t length;
 
15890
        item_list.push_back(new Item_string(key_info->name,
 
15891
                                            strlen(key_info->name),
 
15892
                                            system_charset_info));
 
15893
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) - 
 
15894
                keylen_str_buf;
 
15895
        item_list.push_back(new Item_string(keylen_str_buf, length,
 
15896
                                            system_charset_info));
 
15897
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
 
15898
        {
 
15899
          if (tmp2.length())
 
15900
            tmp2.append(',');
 
15901
          tmp2.append((*ref)->name(), strlen((*ref)->name()),
 
15902
                      system_charset_info);
 
15903
        }
 
15904
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15905
      }
 
15906
      else if (tab->type == JT_NEXT)
 
15907
      {
 
15908
        KEY *key_info=table->key_info+ tab->index;
 
15909
        register uint32_t length;
 
15910
        item_list.push_back(new Item_string(key_info->name,
 
15911
                                            strlen(key_info->name),cs));
 
15912
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) - 
 
15913
                keylen_str_buf;
 
15914
        item_list.push_back(new Item_string(keylen_str_buf, 
 
15915
                                            length,
 
15916
                                            system_charset_info));
 
15917
        item_list.push_back(item_null);
 
15918
      }
 
15919
      else if (tab->select && tab->select->quick)
 
15920
      {
 
15921
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
 
15922
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15923
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
 
15924
        item_list.push_back(item_null);
 
15925
      }
 
15926
      else
 
15927
      {
 
15928
        if (table_list->schema_table && table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
15929
        {
 
15930
          const char *tmp_buff;
 
15931
          int f_idx;
 
15932
          if (table_list->has_db_lookup_value)
 
15933
          {
 
15934
            f_idx= table_list->schema_table->idx_field1;
 
15935
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
15936
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
15937
          }          
 
15938
          if (table_list->has_table_lookup_value)
 
15939
          {
 
15940
            if (table_list->has_db_lookup_value)
 
15941
              tmp2.append(',');
 
15942
            f_idx= table_list->schema_table->idx_field2;
 
15943
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
15944
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
15945
          }
 
15946
          if (tmp2.length())
 
15947
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15948
          else
 
15949
            item_list.push_back(item_null);
 
15950
        }
 
15951
        else
 
15952
          item_list.push_back(item_null);
 
15953
        item_list.push_back(item_null);
 
15954
        item_list.push_back(item_null);
 
15955
      }
 
15956
      
 
15957
      /* Add "rows" field to item_list. */
 
15958
      if (table_list->schema_table)
 
15959
      {
 
15960
        /* in_rows */
 
15961
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15962
          item_list.push_back(item_null);
 
15963
        /* rows */
 
15964
        item_list.push_back(item_null);
 
15965
      }
 
15966
      else
 
15967
      {
 
15968
        double examined_rows;
 
15969
        if (tab->select && tab->select->quick)
 
15970
          examined_rows= rows2double(tab->select->quick->records);
 
15971
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
 
15972
          examined_rows= rows2double(tab->limit ? tab->limit : 
 
15973
                                     tab->table->file->records());
 
15974
        else
 
15975
          examined_rows= join->best_positions[i].records_read; 
 
15976
 
 
15977
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows, 
 
15978
                                         MY_INT64_NUM_DECIMAL_DIGITS));
 
15979
 
 
15980
        /* Add "filtered" field to item_list. */
 
15981
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15982
        {
 
15983
          float f= 0.0; 
 
15984
          if (examined_rows)
 
15985
            f= (float) (100.0 * join->best_positions[i].records_read /
 
15986
                        examined_rows);
 
15987
          item_list.push_back(new Item_float(f, 2));
 
15988
        }
 
15989
      }
 
15990
 
 
15991
      /* Build "Extra" field and add it to item_list. */
 
15992
      bool key_read=table->key_read;
 
15993
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
 
15994
          table->covering_keys.is_set(tab->index))
 
15995
        key_read=1;
 
15996
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
 
15997
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
 
15998
        key_read=1;
 
15999
        
 
16000
      if (tab->info)
 
16001
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
 
16002
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
 
16003
      {
 
16004
        if (tab->packed_info & TAB_INFO_USING_INDEX)
 
16005
          extra.append(STRING_WITH_LEN("; Using index"));
 
16006
        if (tab->packed_info & TAB_INFO_USING_WHERE)
 
16007
          extra.append(STRING_WITH_LEN("; Using where"));
 
16008
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
 
16009
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
16010
        /* Skip initial "; "*/
 
16011
        const char *str= extra.ptr();
 
16012
        uint32_t len= extra.length();
 
16013
        if (len)
 
16014
        {
 
16015
          str += 2;
 
16016
          len -= 2;
 
16017
        }
 
16018
        item_list.push_back(new Item_string(str, len, cs));
 
16019
      }
 
16020
      else
 
16021
      {
 
16022
        uint32_t keyno= MAX_KEY;
 
16023
        if (tab->ref.key_parts)
 
16024
          keyno= tab->ref.key;
 
16025
        else if (tab->select && tab->select->quick)
 
16026
          keyno = tab->select->quick->index;
 
16027
 
 
16028
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
 
16029
            table->file->pushed_idx_cond)
 
16030
          extra.append(STRING_WITH_LEN("; Using index condition"));
 
16031
 
 
16032
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
 
16033
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
16034
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
 
16035
        {
 
16036
          extra.append(STRING_WITH_LEN("; Using "));
 
16037
          tab->select->quick->add_info_string(&extra);
 
16038
        }
 
16039
          if (tab->select)
 
16040
        {
 
16041
          if (tab->use_quick == 2)
 
16042
          {
 
16043
            /* 4 bits per 1 hex digit + terminating '\0' */
 
16044
            char buf[MAX_KEY / 4 + 1];
 
16045
            extra.append(STRING_WITH_LEN("; Range checked for each "
 
16046
                                         "record (index map: 0x"));
 
16047
            extra.append(tab->keys.print(buf));
 
16048
            extra.append(')');
 
16049
          }
 
16050
          else if (tab->select->cond)
 
16051
          {
 
16052
            const COND *pushed_cond= tab->table->file->pushed_cond;
 
16053
 
 
16054
            if (thd->variables.engine_condition_pushdown && pushed_cond)
 
16055
            {
 
16056
              extra.append(STRING_WITH_LEN("; Using where with pushed "
 
16057
                                           "condition"));
 
16058
              if (thd->lex->describe & DESCRIBE_EXTENDED)
 
16059
              {
 
16060
                extra.append(STRING_WITH_LEN(": "));
 
16061
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
 
16062
              }
 
16063
            }
 
16064
            else
 
16065
              extra.append(STRING_WITH_LEN("; Using where"));
 
16066
          }
 
16067
        }
 
16068
        if (key_read)
 
16069
        {
 
16070
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
16071
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
 
16072
          else
 
16073
            extra.append(STRING_WITH_LEN("; Using index"));
 
16074
        }
 
16075
        if (table->reginfo.not_exists_optimize)
 
16076
          extra.append(STRING_WITH_LEN("; Not exists"));
 
16077
          
 
16078
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
 
16079
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
 
16080
             HA_MRR_USE_DEFAULT_IMPL))
 
16081
        {
 
16082
          extra.append(STRING_WITH_LEN("; Using MRR"));
 
16083
        }
 
16084
 
 
16085
        if (table_list->schema_table &&
 
16086
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
16087
        {
 
16088
          if (!table_list->table_open_method)
 
16089
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
 
16090
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
 
16091
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
 
16092
          else
 
16093
            extra.append(STRING_WITH_LEN("; Open_full_table"));
 
16094
          if (table_list->has_db_lookup_value &&
 
16095
              table_list->has_table_lookup_value)
 
16096
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
 
16097
          else if (table_list->has_db_lookup_value ||
 
16098
                   table_list->has_table_lookup_value)
 
16099
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
 
16100
          else
 
16101
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
 
16102
        }
 
16103
        if (need_tmp_table)
 
16104
        {
 
16105
          need_tmp_table=0;
 
16106
          extra.append(STRING_WITH_LEN("; Using temporary"));
 
16107
        }
 
16108
        if (need_order)
 
16109
        {
 
16110
          need_order=0;
 
16111
          extra.append(STRING_WITH_LEN("; Using filesort"));
 
16112
        }
 
16113
        if (distinct & test_all_bits(used_tables,thd->used_tables))
 
16114
          extra.append(STRING_WITH_LEN("; Distinct"));
 
16115
 
 
16116
        if (tab->insideout_match_tab)
 
16117
        {
 
16118
          extra.append(STRING_WITH_LEN("; LooseScan"));
 
16119
        }
 
16120
 
 
16121
        if (tab->flush_weedout_table)
 
16122
          extra.append(STRING_WITH_LEN("; Start temporary"));
 
16123
        else if (tab->check_weed_out_table)
 
16124
          extra.append(STRING_WITH_LEN("; End temporary"));
 
16125
        else if (tab->do_firstmatch)
 
16126
        {
 
16127
          extra.append(STRING_WITH_LEN("; FirstMatch("));
 
16128
          Table *prev_table=tab->do_firstmatch->table;
 
16129
          if (prev_table->derived_select_number)
 
16130
          {
 
16131
            char namebuf[NAME_LEN];
 
16132
            /* Derived table name generation */
 
16133
            int len= snprintf(namebuf, sizeof(namebuf)-1,
 
16134
                              "<derived%u>",
 
16135
                              prev_table->derived_select_number);
 
16136
            extra.append(namebuf, len);
 
16137
          }
 
16138
          else
 
16139
            extra.append(prev_table->pos_in_table_list->alias);
 
16140
          extra.append(STRING_WITH_LEN(")"));
 
16141
        }
 
16142
 
 
16143
        for (uint32_t part= 0; part < tab->ref.key_parts; part++)
 
16144
        {
 
16145
          if (tab->ref.cond_guards[part])
 
16146
          {
 
16147
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
16148
            break;
 
16149
          }
 
16150
        }
 
16151
 
 
16152
        if (i > 0 && tab[-1].next_select == sub_select_cache)
 
16153
          extra.append(STRING_WITH_LEN("; Using join buffer"));
 
16154
 
 
16155
        /* Skip initial "; "*/
 
16156
        const char *str= extra.ptr();
 
16157
        uint32_t len= extra.length();
 
16158
        if (len)
 
16159
        {
 
16160
          str += 2;
 
16161
          len -= 2;
 
16162
        }
 
16163
        item_list.push_back(new Item_string(str, len, cs));
 
16164
      }
 
16165
      // For next iteration
 
16166
      used_tables|=table->map;
 
16167
      if (result->send_data(item_list))
 
16168
        join->error= 1;
 
16169
    }
 
16170
  }
 
16171
  for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
 
16172
       unit;
 
16173
       unit= unit->next_unit())
 
16174
  {
 
16175
    if (mysql_explain_union(thd, unit, result))
 
16176
      return;
 
16177
  }
 
16178
  return;
 
16179
}
 
16180
 
 
16181
 
 
16182
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
 
16183
{
 
16184
  bool res= 0;
 
16185
  SELECT_LEX *first= unit->first_select();
 
16186
 
 
16187
  for (SELECT_LEX *sl= first;
 
16188
       sl;
 
16189
       sl= sl->next_select())
 
16190
  {
 
16191
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
 
16192
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
 
16193
    sl->type= (((&thd->lex->select_lex)==sl)?
 
16194
               (sl->first_inner_unit() || sl->next_select() ? 
 
16195
                "PRIMARY" : "SIMPLE"):
 
16196
               ((sl == first)?
 
16197
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
 
16198
                 "DERIVED":
 
16199
                 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
16200
                  "DEPENDENT SUBQUERY":
 
16201
                  (uncacheable?"UNCACHEABLE SUBQUERY":
 
16202
                   "SUBQUERY"))):
 
16203
                ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
16204
                 "DEPENDENT UNION":
 
16205
                 uncacheable?"UNCACHEABLE UNION":
 
16206
                 "UNION")));
 
16207
    sl->options|= SELECT_DESCRIBE;
 
16208
  }
 
16209
  if (unit->is_union())
 
16210
  {
 
16211
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
 
16212
    unit->fake_select_lex->type= "UNION RESULT";
 
16213
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
 
16214
    if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
 
16215
      res= unit->exec();
 
16216
    res|= unit->cleanup();
 
16217
  }
 
16218
  else
 
16219
  {
 
16220
    thd->lex->current_select= first;
 
16221
    unit->set_limit(unit->global_parameters);
 
16222
    res= mysql_select(thd, &first->ref_pointer_array,
 
16223
                        (TableList*) first->table_list.first,
 
16224
                        first->with_wild, first->item_list,
 
16225
                        first->where,
 
16226
                        first->order_list.elements +
 
16227
                        first->group_list.elements,
 
16228
                        (order_st*) first->order_list.first,
 
16229
                        (order_st*) first->group_list.first,
 
16230
                        first->having,
 
16231
                        (order_st*) thd->lex->proc_list.first,
 
16232
                        first->options | thd->options | SELECT_DESCRIBE,
 
16233
                        result, unit, first);
 
16234
  }
 
16235
  return(res || thd->is_error());
 
16236
}
 
16237
 
 
16238
 
 
16239
static void print_table_array(THD *thd, String *str, TableList **table, 
6258
16240
                              TableList **end)
6259
16241
{
6260
 
  (*table)->print(session, str, QT_ORDINARY);
 
16242
  (*table)->print(thd, str, QT_ORDINARY);
6261
16243
 
6262
16244
  for (TableList **tbl= table + 1; tbl < end; tbl++)
6263
16245
  {
6269
16251
    }
6270
16252
    else if (curr->straight)
6271
16253
      str->append(STRING_WITH_LEN(" straight_join "));
 
16254
    else if (curr->sj_inner_tables)
 
16255
      str->append(STRING_WITH_LEN(" semi join "));
6272
16256
    else
6273
16257
      str->append(STRING_WITH_LEN(" join "));
6274
 
    curr->print(session, str, QT_ORDINARY);
 
16258
    curr->print(thd, str, QT_ORDINARY);
6275
16259
    if (curr->on_expr)
6276
16260
    {
6277
16261
      str->append(STRING_WITH_LEN(" on("));
6281
16265
  }
6282
16266
}
6283
16267
 
 
16268
 
6284
16269
/**
6285
16270
  Print joins from the FROM clause.
6286
 
  @param session     thread Cursor
 
16271
  @param thd     thread handler
6287
16272
  @param str     string where table should be printed
6288
16273
  @param tables  list of tables in join
6289
16274
  @query_type    type of the query is being generated
6290
16275
*/
6291
 
void print_join(Session *session, String *str,
6292
 
                List<TableList> *tables, enum_query_type)
 
16276
 
 
16277
static void print_join(THD *thd,
 
16278
                       String *str,
 
16279
                       List<TableList> *tables,
 
16280
                       enum_query_type query_type __attribute__((unused)))
6293
16281
{
6294
16282
  /* 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*) *
 
16283
  List_iterator_fast<TableList> ti(*tables);
 
16284
  TableList **table= (TableList **)thd->alloc(sizeof(TableList*) *
6297
16285
                                                tables->elements);
6298
16286
  if (table == 0)
6299
16287
    return;  // out of memory
6300
16288
 
6301
16289
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
6302
16290
    *t= ti++;
 
16291
  
 
16292
  /* 
 
16293
    If the first table is a semi-join nest, swap it with something that is
 
16294
    not a semi-join nest.
 
16295
  */
 
16296
  if ((*table)->sj_inner_tables)
 
16297
  {
 
16298
    TableList **end= table + tables->elements;
 
16299
    for (TableList **t2= table; t2!=end; t2++)
 
16300
    {
 
16301
      if (!(*t2)->sj_inner_tables)
 
16302
      {
 
16303
        TableList *tmp= *t2;
 
16304
        *t2= *table;
 
16305
        *table= tmp;
 
16306
        break;
 
16307
      }
 
16308
    }
 
16309
  }
6303
16310
  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
 
 
 
16311
  print_table_array(thd, str, table, table + tables->elements);
 
16312
}
 
16313
 
 
16314
 
 
16315
/**
 
16316
  @brief Print an index hint
 
16317
 
 
16318
  @details Prints out the USE|FORCE|IGNORE index hint.
 
16319
 
 
16320
  @param      thd         the current thread
 
16321
  @param[out] str         appends the index hint here
 
16322
  @param      hint        what the hint is (as string : "USE INDEX"|
 
16323
                          "FORCE INDEX"|"IGNORE INDEX")
 
16324
  @param      hint_length the length of the string in 'hint'
 
16325
  @param      indexes     a list of index names for the hint
 
16326
*/
 
16327
 
 
16328
void 
 
16329
Index_hint::print(THD *thd, String *str)
 
16330
{
 
16331
  switch (type)
 
16332
  {
 
16333
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
 
16334
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
 
16335
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
 
16336
  }
 
16337
  str->append (STRING_WITH_LEN(" ("));
 
16338
  if (key_name.length)
 
16339
  {
 
16340
    if (thd && !my_strnncoll(system_charset_info,
 
16341
                             (const unsigned char *)key_name.str, key_name.length, 
 
16342
                             (const unsigned char *)primary_key_name, 
 
16343
                             strlen(primary_key_name)))
 
16344
      str->append(primary_key_name);
 
16345
    else
 
16346
      append_identifier(thd, str, key_name.str, key_name.length);
 
16347
  }
 
16348
  str->append(')');
 
16349
}
 
16350
 
 
16351
 
 
16352
/**
 
16353
  Print table as it should be in join list.
 
16354
 
 
16355
  @param str   string where table should be printed
 
16356
*/
 
16357
 
 
16358
void TableList::print(THD *thd, String *str, enum_query_type query_type)
 
16359
{
 
16360
  if (nested_join)
 
16361
  {
 
16362
    str->append('(');
 
16363
    print_join(thd, str, &nested_join->join_list, query_type);
 
16364
    str->append(')');
 
16365
  }
 
16366
  else
 
16367
  {
 
16368
    const char *cmp_name;                         // Name to compare with alias
 
16369
    if (derived)
 
16370
    {
 
16371
      // A derived table
 
16372
      str->append('(');
 
16373
      derived->print(str, query_type);
 
16374
      str->append(')');
 
16375
      cmp_name= "";                               // Force printing of alias
 
16376
    }
 
16377
    else
 
16378
    {
 
16379
      // A normal table
 
16380
      {
 
16381
        append_identifier(thd, str, db, db_length);
 
16382
        str->append('.');
 
16383
      }
 
16384
      if (schema_table)
 
16385
      {
 
16386
        append_identifier(thd, str, schema_table_name,
 
16387
                          strlen(schema_table_name));
 
16388
        cmp_name= schema_table_name;
 
16389
      }
 
16390
      else
 
16391
      {
 
16392
        append_identifier(thd, str, table_name, table_name_length);
 
16393
        cmp_name= table_name;
 
16394
      }
 
16395
    }
 
16396
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
 
16397
    {
 
16398
      char t_alias_buff[MAX_ALIAS_NAME];
 
16399
      const char *t_alias= alias;
 
16400
 
 
16401
      str->append(' ');
 
16402
      if (lower_case_table_names== 1)
 
16403
      {
 
16404
        if (alias && alias[0])
 
16405
        {
 
16406
          my_stpcpy(t_alias_buff, alias);
 
16407
          my_casedn_str(files_charset_info, t_alias_buff);
 
16408
          t_alias= t_alias_buff;
 
16409
        }
 
16410
      }
 
16411
 
 
16412
      append_identifier(thd, str, t_alias, strlen(t_alias));
 
16413
    }
 
16414
 
 
16415
    if (index_hints)
 
16416
    {
 
16417
      List_iterator<Index_hint> it(*index_hints);
 
16418
      Index_hint *hint;
 
16419
 
 
16420
      while ((hint= it++))
 
16421
      {
 
16422
        str->append (STRING_WITH_LEN(" "));
 
16423
        hint->print (thd, str);
 
16424
      }
 
16425
    }
 
16426
  }
 
16427
}
 
16428
 
 
16429
 
 
16430
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
 
16431
{
 
16432
  /* QQ: thd may not be set for sub queries, but this should be fixed */
 
16433
  if (!thd)
 
16434
    thd= current_thd;
6313
16435
 
6314
16436
  str->append(STRING_WITH_LEN("select "));
6315
16437
 
6316
16438
  /* First add options */
6317
16439
  if (options & SELECT_STRAIGHT_JOIN)
6318
16440
    str->append(STRING_WITH_LEN("straight_join "));
 
16441
  if ((thd->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
 
16442
      (this == &thd->lex->select_lex))
 
16443
    str->append(STRING_WITH_LEN("high_priority "));
6319
16444
  if (options & SELECT_DISTINCT)
6320
16445
    str->append(STRING_WITH_LEN("distinct "));
6321
16446
  if (options & SELECT_SMALL_RESULT)
6329
16454
 
6330
16455
  //Item List
6331
16456
  bool first= 1;
6332
 
  List<Item>::iterator it(item_list.begin());
 
16457
  List_iterator_fast<Item> it(item_list);
6333
16458
  Item *item;
6334
16459
  while ((item= it++))
6335
16460
  {
6342
16467
 
6343
16468
  /*
6344
16469
    from clause
6345
 
    @todo support USING/FORCE/IGNORE index
 
16470
    TODO: support USING/FORCE/IGNORE index
6346
16471
  */
6347
16472
  if (table_list.elements)
6348
16473
  {
6349
16474
    str->append(STRING_WITH_LEN(" from "));
6350
16475
    /* go through join tree */
6351
 
    print_join(session, str, &top_join_list, query_type);
 
16476
    print_join(thd, str, &top_join_list, query_type);
6352
16477
  }
6353
16478
  else if (where)
6354
16479
  {
6355
16480
    /*
6356
 
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as
 
16481
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as 
6357
16482
      "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
6358
16483
    */
6359
16484
    str->append(STRING_WITH_LEN(" from DUAL "));
6376
16501
  if (group_list.elements)
6377
16502
  {
6378
16503
    str->append(STRING_WITH_LEN(" group by "));
6379
 
    print_order(str, (Order *) group_list.first, query_type);
 
16504
    print_order(str, (order_st *) group_list.first, query_type);
6380
16505
    switch (olap)
6381
16506
    {
6382
16507
      case CUBE_TYPE:
6407
16532
  if (order_list.elements)
6408
16533
  {
6409
16534
    str->append(STRING_WITH_LEN(" order by "));
6410
 
    print_order(str, (Order *) order_list.first, query_type);
 
16535
    print_order(str, (order_st *) order_list.first, query_type);
6411
16536
  }
6412
16537
 
6413
16538
  // limit
6414
 
  print_limit(session, str, query_type);
 
16539
  print_limit(thd, str, query_type);
6415
16540
 
6416
16541
  // PROCEDURE unsupported here
6417
16542
}
6418
16543
 
 
16544
 
 
16545
/**
 
16546
  change select_result object of JOIN.
 
16547
 
 
16548
  @param res            new select_result object
 
16549
 
 
16550
  @retval
 
16551
    false   OK
 
16552
  @retval
 
16553
    true    error
 
16554
*/
 
16555
 
 
16556
bool JOIN::change_result(select_result *res)
 
16557
{
 
16558
  result= res;
 
16559
  if (result->prepare(fields_list, select_lex->master_unit()) ||
 
16560
                     result->prepare2())
 
16561
  {
 
16562
    return(true);
 
16563
  }
 
16564
  return(false);
 
16565
}
 
16566
 
6419
16567
/**
6420
16568
  @} (end of group Query_Optimizer)
6421
16569
*/
6422
 
 
6423
 
} /* namespace drizzled */