~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Monty Taylor
  • Date: 2008-08-01 22:33:44 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080801223344-vzhlflfmtijp1imv
First pass at gettexizing the error messages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/**
17
17
  @file
18
18
 
19
19
  @brief
20
 
  select_query and join optimization
 
20
  mysql_select and join optimization
 
21
 
21
22
 
22
23
  @defgroup Query_Optimizer  Query Optimizer
23
24
  @{
24
25
*/
25
 
#include <config.h>
26
 
 
27
 
#include <string>
28
 
#include <iostream>
29
 
#include <algorithm>
30
 
#include <vector>
31
 
 
32
 
#include <drizzled/sql_select.h> /* include join.h */
33
 
 
34
 
#include <drizzled/error.h>
35
 
#include <drizzled/gettext.h>
36
 
#include <drizzled/util/test.h>
37
 
#include <drizzled/name_resolution_context_state.h>
38
 
#include <drizzled/nested_join.h>
39
 
#include <drizzled/probes.h>
40
 
#include <drizzled/show.h>
41
 
#include <drizzled/item/cache.h>
42
 
#include <drizzled/item/cmpfunc.h>
43
 
#include <drizzled/item/copy_string.h>
44
 
#include <drizzled/item/uint.h>
45
 
#include <drizzled/cached_item.h>
46
 
#include <drizzled/sql_base.h>
47
 
#include <drizzled/field/blob.h>
48
 
#include <drizzled/check_stack_overrun.h>
49
 
#include <drizzled/lock.h>
50
 
#include <drizzled/item/outer_ref.h>
51
 
#include <drizzled/index_hint.h>
52
 
#include <drizzled/records.h>
53
 
#include <drizzled/internal/iocache.h>
54
 
#include <drizzled/drizzled.h>
55
 
#include <drizzled/plugin/storage_engine.h>
56
 
 
57
 
#include <drizzled/sql_union.h>
58
 
#include <drizzled/optimizer/key_field.h>
59
 
#include <drizzled/optimizer/position.h>
60
 
#include <drizzled/optimizer/sargable_param.h>
61
 
#include <drizzled/optimizer/key_use.h>
62
 
#include <drizzled/optimizer/range.h>
63
 
#include <drizzled/optimizer/quick_range_select.h>
64
 
#include <drizzled/optimizer/quick_ror_intersect_select.h>
65
 
 
66
 
#include <drizzled/filesort.h>
67
 
#include <drizzled/sql_lex.h>
68
 
#include <drizzled/session.h>
69
 
#include <drizzled/sort_field.h>
70
 
#include <drizzled/select_result.h>
71
 
 
72
 
using namespace std;
73
 
 
74
 
namespace drizzled
75
 
{
76
 
 
77
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
78
 
static COND *build_equal_items(Session *session, COND *cond,
 
26
 
 
27
#ifdef USE_PRAGMA_IMPLEMENTATION
 
28
#pragma implementation                          // gcc: Class implementation
 
29
#endif
 
30
 
 
31
#include "mysql_priv.h"
 
32
#include "sql_select.h"
 
33
 
 
34
#include <mysys/my_bit.h>
 
35
#include <mysys/hash.h>
 
36
#include <drizzled/drizzled_error_messages.h>
 
37
 
 
38
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
 
39
                              "MAYBE_REF","ALL","range","index",
 
40
                              "ref_or_null","unique_subquery","index_subquery",
 
41
                              "index_merge"
 
42
};
 
43
 
 
44
struct st_sargable_param;
 
45
 
 
46
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
 
47
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
 
48
                                 DYNAMIC_ARRAY *keyuse);
 
49
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
 
50
                                JOIN_TAB *join_tab,
 
51
                                uint tables, COND *conds,
 
52
                                COND_EQUAL *cond_equal,
 
53
                                table_map table_map, SELECT_LEX *select_lex,
 
54
                                st_sargable_param **sargables);
 
55
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
 
56
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
 
57
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
58
                               table_map used_tables);
 
59
static bool choose_plan(JOIN *join,table_map join_tables);
 
60
 
 
61
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
 
62
                             table_map remaining_tables, uint idx,
 
63
                             double record_count, double read_time);
 
64
static void optimize_straight_join(JOIN *join, table_map join_tables);
 
65
static bool greedy_search(JOIN *join, table_map remaining_tables,
 
66
                             uint depth, uint prune_level);
 
67
static bool best_extension_by_limited_search(JOIN *join,
 
68
                                             table_map remaining_tables,
 
69
                                             uint idx, double record_count,
 
70
                                             double read_time, uint depth,
 
71
                                             uint prune_level);
 
72
static uint determine_search_depth(JOIN* join);
 
73
static int join_tab_cmp(const void* ptr1, const void* ptr2);
 
74
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
 
75
/*
 
76
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
 
77
  tested and approved.
 
78
*/
 
79
static bool find_best(JOIN *join,table_map rest_tables,uint index,
 
80
                      double record_count,double read_time);
 
81
static uint cache_record_length(JOIN *join,uint index);
 
82
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
 
83
static bool get_best_combination(JOIN *join);
 
84
static store_key *get_store_key(THD *thd,
 
85
                                KEYUSE *keyuse, table_map used_tables,
 
86
                                KEY_PART_INFO *key_part, uchar *key_buff,
 
87
                                uint maybe_null);
 
88
static bool make_simple_join(JOIN *join,TABLE *tmp_table);
 
89
static void make_outerjoin_info(JOIN *join);
 
90
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
 
91
static bool make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after);
 
92
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables);
 
93
static void update_depend_map(JOIN *join);
 
94
static void update_depend_map(JOIN *join, ORDER *order);
 
95
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond,
 
96
                           bool change_list, bool *simple_order);
 
97
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables,
 
98
                            List<Item> &fields, bool send_row,
 
99
                            uint64_t select_options, const char *info,
 
100
                            Item *having);
 
101
static COND *build_equal_items(THD *thd, COND *cond,
79
102
                               COND_EQUAL *inherited,
80
 
                               List<TableList> *join_list,
 
103
                               List<TABLE_LIST> *join_list,
81
104
                               COND_EQUAL **cond_equal_ref);
82
 
 
83
 
static Item* part_of_refkey(Table *form,Field *field);
84
 
static bool cmp_buffer_with_ref(JoinTable *tab);
85
 
static void change_cond_ref_to_const(Session *session,
86
 
                                     list<COND_CMP>& save_list,
87
 
                                     Item *and_father,
88
 
                                     Item *cond,
89
 
                                     Item *field,
90
 
                                     Item *value);
91
 
static bool copy_blobs(Field **ptr);
92
 
 
93
 
static bool eval_const_cond(COND *cond)
94
 
{
95
 
    return ((Item_func*) cond)->val_int() ? true : false;
96
 
}
 
105
static COND* substitute_for_best_equal_field(COND *cond,
 
106
                                             COND_EQUAL *cond_equal,
 
107
                                             void *table_join_idx);
 
108
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
 
109
                            COND *conds, bool top, bool in_sj);
 
110
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
 
111
static void restore_prev_nj_state(JOIN_TAB *last);
 
112
static void reset_nj_counters(List<TABLE_LIST> *join_list);
 
113
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
 
114
                                          uint first_unused);
 
115
 
 
116
static 
 
117
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
 
118
static void restore_prev_sj_state(const table_map remaining_tables, 
 
119
                                  const JOIN_TAB *tab);
 
120
 
 
121
static COND *optimize_cond(JOIN *join, COND *conds,
 
122
                           List<TABLE_LIST> *join_list,
 
123
                           Item::cond_result *cond_value);
 
124
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
 
125
static bool open_tmp_table(TABLE *table);
 
126
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
 
127
                                    MI_COLUMNDEF *start_recinfo,
 
128
                                    MI_COLUMNDEF **recinfo,
 
129
                                    uint64_t options);
 
130
static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table);
 
131
 
 
132
static enum_nested_loop_state
 
133
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
134
                     int error);
 
135
static enum_nested_loop_state
 
136
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
 
137
static enum_nested_loop_state
 
138
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
 
139
static enum_nested_loop_state
 
140
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
141
static enum_nested_loop_state
 
142
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
143
static enum_nested_loop_state
 
144
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
145
static enum_nested_loop_state
 
146
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
147
 
 
148
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
 
149
static int join_read_system(JOIN_TAB *tab);
 
150
static int join_read_const(JOIN_TAB *tab);
 
151
static int join_read_key(JOIN_TAB *tab);
 
152
static int join_read_always_key(JOIN_TAB *tab);
 
153
static int join_read_last_key(JOIN_TAB *tab);
 
154
static int join_no_more_records(READ_RECORD *info);
 
155
static int join_read_next(READ_RECORD *info);
 
156
static int join_read_next_different(READ_RECORD *info);
 
157
static int join_init_quick_read_record(JOIN_TAB *tab);
 
158
static int test_if_quick_select(JOIN_TAB *tab);
 
159
static int join_init_read_record(JOIN_TAB *tab);
 
160
static int join_read_first(JOIN_TAB *tab);
 
161
static int join_read_next_same(READ_RECORD *info);
 
162
static int join_read_next_same_diff(READ_RECORD *info);
 
163
static int join_read_last(JOIN_TAB *tab);
 
164
static int join_read_prev_same(READ_RECORD *info);
 
165
static int join_read_prev(READ_RECORD *info);
 
166
int join_read_always_key_or_null(JOIN_TAB *tab);
 
167
int join_read_next_same_or_null(READ_RECORD *info);
 
168
static COND *make_cond_for_table(COND *cond,table_map table,
 
169
                                 table_map used_table,
 
170
                                 bool exclude_expensive_cond);
 
171
static Item* part_of_refkey(TABLE *form,Field *field);
 
172
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
 
173
                                    ha_rows select_limit, bool no_changes,
 
174
                                    const key_map *map);
 
175
static bool list_contains_unique_index(TABLE *table,
 
176
                          bool (*find_func) (Field *, void *), void *data);
 
177
static bool find_field_in_item_list (Field *field, void *data);
 
178
static bool find_field_in_order_list (Field *field, void *data);
 
179
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
 
180
                             ha_rows filesort_limit, ha_rows select_limit,
 
181
                             bool is_order_by);
 
182
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
 
183
                             Item *having);
 
184
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
 
185
                                   ulong offset,Item *having);
 
186
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
 
187
                                      uint field_count, Field **first_field,
 
188
 
 
189
                                      ulong key_length,Item *having);
 
190
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
 
191
static ulong used_blob_length(CACHE_FIELD **ptr);
 
192
static bool store_record_in_cache(JOIN_CACHE *cache);
 
193
static void reset_cache_read(JOIN_CACHE *cache);
 
194
static void reset_cache_write(JOIN_CACHE *cache);
 
195
static void read_cached_record(JOIN_TAB *tab);
 
196
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
 
197
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
 
198
                                    ORDER *order, List<Item> &fields,
 
199
                                    List<Item> &all_fields,
 
200
                                    bool *all_order_by_fields_used);
 
201
static bool test_if_subpart(ORDER *a,ORDER *b);
 
202
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
 
203
static void calc_group_buffer(JOIN *join,ORDER *group);
 
204
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
 
205
static bool alloc_group_fields(JOIN *join,ORDER *group);
 
206
// Create list for using with tempory table
 
207
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
208
                                     List<Item> &new_list1,
 
209
                                     List<Item> &new_list2,
 
210
                                     uint elements, List<Item> &items);
 
211
// Create list for using with tempory table
 
212
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
213
                                      List<Item> &new_list1,
 
214
                                      List<Item> &new_list2,
 
215
                                      uint elements, List<Item> &items);
 
216
static void init_tmptable_sum_functions(Item_sum **func);
 
217
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
 
218
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
 
219
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
 
220
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
 
221
static bool init_sum_functions(Item_sum **func, Item_sum **end);
 
222
static bool update_sum_func(Item_sum **func);
 
223
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
 
224
                            bool distinct, const char *message=NullS);
 
225
static Item *remove_additional_cond(Item* conds);
 
226
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
 
227
static bool test_if_ref(Item_field *left_item,Item *right_item);
 
228
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
229
                                       Item *new_cond, bool fix_fields);
97
230
 
98
231
/*
99
232
  This is used to mark equalities that were made from i-th IN-equality.
103
236
const char *subq_sj_cond_name=
104
237
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
105
238
 
106
 
static bool copy_blobs(Field **ptr)
 
239
static bool bitmap_covers(const table_map x, const table_map y)
107
240
{
108
 
  for (; *ptr ; ptr++)
109
 
  {
110
 
    if ((*ptr)->flags & BLOB_FLAG)
111
 
      if (((Field_blob *) (*ptr))->copy())
112
 
        return 1;                               // Error
113
 
  }
114
 
  return 0;
 
241
  return !test(y & ~x);
115
242
}
116
243
 
117
244
/**
118
245
  This handles SELECT with and without UNION.
119
246
*/
120
 
bool handle_select(Session *session, LEX *lex, select_result *result,
121
 
                   uint64_t setup_tables_done_option)
 
247
 
 
248
bool handle_select(THD *thd, LEX *lex, select_result *result,
 
249
                   ulong setup_tables_done_option)
122
250
{
123
251
  bool res;
124
 
  register Select_Lex *select_lex= &lex->select_lex;
125
 
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
 
252
  register SELECT_LEX *select_lex = &lex->select_lex;
 
253
  MYSQL_SELECT_START();
126
254
 
127
 
  if (select_lex->master_unit()->is_union() ||
 
255
  if (select_lex->master_unit()->is_union() || 
128
256
      select_lex->master_unit()->fake_select_lex)
129
 
  {
130
 
    res= drizzle_union(session, lex, result, &lex->unit,
131
 
                       setup_tables_done_option);
132
 
  }
 
257
    res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
133
258
  else
134
259
  {
135
 
    Select_Lex_Unit *unit= &lex->unit;
 
260
    SELECT_LEX_UNIT *unit= &lex->unit;
136
261
    unit->set_limit(unit->global_parameters);
137
 
    session->session_marker= 0;
 
262
    thd->thd_marker= 0;
138
263
    /*
139
 
      'options' of select_query will be set in JOIN, as far as JOIN for
140
 
      every PS/SP execution new, we will not need reset this flag if
 
264
      'options' of mysql_select will be set in JOIN, as far as JOIN for
 
265
      every PS/SP execution new, we will not need reset this flag if 
141
266
      setup_tables_done_option changed for next rexecution
142
267
    */
143
 
    res= select_query(session,
144
 
                      &select_lex->ref_pointer_array,
145
 
                      (TableList*) select_lex->table_list.first,
146
 
                      select_lex->with_wild,
147
 
                      select_lex->item_list,
 
268
    res= mysql_select(thd, &select_lex->ref_pointer_array,
 
269
                      (TABLE_LIST*) select_lex->table_list.first,
 
270
                      select_lex->with_wild, select_lex->item_list,
148
271
                      select_lex->where,
149
272
                      select_lex->order_list.elements +
150
273
                      select_lex->group_list.elements,
151
 
                      (Order*) select_lex->order_list.first,
152
 
                      (Order*) select_lex->group_list.first,
 
274
                      (ORDER*) select_lex->order_list.first,
 
275
                      (ORDER*) select_lex->group_list.first,
153
276
                      select_lex->having,
154
 
                      select_lex->options | session->options |
 
277
                      (ORDER*) lex->proc_list.first,
 
278
                      select_lex->options | thd->options |
155
279
                      setup_tables_done_option,
156
280
                      result, unit, select_lex);
157
281
  }
158
 
  res|= session->is_error();
 
282
  res|= thd->is_error();
159
283
  if (unlikely(res))
160
284
    result->abort();
161
285
 
162
 
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
163
 
  return res;
 
286
  MYSQL_SELECT_END();
 
287
  return(res);
164
288
}
165
289
 
 
290
 
166
291
/*
167
292
  Fix fields referenced from inner selects.
168
293
 
169
294
  SYNOPSIS
170
295
    fix_inner_refs()
171
 
    session               Thread handle
 
296
    thd               Thread handle
172
297
    all_fields        List of all fields used in select
173
298
    select            Current select
174
299
    ref_pointer_array Array of references to Items used in current select
203
328
    true  an error occured
204
329
    false ok
205
330
*/
206
 
bool fix_inner_refs(Session *session, 
207
 
                    List<Item> &all_fields, 
208
 
                    Select_Lex *select, 
209
 
                    Item **ref_pointer_array)
 
331
 
 
332
bool
 
333
fix_inner_refs(THD *thd, List<Item> &all_fields, SELECT_LEX *select,
 
334
                 Item **ref_pointer_array)
210
335
{
211
336
  Item_outer_ref *ref;
212
337
  bool res= false;
213
338
  bool direct_ref= false;
214
339
 
215
 
  List<Item_outer_ref>::iterator ref_it(select->inner_refs_list.begin());
 
340
  List_iterator<Item_outer_ref> ref_it(select->inner_refs_list);
216
341
  while ((ref= ref_it++))
217
342
  {
218
343
    Item *item= ref->outer_ref;
219
344
    Item **item_ref= ref->ref;
220
345
    Item_ref *new_ref;
221
346
    /*
222
 
      @todo this field item already might be present in the select list.
 
347
      TODO: this field item already might be present in the select list.
223
348
      In this case instead of adding new field item we could use an
224
349
      existing one. The change will lead to less operations for copying fields,
225
350
      smaller temporary tables and less data passed through filesort.
266
391
    ref->outer_ref= new_ref;
267
392
    ref->ref= &ref->outer_ref;
268
393
 
269
 
    if (!ref->fixed && ref->fix_fields(session, 0))
 
394
    if (!ref->fixed && ref->fix_fields(thd, 0))
270
395
      return true;
271
 
    session->used_tables|= item->used_tables();
 
396
    thd->used_tables|= item->used_tables();
272
397
  }
273
398
  return res;
274
399
}
275
400
 
 
401
#define MAGIC_IN_WHERE_TOP_LEVEL 10
 
402
/**
 
403
  Function to setup clauses without sum functions.
 
404
*/
 
405
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
 
406
                               TABLE_LIST *tables,
 
407
                               TABLE_LIST *leaves,
 
408
                               List<Item> &fields,
 
409
                               List<Item> &all_fields,
 
410
                               COND **conds,
 
411
                               ORDER *order,
 
412
                               ORDER *group, bool *hidden_group_fields)
 
413
{
 
414
  int res;
 
415
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
 
416
 
 
417
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
418
  res= setup_conds(thd, tables, leaves, conds);
 
419
 
 
420
  thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
 
421
  res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
 
422
                          order);
 
423
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
424
  res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
 
425
                          group, hidden_group_fields);
 
426
  thd->lex->allow_sum_func= save_allow_sum_func;
 
427
  return(res);
 
428
}
 
429
 
276
430
/*****************************************************************************
277
431
  Check fields, find best join, do the select and output fields.
278
 
  select_query assumes that all tables are already opened
 
432
  mysql_select assumes that all tables are already opened
279
433
*****************************************************************************/
280
434
 
 
435
/**
 
436
  Prepare of whole select (including sub queries in future).
 
437
 
 
438
  @todo
 
439
    Add check of calculation of GROUP functions and fields:
 
440
    SELECT COUNT(*)+table.col1 from table1;
 
441
 
 
442
  @retval
 
443
    -1   on error
 
444
  @retval
 
445
    0   on success
 
446
*/
 
447
int
 
448
JOIN::prepare(Item ***rref_pointer_array,
 
449
              TABLE_LIST *tables_init,
 
450
              uint wild_num, COND *conds_init, uint og_num,
 
451
              ORDER *order_init, ORDER *group_init,
 
452
              Item *having_init,
 
453
              ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
 
454
              SELECT_LEX_UNIT *unit_arg)
 
455
{
 
456
  // to prevent double initialization on EXPLAIN
 
457
  if (optimized)
 
458
    return(0);
 
459
 
 
460
  conds= conds_init;
 
461
  order= order_init;
 
462
  group_list= group_init;
 
463
  having= having_init;
 
464
  proc_param= proc_param_init;
 
465
  tables_list= tables_init;
 
466
  select_lex= select_lex_arg;
 
467
  select_lex->join= this;
 
468
  join_list= &select_lex->top_join_list;
 
469
  union_part= unit_arg->is_union();
 
470
 
 
471
  thd->lex->current_select->is_item_list_lookup= 1;
 
472
  /*
 
473
    If we have already executed SELECT, then it have not sense to prevent
 
474
    its table from update (see unique_table())
 
475
  */
 
476
  if (thd->derived_tables_processing)
 
477
    select_lex->exclude_from_table_unique_test= true;
 
478
 
 
479
  /* Check that all tables, fields, conds and order are ok */
 
480
 
 
481
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
 
482
      setup_tables_and_check_access(thd, &select_lex->context, join_list,
 
483
                                    tables_list, &select_lex->leaf_tables,
 
484
                                    false))
 
485
      return(-1);
 
486
 
 
487
  TABLE_LIST *table_ptr;
 
488
  for (table_ptr= select_lex->leaf_tables;
 
489
       table_ptr;
 
490
       table_ptr= table_ptr->next_leaf)
 
491
    tables++;
 
492
 
 
493
  if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
 
494
      select_lex->setup_ref_array(thd, og_num) ||
 
495
      setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
 
496
                   &all_fields, 1) ||
 
497
      setup_without_group(thd, (*rref_pointer_array), tables_list,
 
498
                          select_lex->leaf_tables, fields_list,
 
499
                          all_fields, &conds, order, group_list,
 
500
                          &hidden_group_fields))
 
501
    return(-1);                         /* purecov: inspected */
 
502
 
 
503
  ref_pointer_array= *rref_pointer_array;
 
504
  
 
505
  if (having)
 
506
  {
 
507
    nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
 
508
    thd->where="having clause";
 
509
    thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
510
    select_lex->having_fix_field= 1;
 
511
    bool having_fix_rc= (!having->fixed &&
 
512
                         (having->fix_fields(thd, &having) ||
 
513
                          having->check_cols(1)));
 
514
    select_lex->having_fix_field= 0;
 
515
    if (having_fix_rc || thd->is_error())
 
516
      return(-1);                               /* purecov: inspected */
 
517
    thd->lex->allow_sum_func= save_allow_sum_func;
 
518
  }
 
519
 
 
520
  {
 
521
    Item_subselect *subselect;
 
522
    Item_in_subselect *in_subs= NULL;
 
523
    /*
 
524
      Are we in a subquery predicate?
 
525
      TODO: the block below will be executed for every PS execution without need.
 
526
    */
 
527
    if ((subselect= select_lex->master_unit()->item))
 
528
    {
 
529
      bool do_semijoin= !test(thd->variables.optimizer_switch &
 
530
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
 
531
      if (subselect->substype() == Item_subselect::IN_SUBS)
 
532
        in_subs= (Item_in_subselect*)subselect;
 
533
 
 
534
      /*
 
535
        Check if we're in subquery that is a candidate for flattening into a
 
536
        semi-join (which is done done in flatten_subqueries()). The
 
537
        requirements are:
 
538
          1. Subquery predicate is an IN/=ANY subq predicate
 
539
          2. Subquery is a single SELECT (not a UNION)
 
540
          3. Subquery does not have GROUP BY or ORDER BY
 
541
          4. Subquery does not use aggregate functions or HAVING
 
542
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
 
543
          6. No execution method was already chosen (by a prepared statement).
 
544
 
 
545
          (*). We are not in a subquery of a single table UPDATE/DELETE that 
 
546
               doesn't have a JOIN (TODO: We should handle this at some
 
547
               point by switching to multi-table UPDATE/DELETE)
 
548
 
 
549
          (**). We're not in a confluent table-less subquery, like
 
550
                "SELECT 1". 
 
551
      */
 
552
      if (in_subs &&                                                    // 1
 
553
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
554
          !select_lex->group_list.elements && !order &&                 // 3
 
555
          !having && !select_lex->with_sum_func &&                      // 4
 
556
          thd->thd_marker &&                                            // 5
 
557
          select_lex->outer_select()->join &&                           // (*)
 
558
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**) 
 
559
          do_semijoin &&
 
560
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
 
561
      {
 
562
        {
 
563
          if (!in_subs->left_expr->fixed &&
 
564
               in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
 
565
          {
 
566
            return(-1);
 
567
          }
 
568
          /*
 
569
            Check that the right part of the subselect contains no more than one
 
570
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
 
571
          */
 
572
          if (subselect->substype() == Item_subselect::IN_SUBS &&
 
573
             (select_lex->item_list.elements != 
 
574
              ((Item_in_subselect*)subselect)->left_expr->cols()))
 
575
          {
 
576
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
 
577
            return(-1);
 
578
          }
 
579
        }
 
580
 
 
581
        /* Register the subquery for further processing */
 
582
        select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
 
583
        in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
 
584
      }
 
585
      else
 
586
      {
 
587
        bool do_materialize= !test(thd->variables.optimizer_switch &
 
588
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
 
589
        /*
 
590
          Check if the subquery predicate can be executed via materialization.
 
591
          The required conditions are:
 
592
          1. Subquery predicate is an IN/=ANY subq predicate
 
593
          2. Subquery is a single SELECT (not a UNION)
 
594
          3. Subquery is not a table-less query. In this case there is no
 
595
             point in materializing.
 
596
          4. Subquery predicate is a top-level predicate
 
597
             (this implies it is not negated)
 
598
             TODO: this is a limitation that should be lifeted once we
 
599
             implement correct NULL semantics (WL#3830)
 
600
          5. Subquery is non-correlated
 
601
             TODO:
 
602
             This is an overly restrictive condition. It can be extended to:
 
603
             (Subquery is non-correlated ||
 
604
              Subquery is correlated to any query outer to IN predicate ||
 
605
              (Subquery is correlated to the immediate outer query &&
 
606
               Subquery !contains {GROUP BY, ORDER BY [LIMIT],
 
607
               aggregate functions) && subquery predicate is not under "NOT IN"))
 
608
          6. No execution method was already chosen (by a prepared statement).
 
609
 
 
610
          (*) The subquery must be part of a SELECT statement. The current
 
611
               condition also excludes multi-table update statements.
 
612
 
 
613
          We have to determine whether we will perform subquery materialization
 
614
          before calling the IN=>EXISTS transformation, so that we know whether to
 
615
          perform the whole transformation or only that part of it which wraps
 
616
          Item_in_subselect in an Item_in_optimizer.
 
617
        */
 
618
        if (do_materialize && 
 
619
            in_subs  &&                                                   // 1
 
620
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
621
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
 
622
            thd->lex->sql_command == SQLCOM_SELECT)                       // *
 
623
        {
 
624
          if (in_subs->is_top_level_item() &&                             // 4
 
625
              !in_subs->is_correlated &&                                  // 5
 
626
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
 
627
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
 
628
        }
 
629
 
 
630
        Item_subselect::trans_res trans_res;
 
631
        if ((trans_res= subselect->select_transformer(this)) !=
 
632
            Item_subselect::RES_OK)
 
633
        {
 
634
          return((trans_res == Item_subselect::RES_ERROR));
 
635
        }
 
636
      }
 
637
    }
 
638
  }
 
639
 
 
640
  if (order)
 
641
  {
 
642
    ORDER *ord;
 
643
    for (ord= order; ord; ord= ord->next)
 
644
    {
 
645
      Item *item= *ord->item;
 
646
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
 
647
        item->split_sum_func(thd, ref_pointer_array, all_fields);
 
648
    }
 
649
  }
 
650
 
 
651
  if (having && having->with_sum_func)
 
652
    having->split_sum_func2(thd, ref_pointer_array, all_fields,
 
653
                            &having, true);
 
654
  if (select_lex->inner_sum_func_list)
 
655
  {
 
656
    Item_sum *end=select_lex->inner_sum_func_list;
 
657
    Item_sum *item_sum= end;  
 
658
    do
 
659
    { 
 
660
      item_sum= item_sum->next;
 
661
      item_sum->split_sum_func2(thd, ref_pointer_array,
 
662
                                all_fields, item_sum->ref_by, false);
 
663
    } while (item_sum != end);
 
664
  }
 
665
 
 
666
  if (select_lex->inner_refs_list.elements &&
 
667
      fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
 
668
    return(-1);
 
669
 
 
670
  /*
 
671
    Check if there are references to un-aggregated columns when computing 
 
672
    aggregate functions with implicit grouping (there is no GROUP BY).
 
673
 
 
674
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
 
675
  */
 
676
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
 
677
  {
 
678
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
 
679
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
 
680
    return(-1);
 
681
  }
 
682
  {
 
683
    /* Caclulate the number of groups */
 
684
    send_group_parts= 0;
 
685
    for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
686
      send_group_parts++;
 
687
  }
 
688
  
 
689
  if (error)
 
690
    goto err;                                   /* purecov: inspected */
 
691
 
 
692
  if (result && result->prepare(fields_list, unit_arg))
 
693
    goto err;                                   /* purecov: inspected */
 
694
 
 
695
  /* Init join struct */
 
696
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
697
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
698
  this->group= group_list != 0;
 
699
  unit= unit_arg;
 
700
 
 
701
#ifdef RESTRICTED_GROUP
 
702
  if (sum_func_count && !group_list && (func_count || field_count))
 
703
  {
 
704
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
 
705
    goto err;
 
706
  }
 
707
#endif
 
708
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
 
709
    goto err;
 
710
  if (alloc_func_list())
 
711
    goto err;
 
712
 
 
713
  return(0); // All OK
 
714
 
 
715
err:
 
716
  return(-1);                           /* purecov: inspected */
 
717
}
 
718
 
 
719
 
 
720
/*
 
721
  Remove the predicates pushed down into the subquery
 
722
 
 
723
  SYNOPSIS
 
724
    JOIN::remove_subq_pushed_predicates()
 
725
      where   IN  Must be NULL
 
726
              OUT The remaining WHERE condition, or NULL
 
727
 
 
728
  DESCRIPTION
 
729
    Given that this join will be executed using (unique|index)_subquery,
 
730
    without "checking NULL", remove the predicates that were pushed down
 
731
    into the subquery.
 
732
 
 
733
    If the subquery compares scalar values, we can remove the condition that
 
734
    was wrapped into trig_cond (it will be checked when needed by the subquery
 
735
    engine)
 
736
 
 
737
    If the subquery compares row values, we need to keep the wrapped
 
738
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
 
739
    and non-NULL values, we'll do a full table scan and will rely on the
 
740
    equalities corresponding to non-NULL parts of left tuple to filter out
 
741
    non-matching records.
 
742
 
 
743
    TODO: We can remove the equalities that will be guaranteed to be true by the
 
744
    fact that subquery engine will be using index lookup. This must be done only
 
745
    for cases where there are no conversion errors of significance, e.g. 257
 
746
    that is searched in a byte. But this requires homogenization of the return 
 
747
    codes of all Field*::store() methods.
 
748
*/
 
749
 
 
750
void JOIN::remove_subq_pushed_predicates(Item **where)
 
751
{
 
752
  if (conds->type() == Item::FUNC_ITEM &&
 
753
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
 
754
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
 
755
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
 
756
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
 
757
                   ((Item_func *)conds)->arguments()[0]))
 
758
  {
 
759
    *where= 0;
 
760
    return;
 
761
  }
 
762
}
 
763
 
 
764
 
281
765
/*
282
766
  Index lookup-based subquery: save some flags for EXPLAIN output
283
767
 
290
774
  DESCRIPTION
291
775
    For index lookup-based subquery (i.e. one executed with
292
776
    subselect_uniquesubquery_engine or subselect_indexsubquery_engine),
293
 
    check its EXPLAIN output row should contain
294
 
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL)
 
777
    check its EXPLAIN output row should contain 
 
778
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL) 
295
779
      "Using Where" (TAB_INFO_USING_WHERE)
296
780
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
297
781
    and set appropriate flags in join_tab->packed_info.
298
782
*/
299
 
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
 
783
 
 
784
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
300
785
{
301
786
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
302
 
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
 
787
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
303
788
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
304
789
  if (where)
305
790
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
306
 
  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
 
791
  for (uint i = 0; i < join_tab->ref.key_parts; i++)
307
792
  {
308
793
    if (join_tab->ref.cond_guards[i])
309
794
    {
313
798
  }
314
799
}
315
800
 
 
801
 
 
802
 
 
803
 
 
804
/*
 
805
  Check if the table's rowid is included in the temptable
 
806
 
 
807
  SYNOPSIS
 
808
    sj_table_is_included()
 
809
      join      The join
 
810
      join_tab  The table to be checked
 
811
 
 
812
  DESCRIPTION
 
813
    SemiJoinDuplicateElimination: check the table's rowid should be included
 
814
    in the temptable. This is so if
 
815
 
 
816
    1. The table is not embedded within some semi-join nest
 
817
    2. The has been pulled out of a semi-join nest, or
 
818
 
 
819
    3. The table is functionally dependent on some previous table
 
820
 
 
821
    [4. This is also true for constant tables that can't be
 
822
        NULL-complemented but this function is not called for such tables]
 
823
 
 
824
  RETURN
 
825
    true  - Include table's rowid
 
826
    false - Don't
 
827
*/
 
828
 
 
829
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
 
830
{
 
831
  if (join_tab->emb_sj_nest)
 
832
    return false;
 
833
  
 
834
  /* Check if this table is functionally dependent on the tables that
 
835
     are within the same outer join nest
 
836
  */
 
837
  TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
 
838
  if (join_tab->type == JT_EQ_REF)
 
839
  {
 
840
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
 
841
    uint idx;
 
842
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
 
843
    {
 
844
      JOIN_TAB *ref_tab= join->join_tab + idx;
 
845
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
 
846
        return true;
 
847
    }
 
848
    /* Ok, functionally dependent */
 
849
    return false;
 
850
  }
 
851
  /* Not functionally dependent => need to include*/
 
852
  return true;
 
853
}
 
854
 
 
855
 
 
856
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg,
 
857
                                          SJ_TMP_TABLE *sjtbl);
 
858
 
 
859
 
 
860
/*
 
861
  Setup the strategies to eliminate semi-join duplicates.
 
862
  
 
863
  SYNOPSIS
 
864
    setup_semijoin_dups_elimination()
 
865
      join           Join to process
 
866
      options        Join options (needed to see if join buffering will be 
 
867
                     used or not)
 
868
      no_jbuf_after  Another bit of information re where join buffering will
 
869
                     be used.
 
870
 
 
871
  DESCRIPTION
 
872
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
 
873
    strategies:
 
874
 
 
875
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
 
876
                         of row combinations)
 
877
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
 
878
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
 
879
                  together and picking the 1st one)
 
880
    
 
881
    The join order has "duplicate-generating ranges", and every range is
 
882
    served by one strategy or a combination of FirstMatch with with some
 
883
    other strategy.
 
884
    
 
885
    "Duplicate-generating range" is defined as a range within the join order
 
886
    that contains all of the inner tables of a semi-join. All ranges must be
 
887
    disjoint, if tables of several semi-joins are interleaved, then the ranges
 
888
    are joined together, which is equivalent to converting
 
889
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
 
890
    to
 
891
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
 
892
    .
 
893
 
 
894
    Applicability conditions are as follows:
 
895
 
 
896
    DuplicateWeedout strategy
 
897
    ~~~~~~~~~~~~~~~~~~~~~~~~~
 
898
 
 
899
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
 
900
      +------+  +=========================+  +---+
 
901
        (1)                 (2)               (3)
 
902
 
 
903
       (1) - Prefix of OuterTables (those that participate in 
 
904
             IN-equality and/or are correlated with subquery) and outer 
 
905
             Noncorrelated Tables.
 
906
       (2) - The handled range. The range starts with the first sj-inner
 
907
             table, and covers all sj-inner and outer tables 
 
908
             Within the range,  Inner, Outer, outer Noncorrelated tables
 
909
             may follow in any order.
 
910
       (3) - The suffix of outer Noncorrelated tables.
 
911
    
 
912
    FirstMatch strategy
 
913
    ~~~~~~~~~~~~~~~~~~~
 
914
 
 
915
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
 
916
      +------+  +==================+  +---+
 
917
        (1)             (2)          (3)
 
918
 
 
919
      (1) - Prefix of outer and non-correlated tables
 
920
      (2) - The handled range, which may contain only inner and
 
921
            non-correlated tables.
 
922
      (3) - The suffix of outer Noncorrelated tables.
 
923
 
 
924
    InsideOut strategy 
 
925
    ~~~~~~~~~~~~~~~~~~
 
926
 
 
927
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
 
928
     +--------+   +===========+ +=============+   +------+
 
929
        (1)           (2)          (3)              (4)
 
930
     
 
931
      (1) - Prefix that may contain any outer tables. The prefix must contain
 
932
            all the non-trivially correlated outer tables. (non-trivially means
 
933
            that the correlation is not just through the IN-equality).
 
934
      
 
935
      (2) - Inner table for which the InsideOut scan is performed.
 
936
 
 
937
      (3) - The remainder of the duplicate-generating range. It is served by 
 
938
            application of FirstMatch strategy, with the exception that
 
939
            outer IN-correlated tables are considered to be non-correlated.
 
940
 
 
941
      (4) - THe suffix of outer and outer non-correlated tables.
 
942
 
 
943
    If several strategies are applicable, their relative priorities are:
 
944
      1. InsideOut
 
945
      2. FirstMatch 
 
946
      3. DuplicateWeedout
 
947
 
 
948
    This function walks over the join order and sets up the strategies by
 
949
    setting appropriate members in join_tab structures.
 
950
 
 
951
  RETURN
 
952
    false  OK 
 
953
    true   Out of memory error
 
954
*/
 
955
 
 
956
static
 
957
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint no_jbuf_after)
 
958
{
 
959
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
960
  struct {
 
961
    /* 
 
962
      0 - invalid (EOF marker), 
 
963
      1 - InsideOut, 
 
964
      2 - Temptable (maybe confluent),
 
965
      3 - Temptable with join buffering
 
966
    */
 
967
    uint strategy;
 
968
    uint start_idx; /* Left range bound */
 
969
    uint end_idx;   /* Right range bound */
 
970
    /* 
 
971
      For Temptable strategy: Bitmap of all outer and correlated tables from 
 
972
      all involved join nests.
 
973
    */
 
974
    table_map outer_tables;
 
975
  } dups_ranges [MAX_TABLES];
 
976
 
 
977
  TABLE_LIST *emb_insideout_nest= NULL;
 
978
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
 
979
                               tables) whose ranges we're in */
 
980
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
 
981
  table_map range_start_map= 0; /* table_map at current range start */
 
982
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
 
983
  int cur_range= 0;
 
984
  uint i;
 
985
 
 
986
  /*
 
987
    First pass: locate the duplicate-generating ranges and pick the strategies.
 
988
  */
 
989
  for (i=join->const_tables ; i < join->tables ; i++)
 
990
  {
 
991
    JOIN_TAB *tab=join->join_tab+i;
 
992
    TABLE *table=tab->table;
 
993
    cur_map |= table->map;
 
994
 
 
995
    if (tab->emb_sj_nest) // Encountered an sj-inner table
 
996
    {
 
997
      if (!emb_sj_map)
 
998
      {
 
999
        dups_ranges[cur_range].start_idx= i;
 
1000
        range_start_map= cur_map & ~table->map;
 
1001
        /*
 
1002
          Remember if this is a possible start of range that is covered by
 
1003
          the InsideOut strategy (the reason that it is not covered could
 
1004
          be that it overlaps with anther semi-join's range. we don't
 
1005
          support InsideOut for joined ranges)
 
1006
        */
 
1007
        if (join->best_positions[i].use_insideout_scan)
 
1008
          emb_insideout_nest= tab->emb_sj_nest;
 
1009
      }
 
1010
 
 
1011
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
 
1012
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
 
1013
 
 
1014
      if (tab->emb_sj_nest != emb_insideout_nest)
 
1015
      {
 
1016
        /*
 
1017
          Two different semi-joins interleave. This cannot be handled by
 
1018
          InsideOut strategy.
 
1019
        */
 
1020
        emb_insideout_nest= NULL;
 
1021
      }
 
1022
    }
 
1023
 
 
1024
    if (emb_sj_map) /* We're in duplicate-generating range */
 
1025
    {
 
1026
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
1027
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
 
1028
          i <= no_jbuf_after && !dealing_with_jbuf)
 
1029
      {
 
1030
        /*
 
1031
          This table uses join buffering, which makes use of FirstMatch or 
 
1032
          InsideOut strategies impossible for the current and (we assume) 
 
1033
          preceding duplicate-producing ranges.
 
1034
          That is, for the join order:
 
1035
 
 
1036
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
 
1037
                  |     |     |     |          | \
 
1038
                  +-----+     +-----+          |  join buffering use
 
1039
                     r1          r2         we're here
 
1040
 
 
1041
          we'll have to remove r1 and r2 and use duplicate-elimination
 
1042
          strategy that spans all the tables, starting from the very 1st
 
1043
          one.
 
1044
        */
 
1045
        dealing_with_jbuf= true;
 
1046
        emb_insideout_nest= false;
 
1047
 
 
1048
        /* 
 
1049
          Absorb all preceding duplicate-eliminating ranges. Their strategies
 
1050
          do not matter: 
 
1051
        */
 
1052
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
 
1053
        {
 
1054
          dups_ranges[cur_range].outer_tables |= 
 
1055
            dups_ranges[prev_range].outer_tables;
 
1056
        }
 
1057
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
 
1058
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
 
1059
        cur_range=  0;
 
1060
      }
 
1061
 
 
1062
      /*
 
1063
        Check if we are at the end of duplicate-producing range. We are if
 
1064
 
 
1065
        1. It's an InsideOut range (which presumes all correlated tables are
 
1066
           in the prefix), and all inner tables are in the join order prefix,
 
1067
           or
 
1068
        2. It's a DuplicateElimination range (possibly covering several
 
1069
           SJ-nests), and all inner, outer, and correlated tables of all 
 
1070
           sj-nests are in the join order prefix.
 
1071
      */
 
1072
      bool end_of_range= false;
 
1073
      if (emb_insideout_nest && 
 
1074
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
 
1075
      {
 
1076
        /* Save that this range is handled with InsideOut: */
 
1077
        dups_ranges[cur_range].strategy= 1;
 
1078
        end_of_range= true;
 
1079
      }
 
1080
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
 
1081
      {
 
1082
        /*
 
1083
          This is a complete range to be handled with either DuplicateWeedout 
 
1084
          or FirstMatch
 
1085
        */
 
1086
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
 
1087
        /* 
 
1088
          This will hold tables from within the range that need to be put 
 
1089
          into the join buffer before we can use the FirstMatch on its tail.
 
1090
        */
 
1091
        dups_ranges[cur_range].outer_tables= emb_outer_tables & 
 
1092
                                             ~range_start_map;
 
1093
        end_of_range= true;
 
1094
      }
 
1095
 
 
1096
      if (end_of_range)
 
1097
      {
 
1098
        dups_ranges[cur_range].end_idx= i+1;
 
1099
        emb_sj_map= emb_outer_tables= 0;
 
1100
        emb_insideout_nest= NULL;
 
1101
        dealing_with_jbuf= false;
 
1102
        dups_ranges[++cur_range].strategy= 0;
 
1103
      }
 
1104
    }
 
1105
  }
 
1106
 
 
1107
  THD *thd= join->thd;
 
1108
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
 
1109
  /*
 
1110
    Second pass: setup the chosen strategies    
 
1111
  */
 
1112
  for (int j= 0; j < cur_range; j++)
 
1113
  {
 
1114
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
 
1115
    JOIN_TAB *jump_to;
 
1116
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
 
1117
    {
 
1118
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
 
1119
      jump_to= tab++;
 
1120
    }
 
1121
    else // DuplicateWeedout strategy
 
1122
    {
 
1123
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
 
1124
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
1125
      uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
1126
      uint jt_null_bits= 0;    // # null bits in tuple bytes
 
1127
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
 
1128
      uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1129
      JOIN_TAB *last_outer_tab= tab - 1;
 
1130
      /*
 
1131
        Walk through the range and remember
 
1132
         - tables that need their rowids to be put into temptable
 
1133
         - the last outer table
 
1134
      */
 
1135
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1136
      {
 
1137
        if (sj_table_is_included(join, tab))
 
1138
        {
 
1139
          last_tab->join_tab= tab;
 
1140
          last_tab->rowid_offset= jt_rowid_offset;
 
1141
          jt_rowid_offset += tab->table->file->ref_length;
 
1142
          if (tab->table->maybe_null)
 
1143
          {
 
1144
            last_tab->null_byte= jt_null_bits / 8;
 
1145
            last_tab->null_bit= jt_null_bits++;
 
1146
          }
 
1147
          last_tab++;
 
1148
          tab->table->prepare_for_position();
 
1149
          tab->rowid_keep_flags= rowid_keep_flags;
 
1150
        }
 
1151
        cur_map |= tab->table->map;
 
1152
        if (!tab->emb_sj_nest && bitmap_covers(cur_map, 
 
1153
                                               dups_ranges[j].outer_tables))
 
1154
          last_outer_tab= tab;
 
1155
      }
 
1156
 
 
1157
      if (jt_rowid_offset) /* Temptable has at least one rowid */
 
1158
      {
 
1159
        SJ_TMP_TABLE *sjtbl;
 
1160
        uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1161
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
 
1162
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
 
1163
          return(true);
 
1164
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
 
1165
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
 
1166
        sjtbl->rowid_len= jt_rowid_offset;
 
1167
        sjtbl->null_bits= jt_null_bits;
 
1168
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
 
1169
 
 
1170
        *next_sjtbl_ptr= sjtbl;
 
1171
        next_sjtbl_ptr= &(sjtbl->next);
 
1172
        sjtbl->next= NULL;
 
1173
 
 
1174
        sjtbl->tmp_table= 
 
1175
          create_duplicate_weedout_tmp_table(thd, 
 
1176
                                             sjtbl->rowid_len + 
 
1177
                                             sjtbl->null_bytes,
 
1178
                                             sjtbl);
 
1179
 
 
1180
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
 
1181
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
 
1182
      }
 
1183
      tab= last_outer_tab + 1;
 
1184
      jump_to= last_outer_tab;
 
1185
    }
 
1186
 
 
1187
    /* Create the FirstMatch tail */
 
1188
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1189
    {
 
1190
      if (tab->emb_sj_nest)
 
1191
        tab->do_firstmatch= jump_to; 
 
1192
      else
 
1193
        jump_to= tab;
 
1194
    }
 
1195
  }
 
1196
  return(false);
 
1197
}
 
1198
 
 
1199
 
 
1200
static void cleanup_sj_tmp_tables(JOIN *join)
 
1201
{
 
1202
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl; 
 
1203
       sj_tbl= sj_tbl->next)
 
1204
  {
 
1205
    if (sj_tbl->tmp_table)
 
1206
    {
 
1207
      free_tmp_table(join->thd, sj_tbl->tmp_table);
 
1208
    }
 
1209
  }
 
1210
  join->sj_tmp_tables= NULL;
 
1211
}
 
1212
 
 
1213
uint make_join_orderinfo(JOIN *join);
 
1214
 
 
1215
/**
 
1216
  global select optimisation.
 
1217
 
 
1218
  @note
 
1219
    error code saved in field 'error'
 
1220
 
 
1221
  @retval
 
1222
    0   success
 
1223
  @retval
 
1224
    1   error
 
1225
*/
 
1226
 
 
1227
int
 
1228
JOIN::optimize()
 
1229
{
 
1230
  // to prevent double initialization on EXPLAIN
 
1231
  if (optimized)
 
1232
    return(0);
 
1233
  optimized= 1;
 
1234
 
 
1235
  thd_proc_info(thd, "optimizing");
 
1236
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
 
1237
              unit->select_limit_cnt);
 
1238
  /* select_limit is used to decide if we are likely to scan the whole table */
 
1239
  select_limit= unit->select_limit_cnt;
 
1240
  if (having || (select_options & OPTION_FOUND_ROWS))
 
1241
    select_limit= HA_POS_ERROR;
 
1242
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
 
1243
  // Ignore errors of execution if option IGNORE present
 
1244
  if (thd->lex->ignore)
 
1245
    thd->lex->current_select->no_error= 1;
 
1246
 
 
1247
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
 
1248
  /* Add HAVING to WHERE if possible */
 
1249
  if (having && !group_list && !sum_func_count)
 
1250
  {
 
1251
    if (!conds)
 
1252
    {
 
1253
      conds= having;
 
1254
      having= 0;
 
1255
    }
 
1256
    else if ((conds=new Item_cond_and(conds,having)))
 
1257
    {
 
1258
      /*
 
1259
        Item_cond_and can't be fixed after creation, so we do not check
 
1260
        conds->fixed
 
1261
      */
 
1262
      conds->fix_fields(thd, &conds);
 
1263
      conds->change_ref_to_fields(thd, tables_list);
 
1264
      conds->top_level_item();
 
1265
      having= 0;
 
1266
    }
 
1267
  }
 
1268
#endif
 
1269
  SELECT_LEX *sel= thd->lex->current_select;
 
1270
  if (sel->first_cond_optimization)
 
1271
  {
 
1272
    /*
 
1273
      The following code will allocate the new items in a permanent
 
1274
      MEMROOT for prepared statements and stored procedures.
 
1275
    */
 
1276
    sel->first_cond_optimization= 0;
 
1277
 
 
1278
    /* Convert all outer joins to inner joins if possible */
 
1279
    conds= simplify_joins(this, join_list, conds, true, false);
 
1280
    build_bitmap_for_nested_joins(join_list, 0);
 
1281
  }
 
1282
 
 
1283
  conds= optimize_cond(this, conds, join_list, &cond_value);   
 
1284
  if (thd->is_error())
 
1285
  {
 
1286
    error= 1;
 
1287
    return(1);
 
1288
  }
 
1289
 
 
1290
  {
 
1291
    having= optimize_cond(this, having, join_list, &having_value);
 
1292
    if (thd->is_error())
 
1293
    {
 
1294
      error= 1;
 
1295
      return(1);
 
1296
    }
 
1297
    if (select_lex->where)
 
1298
      select_lex->cond_value= cond_value;
 
1299
    if (select_lex->having)
 
1300
      select_lex->having_value= having_value;
 
1301
 
 
1302
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 
 
1303
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
 
1304
    {                                           /* Impossible cond */
 
1305
      zero_result_cause=  having_value == Item::COND_FALSE ?
 
1306
                           "Impossible HAVING" : "Impossible WHERE";
 
1307
      error= 0;
 
1308
      return(0);
 
1309
    }
 
1310
  }
 
1311
 
 
1312
  /* Optimize count(*), min() and max() */
 
1313
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
 
1314
  {
 
1315
    int res;
 
1316
    /*
 
1317
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
 
1318
      to the WHERE conditions,
 
1319
      or 1 if all items were resolved,
 
1320
      or 0, or an error number HA_ERR_...
 
1321
    */
 
1322
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
 
1323
    {
 
1324
      if (res == HA_ERR_KEY_NOT_FOUND)
 
1325
      {
 
1326
        zero_result_cause= "No matching min/max row";
 
1327
        error=0;
 
1328
        return(0);
 
1329
      }
 
1330
      if (res > 1)
 
1331
      {
 
1332
        error= res;
 
1333
        return(1);
 
1334
      }
 
1335
      if (res < 0)
 
1336
      {
 
1337
        zero_result_cause= "No matching min/max row";
 
1338
        error=0;
 
1339
        return(0);
 
1340
      }
 
1341
      zero_result_cause= "Select tables optimized away";
 
1342
      tables_list= 0;                           // All tables resolved
 
1343
      /*
 
1344
        Extract all table-independent conditions and replace the WHERE
 
1345
        clause with them. All other conditions were computed by opt_sum_query
 
1346
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
 
1347
        so there is no need to compute the whole WHERE clause again.
 
1348
        Notice that make_cond_for_table() will always succeed to remove all
 
1349
        computed conditions, because opt_sum_query() is applicable only to
 
1350
        conjunctions.
 
1351
        Preserve conditions for EXPLAIN.
 
1352
      */
 
1353
      if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
 
1354
      {
 
1355
        COND *table_independent_conds=
 
1356
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
1357
        conds= table_independent_conds;
 
1358
      }
 
1359
    }
 
1360
  }
 
1361
  if (!tables_list)
 
1362
  {
 
1363
    error= 0;
 
1364
    return(0);
 
1365
  }
 
1366
  error= -1;                                    // Error is sent to client
 
1367
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
 
1368
 
 
1369
  /* Calculate how to do the join */
 
1370
  thd_proc_info(thd, "statistics");
 
1371
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
 
1372
      thd->is_fatal_error)
 
1373
  {
 
1374
    return(1);
 
1375
  }
 
1376
 
 
1377
  /* Remove distinct if only const tables */
 
1378
  select_distinct= select_distinct && (const_tables != tables);
 
1379
  thd_proc_info(thd, "preparing");
 
1380
  if (result->initialize_tables(this))
 
1381
  {
 
1382
    return(1);                          // error == -1
 
1383
  }
 
1384
  if (const_table_map != found_const_table_map &&
 
1385
      !(select_options & SELECT_DESCRIBE) &&
 
1386
      (!conds ||
 
1387
       !(conds->used_tables() & RAND_TABLE_BIT) ||
 
1388
       select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
 
1389
  {
 
1390
    zero_result_cause= "no matching row in const table";
 
1391
    error= 0;
 
1392
    return(0);
 
1393
  }
 
1394
  if (!(thd->options & OPTION_BIG_SELECTS) &&
 
1395
      best_read > (double) thd->variables.max_join_size &&
 
1396
      !(select_options & SELECT_DESCRIBE))
 
1397
  {                                             /* purecov: inspected */
 
1398
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
 
1399
    error= -1;
 
1400
    return(1);
 
1401
  }
 
1402
  if (const_tables && !thd->locked_tables &&
 
1403
      !(select_options & SELECT_NO_UNLOCK))
 
1404
    mysql_unlock_some_tables(thd, table, const_tables);
 
1405
  if (!conds && outer_join)
 
1406
  {
 
1407
    /* Handle the case where we have an OUTER JOIN without a WHERE */
 
1408
    conds=new Item_int((int64_t) 1,1);  // Always true
 
1409
  }
 
1410
  select= make_select(*table, const_table_map,
 
1411
                      const_table_map, conds, 1, &error);
 
1412
  if (error)
 
1413
  {                                             /* purecov: inspected */
 
1414
    error= -1;                                  /* purecov: inspected */
 
1415
    return(1);
 
1416
  }
 
1417
  
 
1418
  reset_nj_counters(join_list);
 
1419
  make_outerjoin_info(this);
 
1420
 
 
1421
  /*
 
1422
    Among the equal fields belonging to the same multiple equality
 
1423
    choose the one that is to be retrieved first and substitute
 
1424
    all references to these in where condition for a reference for
 
1425
    the selected field.
 
1426
  */
 
1427
  if (conds)
 
1428
  {
 
1429
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
 
1430
    conds->update_used_tables();
 
1431
  }
 
1432
 
 
1433
  /*
 
1434
    Permorm the the optimization on fields evaluation mentioned above
 
1435
    for all on expressions.
 
1436
  */ 
 
1437
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
 
1438
  {
 
1439
    if (*tab->on_expr_ref)
 
1440
    {
 
1441
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
 
1442
                                                         tab->cond_equal,
 
1443
                                                         map2table);
 
1444
      (*tab->on_expr_ref)->update_used_tables();
 
1445
    }
 
1446
  }
 
1447
 
 
1448
  if (conds &&!outer_join && const_table_map != found_const_table_map && 
 
1449
      (select_options & SELECT_DESCRIBE) &&
 
1450
      select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
 
1451
  {
 
1452
    conds=new Item_int((int64_t) 0,1);  // Always false
 
1453
  }
 
1454
  if (make_join_select(this, select, conds))
 
1455
  {
 
1456
    zero_result_cause=
 
1457
      "Impossible WHERE noticed after reading const tables";
 
1458
    return(0);                          // error == 0
 
1459
  }
 
1460
 
 
1461
  error= -1;                                    /* if goto err */
 
1462
 
 
1463
  /* Optimize distinct away if possible */
 
1464
  {
 
1465
    ORDER *org_order= order;
 
1466
    order=remove_const(this, order,conds,1, &simple_order);
 
1467
    if (thd->is_error())
 
1468
    {
 
1469
      error= 1;
 
1470
      return(1);
 
1471
    }
 
1472
 
 
1473
    /*
 
1474
      If we are using ORDER BY NULL or ORDER BY const_expression,
 
1475
      return result in any order (even if we are using a GROUP BY)
 
1476
    */
 
1477
    if (!order && org_order)
 
1478
      skip_sort_order= 1;
 
1479
  }
 
1480
  /*
 
1481
     Check if we can optimize away GROUP BY/DISTINCT.
 
1482
     We can do that if there are no aggregate functions, the
 
1483
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
 
1484
     (if present) contain direct references to all key parts of
 
1485
     an unique index (in whatever order) and if the key parts of the
 
1486
     unique index cannot contain NULLs.
 
1487
     Note that the unique keys for DISTINCT and GROUP BY should not
 
1488
     be the same (as long as they are unique).
 
1489
 
 
1490
     The FROM clause must contain a single non-constant table.
 
1491
  */
 
1492
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
 
1493
      !tmp_table_param.sum_func_count &&
 
1494
      (!join_tab[const_tables].select ||
 
1495
       !join_tab[const_tables].select->quick ||
 
1496
       join_tab[const_tables].select->quick->get_type() != 
 
1497
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
 
1498
  {
 
1499
    if (group_list &&
 
1500
       list_contains_unique_index(join_tab[const_tables].table,
 
1501
                                 find_field_in_order_list,
 
1502
                                 (void *) group_list))
 
1503
    {
 
1504
      /*
 
1505
        We have found that grouping can be removed since groups correspond to
 
1506
        only one row anyway, but we still have to guarantee correct result
 
1507
        order. The line below effectively rewrites the query from GROUP BY
 
1508
        <fields> to ORDER BY <fields>. There are two exceptions:
 
1509
        - if skip_sort_order is set (see above), then we can simply skip
 
1510
          GROUP BY;
 
1511
        - we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
 
1512
          with the GROUP BY ones, i.e. either one is a prefix of another.
 
1513
          We only check if the ORDER BY is a prefix of GROUP BY. In this case
 
1514
          test_if_subpart() copies the ASC/DESC attributes from the original
 
1515
          ORDER BY fields.
 
1516
          If GROUP BY is a prefix of ORDER BY, then it is safe to leave
 
1517
          'order' as is.
 
1518
       */
 
1519
      if (!order || test_if_subpart(group_list, order))
 
1520
          order= skip_sort_order ? 0 : group_list;
 
1521
      /*
 
1522
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be 
 
1523
        rewritten to IGNORE INDEX FOR ORDER BY(fields).
 
1524
      */
 
1525
      join_tab->table->keys_in_use_for_order_by=
 
1526
        join_tab->table->keys_in_use_for_group_by;
 
1527
      group_list= 0;
 
1528
      group= 0;
 
1529
    }
 
1530
    if (select_distinct &&
 
1531
       list_contains_unique_index(join_tab[const_tables].table,
 
1532
                                 find_field_in_item_list,
 
1533
                                 (void *) &fields_list))
 
1534
    {
 
1535
      select_distinct= 0;
 
1536
    }
 
1537
  }
 
1538
  if (group_list || tmp_table_param.sum_func_count)
 
1539
  {
 
1540
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
1541
      select_distinct=0;
 
1542
  }
 
1543
  else if (select_distinct && tables - const_tables == 1)
 
1544
  {
 
1545
    /*
 
1546
      We are only using one table. In this case we change DISTINCT to a
 
1547
      GROUP BY query if:
 
1548
      - The GROUP BY can be done through indexes (no sort) and the ORDER
 
1549
        BY only uses selected fields.
 
1550
        (In this case we can later optimize away GROUP BY and ORDER BY)
 
1551
      - We are scanning the whole table without LIMIT
 
1552
        This can happen if:
 
1553
        - We are using CALC_FOUND_ROWS
 
1554
        - We are using an ORDER BY that can't be optimized away.
 
1555
 
 
1556
      We don't want to use this optimization when we are using LIMIT
 
1557
      because in this case we can just create a temporary table that
 
1558
      holds LIMIT rows and stop when this table is full.
 
1559
    */
 
1560
    JOIN_TAB *tab= &join_tab[const_tables];
 
1561
    bool all_order_fields_used;
 
1562
    if (order)
 
1563
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1, 
 
1564
        &tab->table->keys_in_use_for_order_by);
 
1565
    if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
 
1566
                                          order, fields_list, all_fields,
 
1567
                                          &all_order_fields_used)))
 
1568
    {
 
1569
      bool skip_group= (skip_sort_order &&
 
1570
        test_if_skip_sort_order(tab, group_list, select_limit, 1, 
 
1571
                                &tab->table->keys_in_use_for_group_by) != 0);
 
1572
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
1573
      if ((skip_group && all_order_fields_used) ||
 
1574
          select_limit == HA_POS_ERROR ||
 
1575
          (order && !skip_sort_order))
 
1576
      {
 
1577
        /*  Change DISTINCT to GROUP BY */
 
1578
        select_distinct= 0;
 
1579
        no_order= !order;
 
1580
        if (all_order_fields_used)
 
1581
        {
 
1582
          if (order && skip_sort_order)
 
1583
          {
 
1584
            /*
 
1585
              Force MySQL to read the table in sorted order to get result in
 
1586
              ORDER BY order.
 
1587
            */
 
1588
            tmp_table_param.quick_group=0;
 
1589
          }
 
1590
          order=0;
 
1591
        }
 
1592
        group=1;                                // For end_write_group
 
1593
      }
 
1594
      else
 
1595
        group_list= 0;
 
1596
    }
 
1597
    else if (thd->is_fatal_error)                       // End of memory
 
1598
      return(1);
 
1599
  }
 
1600
  simple_group= 0;
 
1601
  {
 
1602
    ORDER *old_group_list;
 
1603
    group_list= remove_const(this, (old_group_list= group_list), conds,
 
1604
                             rollup.state == ROLLUP::STATE_NONE,
 
1605
                             &simple_group);
 
1606
    if (thd->is_error())
 
1607
    {
 
1608
      error= 1;
 
1609
      return(1);
 
1610
    }
 
1611
    if (old_group_list && !group_list)
 
1612
      select_distinct= 0;
 
1613
  }
 
1614
  if (!group_list && group)
 
1615
  {
 
1616
    order=0;                                    // The output has only one row
 
1617
    simple_order=1;
 
1618
    select_distinct= 0;                       // No need in distinct for 1 row
 
1619
    group_optimized_away= 1;
 
1620
  }
 
1621
 
 
1622
  calc_group_buffer(this, group_list);
 
1623
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
 
1624
 
 
1625
  if (test_if_subpart(group_list, order) ||
 
1626
      (!group_list && tmp_table_param.sum_func_count))
 
1627
    order=0;
 
1628
 
 
1629
  // Can't use sort on head table if using row cache
 
1630
  if (full_join)
 
1631
  {
 
1632
    if (group_list)
 
1633
      simple_group=0;
 
1634
    if (order)
 
1635
      simple_order=0;
 
1636
  }
 
1637
 
 
1638
  /*
 
1639
    Check if we need to create a temporary table.
 
1640
    This has to be done if all tables are not already read (const tables)
 
1641
    and one of the following conditions holds:
 
1642
    - We are using DISTINCT (simple distinct's are already optimized away)
 
1643
    - We are using an ORDER BY or GROUP BY on fields not in the first table
 
1644
    - We are using different ORDER BY and GROUP BY orders
 
1645
    - The user wants us to buffer the result.
 
1646
  */
 
1647
  need_tmp= (const_tables != tables &&
 
1648
             ((select_distinct || !simple_order || !simple_group) ||
 
1649
              (group_list && order) ||
 
1650
              test(select_options & OPTION_BUFFER_RESULT)));
 
1651
 
 
1652
  uint no_jbuf_after= make_join_orderinfo(this);
 
1653
  uint64_t select_opts_for_readinfo= 
 
1654
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
 
1655
 
 
1656
  sj_tmp_tables= NULL;
 
1657
  if (!select_lex->sj_nests.is_empty())
 
1658
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
 
1659
                                    no_jbuf_after);
 
1660
 
 
1661
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
 
1662
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
 
1663
    return(1);
 
1664
 
 
1665
  /* Create all structures needed for materialized subquery execution. */
 
1666
  if (setup_subquery_materialization())
 
1667
    return(1);
 
1668
 
 
1669
  /*
 
1670
    is this simple IN subquery?
 
1671
  */
 
1672
  if (!group_list && !order &&
 
1673
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
 
1674
      tables == 1 && conds &&
 
1675
      !unit->is_union())
 
1676
  {
 
1677
    if (!having)
 
1678
    {
 
1679
      Item *where= conds;
 
1680
      if (join_tab[0].type == JT_EQ_REF &&
 
1681
          join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1682
      {
 
1683
        remove_subq_pushed_predicates(&where);
 
1684
        save_index_subquery_explain_info(join_tab, where);
 
1685
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
 
1686
        error= 0;
 
1687
        return(unit->item->
 
1688
                    change_engine(new
 
1689
                                  subselect_uniquesubquery_engine(thd,
 
1690
                                                                  join_tab,
 
1691
                                                                  unit->item,
 
1692
                                                                  where)));
 
1693
      }
 
1694
      else if (join_tab[0].type == JT_REF &&
 
1695
               join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1696
      {
 
1697
        remove_subq_pushed_predicates(&where);
 
1698
        save_index_subquery_explain_info(join_tab, where);
 
1699
        join_tab[0].type= JT_INDEX_SUBQUERY;
 
1700
        error= 0;
 
1701
        return(unit->item->
 
1702
                    change_engine(new
 
1703
                                  subselect_indexsubquery_engine(thd,
 
1704
                                                                 join_tab,
 
1705
                                                                 unit->item,
 
1706
                                                                 where,
 
1707
                                                                 NULL,
 
1708
                                                                 0)));
 
1709
      }
 
1710
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
 
1711
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
 
1712
               having->name == in_having_cond)
 
1713
    {
 
1714
      join_tab[0].type= JT_INDEX_SUBQUERY;
 
1715
      error= 0;
 
1716
      conds= remove_additional_cond(conds);
 
1717
      save_index_subquery_explain_info(join_tab, conds);
 
1718
      return(unit->item->
 
1719
                  change_engine(new subselect_indexsubquery_engine(thd,
 
1720
                                                                   join_tab,
 
1721
                                                                   unit->item,
 
1722
                                                                   conds,
 
1723
                                                                   having,
 
1724
                                                                   1)));
 
1725
    }
 
1726
 
 
1727
  }
 
1728
  /*
 
1729
    Need to tell handlers that to play it safe, it should fetch all
 
1730
    columns of the primary key of the tables: this is because MySQL may
 
1731
    build row pointers for the rows, and for all columns of the primary key
 
1732
    the read set has not necessarily been set by the server code.
 
1733
  */
 
1734
  if (need_tmp || select_distinct || group_list || order)
 
1735
  {
 
1736
    for (uint i = const_tables; i < tables; i++)
 
1737
      join_tab[i].table->prepare_for_position();
 
1738
  }
 
1739
 
 
1740
  if (const_tables != tables)
 
1741
  {
 
1742
    /*
 
1743
      Because filesort always does a full table scan or a quick range scan
 
1744
      we must add the removed reference to the select for the table.
 
1745
      We only need to do this when we have a simple_order or simple_group
 
1746
      as in other cases the join is done before the sort.
 
1747
    */
 
1748
    if ((order || group_list) &&
 
1749
        (join_tab[const_tables].type != JT_ALL) &&
 
1750
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
 
1751
        ((order && simple_order) || (group_list && simple_group)))
 
1752
    {
 
1753
      if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
 
1754
        return(1);
 
1755
      }
 
1756
    }
 
1757
    
 
1758
    if (!(select_options & SELECT_BIG_RESULT) &&
 
1759
        ((group_list &&
 
1760
          (!simple_group ||
 
1761
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
 
1762
                                    unit->select_limit_cnt, 0, 
 
1763
                                    &join_tab[const_tables].table->
 
1764
                                    keys_in_use_for_group_by))) ||
 
1765
         select_distinct) &&
 
1766
        tmp_table_param.quick_group)
 
1767
    {
 
1768
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
 
1769
    }
 
1770
    if (order)
 
1771
    {
 
1772
      /*
 
1773
        Force using of tmp table if sorting by a SP or UDF function due to
 
1774
        their expensive and probably non-deterministic nature.
 
1775
      */
 
1776
      for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
1777
      {
 
1778
        Item *item= *tmp_order->item;
 
1779
        if (item->is_expensive())
 
1780
        {
 
1781
          /* Force tmp table without sort */
 
1782
          need_tmp=1; simple_order=simple_group=0;
 
1783
          break;
 
1784
        }
 
1785
      }
 
1786
    }
 
1787
  }
 
1788
 
 
1789
  tmp_having= having;
 
1790
  if (select_options & SELECT_DESCRIBE)
 
1791
  {
 
1792
    error= 0;
 
1793
    return(0);
 
1794
  }
 
1795
  having= 0;
 
1796
 
 
1797
  /*
 
1798
    The loose index scan access method guarantees that all grouping or
 
1799
    duplicate row elimination (for distinct) is already performed
 
1800
    during data retrieval, and that all MIN/MAX functions are already
 
1801
    computed for each group. Thus all MIN/MAX functions should be
 
1802
    treated as regular functions, and there is no need to perform
 
1803
    grouping in the main execution loop.
 
1804
    Notice that currently loose index scan is applicable only for
 
1805
    single table queries, thus it is sufficient to test only the first
 
1806
    join_tab element of the plan for its access method.
 
1807
  */
 
1808
  if (join_tab->is_using_loose_index_scan())
 
1809
    tmp_table_param.precomputed_group_by= true;
 
1810
 
 
1811
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1812
  if (need_tmp)
 
1813
  {
 
1814
    thd_proc_info(thd, "Creating tmp table");
 
1815
 
 
1816
    init_items_ref_array();
 
1817
 
 
1818
    tmp_table_param.hidden_field_count= (all_fields.elements -
 
1819
                                         fields_list.elements);
 
1820
    ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1821
                                                             (ORDER*) 0);
 
1822
    /*
 
1823
      Pushing LIMIT to the temporary table creation is not applicable
 
1824
      when there is ORDER BY or GROUP BY or there is no GROUP BY, but
 
1825
      there are aggregate functions, because in all these cases we need
 
1826
      all result rows.
 
1827
    */
 
1828
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
 
1829
                             !tmp_group &&
 
1830
                             !thd->lex->current_select->with_sum_func) ?
 
1831
                            select_limit : HA_POS_ERROR;
 
1832
 
 
1833
    if (!(exec_tmp_table1=
 
1834
          create_tmp_table(thd, &tmp_table_param, all_fields,
 
1835
                           tmp_group,
 
1836
                           group_list ? 0 : select_distinct,
 
1837
                           group_list && simple_group,
 
1838
                           select_options,
 
1839
                           tmp_rows_limit,
 
1840
                           (char *) "")))
 
1841
                {
 
1842
      return(1);
 
1843
    }
 
1844
 
 
1845
    /*
 
1846
      We don't have to store rows in temp table that doesn't match HAVING if:
 
1847
      - we are sorting the table and writing complete group rows to the
 
1848
        temp table.
 
1849
      - We are using DISTINCT without resolving the distinct as a GROUP BY
 
1850
        on all columns.
 
1851
      
 
1852
      If having is not handled here, it will be checked before the row
 
1853
      is sent to the client.
 
1854
    */    
 
1855
    if (tmp_having && 
 
1856
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
 
1857
      having= tmp_having;
 
1858
 
 
1859
    /* if group or order on first table, sort first */
 
1860
    if (group_list && simple_group)
 
1861
    {
 
1862
      thd_proc_info(thd, "Sorting for group");
 
1863
      if (create_sort_index(thd, this, group_list,
 
1864
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1865
          alloc_group_fields(this, group_list) ||
 
1866
          make_sum_func_list(all_fields, fields_list, 1) ||
 
1867
          setup_sum_funcs(thd, sum_funcs))
 
1868
      {
 
1869
        return(1);
 
1870
      }
 
1871
      group_list=0;
 
1872
    }
 
1873
    else
 
1874
    {
 
1875
      if (make_sum_func_list(all_fields, fields_list, 0) ||
 
1876
          setup_sum_funcs(thd, sum_funcs))
 
1877
      {
 
1878
        return(1);
 
1879
      }
 
1880
 
 
1881
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
 
1882
      {
 
1883
        thd_proc_info(thd, "Sorting for order");
 
1884
        if (create_sort_index(thd, this, order,
 
1885
                              HA_POS_ERROR, HA_POS_ERROR, true))
 
1886
        {
 
1887
          return(1);
 
1888
        }
 
1889
        order=0;
 
1890
      }
 
1891
    }
 
1892
    
 
1893
    /*
 
1894
      Optimize distinct when used on some of the tables
 
1895
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
 
1896
      In this case we can stop scanning t2 when we have found one t1.a
 
1897
    */
 
1898
 
 
1899
    if (exec_tmp_table1->distinct)
 
1900
    {
 
1901
      table_map used_tables= thd->used_tables;
 
1902
      JOIN_TAB *last_join_tab= join_tab+tables-1;
 
1903
      do
 
1904
      {
 
1905
        if (used_tables & last_join_tab->table->map)
 
1906
          break;
 
1907
        last_join_tab->not_used_in_distinct=1;
 
1908
      } while (last_join_tab-- != join_tab);
 
1909
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
 
1910
      if (order && skip_sort_order)
 
1911
      {
 
1912
        /* Should always succeed */
 
1913
        if (test_if_skip_sort_order(&join_tab[const_tables],
 
1914
                                    order, unit->select_limit_cnt, 0, 
 
1915
                                    &join_tab[const_tables].table->
 
1916
                                      keys_in_use_for_order_by))
 
1917
          order=0;
 
1918
      }
 
1919
    }
 
1920
 
 
1921
    /* 
 
1922
      If this join belongs to an uncacheable subquery save 
 
1923
      the original join 
 
1924
    */
 
1925
    if (select_lex->uncacheable && !is_top_level_join() &&
 
1926
        init_save_join_tab())
 
1927
      return(-1);                         /* purecov: inspected */
 
1928
  }
 
1929
 
 
1930
  error= 0;
 
1931
  return(0);
 
1932
}
 
1933
 
 
1934
 
 
1935
/**
 
1936
  Restore values in temporary join.
 
1937
*/
 
1938
void JOIN::restore_tmp()
 
1939
{
 
1940
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
 
1941
}
 
1942
 
 
1943
 
 
1944
int
 
1945
JOIN::reinit()
 
1946
{
 
1947
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
 
1948
                                    select_lex->offset_limit->val_uint() :
 
1949
                                    0ULL);
 
1950
 
 
1951
  first_record= 0;
 
1952
 
 
1953
  if (exec_tmp_table1)
 
1954
  {
 
1955
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
 
1956
    exec_tmp_table1->file->ha_delete_all_rows();
 
1957
    free_io_cache(exec_tmp_table1);
 
1958
    filesort_free_buffers(exec_tmp_table1,0);
 
1959
  }
 
1960
  if (exec_tmp_table2)
 
1961
  {
 
1962
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
 
1963
    exec_tmp_table2->file->ha_delete_all_rows();
 
1964
    free_io_cache(exec_tmp_table2);
 
1965
    filesort_free_buffers(exec_tmp_table2,0);
 
1966
  }
 
1967
  if (items0)
 
1968
    set_items_ref_array(items0);
 
1969
 
 
1970
  if (join_tab_save)
 
1971
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
 
1972
 
 
1973
  if (tmp_join)
 
1974
    restore_tmp();
 
1975
 
 
1976
  /* Reset of sum functions */
 
1977
  if (sum_funcs)
 
1978
  {
 
1979
    Item_sum *func, **func_ptr= sum_funcs;
 
1980
    while ((func= *(func_ptr++)))
 
1981
      func->clear();
 
1982
  }
 
1983
 
 
1984
  return(0);
 
1985
}
 
1986
 
 
1987
/**
 
1988
   @brief Save the original join layout
 
1989
      
 
1990
   @details Saves the original join layout so it can be reused in 
 
1991
   re-execution and for EXPLAIN.
 
1992
             
 
1993
   @return Operation status
 
1994
   @retval 0      success.
 
1995
   @retval 1      error occurred.
 
1996
*/
 
1997
 
 
1998
bool
 
1999
JOIN::init_save_join_tab()
 
2000
{
 
2001
  if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
 
2002
    return 1;                                  /* purecov: inspected */
 
2003
  error= 0;                                    // Ensure that tmp_join.error= 0
 
2004
  restore_tmp();
 
2005
  return 0;
 
2006
}
 
2007
 
 
2008
 
 
2009
bool
 
2010
JOIN::save_join_tab()
 
2011
{
 
2012
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
 
2013
  {
 
2014
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
 
2015
                                                sizeof(JOIN_TAB) * tables)))
 
2016
      return 1;
 
2017
  }
 
2018
  return 0;
 
2019
}
 
2020
 
 
2021
 
 
2022
/**
 
2023
  Exec select.
 
2024
 
 
2025
  @todo
 
2026
    Note, that create_sort_index calls test_if_skip_sort_order and may
 
2027
    finally replace sorting with index scan if there is a LIMIT clause in
 
2028
    the query.  It's never shown in EXPLAIN!
 
2029
 
 
2030
  @todo
 
2031
    When can we have here thd->net.report_error not zero?
 
2032
*/
 
2033
void
 
2034
JOIN::exec()
 
2035
{
 
2036
  List<Item> *columns_list= &fields_list;
 
2037
  int      tmp_error;
 
2038
 
 
2039
  thd_proc_info(thd, "executing");
 
2040
  error= 0;
 
2041
  (void) result->prepare2(); // Currently, this cannot fail.
 
2042
 
 
2043
  if (!tables_list && (tables || !select_lex->with_sum_func))
 
2044
  {                                           // Only test of functions
 
2045
    if (select_options & SELECT_DESCRIBE)
 
2046
      select_describe(this, false, false, false,
 
2047
                      (zero_result_cause?zero_result_cause:"No tables used"));
 
2048
    else
 
2049
    {
 
2050
      result->send_fields(*columns_list,
 
2051
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2052
      /*
 
2053
        We have to test for 'conds' here as the WHERE may not be constant
 
2054
        even if we don't have any tables for prepared statements or if
 
2055
        conds uses something like 'rand()'.
 
2056
      */
 
2057
      if (cond_value != Item::COND_FALSE &&
 
2058
          (!conds || conds->val_int()) &&
 
2059
          (!having || having->val_int()))
 
2060
      {
 
2061
        if (do_send_rows && result->send_data(fields_list))
 
2062
          error= 1;
 
2063
        else
 
2064
        {
 
2065
          error= (int) result->send_eof();
 
2066
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
 
2067
                         thd->sent_row_count);
 
2068
        }
 
2069
      }
 
2070
      else
 
2071
      {
 
2072
        error=(int) result->send_eof();
 
2073
        send_records= 0;
 
2074
      }
 
2075
    }
 
2076
    /* Single select (without union) always returns 0 or 1 row */
 
2077
    thd->limit_found_rows= send_records;
 
2078
    thd->examined_row_count= 0;
 
2079
    return;
 
2080
  }
 
2081
  /*
 
2082
    Don't reset the found rows count if there're no tables as
 
2083
    FOUND_ROWS() may be called. Never reset the examined row count here.
 
2084
    It must be accumulated from all join iterations of all join parts.
 
2085
  */
 
2086
  if (tables)
 
2087
    thd->limit_found_rows= 0;
 
2088
 
 
2089
  if (zero_result_cause)
 
2090
  {
 
2091
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
 
2092
                            *columns_list,
 
2093
                            send_row_on_empty_set(),
 
2094
                            select_options,
 
2095
                            zero_result_cause,
 
2096
                            having);
 
2097
    return;
 
2098
  }
 
2099
 
 
2100
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
2101
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
2102
    return;
 
2103
 
 
2104
  if (select_options & SELECT_DESCRIBE)
 
2105
  {
 
2106
    /*
 
2107
      Check if we managed to optimize ORDER BY away and don't use temporary
 
2108
      table to resolve ORDER BY: in that case, we only may need to do
 
2109
      filesort for GROUP BY.
 
2110
    */
 
2111
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
 
2112
    {
 
2113
      /*
 
2114
        Reset 'order' to 'group_list' and reinit variables describing
 
2115
        'order'
 
2116
      */
 
2117
      order= group_list;
 
2118
      simple_order= simple_group;
 
2119
      skip_sort_order= 0;
 
2120
    }
 
2121
    if (order && 
 
2122
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
 
2123
        (const_tables == tables ||
 
2124
         ((simple_order || skip_sort_order) &&
 
2125
          test_if_skip_sort_order(&join_tab[const_tables], order,
 
2126
                                  select_limit, 0, 
 
2127
                                  &join_tab[const_tables].table->
 
2128
                                    keys_in_use_for_query))))
 
2129
      order=0;
 
2130
    having= tmp_having;
 
2131
    select_describe(this, need_tmp,
 
2132
                    order != 0 && !skip_sort_order,
 
2133
                    select_distinct,
 
2134
                    !tables ? "No tables used" : NullS);
 
2135
    return;
 
2136
  }
 
2137
 
 
2138
  JOIN *curr_join= this;
 
2139
  List<Item> *curr_all_fields= &all_fields;
 
2140
  List<Item> *curr_fields_list= &fields_list;
 
2141
  TABLE *curr_tmp_table= 0;
 
2142
  /*
 
2143
    Initialize examined rows here because the values from all join parts
 
2144
    must be accumulated in examined_row_count. Hence every join
 
2145
    iteration must count from zero.
 
2146
  */
 
2147
  curr_join->examined_rows= 0;
 
2148
 
 
2149
  /* Create a tmp table if distinct or if the sort is too complicated */
 
2150
  if (need_tmp)
 
2151
  {
 
2152
    if (tmp_join)
 
2153
    {
 
2154
      /*
 
2155
        We are in a non cacheable sub query. Get the saved join structure
 
2156
        after optimization.
 
2157
        (curr_join may have been modified during last exection and we need
 
2158
        to reset it)
 
2159
      */
 
2160
      curr_join= tmp_join;
 
2161
    }
 
2162
    curr_tmp_table= exec_tmp_table1;
 
2163
 
 
2164
    /* Copy data to the temporary table */
 
2165
    thd_proc_info(thd, "Copying to tmp table");
 
2166
    if (!curr_join->sort_and_group &&
 
2167
        curr_join->const_tables != curr_join->tables)
 
2168
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2169
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2170
    {
 
2171
      error= tmp_error;
 
2172
      return;
 
2173
    }
 
2174
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
 
2175
    
 
2176
    if (curr_join->having)
 
2177
      curr_join->having= curr_join->tmp_having= 0; // Allready done
 
2178
    
 
2179
    /* Change sum_fields reference to calculated fields in tmp_table */
 
2180
    curr_join->all_fields= *curr_all_fields;
 
2181
    if (!items1)
 
2182
    {
 
2183
      items1= items0 + all_fields.elements;
 
2184
      if (sort_and_group || curr_tmp_table->group)
 
2185
      {
 
2186
        if (change_to_use_tmp_fields(thd, items1,
 
2187
                                     tmp_fields_list1, tmp_all_fields1,
 
2188
                                     fields_list.elements, all_fields))
 
2189
          return;
 
2190
      }
 
2191
      else
 
2192
      {
 
2193
        if (change_refs_to_tmp_fields(thd, items1,
 
2194
                                      tmp_fields_list1, tmp_all_fields1,
 
2195
                                      fields_list.elements, all_fields))
 
2196
          return;
 
2197
      }
 
2198
      curr_join->tmp_all_fields1= tmp_all_fields1;
 
2199
      curr_join->tmp_fields_list1= tmp_fields_list1;
 
2200
      curr_join->items1= items1;
 
2201
    }
 
2202
    curr_all_fields= &tmp_all_fields1;
 
2203
    curr_fields_list= &tmp_fields_list1;
 
2204
    curr_join->set_items_ref_array(items1);
 
2205
    
 
2206
    if (sort_and_group || curr_tmp_table->group)
 
2207
    {
 
2208
      curr_join->tmp_table_param.field_count+= 
 
2209
        curr_join->tmp_table_param.sum_func_count+
 
2210
        curr_join->tmp_table_param.func_count;
 
2211
      curr_join->tmp_table_param.sum_func_count= 
 
2212
        curr_join->tmp_table_param.func_count= 0;
 
2213
    }
 
2214
    else
 
2215
    {
 
2216
      curr_join->tmp_table_param.field_count+= 
 
2217
        curr_join->tmp_table_param.func_count;
 
2218
      curr_join->tmp_table_param.func_count= 0;
 
2219
    }
 
2220
    
 
2221
    if (curr_tmp_table->group)
 
2222
    {                                           // Already grouped
 
2223
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
 
2224
        curr_join->order= curr_join->group_list;  /* order by group */
 
2225
      curr_join->group_list= 0;
 
2226
    }
 
2227
    
 
2228
    /*
 
2229
      If we have different sort & group then we must sort the data by group
 
2230
      and copy it to another tmp table
 
2231
      This code is also used if we are using distinct something
 
2232
      we haven't been able to store in the temporary table yet
 
2233
      like SEC_TO_TIME(SUM(...)).
 
2234
    */
 
2235
 
 
2236
    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))
 
2237
    {                                   /* Must copy to another table */
 
2238
      /* Free first data from old join */
 
2239
      curr_join->join_free();
 
2240
      if (make_simple_join(curr_join, curr_tmp_table))
 
2241
        return;
 
2242
      calc_group_buffer(curr_join, group_list);
 
2243
      count_field_types(select_lex, &curr_join->tmp_table_param,
 
2244
                        curr_join->tmp_all_fields1,
 
2245
                        curr_join->select_distinct && !curr_join->group_list);
 
2246
      curr_join->tmp_table_param.hidden_field_count= 
 
2247
        (curr_join->tmp_all_fields1.elements-
 
2248
         curr_join->tmp_fields_list1.elements);
 
2249
      
 
2250
      
 
2251
      if (exec_tmp_table2)
 
2252
        curr_tmp_table= exec_tmp_table2;
 
2253
      else
 
2254
      {
 
2255
        /* group data to new table */
 
2256
 
 
2257
        /*
 
2258
          If the access method is loose index scan then all MIN/MAX
 
2259
          functions are precomputed, and should be treated as regular
 
2260
          functions. See extended comment in JOIN::exec.
 
2261
        */
 
2262
        if (curr_join->join_tab->is_using_loose_index_scan())
 
2263
          curr_join->tmp_table_param.precomputed_group_by= true;
 
2264
 
 
2265
        if (!(curr_tmp_table=
 
2266
              exec_tmp_table2= create_tmp_table(thd,
 
2267
                                                &curr_join->tmp_table_param,
 
2268
                                                *curr_all_fields,
 
2269
                                                (ORDER*) 0,
 
2270
                                                curr_join->select_distinct && 
 
2271
                                                !curr_join->group_list,
 
2272
                                                1, curr_join->select_options,
 
2273
                                                HA_POS_ERROR,
 
2274
                                                (char *) "")))
 
2275
          return;
 
2276
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
2277
      }
 
2278
      if (curr_join->group_list)
 
2279
      {
 
2280
        thd_proc_info(thd, "Creating sort index");
 
2281
        if (curr_join->join_tab == join_tab && save_join_tab())
 
2282
        {
 
2283
          return;
 
2284
        }
 
2285
        if (create_sort_index(thd, curr_join, curr_join->group_list,
 
2286
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
 
2287
            make_group_fields(this, curr_join))
 
2288
        {
 
2289
          return;
 
2290
        }
 
2291
        sortorder= curr_join->sortorder;
 
2292
      }
 
2293
      
 
2294
      thd_proc_info(thd, "Copying to group table");
 
2295
      tmp_error= -1;
 
2296
      if (curr_join != this)
 
2297
      {
 
2298
        if (sum_funcs2)
 
2299
        {
 
2300
          curr_join->sum_funcs= sum_funcs2;
 
2301
          curr_join->sum_funcs_end= sum_funcs_end2; 
 
2302
        }
 
2303
        else
 
2304
        {
 
2305
          curr_join->alloc_func_list();
 
2306
          sum_funcs2= curr_join->sum_funcs;
 
2307
          sum_funcs_end2= curr_join->sum_funcs_end;
 
2308
        }
 
2309
      }
 
2310
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2311
                                        1, true))
 
2312
        return;
 
2313
      curr_join->group_list= 0;
 
2314
      if (!curr_join->sort_and_group &&
 
2315
          curr_join->const_tables != curr_join->tables)
 
2316
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2317
      if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2318
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2319
      {
 
2320
        error= tmp_error;
 
2321
        return;
 
2322
      }
 
2323
      end_read_record(&curr_join->join_tab->read_record);
 
2324
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
 
2325
      curr_join->join_tab[0].table= 0;           // Table is freed
 
2326
      
 
2327
      // No sum funcs anymore
 
2328
      if (!items2)
 
2329
      {
 
2330
        items2= items1 + all_fields.elements;
 
2331
        if (change_to_use_tmp_fields(thd, items2,
 
2332
                                     tmp_fields_list2, tmp_all_fields2, 
 
2333
                                     fields_list.elements, tmp_all_fields1))
 
2334
          return;
 
2335
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
2336
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
2337
      }
 
2338
      curr_fields_list= &curr_join->tmp_fields_list2;
 
2339
      curr_all_fields= &curr_join->tmp_all_fields2;
 
2340
      curr_join->set_items_ref_array(items2);
 
2341
      curr_join->tmp_table_param.field_count+= 
 
2342
        curr_join->tmp_table_param.sum_func_count;
 
2343
      curr_join->tmp_table_param.sum_func_count= 0;
 
2344
    }
 
2345
    if (curr_tmp_table->distinct)
 
2346
      curr_join->select_distinct=0;             /* Each row is unique */
 
2347
    
 
2348
    curr_join->join_free();                     /* Free quick selects */
 
2349
    if (curr_join->select_distinct && ! curr_join->group_list)
 
2350
    {
 
2351
      thd_proc_info(thd, "Removing duplicates");
 
2352
      if (curr_join->tmp_having)
 
2353
        curr_join->tmp_having->update_used_tables();
 
2354
      if (remove_duplicates(curr_join, curr_tmp_table,
 
2355
                            *curr_fields_list, curr_join->tmp_having))
 
2356
        return;
 
2357
      curr_join->tmp_having=0;
 
2358
      curr_join->select_distinct=0;
 
2359
    }
 
2360
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
 
2361
    if (make_simple_join(curr_join, curr_tmp_table))
 
2362
      return;
 
2363
    calc_group_buffer(curr_join, curr_join->group_list);
 
2364
    count_field_types(select_lex, &curr_join->tmp_table_param, 
 
2365
                      *curr_all_fields, 0);
 
2366
    
 
2367
  }
 
2368
  
 
2369
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
 
2370
  {
 
2371
    if (make_group_fields(this, curr_join))
 
2372
    {
 
2373
      return;
 
2374
    }
 
2375
    if (!items3)
 
2376
    {
 
2377
      if (!items0)
 
2378
        init_items_ref_array();
 
2379
      items3= ref_pointer_array + (all_fields.elements*4);
 
2380
      setup_copy_fields(thd, &curr_join->tmp_table_param,
 
2381
                        items3, tmp_fields_list3, tmp_all_fields3,
 
2382
                        curr_fields_list->elements, *curr_all_fields);
 
2383
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
 
2384
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
 
2385
      tmp_table_param.save_copy_field_end=
 
2386
        curr_join->tmp_table_param.copy_field_end;
 
2387
      curr_join->tmp_all_fields3= tmp_all_fields3;
 
2388
      curr_join->tmp_fields_list3= tmp_fields_list3;
 
2389
    }
 
2390
    else
 
2391
    {
 
2392
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
 
2393
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
 
2394
      curr_join->tmp_table_param.copy_field_end=
 
2395
        tmp_table_param.save_copy_field_end;
 
2396
    }
 
2397
    curr_fields_list= &tmp_fields_list3;
 
2398
    curr_all_fields= &tmp_all_fields3;
 
2399
    curr_join->set_items_ref_array(items3);
 
2400
 
 
2401
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2402
                                      1, true) || 
 
2403
        setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2404
        thd->is_fatal_error)
 
2405
      return;
 
2406
  }
 
2407
  if (curr_join->group_list || curr_join->order)
 
2408
  {
 
2409
    thd_proc_info(thd, "Sorting result");
 
2410
    /* If we have already done the group, add HAVING to sorted table */
 
2411
    if (curr_join->tmp_having && ! curr_join->group_list && 
 
2412
        ! curr_join->sort_and_group)
 
2413
    {
 
2414
      // Some tables may have been const
 
2415
      curr_join->tmp_having->update_used_tables();
 
2416
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
 
2417
      table_map used_tables= (curr_join->const_table_map |
 
2418
                              curr_table->table->map);
 
2419
 
 
2420
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
 
2421
                                                 used_tables,
 
2422
                                                 used_tables, 0);
 
2423
      if (sort_table_cond)
 
2424
      {
 
2425
        if (!curr_table->select)
 
2426
          if (!(curr_table->select= new SQL_SELECT))
 
2427
            return;
 
2428
        if (!curr_table->select->cond)
 
2429
          curr_table->select->cond= sort_table_cond;
 
2430
        else                                    // This should never happen
 
2431
        {
 
2432
          if (!(curr_table->select->cond=
 
2433
                new Item_cond_and(curr_table->select->cond,
 
2434
                                  sort_table_cond)))
 
2435
            return;
 
2436
          /*
 
2437
            Item_cond_and do not need fix_fields for execution, its parameters
 
2438
            are fixed or do not need fix_fields, too
 
2439
          */
 
2440
          curr_table->select->cond->quick_fix_field();
 
2441
        }
 
2442
        curr_table->select_cond= curr_table->select->cond;
 
2443
        curr_table->select_cond->top_level_item();
 
2444
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
2445
                                                   ~ (table_map) 0,
 
2446
                                                   ~used_tables, 0);
 
2447
      }
 
2448
    }
 
2449
    {
 
2450
      if (group)
 
2451
        curr_join->select_limit= HA_POS_ERROR;
 
2452
      else
 
2453
      {
 
2454
        /*
 
2455
          We can abort sorting after thd->select_limit rows if we there is no
 
2456
          WHERE clause for any tables after the sorted one.
 
2457
        */
 
2458
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
2459
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
2460
        for (; curr_table < end_table ; curr_table++)
 
2461
        {
 
2462
          /*
 
2463
            table->keyuse is set in the case there was an original WHERE clause
 
2464
            on the table that was optimized away.
 
2465
          */
 
2466
          if (curr_table->select_cond ||
 
2467
              (curr_table->keyuse && !curr_table->first_inner))
 
2468
          {
 
2469
            /* We have to sort all rows */
 
2470
            curr_join->select_limit= HA_POS_ERROR;
 
2471
            break;
 
2472
          }
 
2473
        }
 
2474
      }
 
2475
      if (curr_join->join_tab == join_tab && save_join_tab())
 
2476
      {
 
2477
        return;
 
2478
      }
 
2479
      /*
 
2480
        Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
 
2481
        chose FILESORT to be faster than INDEX SCAN or there is no 
 
2482
        suitable index present.
 
2483
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
2484
        finally replace sorting with index scan if there is a LIMIT clause in
 
2485
        the query. XXX: it's never shown in EXPLAIN!
 
2486
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
2487
      */
 
2488
      if (create_sort_index(thd, curr_join,
 
2489
                            curr_join->group_list ? 
 
2490
                            curr_join->group_list : curr_join->order,
 
2491
                            curr_join->select_limit,
 
2492
                            (select_options & OPTION_FOUND_ROWS ?
 
2493
                             HA_POS_ERROR : unit->select_limit_cnt),
 
2494
                            curr_join->group_list ? true : false))
 
2495
        return;
 
2496
      sortorder= curr_join->sortorder;
 
2497
      if (curr_join->const_tables != curr_join->tables &&
 
2498
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
 
2499
      {
 
2500
        /*
 
2501
          If no IO cache exists for the first table then we are using an
 
2502
          INDEX SCAN and no filesort. Thus we should not remove the sorted
 
2503
          attribute on the INDEX SCAN.
 
2504
        */
 
2505
        skip_sort_order= 1;
 
2506
      }
 
2507
    }
 
2508
  }
 
2509
  /* XXX: When can we have here thd->is_error() not zero? */
 
2510
  if (thd->is_error())
 
2511
  {
 
2512
    error= thd->is_error();
 
2513
    return;
 
2514
  }
 
2515
  curr_join->having= curr_join->tmp_having;
 
2516
  curr_join->fields= curr_fields_list;
 
2517
 
 
2518
  {
 
2519
    thd_proc_info(thd, "Sending data");
 
2520
    result->send_fields(*curr_fields_list,
 
2521
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2522
    error= do_select(curr_join, curr_fields_list, NULL);
 
2523
    thd->limit_found_rows= curr_join->send_records;
 
2524
  }
 
2525
 
 
2526
  /* Accumulate the counts from all join iterations of all join parts. */
 
2527
  thd->examined_row_count+= curr_join->examined_rows;
 
2528
 
 
2529
  /* 
 
2530
    With EXPLAIN EXTENDED we have to restore original ref_array
 
2531
    for a derived table which is always materialized.
 
2532
    Otherwise we would not be able to print the query  correctly.
 
2533
  */ 
 
2534
  if (items0 &&
 
2535
      (thd->lex->describe & DESCRIBE_EXTENDED) &&
 
2536
      select_lex->linkage == DERIVED_TABLE_TYPE)      
 
2537
    set_items_ref_array(items0);
 
2538
 
 
2539
  return;
 
2540
}
 
2541
 
 
2542
 
 
2543
/**
 
2544
  Clean up join.
 
2545
 
 
2546
  @return
 
2547
    Return error that hold JOIN.
 
2548
*/
 
2549
 
 
2550
int
 
2551
JOIN::destroy()
 
2552
{
 
2553
  select_lex->join= 0;
 
2554
 
 
2555
  if (tmp_join)
 
2556
  {
 
2557
    if (join_tab != tmp_join->join_tab)
 
2558
    {
 
2559
      JOIN_TAB *tab, *end;
 
2560
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
 
2561
        tab->cleanup();
 
2562
    }
 
2563
    tmp_join->tmp_join= 0;
 
2564
    tmp_table_param.copy_field=0;
 
2565
    return(tmp_join->destroy());
 
2566
  }
 
2567
  cond_equal= 0;
 
2568
 
 
2569
  cleanup(1);
 
2570
  if (exec_tmp_table1)
 
2571
    free_tmp_table(thd, exec_tmp_table1);
 
2572
  if (exec_tmp_table2)
 
2573
    free_tmp_table(thd, exec_tmp_table2);
 
2574
  delete select;
 
2575
  delete_dynamic(&keyuse);
 
2576
  return(error);
 
2577
}
 
2578
 
 
2579
 
 
2580
 
316
2581
/**
317
2582
  An entry point to single-unit select (a select without UNION).
318
2583
 
319
 
  @param session                  thread Cursor
 
2584
  @param thd                  thread handler
320
2585
  @param rref_pointer_array   a reference to ref_pointer_array of
321
2586
                              the top-level select_lex for this query
322
2587
  @param tables               list of all tables used in this query.
323
2588
                              The tables have been pre-opened.
324
 
  @param wild_num             number of wildcards used in the top level
 
2589
  @param wild_num             number of wildcards used in the top level 
325
2590
                              select of this query.
326
2591
                              For example statement
327
2592
                              SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
337
2602
  @param order                linked list of ORDER BY agruments
338
2603
  @param group                linked list of GROUP BY arguments
339
2604
  @param having               top level item of HAVING expression
 
2605
  @param proc_param           list of PROCEDUREs
340
2606
  @param select_options       select options (BIG_RESULT, etc)
341
2607
  @param result               an instance of result set handling class.
342
2608
                              This object is responsible for send result
343
2609
                              set rows to the client or inserting them
344
2610
                              into a table.
345
 
  @param select_lex           the only Select_Lex of this query
 
2611
  @param select_lex           the only SELECT_LEX of this query
346
2612
  @param unit                 top-level UNIT of this query
347
2613
                              UNIT is an artificial object created by the
348
2614
                              parser for every SELECT clause.
355
2621
  @retval
356
2622
    true   an error
357
2623
*/
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)
 
2624
 
 
2625
bool
 
2626
mysql_select(THD *thd, Item ***rref_pointer_array,
 
2627
             TABLE_LIST *tables, uint wild_num, List<Item> &fields,
 
2628
             COND *conds, uint og_num,  ORDER *order, ORDER *group,
 
2629
             Item *having, ORDER *proc_param, uint64_t select_options,
 
2630
             select_result *result, SELECT_LEX_UNIT *unit,
 
2631
             SELECT_LEX *select_lex)
372
2632
{
373
2633
  bool err;
374
2634
  bool free_join= 1;
375
2635
 
376
2636
  select_lex->context.resolve_in_select_list= true;
377
 
  Join *join;
 
2637
  JOIN *join;
378
2638
  if (select_lex->join != 0)
379
2639
  {
380
2640
    join= select_lex->join;
383
2643
      creation
384
2644
    */
385
2645
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
386
 
        (select_options & SELECT_DESCRIBE))
 
2646
        (select_options & SELECT_DESCRIBE))
387
2647
    {
388
2648
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
389
2649
      {
390
 
        //here is EXPLAIN of subselect or derived table
391
 
        if (join->change_result(result))
392
 
        {
393
 
          return(true);
394
 
        }
 
2650
        //here is EXPLAIN of subselect or derived table
 
2651
        if (join->change_result(result))
 
2652
        {
 
2653
          return(true);
 
2654
        }
395
2655
      }
396
2656
      else
397
2657
      {
398
2658
        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
 
        }
 
2659
                               conds, og_num, order, group, having, proc_param,
 
2660
                               select_lex, unit)))
 
2661
        {
 
2662
          goto err;
 
2663
        }
403
2664
      }
404
2665
    }
405
2666
    free_join= 0;
407
2668
  }
408
2669
  else
409
2670
  {
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
 
2671
    if (!(join= new JOIN(thd, fields, select_options, result)))
 
2672
        return(true);
 
2673
    thd_proc_info(thd, "init");
 
2674
    thd->used_tables=0;                         // Updated by setup_fields
414
2675
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
415
 
                           conds, og_num, order, group, having,
 
2676
                           conds, og_num, order, group, having, proc_param,
416
2677
                           select_lex, unit)) == true)
417
2678
    {
418
2679
      goto err;
419
2680
    }
420
2681
  }
421
2682
 
422
 
  err= join->optimize();
423
 
  if (err)
424
 
  {
425
 
    goto err; // 1
426
 
  }
427
 
 
428
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
2683
  /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
 
2684
  if (join->flatten_subqueries())
 
2685
  {
 
2686
    err= 1;
 
2687
    goto err;
 
2688
  }
 
2689
  /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
 
2690
 
 
2691
  if ((err= join->optimize()))
 
2692
  {
 
2693
    goto err;                                   // 1
 
2694
  }
 
2695
 
 
2696
  if (thd->lex->describe & DESCRIBE_EXTENDED)
429
2697
  {
430
2698
    join->conds_history= join->conds;
431
2699
    join->having_history= (join->having?join->having:join->tmp_having);
432
2700
  }
433
2701
 
434
 
  if (session->is_error())
 
2702
  if (thd->is_error())
435
2703
    goto err;
436
2704
 
437
2705
  join->exec();
438
2706
 
439
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
2707
  if (thd->lex->describe & DESCRIBE_EXTENDED)
440
2708
  {
441
2709
    select_lex->where= join->conds_history;
442
2710
    select_lex->having= join->having_history;
445
2713
err:
446
2714
  if (free_join)
447
2715
  {
448
 
    session->set_proc_info("end");
 
2716
    thd_proc_info(thd, "end");
449
2717
    err|= select_lex->cleanup();
450
 
    return(err || session->is_error());
 
2718
    return(err || thd->is_error());
451
2719
  }
452
2720
  return(join->error);
453
2721
}
454
2722
 
455
 
inline Item *and_items(Item* cond, Item *item)
 
2723
 
 
2724
int subq_sj_candidate_cmp(Item_in_subselect* const *el1, 
 
2725
                          Item_in_subselect* const *el2)
 
2726
{
 
2727
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 : 
 
2728
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
 
2729
}
 
2730
 
 
2731
 
 
2732
inline Item * and_items(Item* cond, Item *item)
456
2733
{
457
2734
  return (cond? (new Item_cond_and(cond, item)) : item);
458
2735
}
459
2736
 
 
2737
 
 
2738
static TABLE_LIST *alloc_join_nest(THD *thd)
 
2739
{
 
2740
  TABLE_LIST *tbl;
 
2741
  if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
 
2742
                                       sizeof(NESTED_JOIN))))
 
2743
    return NULL;
 
2744
  tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl + 
 
2745
                                    ALIGN_SIZE(sizeof(TABLE_LIST)));
 
2746
  return tbl;
 
2747
}
 
2748
 
 
2749
 
 
2750
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
 
2751
{
 
2752
  List_iterator<TABLE_LIST> it(*tlist);
 
2753
  TABLE_LIST *table;
 
2754
  while ((table= it++))
 
2755
  {
 
2756
    if (table->on_expr)
 
2757
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
 
2758
    if (table->nested_join)
 
2759
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
 
2760
  }
 
2761
}
 
2762
 
 
2763
 
 
2764
/*
 
2765
  Convert a subquery predicate into a TABLE_LIST semi-join nest
 
2766
 
 
2767
  SYNOPSIS
 
2768
    convert_subq_to_sj()
 
2769
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON 
 
2770
                    clause
 
2771
       subq_pred    Subquery predicate to be converted
 
2772
  
 
2773
  DESCRIPTION
 
2774
    Convert a subquery predicate into a TABLE_LIST semi-join nest. All the 
 
2775
    prerequisites are already checked, so the conversion is always successfull.
 
2776
 
 
2777
    Prepared Statements: the transformation is permanent:
 
2778
     - Changes in TABLE_LIST structures are naturally permanent
 
2779
     - Item tree changes are performed on statement MEM_ROOT:
 
2780
        = we activate statement MEM_ROOT 
 
2781
        = this function is called before the first fix_prepare_information
 
2782
          call.
 
2783
 
 
2784
    This is intended because the criteria for subquery-to-sj conversion remain
 
2785
    constant for the lifetime of the Prepared Statement.
 
2786
 
 
2787
  RETURN
 
2788
    false  OK
 
2789
    true   Out of memory error
 
2790
*/
 
2791
 
 
2792
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
 
2793
{
 
2794
  SELECT_LEX *parent_lex= parent_join->select_lex;
 
2795
  TABLE_LIST *emb_tbl_nest= NULL;
 
2796
  List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
 
2797
  THD *thd= parent_join->thd;
 
2798
 
 
2799
  /*
 
2800
    1. Find out where to put the predicate into.
 
2801
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
 
2802
  */
 
2803
  if ((void*)subq_pred->expr_join_nest != (void*)1)
 
2804
  {
 
2805
    if (subq_pred->expr_join_nest->nested_join)
 
2806
    {
 
2807
      /*
 
2808
        We're dealing with
 
2809
 
 
2810
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
 
2811
 
 
2812
        The sj-nest will be inserted into the brackets nest.
 
2813
      */
 
2814
      emb_tbl_nest=  subq_pred->expr_join_nest;
 
2815
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2816
    }
 
2817
    else if (!subq_pred->expr_join_nest->outer_join)
 
2818
    {
 
2819
      /*
 
2820
        We're dealing with
 
2821
 
 
2822
          ... INNER JOIN tblX ON (subquery AND whatever) ...
 
2823
 
 
2824
        The sj-nest will be tblX's "sibling", i.e. another child of its
 
2825
        parent. This is ok because tblX is joined as an inner join.
 
2826
      */
 
2827
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
 
2828
      if (emb_tbl_nest)
 
2829
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2830
    }
 
2831
    else if (!subq_pred->expr_join_nest->nested_join)
 
2832
    {
 
2833
      TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;      
 
2834
      TABLE_LIST *wrap_nest;
 
2835
      /*
 
2836
        We're dealing with
 
2837
 
 
2838
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
 
2839
 
 
2840
        we'll need to convert it into:
 
2841
 
 
2842
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
 
2843
                        |                      |
 
2844
                        |<----- wrap_nest ---->|
 
2845
        
 
2846
        Q:  other subqueries may be pointing to this element. What to do?
 
2847
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
 
2848
            But we'll need to fix other pointers.
 
2849
        A2: Another way: have TABLE_LIST::next_ptr so the following
 
2850
            subqueries know the table has been nested.
 
2851
        A3: changes in the TABLE_LIST::outer_join will make everything work
 
2852
            automatically.
 
2853
      */
 
2854
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
 
2855
      {
 
2856
        return(true);
 
2857
      }
 
2858
      wrap_nest->embedding= outer_tbl->embedding;
 
2859
      wrap_nest->join_list= outer_tbl->join_list;
 
2860
      wrap_nest->alias= (char*) "(sj-wrap)";
 
2861
 
 
2862
      wrap_nest->nested_join->join_list.empty();
 
2863
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
 
2864
 
 
2865
      outer_tbl->embedding= wrap_nest;
 
2866
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
 
2867
 
 
2868
      /*
 
2869
        wrap_nest will take place of outer_tbl, so move the outer join flag
 
2870
        and on_expr
 
2871
      */
 
2872
      wrap_nest->outer_join= outer_tbl->outer_join;
 
2873
      outer_tbl->outer_join= 0;
 
2874
 
 
2875
      wrap_nest->on_expr= outer_tbl->on_expr;
 
2876
      outer_tbl->on_expr= NULL;
 
2877
 
 
2878
      List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
 
2879
      TABLE_LIST *tbl;
 
2880
      while ((tbl= li++))
 
2881
      {
 
2882
        if (tbl == outer_tbl)
 
2883
        {
 
2884
          li.replace(wrap_nest);
 
2885
          break;
 
2886
        }
 
2887
      }
 
2888
      /*
 
2889
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the 
 
2890
        semi-join nest into it
 
2891
      */
 
2892
      emb_join_list= &wrap_nest->nested_join->join_list;
 
2893
      emb_tbl_nest=  wrap_nest;
 
2894
    }
 
2895
  }
 
2896
 
 
2897
  TABLE_LIST *sj_nest;
 
2898
  NESTED_JOIN *nested_join;
 
2899
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
 
2900
  {
 
2901
    return(true);
 
2902
  }
 
2903
  nested_join= sj_nest->nested_join;
 
2904
 
 
2905
  sj_nest->join_list= emb_join_list;
 
2906
  sj_nest->embedding= emb_tbl_nest;
 
2907
  sj_nest->alias= (char*) "(sj-nest)";
 
2908
  /* Nests do not participate in those 'chains', so: */
 
2909
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
 
2910
  emb_join_list->push_back(sj_nest);
 
2911
 
 
2912
  /* 
 
2913
    nested_join->used_tables and nested_join->not_null_tables are
 
2914
    initialized in simplify_joins().
 
2915
  */
 
2916
  
 
2917
  /* 
 
2918
    2. Walk through subquery's top list and set 'embedding' to point to the
 
2919
       sj-nest.
 
2920
  */
 
2921
  st_select_lex *subq_lex= subq_pred->unit->first_select();
 
2922
  nested_join->join_list.empty();
 
2923
  List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
 
2924
  TABLE_LIST *tl, *last_leaf;
 
2925
  while ((tl= li++))
 
2926
  {
 
2927
    tl->embedding= sj_nest;
 
2928
    tl->join_list= &nested_join->join_list;
 
2929
    nested_join->join_list.push_back(tl);
 
2930
  }
 
2931
  
 
2932
  /*
 
2933
    Reconnect the next_leaf chain.
 
2934
    TODO: Do we have to put subquery's tables at the end of the chain?
 
2935
          Inserting them at the beginning would be a bit faster.
 
2936
    NOTE: We actually insert them at the front! That's because the order is
 
2937
          reversed in this list.
 
2938
  */
 
2939
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
 
2940
  tl->next_leaf= subq_lex->leaf_tables;
 
2941
  last_leaf= tl;
 
2942
 
 
2943
  /*
 
2944
    Same as above for next_local chain
 
2945
    (a theory: a next_local chain always starts with ::leaf_tables
 
2946
     because view's tables are inserted after the view)
 
2947
  */
 
2948
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
 
2949
  tl->next_local= subq_lex->leaf_tables;
 
2950
 
 
2951
  /* A theory: no need to re-connect the next_global chain */
 
2952
 
 
2953
  /* 3. Remove the original subquery predicate from the WHERE/ON */
 
2954
 
 
2955
  // The subqueries were replaced for Item_int(1) earlier
 
2956
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
 
2957
  /*TODO: also reset the 'with_subselect' there. */
 
2958
 
 
2959
  /* n. Adjust the parent_join->tables counter */
 
2960
  uint table_no= parent_join->tables;
 
2961
  /* n. Walk through child's tables and adjust table->map */
 
2962
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
 
2963
  {
 
2964
    tl->table->tablenr= table_no;
 
2965
    tl->table->map= ((table_map)1) << table_no;
 
2966
    SELECT_LEX *old_sl= tl->select_lex;
 
2967
    tl->select_lex= parent_join->select_lex; 
 
2968
    for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
 
2969
      emb->select_lex= parent_join->select_lex;
 
2970
  }
 
2971
  parent_join->tables += subq_lex->join->tables;
 
2972
 
 
2973
  /* 
 
2974
    Put the subquery's WHERE into semi-join's sj_on_expr
 
2975
    Add the subquery-induced equalities too.
 
2976
  */
 
2977
  SELECT_LEX *save_lex= thd->lex->current_select;
 
2978
  thd->lex->current_select=subq_lex;
 
2979
  if (!subq_pred->left_expr->fixed &&
 
2980
       subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
 
2981
    return(true);
 
2982
  thd->lex->current_select=save_lex;
 
2983
 
 
2984
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
 
2985
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
 
2986
                                        subq_pred->left_expr->used_tables();
 
2987
  sj_nest->sj_on_expr= subq_lex->where;
 
2988
 
 
2989
  /*
 
2990
    Create the IN-equalities and inject them into semi-join's ON expression.
 
2991
    Additionally, for InsideOut strategy
 
2992
     - Record the number of IN-equalities.
 
2993
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
 
2994
       see which of the expressions are bound and which are not (for those
 
2995
       we'll produce a distinct stream of (ie_i1,...ie_ik).
 
2996
 
 
2997
       (TODO: can we just create a list of pointers and hope the expressions
 
2998
       will not substitute themselves on fix_fields()? or we need to wrap
 
2999
       them into Item_direct_view_refs and store pointers to those. The
 
3000
       pointers to Item_direct_view_refs are guaranteed to be stable as 
 
3001
       Item_direct_view_refs doesn't substitute itself with anything in 
 
3002
       Item_direct_view_ref::fix_fields.
 
3003
  */
 
3004
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
 
3005
  sj_nest->nested_join->sj_outer_expr_list.empty();
 
3006
 
 
3007
  if (subq_pred->left_expr->cols() == 1)
 
3008
  {
 
3009
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
 
3010
 
 
3011
    Item *item_eq= new Item_func_eq(subq_pred->left_expr, 
 
3012
                                    subq_lex->ref_pointer_array[0]);
 
3013
    item_eq->name= (char*)subq_sj_cond_name;
 
3014
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3015
  }
 
3016
  else
 
3017
  {
 
3018
    for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
 
3019
    {
 
3020
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
 
3021
                                                element_index(i));
 
3022
      Item *item_eq= 
 
3023
        new Item_func_eq(subq_pred->left_expr->element_index(i), 
 
3024
                         subq_lex->ref_pointer_array[i]);
 
3025
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
 
3026
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3027
    }
 
3028
  }
 
3029
  /* Fix the created equality and AND */
 
3030
  sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
 
3031
 
 
3032
  /*
 
3033
    Walk through sj nest's WHERE and ON expressions and call
 
3034
    item->fix_table_changes() for all items.
 
3035
  */
 
3036
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
 
3037
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
 
3038
 
 
3039
 
 
3040
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
 
3041
  subq_lex->master_unit()->exclude_level();
 
3042
 
 
3043
  /* Inject sj_on_expr into the parent's WHERE or ON */
 
3044
  if (emb_tbl_nest)
 
3045
  {
 
3046
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr, 
 
3047
                                     sj_nest->sj_on_expr);
 
3048
    emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
 
3049
  }
 
3050
  else
 
3051
  {
 
3052
    /* Inject into the WHERE */
 
3053
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
 
3054
    parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
 
3055
    parent_join->select_lex->where= parent_join->conds;
 
3056
  }
 
3057
 
 
3058
  return(false);
 
3059
}
 
3060
 
 
3061
 
 
3062
/*
 
3063
  Convert candidate subquery predicates to semi-joins
 
3064
 
 
3065
  SYNOPSIS
 
3066
    JOIN::flatten_subqueries()
 
3067
 
 
3068
  DESCRIPTION
 
3069
    Convert candidate subquery predicates to semi-joins.
 
3070
 
 
3071
  RETURN 
 
3072
    false  OK
 
3073
    true   Error
 
3074
*/
 
3075
 
 
3076
bool JOIN::flatten_subqueries()
 
3077
{
 
3078
  Item_in_subselect **in_subq;
 
3079
  Item_in_subselect **in_subq_end;
 
3080
 
 
3081
  if (sj_subselects.elements() == 0)
 
3082
    return(false);
 
3083
 
 
3084
  /* 1. Fix children subqueries */
 
3085
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); 
 
3086
       in_subq != in_subq_end; in_subq++)
 
3087
  {
 
3088
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3089
    child_join->outer_tables = child_join->tables;
 
3090
    if (child_join->flatten_subqueries())
 
3091
      return(true);
 
3092
    (*in_subq)->sj_convert_priority= 
 
3093
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
 
3094
  }
 
3095
 
 
3096
  //dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
 
3097
  /* 
 
3098
    2. Pick which subqueries to convert:
 
3099
      sort the subquery array
 
3100
      - prefer correlated subqueries over uncorrelated;
 
3101
      - prefer subqueries that have greater number of outer tables;
 
3102
  */
 
3103
  sj_subselects.sort(subq_sj_candidate_cmp);
 
3104
  // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
 
3105
  /* Replace all subqueries to be flattened with Item_int(1) */
 
3106
  for (in_subq= sj_subselects.front(); 
 
3107
       in_subq != in_subq_end && 
 
3108
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3109
       in_subq++)
 
3110
  {
 
3111
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
 
3112
      return(true);
 
3113
  }
 
3114
 
 
3115
  for (in_subq= sj_subselects.front(); 
 
3116
       in_subq != in_subq_end && 
 
3117
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3118
       in_subq++)
 
3119
  {
 
3120
    if (convert_subq_to_sj(this, *in_subq))
 
3121
      return(true);
 
3122
  }
 
3123
 
 
3124
  /* 3. Finalize those we didn't convert */
 
3125
  for (; in_subq!= in_subq_end; in_subq++)
 
3126
  {
 
3127
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3128
    Item_subselect::trans_res res;
 
3129
    (*in_subq)->changed= 0;
 
3130
    (*in_subq)->fixed= 0;
 
3131
    res= (*in_subq)->select_transformer(child_join);
 
3132
    if (res == Item_subselect::RES_ERROR)
 
3133
      return(true);
 
3134
 
 
3135
    (*in_subq)->changed= 1;
 
3136
    (*in_subq)->fixed= 1;
 
3137
 
 
3138
    Item *substitute= (*in_subq)->substitution;
 
3139
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
 
3140
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
 
3141
      return(true);
 
3142
 
 
3143
    //if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
 
3144
    //  return(true);
 
3145
  }
 
3146
  sj_subselects.clear();
 
3147
  return(false);
 
3148
}
 
3149
 
 
3150
 
 
3151
/**
 
3152
  Setup for execution all subqueries of a query, for which the optimizer
 
3153
  chose hash semi-join.
 
3154
 
 
3155
  @details Iterate over all subqueries of the query, and if they are under an
 
3156
  IN predicate, and the optimizer chose to compute it via hash semi-join:
 
3157
  - try to initialize all data structures needed for the materialized execution
 
3158
    of the IN predicate,
 
3159
  - if this fails, then perform the IN=>EXISTS transformation which was
 
3160
    previously blocked during JOIN::prepare.
 
3161
 
 
3162
  This method is part of the "code generation" query processing phase.
 
3163
 
 
3164
  This phase must be called after substitute_for_best_equal_field() because
 
3165
  that function may replace items with other items from a multiple equality,
 
3166
  and we need to reference the correct items in the index access method of the
 
3167
  IN predicate.
 
3168
 
 
3169
  @return Operation status
 
3170
  @retval false     success.
 
3171
  @retval true      error occurred.
 
3172
*/
 
3173
 
 
3174
bool JOIN::setup_subquery_materialization()
 
3175
{
 
3176
  for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
 
3177
       un= un->next_unit())
 
3178
  {
 
3179
    for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
 
3180
    {
 
3181
      Item_subselect *subquery_predicate= sl->master_unit()->item;
 
3182
      if (subquery_predicate &&
 
3183
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
 
3184
      {
 
3185
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
 
3186
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
 
3187
            in_subs->setup_engine())
 
3188
          return true;
 
3189
      }
 
3190
    }
 
3191
  }
 
3192
  return false;
 
3193
}
 
3194
 
 
3195
 
 
3196
/*
 
3197
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3198
 
 
3199
  SYNOPSIS
 
3200
    find_eq_ref_candidate()
 
3201
      table             Table to be checked
 
3202
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
 
3203
                        count.
 
3204
 
 
3205
  DESCRIPTION
 
3206
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3207
 
 
3208
  TODO
 
3209
    Check again if it is feasible to factor common parts with constant table
 
3210
    search
 
3211
 
 
3212
  RETURN
 
3213
    true  - There exists an eq_ref(outer-tables) candidate
 
3214
    false - Otherwise
 
3215
*/
 
3216
 
 
3217
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
 
3218
{
 
3219
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
 
3220
  uint key;
 
3221
 
 
3222
  if (keyuse)
 
3223
  {
 
3224
    while (1) /* For each key */
 
3225
    {
 
3226
      key= keyuse->key;
 
3227
      KEY *keyinfo= table->key_info + key;
 
3228
      key_part_map bound_parts= 0;
 
3229
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
 
3230
      {
 
3231
        do  /* For all equalities on all key parts */
 
3232
        {
 
3233
          /* Check if this is "t.keypart = expr(outer_tables) */
 
3234
          if (!(keyuse->used_tables & sj_inner_tables) &&
 
3235
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
 
3236
          {
 
3237
            bound_parts |= 1 << keyuse->keypart;
 
3238
          }
 
3239
          keyuse++;
 
3240
        } while (keyuse->key == key && keyuse->table == table);
 
3241
 
 
3242
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
 
3243
          return true;
 
3244
        if (keyuse->table != table)
 
3245
          return false;
 
3246
      }
 
3247
      else
 
3248
      {
 
3249
        do
 
3250
        {
 
3251
          keyuse++;
 
3252
          if (keyuse->table != table)
 
3253
            return false;
 
3254
        }
 
3255
        while (keyuse->key == key);
 
3256
      }
 
3257
    }
 
3258
  }
 
3259
  return false;
 
3260
}
 
3261
 
 
3262
 
 
3263
/*
 
3264
  Pull tables out of semi-join nests, if possible
 
3265
 
 
3266
  SYNOPSIS
 
3267
    pull_out_semijoin_tables()
 
3268
      join  The join where to do the semi-join flattening
 
3269
 
 
3270
  DESCRIPTION
 
3271
    Try to pull tables out of semi-join nests.
 
3272
     
 
3273
    PRECONDITIONS
 
3274
    When this function is called, the join may have several semi-join nests
 
3275
    (possibly within different semi-join nests), but it is guaranteed that
 
3276
    one semi-join nest does not contain another.
 
3277
   
 
3278
    ACTION
 
3279
    A table can be pulled out of the semi-join nest if
 
3280
     - It is a constant table
 
3281
     - It is accessed 
 
3282
 
 
3283
    POSTCONDITIONS
 
3284
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
 
3285
       tables)
 
3286
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
 
3287
     * Semi-join nests TABLE_LIST::sj_inner_tables
 
3288
 
 
3289
    This operation is (and should be) performed at each PS execution since
 
3290
    tables may become/cease to be constant across PS reexecutions.
 
3291
 
 
3292
  RETURN 
 
3293
    0 - OK
 
3294
    1 - Out of memory error
 
3295
*/
 
3296
 
 
3297
int pull_out_semijoin_tables(JOIN *join)
 
3298
{
 
3299
  TABLE_LIST *sj_nest;
 
3300
  List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
 
3301
   
 
3302
  /* Try pulling out of the each of the semi-joins */
 
3303
  while ((sj_nest= sj_list_it++))
 
3304
  {
 
3305
    /* Action #1: Mark the constant tables to be pulled out */
 
3306
    table_map pulled_tables= 0;
 
3307
     
 
3308
    List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
 
3309
    TABLE_LIST *tbl;
 
3310
    while ((tbl= child_li++))
 
3311
    {
 
3312
      if (tbl->table)
 
3313
      {
 
3314
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
 
3315
        if (tbl->table->map & join->const_table_map)
 
3316
        {
 
3317
          pulled_tables |= tbl->table->map;
 
3318
        }
 
3319
      }
 
3320
    }
 
3321
    
 
3322
    /*
 
3323
      Action #2: Find which tables we can pull out based on
 
3324
      update_ref_and_keys() data. Note that pulling one table out can allow
 
3325
      us to pull out some other tables too.
 
3326
    */
 
3327
    bool pulled_a_table;
 
3328
    do 
 
3329
    {
 
3330
      pulled_a_table= false;
 
3331
      child_li.rewind();
 
3332
      while ((tbl= child_li++))
 
3333
      {
 
3334
        if (tbl->table && !(pulled_tables & tbl->table->map))
 
3335
        {
 
3336
          if (find_eq_ref_candidate(tbl->table, 
 
3337
                                    sj_nest->nested_join->used_tables & 
 
3338
                                    ~pulled_tables))
 
3339
          {
 
3340
            pulled_a_table= true;
 
3341
            pulled_tables |= tbl->table->map;
 
3342
          }
 
3343
        }
 
3344
      }
 
3345
    } while (pulled_a_table);
 
3346
 
 
3347
    child_li.rewind();
 
3348
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
 
3349
    {
 
3350
      (sj_nest)->sj_inner_tables= 0;
 
3351
      while ((tbl= child_li++))
 
3352
      {
 
3353
        if (tbl->table)
 
3354
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3355
      }
 
3356
    }
 
3357
    else
 
3358
    {
 
3359
      /* Record the bitmap of inner tables, mark the inner tables */
 
3360
      table_map inner_tables=(sj_nest)->nested_join->used_tables & 
 
3361
                             ~pulled_tables;
 
3362
      (sj_nest)->sj_inner_tables= inner_tables;
 
3363
      while ((tbl= child_li++))
 
3364
      {
 
3365
        if (tbl->table)
 
3366
        {
 
3367
          if (inner_tables & tbl->table->map)
 
3368
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
 
3369
          else
 
3370
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3371
        }
 
3372
      }
 
3373
    }
 
3374
  }
 
3375
  return(0);
 
3376
}
 
3377
 
460
3378
/*****************************************************************************
461
 
  Create JoinTableS, make a guess about the table types,
 
3379
  Create JOIN_TABS, make a guess about the table types,
462
3380
  Approximate how many records will be used in each table
463
3381
*****************************************************************************/
464
 
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
 
3382
 
 
3383
 
 
3384
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
 
3385
                                      TABLE *table,
 
3386
                                      const key_map *keys,ha_rows limit)
465
3387
{
466
3388
  int error;
467
 
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
 
3389
  if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
468
3390
    return(0);                           // Fatal error flag is set
469
3391
  if (select)
470
3392
  {
471
3393
    select->head=table;
472
3394
    table->reginfo.impossible_range=0;
473
 
    if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
 
3395
    if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
474
3396
                                          limit, 0, false)) == 1)
475
3397
      return(select->quick->records);
476
3398
    if (error == -1)
482
3404
  return(HA_POS_ERROR);                 /* This shouldn't happend */
483
3405
}
484
3406
 
 
3407
/*
 
3408
   This structure is used to collect info on potentially sargable
 
3409
   predicates in order to check whether they become sargable after
 
3410
   reading const tables.
 
3411
   We form a bitmap of indexes that can be used for sargable predicates.
 
3412
   Only such indexes are involved in range analysis.
 
3413
*/
 
3414
typedef struct st_sargable_param
 
3415
{
 
3416
  Field *field;              /* field against which to check sargability */
 
3417
  Item **arg_value;          /* values of potential keys for lookups     */
 
3418
  uint num_values;           /* number of values in the above array      */
 
3419
} SARGABLE_PARAM;  
 
3420
 
 
3421
/**
 
3422
  Calculate the best possible join and initialize the join structure.
 
3423
 
 
3424
  @retval
 
3425
    0   ok
 
3426
  @retval
 
3427
    1   Fatal error
 
3428
*/
 
3429
 
 
3430
static bool
 
3431
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
 
3432
                     DYNAMIC_ARRAY *keyuse_array)
 
3433
{
 
3434
  int error;
 
3435
  TABLE *table;
 
3436
  uint i,table_count,const_count,key;
 
3437
  table_map found_const_table_map, all_table_map, found_ref, refs;
 
3438
  key_map const_ref, eq_part;
 
3439
  TABLE **table_vector;
 
3440
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
 
3441
  KEYUSE *keyuse,*start_keyuse;
 
3442
  table_map outer_join=0;
 
3443
  SARGABLE_PARAM *sargables= 0;
 
3444
  JOIN_TAB *stat_vector[MAX_TABLES+1];
 
3445
 
 
3446
  table_count=join->tables;
 
3447
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
 
3448
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
 
3449
  table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
 
3450
  if (!stat || !stat_ref || !table_vector)
 
3451
    return(1);                          // Eom /* purecov: inspected */
 
3452
 
 
3453
  join->best_ref=stat_vector;
 
3454
 
 
3455
  stat_end=stat+table_count;
 
3456
  found_const_table_map= all_table_map=0;
 
3457
  const_count=0;
 
3458
 
 
3459
  for (s= stat, i= 0;
 
3460
       tables;
 
3461
       s++, tables= tables->next_leaf, i++)
 
3462
  {
 
3463
    TABLE_LIST *embedding= tables->embedding;
 
3464
    stat_vector[i]=s;
 
3465
    s->keys.init();
 
3466
    s->const_keys.init();
 
3467
    s->checked_keys.init();
 
3468
    s->needed_reg.init();
 
3469
    table_vector[i]=s->table=table=tables->table;
 
3470
    table->pos_in_table_list= tables;
 
3471
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
3472
    if(error)
 
3473
    {
 
3474
        table->file->print_error(error, MYF(0));
 
3475
        return(1);
 
3476
    }
 
3477
    table->quick_keys.clear_all();
 
3478
    table->reginfo.join_tab=s;
 
3479
    table->reginfo.not_exists_optimize=0;
 
3480
    memset((char*) table->const_key_parts, 0,
 
3481
           sizeof(key_part_map)*table->s->keys);
 
3482
    all_table_map|= table->map;
 
3483
    s->join=join;
 
3484
    s->info=0;                                  // For describe
 
3485
 
 
3486
    s->dependent= tables->dep_tables;
 
3487
    s->key_dependent= 0;
 
3488
    if (tables->schema_table)
 
3489
      table->file->stats.records= 2;
 
3490
    table->quick_condition_rows= table->file->stats.records;
 
3491
 
 
3492
    s->on_expr_ref= &tables->on_expr;
 
3493
    if (*s->on_expr_ref)
 
3494
    {
 
3495
      /* s is the only inner table of an outer join */
 
3496
      if (!table->file->stats.records && !embedding)
 
3497
      {                                         // Empty table
 
3498
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
 
3499
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3500
        continue;
 
3501
      }
 
3502
      outer_join|= table->map;
 
3503
      s->embedding_map= 0;
 
3504
      for (;embedding; embedding= embedding->embedding)
 
3505
        s->embedding_map|= embedding->nested_join->nj_map;
 
3506
      continue;
 
3507
    }
 
3508
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
 
3509
    {
 
3510
      /* s belongs to a nested join, maybe to several embedded joins */
 
3511
      s->embedding_map= 0;
 
3512
      do
 
3513
      {
 
3514
        NESTED_JOIN *nested_join= embedding->nested_join;
 
3515
        s->embedding_map|=nested_join->nj_map;
 
3516
        s->dependent|= embedding->dep_tables;
 
3517
        embedding= embedding->embedding;
 
3518
        outer_join|= nested_join->used_tables;
 
3519
      }
 
3520
      while (embedding);
 
3521
      continue;
 
3522
    }
 
3523
    if ((table->s->system || table->file->stats.records <= 1) &&
 
3524
        !s->dependent &&
 
3525
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
 
3526
    {
 
3527
      set_position(join,const_count++,s,(KEYUSE*) 0);
 
3528
    }
 
3529
  }
 
3530
  stat_vector[i]=0;
 
3531
  join->outer_join=outer_join;
 
3532
 
 
3533
  if (join->outer_join)
 
3534
  {
 
3535
    /* 
 
3536
       Build transitive closure for relation 'to be dependent on'.
 
3537
       This will speed up the plan search for many cases with outer joins,
 
3538
       as well as allow us to catch illegal cross references/
 
3539
       Warshall's algorithm is used to build the transitive closure.
 
3540
       As we use bitmaps to represent the relation the complexity
 
3541
       of the algorithm is O((number of tables)^2). 
 
3542
    */
 
3543
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3544
    {
 
3545
      for (uint j= 0 ; j < table_count ; j++)
 
3546
      {
 
3547
        table= stat[j].table;
 
3548
        if (s->dependent & table->map)
 
3549
          s->dependent |= table->reginfo.join_tab->dependent;
 
3550
      }
 
3551
      if (s->dependent)
 
3552
        s->table->maybe_null= 1;
 
3553
    }
 
3554
    /* Catch illegal cross references for outer joins */
 
3555
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3556
    {
 
3557
      if (s->dependent & s->table->map)
 
3558
      {
 
3559
        join->tables=0;                 // Don't use join->table
 
3560
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
 
3561
        return(1);
 
3562
      }
 
3563
      s->key_dependent= s->dependent;
 
3564
    }
 
3565
  }
 
3566
 
 
3567
  if (conds || outer_join)
 
3568
    if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
 
3569
                            conds, join->cond_equal,
 
3570
                            ~outer_join, join->select_lex, &sargables))
 
3571
      return(1);
 
3572
 
 
3573
  /* Read tables with 0 or 1 rows (system tables) */
 
3574
  join->const_table_map= 0;
 
3575
 
 
3576
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
 
3577
       p_pos < p_end ;
 
3578
       p_pos++)
 
3579
  {
 
3580
    int tmp;
 
3581
    s= p_pos->table;
 
3582
    s->type=JT_SYSTEM;
 
3583
    join->const_table_map|=s->table->map;
 
3584
    if ((tmp=join_read_const_table(s, p_pos)))
 
3585
    {
 
3586
      if (tmp > 0)
 
3587
        return(1);                      // Fatal error
 
3588
    }
 
3589
    else
 
3590
      found_const_table_map|= s->table->map;
 
3591
  }
 
3592
 
 
3593
  /* loop until no more const tables are found */
 
3594
  int ref_changed;
 
3595
  do
 
3596
  {
 
3597
  more_const_tables_found:
 
3598
    ref_changed = 0;
 
3599
    found_ref=0;
 
3600
 
 
3601
    /*
 
3602
      We only have to loop from stat_vector + const_count as
 
3603
      set_position() will move all const_tables first in stat_vector
 
3604
    */
 
3605
 
 
3606
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
 
3607
    {
 
3608
      table=s->table;
 
3609
 
 
3610
      /* 
 
3611
        If equi-join condition by a key is null rejecting and after a
 
3612
        substitution of a const table the key value happens to be null
 
3613
        then we can state that there are no matches for this equi-join.
 
3614
      */  
 
3615
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
 
3616
      {
 
3617
        /* 
 
3618
          When performing an outer join operation if there are no matching rows
 
3619
          for the single row of the outer table all the inner tables are to be
 
3620
          null complemented and thus considered as constant tables.
 
3621
          Here we apply this consideration to the case of outer join operations 
 
3622
          with a single inner table only because the case with nested tables
 
3623
          would require a more thorough analysis.
 
3624
          TODO. Apply single row substitution to null complemented inner tables
 
3625
          for nested outer join operations. 
 
3626
        */              
 
3627
        while (keyuse->table == table)
 
3628
        {
 
3629
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
 
3630
              keyuse->val->is_null() && keyuse->null_rejecting)
 
3631
          {
 
3632
            s->type= JT_CONST;
 
3633
            mark_as_null_row(table);
 
3634
            found_const_table_map|= table->map;
 
3635
            join->const_table_map|= table->map;
 
3636
            set_position(join,const_count++,s,(KEYUSE*) 0);
 
3637
            goto more_const_tables_found;
 
3638
           }
 
3639
          keyuse++;
 
3640
        }
 
3641
      }
 
3642
 
 
3643
      if (s->dependent)                         // If dependent on some table
 
3644
      {
 
3645
        // All dep. must be constants
 
3646
        if (s->dependent & ~(found_const_table_map))
 
3647
          continue;
 
3648
        if (table->file->stats.records <= 1L &&
 
3649
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3650
            !table->pos_in_table_list->embedding)
 
3651
        {                                       // system table
 
3652
          int tmp= 0;
 
3653
          s->type=JT_SYSTEM;
 
3654
          join->const_table_map|=table->map;
 
3655
          set_position(join,const_count++,s,(KEYUSE*) 0);
 
3656
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
 
3657
          {
 
3658
            if (tmp > 0)
 
3659
              return(1);                        // Fatal error
 
3660
          }
 
3661
          else
 
3662
            found_const_table_map|= table->map;
 
3663
          continue;
 
3664
        }
 
3665
      }
 
3666
      /* check if table can be read by key or table only uses const refs */
 
3667
      if ((keyuse=s->keyuse))
 
3668
      {
 
3669
        s->type= JT_REF;
 
3670
        while (keyuse->table == table)
 
3671
        {
 
3672
          start_keyuse=keyuse;
 
3673
          key=keyuse->key;
 
3674
          s->keys.set_bit(key);               // QQ: remove this ?
 
3675
 
 
3676
          refs=0;
 
3677
          const_ref.clear_all();
 
3678
          eq_part.clear_all();
 
3679
          do
 
3680
          {
 
3681
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
 
3682
            {
 
3683
              if (!((~found_const_table_map) & keyuse->used_tables))
 
3684
                const_ref.set_bit(keyuse->keypart);
 
3685
              else
 
3686
                refs|=keyuse->used_tables;
 
3687
              eq_part.set_bit(keyuse->keypart);
 
3688
            }
 
3689
            keyuse++;
 
3690
          } while (keyuse->table == table && keyuse->key == key);
 
3691
 
 
3692
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3693
              !table->pos_in_table_list->embedding)
 
3694
          {
 
3695
            if ((table->key_info[key].flags & (HA_NOSAME))
 
3696
                 == HA_NOSAME)
 
3697
            {
 
3698
              if (const_ref == eq_part)
 
3699
              {                                 // Found everything for ref.
 
3700
                int tmp;
 
3701
                ref_changed = 1;
 
3702
                s->type= JT_CONST;
 
3703
                join->const_table_map|=table->map;
 
3704
                set_position(join,const_count++,s,start_keyuse);
 
3705
                if (create_ref_for_key(join, s, start_keyuse,
 
3706
                                       found_const_table_map))
 
3707
                  return(1);
 
3708
                if ((tmp=join_read_const_table(s,
 
3709
                                               join->positions+const_count-1)))
 
3710
                {
 
3711
                  if (tmp > 0)
 
3712
                    return(1);                  // Fatal error
 
3713
                }
 
3714
                else
 
3715
                  found_const_table_map|= table->map;
 
3716
                break;
 
3717
              }
 
3718
              else
 
3719
                found_ref|= refs;      // Table is const if all refs are const
 
3720
            }
 
3721
            else if (const_ref == eq_part)
 
3722
              s->const_keys.set_bit(key);
 
3723
          }
 
3724
        }
 
3725
      }
 
3726
    }
 
3727
  } while (join->const_table_map & found_ref && ref_changed);
 
3728
 
 
3729
  /* 
 
3730
    Update info on indexes that can be used for search lookups as
 
3731
    reading const tables may has added new sargable predicates. 
 
3732
  */
 
3733
  if (const_count && sargables)
 
3734
  {
 
3735
    for( ; sargables->field ; sargables++)
 
3736
    {
 
3737
      Field *field= sargables->field;
 
3738
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
 
3739
      key_map possible_keys= field->key_start;
 
3740
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
3741
      bool is_const= 1;
 
3742
      for (uint j=0; j < sargables->num_values; j++)
 
3743
        is_const&= sargables->arg_value[j]->const_item();
 
3744
      if (is_const)
 
3745
        join_tab[0].const_keys.merge(possible_keys);
 
3746
    }
 
3747
  }
 
3748
 
 
3749
  if (pull_out_semijoin_tables(join))
 
3750
    return(true);
 
3751
 
 
3752
  /* Calc how many (possible) matched records in each table */
 
3753
 
 
3754
  for (s=stat ; s < stat_end ; s++)
 
3755
  {
 
3756
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
 
3757
    {
 
3758
      /* Only one matching row */
 
3759
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
 
3760
      continue;
 
3761
    }
 
3762
    /* Approximate found rows and time to read them */
 
3763
    s->found_records=s->records=s->table->file->stats.records;
 
3764
    s->read_time=(ha_rows) s->table->file->scan_time();
 
3765
 
 
3766
    /*
 
3767
      Set a max range of how many seeks we can expect when using keys
 
3768
      This is can't be to high as otherwise we are likely to use
 
3769
      table scan.
 
3770
    */
 
3771
    s->worst_seeks= min((double) s->found_records / 10,
 
3772
                        (double) s->read_time*3);
 
3773
    if (s->worst_seeks < 2.0)                   // Fix for small tables
 
3774
      s->worst_seeks=2.0;
 
3775
 
 
3776
    /*
 
3777
      Add to stat->const_keys those indexes for which all group fields or
 
3778
      all select distinct fields participate in one index.
 
3779
    */
 
3780
    add_group_and_distinct_keys(join, s);
 
3781
 
 
3782
    if (!s->const_keys.is_clear_all() &&
 
3783
        !s->table->pos_in_table_list->embedding)
 
3784
    {
 
3785
      ha_rows records;
 
3786
      SQL_SELECT *select;
 
3787
      select= make_select(s->table, found_const_table_map,
 
3788
                          found_const_table_map,
 
3789
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
 
3790
                          1, &error);
 
3791
      if (!select)
 
3792
        return(1);
 
3793
      records= get_quick_record_count(join->thd, select, s->table,
 
3794
                                      &s->const_keys, join->row_limit);
 
3795
      s->quick=select->quick;
 
3796
      s->needed_reg=select->needed_reg;
 
3797
      select->quick=0;
 
3798
      if (records == 0 && s->table->reginfo.impossible_range)
 
3799
      {
 
3800
        /*
 
3801
          Impossible WHERE or ON expression
 
3802
          In case of ON, we mark that the we match one empty NULL row.
 
3803
          In case of WHERE, don't set found_const_table_map to get the
 
3804
          caller to abort with a zero row result.
 
3805
        */
 
3806
        join->const_table_map|= s->table->map;
 
3807
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3808
        s->type= JT_CONST;
 
3809
        if (*s->on_expr_ref)
 
3810
        {
 
3811
          /* Generate empty row */
 
3812
          s->info= "Impossible ON condition";
 
3813
          found_const_table_map|= s->table->map;
 
3814
          s->type= JT_CONST;
 
3815
          mark_as_null_row(s->table);           // All fields are NULL
 
3816
        }
 
3817
      }
 
3818
      if (records != HA_POS_ERROR)
 
3819
      {
 
3820
        s->found_records=records;
 
3821
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
3822
      }
 
3823
      delete select;
 
3824
    }
 
3825
  }
 
3826
 
 
3827
  join->join_tab=stat;
 
3828
  join->map2table=stat_ref;
 
3829
  join->table= join->all_tables=table_vector;
 
3830
  join->const_tables=const_count;
 
3831
  join->found_const_table_map=found_const_table_map;
 
3832
 
 
3833
  /* Find an optimal join order of the non-constant tables. */
 
3834
  if (join->const_tables != join->tables)
 
3835
  {
 
3836
    optimize_keyuse(join, keyuse_array);
 
3837
    if (choose_plan(join, all_table_map & ~join->const_table_map))
 
3838
      return(true);
 
3839
  }
 
3840
  else
 
3841
  {
 
3842
    memcpy((uchar*) join->best_positions,(uchar*) join->positions,
 
3843
           sizeof(POSITION)*join->const_tables);
 
3844
    join->best_read=1.0;
 
3845
  }
 
3846
  /* Generate an execution plan from the found optimal join order. */
 
3847
  return(join->thd->killed || get_best_combination(join));
 
3848
}
 
3849
 
 
3850
 
485
3851
/*****************************************************************************
486
3852
  Check with keys are used and with tables references with tables
487
3853
  Updates in stat:
490
3856
          keyuse     Pointer to possible keys
491
3857
*****************************************************************************/
492
3858
 
 
3859
/// Used when finding key fields
 
3860
typedef struct key_field_t {
 
3861
  Field         *field;
 
3862
  Item          *val;                   ///< May be empty if diff constant
 
3863
  uint          level;
 
3864
  uint          optimize; // KEY_OPTIMIZE_*
 
3865
  bool          eq_func;
 
3866
  /**
 
3867
    If true, the condition this struct represents will not be satisfied
 
3868
    when val IS NULL.
 
3869
  */
 
3870
  bool          null_rejecting; 
 
3871
  bool          *cond_guard; /* See KEYUSE::cond_guard */
 
3872
  uint          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3873
} KEY_FIELD;
 
3874
 
 
3875
/**
 
3876
  Merge new key definitions to old ones, remove those not used in both.
 
3877
 
 
3878
  This is called for OR between different levels.
 
3879
 
 
3880
  To be able to do 'ref_or_null' we merge a comparison of a column
 
3881
  and 'column IS NULL' to one test.  This is useful for sub select queries
 
3882
  that are internally transformed to something like:.
 
3883
 
 
3884
  @code
 
3885
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL 
 
3886
  @endcode
 
3887
 
 
3888
  KEY_FIELD::null_rejecting is processed as follows: @n
 
3889
  result has null_rejecting=true if it is set for both ORed references.
 
3890
  for example:
 
3891
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
 
3892
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
 
3893
 
 
3894
  @todo
 
3895
    The result of this is that we're missing some 'ref' accesses.
 
3896
    OptimizerTeam: Fix this
 
3897
*/
 
3898
 
 
3899
static KEY_FIELD *
 
3900
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
 
3901
                 uint and_level)
 
3902
{
 
3903
  if (start == new_fields)
 
3904
    return start;                               // Impossible or
 
3905
  if (new_fields == end)
 
3906
    return start;                               // No new fields, skip all
 
3907
 
 
3908
  KEY_FIELD *first_free=new_fields;
 
3909
 
 
3910
  /* Mark all found fields in old array */
 
3911
  for (; new_fields != end ; new_fields++)
 
3912
  {
 
3913
    for (KEY_FIELD *old=start ; old != first_free ; old++)
 
3914
    {
 
3915
      if (old->field == new_fields->field)
 
3916
      {
 
3917
        /*
 
3918
          NOTE: below const_item() call really works as "!used_tables()", i.e.
 
3919
          it can return false where it is feasible to make it return true.
 
3920
          
 
3921
          The cause is as follows: Some of the tables are already known to be
 
3922
          const tables (the detection code is in make_join_statistics(),
 
3923
          above the update_ref_and_keys() call), but we didn't propagate 
 
3924
          information about this: TABLE::const_table is not set to true, and
 
3925
          Item::update_used_tables() hasn't been called for each item.
 
3926
          The result of this is that we're missing some 'ref' accesses.
 
3927
          TODO: OptimizerTeam: Fix this
 
3928
        */
 
3929
        if (!new_fields->val->const_item())
 
3930
        {
 
3931
          /*
 
3932
            If the value matches, we can use the key reference.
 
3933
            If not, we keep it until we have examined all new values
 
3934
          */
 
3935
          if (old->val->eq(new_fields->val, old->field->binary()))
 
3936
          {
 
3937
            old->level= and_level;
 
3938
            old->optimize= ((old->optimize & new_fields->optimize &
 
3939
                             KEY_OPTIMIZE_EXISTS) |
 
3940
                            ((old->optimize | new_fields->optimize) &
 
3941
                             KEY_OPTIMIZE_REF_OR_NULL));
 
3942
            old->null_rejecting= (old->null_rejecting &&
 
3943
                                  new_fields->null_rejecting);
 
3944
          }
 
3945
        }
 
3946
        else if (old->eq_func && new_fields->eq_func &&
 
3947
                 old->val->eq_by_collation(new_fields->val, 
 
3948
                                           old->field->binary(),
 
3949
                                           old->field->charset()))
 
3950
 
 
3951
        {
 
3952
          old->level= and_level;
 
3953
          old->optimize= ((old->optimize & new_fields->optimize &
 
3954
                           KEY_OPTIMIZE_EXISTS) |
 
3955
                          ((old->optimize | new_fields->optimize) &
 
3956
                           KEY_OPTIMIZE_REF_OR_NULL));
 
3957
          old->null_rejecting= (old->null_rejecting &&
 
3958
                                new_fields->null_rejecting);
 
3959
        }
 
3960
        else if (old->eq_func && new_fields->eq_func &&
 
3961
                 ((old->val->const_item() && old->val->is_null()) || 
 
3962
                  new_fields->val->is_null()))
 
3963
        {
 
3964
          /* field = expression OR field IS NULL */
 
3965
          old->level= and_level;
 
3966
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
 
3967
          /*
 
3968
            Remember the NOT NULL value unless the value does not depend
 
3969
            on other tables.
 
3970
          */
 
3971
          if (!old->val->used_tables() && old->val->is_null())
 
3972
            old->val= new_fields->val;
 
3973
          /* The referred expression can be NULL: */ 
 
3974
          old->null_rejecting= 0;
 
3975
        }
 
3976
        else
 
3977
        {
 
3978
          /*
 
3979
            We are comparing two different const.  In this case we can't
 
3980
            use a key-lookup on this so it's better to remove the value
 
3981
            and let the range optimzier handle it
 
3982
          */
 
3983
          if (old == --first_free)              // If last item
 
3984
            break;
 
3985
          *old= *first_free;                    // Remove old value
 
3986
          old--;                                // Retry this value
 
3987
        }
 
3988
      }
 
3989
    }
 
3990
  }
 
3991
  /* Remove all not used items */
 
3992
  for (KEY_FIELD *old=start ; old != first_free ;)
 
3993
  {
 
3994
    if (old->level != and_level)
 
3995
    {                                           // Not used in all levels
 
3996
      if (old == --first_free)
 
3997
        break;
 
3998
      *old= *first_free;                        // Remove old value
 
3999
      continue;
 
4000
    }
 
4001
    old++;
 
4002
  }
 
4003
  return first_free;
 
4004
}
 
4005
 
 
4006
 
 
4007
/**
 
4008
  Add a possible key to array of possible keys if it's usable as a key
 
4009
 
 
4010
    @param key_fields      Pointer to add key, if usable
 
4011
    @param and_level       And level, to be stored in KEY_FIELD
 
4012
    @param cond            Condition predicate
 
4013
    @param field           Field used in comparision
 
4014
    @param eq_func         True if we used =, <=> or IS NULL
 
4015
    @param value           Value used for comparison with field
 
4016
    @param usable_tables   Tables which can be used for key optimization
 
4017
    @param sargables       IN/OUT Array of found sargable candidates
 
4018
 
 
4019
  @note
 
4020
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
 
4021
    table, we store this to be able to do not exists optimization later.
 
4022
 
 
4023
  @returns
 
4024
    *key_fields is incremented if we stored a key in the array
 
4025
*/
 
4026
 
 
4027
static void
 
4028
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
 
4029
              Field *field, bool eq_func, Item **value, uint num_values,
 
4030
              table_map usable_tables, SARGABLE_PARAM **sargables)
 
4031
{
 
4032
  uint exists_optimize= 0;
 
4033
  if (!(field->flags & PART_KEY_FLAG))
 
4034
  {
 
4035
    // Don't remove column IS NULL on a LEFT JOIN table
 
4036
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4037
        !field->table->maybe_null || field->null_ptr)
 
4038
      return;                                   // Not a key. Skip it
 
4039
    exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4040
    assert(num_values == 1);
 
4041
  }
 
4042
  else
 
4043
  {
 
4044
    table_map used_tables=0;
 
4045
    bool optimizable=0;
 
4046
    for (uint i=0; i<num_values; i++)
 
4047
    {
 
4048
      used_tables|=(value[i])->used_tables();
 
4049
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
 
4050
        optimizable=1;
 
4051
    }
 
4052
    if (!optimizable)
 
4053
      return;
 
4054
    if (!(usable_tables & field->table->map))
 
4055
    {
 
4056
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4057
          !field->table->maybe_null || field->null_ptr)
 
4058
        return;                                 // Can't use left join optimize
 
4059
      exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4060
    }
 
4061
    else
 
4062
    {
 
4063
      JOIN_TAB *stat=field->table->reginfo.join_tab;
 
4064
      key_map possible_keys=field->key_start;
 
4065
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
4066
      stat[0].keys.merge(possible_keys);             // Add possible keys
 
4067
 
 
4068
      /*
 
4069
        Save the following cases:
 
4070
        Field op constant
 
4071
        Field LIKE constant where constant doesn't start with a wildcard
 
4072
        Field = field2 where field2 is in a different table
 
4073
        Field op formula
 
4074
        Field IS NULL
 
4075
        Field IS NOT NULL
 
4076
         Field BETWEEN ...
 
4077
         Field IN ...
 
4078
      */
 
4079
      stat[0].key_dependent|=used_tables;
 
4080
 
 
4081
      bool is_const=1;
 
4082
      for (uint i=0; i<num_values; i++)
 
4083
      {
 
4084
        if (!(is_const&= value[i]->const_item()))
 
4085
          break;
 
4086
      }
 
4087
      if (is_const)
 
4088
        stat[0].const_keys.merge(possible_keys);
 
4089
      else if (!eq_func)
 
4090
      {
 
4091
        /* 
 
4092
          Save info to be able check whether this predicate can be 
 
4093
          considered as sargable for range analisis after reading const tables.
 
4094
          We do not save info about equalities as update_const_equal_items
 
4095
          will take care of updating info on keys from sargable equalities. 
 
4096
        */
 
4097
        (*sargables)--;
 
4098
        (*sargables)->field= field;
 
4099
        (*sargables)->arg_value= value;
 
4100
        (*sargables)->num_values= num_values;
 
4101
      }
 
4102
      /*
 
4103
        We can't always use indexes when comparing a string index to a
 
4104
        number. cmp_type() is checked to allow compare of dates to numbers.
 
4105
        eq_func is NEVER true when num_values > 1
 
4106
       */
 
4107
      if (!eq_func)
 
4108
      {
 
4109
        /* 
 
4110
          Additional optimization: if we're processing
 
4111
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
 
4112
          "t.key = c1".
 
4113
          TODO: This is a very limited fix. A more generic fix is possible. 
 
4114
          There are 2 options:
 
4115
          A) Make equality propagation code be able to handle BETWEEN
 
4116
             (including cases like t1.key BETWEEN t2.key AND t3.key)
 
4117
          B) Make range optimizer to infer additional "t.key = c" equalities
 
4118
             and use them in equality propagation process (see details in
 
4119
             OptimizerKBAndTodo)
 
4120
        */
 
4121
        if ((cond->functype() != Item_func::BETWEEN) ||
 
4122
            ((Item_func_between*) cond)->negated ||
 
4123
            !value[0]->eq(value[1], field->binary()))
 
4124
          return;
 
4125
        eq_func= true;
 
4126
      }
 
4127
 
 
4128
      if (field->result_type() == STRING_RESULT)
 
4129
      {
 
4130
        if ((*value)->result_type() != STRING_RESULT)
 
4131
        {
 
4132
          if (field->cmp_type() != (*value)->result_type())
 
4133
            return;
 
4134
        }
 
4135
        else
 
4136
        {
 
4137
          /*
 
4138
            We can't use indexes if the effective collation
 
4139
            of the operation differ from the field collation.
 
4140
          */
 
4141
          if (field->cmp_type() == STRING_RESULT &&
 
4142
              ((Field_str*)field)->charset() != cond->compare_collation())
 
4143
            return;
 
4144
        }
 
4145
      }
 
4146
    }
 
4147
  }
 
4148
  /*
 
4149
    For the moment eq_func is always true. This slot is reserved for future
 
4150
    extensions where we want to remembers other things than just eq comparisons
 
4151
  */
 
4152
  assert(eq_func);
 
4153
  /* Store possible eq field */
 
4154
  (*key_fields)->field=         field;
 
4155
  (*key_fields)->eq_func=       eq_func;
 
4156
  (*key_fields)->val=           *value;
 
4157
  (*key_fields)->level=         and_level;
 
4158
  (*key_fields)->optimize=      exists_optimize;
 
4159
  /*
 
4160
    If the condition has form "tbl.keypart = othertbl.field" and 
 
4161
    othertbl.field can be NULL, there will be no matches if othertbl.field 
 
4162
    has NULL value.
 
4163
    We use null_rejecting in add_not_null_conds() to add
 
4164
    'othertbl.field IS NOT NULL' to tab->select_cond.
 
4165
  */
 
4166
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
 
4167
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
 
4168
                                  ((*value)->type() == Item::FIELD_ITEM) &&
 
4169
                                  ((Item_field*)*value)->field->maybe_null());
 
4170
  (*key_fields)->cond_guard= NULL;
 
4171
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name && 
 
4172
                              cond->name < subq_sj_cond_name + 64)? 
 
4173
                              cond->name - subq_sj_cond_name: UINT_MAX;
 
4174
  (*key_fields)++;
 
4175
}
 
4176
 
 
4177
/**
 
4178
  Add possible keys to array of possible keys originated from a simple
 
4179
  predicate.
 
4180
 
 
4181
    @param  key_fields     Pointer to add key, if usable
 
4182
    @param  and_level      And level, to be stored in KEY_FIELD
 
4183
    @param  cond           Condition predicate
 
4184
    @param  field          Field used in comparision
 
4185
    @param  eq_func        True if we used =, <=> or IS NULL
 
4186
    @param  value          Value used for comparison with field
 
4187
                           Is NULL for BETWEEN and IN    
 
4188
    @param  usable_tables  Tables which can be used for key optimization
 
4189
    @param  sargables      IN/OUT Array of found sargable candidates
 
4190
 
 
4191
  @note
 
4192
    If field items f1 and f2 belong to the same multiple equality and
 
4193
    a key is added for f1, the the same key is added for f2.
 
4194
 
 
4195
  @returns
 
4196
    *key_fields is incremented if we stored a key in the array
 
4197
*/
 
4198
 
 
4199
static void
 
4200
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
 
4201
                     Item_func *cond, Item_field *field_item,
 
4202
                     bool eq_func, Item **val,
 
4203
                     uint num_values, table_map usable_tables,
 
4204
                     SARGABLE_PARAM **sargables)
 
4205
{
 
4206
  Field *field= field_item->field;
 
4207
  add_key_field(key_fields, and_level, cond, field,
 
4208
                eq_func, val, num_values, usable_tables, sargables);
 
4209
  Item_equal *item_equal= field_item->item_equal;
 
4210
  if (item_equal)
 
4211
  { 
 
4212
    /*
 
4213
      Add to the set of possible key values every substitution of
 
4214
      the field for an equal field included into item_equal
 
4215
    */
 
4216
    Item_equal_iterator it(*item_equal);
 
4217
    Item_field *item;
 
4218
    while ((item= it++))
 
4219
    {
 
4220
      if (!field->eq(item->field))
 
4221
      {
 
4222
        add_key_field(key_fields, and_level, cond, item->field,
 
4223
                      eq_func, val, num_values, usable_tables,
 
4224
                      sargables);
 
4225
      }
 
4226
    }
 
4227
  }
 
4228
}
 
4229
 
 
4230
static void
 
4231
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
 
4232
               COND *cond, table_map usable_tables,
 
4233
               SARGABLE_PARAM **sargables)
 
4234
{
 
4235
  if (cond->type() == Item_func::COND_ITEM)
 
4236
  {
 
4237
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
 
4238
    KEY_FIELD *org_key_fields= *key_fields;
 
4239
 
 
4240
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
4241
    {
 
4242
      Item *item;
 
4243
      while ((item=li++))
 
4244
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4245
                       sargables);
 
4246
      for (; org_key_fields != *key_fields ; org_key_fields++)
 
4247
        org_key_fields->level= *and_level;
 
4248
    }
 
4249
    else
 
4250
    {
 
4251
      (*and_level)++;
 
4252
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
 
4253
                     sargables);
 
4254
      Item *item;
 
4255
      while ((item=li++))
 
4256
      {
 
4257
        KEY_FIELD *start_key_fields= *key_fields;
 
4258
        (*and_level)++;
 
4259
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4260
                       sargables);
 
4261
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
 
4262
                                     *key_fields,++(*and_level));
 
4263
      }
 
4264
    }
 
4265
    return;
 
4266
  }
 
4267
 
 
4268
  /* 
 
4269
    Subquery optimization: Conditions that are pushed down into subqueries
 
4270
    are wrapped into Item_func_trig_cond. We process the wrapped condition
 
4271
    but need to set cond_guard for KEYUSE elements generated from it.
 
4272
  */
 
4273
  {
 
4274
    if (cond->type() == Item::FUNC_ITEM &&
 
4275
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
 
4276
    {
 
4277
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
 
4278
      if (!join->group_list && !join->order &&
 
4279
          join->unit->item && 
 
4280
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
 
4281
          !join->unit->is_union())
 
4282
      {
 
4283
        KEY_FIELD *save= *key_fields;
 
4284
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
 
4285
                       sargables);
 
4286
        // Indicate that this ref access candidate is for subquery lookup:
 
4287
        for (; save != *key_fields; save++)
 
4288
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
 
4289
      }
 
4290
      return;
 
4291
    }
 
4292
  }
 
4293
 
 
4294
  /* If item is of type 'field op field/constant' add it to key_fields */
 
4295
  if (cond->type() != Item::FUNC_ITEM)
 
4296
    return;
 
4297
  Item_func *cond_func= (Item_func*) cond;
 
4298
  switch (cond_func->select_optimize()) {
 
4299
  case Item_func::OPTIMIZE_NONE:
 
4300
    break;
 
4301
  case Item_func::OPTIMIZE_KEY:
 
4302
  {
 
4303
    Item **values;
 
4304
    // BETWEEN, IN, NE
 
4305
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
 
4306
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4307
    {
 
4308
      values= cond_func->arguments()+1;
 
4309
      if (cond_func->functype() == Item_func::NE_FUNC &&
 
4310
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4311
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4312
        values--;
 
4313
      assert(cond_func->functype() != Item_func::IN_FUNC ||
 
4314
                  cond_func->argument_count() != 2);
 
4315
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4316
                           (Item_field*) (cond_func->key_item()->real_item()),
 
4317
                           0, values, 
 
4318
                           cond_func->argument_count()-1,
 
4319
                           usable_tables, sargables);
 
4320
    }
 
4321
    if (cond_func->functype() == Item_func::BETWEEN)
 
4322
    {
 
4323
      values= cond_func->arguments();
 
4324
      for (uint i= 1 ; i < cond_func->argument_count() ; i++)
 
4325
      {
 
4326
        Item_field *field_item;
 
4327
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
 
4328
            &&
 
4329
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
 
4330
        {
 
4331
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
 
4332
          add_key_equal_fields(key_fields, *and_level, cond_func,
 
4333
                               field_item, 0, values, 1, usable_tables, 
 
4334
                               sargables);
 
4335
        }
 
4336
      }  
 
4337
    }
 
4338
    break;
 
4339
  }
 
4340
  case Item_func::OPTIMIZE_OP:
 
4341
  {
 
4342
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
 
4343
                     cond_func->functype() == Item_func::EQUAL_FUNC);
 
4344
 
 
4345
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4346
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4347
    {
 
4348
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4349
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4350
                           equal_func,
 
4351
                           cond_func->arguments()+1, 1, usable_tables,
 
4352
                           sargables);
 
4353
    }
 
4354
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4355
        cond_func->functype() != Item_func::LIKE_FUNC &&
 
4356
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
 
4357
    {
 
4358
      add_key_equal_fields(key_fields, *and_level, cond_func, 
 
4359
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
 
4360
                           equal_func,
 
4361
                           cond_func->arguments(),1,usable_tables,
 
4362
                           sargables);
 
4363
    }
 
4364
    break;
 
4365
  }
 
4366
  case Item_func::OPTIMIZE_NULL:
 
4367
    /* column_name IS [NOT] NULL */
 
4368
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4369
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4370
    {
 
4371
      Item *tmp=new Item_null;
 
4372
      if (unlikely(!tmp))                       // Should never be true
 
4373
        return;
 
4374
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4375
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4376
                    cond_func->functype() == Item_func::ISNULL_FUNC,
 
4377
                           &tmp, 1, usable_tables, sargables);
 
4378
    }
 
4379
    break;
 
4380
  case Item_func::OPTIMIZE_EQUAL:
 
4381
    Item_equal *item_equal= (Item_equal *) cond;
 
4382
    Item *const_item= item_equal->get_const();
 
4383
    Item_equal_iterator it(*item_equal);
 
4384
    Item_field *item;
 
4385
    if (const_item)
 
4386
    {
 
4387
      /*
 
4388
        For each field field1 from item_equal consider the equality 
 
4389
        field1=const_item as a condition allowing an index access of the table
 
4390
        with field1 by the keys value of field1.
 
4391
      */   
 
4392
      while ((item= it++))
 
4393
      {
 
4394
        add_key_field(key_fields, *and_level, cond_func, item->field,
 
4395
                      true, &const_item, 1, usable_tables, sargables);
 
4396
      }
 
4397
    }
 
4398
    else 
 
4399
    {
 
4400
      /*
 
4401
        Consider all pairs of different fields included into item_equal.
 
4402
        For each of them (field1, field1) consider the equality 
 
4403
        field1=field2 as a condition allowing an index access of the table
 
4404
        with field1 by the keys value of field2.
 
4405
      */   
 
4406
      Item_equal_iterator fi(*item_equal);
 
4407
      while ((item= fi++))
 
4408
      {
 
4409
        Field *field= item->field;
 
4410
        while ((item= it++))
 
4411
        {
 
4412
          if (!field->eq(item->field))
 
4413
          {
 
4414
            add_key_field(key_fields, *and_level, cond_func, field,
 
4415
                          true, (Item **) &item, 1, usable_tables,
 
4416
                          sargables);
 
4417
          }
 
4418
        }
 
4419
        it.rewind();
 
4420
      }
 
4421
    }
 
4422
    break;
 
4423
  }
 
4424
}
493
4425
 
494
4426
/**
495
4427
  Add all keys with uses 'field' for some keypart.
496
4428
 
497
4429
  If field->and_level != and_level then only mark key_part as const_part.
498
4430
*/
499
 
uint32_t max_part_bit(key_part_map bits)
 
4431
 
 
4432
static uint
 
4433
max_part_bit(key_part_map bits)
500
4434
{
501
 
  uint32_t found;
 
4435
  uint found;
502
4436
  for (found=0; bits & 1 ; found++,bits>>=1) ;
503
4437
  return found;
504
4438
}
505
4439
 
506
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
 
4440
static void
 
4441
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
 
4442
{
 
4443
  Field *field=key_field->field;
 
4444
  TABLE *form= field->table;
 
4445
  KEYUSE keyuse;
 
4446
 
 
4447
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
 
4448
  {
 
4449
    for (uint key=0 ; key < form->s->keys ; key++)
 
4450
    {
 
4451
      if (!(form->keys_in_use_for_query.is_set(key)))
 
4452
        continue;
 
4453
 
 
4454
      uint key_parts= (uint) form->key_info[key].key_parts;
 
4455
      for (uint part=0 ; part <  key_parts ; part++)
 
4456
      {
 
4457
        if (field->eq(form->key_info[key].key_part[part].field))
 
4458
        {
 
4459
          keyuse.table= field->table;
 
4460
          keyuse.val =  key_field->val;
 
4461
          keyuse.key =  key;
 
4462
          keyuse.keypart=part;
 
4463
          keyuse.keypart_map= (key_part_map) 1 << part;
 
4464
          keyuse.used_tables=key_field->val->used_tables();
 
4465
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
 
4466
          keyuse.null_rejecting= key_field->null_rejecting;
 
4467
          keyuse.cond_guard= key_field->cond_guard;
 
4468
          keyuse.sj_pred_no= key_field->sj_pred_no;
 
4469
          VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
 
4470
        }
 
4471
      }
 
4472
    }
 
4473
  }
 
4474
}
 
4475
 
 
4476
static int
 
4477
sort_keyuse(KEYUSE *a,KEYUSE *b)
507
4478
{
508
4479
  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()));
 
4480
  if (a->table->tablenr != b->table->tablenr)
 
4481
    return (int) (a->table->tablenr - b->table->tablenr);
 
4482
  if (a->key != b->key)
 
4483
    return (int) (a->key - b->key);
 
4484
  if (a->keypart != b->keypart)
 
4485
    return (int) (a->keypart - b->keypart);
515
4486
  // Place const values before other ones
516
 
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
517
 
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
 
4487
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
 
4488
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
518
4489
    return res;
519
4490
  /* 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)));
 
4491
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
 
4492
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
 
4493
}
 
4494
 
 
4495
 
 
4496
/*
 
4497
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
 
4498
 
 
4499
    This function populates KEY_FIELD array with entries generated from the 
 
4500
    ON condition of the given nested join, and does the same for nested joins 
 
4501
    contained within this nested join.
 
4502
 
 
4503
  @param[in]      nested_join_table   Nested join pseudo-table to process
 
4504
  @param[in,out]  end                 End of the key field array
 
4505
  @param[in,out]  and_level           And-level
 
4506
  @param[in,out]  sargables           Array of found sargable candidates
 
4507
 
 
4508
 
 
4509
  @note
 
4510
    We can add accesses to the tables that are direct children of this nested 
 
4511
    join (1), and are not inner tables w.r.t their neighbours (2).
 
4512
    
 
4513
    Example for #1 (outer brackets pair denotes nested join this function is 
 
4514
    invoked for):
 
4515
    @code
 
4516
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
 
4517
    @endcode
 
4518
    Example for #2:
 
4519
    @code
 
4520
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
 
4521
    @endcode
 
4522
    In examples 1-2 for condition cond, we can add 'ref' access candidates to 
 
4523
    t1 only.
 
4524
    Example #3:
 
4525
    @code
 
4526
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
 
4527
    @endcode
 
4528
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
 
4529
*/
 
4530
 
 
4531
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
 
4532
                                  KEY_FIELD **end, uint *and_level,
 
4533
                                  SARGABLE_PARAM **sargables)
 
4534
{
 
4535
  List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
 
4536
  List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
 
4537
  bool have_another = false;
 
4538
  table_map tables= 0;
 
4539
  TABLE_LIST *table;
 
4540
  assert(nested_join_table->nested_join);
 
4541
 
 
4542
  while ((table= li++) || (have_another && (li=li2, have_another=false,
 
4543
                                            (table= li++))))
 
4544
  {
 
4545
    if (table->nested_join)
 
4546
    {
 
4547
      if (!table->on_expr)
 
4548
      {
 
4549
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
 
4550
        have_another= true;
 
4551
        li2= li;
 
4552
        li= List_iterator<TABLE_LIST>(table->nested_join->join_list); 
 
4553
      }
 
4554
      else
 
4555
        add_key_fields_for_nj(join, table, end, and_level, sargables);
 
4556
    }
 
4557
    else
 
4558
      if (!table->on_expr)
 
4559
        tables |= table->table->map;
 
4560
  }
 
4561
  if (nested_join_table->on_expr)
 
4562
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
 
4563
                   sargables);
522
4564
}
523
4565
 
524
4566
 
525
4567
/**
526
4568
  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
 
4569
  
 
4570
  @param       thd 
 
4571
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
530
4572
  @param       join_tab       Array in tablenr_order
531
4573
  @param       tables         Number of tables in join
532
4574
  @param       cond           WHERE condition (note that the function analyzes
535
4577
                              for which we can make ref access based the WHERE
536
4578
                              clause)
537
4579
  @param       select_lex     current SELECT
538
 
  @param[out]  sargables      std::vector of found sargable candidates
539
 
 
 
4580
  @param[out]  sargables      Array of found sargable candidates
 
4581
      
540
4582
   @retval
541
4583
     0  OK
542
4584
   @retval
543
4585
     1  Out of memory.
544
4586
*/
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)
 
4587
 
 
4588
static bool
 
4589
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
 
4590
                    uint tables, COND *cond,
 
4591
                    COND_EQUAL *cond_equal __attribute__((unused)),
 
4592
                    table_map normal_tables, SELECT_LEX *select_lex,
 
4593
                    SARGABLE_PARAM **sargables)
554
4594
{
555
 
  uint  and_level,found_eq_constant;
556
 
  optimizer::KeyField *key_fields, *end, *field;
557
 
  uint32_t sz;
558
 
  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
559
 
 
560
 
  /*
561
 
    All predicates that are used to fill arrays of KeyField
562
 
    and SargableParam classes have at most 2 arguments
563
 
    except BETWEEN predicates that have 3 arguments and
 
4595
  uint  and_level,i,found_eq_constant;
 
4596
  KEY_FIELD *key_fields, *end, *field;
 
4597
  uint sz;
 
4598
  uint m= max(select_lex->max_equal_elems,1);
 
4599
  
 
4600
  /* 
 
4601
    We use the same piece of memory to store both  KEY_FIELD 
 
4602
    and SARGABLE_PARAM structure.
 
4603
    KEY_FIELD values are placed at the beginning this memory
 
4604
    while  SARGABLE_PARAM values are put at the end.
 
4605
    All predicates that are used to fill arrays of KEY_FIELD
 
4606
    and SARGABLE_PARAM structures have at most 2 arguments
 
4607
    except BETWEEN predicates that have 3 arguments and 
564
4608
    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
 
4609
    This any predicate if it's not BETWEEN/IN can be used 
 
4610
    directly to fill at most 2 array elements, either of KEY_FIELD
 
4611
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
568
4612
    can be filled as this predicate is considered as
569
4613
    saragable with respect to each of its argument.
570
4614
    An IN predicate can require at most 1 element as currently
571
4615
    it is considered as sargable only for its first argument.
572
4616
    Multiple equality can add  elements that are filled after
573
4617
    substitution of field arguments by equal fields. There
574
 
    can be not more than select_lex->max_equal_elems such
 
4618
    can be not more than select_lex->max_equal_elems such 
575
4619
    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;
 
4620
  */ 
 
4621
  sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4622
      (((thd->lex->current_select->cond_count+1)*2 +
 
4623
        thd->lex->current_select->between_count)*m+1);
 
4624
  if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
 
4625
    return true; /* purecov: inspected */
582
4626
  and_level= 0;
583
4627
  field= end= key_fields;
 
4628
  *sargables= (SARGABLE_PARAM *) key_fields + 
 
4629
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
 
4630
  /* set a barrier for the array of SARGABLE_PARAM */
 
4631
  (*sargables)[0].field= 0; 
584
4632
 
585
 
  if (my_init_dynamic_array(keyuse, sizeof(optimizer::KeyUse), 20, 64))
 
4633
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
586
4634
    return true;
587
4635
  if (cond)
588
4636
  {
589
4637
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
590
4638
                   sargables);
591
 
    for (; field != end; field++)
 
4639
    for (; field != end ; field++)
592
4640
    {
593
 
      add_key_part(keyuse, field);
 
4641
      add_key_part(keyuse,field);
594
4642
      /* 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
 
      }
 
4643
      if (field->val->type() == Item::NULL_ITEM &&
 
4644
          !field->field->real_maybe_null())
 
4645
        field->field->table->reginfo.not_exists_optimize=1;
600
4646
    }
601
4647
  }
602
 
  for (uint32_t i= 0; i < tables; i++)
 
4648
  for (i=0 ; i < tables ; i++)
603
4649
  {
604
4650
    /*
605
4651
      Block the creation of keys for inner tables of outer joins.
609
4655
      In the future when we introduce conditional accesses
610
4656
      for inner tables in outer joins these keys will be taken
611
4657
      into account as well.
612
 
    */
 
4658
    */ 
613
4659
    if (*join_tab[i].on_expr_ref)
614
 
      add_key_fields(join_tab->join, &end, &and_level,
 
4660
      add_key_fields(join_tab->join, &end, &and_level, 
615
4661
                     *join_tab[i].on_expr_ref,
616
4662
                     join_tab[i].table->map, sargables);
617
4663
  }
618
4664
 
619
4665
  /* Process ON conditions for the nested joins */
620
4666
  {
621
 
    List<TableList>::iterator li(join_tab->join->join_list->begin());
622
 
    TableList *table;
 
4667
    List_iterator<TABLE_LIST> li(*join_tab->join->join_list);
 
4668
    TABLE_LIST *table;
623
4669
    while ((table= li++))
624
4670
    {
625
 
      if (table->getNestedJoin())
626
 
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
 
4671
      if (table->nested_join)
 
4672
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level, 
627
4673
                              sargables);
628
4674
    }
629
4675
  }
643
4689
  */
644
4690
  if (keyuse->elements)
645
4691
  {
646
 
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
647
 
 
648
 
    internal::my_qsort(keyuse->buffer,keyuse->elements,sizeof(optimizer::KeyUse),
649
 
                       (qsort_cmp) sort_keyuse);
650
 
 
651
 
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
652
 
    insert_dynamic(keyuse,(unsigned char*) &key_end);
653
 
 
654
 
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
 
4692
    KEYUSE key_end,*prev,*save_pos,*use;
 
4693
 
 
4694
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
 
4695
          (qsort_cmp) sort_keyuse);
 
4696
 
 
4697
    memset((char*) &key_end, 0, sizeof(key_end)); /* Add for easy testing */
 
4698
    VOID(insert_dynamic(keyuse,(uchar*) &key_end));
 
4699
 
 
4700
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
655
4701
    prev= &key_end;
656
 
    found_eq_constant= 0;
 
4702
    found_eq_constant=0;
 
4703
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
657
4704
    {
658
 
      uint32_t i;
659
 
 
660
 
      for (i= 0; i < keyuse->elements-1; i++, use++)
 
4705
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
 
4706
        use->table->const_key_parts[use->key]|= use->keypart_map;
661
4707
      {
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;
 
4708
        if (use->key == prev->key && use->table == prev->table)
 
4709
        {
 
4710
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
 
4711
            continue;                           /* remove */
 
4712
        }
 
4713
        else if (use->keypart != 0)             // First found must be 0
 
4714
          continue;
 
4715
      }
672
4716
 
673
 
#ifdef HAVE_VALGRIND
674
 
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
675
 
        if (save_pos != use)
 
4717
#ifdef HAVE_purify
 
4718
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
 
4719
      if (save_pos != use)
676
4720
#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;
 
4721
        *save_pos= *use;
 
4722
      prev=use;
 
4723
      found_eq_constant= !use->used_tables;
 
4724
      /* Save ptr to first use */
 
4725
      if (!use->table->reginfo.join_tab->keyuse)
 
4726
        use->table->reginfo.join_tab->keyuse=save_pos;
 
4727
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
 
4728
      save_pos++;
689
4729
    }
 
4730
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
 
4731
    VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
 
4732
    keyuse->elements=i;
690
4733
  }
691
4734
  return false;
692
4735
}
694
4737
/**
695
4738
  Update some values in keyuse for faster choose_plan() loop.
696
4739
*/
697
 
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
 
4740
 
 
4741
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
698
4742
{
699
 
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
700
 
                                                  0, 
701
 
                                                  optimizer::KeyUse*);
 
4743
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
702
4744
 
703
4745
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
704
4746
  {
711
4753
      Constant tables are ignored.
712
4754
      To avoid bad matches, we don't make ref_table_rows less than 100.
713
4755
    */
714
 
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
715
 
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
 
4756
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
 
4757
    if (keyuse->used_tables &
 
4758
        (map= (keyuse->used_tables & ~join->const_table_map &
 
4759
               ~OUTER_REF_TABLE_BIT)))
716
4760
    {
717
 
      uint32_t tablenr;
 
4761
      uint tablenr;
718
4762
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
719
4763
      if (map == 1)                     // Only one table
720
4764
      {
721
 
        Table *tmp_table=join->all_tables[tablenr];
722
 
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
 
4765
        TABLE *tmp_table=join->all_tables[tablenr];
 
4766
        keyuse->ref_table_rows= max(tmp_table->file->stats.records, 100);
723
4767
      }
724
4768
    }
725
4769
    /*
726
4770
      Outer reference (external field) is constant for single executing
727
4771
      of subquery
728
4772
    */
729
 
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
730
 
      keyuse->setTableRows(1);
 
4773
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
 
4774
      keyuse->ref_table_rows= 1;
731
4775
  }
732
4776
}
733
4777
 
749
4793
  @return
750
4794
    None
751
4795
*/
752
 
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
 
4796
 
 
4797
static void
 
4798
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
753
4799
{
754
4800
  List<Item_field> indexed_fields;
755
 
  List<Item_field>::iterator indexed_fields_it(indexed_fields.begin());
756
 
  Order      *cur_group;
 
4801
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
 
4802
  ORDER      *cur_group;
757
4803
  Item_field *cur_item;
758
4804
  key_map possible_keys(0);
759
4805
 
761
4807
  { /* Collect all query fields referenced in the GROUP clause. */
762
4808
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
763
4809
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
764
 
                               (unsigned char*) &indexed_fields);
 
4810
                               (uchar*) &indexed_fields);
765
4811
  }
766
4812
  else if (join->select_distinct)
767
4813
  { /* Collect all query fields referenced in the SELECT clause. */
768
4814
    List<Item> &select_items= join->fields_list;
769
 
    List<Item>::iterator select_items_it(select_items.begin());
 
4815
    List_iterator<Item> select_items_it(select_items);
770
4816
    Item *item;
771
4817
    while ((item= select_items_it++))
772
4818
      item->walk(&Item::collect_item_field_processor, 0,
773
 
                 (unsigned char*) &indexed_fields);
 
4819
                 (uchar*) &indexed_fields);
774
4820
  }
775
4821
  else
776
4822
    return;
780
4826
 
781
4827
  /* Intersect the keys of all group fields. */
782
4828
  cur_item= indexed_fields_it++;
783
 
  possible_keys|= cur_item->field->part_of_key;
 
4829
  possible_keys.merge(cur_item->field->part_of_key);
784
4830
  while ((cur_item= indexed_fields_it++))
785
4831
  {
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
 
4832
    possible_keys.intersect(cur_item->field->part_of_key);
 
4833
  }
 
4834
 
 
4835
  if (!possible_keys.is_clear_all())
 
4836
    join_tab->const_keys.merge(possible_keys);
 
4837
}
 
4838
 
 
4839
 
 
4840
/*****************************************************************************
 
4841
  Go through all combinations of not marked tables and find the one
 
4842
  which uses least records
 
4843
*****************************************************************************/
 
4844
 
 
4845
/** Save const tables first as used tables. */
 
4846
 
 
4847
static void
 
4848
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
 
4849
{
 
4850
  join->positions[idx].table= table;
 
4851
  join->positions[idx].key=key;
 
4852
  join->positions[idx].records_read=1.0;        /* This is a const table */
 
4853
  join->positions[idx].ref_depend_map= 0;
 
4854
 
 
4855
  /* Move the const table as down as possible in best_ref */
 
4856
  JOIN_TAB **pos=join->best_ref+idx+1;
 
4857
  JOIN_TAB *next=join->best_ref[idx];
 
4858
  for (;next != table ; pos++)
 
4859
  {
 
4860
    JOIN_TAB *tmp=pos[0];
 
4861
    pos[0]=next;
 
4862
    next=tmp;
 
4863
  }
 
4864
  join->best_ref[idx]=table;
 
4865
}
 
4866
 
 
4867
 
 
4868
/*
 
4869
  Given a semi-join nest, find out which of the IN-equalities are bound
 
4870
 
 
4871
  SYNOPSIS
 
4872
    get_bound_sj_equalities()
 
4873
      sj_nest           Semi-join nest
 
4874
      remaining_tables  Tables that are not yet bound
 
4875
 
 
4876
  DESCRIPTION
 
4877
    Given a semi-join nest, find out which of the IN-equalities have their
 
4878
    left part expression bound (i.e. the said expression doesn't refer to
 
4879
    any of remaining_tables and can be evaluated).
 
4880
 
 
4881
  RETURN
 
4882
    Bitmap of bound IN-equalities.
 
4883
*/
 
4884
 
 
4885
uint64_t get_bound_sj_equalities(TABLE_LIST *sj_nest, 
 
4886
                                  table_map remaining_tables)
 
4887
{
 
4888
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
 
4889
  Item *item;
 
4890
  uint i= 0;
 
4891
  uint64_t res= 0;
 
4892
  while ((item= li++))
 
4893
  {
 
4894
    /*
 
4895
      Q: should this take into account equality propagation and how?
 
4896
      A: If e->outer_side is an Item_field, walk over the equality
 
4897
         class and see if there is an element that is bound?
 
4898
      (this is an optional feature)
 
4899
    */
 
4900
    if (!(item->used_tables() & remaining_tables))
 
4901
    {
 
4902
      res |= 1ULL < i;
 
4903
    }
 
4904
  }
 
4905
  return res;
 
4906
}
 
4907
 
 
4908
 
 
4909
/**
 
4910
  Find the best access path for an extension of a partial execution
 
4911
  plan and add this path to the plan.
 
4912
 
 
4913
  The function finds the best access path to table 's' from the passed
 
4914
  partial plan where an access path is the general term for any means to
 
4915
  access the data in 's'. An access path may use either an index or a scan,
 
4916
  whichever is cheaper. The input partial plan is passed via the array
 
4917
  'join->positions' of length 'idx'. The chosen access method for 's' and its
 
4918
  cost are stored in 'join->positions[idx]'.
 
4919
 
 
4920
  @param join             pointer to the structure providing all context info
 
4921
                          for the query
 
4922
  @param s                the table to be joined by the function
 
4923
  @param thd              thread for the connection that submitted the query
 
4924
  @param remaining_tables set of tables not included into the partial plan yet
 
4925
  @param idx              the length of the partial plan
 
4926
  @param record_count     estimate for the number of records returned by the
 
4927
                          partial plan
 
4928
  @param read_time        the cost of the partial plan
 
4929
 
 
4930
  @return
 
4931
    None
 
4932
*/
 
4933
 
 
4934
static void
 
4935
best_access_path(JOIN      *join,
 
4936
                 JOIN_TAB  *s,
 
4937
                 THD       *thd,
 
4938
                 table_map remaining_tables,
 
4939
                 uint      idx,
 
4940
                 double    record_count,
 
4941
                 double    read_time __attribute__((unused)))
 
4942
{
 
4943
  KEYUSE *best_key=         0;
 
4944
  uint best_max_key_part=   0;
 
4945
  bool found_constraint= 0;
 
4946
  double best=              DBL_MAX;
 
4947
  double best_time=         DBL_MAX;
 
4948
  double records=           DBL_MAX;
 
4949
  table_map best_ref_depends_map= 0;
 
4950
  double tmp;
 
4951
  ha_rows rec;
 
4952
  uint best_is_sj_inside_out=    0;
 
4953
 
 
4954
  if (s->keyuse)
 
4955
  {                                            /* Use key if possible */
 
4956
    TABLE *table= s->table;
 
4957
    KEYUSE *keyuse,*start_key=0;
 
4958
    double best_records= DBL_MAX;
 
4959
    uint max_key_part=0;
 
4960
    uint64_t bound_sj_equalities= 0;
 
4961
    bool try_sj_inside_out= false;
 
4962
    /*
 
4963
      Discover the bound equalites. We need to do this, if
 
4964
        1. The next table is an SJ-inner table, and
 
4965
        2. It is the first table from that semijoin, and
 
4966
        3. We're not within a semi-join range (i.e. all semi-joins either have
 
4967
           all or none of their tables in join_table_map), except
 
4968
           s->emb_sj_nest (which we've just entered).
 
4969
        3. All correlation references from this sj-nest are bound
 
4970
    */
 
4971
    if (s->emb_sj_nest &&                                                 // (1)
 
4972
        s->emb_sj_nest->sj_in_exprs < 64 && 
 
4973
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
 
4974
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
 
4975
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
 
4976
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
 
4977
    {
 
4978
      /* This table is an InsideOut scan candidate */
 
4979
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest, 
 
4980
                                                   remaining_tables);
 
4981
      try_sj_inside_out= true;
 
4982
    }
 
4983
 
 
4984
    /* Test how we can use keys */
 
4985
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
 
4986
    for (keyuse=s->keyuse ; keyuse->table == table ;)
 
4987
    {
 
4988
      key_part_map found_part= 0;
 
4989
      table_map found_ref= 0;
 
4990
      uint key= keyuse->key;
 
4991
      KEY *keyinfo= table->key_info+key;
 
4992
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
 
4993
      key_part_map const_part= 0;
 
4994
      /* The or-null keypart in ref-or-null access: */
 
4995
      key_part_map ref_or_null_part= 0;
 
4996
 
 
4997
      /* Calculate how many key segments of the current key we can use */
 
4998
      start_key= keyuse;
 
4999
      uint64_t handled_sj_equalities=0;
 
5000
      key_part_map sj_insideout_map= 0;
 
5001
 
 
5002
      do /* For each keypart */
 
5003
      {
 
5004
        uint keypart= keyuse->keypart;
 
5005
        table_map best_part_found_ref= 0;
 
5006
        double best_prev_record_reads= DBL_MAX;
 
5007
        
 
5008
        do /* For each way to access the keypart */
 
5009
        {
 
5010
 
 
5011
          /*
 
5012
            if 1. expression doesn't refer to forward tables
 
5013
               2. we won't get two ref-or-null's
 
5014
          */
 
5015
          if (!(remaining_tables & keyuse->used_tables) &&
 
5016
              !(ref_or_null_part && (keyuse->optimize &
 
5017
                                     KEY_OPTIMIZE_REF_OR_NULL)))
 
5018
          {
 
5019
            found_part|= keyuse->keypart_map;
 
5020
            if (!(keyuse->used_tables & ~join->const_table_map))
 
5021
              const_part|= keyuse->keypart_map;
 
5022
 
 
5023
            double tmp2= prev_record_reads(join, idx, (found_ref |
 
5024
                                                      keyuse->used_tables));
 
5025
            if (tmp2 < best_prev_record_reads)
 
5026
            {
 
5027
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
 
5028
              best_prev_record_reads= tmp2;
 
5029
            }
 
5030
            if (rec > keyuse->ref_table_rows)
 
5031
              rec= keyuse->ref_table_rows;
 
5032
            /*
 
5033
              If there is one 'key_column IS NULL' expression, we can
 
5034
              use this ref_or_null optimisation of this field
 
5035
            */
 
5036
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
 
5037
              ref_or_null_part |= keyuse->keypart_map;
 
5038
          }
 
5039
 
 
5040
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
 
5041
          {
 
5042
            if (!(remaining_tables & keyuse->used_tables))
 
5043
              bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
 
5044
            else
 
5045
            {
 
5046
              handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
 
5047
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
 
5048
            }
 
5049
          }
 
5050
 
 
5051
          keyuse++;
 
5052
        } while (keyuse->table == table && keyuse->key == key &&
 
5053
                 keyuse->keypart == keypart);
 
5054
        found_ref|= best_part_found_ref;
 
5055
      } while (keyuse->table == table && keyuse->key == key);
 
5056
 
 
5057
      /*
 
5058
        Assume that that each key matches a proportional part of table.
 
5059
      */
 
5060
      if (!found_part && !handled_sj_equalities)
 
5061
        continue;                               // Nothing usable found
 
5062
 
 
5063
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
 
5064
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
 
5065
 
 
5066
      bool sj_inside_out_scan= false;
 
5067
      {
 
5068
        found_constraint= 1;
 
5069
        /*
 
5070
          Check if InsideOut scan is applicable:
 
5071
          1. All IN-equalities are either "bound" or "handled"
 
5072
          2. Index keyparts are 
 
5073
             ...
 
5074
        */
 
5075
        if (try_sj_inside_out && 
 
5076
            table->covering_keys.is_set(key) &&
 
5077
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
 
5078
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
 
5079
        {
 
5080
          uint n_fixed_parts= max_part_bit(found_part);
 
5081
          if (n_fixed_parts != keyinfo->key_parts &&
 
5082
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
 
5083
               PREV_BITS(uint, keyinfo->key_parts))
 
5084
          {
 
5085
            /*
 
5086
              Not all parts are fixed. Produce bitmap of remaining bits and
 
5087
              check if all of them are covered.
 
5088
            */
 
5089
            sj_inside_out_scan= true;
 
5090
            if (!n_fixed_parts)
 
5091
            {
 
5092
              /*
 
5093
                It's a confluent ref scan.
 
5094
 
 
5095
                That is, all found KEYUSE elements refer to IN-equalities,
 
5096
                and there is really no ref access because there is no
 
5097
                  t.keypart0 = {bound expression}
 
5098
 
 
5099
                Calculate the cost of complete loose index scan.
 
5100
              */
 
5101
              records= (double)s->table->file->stats.records;
 
5102
 
 
5103
              /* The cost is entire index scan cost (divided by 2) */
 
5104
              best_time= s->table->file->index_only_read_time(key, records);
 
5105
 
 
5106
              /* Now figure how many different keys we will get */
 
5107
              ulong rpc;
 
5108
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5109
                records= records / rpc;
 
5110
              start_key= NULL;
 
5111
            }
 
5112
          }
 
5113
        }
 
5114
 
 
5115
        /*
 
5116
          Check if we found full key
 
5117
        */
 
5118
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
 
5119
            !ref_or_null_part)
 
5120
        {                                         /* use eq key */
 
5121
          max_key_part= (uint) ~0;
 
5122
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
 
5123
          {
 
5124
            tmp = prev_record_reads(join, idx, found_ref);
 
5125
            records=1.0;
 
5126
          }
 
5127
          else
 
5128
          {
 
5129
            if (!found_ref)
 
5130
            {                                     /* We found a const key */
 
5131
              /*
 
5132
                ReuseRangeEstimateForRef-1:
 
5133
                We get here if we've found a ref(const) (c_i are constants):
 
5134
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
 
5135
                
 
5136
                If range optimizer was able to construct a "range" 
 
5137
                access on this index, then its condition "quick_cond" was
 
5138
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
 
5139
                from the range optimizer.
 
5140
                
 
5141
                Proof of (*): By properties of range and ref optimizers 
 
5142
                quick_cond will be equal or tighther than ref_const_cond. 
 
5143
                ref_const_cond already covers "smallest" possible interval - 
 
5144
                a singlepoint interval over all keyparts. Therefore, 
 
5145
                quick_cond is equivalent to ref_const_cond (if it was an 
 
5146
                empty interval we wouldn't have got here).
 
5147
              */
 
5148
              if (table->quick_keys.is_set(key))
 
5149
                records= (double) table->quick_rows[key];
 
5150
              else
 
5151
              {
 
5152
                /* quick_range couldn't use key! */
 
5153
                records= (double) s->records/rec;
 
5154
              }
 
5155
            }
 
5156
            else
 
5157
            {
 
5158
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5159
              {                                   /* Prefer longer keys */
 
5160
                records=
 
5161
                  ((double) s->records / (double) rec *
 
5162
                   (1.0 +
 
5163
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
 
5164
                     (double) table->s->max_key_length)));
 
5165
                if (records < 2.0)
 
5166
                  records=2.0;               /* Can't be as good as a unique */
 
5167
              }
 
5168
              /*
 
5169
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
 
5170
                E(#rows) from range optimizer. Make another try:
 
5171
                
 
5172
                If range optimizer produced E(#rows) for a prefix of the ref
 
5173
                access we're considering, and that E(#rows) is lower then our
 
5174
                current estimate, make an adjustment. The criteria of when we
 
5175
                can make an adjustment is a special case of the criteria used
 
5176
                in ReuseRangeEstimateForRef-3.
 
5177
              */
 
5178
              if (table->quick_keys.is_set(key) &&
 
5179
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5180
                  table->quick_n_ranges[key] == 1 &&
 
5181
                  records > (double) table->quick_rows[key])
 
5182
              {
 
5183
                records= (double) table->quick_rows[key];
 
5184
              }
 
5185
            }
 
5186
            /* Limit the number of matched rows */
 
5187
            tmp= records;
 
5188
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5189
            if (table->covering_keys.is_set(key))
 
5190
            {
 
5191
              /* we can use only index tree */
 
5192
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5193
            }
 
5194
            else
 
5195
              tmp= record_count*min(tmp,s->worst_seeks);
 
5196
          }
 
5197
        }
 
5198
        else
 
5199
        {
 
5200
          /*
 
5201
            Use as much key-parts as possible and a uniq key is better
 
5202
            than a not unique key
 
5203
            Set tmp to (previous record count) * (records / combination)
 
5204
          */
 
5205
          if ((found_part & 1) &&
 
5206
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
 
5207
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
 
5208
          {
 
5209
            max_key_part= max_part_bit(found_part);
 
5210
            /*
 
5211
              ReuseRangeEstimateForRef-3:
 
5212
              We're now considering a ref[or_null] access via
 
5213
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR  
 
5214
              (same-as-above but with one cond replaced 
 
5215
               with "t.keypart_i IS NULL")]  (**)
 
5216
              
 
5217
              Try re-using E(#rows) from "range" optimizer:
 
5218
              We can do so if "range" optimizer used the same intervals as
 
5219
              in (**). The intervals used by range optimizer may be not 
 
5220
              available at this point (as "range" access might have choosen to
 
5221
              create quick select over another index), so we can't compare
 
5222
              them to (**). We'll make indirect judgements instead.
 
5223
              The sufficient conditions for re-use are:
 
5224
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
 
5225
                   this is not satisfied we have no way to know which ranges
 
5226
                   will be actually scanned by 'ref' until we execute the 
 
5227
                   join)
 
5228
              (C2) max #key parts in 'range' access == K == max_key_part (this
 
5229
                   is apparently a necessary requirement)
 
5230
 
 
5231
              We also have a property that "range optimizer produces equal or 
 
5232
              tighter set of scan intervals than ref(const) optimizer". Each
 
5233
              of the intervals in (**) are "tightest possible" intervals when 
 
5234
              one limits itself to using keyparts 1..K (which we do in #2).              
 
5235
              From here it follows that range access used either one, or
 
5236
              both of the (I1) and (I2) intervals:
 
5237
              
 
5238
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1) 
 
5239
               (same-as-above but with one cond replaced  
 
5240
                with "t.keypart_i IS NULL")               (I2)
 
5241
 
 
5242
              The remaining part is to exclude the situation where range
 
5243
              optimizer used one interval while we're considering
 
5244
              ref-or-null and looking for estimate for two intervals. This
 
5245
              is done by last limitation:
 
5246
 
 
5247
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
 
5248
            */
 
5249
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
 
5250
                table->quick_key_parts[key] == max_key_part &&          //(C2)
 
5251
                table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
 
5252
            {
 
5253
              tmp= records= (double) table->quick_rows[key];
 
5254
            }
 
5255
            else
 
5256
            {
 
5257
              /* Check if we have statistic about the distribution */
 
5258
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
 
5259
              {
 
5260
                /* 
 
5261
                  Fix for the case where the index statistics is too
 
5262
                  optimistic: If 
 
5263
                  (1) We're considering ref(const) and there is quick select
 
5264
                      on the same index, 
 
5265
                  (2) and that quick select uses more keyparts (i.e. it will
 
5266
                      scan equal/smaller interval then this ref(const))
 
5267
                  (3) and E(#rows) for quick select is higher then our
 
5268
                      estimate,
 
5269
                  Then 
 
5270
                    We'll use E(#rows) from quick select.
 
5271
 
 
5272
                  Q: Why do we choose to use 'ref'? Won't quick select be
 
5273
                  cheaper in some cases ?
 
5274
                  TODO: figure this out and adjust the plan choice if needed.
 
5275
                */
 
5276
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
 
5277
                    table->quick_key_parts[key] > max_key_part &&     // (2)
 
5278
                    records < (double)table->quick_rows[key])         // (3)
 
5279
                  records= (double)table->quick_rows[key];
 
5280
 
 
5281
                tmp= records;
 
5282
              }
 
5283
              else
 
5284
              {
 
5285
                /*
 
5286
                  Assume that the first key part matches 1% of the file
 
5287
                  and that the whole key matches 10 (duplicates) or 1
 
5288
                  (unique) records.
 
5289
                  Assume also that more key matches proportionally more
 
5290
                  records
 
5291
                  This gives the formula:
 
5292
                  records = (x * (b-a) + a*c-b)/(c-1)
 
5293
 
 
5294
                  b = records matched by whole key
 
5295
                  a = records matched by first key part (1% of all records?)
 
5296
                  c = number of key parts in key
 
5297
                  x = used key parts (1 <= x <= c)
 
5298
                */
 
5299
                double rec_per_key;
 
5300
                if (!(rec_per_key=(double)
 
5301
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5302
                  rec_per_key=(double) s->records/rec+1;
 
5303
 
 
5304
                if (!s->records)
 
5305
                  tmp = 0;
 
5306
                else if (rec_per_key/(double) s->records >= 0.01)
 
5307
                  tmp = rec_per_key;
 
5308
                else
 
5309
                {
 
5310
                  double a=s->records*0.01;
 
5311
                  if (keyinfo->key_parts > 1)
 
5312
                    tmp= (max_key_part * (rec_per_key - a) +
 
5313
                          a*keyinfo->key_parts - rec_per_key)/
 
5314
                         (keyinfo->key_parts-1);
 
5315
                  else
 
5316
                    tmp= a;
 
5317
                  set_if_bigger(tmp,1.0);
 
5318
                }
 
5319
                records = (ulong) tmp;
 
5320
              }
 
5321
 
 
5322
              if (ref_or_null_part)
 
5323
              {
 
5324
                /* We need to do two key searches to find key */
 
5325
                tmp *= 2.0;
 
5326
                records *= 2.0;
 
5327
              }
 
5328
 
 
5329
              /*
 
5330
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
 
5331
                E(#rows) from range optimizer. Make another try:
 
5332
                
 
5333
                If range optimizer produced E(#rows) for a prefix of the ref 
 
5334
                access we're considering, and that E(#rows) is lower then our
 
5335
                current estimate, make the adjustment.
 
5336
 
 
5337
                The decision whether we can re-use the estimate from the range
 
5338
                optimizer is the same as in ReuseRangeEstimateForRef-3,
 
5339
                applied to first table->quick_key_parts[key] key parts.
 
5340
              */
 
5341
              if (table->quick_keys.is_set(key) &&
 
5342
                  table->quick_key_parts[key] <= max_key_part &&
 
5343
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5344
                  table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
 
5345
                                                         const_part) &&
 
5346
                  records > (double) table->quick_rows[key])
 
5347
              {
 
5348
                tmp= records= (double) table->quick_rows[key];
 
5349
              }
 
5350
            }
 
5351
 
 
5352
            /* Limit the number of matched rows */
 
5353
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5354
            if (table->covering_keys.is_set(key))
 
5355
            {
 
5356
              /* we can use only index tree */
 
5357
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5358
            }
 
5359
            else
 
5360
              tmp= record_count * min(tmp,s->worst_seeks);
 
5361
          }
 
5362
          else
 
5363
            tmp= best_time;                    // Do nothing
 
5364
        }
 
5365
 
 
5366
        if (sj_inside_out_scan && !start_key)
 
5367
        {
 
5368
          tmp= tmp/2;
 
5369
          if (records)
 
5370
            records= records/2;
 
5371
        }
 
5372
 
 
5373
      }
 
5374
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
 
5375
      {
 
5376
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
 
5377
        best= tmp;
 
5378
        best_records= records;
 
5379
        best_key= start_key;
 
5380
        best_max_key_part= max_key_part;
 
5381
        best_ref_depends_map= found_ref;
 
5382
        best_is_sj_inside_out= sj_inside_out_scan;
 
5383
      }
 
5384
    }
 
5385
    records= best_records;
 
5386
  }
 
5387
 
 
5388
  /*
 
5389
    Don't test table scan if it can't be better.
 
5390
    Prefer key lookup if we would use the same key for scanning.
 
5391
 
 
5392
    Don't do a table scan on InnoDB tables, if we can read the used
 
5393
    parts of the row from any of the used index.
 
5394
    This is because table scans uses index and we would not win
 
5395
    anything by using a table scan.
 
5396
 
 
5397
    A word for word translation of the below if-statement in sergefp's
 
5398
    understanding: we check if we should use table scan if:
 
5399
    (1) The found 'ref' access produces more records than a table scan
 
5400
        (or index scan, or quick select), or 'ref' is more expensive than
 
5401
        any of them.
 
5402
    (2) This doesn't hold: the best way to perform table scan is to to perform
 
5403
        'range' access using index IDX, and the best way to perform 'ref' 
 
5404
        access is to use the same index IDX, with the same or more key parts.
 
5405
        (note: it is not clear how this rule is/should be extended to 
 
5406
        index_merge quick selects)
 
5407
    (3) See above note about InnoDB.
 
5408
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
 
5409
             path, but there is no quick select)
 
5410
        If the condition in the above brackets holds, then the only possible
 
5411
        "table scan" access method is ALL/index (there is no quick select).
 
5412
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
 
5413
        choose it over ALL/index, there is no need to consider a full table
 
5414
        scan.
 
5415
  */
 
5416
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
 
5417
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
 
5418
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
 
5419
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
 
5420
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
 
5421
      !(s->table->force_index && best_key && !s->quick))                 // (4)
 
5422
  {                                             // Check full join
 
5423
    ha_rows rnd_records= s->found_records;
 
5424
    /*
 
5425
      If there is a filtering condition on the table (i.e. ref analyzer found
 
5426
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
 
5427
      preceding this table in the join order we're now considering), then 
 
5428
      assume that 25% of the rows will be filtered out by this condition.
 
5429
 
 
5430
      This heuristic is supposed to force tables used in exprZ to be before
 
5431
      this table in join order.
 
5432
    */
 
5433
    if (found_constraint)
 
5434
      rnd_records-= rnd_records/4;
 
5435
 
 
5436
    /*
 
5437
      If applicable, get a more accurate estimate. Don't use the two
 
5438
      heuristics at once.
 
5439
    */
 
5440
    if (s->table->quick_condition_rows != s->found_records)
 
5441
      rnd_records= s->table->quick_condition_rows;
 
5442
 
 
5443
    /*
 
5444
      Range optimizer never proposes a RANGE if it isn't better
 
5445
      than FULL: so if RANGE is present, it's always preferred to FULL.
 
5446
      Here we estimate its cost.
 
5447
    */
 
5448
    if (s->quick)
 
5449
    {
 
5450
      /*
 
5451
        For each record we:
 
5452
        - read record range through 'quick'
 
5453
        - skip rows which does not satisfy WHERE constraints
 
5454
        TODO: 
 
5455
        We take into account possible use of join cache for ALL/index
 
5456
        access (see first else-branch below), but we don't take it into 
 
5457
        account here for range/index_merge access. Find out why this is so.
 
5458
      */
 
5459
      tmp= record_count *
 
5460
        (s->quick->read_time +
 
5461
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5462
    }
 
5463
    else
 
5464
    {
 
5465
      /* Estimate cost of reading table. */
 
5466
      tmp= s->table->file->scan_time();
 
5467
      if (s->table->map & join->outer_join)     // Can't use join cache
 
5468
      {
 
5469
        /*
 
5470
          For each record we have to:
 
5471
          - read the whole table record 
 
5472
          - skip rows which does not satisfy join condition
 
5473
        */
 
5474
        tmp= record_count *
 
5475
          (tmp +
 
5476
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5477
      }
 
5478
      else
 
5479
      {
 
5480
        /* We read the table as many times as join buffer becomes full. */
 
5481
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
 
5482
                           record_count /
 
5483
                           (double) thd->variables.join_buff_size));
 
5484
        /* 
 
5485
            We don't make full cartesian product between rows in the scanned
 
5486
           table and existing records because we skip all rows from the
 
5487
           scanned table, which does not satisfy join condition when 
 
5488
           we read the table (see flush_cached_records for details). Here we
 
5489
           take into account cost to read and skip these records.
 
5490
        */
 
5491
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
 
5492
      }
 
5493
    }
 
5494
 
 
5495
    /*
 
5496
      We estimate the cost of evaluating WHERE clause for found records
 
5497
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
 
5498
      tmp give us total cost of using TABLE SCAN
 
5499
    */
 
5500
    if (best == DBL_MAX ||
 
5501
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
 
5502
         best + record_count/(double) TIME_FOR_COMPARE*records))
 
5503
    {
 
5504
      /*
 
5505
        If the table has a range (s->quick is set) make_join_select()
 
5506
        will ensure that this will be used
 
5507
      */
 
5508
      best= tmp;
 
5509
      records= rows2double(rnd_records);
 
5510
      best_key= 0;
 
5511
      /* range/index_merge/ALL/index access method are "independent", so: */
 
5512
      best_ref_depends_map= 0;
 
5513
      best_is_sj_inside_out= false;
 
5514
    }
 
5515
  }
 
5516
 
 
5517
  /* Update the cost information for the current partial plan */
 
5518
  join->positions[idx].records_read= records;
 
5519
  join->positions[idx].read_time=    best;
 
5520
  join->positions[idx].key=          best_key;
 
5521
  join->positions[idx].table=        s;
 
5522
  join->positions[idx].ref_depend_map= best_ref_depends_map;
 
5523
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
 
5524
 
 
5525
  if (!best_key &&
 
5526
      idx == join->const_tables &&
 
5527
      s->table == join->sort_by_table &&
 
5528
      join->unit->select_limit_cnt >= records)
 
5529
    join->sort_by_table= (TABLE*) 1;  // Must use temporary table
 
5530
 
 
5531
  return;
 
5532
}
 
5533
 
 
5534
 
 
5535
/**
 
5536
  Selects and invokes a search strategy for an optimal query plan.
 
5537
 
 
5538
  The function checks user-configurable parameters that control the search
 
5539
  strategy for an optimal plan, selects the search method and then invokes
 
5540
  it. Each specific optimization procedure stores the final optimal plan in
 
5541
  the array 'join->best_positions', and the cost of the plan in
 
5542
  'join->best_read'.
 
5543
 
 
5544
  @param join         pointer to the structure providing all context info for
 
5545
                      the query
 
5546
  @param join_tables  set of the tables in the query
 
5547
 
 
5548
  @todo
 
5549
    'MAX_TABLES+2' denotes the old implementation of find_best before
 
5550
    the greedy version. Will be removed when greedy_search is approved.
 
5551
 
 
5552
  @retval
 
5553
    false       ok
 
5554
  @retval
 
5555
    true        Fatal error
 
5556
*/
 
5557
 
 
5558
static bool
 
5559
choose_plan(JOIN *join, table_map join_tables)
 
5560
{
 
5561
  uint search_depth= join->thd->variables.optimizer_search_depth;
 
5562
  uint prune_level=  join->thd->variables.optimizer_prune_level;
 
5563
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
5564
 
 
5565
  join->cur_embedding_map= 0;
 
5566
  reset_nj_counters(join->join_list);
 
5567
  /*
 
5568
    if (SELECT_STRAIGHT_JOIN option is set)
 
5569
      reorder tables so dependent tables come after tables they depend 
 
5570
      on, otherwise keep tables in the order they were specified in the query 
 
5571
    else
 
5572
      Apply heuristic: pre-sort all access plans with respect to the number of
 
5573
      records accessed.
 
5574
  */
 
5575
  my_qsort(join->best_ref + join->const_tables,
 
5576
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
 
5577
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
 
5578
  join->cur_emb_sj_nests= 0;
 
5579
  if (straight_join)
 
5580
  {
 
5581
    optimize_straight_join(join, join_tables);
 
5582
  }
 
5583
  else
 
5584
  {
 
5585
    if (search_depth == MAX_TABLES+2)
 
5586
    { /*
 
5587
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
 
5588
        the greedy version. Will be removed when greedy_search is approved.
 
5589
      */
 
5590
      join->best_read= DBL_MAX;
 
5591
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
 
5592
        return(true);
 
5593
    } 
 
5594
    else
 
5595
    {
 
5596
      if (search_depth == 0)
 
5597
        /* Automatically determine a reasonable value for 'search_depth' */
 
5598
        search_depth= determine_search_depth(join);
 
5599
      if (greedy_search(join, join_tables, search_depth, prune_level))
 
5600
        return(true);
 
5601
    }
 
5602
  }
 
5603
 
 
5604
  /* 
 
5605
    Store the cost of this query into a user variable
 
5606
    Don't update last_query_cost for statements that are not "flat joins" :
 
5607
    i.e. they have subqueries, unions or call stored procedures.
 
5608
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
 
5609
  */
 
5610
  if (join->thd->lex->is_single_level_stmt())
 
5611
    join->thd->status_var.last_query_cost= join->best_read;
 
5612
  return(false);
 
5613
}
 
5614
 
 
5615
 
 
5616
/**
 
5617
  Compare two JOIN_TAB objects based on the number of accessed records.
 
5618
 
 
5619
  @param ptr1 pointer to first JOIN_TAB object
 
5620
  @param ptr2 pointer to second JOIN_TAB object
798
5621
 
799
5622
  NOTES
800
5623
    The order relation implemented by join_tab_cmp() is not transitive,
806
5629
      a: dependent = 0x0 table->map = 0x1 found_records = 3 ptr = 0x907e6b0
807
5630
      b: dependent = 0x0 table->map = 0x2 found_records = 3 ptr = 0x907e838
808
5631
      c: dependent = 0x6 table->map = 0x10 found_records = 2 ptr = 0x907ecd0
809
 
 
 
5632
     
810
5633
  @retval
811
5634
    1  if first is bigger
812
5635
  @retval
814
5637
  @retval
815
5638
    0  if equal
816
5639
*/
817
 
int join_tab_cmp(const void* ptr1, const void* ptr2)
 
5640
 
 
5641
static int
 
5642
join_tab_cmp(const void* ptr1, const void* ptr2)
818
5643
{
819
 
  JoinTable *jt1= *(JoinTable**) ptr1;
820
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5644
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5645
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
821
5646
 
822
5647
  if (jt1->dependent & jt2->table->map)
823
5648
    return 1;
824
5649
  if (jt2->dependent & jt1->table->map)
825
 
    return -1;
 
5650
    return -1;  
826
5651
  if (jt1->found_records > jt2->found_records)
827
5652
    return 1;
828
5653
  if (jt1->found_records < jt2->found_records)
829
 
    return -1;
 
5654
    return -1; 
830
5655
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
831
5656
}
832
5657
 
 
5658
 
833
5659
/**
834
5660
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
835
5661
*/
836
 
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
 
5662
 
 
5663
static int
 
5664
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
837
5665
{
838
 
  JoinTable *jt1= *(JoinTable**) ptr1;
839
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5666
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5667
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
840
5668
 
841
5669
  if (jt1->dependent & jt2->table->map)
842
5670
    return 1;
846
5674
}
847
5675
 
848
5676
/**
 
5677
  Heuristic procedure to automatically guess a reasonable degree of
 
5678
  exhaustiveness for the greedy search procedure.
 
5679
 
 
5680
  The procedure estimates the optimization time and selects a search depth
 
5681
  big enough to result in a near-optimal QEP, that doesn't take too long to
 
5682
  find. If the number of tables in the query exceeds some constant, then
 
5683
  search_depth is set to this constant.
 
5684
 
 
5685
  @param join   pointer to the structure providing all context info for
 
5686
                the query
 
5687
 
 
5688
  @note
 
5689
    This is an extremely simplistic implementation that serves as a stub for a
 
5690
    more advanced analysis of the join. Ideally the search depth should be
 
5691
    determined by learning from previous query optimizations, because it will
 
5692
    depend on the CPU power (and other factors).
 
5693
 
 
5694
  @todo
 
5695
    this value should be determined dynamically, based on statistics:
 
5696
    uint max_tables_for_exhaustive_opt= 7;
 
5697
 
 
5698
  @todo
 
5699
    this value could be determined by some mapping of the form:
 
5700
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5701
 
 
5702
  @return
 
5703
    A positive integer that specifies the search depth (and thus the
 
5704
    exhaustiveness) of the depth-first search algorithm used by
 
5705
    'greedy_search'.
 
5706
*/
 
5707
 
 
5708
static uint
 
5709
determine_search_depth(JOIN *join)
 
5710
{
 
5711
  uint table_count=  join->tables - join->const_tables;
 
5712
  uint search_depth;
 
5713
  /* TODO: this value should be determined dynamically, based on statistics: */
 
5714
  uint max_tables_for_exhaustive_opt= 7;
 
5715
 
 
5716
  if (table_count <= max_tables_for_exhaustive_opt)
 
5717
    search_depth= table_count+1; // use exhaustive for small number of tables
 
5718
  else
 
5719
    /*
 
5720
      TODO: this value could be determined by some mapping of the form:
 
5721
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5722
    */
 
5723
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
 
5724
 
 
5725
  return search_depth;
 
5726
}
 
5727
 
 
5728
 
 
5729
/**
 
5730
  Select the best ways to access the tables in a query without reordering them.
 
5731
 
 
5732
    Find the best access paths for each query table and compute their costs
 
5733
    according to their order in the array 'join->best_ref' (thus without
 
5734
    reordering the join tables). The function calls sequentially
 
5735
    'best_access_path' for each table in the query to select the best table
 
5736
    access method. The final optimal plan is stored in the array
 
5737
    'join->best_positions', and the corresponding cost in 'join->best_read'.
 
5738
 
 
5739
  @param join          pointer to the structure providing all context info for
 
5740
                       the query
 
5741
  @param join_tables   set of the tables in the query
 
5742
 
 
5743
  @note
 
5744
    This function can be applied to:
 
5745
    - queries with STRAIGHT_JOIN
 
5746
    - internally to compute the cost of an arbitrary QEP
 
5747
  @par
 
5748
    Thus 'optimize_straight_join' can be used at any stage of the query
 
5749
    optimization process to finalize a QEP as it is.
 
5750
*/
 
5751
 
 
5752
static void
 
5753
optimize_straight_join(JOIN *join, table_map join_tables)
 
5754
{
 
5755
  JOIN_TAB *s;
 
5756
  uint idx= join->const_tables;
 
5757
  double    record_count= 1.0;
 
5758
  double    read_time=    0.0;
 
5759
 
 
5760
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
5761
  {
 
5762
    /* Find the best access method from 's' to the current partial plan */
 
5763
    advance_sj_state(join_tables, s);
 
5764
    best_access_path(join, s, join->thd, join_tables, idx,
 
5765
                     record_count, read_time);
 
5766
    /* compute the cost of the new plan extended with 's' */
 
5767
    record_count*= join->positions[idx].records_read;
 
5768
    read_time+=    join->positions[idx].read_time;
 
5769
    join_tables&= ~(s->table->map);
 
5770
    ++idx;
 
5771
  }
 
5772
 
 
5773
  read_time+= record_count / (double) TIME_FOR_COMPARE;
 
5774
  if (join->sort_by_table &&
 
5775
      join->sort_by_table != join->positions[join->const_tables].table->table)
 
5776
    read_time+= record_count;  // We have to make a temp table
 
5777
  memcpy((uchar*) join->best_positions, (uchar*) join->positions,
 
5778
         sizeof(POSITION)*idx);
 
5779
  join->best_read= read_time;
 
5780
}
 
5781
 
 
5782
 
 
5783
/**
 
5784
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
 
5785
 
 
5786
    The search procedure uses a hybrid greedy/exhaustive search with controlled
 
5787
    exhaustiveness. The search is performed in N = card(remaining_tables)
 
5788
    steps. Each step evaluates how promising is each of the unoptimized tables,
 
5789
    selects the most promising table, and extends the current partial QEP with
 
5790
    that table.  Currenly the most 'promising' table is the one with least
 
5791
    expensive extension.\
 
5792
 
 
5793
    There are two extreme cases:
 
5794
    -# When (card(remaining_tables) < search_depth), the estimate finds the
 
5795
    best complete continuation of the partial QEP. This continuation can be
 
5796
    used directly as a result of the search.
 
5797
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
 
5798
    consideres the extension of the current QEP with each of the remaining
 
5799
    unoptimized tables.
 
5800
 
 
5801
    All other cases are in-between these two extremes. Thus the parameter
 
5802
    'search_depth' controlls the exhaustiveness of the search. The higher the
 
5803
    value, the longer the optimizaton time and possibly the better the
 
5804
    resulting plan. The lower the value, the fewer alternative plans are
 
5805
    estimated, but the more likely to get a bad QEP.
 
5806
 
 
5807
    All intermediate and final results of the procedure are stored in 'join':
 
5808
    - join->positions     : modified for every partial QEP that is explored
 
5809
    - join->best_positions: modified for the current best complete QEP
 
5810
    - join->best_read     : modified for the current best complete QEP
 
5811
    - join->best_ref      : might be partially reordered
 
5812
 
 
5813
    The final optimal plan is stored in 'join->best_positions', and its
 
5814
    corresponding cost in 'join->best_read'.
 
5815
 
 
5816
  @note
 
5817
    The following pseudocode describes the algorithm of 'greedy_search':
 
5818
 
 
5819
    @code
 
5820
    procedure greedy_search
 
5821
    input: remaining_tables
 
5822
    output: pplan;
 
5823
    {
 
5824
      pplan = <>;
 
5825
      do {
 
5826
        (t, a) = best_extension(pplan, remaining_tables);
 
5827
        pplan = concat(pplan, (t, a));
 
5828
        remaining_tables = remaining_tables - t;
 
5829
      } while (remaining_tables != {})
 
5830
      return pplan;
 
5831
    }
 
5832
 
 
5833
  @endcode
 
5834
    where 'best_extension' is a placeholder for a procedure that selects the
 
5835
    most "promising" of all tables in 'remaining_tables'.
 
5836
    Currently this estimate is performed by calling
 
5837
    'best_extension_by_limited_search' to evaluate all extensions of the
 
5838
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
 
5839
    mainly depends on that of 'best_extension_by_limited_search'.
 
5840
 
 
5841
  @par
 
5842
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
 
5843
    worst-case complexity of this algorithm is <=
 
5844
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
 
5845
    complexity of greedy_search is O(N!).
 
5846
 
 
5847
  @par
 
5848
    In the future, 'greedy_search' might be extended to support other
 
5849
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
 
5850
 
 
5851
  @param join             pointer to the structure providing all context info
 
5852
                          for the query
 
5853
  @param remaining_tables set of tables not included into the partial plan yet
 
5854
  @param search_depth     controlls the exhaustiveness of the search
 
5855
  @param prune_level      the pruning heuristics that should be applied during
 
5856
                          search
 
5857
 
 
5858
  @retval
 
5859
    false       ok
 
5860
  @retval
 
5861
    true        Fatal error
 
5862
*/
 
5863
 
 
5864
static bool
 
5865
greedy_search(JOIN      *join,
 
5866
              table_map remaining_tables,
 
5867
              uint      search_depth,
 
5868
              uint      prune_level)
 
5869
{
 
5870
  double    record_count= 1.0;
 
5871
  double    read_time=    0.0;
 
5872
  uint      idx= join->const_tables; // index into 'join->best_ref'
 
5873
  uint      best_idx;
 
5874
  uint      size_remain;    // cardinality of remaining_tables
 
5875
  POSITION  best_pos;
 
5876
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
 
5877
 
 
5878
  /* number of tables that remain to be optimized */
 
5879
  size_remain= my_count_bits(remaining_tables);
 
5880
 
 
5881
  do {
 
5882
    /* Find the extension of the current QEP with the lowest cost */
 
5883
    join->best_read= DBL_MAX;
 
5884
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
 
5885
                                         read_time, search_depth, prune_level))
 
5886
      return(true);
 
5887
 
 
5888
    if (size_remain <= search_depth)
 
5889
    {
 
5890
      /*
 
5891
        'join->best_positions' contains a complete optimal extension of the
 
5892
        current partial QEP.
 
5893
      */
 
5894
      return(false);
 
5895
    }
 
5896
 
 
5897
    /* select the first table in the optimal extension as most promising */
 
5898
    best_pos= join->best_positions[idx];
 
5899
    best_table= best_pos.table;
 
5900
    /*
 
5901
      Each subsequent loop of 'best_extension_by_limited_search' uses
 
5902
      'join->positions' for cost estimates, therefore we have to update its
 
5903
      value.
 
5904
    */
 
5905
    join->positions[idx]= best_pos;
 
5906
 
 
5907
    /* find the position of 'best_table' in 'join->best_ref' */
 
5908
    best_idx= idx;
 
5909
    JOIN_TAB *pos= join->best_ref[best_idx];
 
5910
    while (pos && best_table != pos)
 
5911
      pos= join->best_ref[++best_idx];
 
5912
    assert((pos != NULL)); // should always find 'best_table'
 
5913
    /* move 'best_table' at the first free position in the array of joins */
 
5914
    swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
 
5915
 
 
5916
    /* compute the cost of the new plan extended with 'best_table' */
 
5917
    record_count*= join->positions[idx].records_read;
 
5918
    read_time+=    join->positions[idx].read_time;
 
5919
 
 
5920
    remaining_tables&= ~(best_table->table->map);
 
5921
    --size_remain;
 
5922
    ++idx;
 
5923
  } while (true);
 
5924
}
 
5925
 
 
5926
 
 
5927
/**
 
5928
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
 
5929
  exhaustive search.
 
5930
 
 
5931
    The procedure searches for the optimal ordering of the query tables in set
 
5932
    'remaining_tables' of size N, and the corresponding optimal access paths to
 
5933
    each table. The choice of a table order and an access path for each table
 
5934
    constitutes a query execution plan (QEP) that fully specifies how to
 
5935
    execute the query.
 
5936
   
 
5937
    The maximal size of the found plan is controlled by the parameter
 
5938
    'search_depth'. When search_depth == N, the resulting plan is complete and
 
5939
    can be used directly as a QEP. If search_depth < N, the found plan consists
 
5940
    of only some of the query tables. Such "partial" optimal plans are useful
 
5941
    only as input to query optimization procedures, and cannot be used directly
 
5942
    to execute a query.
 
5943
 
 
5944
    The algorithm begins with an empty partial plan stored in 'join->positions'
 
5945
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
 
5946
    evaluates the cost of the partial plan extended by all access plans for
 
5947
    each of the relations in 'remaining_tables', expands the current partial
 
5948
    plan with the access plan that results in lowest cost of the expanded
 
5949
    partial plan, and removes the corresponding relation from
 
5950
    'remaining_tables'. The algorithm continues until it either constructs a
 
5951
    complete optimal plan, or constructs an optimal plartial plan with size =
 
5952
    search_depth.
 
5953
 
 
5954
    The final optimal plan is stored in 'join->best_positions'. The
 
5955
    corresponding cost of the optimal plan is in 'join->best_read'.
 
5956
 
 
5957
  @note
 
5958
    The procedure uses a recursive depth-first search where the depth of the
 
5959
    recursion (and thus the exhaustiveness of the search) is controlled by the
 
5960
    parameter 'search_depth'.
 
5961
 
 
5962
  @note
 
5963
    The pseudocode below describes the algorithm of
 
5964
    'best_extension_by_limited_search'. The worst-case complexity of this
 
5965
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
 
5966
    the complexity of greedy_search is O(N!).
 
5967
 
 
5968
    @code
 
5969
    procedure best_extension_by_limited_search(
 
5970
      pplan in,             // in, partial plan of tables-joined-so-far
 
5971
      pplan_cost,           // in, cost of pplan
 
5972
      remaining_tables,     // in, set of tables not referenced in pplan
 
5973
      best_plan_so_far,     // in/out, best plan found so far
 
5974
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
 
5975
      search_depth)         // in, maximum size of the plans being considered
 
5976
    {
 
5977
      for each table T from remaining_tables
 
5978
      {
 
5979
        // Calculate the cost of using table T as above
 
5980
        cost = complex-series-of-calculations;
 
5981
 
 
5982
        // Add the cost to the cost so far.
 
5983
        pplan_cost+= cost;
 
5984
 
 
5985
        if (pplan_cost >= best_plan_so_far_cost)
 
5986
          // pplan_cost already too great, stop search
 
5987
          continue;
 
5988
 
 
5989
        pplan= expand pplan by best_access_method;
 
5990
        remaining_tables= remaining_tables - table T;
 
5991
        if (remaining_tables is not an empty set
 
5992
            and
 
5993
            search_depth > 1)
 
5994
        {
 
5995
          best_extension_by_limited_search(pplan, pplan_cost,
 
5996
                                           remaining_tables,
 
5997
                                           best_plan_so_far,
 
5998
                                           best_plan_so_far_cost,
 
5999
                                           search_depth - 1);
 
6000
        }
 
6001
        else
 
6002
        {
 
6003
          best_plan_so_far_cost= pplan_cost;
 
6004
          best_plan_so_far= pplan;
 
6005
        }
 
6006
      }
 
6007
    }
 
6008
    @endcode
 
6009
 
 
6010
  @note
 
6011
    When 'best_extension_by_limited_search' is called for the first time,
 
6012
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
 
6013
    The actual implementation provides a way to optionally use pruning
 
6014
    heuristic (controlled by the parameter 'prune_level') to reduce the search
 
6015
    space by skipping some partial plans.
 
6016
 
 
6017
  @note
 
6018
    The parameter 'search_depth' provides control over the recursion
 
6019
    depth, and thus the size of the resulting optimal plan.
 
6020
 
 
6021
  @param join             pointer to the structure providing all context info
 
6022
                          for the query
 
6023
  @param remaining_tables set of tables not included into the partial plan yet
 
6024
  @param idx              length of the partial QEP in 'join->positions';
 
6025
                          since a depth-first search is used, also corresponds
 
6026
                          to the current depth of the search tree;
 
6027
                          also an index in the array 'join->best_ref';
 
6028
  @param record_count     estimate for the number of records returned by the
 
6029
                          best partial plan
 
6030
  @param read_time        the cost of the best partial plan
 
6031
  @param search_depth     maximum depth of the recursion and thus size of the
 
6032
                          found optimal plan
 
6033
                          (0 < search_depth <= join->tables+1).
 
6034
  @param prune_level      pruning heuristics that should be applied during
 
6035
                          optimization
 
6036
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
 
6037
 
 
6038
  @retval
 
6039
    false       ok
 
6040
  @retval
 
6041
    true        Fatal error
 
6042
*/
 
6043
 
 
6044
static bool
 
6045
best_extension_by_limited_search(JOIN      *join,
 
6046
                                 table_map remaining_tables,
 
6047
                                 uint      idx,
 
6048
                                 double    record_count,
 
6049
                                 double    read_time,
 
6050
                                 uint      search_depth,
 
6051
                                 uint      prune_level)
 
6052
{
 
6053
  THD *thd= join->thd;
 
6054
  if (thd->killed)  // Abort
 
6055
    return(true);
 
6056
 
 
6057
  /* 
 
6058
     'join' is a partial plan with lower cost than the best plan so far,
 
6059
     so continue expanding it further with the tables in 'remaining_tables'.
 
6060
  */
 
6061
  JOIN_TAB *s;
 
6062
  double best_record_count= DBL_MAX;
 
6063
  double best_read_time=    DBL_MAX;
 
6064
 
 
6065
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
6066
  {
 
6067
    table_map real_table_bit= s->table->map;
 
6068
    if ((remaining_tables & real_table_bit) && 
 
6069
        !(remaining_tables & s->dependent) && 
 
6070
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6071
    {
 
6072
      double current_record_count, current_read_time;
 
6073
      advance_sj_state(remaining_tables, s);
 
6074
 
 
6075
      /*
 
6076
        psergey-insideout-todo: 
 
6077
          when best_access_path() detects it could do an InsideOut scan or 
 
6078
          some other scan, have it return an insideout scan and a flag that 
 
6079
          requests to "fork" this loop iteration. (Q: how does that behave 
 
6080
          when the depth is insufficient??)
 
6081
      */
 
6082
      /* Find the best access method from 's' to the current partial plan */
 
6083
      best_access_path(join, s, thd, remaining_tables, idx,
 
6084
                       record_count, read_time);
 
6085
      /* Compute the cost of extending the plan with 's' */
 
6086
      current_record_count= record_count * join->positions[idx].records_read;
 
6087
      current_read_time=    read_time + join->positions[idx].read_time;
 
6088
 
 
6089
      /* Expand only partial plans with lower cost than the best QEP so far */
 
6090
      if ((current_read_time +
 
6091
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
 
6092
      {
 
6093
        restore_prev_nj_state(s);
 
6094
        restore_prev_sj_state(remaining_tables, s);
 
6095
        continue;
 
6096
      }
 
6097
 
 
6098
      /*
 
6099
        Prune some less promising partial plans. This heuristic may miss
 
6100
        the optimal QEPs, thus it results in a non-exhaustive search.
 
6101
      */
 
6102
      if (prune_level == 1)
 
6103
      {
 
6104
        if (best_record_count > current_record_count ||
 
6105
            best_read_time > current_read_time ||
 
6106
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
 
6107
        {
 
6108
          if (best_record_count >= current_record_count &&
 
6109
              best_read_time >= current_read_time &&
 
6110
              /* TODO: What is the reasoning behind this condition? */
 
6111
              (!(s->key_dependent & remaining_tables) ||
 
6112
               join->positions[idx].records_read < 2.0))
 
6113
          {
 
6114
            best_record_count= current_record_count;
 
6115
            best_read_time=    current_read_time;
 
6116
          }
 
6117
        }
 
6118
        else
 
6119
        {
 
6120
          restore_prev_nj_state(s);
 
6121
          restore_prev_sj_state(remaining_tables, s);
 
6122
          continue;
 
6123
        }
 
6124
      }
 
6125
 
 
6126
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
 
6127
      { /* Recursively expand the current partial plan */
 
6128
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6129
        if (best_extension_by_limited_search(join,
 
6130
                                             remaining_tables & ~real_table_bit,
 
6131
                                             idx + 1,
 
6132
                                             current_record_count,
 
6133
                                             current_read_time,
 
6134
                                             search_depth - 1,
 
6135
                                             prune_level))
 
6136
          return(true);
 
6137
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6138
      }
 
6139
      else
 
6140
      { /*
 
6141
          'join' is either the best partial QEP with 'search_depth' relations,
 
6142
          or the best complete QEP so far, whichever is smaller.
 
6143
        */
 
6144
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
 
6145
        if (join->sort_by_table &&
 
6146
            join->sort_by_table !=
 
6147
            join->positions[join->const_tables].table->table)
 
6148
          /* We have to make a temp table */
 
6149
          current_read_time+= current_record_count;
 
6150
        if ((search_depth == 1) || (current_read_time < join->best_read))
 
6151
        {
 
6152
          memcpy((uchar*) join->best_positions, (uchar*) join->positions,
 
6153
                 sizeof(POSITION) * (idx + 1));
 
6154
          join->best_read= current_read_time - 0.001;
 
6155
        }
 
6156
      }
 
6157
      restore_prev_nj_state(s);
 
6158
      restore_prev_sj_state(remaining_tables, s);
 
6159
    }
 
6160
  }
 
6161
  return(false);
 
6162
}
 
6163
 
 
6164
 
 
6165
/**
 
6166
  @todo
 
6167
  - TODO: this function is here only temporarily until 'greedy_search' is
 
6168
  tested and accepted.
 
6169
 
 
6170
  RETURN VALUES
 
6171
    false       ok
 
6172
    true        Fatal error
 
6173
*/
 
6174
static bool
 
6175
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
 
6176
          double read_time)
 
6177
{
 
6178
  THD *thd= join->thd;
 
6179
  if (thd->killed)
 
6180
    return(true);
 
6181
  if (!rest_tables)
 
6182
  {
 
6183
    read_time+=record_count/(double) TIME_FOR_COMPARE;
 
6184
    if (join->sort_by_table &&
 
6185
        join->sort_by_table !=
 
6186
        join->positions[join->const_tables].table->table)
 
6187
      read_time+=record_count;                  // We have to make a temp table
 
6188
    if (read_time < join->best_read)
 
6189
    {
 
6190
      memcpy((uchar*) join->best_positions,(uchar*) join->positions,
 
6191
             sizeof(POSITION)*idx);
 
6192
      join->best_read= read_time - 0.001;
 
6193
    }
 
6194
    return(false);
 
6195
  }
 
6196
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
 
6197
    return(false);                                      /* Found better before */
 
6198
 
 
6199
  JOIN_TAB *s;
 
6200
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
 
6201
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
 
6202
  {
 
6203
    table_map real_table_bit=s->table->map;
 
6204
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
 
6205
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6206
    {
 
6207
      double records, best;
 
6208
      advance_sj_state(rest_tables, s);
 
6209
      best_access_path(join, s, thd, rest_tables, idx, record_count, 
 
6210
                       read_time);
 
6211
      records= join->positions[idx].records_read;
 
6212
      best= join->positions[idx].read_time;
 
6213
      /*
 
6214
        Go to the next level only if there hasn't been a better key on
 
6215
        this level! This will cut down the search for a lot simple cases!
 
6216
      */
 
6217
      double current_record_count=record_count*records;
 
6218
      double current_read_time=read_time+best;
 
6219
      if (best_record_count > current_record_count ||
 
6220
          best_read_time > current_read_time ||
 
6221
          (idx == join->const_tables && s->table == join->sort_by_table))
 
6222
      {
 
6223
        if (best_record_count >= current_record_count &&
 
6224
            best_read_time >= current_read_time &&
 
6225
            (!(s->key_dependent & rest_tables) || records < 2.0))
 
6226
        {
 
6227
          best_record_count=current_record_count;
 
6228
          best_read_time=current_read_time;
 
6229
        }
 
6230
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6231
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
 
6232
                      current_record_count,current_read_time))
 
6233
          return(true);
 
6234
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6235
      }
 
6236
      restore_prev_nj_state(s);
 
6237
      restore_prev_sj_state(rest_tables, s);
 
6238
      if (join->select_options & SELECT_STRAIGHT_JOIN)
 
6239
        break;                          // Don't test all combinations
 
6240
    }
 
6241
  }
 
6242
  return(false);
 
6243
}
 
6244
 
 
6245
 
 
6246
/**
849
6247
  Find how much space the prevous read not const tables takes in cache.
850
6248
*/
851
 
void calc_used_field_length(Session *, JoinTable *join_tab)
 
6249
 
 
6250
static void calc_used_field_length(THD *thd __attribute__((unused)),
 
6251
                                   JOIN_TAB *join_tab)
852
6252
{
853
 
  uint32_t null_fields,blobs,fields,rec_length;
 
6253
  uint null_fields,blobs,fields,rec_length;
854
6254
  Field **f_ptr,*field;
 
6255
  MY_BITMAP *read_set= join_tab->table->read_set;;
855
6256
 
856
6257
  null_fields= blobs= fields= rec_length=0;
857
 
  for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
 
6258
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
858
6259
  {
859
 
    if (field->isReadSet())
 
6260
    if (bitmap_is_set(read_set, field->field_index))
860
6261
    {
861
 
      uint32_t flags=field->flags;
 
6262
      uint flags=field->flags;
862
6263
      fields++;
863
6264
      rec_length+=field->pack_length();
864
6265
      if (flags & BLOB_FLAG)
865
 
        blobs++;
 
6266
        blobs++;
866
6267
      if (!(flags & NOT_NULL_FLAG))
867
 
        null_fields++;
 
6268
        null_fields++;
868
6269
    }
869
6270
  }
870
6271
  if (null_fields)
871
 
    rec_length+=(join_tab->table->getNullFields() + 7)/8;
 
6272
    rec_length+=(join_tab->table->s->null_fields+7)/8;
872
6273
  if (join_tab->table->maybe_null)
873
6274
    rec_length+=sizeof(bool);
874
6275
  if (blobs)
875
6276
  {
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
 
}
 
6277
    uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
 
6278
                             (join_tab->table->s->reclength- rec_length));
 
6279
    rec_length+=(uint) max(4,blob_length);
 
6280
  }
 
6281
  join_tab->used_fields=fields;
 
6282
  join_tab->used_fieldlength=rec_length;
 
6283
  join_tab->used_blobs=blobs;
 
6284
}
 
6285
 
 
6286
 
 
6287
static uint
 
6288
cache_record_length(JOIN *join,uint idx)
 
6289
{
 
6290
  uint length=0;
 
6291
  JOIN_TAB **pos,**end;
 
6292
  THD *thd=join->thd;
 
6293
 
 
6294
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
 
6295
       pos != end ;
 
6296
       pos++)
 
6297
  {
 
6298
    JOIN_TAB *join_tab= *pos;
 
6299
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
6300
      calc_used_field_length(thd, join_tab);
 
6301
    length+=join_tab->used_fieldlength;
 
6302
  }
 
6303
  return length;
 
6304
}
 
6305
 
 
6306
 
 
6307
/*
 
6308
  Get the number of different row combinations for subset of partial join
 
6309
 
 
6310
  SYNOPSIS
 
6311
    prev_record_reads()
 
6312
      join       The join structure
 
6313
      idx        Number of tables in the partial join order (i.e. the
 
6314
                 partial join order is in join->positions[0..idx-1])
 
6315
      found_ref  Bitmap of tables for which we need to find # of distinct
 
6316
                 row combinations.
 
6317
 
 
6318
  DESCRIPTION
 
6319
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
 
6320
    tables within that join order (specified in found_ref), find out how many
 
6321
    distinct row combinations of subset tables will be in the result of the
 
6322
    partial join order.
 
6323
     
 
6324
    This is used as follows: Suppose we have a table accessed with a ref-based
 
6325
    method. The ref access depends on current rows of tables in found_ref.
 
6326
    We want to count # of different ref accesses. We assume two ref accesses
 
6327
    will be different if at least one of access parameters is different.
 
6328
    Example: consider a query
 
6329
 
 
6330
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
 
6331
 
 
6332
    and a join order:
 
6333
      t1,  ref access on t1.key=c1
 
6334
      t2,  ref access on t2.key=c2       
 
6335
      t3,  ref access on t3.key=t1.field 
 
6336
    
 
6337
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
 
6338
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
 
6339
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
 
6340
            n_distinct_ref_scans = #records_read(t1)
 
6341
    
 
6342
    The reason for having this function (at least the latest version of it)
 
6343
    is that we need to account for buffering in join execution. 
 
6344
    
 
6345
    An edge-case example: if we have a non-first table in join accessed via
 
6346
    ref(const) or ref(param) where there is a small number of different
 
6347
    values of param, then the access will likely hit the disk cache and will
 
6348
    not require any disk seeks.
 
6349
    
 
6350
    The proper solution would be to assume an LRU disk cache of some size,
 
6351
    calculate probability of cache hits, etc. For now we just count
 
6352
    identical ref accesses as one.
 
6353
 
 
6354
  RETURN 
 
6355
    Expected number of row combinations
 
6356
*/
 
6357
 
 
6358
static double
 
6359
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
 
6360
{
 
6361
  double found=1.0;
 
6362
  POSITION *pos_end= join->positions - 1;
 
6363
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
 
6364
  {
 
6365
    if (pos->table->table->map & found_ref)
 
6366
    {
 
6367
      found_ref|= pos->ref_depend_map;
 
6368
      /* 
 
6369
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table 
 
6370
        with no matching row we will get position[t2].records_read==0. 
 
6371
        Actually the size of output is one null-complemented row, therefore 
 
6372
        we will use value of 1 whenever we get records_read==0.
 
6373
 
 
6374
        Note
 
6375
        - the above case can't occur if inner part of outer join has more 
 
6376
          than one table: table with no matches will not be marked as const.
 
6377
 
 
6378
        - Ideally we should add 1 to records_read for every possible null-
 
6379
          complemented row. We're not doing it because: 1. it will require
 
6380
          non-trivial code and add overhead. 2. The value of records_read
 
6381
          is an inprecise estimate and adding 1 (or, in the worst case,
 
6382
          #max_nested_outer_joins=64-1) will not make it any more precise.
 
6383
      */
 
6384
      if (pos->records_read > DBL_EPSILON)
 
6385
        found*= pos->records_read;
 
6386
    }
 
6387
  }
 
6388
  return found;
 
6389
}
 
6390
 
923
6391
 
924
6392
/**
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.
 
6393
  Set up join struct according to best position.
929
6394
*/
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)
 
6395
 
 
6396
static bool
 
6397
get_best_combination(JOIN *join)
 
6398
{
 
6399
  uint i,tablenr;
 
6400
  table_map used_tables;
 
6401
  JOIN_TAB *join_tab,*j;
 
6402
  KEYUSE *keyuse;
 
6403
  uint table_count;
 
6404
  THD *thd=join->thd;
 
6405
 
 
6406
  table_count=join->tables;
 
6407
  if (!(join->join_tab=join_tab=
 
6408
        (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
 
6409
    return(true);
 
6410
 
 
6411
  join->full_join=0;
 
6412
 
 
6413
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
 
6414
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
952
6415
  {
953
 
    Item *res;
954
 
    if ((res= new Item_cond_and(*e1, e2)))
 
6416
    TABLE *form;
 
6417
    *j= *join->best_positions[tablenr].table;
 
6418
    form=join->table[tablenr]=j->table;
 
6419
    used_tables|= form->map;
 
6420
    form->reginfo.join_tab=j;
 
6421
    if (!*j->on_expr_ref)
 
6422
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
 
6423
    if (j->type == JT_CONST)
 
6424
      continue;                                 // Handled in make_join_stat..
 
6425
 
 
6426
    j->ref.key = -1;
 
6427
    j->ref.key_parts=0;
 
6428
 
 
6429
    if (j->type == JT_SYSTEM)
 
6430
      continue;
 
6431
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
955
6432
    {
956
 
      *e1= res;
957
 
      res->quick_fix_field();
 
6433
      j->type=JT_ALL;
 
6434
      if (tablenr != join->const_tables)
 
6435
        join->full_join=1;
958
6436
    }
 
6437
    else if (create_ref_for_key(join, j, keyuse, used_tables))
 
6438
      return(true);                        // Something went wrong
959
6439
  }
960
 
  else
961
 
    *e1= e2;
 
6440
 
 
6441
  for (i=0 ; i < table_count ; i++)
 
6442
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
 
6443
  update_depend_map(join);
 
6444
  return(0);
962
6445
}
963
6446
 
964
 
bool create_ref_for_key(Join *join, 
965
 
                        JoinTable *j, 
966
 
                        optimizer::KeyUse *org_keyuse,
967
 
                        table_map used_tables)
 
6447
 
 
6448
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
6449
                               table_map used_tables)
968
6450
{
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;
 
6451
  KEYUSE *keyuse=org_keyuse;
 
6452
  THD  *thd= join->thd;
 
6453
  uint keyparts,length,key;
 
6454
  TABLE *table;
 
6455
  KEY *keyinfo;
976
6456
 
977
6457
  /*  Use best key from find_best */
978
 
  table= j->table;
979
 
  key= keyuse->getKey();
980
 
  keyinfo= table->key_info + key;
 
6458
  table=j->table;
 
6459
  key=keyuse->key;
 
6460
  keyinfo=table->key_info+key;
981
6461
 
982
6462
  {
983
 
    keyparts= length= 0;
984
 
    uint32_t found_part_ref_or_null= 0;
 
6463
    keyparts=length=0;
 
6464
    uint found_part_ref_or_null= 0;
985
6465
    /*
986
6466
      Calculate length for the used key
987
6467
      Stop if there is a missing key part or when we find second key_part
989
6469
    */
990
6470
    do
991
6471
    {
992
 
      if (! (~used_tables & keyuse->getUsedTables()))
 
6472
      if (!(~used_tables & keyuse->used_tables))
993
6473
      {
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
 
        }
 
6474
        if (keyparts == keyuse->keypart &&
 
6475
            !(found_part_ref_or_null & keyuse->optimize))
 
6476
        {
 
6477
          keyparts++;
 
6478
          length+= keyinfo->key_part[keyuse->keypart].store_length;
 
6479
          found_part_ref_or_null|= keyuse->optimize;
 
6480
        }
1001
6481
      }
1002
6482
      keyuse++;
1003
 
    } while (keyuse->getTable() == table && keyuse->getKey() == key);
 
6483
    } while (keyuse->table == table && keyuse->key == key);
1004
6484
  }
1005
6485
 
1006
6486
  /* set up fieldref */
1008
6488
  j->ref.key_parts=keyparts;
1009
6489
  j->ref.key_length=length;
1010
6490
  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)))
 
6491
  if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
 
6492
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
 
6493
                                                   (keyparts+1)))) ||
 
6494
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
 
6495
      !(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
1016
6496
  {
1017
6497
    return(true);
1018
6498
  }
1022
6502
  j->ref.disable_cache= false;
1023
6503
  keyuse=org_keyuse;
1024
6504
 
1025
 
  StoredKey **ref_key= j->ref.key_copy;
1026
 
  unsigned char *key_buff= j->ref.key_buff, *null_ref_key= 0;
 
6505
  store_key **ref_key= j->ref.key_copy;
 
6506
  uchar *key_buff=j->ref.key_buff, *null_ref_key= 0;
1027
6507
  bool keyuse_uses_no_tables= true;
1028
6508
  {
1029
 
    for (uint32_t i= 0; i < keyparts; keyuse++, i++)
 
6509
    uint i;
 
6510
    for (i=0 ; i < keyparts ; keyuse++,i++)
1030
6511
    {
1031
 
      while (keyuse->getKeypart() != i ||
1032
 
             ((~used_tables) & keyuse->getUsedTables()))
1033
 
        keyuse++;       /* Skip other parts */
 
6512
      while (keyuse->keypart != i ||
 
6513
             ((~used_tables) & keyuse->used_tables))
 
6514
        keyuse++;                               /* Skip other parts */
1034
6515
 
1035
 
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
1036
 
      j->ref.items[i]= keyuse->getVal();    // Save for cond removal
1037
 
      j->ref.cond_guards[i]= keyuse->getConditionalGuard();
1038
 
      if (keyuse->isNullRejected())
 
6516
      uint maybe_null= test(keyinfo->key_part[i].null_bit);
 
6517
      j->ref.items[i]=keyuse->val;              // Save for cond removal
 
6518
      j->ref.cond_guards[i]= keyuse->cond_guard;
 
6519
      if (keyuse->null_rejecting) 
1039
6520
        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,
 
6521
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
 
6522
      if (!keyuse->used_tables &&
 
6523
          !(join->select_options & SELECT_DESCRIBE))
 
6524
      {                                 // Compare against constant
 
6525
        store_key_item tmp(thd, keyinfo->key_part[i].field,
1044
6526
                           key_buff + maybe_null,
1045
6527
                           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();
 
6528
                           keyinfo->key_part[i].length, keyuse->val);
 
6529
        if (thd->is_fatal_error)
 
6530
          return(true);
 
6531
        tmp.copy();
1050
6532
      }
1051
6533
      else
1052
 
        *ref_key++= get_store_key(session,
1053
 
          keyuse,join->const_table_map,
1054
 
          &keyinfo->key_part[i],
1055
 
          key_buff, maybe_null);
 
6534
        *ref_key++= get_store_key(thd,
 
6535
                                  keyuse,join->const_table_map,
 
6536
                                  &keyinfo->key_part[i],
 
6537
                                  key_buff, maybe_null);
1056
6538
      /*
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
 
6539
        Remember if we are going to use REF_OR_NULL
 
6540
        But only if field _really_ can be null i.e. we force JT_REF
 
6541
        instead of JT_REF_OR_NULL in case if field can't be null
1060
6542
      */
1061
 
      if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
1062
 
        null_ref_key= key_buff;
 
6543
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
6544
        null_ref_key= key_buff;
1063
6545
      key_buff+=keyinfo->key_part[i].store_length;
1064
6546
    }
1065
6547
  }
1066
 
  *ref_key= 0;       // end_marker
1067
 
  if (j->type == AM_CONST)
 
6548
  *ref_key=0;                           // end_marker
 
6549
  if (j->type == JT_CONST)
1068
6550
    j->table->const_table= 1;
1069
6551
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
1070
6552
           keyparts != keyinfo->key_parts || null_ref_key)
1071
6553
  {
1072
6554
    /* Must read with repeat */
1073
 
    j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
 
6555
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
1074
6556
    j->ref.null_ref_key= null_ref_key;
1075
6557
  }
1076
6558
  else if (keyuse_uses_no_tables)
1082
6564
      Here we should not mark the table as a 'const' as a field may
1083
6565
      have a 'normal' value or a NULL value.
1084
6566
    */
1085
 
    j->type= AM_CONST;
1086
 
  }
1087
 
  else
1088
 
    j->type= AM_EQ_REF;
1089
 
  return 0;
1090
 
}
 
6567
    j->type=JT_CONST;
 
6568
  }
 
6569
  else
 
6570
    j->type=JT_EQ_REF;
 
6571
  return(0);
 
6572
}
 
6573
 
 
6574
 
 
6575
 
 
6576
static store_key *
 
6577
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
 
6578
              KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
 
6579
{
 
6580
  if (!((~used_tables) & keyuse->used_tables))          // if const item
 
6581
  {
 
6582
    return new store_key_const_item(thd,
 
6583
                                    key_part->field,
 
6584
                                    key_buff + maybe_null,
 
6585
                                    maybe_null ? key_buff : 0,
 
6586
                                    key_part->length,
 
6587
                                    keyuse->val);
 
6588
  }
 
6589
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
 
6590
           (keyuse->val->type() == Item::REF_ITEM &&
 
6591
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
 
6592
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
 
6593
             Item_ref::DIRECT_REF && 
 
6594
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
 
6595
    return new store_key_field(thd,
 
6596
                               key_part->field,
 
6597
                               key_buff + maybe_null,
 
6598
                               maybe_null ? key_buff : 0,
 
6599
                               key_part->length,
 
6600
                               ((Item_field*) keyuse->val->real_item())->field,
 
6601
                               keyuse->val->full_name());
 
6602
  return new store_key_item(thd,
 
6603
                            key_part->field,
 
6604
                            key_buff + maybe_null,
 
6605
                            maybe_null ? key_buff : 0,
 
6606
                            key_part->length,
 
6607
                            keyuse->val);
 
6608
}
 
6609
 
 
6610
/**
 
6611
  This function is only called for const items on fields which are keys.
 
6612
 
 
6613
  @return
 
6614
    returns 1 if there was some conversion made when the field was stored.
 
6615
*/
 
6616
 
 
6617
bool
 
6618
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
6619
{
 
6620
  bool error;
 
6621
  TABLE *table= field->table;
 
6622
  THD *thd= table->in_use;
 
6623
  ha_rows cuted_fields=thd->cuted_fields;
 
6624
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
 
6625
                                                   table->write_set);
 
6626
 
 
6627
  /*
 
6628
    we should restore old value of count_cuted_fields because
 
6629
    store_val_in_field can be called from mysql_insert 
 
6630
    with select_insert, which make count_cuted_fields= 1
 
6631
   */
 
6632
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
 
6633
  thd->count_cuted_fields= check_flag;
 
6634
  error= item->save_in_field(field, 1);
 
6635
  thd->count_cuted_fields= old_count_cuted_fields;
 
6636
  dbug_tmp_restore_column_map(table->write_set, old_map);
 
6637
  return error || cuted_fields != thd->cuted_fields;
 
6638
}
 
6639
 
 
6640
 
 
6641
static bool
 
6642
make_simple_join(JOIN *join,TABLE *tmp_table)
 
6643
{
 
6644
  TABLE **tableptr;
 
6645
  JOIN_TAB *join_tab;
 
6646
 
 
6647
  /*
 
6648
    Reuse TABLE * and JOIN_TAB if already allocated by a previous call
 
6649
    to this function through JOIN::exec (may happen for sub-queries).
 
6650
  */
 
6651
  if (!join->table_reexec)
 
6652
  {
 
6653
    if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*))))
 
6654
      return(true);                        /* purecov: inspected */
 
6655
    if (join->tmp_join)
 
6656
      join->tmp_join->table_reexec= join->table_reexec;
 
6657
  }
 
6658
  if (!join->join_tab_reexec)
 
6659
  {
 
6660
    if (!(join->join_tab_reexec=
 
6661
          (JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
 
6662
      return(true);                        /* purecov: inspected */
 
6663
    if (join->tmp_join)
 
6664
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
 
6665
  }
 
6666
  tableptr= join->table_reexec;
 
6667
  join_tab= join->join_tab_reexec;
 
6668
 
 
6669
  join->join_tab=join_tab;
 
6670
  join->table=tableptr; tableptr[0]=tmp_table;
 
6671
  join->tables=1;
 
6672
  join->const_tables=0;
 
6673
  join->const_table_map=0;
 
6674
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
 
6675
    join->tmp_table_param.func_count=0;
 
6676
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
 
6677
  join->first_record=join->sort_and_group=0;
 
6678
  join->send_records=(ha_rows) 0;
 
6679
  join->group=0;
 
6680
  join->row_limit=join->unit->select_limit_cnt;
 
6681
  join->do_send_rows = (join->row_limit) ? 1 : 0;
 
6682
 
 
6683
  join_tab->cache.buff=0;                       /* No caching */
 
6684
  join_tab->table=tmp_table;
 
6685
  join_tab->select=0;
 
6686
  join_tab->select_cond=0;
 
6687
  join_tab->quick=0;
 
6688
  join_tab->type= JT_ALL;                       /* Map through all records */
 
6689
  join_tab->keys.init();
 
6690
  join_tab->keys.set_all();                     /* test everything in quick */
 
6691
  join_tab->info=0;
 
6692
  join_tab->on_expr_ref=0;
 
6693
  join_tab->last_inner= 0;
 
6694
  join_tab->first_unmatched= 0;
 
6695
  join_tab->ref.key = -1;
 
6696
  join_tab->not_used_in_distinct=0;
 
6697
  join_tab->read_first_record= join_init_read_record;
 
6698
  join_tab->join=join;
 
6699
  join_tab->ref.key_parts= 0;
 
6700
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
 
6701
  join_tab->do_firstmatch= NULL;
 
6702
  memset((char*) &join_tab->read_record, 0, sizeof(join_tab->read_record));
 
6703
  tmp_table->status=0;
 
6704
  tmp_table->null_row=0;
 
6705
  return(false);
 
6706
}
 
6707
 
 
6708
 
 
6709
inline void add_cond_and_fix(Item **e1, Item *e2)
 
6710
{
 
6711
  if (*e1)
 
6712
  {
 
6713
    Item *res;
 
6714
    if ((res= new Item_cond_and(*e1, e2)))
 
6715
    {
 
6716
      *e1= res;
 
6717
      res->quick_fix_field();
 
6718
    }
 
6719
  }
 
6720
  else
 
6721
    *e1= e2;
 
6722
}
 
6723
 
1091
6724
 
1092
6725
/**
1093
6726
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
1102
6735
    add "t1.field IS NOT NULL" to t1's table condition. @n
1103
6736
 
1104
6737
    Description of the optimization:
1105
 
 
 
6738
    
1106
6739
      We look through equalities choosen to perform ref/eq_ref access,
1107
6740
      pick equalities that have form "tbl.part_of_key = othertbl.field"
1108
6741
      (where othertbl is a non-const table and othertbl.field may be NULL)
1130
6763
      This optimization doesn't affect the choices that ref, range, or join
1131
6764
      optimizer make. This was intentional because this was added after 4.1
1132
6765
      was GA.
1133
 
 
 
6766
      
1134
6767
    Implementation overview
1135
6768
      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.
 
6769
         predicates in in KEY_FIELD::null_rejecting
 
6770
      1.1 add_key_part saves these to KEYUSE.
 
6771
      2. create_ref_for_key copies them to TABLE_REF.
1139
6772
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
1140
 
         appropiate JoinTable members.
 
6773
         appropiate JOIN_TAB members.
1141
6774
*/
1142
 
void add_not_null_conds(Join *join)
 
6775
 
 
6776
static void add_not_null_conds(JOIN *join)
1143
6777
{
1144
 
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
6778
  for (uint i=join->const_tables ; i < join->tables ; i++)
1145
6779
  {
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) &&
 
6780
    JOIN_TAB *tab=join->join_tab+i;
 
6781
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
 
6782
         tab->type == JT_REF_OR_NULL) &&
1149
6783
        !tab->table->maybe_null)
1150
6784
    {
1151
 
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
 
6785
      for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
1152
6786
      {
1153
6787
        if (tab->ref.null_rejecting & (1 << keypart))
1154
6788
        {
1156
6790
          Item *notnull;
1157
6791
          assert(item->type() == Item::FIELD_ITEM);
1158
6792
          Item_field *not_null_item= (Item_field*)item;
1159
 
          JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
 
6793
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
1160
6794
          /*
1161
6795
            For UPDATE queries such as:
1162
6796
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1168
6802
            return;
1169
6803
          /*
1170
6804
            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
 
6805
            notnull->const_item(). This is needed e.g. by test_quick_select 
 
6806
            when it is called from make_join_select after this function is 
1173
6807
            called.
1174
6808
          */
1175
 
          if (notnull->fix_fields(join->session, &notnull))
 
6809
          if (notnull->fix_fields(join->thd, &notnull))
1176
6810
            return;
1177
6811
          add_cond_and_fix(&referred_tab->select_cond, notnull);
1178
6812
        }
1197
6831
    -  pointer to the guarded predicate, if success
1198
6832
    -  0, otherwise
1199
6833
*/
1200
 
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
 
6834
 
 
6835
static COND*
 
6836
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
1201
6837
{
1202
6838
  COND *tmp;
1203
6839
  assert(cond != 0);
1213
6849
  return tmp;
1214
6850
}
1215
6851
 
 
6852
 
 
6853
/**
 
6854
  Fill in outer join related info for the execution plan structure.
 
6855
 
 
6856
    For each outer join operation left after simplification of the
 
6857
    original query the function set up the following pointers in the linear
 
6858
    structure join->join_tab representing the selected execution plan.
 
6859
    The first inner table t0 for the operation is set to refer to the last
 
6860
    inner table tk through the field t0->last_inner.
 
6861
    Any inner table ti for the operation are set to refer to the first
 
6862
    inner table ti->first_inner.
 
6863
    The first inner table t0 for the operation is set to refer to the
 
6864
    first inner table of the embedding outer join operation, if there is any,
 
6865
    through the field t0->first_upper.
 
6866
    The on expression for the outer join operation is attached to the
 
6867
    corresponding first inner table through the field t0->on_expr_ref.
 
6868
    Here ti are structures of the JOIN_TAB type.
 
6869
 
 
6870
  EXAMPLE. For the query: 
 
6871
  @code
 
6872
        SELECT * FROM t1
 
6873
                      LEFT JOIN
 
6874
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
 
6875
                      ON (t1.a=t2.a AND t1.b=t3.b)
 
6876
          WHERE t1.c > 5,
 
6877
  @endcode
 
6878
 
 
6879
    given the execution plan with the table order t1,t2,t3,t4
 
6880
    is selected, the following references will be set;
 
6881
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
 
6882
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
 
6883
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to 
 
6884
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
 
6885
 
 
6886
  @param join   reference to the info fully describing the query
 
6887
 
 
6888
  @note
 
6889
    The function assumes that the simplification procedure has been
 
6890
    already applied to the join query (see simplify_joins).
 
6891
    This function can be called only after the execution plan
 
6892
    has been chosen.
 
6893
*/
 
6894
 
 
6895
static void
 
6896
make_outerjoin_info(JOIN *join)
 
6897
{
 
6898
  for (uint i=join->const_tables ; i < join->tables ; i++)
 
6899
  {
 
6900
    JOIN_TAB *tab=join->join_tab+i;
 
6901
    TABLE *table=tab->table;
 
6902
    TABLE_LIST *tbl= table->pos_in_table_list;
 
6903
    TABLE_LIST *embedding= tbl->embedding;
 
6904
 
 
6905
    if (tbl->outer_join)
 
6906
    {
 
6907
      /* 
 
6908
        Table tab is the only one inner table for outer join.
 
6909
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
 
6910
        is in the query above.)
 
6911
      */
 
6912
      tab->last_inner= tab->first_inner= tab;
 
6913
      tab->on_expr_ref= &tbl->on_expr;
 
6914
      tab->cond_equal= tbl->cond_equal;
 
6915
      if (embedding)
 
6916
        tab->first_upper= embedding->nested_join->first_nested;
 
6917
    }    
 
6918
    for ( ; embedding ; embedding= embedding->embedding)
 
6919
    {
 
6920
      /* Ignore sj-nests: */
 
6921
      if (!embedding->on_expr)
 
6922
        continue;
 
6923
      NESTED_JOIN *nested_join= embedding->nested_join;
 
6924
      if (!nested_join->counter_)
 
6925
      {
 
6926
        /* 
 
6927
          Table tab is the first inner table for nested_join.
 
6928
          Save reference to it in the nested join structure.
 
6929
        */ 
 
6930
        nested_join->first_nested= tab;
 
6931
        tab->on_expr_ref= &embedding->on_expr;
 
6932
        tab->cond_equal= tbl->cond_equal;
 
6933
        if (embedding->embedding)
 
6934
          tab->first_upper= embedding->embedding->nested_join->first_nested;
 
6935
      }
 
6936
      if (!tab->first_inner)  
 
6937
        tab->first_inner= nested_join->first_nested;
 
6938
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
6939
        break;
 
6940
      /* Table tab is the last inner table for nested join. */
 
6941
      nested_join->first_nested->last_inner= tab;
 
6942
    }
 
6943
  }
 
6944
  return;
 
6945
}
 
6946
 
 
6947
 
 
6948
static bool
 
6949
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
 
6950
{
 
6951
  THD *thd= join->thd;
 
6952
  if (select)
 
6953
  {
 
6954
    add_not_null_conds(join);
 
6955
    table_map used_tables;
 
6956
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
 
6957
    {                        /* there may be a select without a cond. */    
 
6958
      if (join->tables > 1)
 
6959
        cond->update_used_tables();             // Tablenr may have changed
 
6960
      if (join->const_tables == join->tables &&
 
6961
          thd->lex->current_select->master_unit() ==
 
6962
          &thd->lex->unit)              // not upper level SELECT
 
6963
        join->const_table_map|=RAND_TABLE_BIT;
 
6964
      {                                         // Check const tables
 
6965
        COND *const_cond=
 
6966
          make_cond_for_table(cond,
 
6967
                              join->const_table_map,
 
6968
                              (table_map) 0, 1);
 
6969
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
 
6970
             tab < join->join_tab+join->tables ; tab++)
 
6971
        {
 
6972
          if (*tab->on_expr_ref)
 
6973
          {
 
6974
            JOIN_TAB *cond_tab= tab->first_inner;
 
6975
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
 
6976
                                           join->const_table_map,
 
6977
                                           (  table_map) 0, 0);
 
6978
            if (!tmp)
 
6979
              continue;
 
6980
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
6981
            if (!tmp)
 
6982
              return(1);
 
6983
            tmp->quick_fix_field();
 
6984
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
6985
                                    new Item_cond_and(cond_tab->select_cond,
 
6986
                                                      tmp);
 
6987
            if (!cond_tab->select_cond)
 
6988
              return(1);
 
6989
            cond_tab->select_cond->quick_fix_field();
 
6990
          }       
 
6991
        }
 
6992
        if (const_cond && !const_cond->val_int())
 
6993
        {
 
6994
          return(1);     // Impossible const condition
 
6995
        }
 
6996
      }
 
6997
    }
 
6998
    used_tables=((select->const_tables=join->const_table_map) |
 
6999
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7000
    for (uint i=join->const_tables ; i < join->tables ; i++)
 
7001
    {
 
7002
      JOIN_TAB *tab=join->join_tab+i;
 
7003
      /*
 
7004
        first_inner is the X in queries like:
 
7005
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
 
7006
      */
 
7007
      JOIN_TAB *first_inner_tab= tab->first_inner; 
 
7008
      table_map current_map= tab->table->map;
 
7009
      bool use_quick_range=0;
 
7010
      COND *tmp;
 
7011
 
 
7012
      /*
 
7013
        Following force including random expression in last table condition.
 
7014
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
 
7015
      */
 
7016
      if (i == join->tables-1)
 
7017
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
 
7018
      used_tables|=current_map;
 
7019
 
 
7020
      if (tab->type == JT_REF && tab->quick &&
 
7021
          (uint) tab->ref.key == tab->quick->index &&
 
7022
          tab->ref.key_length < tab->quick->max_used_key_length)
 
7023
      {
 
7024
        /* Range uses longer key;  Use this instead of ref on key */
 
7025
        tab->type=JT_ALL;
 
7026
        use_quick_range=1;
 
7027
        tab->use_quick=1;
 
7028
        tab->ref.key= -1;
 
7029
        tab->ref.key_parts=0;           // Don't use ref key.
 
7030
        join->best_positions[i].records_read= rows2double(tab->quick->records);
 
7031
        /* 
 
7032
          We will use join cache here : prevent sorting of the first
 
7033
          table only and sort at the end.
 
7034
        */
 
7035
        if (i != join->const_tables && join->tables > join->const_tables + 1)
 
7036
          join->full_join= 1;
 
7037
      }
 
7038
 
 
7039
      tmp= NULL;
 
7040
      if (cond)
 
7041
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
 
7042
      if (cond && !tmp && tab->quick)
 
7043
      {                                         // Outer join
 
7044
        if (tab->type != JT_ALL)
 
7045
        {
 
7046
          /*
 
7047
            Don't use the quick method
 
7048
            We come here in the case where we have 'key=constant' and
 
7049
            the test is removed by make_cond_for_table()
 
7050
          */
 
7051
          delete tab->quick;
 
7052
          tab->quick= 0;
 
7053
        }
 
7054
        else
 
7055
        {
 
7056
          /*
 
7057
            Hack to handle the case where we only refer to a table
 
7058
            in the ON part of an OUTER JOIN. In this case we want the code
 
7059
            below to check if we should use 'quick' instead.
 
7060
          */
 
7061
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
7062
        }
 
7063
 
 
7064
      }
 
7065
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
 
7066
          tab->type == JT_EQ_REF)
 
7067
      {
 
7068
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
 
7069
                                       thd->memdup((uchar*) select,
 
7070
                                                   sizeof(*select)));
 
7071
        if (!sel)
 
7072
          return(1);                    // End of memory
 
7073
        /*
 
7074
          If tab is an inner table of an outer join operation,
 
7075
          add a match guard to the pushed down predicate.
 
7076
          The guard will turn the predicate on only after
 
7077
          the first match for outer tables is encountered.
 
7078
        */        
 
7079
        if (cond && tmp)
 
7080
        {
 
7081
          /*
 
7082
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
 
7083
            a cond, so neutralize the hack above.
 
7084
          */
 
7085
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
 
7086
            return(1);
 
7087
          tab->select_cond=sel->cond=tmp;
 
7088
          /* Push condition to storage engine if this is enabled
 
7089
             and the condition is not guarded */
 
7090
          tab->table->file->pushed_cond= NULL;
 
7091
          if (thd->variables.engine_condition_pushdown)
 
7092
          {
 
7093
            COND *push_cond= 
 
7094
              make_cond_for_table(tmp, current_map, current_map, 0);
 
7095
            if (push_cond)
 
7096
            {
 
7097
              /* Push condition to handler */
 
7098
              if (!tab->table->file->cond_push(push_cond))
 
7099
                tab->table->file->pushed_cond= push_cond;
 
7100
            }
 
7101
          }
 
7102
        }
 
7103
        else
 
7104
          tab->select_cond= sel->cond= NULL;
 
7105
 
 
7106
        sel->head=tab->table;
 
7107
        if (tab->quick)
 
7108
        {
 
7109
          /* Use quick key read if it's a constant and it's not used
 
7110
             with key reading */
 
7111
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
 
7112
              && (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
 
7113
          {
 
7114
            sel->quick=tab->quick;              // Use value from get_quick_...
 
7115
            sel->quick_keys.clear_all();
 
7116
            sel->needed_reg.clear_all();
 
7117
          }
 
7118
          else
 
7119
          {
 
7120
            delete tab->quick;
 
7121
          }
 
7122
          tab->quick=0;
 
7123
        }
 
7124
        uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
 
7125
        if (i == join->const_tables && ref_key)
 
7126
        {
 
7127
          if (!tab->const_keys.is_clear_all() &&
 
7128
              tab->table->reginfo.impossible_range)
 
7129
            return(1);
 
7130
        }
 
7131
        else if (tab->type == JT_ALL && ! use_quick_range)
 
7132
        {
 
7133
          if (!tab->const_keys.is_clear_all() &&
 
7134
              tab->table->reginfo.impossible_range)
 
7135
            return(1);                          // Impossible range
 
7136
          /*
 
7137
            We plan to scan all rows.
 
7138
            Check again if we should use an index.
 
7139
            We could have used an column from a previous table in
 
7140
            the index if we are using limit and this is the first table
 
7141
          */
 
7142
 
 
7143
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
 
7144
              (!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)))
 
7145
          {
 
7146
            /* Join with outer join condition */
 
7147
            COND *orig_cond=sel->cond;
 
7148
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
 
7149
 
 
7150
            /*
 
7151
              We can't call sel->cond->fix_fields,
 
7152
              as it will break tab->on_expr if it's AND condition
 
7153
              (fix_fields currently removes extra AND/OR levels).
 
7154
              Yet attributes of the just built condition are not needed.
 
7155
              Thus we call sel->cond->quick_fix_field for safety.
 
7156
            */
 
7157
            if (sel->cond && !sel->cond->fixed)
 
7158
              sel->cond->quick_fix_field();
 
7159
 
 
7160
            if (sel->test_quick_select(thd, tab->keys,
 
7161
                                       used_tables & ~ current_map,
 
7162
                                       (join->select_options &
 
7163
                                        OPTION_FOUND_ROWS ?
 
7164
                                        HA_POS_ERROR :
 
7165
                                        join->unit->select_limit_cnt), 0,
 
7166
                                        false) < 0)
 
7167
            {
 
7168
              /*
 
7169
                Before reporting "Impossible WHERE" for the whole query
 
7170
                we have to check isn't it only "impossible ON" instead
 
7171
              */
 
7172
              sel->cond=orig_cond;
 
7173
              if (!*tab->on_expr_ref ||
 
7174
                  sel->test_quick_select(thd, tab->keys,
 
7175
                                         used_tables & ~ current_map,
 
7176
                                         (join->select_options &
 
7177
                                          OPTION_FOUND_ROWS ?
 
7178
                                          HA_POS_ERROR :
 
7179
                                          join->unit->select_limit_cnt),0,
 
7180
                                          false) < 0)
 
7181
                return(1);                      // Impossible WHERE
 
7182
            }
 
7183
            else
 
7184
              sel->cond=orig_cond;
 
7185
 
 
7186
            /* Fix for EXPLAIN */
 
7187
            if (sel->quick)
 
7188
              join->best_positions[i].records_read= (double)sel->quick->records;
 
7189
          }
 
7190
          else
 
7191
          {
 
7192
            sel->needed_reg=tab->needed_reg;
 
7193
            sel->quick_keys.clear_all();
 
7194
          }
 
7195
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
 
7196
              !sel->needed_reg.is_subset(tab->checked_keys))
 
7197
          {
 
7198
            tab->keys=sel->quick_keys;
 
7199
            tab->keys.merge(sel->needed_reg);
 
7200
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
 
7201
                             (select->quick_keys.is_clear_all() ||
 
7202
                              (select->quick &&
 
7203
                               (select->quick->records >= 100L)))) ?
 
7204
              2 : 1;
 
7205
            sel->read_tables= used_tables & ~current_map;
 
7206
          }
 
7207
          if (i != join->const_tables && tab->use_quick != 2)
 
7208
          {                                     /* Read with cache */
 
7209
            if (cond &&
 
7210
                (tmp=make_cond_for_table(cond,
 
7211
                                         join->const_table_map |
 
7212
                                         current_map,
 
7213
                                         current_map, 0)))
 
7214
            {
 
7215
              tab->cache.select=(SQL_SELECT*)
 
7216
                thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
 
7217
              tab->cache.select->cond=tmp;
 
7218
              tab->cache.select->read_tables=join->const_table_map;
 
7219
            }
 
7220
          }
 
7221
        }
 
7222
      }
 
7223
      
 
7224
      /* 
 
7225
        Push down conditions from all on expressions.
 
7226
        Each of these conditions are guarded by a variable
 
7227
        that turns if off just before null complemented row for
 
7228
        outer joins is formed. Thus, the condition from an
 
7229
        'on expression' are guaranteed not to be checked for
 
7230
        the null complemented row.
 
7231
      */ 
 
7232
 
 
7233
      /* First push down constant conditions from on expressions */
 
7234
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
 
7235
           join_tab < join->join_tab+join->tables ; join_tab++)
 
7236
      {
 
7237
        if (*join_tab->on_expr_ref)
 
7238
        {
 
7239
          JOIN_TAB *cond_tab= join_tab->first_inner;
 
7240
          COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
 
7241
                                         join->const_table_map,
 
7242
                                         (table_map) 0, 0);
 
7243
          if (!tmp)
 
7244
            continue;
 
7245
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
7246
          if (!tmp)
 
7247
            return(1);
 
7248
          tmp->quick_fix_field();
 
7249
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
7250
                                    new Item_cond_and(cond_tab->select_cond,tmp);
 
7251
          if (!cond_tab->select_cond)
 
7252
            return(1);
 
7253
          cond_tab->select_cond->quick_fix_field();
 
7254
        }       
 
7255
      }
 
7256
 
 
7257
      /* Push down non-constant conditions from on expressions */
 
7258
      JOIN_TAB *last_tab= tab;
 
7259
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
 
7260
      {  
 
7261
        /* 
 
7262
          Table tab is the last inner table of an outer join.
 
7263
          An on expression is always attached to it.
 
7264
        */     
 
7265
        COND *on_expr= *first_inner_tab->on_expr_ref;
 
7266
 
 
7267
        table_map used_tables2= (join->const_table_map |
 
7268
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7269
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
 
7270
        {
 
7271
          current_map= tab->table->map;
 
7272
          used_tables2|= current_map;
 
7273
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
 
7274
                                              current_map, 0);
 
7275
          if (tmp_cond)
 
7276
          {
 
7277
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
 
7278
            /*
 
7279
              First add the guards for match variables of
 
7280
              all embedding outer join operations.
 
7281
            */
 
7282
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
 
7283
                                                     tmp_cond,
 
7284
                                                     first_inner_tab)))
 
7285
              return(1);
 
7286
            /* 
 
7287
              Now add the guard turning the predicate off for 
 
7288
              the null complemented row.
 
7289
            */ 
 
7290
            tmp_cond= new Item_func_trig_cond(tmp_cond,
 
7291
                                              &first_inner_tab->
 
7292
                                              not_null_compl);
 
7293
            if (tmp_cond)
 
7294
              tmp_cond->quick_fix_field();
 
7295
            /* Add the predicate to other pushed down predicates */
 
7296
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
 
7297
                                  new Item_cond_and(cond_tab->select_cond,
 
7298
                                                    tmp_cond);
 
7299
            if (!cond_tab->select_cond)
 
7300
              return(1);
 
7301
            cond_tab->select_cond->quick_fix_field();
 
7302
          }              
 
7303
        }
 
7304
        first_inner_tab= first_inner_tab->first_upper;       
 
7305
      }
 
7306
    }
 
7307
  }
 
7308
  return(0);
 
7309
}
 
7310
 
 
7311
 
 
7312
/* 
 
7313
  Check if given expression uses only table fields covered by the given index
 
7314
 
 
7315
  SYNOPSIS
 
7316
    uses_index_fields_only()
 
7317
      item           Expression to check
 
7318
      tbl            The table having the index
 
7319
      keyno          The index number
 
7320
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7321
 
 
7322
  DESCRIPTION
 
7323
    Check if given expression only uses fields covered by index #keyno in the
 
7324
    table tbl. The expression can use any fields in any other tables.
 
7325
    
 
7326
    The expression is guaranteed not to be AND or OR - those constructs are 
 
7327
    handled outside of this function.
 
7328
 
 
7329
  RETURN
 
7330
    true   Yes
 
7331
    false  No
 
7332
*/
 
7333
 
 
7334
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno, 
 
7335
                            bool other_tbls_ok)
 
7336
{
 
7337
  if (item->const_item())
 
7338
    return true;
 
7339
 
 
7340
  /* 
 
7341
    Don't push down the triggered conditions. Nested outer joins execution 
 
7342
    code may need to evaluate a condition several times (both triggered and
 
7343
    untriggered), and there is no way to put thi
 
7344
    TODO: Consider cloning the triggered condition and using the copies for:
 
7345
      1. push the first copy down, to have most restrictive index condition
 
7346
         possible
 
7347
      2. Put the second copy into tab->select_cond. 
 
7348
  */
 
7349
  if (item->type() == Item::FUNC_ITEM && 
 
7350
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
 
7351
    return false;
 
7352
 
 
7353
  if (!(item->used_tables() & tbl->map))
 
7354
    return other_tbls_ok;
 
7355
 
 
7356
  Item::Type item_type= item->type();
 
7357
  switch (item_type) {
 
7358
  case Item::FUNC_ITEM:
 
7359
    {
 
7360
      /* This is a function, apply condition recursively to arguments */
 
7361
      Item_func *item_func= (Item_func*)item;
 
7362
      Item **child;
 
7363
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
 
7364
      for (child= item_func->arguments(); child != item_end; child++)
 
7365
      {
 
7366
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
 
7367
          return false;
 
7368
      }
 
7369
      return true;
 
7370
    }
 
7371
  case Item::COND_ITEM:
 
7372
    {
 
7373
      /* This is a function, apply condition recursively to arguments */
 
7374
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
 
7375
      Item *item;
 
7376
      while ((item=li++))
 
7377
      {
 
7378
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
 
7379
          return false;
 
7380
      }
 
7381
      return true;
 
7382
    }
 
7383
  case Item::FIELD_ITEM:
 
7384
    {
 
7385
      Item_field *item_field= (Item_field*)item;
 
7386
      if (item_field->field->table != tbl) 
 
7387
        return true;
 
7388
      return item_field->field->part_of_key.is_set(keyno);
 
7389
    }
 
7390
  case Item::REF_ITEM:
 
7391
    return uses_index_fields_only(item->real_item(), tbl, keyno,
 
7392
                                  other_tbls_ok);
 
7393
  default:
 
7394
    return false; /* Play it safe, don't push unknown non-const items */
 
7395
  }
 
7396
}
 
7397
 
 
7398
 
1216
7399
#define ICP_COND_USES_INDEX_ONLY 10
1217
7400
 
1218
 
 
1219
 
/**
1220
 
  cleanup JoinTable.
1221
 
*/
1222
 
void JoinTable::cleanup()
1223
 
{
1224
 
  safe_delete(select);
1225
 
  safe_delete(quick);
1226
 
 
1227
 
  if (cache.buff)
1228
 
  {
1229
 
    size_t size= cache.end - cache.buff;
1230
 
    global_join_buffer.sub(size);
1231
 
    free(cache.buff);
1232
 
  }
 
7401
/*
 
7402
  Get a part of the condition that can be checked using only index fields
 
7403
 
 
7404
  SYNOPSIS
 
7405
    make_cond_for_index()
 
7406
      cond           The source condition
 
7407
      table          The table that is partially available
 
7408
      keyno          The index in the above table. Only fields covered by the index
 
7409
                     are available
 
7410
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7411
 
 
7412
  DESCRIPTION
 
7413
    Get a part of the condition that can be checked when for the given table 
 
7414
    we have values only of fields covered by some index. The condition may
 
7415
    refer to other tables, it is assumed that we have values of all of their 
 
7416
    fields.
 
7417
 
 
7418
    Example:
 
7419
      make_cond_for_index(
 
7420
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
 
7421
          t2, keyno(t2.key1)) 
 
7422
      will return
 
7423
        "cond(t1.field) AND cond(t2.key2)"
 
7424
 
 
7425
  RETURN
 
7426
    Index condition, or NULL if no condition could be inferred.
 
7427
*/
 
7428
 
 
7429
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
 
7430
                          bool other_tbls_ok)
 
7431
{
 
7432
  if (!cond)
 
7433
    return NULL;
 
7434
  if (cond->type() == Item::COND_ITEM)
 
7435
  {
 
7436
    uint n_marked= 0;
 
7437
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7438
    {
 
7439
      Item_cond_and *new_cond=new Item_cond_and;
 
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
          new_cond->argument_list()->push_back(fix);
 
7449
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7450
      }
 
7451
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7452
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7453
      switch (new_cond->argument_list()->elements) {
 
7454
      case 0:
 
7455
        return (COND*) 0;
 
7456
      case 1:
 
7457
        return new_cond->argument_list()->head();
 
7458
      default:
 
7459
        new_cond->quick_fix_field();
 
7460
        return new_cond;
 
7461
      }
 
7462
    }
 
7463
    else /* It's OR */
 
7464
    {
 
7465
      Item_cond_or *new_cond=new Item_cond_or;
 
7466
      if (!new_cond)
 
7467
        return (COND*) 0;
 
7468
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7469
      Item *item;
 
7470
      while ((item=li++))
 
7471
      {
 
7472
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7473
        if (!fix)
 
7474
          return (COND*) 0;
 
7475
        new_cond->argument_list()->push_back(fix);
 
7476
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7477
      }
 
7478
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7479
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7480
      new_cond->quick_fix_field();
 
7481
      new_cond->top_level_item();
 
7482
      return new_cond;
 
7483
    }
 
7484
  }
 
7485
 
 
7486
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
 
7487
    return (COND*) 0;
 
7488
  cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7489
  return cond;
 
7490
}
 
7491
 
 
7492
 
 
7493
Item *make_cond_remainder(Item *cond, bool exclude_index)
 
7494
{
 
7495
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
 
7496
    return 0; /* Already checked */
 
7497
 
 
7498
  if (cond->type() == Item::COND_ITEM)
 
7499
  {
 
7500
    table_map tbl_map= 0;
 
7501
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7502
    {
 
7503
      /* Create new top level AND item */
 
7504
      Item_cond_and *new_cond=new Item_cond_and;
 
7505
      if (!new_cond)
 
7506
        return (COND*) 0;
 
7507
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7508
      Item *item;
 
7509
      while ((item=li++))
 
7510
      {
 
7511
        Item *fix= make_cond_remainder(item, exclude_index);
 
7512
        if (fix)
 
7513
        {
 
7514
          new_cond->argument_list()->push_back(fix);
 
7515
          tbl_map |= fix->used_tables();
 
7516
        }
 
7517
      }
 
7518
      switch (new_cond->argument_list()->elements) {
 
7519
      case 0:
 
7520
        return (COND*) 0;
 
7521
      case 1:
 
7522
        return new_cond->argument_list()->head();
 
7523
      default:
 
7524
        new_cond->quick_fix_field();
 
7525
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7526
        return new_cond;
 
7527
      }
 
7528
    }
 
7529
    else /* It's OR */
 
7530
    {
 
7531
      Item_cond_or *new_cond=new Item_cond_or;
 
7532
      if (!new_cond)
 
7533
        return (COND*) 0;
 
7534
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7535
      Item *item;
 
7536
      while ((item=li++))
 
7537
      {
 
7538
        Item *fix= make_cond_remainder(item, false);
 
7539
        if (!fix)
 
7540
          return (COND*) 0;
 
7541
        new_cond->argument_list()->push_back(fix);
 
7542
        tbl_map |= fix->used_tables();
 
7543
      }
 
7544
      new_cond->quick_fix_field();
 
7545
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7546
      new_cond->top_level_item();
 
7547
      return new_cond;
 
7548
    }
 
7549
  }
 
7550
  return cond;
 
7551
}
 
7552
 
 
7553
 
 
7554
/*
 
7555
  Try to extract and push the index condition
 
7556
 
 
7557
  SYNOPSIS
 
7558
    push_index_cond()
 
7559
      tab            A join tab that has tab->table->file and its condition
 
7560
                     in tab->select_cond
 
7561
      keyno          Index for which extract and push the condition
 
7562
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7563
 
 
7564
  DESCRIPTION
 
7565
    Try to extract and push the index condition down to table handler
 
7566
*/
 
7567
 
 
7568
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
 
7569
{
 
7570
  Item *idx_cond;
 
7571
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
 
7572
      tab->join->thd->variables.engine_condition_pushdown)
 
7573
  {
 
7574
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
 
7575
                                  other_tbls_ok);
 
7576
 
 
7577
    if (idx_cond)
 
7578
    {
 
7579
      tab->pre_idx_push_select_cond= tab->select_cond;
 
7580
      Item *idx_remainder_cond= 
 
7581
        tab->table->file->idx_cond_push(keyno, idx_cond);
 
7582
 
 
7583
      /*
 
7584
        Disable eq_ref's "lookup cache" if we've pushed down an index
 
7585
        condition. 
 
7586
        TODO: This check happens to work on current ICP implementations, but
 
7587
        there may exist a compliant implementation that will not work 
 
7588
        correctly with it. Sort this out when we stabilize the condition
 
7589
        pushdown APIs.
 
7590
      */
 
7591
      if (idx_remainder_cond != idx_cond)
 
7592
        tab->ref.disable_cache= true;
 
7593
 
 
7594
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
 
7595
 
 
7596
      if (row_cond)
 
7597
      {
 
7598
        if (!idx_remainder_cond)
 
7599
          tab->select_cond= row_cond;
 
7600
        else
 
7601
        {
 
7602
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
 
7603
          tab->select_cond->quick_fix_field();
 
7604
          ((Item_cond_and*)tab->select_cond)->used_tables_cache= 
 
7605
            row_cond->used_tables() | idx_remainder_cond->used_tables();
 
7606
        }
 
7607
      }
 
7608
      else
 
7609
        tab->select_cond= idx_remainder_cond;
 
7610
      if (tab->select)
 
7611
      {
 
7612
        tab->select->cond= tab->select_cond;
 
7613
      }
 
7614
    }
 
7615
  }
 
7616
  return;
 
7617
}
 
7618
 
 
7619
 
 
7620
 
 
7621
    /*
 
7622
      Determine if the set is already ordered for ORDER BY, so it can 
 
7623
      disable join cache because it will change the ordering of the results.
 
7624
      Code handles sort table that is at any location (not only first after 
 
7625
      the const tables) despite the fact that it's currently prohibited.
 
7626
      We must disable join cache if the first non-const table alone is
 
7627
      ordered. If there is a temp table the ordering is done as a last
 
7628
      operation and doesn't prevent join cache usage.
 
7629
    */
 
7630
uint make_join_orderinfo(JOIN *join)
 
7631
{
 
7632
  uint i;
 
7633
  if (join->need_tmp)
 
7634
    return join->tables;
 
7635
 
 
7636
  for (i=join->const_tables ; i < join->tables ; i++)
 
7637
  {
 
7638
    JOIN_TAB *tab=join->join_tab+i;
 
7639
    TABLE *table=tab->table;
 
7640
    if ((table == join->sort_by_table && 
 
7641
         (!join->order || join->skip_sort_order)) ||
 
7642
        (join->sort_by_table == (TABLE *) 1 && i != join->const_tables))
 
7643
    {
 
7644
      break;
 
7645
    }
 
7646
  }
 
7647
  return i;
 
7648
}
 
7649
 
 
7650
 
 
7651
/*
 
7652
  Plan refinement stage: do various set ups for the executioner
 
7653
 
 
7654
  SYNOPSIS
 
7655
    make_join_readinfo()
 
7656
      join           Join being processed
 
7657
      options        Join's options (checking for SELECT_DESCRIBE, 
 
7658
                     SELECT_NO_JOIN_CACHE)
 
7659
      no_jbuf_after  Don't use join buffering after table with this number.
 
7660
 
 
7661
  DESCRIPTION
 
7662
    Plan refinement stage: do various set ups for the executioner
 
7663
      - set up use of join buffering
 
7664
      - push index conditions
 
7665
      - increment counters
 
7666
      - etc
 
7667
 
 
7668
  RETURN 
 
7669
    false - OK
 
7670
    true  - Out of memory
 
7671
*/
 
7672
 
 
7673
static bool
 
7674
make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after)
 
7675
{
 
7676
  uint i;
 
7677
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
 
7678
  bool sorted= 1;
 
7679
 
 
7680
  for (i=join->const_tables ; i < join->tables ; i++)
 
7681
  {
 
7682
    JOIN_TAB *tab=join->join_tab+i;
 
7683
    TABLE *table=tab->table;
 
7684
    bool using_join_cache;
 
7685
    tab->read_record.table= table;
 
7686
    tab->read_record.file=table->file;
 
7687
    tab->next_select=sub_select;                /* normal select */
 
7688
    /* 
 
7689
      TODO: don't always instruct first table's ref/range access method to 
 
7690
      produce sorted output.
 
7691
    */
 
7692
    tab->sorted= sorted;
 
7693
    sorted= 0;                                  // only first must be sorted
 
7694
    if (tab->insideout_match_tab)
 
7695
    {
 
7696
      if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
 
7697
                                                         [tab->index].
 
7698
                                                         key_length)))
 
7699
        return true;
 
7700
    }
 
7701
    switch (tab->type) {
 
7702
    case JT_SYSTEM:                             // Only happens with left join
 
7703
      table->status=STATUS_NO_RECORD;
 
7704
      tab->read_first_record= join_read_system;
 
7705
      tab->read_record.read_record= join_no_more_records;
 
7706
      break;
 
7707
    case JT_CONST:                              // Only happens with left join
 
7708
      table->status=STATUS_NO_RECORD;
 
7709
      tab->read_first_record= join_read_const;
 
7710
      tab->read_record.read_record= join_no_more_records;
 
7711
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7712
          !table->no_keyread)
 
7713
      {
 
7714
        table->key_read=1;
 
7715
        table->file->extra(HA_EXTRA_KEYREAD);
 
7716
      }
 
7717
      break;
 
7718
    case JT_EQ_REF:
 
7719
      table->status=STATUS_NO_RECORD;
 
7720
      if (tab->select)
 
7721
      {
 
7722
        delete tab->select->quick;
 
7723
        tab->select->quick=0;
 
7724
      }
 
7725
      delete tab->quick;
 
7726
      tab->quick=0;
 
7727
      tab->read_first_record= join_read_key;
 
7728
      tab->read_record.read_record= join_no_more_records;
 
7729
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7730
          !table->no_keyread)
 
7731
      {
 
7732
        table->key_read=1;
 
7733
        table->file->extra(HA_EXTRA_KEYREAD);
 
7734
      }
 
7735
      else
 
7736
        push_index_cond(tab, tab->ref.key, true);
 
7737
      break;
 
7738
    case JT_REF_OR_NULL:
 
7739
    case JT_REF:
 
7740
      table->status=STATUS_NO_RECORD;
 
7741
      if (tab->select)
 
7742
      {
 
7743
        delete tab->select->quick;
 
7744
        tab->select->quick=0;
 
7745
      }
 
7746
      delete tab->quick;
 
7747
      tab->quick=0;
 
7748
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7749
          !table->no_keyread)
 
7750
      {
 
7751
        table->key_read=1;
 
7752
        table->file->extra(HA_EXTRA_KEYREAD);
 
7753
      }
 
7754
      else
 
7755
        push_index_cond(tab, tab->ref.key, true);
 
7756
      if (tab->type == JT_REF)
 
7757
      {
 
7758
        tab->read_first_record= join_read_always_key;
 
7759
        tab->read_record.read_record= tab->insideout_match_tab? 
 
7760
           join_read_next_same_diff : join_read_next_same;
 
7761
      }
 
7762
      else
 
7763
      {
 
7764
        tab->read_first_record= join_read_always_key_or_null;
 
7765
        tab->read_record.read_record= join_read_next_same_or_null;
 
7766
      }
 
7767
      break;
 
7768
    case JT_ALL:
 
7769
      /*
 
7770
        If previous table use cache
 
7771
        If the incoming data set is already sorted don't use cache.
 
7772
      */
 
7773
      table->status=STATUS_NO_RECORD;
 
7774
      using_join_cache= false;
 
7775
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
7776
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
 
7777
          !tab->insideout_match_tab)
 
7778
      {
 
7779
        if ((options & SELECT_DESCRIBE) ||
 
7780
            !join_init_cache(join->thd,join->join_tab+join->const_tables,
 
7781
                             i-join->const_tables))
 
7782
        {
 
7783
          using_join_cache= true;
 
7784
          tab[-1].next_select=sub_select_cache; /* Patch previous */
 
7785
        }
 
7786
      }
 
7787
      /* These init changes read_record */
 
7788
      if (tab->use_quick == 2)
 
7789
      {
 
7790
        join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
 
7791
        tab->read_first_record= join_init_quick_read_record;
 
7792
        if (statistics)
 
7793
          status_var_increment(join->thd->status_var.select_range_check_count);
 
7794
      }
 
7795
      else
 
7796
      {
 
7797
        tab->read_first_record= join_init_read_record;
 
7798
        if (i == join->const_tables)
 
7799
        {
 
7800
          if (tab->select && tab->select->quick)
 
7801
          {
 
7802
            if (statistics)
 
7803
              status_var_increment(join->thd->status_var.select_range_count);
 
7804
          }
 
7805
          else
 
7806
          {
 
7807
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7808
            if (statistics)
 
7809
              status_var_increment(join->thd->status_var.select_scan_count);
 
7810
          }
 
7811
        }
 
7812
        else
 
7813
        {
 
7814
          if (tab->select && tab->select->quick)
 
7815
          {
 
7816
            if (statistics)
 
7817
              status_var_increment(join->thd->status_var.select_full_range_join_count);
 
7818
          }
 
7819
          else
 
7820
          {
 
7821
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7822
            if (statistics)
 
7823
              status_var_increment(join->thd->status_var.select_full_join_count);
 
7824
          }
 
7825
        }
 
7826
        if (!table->no_keyread)
 
7827
        {
 
7828
          if (tab->select && tab->select->quick &&
 
7829
              tab->select->quick->index != MAX_KEY && //not index_merge
 
7830
              table->covering_keys.is_set(tab->select->quick->index))
 
7831
          {
 
7832
            table->key_read=1;
 
7833
            table->file->extra(HA_EXTRA_KEYREAD);
 
7834
          }
 
7835
          else if (!table->covering_keys.is_clear_all() &&
 
7836
                   !(tab->select && tab->select->quick))
 
7837
          {                                     // Only read index tree
 
7838
            if (!tab->insideout_match_tab)
 
7839
            {
 
7840
              /*
 
7841
                See bug #26447: "Using the clustered index for a table scan
 
7842
                is always faster than using a secondary index".
 
7843
              */
 
7844
              if (table->s->primary_key != MAX_KEY &&
 
7845
                  table->file->primary_key_is_clustered())
 
7846
                tab->index= table->s->primary_key;
 
7847
              else
 
7848
                tab->index=find_shortest_key(table, & table->covering_keys);
 
7849
            }
 
7850
            tab->read_first_record= join_read_first;
 
7851
            tab->type=JT_NEXT;          // Read with index_first / index_next
 
7852
          }
 
7853
        }
 
7854
        if (tab->select && tab->select->quick &&
 
7855
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
 
7856
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
 
7857
      }
 
7858
      break;
 
7859
    default:
 
7860
      break;                                    /* purecov: deadcode */
 
7861
    case JT_UNKNOWN:
 
7862
    case JT_MAYBE_REF:
 
7863
      abort();                                  /* purecov: deadcode */
 
7864
    }
 
7865
  }
 
7866
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
 
7867
  return(false);
 
7868
}
 
7869
 
 
7870
 
 
7871
/**
 
7872
  Give error if we some tables are done with a full join.
 
7873
 
 
7874
  This is used by multi_table_update and multi_table_delete when running
 
7875
  in safe mode.
 
7876
 
 
7877
  @param join           Join condition
 
7878
 
 
7879
  @retval
 
7880
    0   ok
 
7881
  @retval
 
7882
    1   Error (full join used)
 
7883
*/
 
7884
 
 
7885
bool error_if_full_join(JOIN *join)
 
7886
{
 
7887
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
 
7888
       tab < end;
 
7889
       tab++)
 
7890
  {
 
7891
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
 
7892
    {
 
7893
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
 
7894
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
 
7895
      return(1);
 
7896
    }
 
7897
  }
 
7898
  return(0);
 
7899
}
 
7900
 
 
7901
 
 
7902
/**
 
7903
  cleanup JOIN_TAB.
 
7904
*/
 
7905
 
 
7906
void JOIN_TAB::cleanup()
 
7907
{
 
7908
  delete select;
 
7909
  select= 0;
 
7910
  delete quick;
 
7911
  quick= 0;
 
7912
  x_free(cache.buff);
1233
7913
  cache.buff= 0;
1234
7914
  limit= 0;
1235
7915
  if (table)
1237
7917
    if (table->key_read)
1238
7918
    {
1239
7919
      table->key_read= 0;
1240
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
7920
      table->file->extra(HA_EXTRA_NO_KEYREAD);
1241
7921
    }
1242
 
    table->cursor->ha_index_or_rnd_end();
 
7922
    table->file->ha_index_or_rnd_end();
1243
7923
    /*
1244
7924
      We need to reset this for next select
1245
7925
      (Tested in part_of_refkey)
1246
7926
    */
1247
7927
    table->reginfo.join_tab= 0;
1248
7928
  }
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
 
}
 
7929
  end_read_record(&read_record);
 
7930
}
 
7931
 
 
7932
 
 
7933
/**
 
7934
  Partially cleanup JOIN after it has executed: close index or rnd read
 
7935
  (table cursors), free quick selects.
 
7936
 
 
7937
    This function is called in the end of execution of a JOIN, before the used
 
7938
    tables are unlocked and closed.
 
7939
 
 
7940
    For a join that is resolved using a temporary table, the first sweep is
 
7941
    performed against actual tables and an intermediate result is inserted
 
7942
    into the temprorary table.
 
7943
    The last sweep is performed against the temporary table. Therefore,
 
7944
    the base tables and associated buffers used to fill the temporary table
 
7945
    are no longer needed, and this function is called to free them.
 
7946
 
 
7947
    For a join that is performed without a temporary table, this function
 
7948
    is called after all rows are sent, but before EOF packet is sent.
 
7949
 
 
7950
    For a simple SELECT with no subqueries this function performs a full
 
7951
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
 
7952
    tables.
 
7953
 
 
7954
    If a JOIN is executed for a subquery or if it has a subquery, we can't
 
7955
    do the full cleanup and need to do a partial cleanup only.
 
7956
    - If a JOIN is not the top level join, we must not unlock the tables
 
7957
    because the outer select may not have been evaluated yet, and we
 
7958
    can't unlock only selected tables of a query.
 
7959
    - Additionally, if this JOIN corresponds to a correlated subquery, we
 
7960
    should not free quick selects and join buffers because they will be
 
7961
    needed for the next execution of the correlated subquery.
 
7962
    - However, if this is a JOIN for a [sub]select, which is not
 
7963
    a correlated subquery itself, but has subqueries, we can free it
 
7964
    fully and also free JOINs of all its subqueries. The exception
 
7965
    is a subquery in SELECT list, e.g: @n
 
7966
    SELECT a, (select max(b) from t1) group by c @n
 
7967
    This subquery will not be evaluated at first sweep and its value will
 
7968
    not be inserted into the temporary table. Instead, it's evaluated
 
7969
    when selecting from the temporary table. Therefore, it can't be freed
 
7970
    here even though it's not correlated.
 
7971
 
 
7972
  @todo
 
7973
    Unlock tables even if the join isn't top level select in the tree
 
7974
*/
 
7975
 
 
7976
void JOIN::join_free()
 
7977
{
 
7978
  SELECT_LEX_UNIT *tmp_unit;
 
7979
  SELECT_LEX *sl;
 
7980
  /*
 
7981
    Optimization: if not EXPLAIN and we are done with the JOIN,
 
7982
    free all tables.
 
7983
  */
 
7984
  bool full= (!select_lex->uncacheable && !thd->lex->describe);
 
7985
  bool can_unlock= full;
 
7986
 
 
7987
  cleanup(full);
 
7988
 
 
7989
  for (tmp_unit= select_lex->first_inner_unit();
 
7990
       tmp_unit;
 
7991
       tmp_unit= tmp_unit->next_unit())
 
7992
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
 
7993
    {
 
7994
      Item_subselect *subselect= sl->master_unit()->item;
 
7995
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
7996
      /*
 
7997
        If this join is evaluated, we can fully clean it up and clean up all
 
7998
        its underlying joins even if they are correlated -- they will not be
 
7999
        used any more anyway.
 
8000
        If this join is not yet evaluated, we still must clean it up to
 
8001
        close its table cursors -- it may never get evaluated, as in case of
 
8002
        ... HAVING false OR a IN (SELECT ...))
 
8003
        but all table cursors must be closed before the unlock.
 
8004
      */
 
8005
      sl->cleanup_all_joins(full_local);
 
8006
      /* Can't unlock if at least one JOIN is still needed */
 
8007
      can_unlock= can_unlock && full_local;
 
8008
    }
 
8009
 
 
8010
  /*
 
8011
    We are not using tables anymore
 
8012
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8013
  */
 
8014
  if (can_unlock && lock && thd->lock &&
 
8015
      !(select_options & SELECT_NO_UNLOCK) &&
 
8016
      !select_lex->subquery_in_having &&
 
8017
      (select_lex == (thd->lex->unit.fake_select_lex ?
 
8018
                      thd->lex->unit.fake_select_lex : &thd->lex->select_lex)))
 
8019
  {
 
8020
    /*
 
8021
      TODO: unlock tables even if the join isn't top level select in the
 
8022
      tree.
 
8023
    */
 
8024
    mysql_unlock_read_tables(thd, lock);           // Don't free join->lock
 
8025
    lock= 0;
 
8026
  }
 
8027
 
 
8028
  return;
 
8029
}
 
8030
 
 
8031
 
 
8032
/**
 
8033
  Free resources of given join.
 
8034
 
 
8035
  @param fill   true if we should free all resources, call with full==1
 
8036
                should be last, before it this function can be called with
 
8037
                full==0
 
8038
 
 
8039
  @note
 
8040
    With subquery this function definitely will be called several times,
 
8041
    but even for simple query it can be called several times.
 
8042
*/
 
8043
 
 
8044
void JOIN::cleanup(bool full)
 
8045
{
 
8046
  if (table)
 
8047
  {
 
8048
    JOIN_TAB *tab,*end;
 
8049
    /*
 
8050
      Only a sorted table may be cached.  This sorted table is always the
 
8051
      first non const table in join->table
 
8052
    */
 
8053
    if (tables > const_tables) // Test for not-const tables
 
8054
    {
 
8055
      free_io_cache(table[const_tables]);
 
8056
      filesort_free_buffers(table[const_tables],full);
 
8057
    }
 
8058
 
 
8059
    if (full)
 
8060
    {
 
8061
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8062
        tab->cleanup();
 
8063
      table= 0;
 
8064
    }
 
8065
    else
 
8066
    {
 
8067
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8068
      {
 
8069
        if (tab->table)
 
8070
          tab->table->file->ha_index_or_rnd_end();
 
8071
      }
 
8072
    }
 
8073
    cleanup_sj_tmp_tables(this);//
 
8074
  }
 
8075
  /*
 
8076
    We are not using tables anymore
 
8077
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8078
  */
 
8079
  if (full)
 
8080
  {
 
8081
    if (tmp_join)
 
8082
      tmp_table_param.copy_field= 0;
 
8083
    group_fields.delete_elements();
 
8084
    /*
 
8085
      We can't call delete_elements() on copy_funcs as this will cause
 
8086
      problems in free_elements() as some of the elements are then deleted.
 
8087
    */
 
8088
    tmp_table_param.copy_funcs.empty();
 
8089
    /*
 
8090
      If we have tmp_join and 'this' JOIN is not tmp_join and
 
8091
      tmp_table_param.copy_field's  of them are equal then we have to remove
 
8092
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
 
8093
      be removed in tmp_table_param.cleanup().
 
8094
    */
 
8095
    if (tmp_join &&
 
8096
        tmp_join != this &&
 
8097
        tmp_join->tmp_table_param.copy_field ==
 
8098
        tmp_table_param.copy_field)
 
8099
    {
 
8100
      tmp_join->tmp_table_param.copy_field=
 
8101
        tmp_join->tmp_table_param.save_copy_field= 0;
 
8102
    }
 
8103
    tmp_table_param.cleanup();
 
8104
  }
 
8105
  return;
 
8106
}
 
8107
 
1261
8108
 
1262
8109
/**
1263
8110
  Remove the following expressions from ORDER BY and GROUP BY:
1264
8111
  Constant expressions @n
1265
8112
  Expression that only uses tables that are of type EQ_REF and the reference
1266
 
  is in the order_st list or if all refereed tables are of the above type.
 
8113
  is in the ORDER list or if all refereed tables are of the above type.
1267
8114
 
1268
8115
  In the following, the X field can be removed:
1269
8116
  @code
1278
8125
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
1279
8126
  @endcode
1280
8127
*/
1281
 
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
 
8128
 
 
8129
static bool
 
8130
eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab)
1282
8131
{
1283
8132
  if (tab->cached_eq_ref_table)                 // If cached
1284
8133
    return tab->eq_ref_table;
1285
8134
  tab->cached_eq_ref_table=1;
1286
8135
  /* 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)
 
8136
  if (tab->type == JT_CONST && !tab->first_inner)
 
8137
    return (tab->eq_ref_table=1);               /* purecov: inspected */
 
8138
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
1290
8139
    return (tab->eq_ref_table=0);               // We must use this
1291
8140
  Item **ref_item=tab->ref.items;
1292
8141
  Item **end=ref_item+tab->ref.key_parts;
1293
 
  uint32_t found=0;
 
8142
  uint found=0;
1294
8143
  table_map map=tab->table->map;
1295
8144
 
1296
8145
  for (; ref_item != end ; ref_item++)
1297
8146
  {
1298
8147
    if (! (*ref_item)->const_item())
1299
8148
    {                                           // Not a const ref
1300
 
      Order *order;
 
8149
      ORDER *order;
1301
8150
      for (order=start_order ; order ; order=order->next)
1302
8151
      {
1303
 
        if ((*ref_item)->eq(order->item[0],0))
1304
 
          break;
 
8152
        if ((*ref_item)->eq(order->item[0],0))
 
8153
          break;
1305
8154
      }
1306
8155
      if (order)
1307
8156
      {
1308
 
        found++;
1309
 
        assert(!(order->used & map));
1310
 
        order->used|=map;
1311
 
        continue;                               // Used in order_st BY
 
8157
        found++;
 
8158
        assert(!(order->used & map));
 
8159
        order->used|=map;
 
8160
        continue;                               // Used in ORDER BY
1312
8161
      }
1313
8162
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1314
 
        return (tab->eq_ref_table= 0);
 
8163
        return (tab->eq_ref_table=0);
1315
8164
    }
1316
8165
  }
1317
8166
  /* Check that there was no reference to table before sort order */
1323
8172
      continue;
1324
8173
    }
1325
8174
    if (start_order->depend_map & map)
1326
 
      return (tab->eq_ref_table= 0);
1327
 
  }
1328
 
  return tab->eq_ref_table= 1;
1329
 
}
 
8175
      return (tab->eq_ref_table=0);
 
8176
  }
 
8177
  return tab->eq_ref_table=1;
 
8178
}
 
8179
 
 
8180
 
 
8181
static bool
 
8182
only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables)
 
8183
{
 
8184
  if (specialflag &  SPECIAL_SAFE_MODE)
 
8185
    return 0;                   // skip this optimize /* purecov: inspected */
 
8186
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
 
8187
  {
 
8188
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
8189
      return 0;
 
8190
  }
 
8191
  return 1;
 
8192
}
 
8193
 
 
8194
 
 
8195
/** Update the dependency map for the tables. */
 
8196
 
 
8197
static void update_depend_map(JOIN *join)
 
8198
{
 
8199
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
 
8200
 
 
8201
  for (; join_tab != end ; join_tab++)
 
8202
  {
 
8203
    TABLE_REF *ref= &join_tab->ref;
 
8204
    table_map depend_map=0;
 
8205
    Item **item=ref->items;
 
8206
    uint i;
 
8207
    for (i=0 ; i < ref->key_parts ; i++,item++)
 
8208
      depend_map|=(*item)->used_tables();
 
8209
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
 
8210
    depend_map&= ~OUTER_REF_TABLE_BIT;
 
8211
    for (JOIN_TAB **tab=join->map2table;
 
8212
         depend_map ;
 
8213
         tab++,depend_map>>=1 )
 
8214
    {
 
8215
      if (depend_map & 1)
 
8216
        ref->depend_map|=(*tab)->ref.depend_map;
 
8217
    }
 
8218
  }
 
8219
}
 
8220
 
 
8221
 
 
8222
/** Update the dependency map for the sort order. */
 
8223
 
 
8224
static void update_depend_map(JOIN *join, ORDER *order)
 
8225
{
 
8226
  for (; order ; order=order->next)
 
8227
  {
 
8228
    table_map depend_map;
 
8229
    order->item[0]->update_used_tables();
 
8230
    order->depend_map=depend_map=order->item[0]->used_tables();
 
8231
    // Not item_sum(), RAND() and no reference to table outside of sub select
 
8232
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
 
8233
        && !order->item[0]->with_sum_func)
 
8234
    {
 
8235
      for (JOIN_TAB **tab=join->map2table;
 
8236
           depend_map ;
 
8237
           tab++, depend_map>>=1)
 
8238
      {
 
8239
        if (depend_map & 1)
 
8240
          order->depend_map|=(*tab)->ref.depend_map;
 
8241
      }
 
8242
    }
 
8243
  }
 
8244
}
 
8245
 
 
8246
 
 
8247
/**
 
8248
  Remove all constants and check if ORDER only contains simple
 
8249
  expressions.
 
8250
 
 
8251
  simple_order is set to 1 if sort_order only uses fields from head table
 
8252
  and the head table is not a LEFT JOIN table.
 
8253
 
 
8254
  @param join                   Join handler
 
8255
  @param first_order            List of SORT or GROUP order
 
8256
  @param cond                   WHERE statement
 
8257
  @param change_list            Set to 1 if we should remove things from list.
 
8258
                               If this is not set, then only simple_order is
 
8259
                               calculated.
 
8260
  @param simple_order           Set to 1 if we are only using simple expressions
 
8261
 
 
8262
  @return
 
8263
    Returns new sort order
 
8264
*/
 
8265
 
 
8266
static ORDER *
 
8267
remove_const(JOIN *join,ORDER *first_order, COND *cond,
 
8268
             bool change_list, bool *simple_order)
 
8269
{
 
8270
  if (join->tables == join->const_tables)
 
8271
    return change_list ? 0 : first_order;               // No need to sort
 
8272
 
 
8273
  ORDER *order,**prev_ptr;
 
8274
  table_map first_table= join->join_tab[join->const_tables].table->map;
 
8275
  table_map not_const_tables= ~join->const_table_map;
 
8276
  table_map ref;
 
8277
 
 
8278
  prev_ptr= &first_order;
 
8279
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
 
8280
 
 
8281
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
 
8282
 
 
8283
  update_depend_map(join, first_order);
 
8284
  for (order=first_order; order ; order=order->next)
 
8285
  {
 
8286
    table_map order_tables=order->item[0]->used_tables();
 
8287
    if (order->item[0]->with_sum_func)
 
8288
      *simple_order=0;                          // Must do a temp table to sort
 
8289
    else if (!(order_tables & not_const_tables))
 
8290
    {
 
8291
      if (order->item[0]->with_subselect)
 
8292
        order->item[0]->val_str(&order->item[0]->str_value);
 
8293
      continue;                                 // skip const item
 
8294
    }
 
8295
    else
 
8296
    {
 
8297
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
 
8298
        *simple_order=0;
 
8299
      else
 
8300
      {
 
8301
        Item *comp_item=0;
 
8302
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
 
8303
        {
 
8304
          continue;
 
8305
        }
 
8306
        if ((ref=order_tables & (not_const_tables ^ first_table)))
 
8307
        {
 
8308
          if (!(order_tables & first_table) &&
 
8309
              only_eq_ref_tables(join,first_order, ref))
 
8310
          {
 
8311
            continue;
 
8312
          }
 
8313
          *simple_order=0;                      // Must do a temp table to sort
 
8314
        }
 
8315
      }
 
8316
    }
 
8317
    if (change_list)
 
8318
      *prev_ptr= order;                         // use this entry
 
8319
    prev_ptr= &order->next;
 
8320
  }
 
8321
  if (change_list)
 
8322
    *prev_ptr=0;
 
8323
  if (prev_ptr == &first_order)                 // Nothing to sort/group
 
8324
    *simple_order=1;
 
8325
  return(first_order);
 
8326
}
 
8327
 
 
8328
 
 
8329
static int
 
8330
return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables,
 
8331
                 List<Item> &fields, bool send_row, uint64_t select_options,
 
8332
                 const char *info, Item *having)
 
8333
{
 
8334
  if (select_options & SELECT_DESCRIBE)
 
8335
  {
 
8336
    select_describe(join, false, false, false, info);
 
8337
    return(0);
 
8338
  }
 
8339
 
 
8340
  join->join_free();
 
8341
 
 
8342
  if (send_row)
 
8343
  {
 
8344
    for (TABLE_LIST *table= tables; table; table= table->next_leaf)
 
8345
      mark_as_null_row(table->table);           // All fields are NULL
 
8346
    if (having && having->val_int() == 0)
 
8347
      send_row=0;
 
8348
  }
 
8349
  if (!(result->send_fields(fields,
 
8350
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
 
8351
  {
 
8352
    if (send_row)
 
8353
    {
 
8354
      List_iterator_fast<Item> it(fields);
 
8355
      Item *item;
 
8356
      while ((item= it++))
 
8357
        item->no_rows_in_result();
 
8358
      result->send_data(fields);
 
8359
    }
 
8360
    result->send_eof();                         // Should be safe
 
8361
  }
 
8362
  /* Update results for FOUND_ROWS */
 
8363
  join->thd->limit_found_rows= join->thd->examined_row_count= 0;
 
8364
  return(0);
 
8365
}
 
8366
 
 
8367
/*
 
8368
  used only in JOIN::clear
 
8369
*/
 
8370
static void clear_tables(JOIN *join)
 
8371
{
 
8372
  /* 
 
8373
    must clear only the non-const tables, as const tables
 
8374
    are not re-calculated.
 
8375
  */
 
8376
  for (uint i=join->const_tables ; i < join->tables ; i++)
 
8377
    mark_as_null_row(join->table[i]);           // All fields are NULL
 
8378
}
 
8379
 
 
8380
/*****************************************************************************
 
8381
  Make som simple condition optimization:
 
8382
  If there is a test 'field = const' change all refs to 'field' to 'const'
 
8383
  Remove all dummy tests 'item = item', 'const op const'.
 
8384
  Remove all 'item is NULL', when item can never be null!
 
8385
  item->marker should be 0 for all items on entry
 
8386
  Return in cond_value false if condition is impossible (1 = 2)
 
8387
*****************************************************************************/
 
8388
 
 
8389
class COND_CMP :public ilink {
 
8390
public:
 
8391
  static void *operator new(size_t size)
 
8392
  {
 
8393
    return (void*) sql_alloc((uint) size);
 
8394
  }
 
8395
  static void operator delete(void *ptr __attribute__((unused)),
 
8396
                              size_t size __attribute__((unused)))
 
8397
  { TRASH(ptr, size); }
 
8398
 
 
8399
  Item *and_level;
 
8400
  Item_func *cmp_func;
 
8401
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
 
8402
};
 
8403
 
 
8404
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
8405
template class I_List<COND_CMP>;
 
8406
template class I_List_iterator<COND_CMP>;
 
8407
#endif
 
8408
 
1330
8409
 
1331
8410
/**
1332
8411
  Find the multiple equality predicate containing a field.
1346
8425
    - Item_equal for the found multiple equality predicate if a success;
1347
8426
    - NULL otherwise.
1348
8427
*/
1349
 
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
 
8428
 
 
8429
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
 
8430
                            bool *inherited_fl)
1350
8431
{
1351
8432
  Item_equal *item= 0;
1352
8433
  bool in_upper_level= false;
1353
8434
  while (cond_equal)
1354
8435
  {
1355
 
    List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
8436
    List_iterator_fast<Item_equal> li(cond_equal->current_level);
1356
8437
    while ((item= li++))
1357
8438
    {
1358
8439
      if (item->contains(field))
1367
8448
  return item;
1368
8449
}
1369
8450
 
 
8451
  
1370
8452
/**
1371
8453
  Check whether an equality can be used to build multiple equalities.
1372
8454
 
1393
8475
    the check_equality will be called for the following equality
1394
8476
    predicates a=b, b=c, b=2 and f=e.
1395
8477
    - For a=b it will be called with *cond_equal=(0,[]) and will transform
1396
 
      *cond_equal into (0,[Item_equal(a,b)]).
 
8478
      *cond_equal into (0,[Item_equal(a,b)]). 
1397
8479
    - For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)])
1398
8480
      and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]).
1399
8481
    - For b=2 it will be called with *cond_equal=(ptr(CE),[])
1406
8488
    the Field::eq_def method) are placed to the same multiple equalities.
1407
8489
    Because of this some equality predicates are not eliminated and
1408
8490
    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
 
8491
    We could weeken the equlity test as soon as at least one of the 
 
8492
    equal fields is to be equal to a constant. It would require a 
1411
8493
    more complicated implementation: we would have to store, in
1412
8494
    general case, its own constant for each fields from the multiple
1413
8495
    equality. But at the same time it would allow us to get rid
1425
8507
    containing just field1 and field2 is added to the existing
1426
8508
    multiple equalities.
1427
8509
    If the function processes the predicate of the form field1=const,
1428
 
    it looks for a multiple equality containing field1. If found, the
 
8510
    it looks for a multiple equality containing field1. If found, the 
1429
8511
    function checks the constant of the multiple equality. If the value
1430
8512
    is unknown, it is setup to const. Otherwise the value is compared with
1431
8513
    const and the evaluation of the equality predicate is performed.
1448
8530
  @retval
1449
8531
    false   otherwise
1450
8532
*/
1451
 
static bool check_simple_equality(Item *left_item,
1452
 
                                  Item *right_item,
1453
 
                                  Item *item,
1454
 
                                  COND_EQUAL *cond_equal)
 
8533
 
 
8534
static bool check_simple_equality(Item *left_item, Item *right_item,
 
8535
                                  Item *item, COND_EQUAL *cond_equal)
1455
8536
{
 
8537
  if (left_item->type() == Item::REF_ITEM &&
 
8538
      ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
 
8539
  {
 
8540
    if (((Item_ref*)left_item)->depended_from)
 
8541
      return false;
 
8542
    left_item= left_item->real_item();
 
8543
  }
 
8544
  if (right_item->type() == Item::REF_ITEM &&
 
8545
      ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
 
8546
  {
 
8547
    if (((Item_ref*)right_item)->depended_from)
 
8548
      return false;
 
8549
    right_item= right_item->real_item();
 
8550
  }
1456
8551
  if (left_item->type() == Item::FIELD_ITEM &&
1457
8552
      right_item->type() == Item::FIELD_ITEM &&
1458
8553
      !((Item_field*)left_item)->depended_from &&
1470
8565
    bool left_copyfl, right_copyfl;
1471
8566
    Item_equal *left_item_equal=
1472
8567
               find_item_equal(cond_equal, left_field, &left_copyfl);
1473
 
    Item_equal *right_item_equal=
 
8568
    Item_equal *right_item_equal= 
1474
8569
               find_item_equal(cond_equal, right_field, &right_copyfl);
1475
8570
 
1476
8571
    /* As (NULL=NULL) != true we can't just remove the predicate f=f */
1477
8572
    if (left_field->eq(right_field)) /* f = f */
1478
 
      return (!(left_field->maybe_null() && !left_item_equal));
 
8573
      return (!(left_field->maybe_null() && !left_item_equal)); 
1479
8574
 
1480
8575
    if (left_item_equal && left_item_equal == right_item_equal)
1481
8576
    {
1482
 
      /*
 
8577
      /* 
1483
8578
        The equality predicate is inference of one of the existing
1484
8579
        multiple equalities, i.e the condition is already covered
1485
8580
        by upper level equalities
1486
8581
      */
1487
8582
       return true;
1488
8583
    }
1489
 
 
1490
 
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name &&
 
8584
    
 
8585
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name && 
1491
8586
                              item->name < subq_sj_cond_name + 64);
1492
8587
    /* Copy the found multiple equalities at the current level if needed */
1493
8588
    if (left_copyfl)
1508
8603
    }
1509
8604
 
1510
8605
    if (left_item_equal)
1511
 
    {
 
8606
    { 
1512
8607
      /* left item was found in the current or one of the upper levels */
1513
8608
      if (! right_item_equal)
1514
8609
        left_item_equal->add((Item_field *) right_item);
1517
8612
        /* Merge two multiple equalities forming a new one */
1518
8613
        left_item_equal->merge(right_item_equal);
1519
8614
        /* Remove the merged multiple equality from the list */
1520
 
        List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
8615
        List_iterator<Item_equal> li(cond_equal->current_level);
1521
8616
        while ((li++) != right_item_equal) {};
1522
8617
        li.remove();
1523
8618
      }
1524
8619
    }
1525
8620
    else
1526
 
    {
 
8621
    { 
1527
8622
      /* left item was not found neither the current nor in upper levels  */
1528
8623
      if (right_item_equal)
1529
8624
      {
1531
8626
        if (copy_item_name)
1532
8627
          right_item_equal->name = item->name;
1533
8628
      }
1534
 
      else
 
8629
      else 
1535
8630
      {
1536
8631
        /* None of the fields was found in multiple equalities */
1537
8632
        Item_equal *item_equal= new Item_equal((Item_field *) left_item,
1570
8665
 
1571
8666
      if (field_item->result_type() == STRING_RESULT)
1572
8667
      {
1573
 
        const CHARSET_INFO * const cs= ((Field_str*) field_item->field)->charset();
 
8668
        CHARSET_INFO *cs= ((Field_str*) field_item->field)->charset();
1574
8669
        if (!item)
1575
8670
        {
1576
8671
          Item_func_eq *eq_item;
1579
8674
          eq_item->set_cmp_func();
1580
8675
          eq_item->quick_fix_field();
1581
8676
          item= eq_item;
1582
 
        }
 
8677
        }  
1583
8678
        if ((cs != ((Item_func *) item)->compare_collation()) ||
1584
8679
            !cs->coll->propagate(cs, 0, 0))
1585
8680
          return false;
1594
8689
      }
1595
8690
      if (item_equal)
1596
8691
      {
1597
 
        /*
 
8692
        /* 
1598
8693
          The flag cond_false will be set to 1 after this, if item_equal
1599
8694
          already contains a constant and its value is  not equal to
1600
8695
          the value of const_item.
1612
8707
  return false;
1613
8708
}
1614
8709
 
 
8710
 
1615
8711
/**
1616
8712
  Convert row equalities into a conjunction of regular equalities.
1617
8713
 
1624
8720
    simple equality nor a row equality the item for this predicate is added
1625
8721
    to eq_list.
1626
8722
 
1627
 
  @param session        thread handle
 
8723
  @param thd        thread handle
1628
8724
  @param left_row   left term of the row equality to be processed
1629
8725
  @param right_row  right term of the row equality to be processed
1630
8726
  @param cond_equal multiple equalities that must hold together with the
1637
8733
  @retval
1638
8734
    false   otherwise
1639
8735
*/
1640
 
static bool check_row_equality(Session *session,
1641
 
                               Item *left_row, 
1642
 
                               Item_row *right_row,
1643
 
                               COND_EQUAL *cond_equal,
1644
 
                               List<Item>* eq_list)
1645
 
{
1646
 
  uint32_t n= left_row->cols();
1647
 
  for (uint32_t i= 0 ; i < n; i++)
 
8736
 
 
8737
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
 
8738
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
 
8739
 
8740
  uint n= left_row->cols();
 
8741
  for (uint i= 0 ; i < n; i++)
1648
8742
  {
1649
8743
    bool is_converted;
1650
8744
    Item *left_item= left_row->element_index(i);
1652
8746
    if (left_item->type() == Item::ROW_ITEM &&
1653
8747
        right_item->type() == Item::ROW_ITEM)
1654
8748
    {
1655
 
      is_converted= check_row_equality(session,
 
8749
      is_converted= check_row_equality(thd, 
1656
8750
                                       (Item_row *) left_item,
1657
8751
                                       (Item_row *) right_item,
1658
8752
                                       cond_equal, eq_list);
1659
8753
      if (!is_converted)
1660
 
        session->getLex()->current_select->cond_count++;
 
8754
        thd->lex->current_select->cond_count++;      
1661
8755
    }
1662
8756
    else
1663
 
    {
 
8757
    { 
1664
8758
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1665
 
      session->getLex()->current_select->cond_count++;
1666
 
    }
1667
 
 
 
8759
      thd->lex->current_select->cond_count++;
 
8760
    }  
 
8761
 
1668
8762
    if (!is_converted)
1669
8763
    {
1670
8764
      Item_func_eq *eq_item;
1678
8772
  return true;
1679
8773
}
1680
8774
 
 
8775
 
1681
8776
/**
1682
8777
  Eliminate row equalities and form multiple equalities predicates.
1683
8778
 
1692
8787
    equalities which are treated in the same way as original equality
1693
8788
    predicates.
1694
8789
 
1695
 
  @param session        thread handle
 
8790
  @param thd        thread handle
1696
8791
  @param item       predicate to process
1697
8792
  @param cond_equal multiple equalities that must hold together with the
1698
8793
                    predicate
1707
8802
           or, if the equality is neither a simple one nor a row equality,
1708
8803
           or, if the procedure fails by a fatal error.
1709
8804
*/
1710
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
 
8805
 
 
8806
static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal,
 
8807
                           List<Item> *eq_list)
1711
8808
{
1712
8809
  if (item->type() == Item::FUNC_ITEM &&
1713
8810
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
1718
8815
    if (left_item->type() == Item::ROW_ITEM &&
1719
8816
        right_item->type() == Item::ROW_ITEM)
1720
8817
    {
1721
 
      session->getLex()->current_select->cond_count--;
1722
 
      return check_row_equality(session,
 
8818
      thd->lex->current_select->cond_count--;
 
8819
      return check_row_equality(thd,
1723
8820
                                (Item_row *) left_item,
1724
8821
                                (Item_row *) right_item,
1725
8822
                                cond_equal, eq_list);
1726
8823
    }
1727
 
    else
 
8824
    else 
1728
8825
      return check_simple_equality(left_item, right_item, item, cond_equal);
1729
 
  }
 
8826
  } 
1730
8827
  return false;
1731
8828
}
1732
8829
 
 
8830
                          
1733
8831
/**
1734
8832
  Replace all equality predicates in a condition by multiple equality items.
1735
8833
 
1736
8834
    At each 'and' level the function detects items for equality predicates
1737
8835
    and replaced them by a set of multiple equality items of class Item_equal,
1738
 
    taking into account inherited equalities from upper levels.
 
8836
    taking into account inherited equalities from upper levels. 
1739
8837
    If an equality predicate is used not in a conjunction it's just
1740
8838
    replaced by a multiple equality predicate.
1741
8839
    For each 'and' level the function set a pointer to the inherited
1742
8840
    multiple equalities in the cond_equal field of the associated
1743
 
    object of the type Item_cond_and.
 
8841
    object of the type Item_cond_and.   
1744
8842
    The function also traverses the cond tree and and for each field reference
1745
8843
    sets a pointer to the multiple equality item containing the field, if there
1746
8844
    is any. If this multiple equality equates fields to a constant the
1747
 
    function replaces the field reference by the constant in the cases
 
8845
    function replaces the field reference by the constant in the cases 
1748
8846
    when the field is not of a string type or when the field reference is
1749
8847
    just an argument of a comparison predicate.
1750
 
    The function also determines the maximum number of members in
 
8848
    The function also determines the maximum number of members in 
1751
8849
    equality lists of each Item_cond_and object assigning it to
1752
 
    session->getLex()->current_select->max_equal_elems.
 
8850
    thd->lex->current_select->max_equal_elems.
1753
8851
 
1754
8852
  @note
1755
8853
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
1761
8859
    in a conjuction for a minimal set of multiple equality predicates.
1762
8860
    This set can be considered as a canonical representation of the
1763
8861
    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
 
8862
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by 
1765
8863
    (=(t1.a,t2.b,t3.c) AND t2.b>5), not by
1766
8864
    (=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5);
1767
8865
    while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by
1772
8870
    The function performs the substitution in a recursive descent by
1773
8871
    the condtion tree, passing to the next AND level a chain of multiple
1774
8872
    equality predicates which have been built at the upper levels.
1775
 
    The Item_equal items built at the level are attached to other
 
8873
    The Item_equal items built at the level are attached to other 
1776
8874
    non-equality conjucts as a sublist. The pointer to the inherited
1777
8875
    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
 
8876
    This chain allows us for any field reference occurence easyly to find a 
1779
8877
    multiple equality that must be held for this occurence.
1780
8878
    For each AND level we do the following:
1781
8879
    - scan it for all equality predicate (=) items
1782
8880
    - join them into disjoint Item_equal() groups
1783
 
    - process the included OR conditions recursively to do the same for
1784
 
      lower AND levels.
 
8881
    - process the included OR conditions recursively to do the same for 
 
8882
      lower AND levels. 
1785
8883
 
1786
8884
    We need to do things in this order as lower AND levels need to know about
1787
8885
    all possible Item_equal objects in upper levels.
1788
8886
 
1789
 
  @param session        thread handle
 
8887
  @param thd        thread handle
1790
8888
  @param cond       condition(expression) where to make replacement
1791
8889
  @param inherited  path to all inherited multiple equality items
1792
8890
 
1793
8891
  @return
1794
8892
    pointer to the transformed condition
1795
8893
*/
1796
 
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
 
8894
 
 
8895
static COND *build_equal_items_for_cond(THD *thd, COND *cond,
 
8896
                                        COND_EQUAL *inherited)
1797
8897
{
1798
8898
  Item_equal *item_equal;
1799
8899
  COND_EQUAL cond_equal;
1805
8905
    bool and_level= ((Item_cond*) cond)->functype() ==
1806
8906
      Item_func::COND_AND_FUNC;
1807
8907
    List<Item> *args= ((Item_cond*) cond)->argument_list();
1808
 
 
1809
 
    List<Item>::iterator li(args->begin());
 
8908
    
 
8909
    List_iterator<Item> li(*args);
1810
8910
    Item *item;
1811
8911
 
1812
8912
    if (and_level)
1814
8914
      /*
1815
8915
         Retrieve all conjucts of this level detecting the equality
1816
8916
         that are subject to substitution by multiple equality items and
1817
 
         removing each such predicate from the conjunction after having
 
8917
         removing each such predicate from the conjunction after having 
1818
8918
         found/created a multiple equality whose inference the predicate is.
1819
 
     */
 
8919
     */      
1820
8920
      while ((item= li++))
1821
8921
      {
1822
8922
        /*
1824
8924
          structure here because it's restored before each
1825
8925
          re-execution of any prepared statement/stored procedure.
1826
8926
        */
1827
 
        if (check_equality(session, item, &cond_equal, &eq_list))
 
8927
        if (check_equality(thd, item, &cond_equal, &eq_list))
1828
8928
          li.remove();
1829
8929
      }
1830
8930
 
1831
 
      List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
8931
      List_iterator_fast<Item_equal> it(cond_equal.current_level);
1832
8932
      while ((item_equal= it++))
1833
8933
      {
1834
8934
        item_equal->fix_length_and_dec();
1835
8935
        item_equal->update_used_tables();
1836
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
1837
 
                      item_equal->members());
 
8936
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8937
                      item_equal->members());  
1838
8938
      }
1839
8939
 
1840
8940
      ((Item_cond_and*)cond)->cond_equal= cond_equal;
1844
8944
       Make replacement of equality predicates for lower levels
1845
8945
       of the condition expression.
1846
8946
    */
1847
 
    li= args->begin();
 
8947
    li.rewind();
1848
8948
    while ((item= li++))
1849
 
    {
 
8949
    { 
1850
8950
      Item *new_item;
1851
 
      if ((new_item= build_equal_items_for_cond(session, item, inherited)) != item)
 
8951
      if ((new_item= build_equal_items_for_cond(thd, item, inherited)) != item)
1852
8952
      {
1853
8953
        /* This replacement happens only for standalone equalities */
1854
8954
        /*
1876
8976
      (b=5) and (a=c) are standalone equalities.
1877
8977
      In general we can't leave alone standalone eqalities:
1878
8978
      for WHERE a=b AND c=d AND (b=c OR d=5)
1879
 
      b=c is replaced by =(a,b,c,d).
 
8979
      b=c is replaced by =(a,b,c,d).  
1880
8980
     */
1881
 
    if (check_equality(session, cond, &cond_equal, &eq_list))
 
8981
    if (check_equality(thd, cond, &cond_equal, &eq_list))
1882
8982
    {
1883
8983
      int n= cond_equal.current_level.elements + eq_list.elements;
1884
8984
      if (n == 0)
1889
8989
        {
1890
8990
          item_equal->fix_length_and_dec();
1891
8991
          item_equal->update_used_tables();
1892
 
        }
 
8992
        }
1893
8993
        else
1894
8994
          item_equal= (Item_equal *) eq_list.pop();
1895
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
1896
 
                      item_equal->members());
 
8995
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8996
                      item_equal->members());  
1897
8997
        return item_equal;
1898
8998
      }
1899
8999
      else
1900
9000
      {
1901
 
        /*
 
9001
        /* 
1902
9002
          Here a new AND level must be created. It can happen only
1903
9003
          when a row equality is processed as a standalone predicate.
1904
 
        */
 
9004
        */
1905
9005
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
1906
9006
        and_cond->quick_fix_field();
1907
9007
        List<Item> *args= and_cond->argument_list();
1908
 
        List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
9008
        List_iterator_fast<Item_equal> it(cond_equal.current_level);
1909
9009
        while ((item_equal= it++))
1910
9010
        {
1911
9011
          item_equal->fix_length_and_dec();
1912
9012
          item_equal->update_used_tables();
1913
 
          set_if_bigger(session->getLex()->current_select->max_equal_elems,
1914
 
                        item_equal->members());
 
9013
          set_if_bigger(thd->lex->current_select->max_equal_elems,
 
9014
                        item_equal->members());  
1915
9015
        }
1916
9016
        and_cond->cond_equal= cond_equal;
1917
9017
        args->concat((List<Item> *)&cond_equal.current_level);
1918
 
 
 
9018
        
1919
9019
        return and_cond;
1920
9020
      }
1921
9021
    }
1922
 
    /*
 
9022
    /* 
1923
9023
      For each field reference in cond, not from equal item predicates,
1924
9024
      set a pointer to the multiple equality it belongs to (if there is any)
1925
9025
      as soon the field is not of a string type or the field reference is
1926
 
      an argument of a comparison predicate.
1927
 
    */
1928
 
    unsigned char *is_subst_valid= (unsigned char *) 1;
 
9026
      an argument of a comparison predicate. 
 
9027
    */ 
 
9028
    uchar *is_subst_valid= (uchar *) 1;
1929
9029
    cond= cond->compile(&Item::subst_argument_checker,
1930
 
                        &is_subst_valid,
 
9030
                        &is_subst_valid, 
1931
9031
                        &Item::equal_fields_propagator,
1932
 
                        (unsigned char *) inherited);
 
9032
                        (uchar *) inherited);
1933
9033
    cond->update_used_tables();
1934
9034
  }
1935
9035
  return cond;
1936
9036
}
1937
9037
 
 
9038
 
1938
9039
/**
1939
9040
  Build multiple equalities for a condition and all on expressions that
1940
9041
  inherit these multiple equalities.
1980
9081
      SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a
1981
9082
        WHERE t1.a=t2.a
1982
9083
    @endcode
1983
 
    that is equivalent to:
 
9084
    that is equivalent to:   
1984
9085
    @code
1985
9086
      SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1
1986
9087
        WHERE t1.a=t2.a
1987
9088
    @endcode
1988
9089
    Thus, applying equalities from the where condition we basically
1989
9090
    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
 
9091
    Althogh we don't use this property now, it probably makes sense to use 
 
9092
    it in the future.    
 
9093
  @param thd                  Thread handler
1993
9094
  @param cond                condition to build the multiple equalities for
1994
9095
  @param inherited           path to all inherited multiple equality items
1995
9096
  @param join_list           list of join tables to which the condition
2000
9101
  @return
2001
9102
    pointer to the transformed condition containing multiple equalities
2002
9103
*/
2003
 
static COND *build_equal_items(Session *session, COND *cond,
 
9104
   
 
9105
static COND *build_equal_items(THD *thd, COND *cond,
2004
9106
                               COND_EQUAL *inherited,
2005
 
                               List<TableList> *join_list,
 
9107
                               List<TABLE_LIST> *join_list,
2006
9108
                               COND_EQUAL **cond_equal_ref)
2007
9109
{
2008
9110
  COND_EQUAL *cond_equal= 0;
2009
9111
 
2010
 
  if (cond)
 
9112
  if (cond) 
2011
9113
  {
2012
 
    cond= build_equal_items_for_cond(session, cond, inherited);
 
9114
    cond= build_equal_items_for_cond(thd, cond, inherited);
2013
9115
    cond->update_used_tables();
2014
9116
    if (cond->type() == Item::COND_ITEM &&
2015
9117
        ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
2030
9132
 
2031
9133
  if (join_list)
2032
9134
  {
2033
 
    TableList *table;
2034
 
    List<TableList>::iterator li(join_list->begin());
 
9135
    TABLE_LIST *table;
 
9136
    List_iterator<TABLE_LIST> li(*join_list);
2035
9137
 
2036
9138
    while ((table= li++))
2037
9139
    {
2038
9140
      if (table->on_expr)
2039
9141
      {
2040
 
        List<TableList> *nested_join_list= table->getNestedJoin() ?
2041
 
          &table->getNestedJoin()->join_list : NULL;
 
9142
        List<TABLE_LIST> *nested_join_list= table->nested_join ?
 
9143
          &table->nested_join->join_list : NULL;
2042
9144
        /*
2043
9145
          We can modify table->on_expr because its old value will
2044
9146
          be restored before re-execution of PS/SP.
2045
9147
        */
2046
 
        table->on_expr= build_equal_items(session, table->on_expr, inherited,
 
9148
        table->on_expr= build_equal_items(thd, table->on_expr, inherited,
2047
9149
                                          nested_join_list,
2048
9150
                                          &table->cond_equal);
2049
9151
      }
2051
9153
  }
2052
9154
 
2053
9155
  return cond;
2054
 
}
 
9156
}    
 
9157
 
2055
9158
 
2056
9159
/**
2057
9160
  Compare field items by table order in the execution plan.
2058
9161
 
2059
9162
    field1 considered as better than field2 if the table containing
2060
 
    field1 is accessed earlier than the table containing field2.
 
9163
    field1 is accessed earlier than the table containing field2.   
2061
9164
    The function finds out what of two fields is better according
2062
9165
    this criteria.
2063
9166
 
2072
9175
  @retval
2073
9176
    0  otherwise
2074
9177
*/
 
9178
 
2075
9179
static int compare_fields_by_table_order(Item_field *field1,
2076
 
                                         Item_field *field2,
2077
 
                                         void *table_join_idx)
 
9180
                                  Item_field *field2,
 
9181
                                  void *table_join_idx)
2078
9182
{
2079
9183
  int cmp= 0;
2080
9184
  bool outer_ref= 0;
2081
9185
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
2082
 
  {
 
9186
  {  
2083
9187
    outer_ref= 1;
2084
9188
    cmp= -1;
2085
9189
  }
2090
9194
  }
2091
9195
  if (outer_ref)
2092
9196
    return cmp;
2093
 
  JoinTable **idx= (JoinTable **) table_join_idx;
2094
 
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
 
9197
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
 
9198
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
2095
9199
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2096
9200
}
2097
9201
 
 
9202
 
2098
9203
/**
2099
9204
  Generate minimal set of simple equalities equivalent to a multiple equality.
2100
9205
 
2134
9239
    a pointer to the simple generated equality, if success.
2135
9240
    - 0, otherwise.
2136
9241
*/
2137
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
 
9242
 
 
9243
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
 
9244
                                  Item_equal *item_equal)
2138
9245
{
2139
9246
  List<Item> eq_list;
2140
9247
  Item_func_eq *eq_item= 0;
2141
9248
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
2142
 
    return new Item_int((int64_t) 0,1);
 
9249
    return new Item_int((int64_t) 0,1); 
2143
9250
  Item *item_const= item_equal->get_const();
2144
9251
  Item_equal_iterator it(*item_equal);
2145
9252
  Item *head;
2156
9263
    Item_equal *upper= item_field->find_item_equal(upper_levels);
2157
9264
    Item_field *item= item_field;
2158
9265
    if (upper)
2159
 
    {
 
9266
    { 
2160
9267
      if (item_const && upper->get_const())
2161
9268
        item= 0;
2162
9269
      else
2200
9307
 
2201
9308
  cond->quick_fix_field();
2202
9309
  cond->update_used_tables();
2203
 
 
 
9310
   
2204
9311
  return cond;
2205
9312
}
2206
9313
 
 
9314
 
2207
9315
/**
2208
9316
  Substitute every field reference in a condition by the best equal field
2209
9317
  and eliminate all multiple equality predicates.
2212
9320
    multiple equality predicate it sorts the field references in it
2213
9321
    according to the order of tables specified by the table_join_idx
2214
9322
    parameter. Then it eliminates the multiple equality predicate it
2215
 
    replacing it by the conjunction of simple equality predicates
 
9323
    replacing it by the conjunction of simple equality predicates 
2216
9324
    equating every field from the multiple equality to the first
2217
9325
    field in it, or to the constant, if there is any.
2218
9326
    After this the function retrieves all other conjuncted
2231
9339
  @return
2232
9340
    The transformed condition
2233
9341
*/
2234
 
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
 
9342
 
 
9343
static COND* substitute_for_best_equal_field(COND *cond,
 
9344
                                             COND_EQUAL *cond_equal,
 
9345
                                             void *table_join_idx)
2235
9346
{
2236
9347
  Item_equal *item_equal;
2237
9348
 
2246
9357
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
2247
9358
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2248
9359
 
2249
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
9360
      List_iterator_fast<Item_equal> it(cond_equal->current_level);      
2250
9361
      while ((item_equal= it++))
2251
9362
      {
2252
9363
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2253
9364
      }
2254
9365
    }
2255
 
 
2256
 
    List<Item>::iterator li(cond_list->begin());
 
9366
    
 
9367
    List_iterator<Item> li(*cond_list);
2257
9368
    Item *item;
2258
9369
    while ((item= li++))
2259
9370
    {
2269
9380
 
2270
9381
    if (and_level)
2271
9382
    {
2272
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
9383
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2273
9384
      while ((item_equal= it++))
2274
9385
      {
2275
9386
        cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
2285
9396
      cond= new Item_int((int32_t)cond->val_bool());
2286
9397
 
2287
9398
  }
2288
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9399
  else if (cond->type() == Item::FUNC_ITEM && 
2289
9400
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2290
9401
  {
2291
9402
    item_equal= (Item_equal *) cond;
2299
9410
  return cond;
2300
9411
}
2301
9412
 
 
9413
 
2302
9414
/**
2303
9415
  Check appearance of new constant items in multiple equalities
2304
9416
  of a condition after reading a constant table.
2311
9423
  @param cond       condition whose multiple equalities are to be checked
2312
9424
  @param table      constant table that has been read
2313
9425
*/
2314
 
void update_const_equal_items(COND *cond, JoinTable *tab)
 
9426
 
 
9427
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
2315
9428
{
2316
9429
  if (!(cond->used_tables() & tab->table->map))
2317
9430
    return;
2318
9431
 
2319
9432
  if (cond->type() == Item::COND_ITEM)
2320
9433
  {
2321
 
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
2322
 
    List<Item>::iterator li(cond_list->begin());
 
9434
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); 
 
9435
    List_iterator_fast<Item> li(*cond_list);
2323
9436
    Item *item;
2324
9437
    while ((item= li++))
2325
9438
      update_const_equal_items(item, tab);
2326
9439
  }
2327
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9440
  else if (cond->type() == Item::FUNC_ITEM && 
2328
9441
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2329
9442
  {
2330
9443
    Item_equal *item_equal= (Item_equal *) cond;
2338
9451
      while ((item_field= it++))
2339
9452
      {
2340
9453
        Field *field= item_field->field;
2341
 
        JoinTable *stat= field->getTable()->reginfo.join_tab;
 
9454
        JOIN_TAB *stat= field->table->reginfo.join_tab;
2342
9455
        key_map possible_keys= field->key_start;
2343
 
        possible_keys&= field->getTable()->keys_in_use_for_query;
2344
 
        stat[0].const_keys|= possible_keys;
 
9456
        possible_keys.intersect(field->table->keys_in_use_for_query);
 
9457
        stat[0].const_keys.merge(possible_keys);
2345
9458
 
2346
9459
        /*
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
 
9460
          For each field in the multiple equality (for which we know that it 
 
9461
          is a constant) we have to find its corresponding key part, and set 
2349
9462
          that key part in const_key_parts.
2350
 
        */
2351
 
        if (possible_keys.any())
 
9463
        */  
 
9464
        if (!possible_keys.is_clear_all())
2352
9465
        {
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 ==
 
9466
          TABLE *tab= field->table;
 
9467
          KEYUSE *use;
 
9468
          for (use= stat->keyuse; use && use->table == tab; use++)
 
9469
            if (possible_keys.is_set(use->key) && 
 
9470
                tab->key_info[use->key].key_part[use->keypart].field ==
2358
9471
                field)
2359
 
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
9472
              tab->const_key_parts[use->key]|= use->keypart_map;
2360
9473
        }
2361
9474
      }
2362
9475
    }
2363
9476
  }
2364
9477
}
2365
9478
 
 
9479
 
2366
9480
/*
2367
9481
  change field = field to field = const for each found field = const in the
2368
9482
  and_level
2369
9483
*/
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)
 
9484
 
 
9485
static void
 
9486
change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
 
9487
                         Item *and_father, Item *cond,
 
9488
                         Item *field, Item *value)
2376
9489
{
2377
9490
  if (cond->type() == Item::COND_ITEM)
2378
9491
  {
2379
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2380
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
9492
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9493
      Item_func::COND_AND_FUNC;
 
9494
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2381
9495
    Item *item;
2382
9496
    while ((item=li++))
2383
 
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2384
 
 
 
9497
      change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item,
 
9498
                               field, value);
2385
9499
    return;
2386
9500
  }
2387
9501
  if (cond->eq_cmp_result() == Item::COND_OK)
2400
9514
       left_item->collation.collation == value->collation.collation))
2401
9515
  {
2402
9516
    Item *tmp=value->clone_item();
 
9517
    tmp->collation.set(right_item->collation);
 
9518
    
2403
9519
    if (tmp)
2404
9520
    {
2405
 
      tmp->collation.set(right_item->collation);
2406
 
      session->change_item_tree(args + 1, tmp);
 
9521
      thd->change_item_tree(args + 1, tmp);
2407
9522
      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())
 
9523
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9524
          && and_father != cond && !left_item->const_item())
2411
9525
      {
2412
 
        cond->marker=1;
2413
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9526
        cond->marker=1;
 
9527
        COND_CMP *tmp2;
 
9528
        if ((tmp2=new COND_CMP(and_father,func)))
 
9529
          save_list->push_back(tmp2);
2414
9530
      }
2415
9531
      func->set_cmp_func();
2416
9532
    }
2422
9538
            right_item->collation.collation == value->collation.collation))
2423
9539
  {
2424
9540
    Item *tmp= value->clone_item();
 
9541
    tmp->collation.set(left_item->collation);
 
9542
    
2425
9543
    if (tmp)
2426
9544
    {
2427
 
      tmp->collation.set(left_item->collation);
2428
 
      session->change_item_tree(args, tmp);
 
9545
      thd->change_item_tree(args, tmp);
2429
9546
      value= tmp;
2430
9547
      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())
 
9548
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9549
          && and_father != cond && !right_item->const_item())
2434
9550
      {
2435
9551
        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) );
 
9552
        thd->change_item_tree(args + 1, value);
 
9553
        cond->marker=1;
 
9554
        COND_CMP *tmp2;
 
9555
        if ((tmp2=new COND_CMP(and_father,func)))
 
9556
          save_list->push_back(tmp2);
2439
9557
      }
2440
9558
      func->set_cmp_func();
2441
9559
    }
2450
9568
  @return
2451
9569
    new conditions
2452
9570
*/
2453
 
Item *remove_additional_cond(Item* conds)
 
9571
 
 
9572
static Item *remove_additional_cond(Item* conds)
2454
9573
{
2455
9574
  if (conds->name == in_additional_cond)
2456
9575
    return 0;
2457
9576
  if (conds->type() == Item::COND_ITEM)
2458
9577
  {
2459
9578
    Item_cond *cnd= (Item_cond*) conds;
2460
 
    List<Item>::iterator li(cnd->argument_list()->begin());
 
9579
    List_iterator<Item> li(*(cnd->argument_list()));
2461
9580
    Item *item;
2462
9581
    while ((item= li++))
2463
9582
    {
2473
9592
  return conds;
2474
9593
}
2475
9594
 
2476
 
static void propagate_cond_constants(Session *session, 
2477
 
                                     list<COND_CMP>& save_list, 
2478
 
                                     COND *and_father, 
2479
 
                                     COND *cond)
 
9595
static void
 
9596
propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list,
 
9597
                         COND *and_father, COND *cond)
2480
9598
{
2481
9599
  if (cond->type() == Item::COND_ITEM)
2482
9600
  {
2483
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2484
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
9601
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9602
      Item_func::COND_AND_FUNC;
 
9603
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2485
9604
    Item *item;
2486
 
    list<COND_CMP> save;
 
9605
    I_List<COND_CMP> save;
2487
9606
    while ((item=li++))
2488
9607
    {
2489
 
      propagate_cond_constants(session, save, and_level ? cond : item, item);
 
9608
      propagate_cond_constants(thd, &save,and_level ? cond : item, item);
2490
9609
    }
2491
9610
    if (and_level)
2492
 
    {
2493
 
      // Handle other found items
2494
 
      for (list<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
 
9611
    {                                           // Handle other found items
 
9612
      I_List_iterator<COND_CMP> cond_itr(save);
 
9613
      COND_CMP *cond_cmp;
 
9614
      while ((cond_cmp=cond_itr++))
2495
9615
      {
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
 
        }
 
9616
        Item **args= cond_cmp->cmp_func->arguments();
 
9617
        if (!args[0]->const_item())
 
9618
          change_cond_ref_to_const(thd, &save,cond_cmp->and_level,
 
9619
                                   cond_cmp->and_level, args[0], args[1]);
2502
9620
      }
2503
9621
    }
2504
9622
  }
2505
9623
  else if (and_father != cond && !cond->marker)         // In a AND group
2506
9624
  {
2507
9625
    if (cond->type() == Item::FUNC_ITEM &&
2508
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2509
 
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
9626
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
9627
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2510
9628
    {
2511
9629
      Item_func_eq *func=(Item_func_eq*) cond;
2512
9630
      Item **args= func->arguments();
2515
9633
      if (!(left_const && right_const) &&
2516
9634
          args[0]->result_type() == args[1]->result_type())
2517
9635
      {
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
 
}
 
9636
        if (right_const)
 
9637
        {
 
9638
          resolve_const_item(thd, &args[1], args[0]);
 
9639
          func->update_used_tables();
 
9640
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9641
                                   args[0], args[1]);
 
9642
        }
 
9643
        else if (left_const)
 
9644
        {
 
9645
          resolve_const_item(thd, &args[0], args[1]);
 
9646
          func->update_used_tables();
 
9647
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9648
                                   args[1], args[0]);
 
9649
        }
 
9650
      }
 
9651
    }
 
9652
  }
 
9653
}
 
9654
 
 
9655
 
 
9656
/**
 
9657
  Simplify joins replacing outer joins by inner joins whenever it's
 
9658
  possible.
 
9659
 
 
9660
    The function, during a retrieval of join_list,  eliminates those
 
9661
    outer joins that can be converted into inner join, possibly nested.
 
9662
    It also moves the on expressions for the converted outer joins
 
9663
    and from inner joins to conds.
 
9664
    The function also calculates some attributes for nested joins:
 
9665
    - used_tables    
 
9666
    - not_null_tables
 
9667
    - dep_tables.
 
9668
    - on_expr_dep_tables
 
9669
    The first two attributes are used to test whether an outer join can
 
9670
    be substituted for an inner join. The third attribute represents the
 
9671
    relation 'to be dependent on' for tables. If table t2 is dependent
 
9672
    on table t1, then in any evaluated execution plan table access to
 
9673
    table t2 must precede access to table t2. This relation is used also
 
9674
    to check whether the query contains  invalid cross-references.
 
9675
    The forth attribute is an auxiliary one and is used to calculate
 
9676
    dep_tables.
 
9677
    As the attribute dep_tables qualifies possibles orders of tables in the
 
9678
    execution plan, the dependencies required by the straight join
 
9679
    modifiers are reflected in this attribute as well.
 
9680
    The function also removes all braces that can be removed from the join
 
9681
    expression without changing its meaning.
 
9682
 
 
9683
  @note
 
9684
    An outer join can be replaced by an inner join if the where condition
 
9685
    or the on expression for an embedding nested join contains a conjunctive
 
9686
    predicate rejecting null values for some attribute of the inner tables.
 
9687
 
 
9688
    E.g. in the query:    
 
9689
    @code
 
9690
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9691
    @endcode
 
9692
    the predicate t2.b < 5 rejects nulls.
 
9693
    The query is converted first to:
 
9694
    @code
 
9695
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9696
    @endcode
 
9697
    then to the equivalent form:
 
9698
    @code
 
9699
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
 
9700
    @endcode
 
9701
 
 
9702
 
 
9703
    Similarly the following query:
 
9704
    @code
 
9705
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
 
9706
        WHERE t2.c < 5  
 
9707
    @endcode
 
9708
    is converted to:
 
9709
    @code
 
9710
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b 
 
9711
 
 
9712
    @endcode
 
9713
 
 
9714
    One conversion might trigger another:
 
9715
    @code
 
9716
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
 
9717
                       LEFT JOIN t3 ON t3.b=t2.b
 
9718
        WHERE t3 IS NOT NULL =>
 
9719
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
 
9720
        WHERE t3 IS NOT NULL AND t3.b=t2.b => 
 
9721
      SELECT * FROM t1, t2, t3
 
9722
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
 
9723
  @endcode
 
9724
 
 
9725
    The function removes all unnecessary braces from the expression
 
9726
    produced by the conversions.
 
9727
    E.g.
 
9728
    @code
 
9729
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9730
    @endcode
 
9731
    finally is converted to: 
 
9732
    @code
 
9733
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9734
 
 
9735
    @endcode
 
9736
 
 
9737
 
 
9738
    It also will remove braces from the following queries:
 
9739
    @code
 
9740
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
 
9741
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
 
9742
    @endcode
 
9743
 
 
9744
    The benefit of this simplification procedure is that it might return 
 
9745
    a query for which the optimizer can evaluate execution plan with more
 
9746
    join orders. With a left join operation the optimizer does not
 
9747
    consider any plan where one of the inner tables is before some of outer
 
9748
    tables.
 
9749
 
 
9750
  IMPLEMENTATION
 
9751
    The function is implemented by a recursive procedure.  On the recursive
 
9752
    ascent all attributes are calculated, all outer joins that can be
 
9753
    converted are replaced and then all unnecessary braces are removed.
 
9754
    As join list contains join tables in the reverse order sequential
 
9755
    elimination of outer joins does not require extra recursive calls.
 
9756
 
 
9757
  SEMI-JOIN NOTES
 
9758
    Remove all semi-joins that have are within another semi-join (i.e. have
 
9759
    an "ancestor" semi-join nest)
 
9760
 
 
9761
  EXAMPLES
 
9762
    Here is an example of a join query with invalid cross references:
 
9763
    @code
 
9764
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b 
 
9765
    @endcode
 
9766
 
 
9767
  @param join        reference to the query info
 
9768
  @param join_list   list representation of the join to be converted
 
9769
  @param conds       conditions to add on expressions for converted joins
 
9770
  @param top         true <=> conds is the where condition
 
9771
 
 
9772
  @return
 
9773
    - The new condition, if success
 
9774
    - 0, otherwise
 
9775
*/
 
9776
 
 
9777
static COND *
 
9778
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
 
9779
               bool in_sj)
 
9780
{
 
9781
  TABLE_LIST *table;
 
9782
  NESTED_JOIN *nested_join;
 
9783
  TABLE_LIST *prev_table= 0;
 
9784
  List_iterator<TABLE_LIST> li(*join_list);
 
9785
 
 
9786
  /* 
 
9787
    Try to simplify join operations from join_list.
 
9788
    The most outer join operation is checked for conversion first. 
 
9789
  */
 
9790
  while ((table= li++))
 
9791
  {
 
9792
    table_map used_tables;
 
9793
    table_map not_null_tables= (table_map) 0;
 
9794
 
 
9795
    if ((nested_join= table->nested_join))
 
9796
    {
 
9797
      /* 
 
9798
         If the element of join_list is a nested join apply
 
9799
         the procedure to its nested join list first.
 
9800
      */
 
9801
      if (table->on_expr)
 
9802
      {
 
9803
        Item *expr= table->on_expr;
 
9804
        /* 
 
9805
           If an on expression E is attached to the table, 
 
9806
           check all null rejected predicates in this expression.
 
9807
           If such a predicate over an attribute belonging to
 
9808
           an inner table of an embedded outer join is found,
 
9809
           the outer join is converted to an inner join and
 
9810
           the corresponding on expression is added to E. 
 
9811
        */ 
 
9812
        expr= simplify_joins(join, &nested_join->join_list,
 
9813
                             expr, false, in_sj || table->sj_on_expr);
 
9814
 
 
9815
        if (!table->prep_on_expr || expr != table->on_expr)
 
9816
        {
 
9817
          assert(expr);
 
9818
 
 
9819
          table->on_expr= expr;
 
9820
          table->prep_on_expr= expr->copy_andor_structure(join->thd);
 
9821
        }
 
9822
      }
 
9823
      nested_join->used_tables= (table_map) 0;
 
9824
      nested_join->not_null_tables=(table_map) 0;
 
9825
      conds= simplify_joins(join, &nested_join->join_list, conds, top, 
 
9826
                            in_sj || table->sj_on_expr);
 
9827
      used_tables= nested_join->used_tables;
 
9828
      not_null_tables= nested_join->not_null_tables;  
 
9829
    }
 
9830
    else
 
9831
    {
 
9832
      if (!table->prep_on_expr)
 
9833
        table->prep_on_expr= table->on_expr;
 
9834
      used_tables= table->table->map;
 
9835
      if (conds)
 
9836
        not_null_tables= conds->not_null_tables();
 
9837
    }
 
9838
      
 
9839
    if (table->embedding)
 
9840
    {
 
9841
      table->embedding->nested_join->used_tables|= used_tables;
 
9842
      table->embedding->nested_join->not_null_tables|= not_null_tables;
 
9843
    }
 
9844
 
 
9845
    if (!table->outer_join || (used_tables & not_null_tables))
 
9846
    {
 
9847
      /* 
 
9848
        For some of the inner tables there are conjunctive predicates
 
9849
        that reject nulls => the outer join can be replaced by an inner join.
 
9850
      */
 
9851
      table->outer_join= 0;
 
9852
      if (table->on_expr)
 
9853
      {
 
9854
        /* Add ON expression to the WHERE or upper-level ON condition. */
 
9855
        if (conds)
 
9856
        {
 
9857
          conds= and_conds(conds, table->on_expr);
 
9858
          conds->top_level_item();
 
9859
          /* conds is always a new item as both cond and on_expr existed */
 
9860
          assert(!conds->fixed);
 
9861
          conds->fix_fields(join->thd, &conds);
 
9862
        }
 
9863
        else
 
9864
          conds= table->on_expr; 
 
9865
        table->prep_on_expr= table->on_expr= 0;
 
9866
      }
 
9867
    }
 
9868
    
 
9869
    if (!top)
 
9870
      continue;
 
9871
 
 
9872
    /* 
 
9873
      Only inner tables of non-convertible outer joins
 
9874
      remain with on_expr.
 
9875
    */ 
 
9876
    if (table->on_expr)
 
9877
    {
 
9878
      table->dep_tables|= table->on_expr->used_tables(); 
 
9879
      if (table->embedding)
 
9880
      {
 
9881
        table->dep_tables&= ~table->embedding->nested_join->used_tables;   
 
9882
        /*
 
9883
           Embedding table depends on tables used
 
9884
           in embedded on expressions. 
 
9885
        */
 
9886
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
 
9887
      }
 
9888
      else
 
9889
        table->dep_tables&= ~table->table->map;
 
9890
    }
 
9891
 
 
9892
    if (prev_table)
 
9893
    {
 
9894
      /* The order of tables is reverse: prev_table follows table */
 
9895
      if (prev_table->straight)
 
9896
        prev_table->dep_tables|= used_tables;
 
9897
      if (prev_table->on_expr)
 
9898
      {
 
9899
        prev_table->dep_tables|= table->on_expr_dep_tables;
 
9900
        table_map prev_used_tables= prev_table->nested_join ?
 
9901
                                    prev_table->nested_join->used_tables :
 
9902
                                    prev_table->table->map;
 
9903
        /* 
 
9904
          If on expression contains only references to inner tables
 
9905
          we still make the inner tables dependent on the outer tables.
 
9906
          It would be enough to set dependency only on one outer table
 
9907
          for them. Yet this is really a rare case.
 
9908
        */  
 
9909
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
 
9910
          prev_table->dep_tables|= used_tables;
 
9911
      }
 
9912
    }
 
9913
    prev_table= table;
 
9914
  }
 
9915
    
 
9916
  /* 
 
9917
    Flatten nested joins that can be flattened.
 
9918
    no ON expression and not a semi-join => can be flattened.
 
9919
  */
 
9920
  li.rewind();
 
9921
  while ((table= li++))
 
9922
  {
 
9923
    nested_join= table->nested_join;
 
9924
    if (table->sj_on_expr && !in_sj)
 
9925
    {
 
9926
       /*
 
9927
         If this is a semi-join that is not contained within another semi-join, 
 
9928
         leave it intact (otherwise it is flattened)
 
9929
       */
 
9930
      join->select_lex->sj_nests.push_back(table);
 
9931
    }
 
9932
    else if (nested_join && !table->on_expr)
 
9933
    {
 
9934
      TABLE_LIST *tbl;
 
9935
      List_iterator<TABLE_LIST> it(nested_join->join_list);
 
9936
      while ((tbl= it++))
 
9937
      {
 
9938
        tbl->embedding= table->embedding;
 
9939
        tbl->join_list= table->join_list;
 
9940
      }      
 
9941
      li.replace(nested_join->join_list);
 
9942
    }
 
9943
  }
 
9944
  return(conds); 
 
9945
}
 
9946
 
 
9947
 
 
9948
/**
 
9949
  Assign each nested join structure a bit in nested_join_map.
 
9950
 
 
9951
    Assign each nested join structure (except "confluent" ones - those that
 
9952
    embed only one element) a bit in nested_join_map.
 
9953
 
 
9954
  @param join          Join being processed
 
9955
  @param join_list     List of tables
 
9956
  @param first_unused  Number of first unused bit in nested_join_map before the
 
9957
                       call
 
9958
 
 
9959
  @note
 
9960
    This function is called after simplify_joins(), when there are no
 
9961
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
 
9962
    we will not run out of bits in nested_join_map.
 
9963
 
 
9964
  @return
 
9965
    First unused bit in nested_join_map after the call.
 
9966
*/
 
9967
 
 
9968
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, 
 
9969
                                          uint first_unused)
 
9970
{
 
9971
  List_iterator<TABLE_LIST> li(*join_list);
 
9972
  TABLE_LIST *table;
 
9973
  while ((table= li++))
 
9974
  {
 
9975
    NESTED_JOIN *nested_join;
 
9976
    if ((nested_join= table->nested_join))
 
9977
    {
 
9978
      /*
 
9979
        It is guaranteed by simplify_joins() function that a nested join
 
9980
        that has only one child is either
 
9981
         - a single-table view (the child is the underlying table), or 
 
9982
         - a single-table semi-join nest
 
9983
 
 
9984
        We don't assign bits to such sj-nests because 
 
9985
        1. it is redundant (a "sequence" of one table cannot be interleaved 
 
9986
            with anything)
 
9987
        2. we could run out bits in nested_join_map otherwise.
 
9988
      */
 
9989
      if (nested_join->join_list.elements != 1)
 
9990
      {
 
9991
        /* Don't assign bits to sj-nests */
 
9992
        if (table->on_expr)
 
9993
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
 
9994
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
 
9995
                                                    first_unused);
 
9996
      }
 
9997
    }
 
9998
  }
 
9999
  return(first_unused);
 
10000
}
 
10001
 
 
10002
 
 
10003
/**
 
10004
  Set NESTED_JOIN::counter=0 in all nested joins in passed list.
 
10005
 
 
10006
    Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
 
10007
    the passed join_list.
 
10008
 
 
10009
  @param join_list  List of nested joins to process. It may also contain base
 
10010
                    tables which will be ignored.
 
10011
*/
 
10012
 
 
10013
static void reset_nj_counters(List<TABLE_LIST> *join_list)
 
10014
{
 
10015
  List_iterator<TABLE_LIST> li(*join_list);
 
10016
  TABLE_LIST *table;
 
10017
  while ((table= li++))
 
10018
  {
 
10019
    NESTED_JOIN *nested_join;
 
10020
    if ((nested_join= table->nested_join))
 
10021
    {
 
10022
      nested_join->counter_= 0;
 
10023
      reset_nj_counters(&nested_join->join_list);
 
10024
    }
 
10025
  }
 
10026
  return;
 
10027
}
 
10028
 
2536
10029
 
2537
10030
/**
2538
10031
  Check interleaving with an inner tables of an outer join for
2539
10032
  extension table.
2540
10033
 
2541
 
    Check if table next_tab can be added to current partial join order, and
 
10034
    Check if table next_tab can be added to current partial join order, and 
2542
10035
    if yes, record that it has been added.
2543
10036
 
2544
10037
    The function assumes that both current partial join order and its
2545
10038
    extension with next_tab are valid wrt table dependencies.
2546
10039
 
2547
10040
  @verbatim
2548
 
     IMPLEMENTATION
2549
 
       LIMITATIONS ON JOIN order_st
 
10041
     IMPLEMENTATION 
 
10042
       LIMITATIONS ON JOIN ORDER
2550
10043
         The nested [outer] joins executioner algorithm imposes these limitations
2551
10044
         on join order:
2552
 
         1. "Outer tables first" -  any "outer" table must be before any
 
10045
         1. "Outer tables first" -  any "outer" table must be before any 
2553
10046
             corresponding "inner" table.
2554
10047
         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
 
10048
            sequence in join order (i.e. the sequence must not be interrupted by 
2556
10049
            tables that are outside of this nested join).
2557
10050
 
2558
10051
         #1 is checked elsewhere, this function checks #2 provided that #1 has
2559
10052
         been already checked.
2560
10053
 
2561
10054
       WHY NEED NON-INTERLEAVING
2562
 
         Consider an example:
 
10055
         Consider an example: 
2563
10056
 
2564
10057
           select * from t0 join t1 left join (t2 join t3) on cond1
2565
10058
 
2583
10076
         The limitations on join order can be rephrased as follows: for valid
2584
10077
         join order one must be able to:
2585
10078
           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
 
10079
           2. for each nested join, put one '(' and one ')' on the said line        
2587
10080
           3. write "LEFT JOIN" and "ON (...)" where appropriate
2588
10081
           4. get a query equivalent to the query we're trying to execute.
2589
10082
 
2590
10083
         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
 
10084
         above described line from left to right. 
 
10085
         A single check_interleaving_with_nj(A,B) call is equivalent to writing 
2593
10086
         table B and appropriate brackets on condition that table A and
2594
10087
         appropriate brackets is the last what was written. Graphically the
2595
10088
         transition is as follows:
2602
10095
                                                      position.
2603
10096
 
2604
10097
         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
 
10098
           The caller guarantees that there is no more then one X-bracket by 
 
10099
           checking "!(remaining_tables & s->dependent)" before calling this 
2607
10100
           function. X-bracket may have a pair in Y-bracket.
2608
10101
 
2609
10102
         When "writing" we store/update this auxilary info about the current
2610
10103
         position:
2611
10104
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
2612
10105
             joins) we've opened but didn't close.
2613
 
          2. {each NestedJoin class not simplified away}->counter - number
 
10106
          2. {each NESTED_JOIN structure not simplified away}->counter - number
2614
10107
             of this nested join's children that have already been added to to
2615
10108
             the partial join order.
2616
10109
  @endverbatim
2617
10110
 
2618
10111
  @param join       Join being processed
 
10112
  @param last_tab   Last table in current partial join order (this function is
 
10113
                    not called for empty partial join orders)
2619
10114
  @param next_tab   Table we're going to extend the current partial join with
2620
10115
 
2621
10116
  @retval
2624
10119
  @retval
2625
10120
    true   Requested join order extension not allowed.
2626
10121
*/
2627
 
bool check_interleaving_with_nj(JoinTable *next_tab)
 
10122
 
 
10123
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
2628
10124
{
2629
 
  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
2630
 
  Join *join= next_tab->join;
 
10125
  TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding;
 
10126
  JOIN *join= last_tab->join;
2631
10127
 
2632
 
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
 
10128
  if (join->cur_embedding_map & ~next_tab->embedding_map)
2633
10129
  {
2634
 
    /*
 
10130
    /* 
2635
10131
      next_tab is outside of the "pair of brackets" we're currently in.
2636
10132
      Cannot add it.
2637
10133
    */
2638
10134
    return true;
2639
10135
  }
2640
 
 
 
10136
   
2641
10137
  /*
2642
10138
    Do update counters for "pairs of brackets" that we've left (marked as
2643
10139
    X,Y,Z in the above picture)
2644
10140
  */
2645
 
  for (;next_emb; next_emb= next_emb->getEmbedding())
 
10141
  for (;next_emb; next_emb= next_emb->embedding)
2646
10142
  {
2647
 
    next_emb->getNestedJoin()->counter_++;
2648
 
    if (next_emb->getNestedJoin()->counter_ == 1)
 
10143
    next_emb->nested_join->counter_++;
 
10144
    if (next_emb->nested_join->counter_ == 1)
2649
10145
    {
2650
 
      /*
 
10146
      /* 
2651
10147
        next_emb is the first table inside a nested join we've "entered". In
2652
10148
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
2653
10149
        X bracket might have Y pair bracket.
2654
10150
      */
2655
 
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
 
10151
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
2656
10152
    }
2657
 
 
2658
 
    if (next_emb->getNestedJoin()->join_list.elements !=
2659
 
        next_emb->getNestedJoin()->counter_)
 
10153
    
 
10154
    if (next_emb->nested_join->join_list.elements !=
 
10155
        next_emb->nested_join->counter_)
2660
10156
      break;
2661
10157
 
2662
10158
    /*
2663
10159
      We're currently at Y or Z-bracket as depicted in the above picture.
2664
10160
      Mark that we've left it and continue walking up the brackets hierarchy.
2665
10161
    */
2666
 
    join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
 
10162
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
2667
10163
  }
2668
10164
  return false;
2669
10165
}
2670
10166
 
2671
 
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
2672
 
{
2673
 
  Session *session= join->session;
 
10167
 
 
10168
/**
 
10169
  Nested joins perspective: Remove the last table from the join order.
 
10170
 
 
10171
    Remove the last table from the partial join order and update the nested
 
10172
    joins counters and join->cur_embedding_map. It is ok to call this 
 
10173
    function for the first table in join order (for which 
 
10174
    check_interleaving_with_nj has not been called)
 
10175
 
 
10176
  @param last  join table to remove, it is assumed to be the last in current
 
10177
               partial join order.
 
10178
*/
 
10179
 
 
10180
static void restore_prev_nj_state(JOIN_TAB *last)
 
10181
{
 
10182
  TABLE_LIST *last_emb= last->table->pos_in_table_list->embedding;
 
10183
  JOIN *join= last->join;
 
10184
  while (last_emb)
 
10185
  {
 
10186
    if (last_emb->on_expr)
 
10187
    {
 
10188
      if (!(--last_emb->nested_join->counter_))
 
10189
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
 
10190
      else if (last_emb->nested_join->join_list.elements-1 ==
 
10191
               last_emb->nested_join->counter_) 
 
10192
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
 
10193
      else
 
10194
        break;
 
10195
    }
 
10196
    last_emb= last_emb->embedding;
 
10197
  }
 
10198
}
 
10199
 
 
10200
 
 
10201
 
 
10202
static 
 
10203
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
 
10204
{
 
10205
  TABLE_LIST *emb_sj_nest;
 
10206
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10207
  {
 
10208
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
 
10209
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
 
10210
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
 
10211
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10212
  }
 
10213
}
 
10214
 
 
10215
 
 
10216
/*
 
10217
  we assume remaining_tables doesnt contain @tab.
 
10218
*/
 
10219
 
 
10220
static void restore_prev_sj_state(const table_map remaining_tables, 
 
10221
                                  const JOIN_TAB *tab)
 
10222
{
 
10223
  TABLE_LIST *emb_sj_nest;
 
10224
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10225
  {
 
10226
    /* If we're removing the last SJ-inner table, remove the sj-nest */
 
10227
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) == 
 
10228
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
 
10229
    {
 
10230
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10231
    }
 
10232
  }
 
10233
}
 
10234
 
 
10235
 
 
10236
static COND *
 
10237
optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list,
 
10238
              Item::cond_result *cond_value)
 
10239
{
 
10240
  THD *thd= join->thd;
2674
10241
 
2675
10242
  if (!conds)
2676
10243
    *cond_value= Item::COND_TRUE;
2677
10244
  else
2678
10245
  {
2679
 
    /*
 
10246
    /* 
2680
10247
      Build all multiple equality predicates and eliminate equality
2681
10248
      predicates that can be inferred from these multiple equalities.
2682
10249
      For each reference of a field included into a multiple equality
2683
10250
      that occurs in a function set a pointer to the multiple equality
2684
10251
      predicate. Substitute a constant instead of this field if the
2685
10252
      multiple equality contains a constant.
2686
 
    */
2687
 
    conds= build_equal_items(join->session, conds, NULL, join_list,
 
10253
    */ 
 
10254
    conds= build_equal_items(join->thd, conds, NULL, join_list,
2688
10255
                             &join->cond_equal);
2689
10256
 
2690
10257
    /* change field = field to field = const for each found field = const */
2691
 
    list<COND_CMP> temp;
2692
 
    propagate_cond_constants(session, temp, conds, conds);
 
10258
    propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
2693
10259
    /*
2694
10260
      Remove all instances of item == item
2695
10261
      Remove all and-levels where CONST item != CONST item
2696
10262
    */
2697
 
    conds= remove_eq_conds(session, conds, cond_value) ;
 
10263
    conds= remove_eq_conds(thd, conds, cond_value) ;
2698
10264
  }
2699
10265
  return(conds);
2700
10266
}
2701
10267
 
 
10268
 
2702
10269
/**
2703
10270
  Remove const and eq items.
2704
10271
 
2709
10276
    - COND_TRUE   : always true ( 1 = 1 )
2710
10277
    - COND_FALSE  : always false        ( 1 = 2 )
2711
10278
*/
2712
 
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
10279
 
 
10280
COND *
 
10281
remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value)
2713
10282
{
2714
10283
  if (cond->type() == Item::COND_ITEM)
2715
10284
  {
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());
 
10285
    bool and_level= ((Item_cond*) cond)->functype()
 
10286
      == Item_func::COND_AND_FUNC;
 
10287
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2719
10288
    Item::cond_result tmp_cond_value;
2720
 
    bool should_fix_fields= false;
 
10289
    bool should_fix_fields=0;
2721
10290
 
2722
 
    *cond_value= Item::COND_UNDEF;
 
10291
    *cond_value=Item::COND_UNDEF;
2723
10292
    Item *item;
2724
 
    while ((item= li++))
 
10293
    while ((item=li++))
2725
10294
    {
2726
 
      Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
2727
 
      if (! new_item)
2728
 
              li.remove();
 
10295
      Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value);
 
10296
      if (!new_item)
 
10297
        li.remove();
2729
10298
      else if (item != new_item)
2730
10299
      {
2731
 
        li.replace(new_item);
2732
 
        should_fix_fields= true;
 
10300
        VOID(li.replace(new_item));
 
10301
        should_fix_fields=1;
2733
10302
      }
2734
10303
      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;
 
10304
        *cond_value=tmp_cond_value;
 
10305
      switch (tmp_cond_value) {
 
10306
      case Item::COND_OK:                       // Not true or false
 
10307
        if (and_level || *cond_value == Item::COND_FALSE)
 
10308
          *cond_value=tmp_cond_value;
 
10309
        break;
 
10310
      case Item::COND_FALSE:
 
10311
        if (and_level)
 
10312
        {
 
10313
          *cond_value=tmp_cond_value;
 
10314
          return (COND*) 0;                     // Always false
 
10315
        }
 
10316
        break;
 
10317
      case Item::COND_TRUE:
 
10318
        if (!and_level)
 
10319
        {
 
10320
          *cond_value= tmp_cond_value;
 
10321
          return (COND*) 0;                     // Always true
 
10322
        }
 
10323
        break;
 
10324
      case Item::COND_UNDEF:                    // Impossible
 
10325
        break; /* purecov: deadcode */
2759
10326
      }
2760
10327
    }
2761
 
 
2762
10328
    if (should_fix_fields)
2763
10329
      cond->update_used_tables();
2764
10330
 
2765
 
    if (! ((Item_cond*) cond)->argument_list()->elements || *cond_value != Item::COND_OK)
2766
 
      return (COND*) NULL;
2767
 
 
 
10331
    if (!((Item_cond*) cond)->argument_list()->elements ||
 
10332
        *cond_value != Item::COND_OK)
 
10333
      return (COND*) 0;
2768
10334
    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 */
 
10335
    {                                           // Remove list
2771
10336
      item= ((Item_cond*) cond)->argument_list()->head();
2772
 
      ((Item_cond*) cond)->argument_list()->clear();
 
10337
      ((Item_cond*) cond)->argument_list()->empty();
2773
10338
      return item;
2774
10339
    }
2775
10340
  }
2776
 
  else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
 
10341
  else if (cond->type() == Item::FUNC_ITEM &&
 
10342
           ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
2777
10343
  {
2778
10344
    /*
2779
10345
      Handles this special case for some ODBC applications:
2785
10351
      SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
2786
10352
    */
2787
10353
 
2788
 
    Item_func_isnull *func= (Item_func_isnull*) cond;
 
10354
    Item_func_isnull *func=(Item_func_isnull*) cond;
2789
10355
    Item **args= func->arguments();
2790
10356
    if (args[0]->type() == Item::FIELD_ITEM)
2791
10357
    {
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
 
          )
 
10358
      Field *field=((Item_field*) args[0])->field;
 
10359
      if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
 
10360
          (thd->options & OPTION_AUTO_IS_NULL) &&
 
10361
          (thd->first_successful_insert_id_in_prev_stmt > 0 &&
 
10362
           thd->substitute_null_with_insert_id))
2801
10363
      {
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;
 
10364
        COND *new_cond;
 
10365
        if ((new_cond= new Item_func_eq(args[0],
 
10366
                                        new Item_int("last_insert_id()",
 
10367
                                                     thd->read_first_successful_insert_id_in_prev_stmt(),
 
10368
                                                     MY_INT64_NUM_DECIMAL_DIGITS))))
 
10369
        {
 
10370
          cond=new_cond;
2808
10371
          /*
2809
10372
            Item_func_eq can't be fixed after creation so we do not check
2810
10373
            cond->fixed, also it do not need tables so we use 0 as second
2811
10374
            argument.
2812
10375
          */
2813
 
          cond->fix_fields(session, &cond);
2814
 
        }
 
10376
          cond->fix_fields(thd, &cond);
 
10377
        }
2815
10378
        /*
2816
10379
          IS NULL should be mapped to LAST_INSERT_ID only for first row, so
2817
10380
          clear for next row
2818
10381
        */
2819
 
        session->substitute_null_with_insert_id= false;
 
10382
        thd->substitute_null_with_insert_id= false;
2820
10383
      }
2821
 
#ifdef NOTDEFINED
2822
10384
      /* 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)
 
10385
      else if (((field->type() == DRIZZLE_TYPE_NEWDATE) ||
 
10386
                (field->type() == DRIZZLE_TYPE_DATETIME)) &&
 
10387
                (field->flags & NOT_NULL_FLAG) &&
 
10388
               !field->table->maybe_null)
2827
10389
      {
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;
 
10390
        COND *new_cond;
 
10391
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
 
10392
        {
 
10393
          cond=new_cond;
2832
10394
          /*
2833
10395
            Item_func_eq can't be fixed after creation so we do not check
2834
10396
            cond->fixed, also it do not need tables so we use 0 as second
2835
10397
            argument.
2836
10398
          */
2837
 
          cond->fix_fields(session, &cond);
2838
 
        }
 
10399
          cond->fix_fields(thd, &cond);
 
10400
        }
2839
10401
      }
2840
 
#endif /* NOTDEFINED */
2841
10402
    }
2842
10403
    if (cond->const_item())
2843
10404
    {
2844
10405
      *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2845
 
      return (COND *) NULL;
 
10406
      return (COND*) 0;
2846
10407
    }
2847
10408
  }
2848
10409
  else if (cond->const_item() && !cond->is_expensive())
2849
10410
  /*
2850
 
    @todo
 
10411
    TODO:
2851
10412
    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
 
 
 
10413
    materialized IN because it is created later than this phase, and cannot be
 
10414
    evaluated at this point.
 
10415
    The condition should be something as (need to fix member access):
 
10416
      !(cond->type() == Item::FUNC_ITEM &&
 
10417
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
 
10418
        ((Item_in_optimizer*)cond)->is_expensive()))
2858
10419
  */
2859
10420
  {
2860
10421
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2861
 
    return (COND *) NULL;
 
10422
    return (COND*) 0;
2862
10423
  }
2863
10424
  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
2864
 
  {                                             
2865
 
    /* boolan compare function */
 
10425
  {                                             // boolan compare function
2866
10426
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2867
10427
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2868
10428
    if (left_item->eq(right_item,1))
2869
10429
    {
2870
 
      if (!left_item->maybe_null || ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
2871
 
              return (COND*) NULL;                      /* Comparison of identical items */
 
10430
      if (!left_item->maybe_null ||
 
10431
          ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
 
10432
        return (COND*) 0;                       // Compare of identical items
2872
10433
    }
2873
10434
  }
2874
 
  *cond_value= Item::COND_OK;
2875
 
  return cond;                                  /* Point at next and return into recursion */
 
10435
  *cond_value=Item::COND_OK;
 
10436
  return cond;                                  // Point at next and level
2876
10437
}
2877
10438
 
2878
 
/*
 
10439
/* 
2879
10440
  Check if equality can be used in removing components of GROUP BY/DISTINCT
2880
 
 
 
10441
  
2881
10442
  SYNOPSIS
2882
10443
    test_if_equality_guarantees_uniqueness()
2883
10444
      l          the left comparison argument (a field if any)
2884
10445
      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
 
10446
  
 
10447
  DESCRIPTION    
 
10448
    Checks if an equality predicate can be used to take away 
 
10449
    DISTINCT/GROUP BY because it is known to be true for exactly one 
2889
10450
    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
 
10451
    Arguments must be of the same type because e.g. 
 
10452
    <string_field> = <int_const> may match more than 1 distinct value from 
 
10453
    the column. 
 
10454
    We must take into consideration and the optimization done for various 
2894
10455
    string constants when compared to dates etc (see Item_int_with_ref) as
2895
10456
    well as the collation of the arguments.
2896
 
 
2897
 
  RETURN VALUE
 
10457
  
 
10458
  RETURN VALUE  
2898
10459
    true    can be used
2899
10460
    false   cannot be used
2900
10461
*/
2901
 
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
10462
static bool
 
10463
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
2902
10464
{
2903
10465
  return r->const_item() &&
2904
10466
    /* elements must be compared as dates */
2913
10475
/**
2914
10476
  Return true if the item is a const value in all the WHERE clause.
2915
10477
*/
2916
 
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
10478
 
 
10479
static bool
 
10480
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
2917
10481
{
2918
10482
  if (cond->type() == Item::COND_ITEM)
2919
10483
  {
2920
10484
    bool and_level= (((Item_cond*) cond)->functype()
2921
10485
                     == Item_func::COND_AND_FUNC);
2922
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
10486
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2923
10487
    Item *item;
2924
10488
    while ((item=li++))
2925
10489
    {
2926
10490
      bool res=const_expression_in_where(item, comp_item, const_item);
2927
10491
      if (res)                                  // Is a const value
2928
10492
      {
2929
 
        if (and_level)
2930
 
          return 1;
 
10493
        if (and_level)
 
10494
          return 1;
2931
10495
      }
2932
10496
      else if (!and_level)
2933
 
        return 0;
 
10497
        return 0;
2934
10498
    }
2935
10499
    return and_level ? 0 : 1;
2936
10500
  }
2938
10502
  {                                             // boolan compare function
2939
10503
    Item_func* func= (Item_func*) cond;
2940
10504
    if (func->functype() != Item_func::EQUAL_FUNC &&
2941
 
              func->functype() != Item_func::EQ_FUNC)
 
10505
        func->functype() != Item_func::EQ_FUNC)
2942
10506
      return 0;
2943
10507
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2944
10508
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2946
10510
    {
2947
10511
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
2948
10512
      {
2949
 
        if (*const_item)
2950
 
          return right_item->eq(*const_item, 1);
2951
 
        *const_item=right_item;
2952
 
        return 1;
 
10513
        if (*const_item)
 
10514
          return right_item->eq(*const_item, 1);
 
10515
        *const_item=right_item;
 
10516
        return 1;
2953
10517
      }
2954
10518
    }
2955
10519
    else if (right_item->eq(comp_item,1))
2956
10520
    {
2957
10521
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
2958
10522
      {
2959
 
        if (*const_item)
2960
 
          return left_item->eq(*const_item, 1);
2961
 
        *const_item=left_item;
2962
 
        return 1;
2963
 
      }
2964
 
    }
2965
 
  }
2966
 
  return 0;
2967
 
}
 
10523
        if (*const_item)
 
10524
          return left_item->eq(*const_item, 1);
 
10525
        *const_item=left_item;
 
10526
        return 1;
 
10527
      }
 
10528
    }
 
10529
  }
 
10530
  return 0;
 
10531
}
 
10532
 
 
10533
/****************************************************************************
 
10534
  Create internal temporary table
 
10535
****************************************************************************/
 
10536
 
 
10537
/**
 
10538
  Create field for temporary table from given field.
 
10539
 
 
10540
  @param thd           Thread handler
 
10541
  @param org_field    field from which new field will be created
 
10542
  @param name         New field name
 
10543
  @param table         Temporary table
 
10544
  @param item          !=NULL if item->result_field should point to new field.
 
10545
                      This is relevant for how fill_record() is going to work:
 
10546
                      If item != NULL then fill_record() will update
 
10547
                      the record in the original table.
 
10548
                      If item == NULL then fill_record() will update
 
10549
                      the temporary table
 
10550
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
 
10551
                               field instead of blob.
 
10552
 
 
10553
  @retval
 
10554
    NULL                on error
 
10555
  @retval
 
10556
    new_created field
 
10557
*/
 
10558
 
 
10559
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
 
10560
                                   const char *name, TABLE *table,
 
10561
                                   Item_field *item, uint convert_blob_length)
 
10562
{
 
10563
  Field *new_field;
 
10564
 
 
10565
  /* 
 
10566
    Make sure that the blob fits into a Field_varstring which has 
 
10567
    2-byte lenght. 
 
10568
  */
 
10569
  if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
 
10570
      (org_field->flags & BLOB_FLAG))
 
10571
    new_field= new Field_varstring(convert_blob_length,
 
10572
                                   org_field->maybe_null(),
 
10573
                                   org_field->field_name, table->s,
 
10574
                                   org_field->charset());
 
10575
  else
 
10576
    new_field= org_field->new_field(thd->mem_root, table,
 
10577
                                    table == org_field->table);
 
10578
  if (new_field)
 
10579
  {
 
10580
    new_field->init(table);
 
10581
    new_field->orig_table= org_field->orig_table;
 
10582
    if (item)
 
10583
      item->result_field= new_field;
 
10584
    else
 
10585
      new_field->field_name= name;
 
10586
    new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
 
10587
    if (org_field->maybe_null() || (item && item->maybe_null))
 
10588
      new_field->flags&= ~NOT_NULL_FLAG;        // Because of outer join
 
10589
    if (org_field->type() == DRIZZLE_TYPE_VARCHAR)
 
10590
      table->s->db_create_options|= HA_OPTION_PACK_RECORD;
 
10591
    else if (org_field->type() == DRIZZLE_TYPE_DOUBLE)
 
10592
      ((Field_double *) new_field)->not_fixed= true;
 
10593
  }
 
10594
  return new_field;
 
10595
}
 
10596
 
 
10597
/**
 
10598
  Create field for temporary table using type of given item.
 
10599
 
 
10600
  @param thd                   Thread handler
 
10601
  @param item                  Item to create a field for
 
10602
  @param table                 Temporary table
 
10603
  @param copy_func             If set and item is a function, store copy of
 
10604
                               item in this array
 
10605
  @param modify_item           1 if item->result_field should point to new
 
10606
                               item. This is relevent for how fill_record()
 
10607
                               is going to work:
 
10608
                               If modify_item is 1 then fill_record() will
 
10609
                               update the record in the original table.
 
10610
                               If modify_item is 0 then fill_record() will
 
10611
                               update the temporary table
 
10612
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
 
10613
                               field instead of blob.
 
10614
 
 
10615
  @retval
 
10616
    0  on error
 
10617
  @retval
 
10618
    new_created field
 
10619
*/
 
10620
 
 
10621
static Field *create_tmp_field_from_item(THD *thd __attribute__((unused)),
 
10622
                                         Item *item, TABLE *table,
 
10623
                                         Item ***copy_func, bool modify_item,
 
10624
                                         uint convert_blob_length)
 
10625
{
 
10626
  bool maybe_null= item->maybe_null;
 
10627
  Field *new_field;
 
10628
 
 
10629
  switch (item->result_type()) {
 
10630
  case REAL_RESULT:
 
10631
    new_field= new Field_double(item->max_length, maybe_null,
 
10632
                                item->name, item->decimals, true);
 
10633
    break;
 
10634
  case INT_RESULT:
 
10635
    /* 
 
10636
      Select an integer type with the minimal fit precision.
 
10637
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
 
10638
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into 
 
10639
      Field_long : make them Field_int64_t.  
 
10640
    */
 
10641
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
10642
      new_field=new Field_int64_t(item->max_length, maybe_null,
 
10643
                                   item->name, item->unsigned_flag);
 
10644
    else
 
10645
      new_field=new Field_long(item->max_length, maybe_null,
 
10646
                               item->name, item->unsigned_flag);
 
10647
    break;
 
10648
  case STRING_RESULT:
 
10649
    assert(item->collation.collation);
 
10650
  
 
10651
    enum enum_field_types type;
 
10652
    /*
 
10653
      DATE/TIME fields have STRING_RESULT result type. 
 
10654
      To preserve type they needed to be handled separately.
 
10655
    */
 
10656
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
 
10657
        type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_NEWDATE ||
 
10658
        type == DRIZZLE_TYPE_TIMESTAMP)
 
10659
      new_field= item->tmp_table_field_from_field_type(table, 1);
 
10660
    /* 
 
10661
      Make sure that the blob fits into a Field_varstring which has 
 
10662
      2-byte lenght. 
 
10663
    */
 
10664
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
 
10665
             convert_blob_length <= Field_varstring::MAX_SIZE && 
 
10666
             convert_blob_length)
 
10667
      new_field= new Field_varstring(convert_blob_length, maybe_null,
 
10668
                                     item->name, table->s,
 
10669
                                     item->collation.collation);
 
10670
    else
 
10671
      new_field= item->make_string_field(table);
 
10672
    new_field->set_derivation(item->collation.derivation);
 
10673
    break;
 
10674
  case DECIMAL_RESULT:
 
10675
  {
 
10676
    uint8_t dec= item->decimals;
 
10677
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
10678
    uint32_t len= item->max_length;
 
10679
 
 
10680
    /*
 
10681
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
10682
      will always throw a warning. We must limit dec to
 
10683
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
10684
    */
 
10685
 
 
10686
    if (dec > 0)
 
10687
    {
 
10688
      signed int overflow;
 
10689
 
 
10690
      dec= min(dec, DECIMAL_MAX_SCALE);
 
10691
 
 
10692
      /*
 
10693
        If the value still overflows the field with the corrected dec,
 
10694
        we'll throw out decimals rather than integers. This is still
 
10695
        bad and of course throws a truncation warning.
 
10696
        +1: for decimal point
 
10697
      */
 
10698
 
 
10699
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
10700
                                               item->unsigned_flag) - len;
 
10701
 
 
10702
      if (overflow > 0)
 
10703
        dec= max(0, dec - overflow);            // too long, discard fract
 
10704
      else
 
10705
        len -= item->decimals - dec;            // corrected value fits
 
10706
    }
 
10707
 
 
10708
    new_field= new Field_new_decimal(len, maybe_null, item->name,
 
10709
                                     dec, item->unsigned_flag);
 
10710
    break;
 
10711
  }
 
10712
  case ROW_RESULT:
 
10713
  default:
 
10714
    // This case should never be choosen
 
10715
    assert(0);
 
10716
    new_field= 0;
 
10717
    break;
 
10718
  }
 
10719
  if (new_field)
 
10720
    new_field->init(table);
 
10721
    
 
10722
  if (copy_func && item->is_result_field())
 
10723
    *((*copy_func)++) = item;                   // Save for copy_funcs
 
10724
  if (modify_item)
 
10725
    item->set_result_field(new_field);
 
10726
  if (item->type() == Item::NULL_ITEM)
 
10727
    new_field->is_created_from_null_item= true;
 
10728
  return new_field;
 
10729
}
 
10730
 
 
10731
 
 
10732
/**
 
10733
  Create field for information schema table.
 
10734
 
 
10735
  @param thd            Thread handler
 
10736
  @param table          Temporary table
 
10737
  @param item           Item to create a field for
 
10738
 
 
10739
  @retval
 
10740
    0                   on error
 
10741
  @retval
 
10742
    new_created field
 
10743
*/
 
10744
 
 
10745
Field *create_tmp_field_for_schema(THD *thd __attribute__((unused)),
 
10746
                                   Item *item, TABLE *table)
 
10747
{
 
10748
  if (item->field_type() == DRIZZLE_TYPE_VARCHAR)
 
10749
  {
 
10750
    Field *field;
 
10751
    if (item->max_length > MAX_FIELD_VARCHARLENGTH)
 
10752
      field= new Field_blob(item->max_length, item->maybe_null,
 
10753
                            item->name, item->collation.collation);
 
10754
    else
 
10755
      field= new Field_varstring(item->max_length, item->maybe_null,
 
10756
                                 item->name,
 
10757
                                 table->s, item->collation.collation);
 
10758
    if (field)
 
10759
      field->init(table);
 
10760
    return field;
 
10761
  }
 
10762
  return item->tmp_table_field_from_field_type(table, 0);
 
10763
}
 
10764
 
 
10765
 
 
10766
/**
 
10767
  Create field for temporary table.
 
10768
 
 
10769
  @param thd            Thread handler
 
10770
  @param table          Temporary table
 
10771
  @param item           Item to create a field for
 
10772
  @param type           Type of item (normally item->type)
 
10773
  @param copy_func      If set and item is a function, store copy of item
 
10774
                       in this array
 
10775
  @param from_field    if field will be created using other field as example,
 
10776
                       pointer example field will be written here
 
10777
  @param default_field  If field has a default value field, store it here
 
10778
  @param group          1 if we are going to do a relative group by on result
 
10779
  @param modify_item    1 if item->result_field should point to new item.
 
10780
                       This is relevent for how fill_record() is going to
 
10781
                       work:
 
10782
                       If modify_item is 1 then fill_record() will update
 
10783
                       the record in the original table.
 
10784
                       If modify_item is 0 then fill_record() will update
 
10785
                       the temporary table
 
10786
  @param convert_blob_length If >0 create a varstring(convert_blob_length)
 
10787
                             field instead of blob.
 
10788
 
 
10789
  @retval
 
10790
    0                   on error
 
10791
  @retval
 
10792
    new_created field
 
10793
*/
 
10794
 
 
10795
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
 
10796
                        Item ***copy_func, Field **from_field,
 
10797
                        Field **default_field,
 
10798
                        bool group, bool modify_item,
 
10799
                        bool table_cant_handle_bit_fields __attribute__((unused)),
 
10800
                        bool make_copy_field,
 
10801
                        uint convert_blob_length)
 
10802
{
 
10803
  Field *result;
 
10804
  Item::Type orig_type= type;
 
10805
  Item *orig_item= 0;
 
10806
 
 
10807
  if (type != Item::FIELD_ITEM &&
 
10808
      item->real_item()->type() == Item::FIELD_ITEM)
 
10809
  {
 
10810
    orig_item= item;
 
10811
    item= item->real_item();
 
10812
    type= Item::FIELD_ITEM;
 
10813
  }
 
10814
 
 
10815
  switch (type) {
 
10816
  case Item::SUM_FUNC_ITEM:
 
10817
  {
 
10818
    Item_sum *item_sum=(Item_sum*) item;
 
10819
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
 
10820
    if (!result)
 
10821
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
10822
    return result;
 
10823
  }
 
10824
  case Item::FIELD_ITEM:
 
10825
  case Item::DEFAULT_VALUE_ITEM:
 
10826
  {
 
10827
    Item_field *field= (Item_field*) item;
 
10828
    bool orig_modify= modify_item;
 
10829
    if (orig_type == Item::REF_ITEM)
 
10830
      modify_item= 0;
 
10831
    /*
 
10832
      If item have to be able to store NULLs but underlaid field can't do it,
 
10833
      create_tmp_field_from_field() can't be used for tmp field creation.
 
10834
    */
 
10835
    if (field->maybe_null && !field->field->maybe_null())
 
10836
    {
 
10837
      result= create_tmp_field_from_item(thd, item, table, NULL,
 
10838
                                         modify_item, convert_blob_length);
 
10839
      *from_field= field->field;
 
10840
      if (result && modify_item)
 
10841
        field->result_field= result;
 
10842
    } 
 
10843
    else
 
10844
      result= create_tmp_field_from_field(thd, (*from_field= field->field),
 
10845
                                          orig_item ? orig_item->name :
 
10846
                                          item->name,
 
10847
                                          table,
 
10848
                                          modify_item ? field :
 
10849
                                          NULL,
 
10850
                                          convert_blob_length);
 
10851
    if (orig_type == Item::REF_ITEM && orig_modify)
 
10852
      ((Item_ref*)orig_item)->set_result_field(result);
 
10853
    if (field->field->eq_def(result))
 
10854
      *default_field= field->field;
 
10855
    return result;
 
10856
  }
 
10857
  /* Fall through */
 
10858
  case Item::FUNC_ITEM:
 
10859
    /* Fall through */
 
10860
  case Item::COND_ITEM:
 
10861
  case Item::FIELD_AVG_ITEM:
 
10862
  case Item::FIELD_STD_ITEM:
 
10863
  case Item::SUBSELECT_ITEM:
 
10864
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
 
10865
  case Item::PROC_ITEM:
 
10866
  case Item::INT_ITEM:
 
10867
  case Item::REAL_ITEM:
 
10868
  case Item::DECIMAL_ITEM:
 
10869
  case Item::STRING_ITEM:
 
10870
  case Item::REF_ITEM:
 
10871
  case Item::NULL_ITEM:
 
10872
  case Item::VARBIN_ITEM:
 
10873
    if (make_copy_field)
 
10874
    {
 
10875
      assert(((Item_result_field*)item)->result_field);
 
10876
      *from_field= ((Item_result_field*)item)->result_field;
 
10877
    }
 
10878
    return create_tmp_field_from_item(thd, item, table,
 
10879
                                      (make_copy_field ? 0 : copy_func),
 
10880
                                       modify_item, convert_blob_length);
 
10881
  case Item::TYPE_HOLDER:  
 
10882
    result= ((Item_type_holder *)item)->make_field_by_type(table);
 
10883
    result->set_derivation(item->collation.derivation);
 
10884
    return result;
 
10885
  default:                                      // Dosen't have to be stored
 
10886
    return 0;
 
10887
  }
 
10888
}
 
10889
 
 
10890
/*
 
10891
  Set up column usage bitmaps for a temporary table
 
10892
 
 
10893
  IMPLEMENTATION
 
10894
    For temporary tables, we need one bitmap with all columns set and
 
10895
    a tmp_set bitmap to be used by things like filesort.
 
10896
*/
 
10897
 
 
10898
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
 
10899
{
 
10900
  uint field_count= table->s->fields;
 
10901
  bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
 
10902
              false);
 
10903
  bitmap_init(&table->tmp_set,
 
10904
              (my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
 
10905
              field_count, false);
 
10906
  /* write_set and all_set are copies of read_set */
 
10907
  table->def_write_set= table->def_read_set;
 
10908
  table->s->all_set= table->def_read_set;
 
10909
  bitmap_set_all(&table->s->all_set);
 
10910
  table->default_column_bitmaps();
 
10911
}
 
10912
 
 
10913
 
 
10914
/**
 
10915
  Create a temp table according to a field list.
 
10916
 
 
10917
  Given field pointers are changed to point at tmp_table for
 
10918
  send_fields. The table object is self contained: it's
 
10919
  allocated in its own memory root, as well as Field objects
 
10920
  created for table columns.
 
10921
  This function will replace Item_sum items in 'fields' list with
 
10922
  corresponding Item_field items, pointing at the fields in the
 
10923
  temporary table, unless this was prohibited by true
 
10924
  value of argument save_sum_fields. The Item_field objects
 
10925
  are created in THD memory root.
 
10926
 
 
10927
  @param thd                  thread handle
 
10928
  @param param                a description used as input to create the table
 
10929
  @param fields               list of items that will be used to define
 
10930
                              column types of the table (also see NOTES)
 
10931
  @param group                TODO document
 
10932
  @param distinct             should table rows be distinct
 
10933
  @param save_sum_fields      see NOTES
 
10934
  @param select_options
 
10935
  @param rows_limit
 
10936
  @param table_alias          possible name of the temporary table that can
 
10937
                              be used for name resolving; can be "".
 
10938
*/
 
10939
 
 
10940
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
 
10941
#define AVG_STRING_LENGTH_TO_PACK_ROWS   64
 
10942
#define RATIO_TO_PACK_ROWS             2
 
10943
#define MIN_STRING_LENGTH_TO_PACK_ROWS   10
 
10944
 
 
10945
TABLE *
 
10946
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
 
10947
                 ORDER *group, bool distinct, bool save_sum_fields,
 
10948
                 uint64_t select_options, ha_rows rows_limit,
 
10949
                 char *table_alias)
 
10950
{
 
10951
  MEM_ROOT *mem_root_save, own_root;
 
10952
  TABLE *table;
 
10953
  TABLE_SHARE *share;
 
10954
  uint  i,field_count,null_count,null_pack_length;
 
10955
  uint  copy_func_count= param->func_count;
 
10956
  uint  hidden_null_count, hidden_null_pack_length, hidden_field_count;
 
10957
  uint  blob_count,group_null_items, string_count;
 
10958
  uint  temp_pool_slot=MY_BIT_NONE;
 
10959
  uint fieldnr= 0;
 
10960
  ulong reclength, string_total_length;
 
10961
  bool  using_unique_constraint= 0;
 
10962
  bool  use_packed_rows= 0;
 
10963
  bool  not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
 
10964
  char  *tmpname,path[FN_REFLEN];
 
10965
  uchar *pos, *group_buff, *bitmaps;
 
10966
  uchar *null_flags;
 
10967
  Field **reg_field, **from_field, **default_field;
 
10968
  uint *blob_field;
 
10969
  Copy_field *copy=0;
 
10970
  KEY *keyinfo;
 
10971
  KEY_PART_INFO *key_part_info;
 
10972
  Item **copy_func;
 
10973
  MI_COLUMNDEF *recinfo;
 
10974
  uint total_uneven_bit_length= 0;
 
10975
  bool force_copy_fields= param->force_copy_fields;
 
10976
 
 
10977
  status_var_increment(thd->status_var.created_tmp_tables);
 
10978
 
 
10979
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
 
10980
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
 
10981
 
 
10982
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
 
10983
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
 
10984
            current_pid, temp_pool_slot);
 
10985
  else
 
10986
  {
 
10987
    /* if we run out of slots or we are not using tempool */
 
10988
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
 
10989
            thd->thread_id, thd->tmp_table++);
 
10990
  }
 
10991
 
 
10992
  /*
 
10993
    No need to change table name to lower case as we are only creating
 
10994
    MyISAM or HEAP tables here
 
10995
  */
 
10996
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
 
10997
 
 
10998
 
 
10999
  if (group)
 
11000
  {
 
11001
    if (!param->quick_group)
 
11002
      group=0;                                  // Can't use group key
 
11003
    else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
 
11004
    {
 
11005
      /*
 
11006
        marker == 4 means two things:
 
11007
        - store NULLs in the key, and
 
11008
        - convert BIT fields to 64-bit long, needed because MEMORY tables
 
11009
          can't index BIT fields.
 
11010
      */
 
11011
      (*tmp->item)->marker= 4;
 
11012
      if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
 
11013
        using_unique_constraint=1;
 
11014
    }
 
11015
    if (param->group_length >= MAX_BLOB_WIDTH)
 
11016
      using_unique_constraint=1;
 
11017
    if (group)
 
11018
      distinct=0;                               // Can't use distinct
 
11019
  }
 
11020
 
 
11021
  field_count=param->field_count+param->func_count+param->sum_func_count;
 
11022
  hidden_field_count=param->hidden_field_count;
 
11023
 
 
11024
  /*
 
11025
    When loose index scan is employed as access method, it already
 
11026
    computes all groups and the result of all aggregate functions. We
 
11027
    make space for the items of the aggregate function in the list of
 
11028
    functions TMP_TABLE_PARAM::items_to_copy, so that the values of
 
11029
    these items are stored in the temporary table.
 
11030
  */
 
11031
  if (param->precomputed_group_by)
 
11032
    copy_func_count+= param->sum_func_count;
 
11033
  
 
11034
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
11035
 
 
11036
  if (!multi_alloc_root(&own_root,
 
11037
                        &table, sizeof(*table),
 
11038
                        &share, sizeof(*share),
 
11039
                        &reg_field, sizeof(Field*) * (field_count+1),
 
11040
                        &default_field, sizeof(Field*) * (field_count),
 
11041
                        &blob_field, sizeof(uint)*(field_count+1),
 
11042
                        &from_field, sizeof(Field*)*field_count,
 
11043
                        &copy_func, sizeof(*copy_func)*(copy_func_count+1),
 
11044
                        &param->keyinfo, sizeof(*param->keyinfo),
 
11045
                        &key_part_info,
 
11046
                        sizeof(*key_part_info)*(param->group_parts+1),
 
11047
                        &param->start_recinfo,
 
11048
                        sizeof(*param->recinfo)*(field_count*2+4),
 
11049
                        &tmpname, (uint) strlen(path)+1,
 
11050
                        &group_buff, (group && ! using_unique_constraint ?
 
11051
                                      param->group_length : 0),
 
11052
                        &bitmaps, bitmap_buffer_size(field_count)*2,
 
11053
                        NullS))
 
11054
  {
 
11055
    if (temp_pool_slot != MY_BIT_NONE)
 
11056
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11057
    return(NULL);                               /* purecov: inspected */
 
11058
  }
 
11059
  /* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
 
11060
  if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
 
11061
  {
 
11062
    if (temp_pool_slot != MY_BIT_NONE)
 
11063
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11064
    free_root(&own_root, MYF(0));               /* purecov: inspected */
 
11065
    return(NULL);                               /* purecov: inspected */
 
11066
  }
 
11067
  param->items_to_copy= copy_func;
 
11068
  strmov(tmpname,path);
 
11069
  /* make table according to fields */
 
11070
 
 
11071
  memset((char*) table, 0, sizeof(*table));
 
11072
  memset((char*) reg_field, 0, sizeof(Field*)*(field_count+1));
 
11073
  memset((char*) default_field, 0, sizeof(Field*) * (field_count));
 
11074
  memset((char*) from_field, 0, sizeof(Field*)*field_count);
 
11075
 
 
11076
  table->mem_root= own_root;
 
11077
  mem_root_save= thd->mem_root;
 
11078
  thd->mem_root= &table->mem_root;
 
11079
 
 
11080
  table->field=reg_field;
 
11081
  table->alias= table_alias;
 
11082
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
 
11083
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
 
11084
  table->map=1;
 
11085
  table->temp_pool_slot = temp_pool_slot;
 
11086
  table->copy_blobs= 1;
 
11087
  table->in_use= thd;
 
11088
  table->quick_keys.init();
 
11089
  table->covering_keys.init();
 
11090
  table->keys_in_use_for_query.init();
 
11091
 
 
11092
  table->s= share;
 
11093
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
 
11094
  share->blob_field= blob_field;
 
11095
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
11096
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
 
11097
  share->table_charset= param->table_charset;
 
11098
  share->primary_key= MAX_KEY;               // Indicate no primary key
 
11099
  share->keys_for_keyread.init();
 
11100
  share->keys_in_use.init();
 
11101
 
 
11102
  /* Calculate which type of fields we will store in the temporary table */
 
11103
 
 
11104
  reclength= string_total_length= 0;
 
11105
  blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
 
11106
  param->using_indirect_summary_function=0;
 
11107
 
 
11108
  List_iterator_fast<Item> li(fields);
 
11109
  Item *item;
 
11110
  Field **tmp_from_field=from_field;
 
11111
  while ((item=li++))
 
11112
  {
 
11113
    Item::Type type=item->type();
 
11114
    if (not_all_columns)
 
11115
    {
 
11116
      if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
 
11117
      {
 
11118
        if (item->used_tables() & OUTER_REF_TABLE_BIT)
 
11119
          item->update_used_tables();
 
11120
        if (type == Item::SUBSELECT_ITEM ||
 
11121
            (item->used_tables() & ~OUTER_REF_TABLE_BIT))
 
11122
        {
 
11123
          /*
 
11124
            Mark that the we have ignored an item that refers to a summary
 
11125
            function. We need to know this if someone is going to use
 
11126
            DISTINCT on the result.
 
11127
          */
 
11128
          param->using_indirect_summary_function=1;
 
11129
          continue;
 
11130
        }
 
11131
      }
 
11132
      if (item->const_item() && (int) hidden_field_count <= 0)
 
11133
        continue; // We don't have to store this
 
11134
    }
 
11135
    if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
 
11136
    {                                           /* Can't calc group yet */
 
11137
      ((Item_sum*) item)->result_field=0;
 
11138
      for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
 
11139
      {
 
11140
        Item **argp= ((Item_sum*) item)->args + i;
 
11141
        Item *arg= *argp;
 
11142
        if (!arg->const_item())
 
11143
        {
 
11144
          Field *new_field=
 
11145
            create_tmp_field(thd, table, arg, arg->type(), &copy_func,
 
11146
                             tmp_from_field, &default_field[fieldnr],
 
11147
                             group != 0,not_all_columns,
 
11148
                             distinct, 0,
 
11149
                             param->convert_blob_length);
 
11150
          if (!new_field)
 
11151
            goto err;                                   // Should be OOM
 
11152
          tmp_from_field++;
 
11153
          reclength+=new_field->pack_length();
 
11154
          if (new_field->flags & BLOB_FLAG)
 
11155
          {
 
11156
            *blob_field++= fieldnr;
 
11157
            blob_count++;
 
11158
          }
 
11159
          *(reg_field++)= new_field;
 
11160
          if (new_field->real_type() == DRIZZLE_TYPE_VARCHAR)
 
11161
          {
 
11162
            string_count++;
 
11163
            string_total_length+= new_field->pack_length();
 
11164
          }
 
11165
          thd->mem_root= mem_root_save;
 
11166
          thd->change_item_tree(argp, new Item_field(new_field));
 
11167
          thd->mem_root= &table->mem_root;
 
11168
          if (!(new_field->flags & NOT_NULL_FLAG))
 
11169
          {
 
11170
            null_count++;
 
11171
            /*
 
11172
              new_field->maybe_null() is still false, it will be
 
11173
              changed below. But we have to setup Item_field correctly
 
11174
            */
 
11175
            (*argp)->maybe_null=1;
 
11176
          }
 
11177
          new_field->field_index= fieldnr++;
 
11178
        }
 
11179
      }
 
11180
    }
 
11181
    else
 
11182
    {
 
11183
      /*
 
11184
        The last parameter to create_tmp_field() is a bit tricky:
 
11185
 
 
11186
        We need to set it to 0 in union, to get fill_record() to modify the
 
11187
        temporary table.
 
11188
        We need to set it to 1 on multi-table-update and in select to
 
11189
        write rows to the temporary table.
 
11190
        We here distinguish between UNION and multi-table-updates by the fact
 
11191
        that in the later case group is set to the row pointer.
 
11192
      */
 
11193
      Field *new_field= (param->schema_table) ?
 
11194
        create_tmp_field_for_schema(thd, item, table) :
 
11195
        create_tmp_field(thd, table, item, type, &copy_func,
 
11196
                         tmp_from_field, &default_field[fieldnr],
 
11197
                         group != 0,
 
11198
                         !force_copy_fields &&
 
11199
                           (not_all_columns || group !=0),
 
11200
                         /*
 
11201
                           If item->marker == 4 then we force create_tmp_field
 
11202
                           to create a 64-bit longs for BIT fields because HEAP
 
11203
                           tables can't index BIT fields directly. We do the same
 
11204
                           for distinct, as we want the distinct index to be
 
11205
                           usable in this case too.
 
11206
                         */
 
11207
                         item->marker == 4 || param->bit_fields_as_long,
 
11208
                         force_copy_fields,
 
11209
                         param->convert_blob_length);
 
11210
 
 
11211
      if (!new_field)
 
11212
      {
 
11213
        if (thd->is_fatal_error)
 
11214
          goto err;                             // Got OOM
 
11215
        continue;                               // Some kindf of const item
 
11216
      }
 
11217
      if (type == Item::SUM_FUNC_ITEM)
 
11218
        ((Item_sum *) item)->result_field= new_field;
 
11219
      tmp_from_field++;
 
11220
      reclength+=new_field->pack_length();
 
11221
      if (!(new_field->flags & NOT_NULL_FLAG))
 
11222
        null_count++;
 
11223
      if (new_field->flags & BLOB_FLAG)
 
11224
      {
 
11225
        *blob_field++= fieldnr;
 
11226
        blob_count++;
 
11227
      }
 
11228
      if (item->marker == 4 && item->maybe_null)
 
11229
      {
 
11230
        group_null_items++;
 
11231
        new_field->flags|= GROUP_FLAG;
 
11232
      }
 
11233
      new_field->field_index= fieldnr++;
 
11234
      *(reg_field++)= new_field;
 
11235
    }
 
11236
    if (!--hidden_field_count)
 
11237
    {
 
11238
      /*
 
11239
        This was the last hidden field; Remember how many hidden fields could
 
11240
        have null
 
11241
      */
 
11242
      hidden_null_count=null_count;
 
11243
      /*
 
11244
        We need to update hidden_field_count as we may have stored group
 
11245
        functions with constant arguments
 
11246
      */
 
11247
      param->hidden_field_count= fieldnr;
 
11248
      null_count= 0;
 
11249
    }
 
11250
  }
 
11251
  assert(fieldnr == (uint) (reg_field - table->field));
 
11252
  assert(field_count >= (uint) (reg_field - table->field));
 
11253
  field_count= fieldnr;
 
11254
  *reg_field= 0;
 
11255
  *blob_field= 0;                               // End marker
 
11256
  share->fields= field_count;
 
11257
 
 
11258
  /* If result table is small; use a heap */
 
11259
  /* future: storage engine selection can be made dynamic? */
 
11260
  if (blob_count || using_unique_constraint ||
 
11261
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
 
11262
      OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
 
11263
  {
 
11264
    share->db_plugin= ha_lock_engine(0, myisam_hton);
 
11265
    table->file= get_new_handler(share, &table->mem_root,
 
11266
                                 share->db_type());
 
11267
    if (group &&
 
11268
        (param->group_parts > table->file->max_key_parts() ||
 
11269
         param->group_length > table->file->max_key_length()))
 
11270
      using_unique_constraint=1;
 
11271
  }
 
11272
  else
 
11273
  {
 
11274
    share->db_plugin= ha_lock_engine(0, heap_hton);
 
11275
    table->file= get_new_handler(share, &table->mem_root,
 
11276
                                 share->db_type());
 
11277
  }
 
11278
  if (!table->file)
 
11279
    goto err;
 
11280
 
 
11281
 
 
11282
  if (!using_unique_constraint)
 
11283
    reclength+= group_null_items;       // null flag is stored separately
 
11284
 
 
11285
  share->blob_fields= blob_count;
 
11286
  if (blob_count == 0)
 
11287
  {
 
11288
    /* We need to ensure that first byte is not 0 for the delete link */
 
11289
    if (param->hidden_field_count)
 
11290
      hidden_null_count++;
 
11291
    else
 
11292
      null_count++;
 
11293
  }
 
11294
  hidden_null_pack_length=(hidden_null_count+7)/8;
 
11295
  null_pack_length= (hidden_null_pack_length +
 
11296
                     (null_count + total_uneven_bit_length + 7) / 8);
 
11297
  reclength+=null_pack_length;
 
11298
  if (!reclength)
 
11299
    reclength=1;                                // Dummy select
 
11300
  /* Use packed rows if there is blobs or a lot of space to gain */
 
11301
  if (blob_count || ((string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS) && (reclength / string_total_length <= RATIO_TO_PACK_ROWS || (string_total_length / string_count) >= AVG_STRING_LENGTH_TO_PACK_ROWS)))
 
11302
    use_packed_rows= 1;
 
11303
 
 
11304
  share->reclength= reclength;
 
11305
  {
 
11306
    uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
 
11307
    share->rec_buff_length= alloc_length;
 
11308
    if (!(table->record[0]= (uchar*)
 
11309
                            alloc_root(&table->mem_root, alloc_length*3)))
 
11310
      goto err;
 
11311
    table->record[1]= table->record[0]+alloc_length;
 
11312
    share->default_values= table->record[1]+alloc_length;
 
11313
  }
 
11314
  copy_func[0]=0;                               // End marker
 
11315
  param->func_count= copy_func - param->items_to_copy; 
 
11316
 
 
11317
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
11318
 
 
11319
  recinfo=param->start_recinfo;
 
11320
  null_flags=(uchar*) table->record[0];
 
11321
  pos=table->record[0]+ null_pack_length;
 
11322
  if (null_pack_length)
 
11323
  {
 
11324
    memset((uchar*) recinfo, 0, sizeof(*recinfo));
 
11325
    recinfo->type=FIELD_NORMAL;
 
11326
    recinfo->length=null_pack_length;
 
11327
    recinfo++;
 
11328
    memset(null_flags, 255, null_pack_length);  // Set null fields
 
11329
 
 
11330
    table->null_flags= (uchar*) table->record[0];
 
11331
    share->null_fields= null_count+ hidden_null_count;
 
11332
    share->null_bytes= null_pack_length;
 
11333
  }
 
11334
  null_count= (blob_count == 0) ? 1 : 0;
 
11335
  hidden_field_count=param->hidden_field_count;
 
11336
  for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
 
11337
  {
 
11338
    Field *field= *reg_field;
 
11339
    uint length;
 
11340
    memset((uchar*) recinfo, 0, sizeof(*recinfo));
 
11341
 
 
11342
    if (!(field->flags & NOT_NULL_FLAG))
 
11343
    {
 
11344
      if (field->flags & GROUP_FLAG && !using_unique_constraint)
 
11345
      {
 
11346
        /*
 
11347
          We have to reserve one byte here for NULL bits,
 
11348
          as this is updated by 'end_update()'
 
11349
        */
 
11350
        *pos++=0;                               // Null is stored here
 
11351
        recinfo->length=1;
 
11352
        recinfo->type=FIELD_NORMAL;
 
11353
        recinfo++;
 
11354
        memset((uchar*) recinfo, 0, sizeof(*recinfo));
 
11355
      }
 
11356
      else
 
11357
      {
 
11358
        recinfo->null_bit= 1 << (null_count & 7);
 
11359
        recinfo->null_pos= null_count/8;
 
11360
      }
 
11361
      field->move_field(pos,null_flags+null_count/8,
 
11362
                        1 << (null_count & 7));
 
11363
      null_count++;
 
11364
    }
 
11365
    else
 
11366
      field->move_field(pos,(uchar*) 0,0);
 
11367
    field->reset();
 
11368
 
 
11369
    /*
 
11370
      Test if there is a default field value. The test for ->ptr is to skip
 
11371
      'offset' fields generated by initalize_tables
 
11372
    */
 
11373
    if (default_field[i] && default_field[i]->ptr)
 
11374
    {
 
11375
      /* 
 
11376
         default_field[i] is set only in the cases  when 'field' can
 
11377
         inherit the default value that is defined for the field referred
 
11378
         by the Item_field object from which 'field' has been created.
 
11379
      */
 
11380
      my_ptrdiff_t diff;
 
11381
      Field *orig_field= default_field[i];
 
11382
      /* Get the value from default_values */
 
11383
      diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
 
11384
                            orig_field->table->record[0]);
 
11385
      orig_field->move_field_offset(diff);      // Points now at default_values
 
11386
      if (orig_field->is_real_null())
 
11387
        field->set_null();
 
11388
      else
 
11389
      {
 
11390
        field->set_notnull();
 
11391
        memcpy(field->ptr, orig_field->ptr, field->pack_length());
 
11392
      }
 
11393
      orig_field->move_field_offset(-diff);     // Back to record[0]
 
11394
    } 
 
11395
 
 
11396
    if (from_field[i])
 
11397
    {                                           /* Not a table Item */
 
11398
      copy->set(field,from_field[i],save_sum_fields);
 
11399
      copy++;
 
11400
    }
 
11401
    length=field->pack_length();
 
11402
    pos+= length;
 
11403
 
 
11404
    /* Make entry for create table */
 
11405
    recinfo->length=length;
 
11406
    if (field->flags & BLOB_FLAG)
 
11407
      recinfo->type= (int) FIELD_BLOB;
 
11408
    else
 
11409
      recinfo->type=FIELD_NORMAL;
 
11410
    if (!--hidden_field_count)
 
11411
      null_count=(null_count+7) & ~7;           // move to next byte
 
11412
 
 
11413
    // fix table name in field entry
 
11414
    field->table_name= &table->alias;
 
11415
  }
 
11416
 
 
11417
  param->copy_field_end=copy;
 
11418
  param->recinfo=recinfo;
 
11419
  store_record(table,s->default_values);        // Make empty default record
 
11420
 
 
11421
  if (thd->variables.tmp_table_size == ~ (uint64_t) 0)          // No limit
 
11422
    share->max_rows= ~(ha_rows) 0;
 
11423
  else
 
11424
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
 
11425
                                 min(thd->variables.tmp_table_size,
 
11426
                                     thd->variables.max_heap_table_size) :
 
11427
                                 thd->variables.tmp_table_size) /
 
11428
                                 share->reclength);
 
11429
  set_if_bigger(share->max_rows,1);             // For dummy start options
 
11430
  /*
 
11431
    Push the LIMIT clause to the temporary table creation, so that we
 
11432
    materialize only up to 'rows_limit' records instead of all result records.
 
11433
  */
 
11434
  set_if_smaller(share->max_rows, rows_limit);
 
11435
  param->end_write_records= rows_limit;
 
11436
 
 
11437
  keyinfo= param->keyinfo;
 
11438
 
 
11439
  if (group)
 
11440
  {
 
11441
    table->group=group;                         /* Table is grouped by key */
 
11442
    param->group_buff=group_buff;
 
11443
    share->keys=1;
 
11444
    share->uniques= test(using_unique_constraint);
 
11445
    table->key_info=keyinfo;
 
11446
    keyinfo->key_part=key_part_info;
 
11447
    keyinfo->flags=HA_NOSAME;
 
11448
    keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
 
11449
    keyinfo->key_length=0;
 
11450
    keyinfo->rec_per_key=0;
 
11451
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11452
    keyinfo->name= (char*) "group_key";
 
11453
    ORDER *cur_group= group;
 
11454
    for (; cur_group ; cur_group= cur_group->next, key_part_info++)
 
11455
    {
 
11456
      Field *field=(*cur_group->item)->get_tmp_table_field();
 
11457
      bool maybe_null=(*cur_group->item)->maybe_null;
 
11458
      key_part_info->null_bit=0;
 
11459
      key_part_info->field=  field;
 
11460
      key_part_info->offset= field->offset(table->record[0]);
 
11461
      key_part_info->length= (uint16_t) field->key_length();
 
11462
      key_part_info->type=   (uint8_t) field->key_type();
 
11463
      key_part_info->key_type =
 
11464
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
 
11465
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
 
11466
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
 
11467
        0 : FIELDFLAG_BINARY;
 
11468
      if (!using_unique_constraint)
 
11469
      {
 
11470
        cur_group->buff=(char*) group_buff;
 
11471
        if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
 
11472
                                                     group_buff +
 
11473
                                                     test(maybe_null),
 
11474
                                                     field->null_ptr,
 
11475
                                                     field->null_bit)))
 
11476
          goto err; /* purecov: inspected */
 
11477
        if (maybe_null)
 
11478
        {
 
11479
          /*
 
11480
            To be able to group on NULL, we reserved place in group_buff
 
11481
            for the NULL flag just before the column. (see above).
 
11482
            The field data is after this flag.
 
11483
            The NULL flag is updated in 'end_update()' and 'end_write()'
 
11484
          */
 
11485
          keyinfo->flags|= HA_NULL_ARE_EQUAL;   // def. that NULL == NULL
 
11486
          key_part_info->null_bit=field->null_bit;
 
11487
          key_part_info->null_offset= (uint) (field->null_ptr -
 
11488
                                              (uchar*) table->record[0]);
 
11489
          cur_group->buff++;                        // Pointer to field data
 
11490
          group_buff++;                         // Skipp null flag
 
11491
        }
 
11492
        /* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
 
11493
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
 
11494
        group_buff+= cur_group->field->pack_length();
 
11495
      }
 
11496
      keyinfo->key_length+=  key_part_info->length;
 
11497
    }
 
11498
  }
 
11499
 
 
11500
  if (distinct && field_count != param->hidden_field_count)
 
11501
  {
 
11502
    /*
 
11503
      Create an unique key or an unique constraint over all columns
 
11504
      that should be in the result.  In the temporary table, there are
 
11505
      'param->hidden_field_count' extra columns, whose null bits are stored
 
11506
      in the first 'hidden_null_pack_length' bytes of the row.
 
11507
    */
 
11508
    if (blob_count)
 
11509
    {
 
11510
      /*
 
11511
        Special mode for index creation in MyISAM used to support unique
 
11512
        indexes on blobs with arbitrary length. Such indexes cannot be
 
11513
        used for lookups.
 
11514
      */
 
11515
      share->uniques= 1;
 
11516
    }
 
11517
    null_pack_length-=hidden_null_pack_length;
 
11518
    keyinfo->key_parts= ((field_count-param->hidden_field_count)+
 
11519
                         (share->uniques ? test(null_pack_length) : 0));
 
11520
    table->distinct= 1;
 
11521
    share->keys= 1;
 
11522
    if (!(key_part_info= (KEY_PART_INFO*)
 
11523
          alloc_root(&table->mem_root,
 
11524
                     keyinfo->key_parts * sizeof(KEY_PART_INFO))))
 
11525
      goto err;
 
11526
    memset(key_part_info, 0, keyinfo->key_parts * sizeof(KEY_PART_INFO));
 
11527
    table->key_info=keyinfo;
 
11528
    keyinfo->key_part=key_part_info;
 
11529
    keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
 
11530
    keyinfo->key_length=(uint16_t) reclength;
 
11531
    keyinfo->name= (char*) "distinct_key";
 
11532
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11533
    keyinfo->rec_per_key=0;
 
11534
 
 
11535
    /*
 
11536
      Create an extra field to hold NULL bits so that unique indexes on
 
11537
      blobs can distinguish NULL from 0. This extra field is not needed
 
11538
      when we do not use UNIQUE indexes for blobs.
 
11539
    */
 
11540
    if (null_pack_length && share->uniques)
 
11541
    {
 
11542
      key_part_info->null_bit=0;
 
11543
      key_part_info->offset=hidden_null_pack_length;
 
11544
      key_part_info->length=null_pack_length;
 
11545
      key_part_info->field= new Field_varstring(table->record[0],
 
11546
                                                (uint32_t) key_part_info->length,
 
11547
                                                0,
 
11548
                                                (uchar*) 0,
 
11549
                                                (uint) 0,
 
11550
                                                Field::NONE,
 
11551
                                                NullS, 
 
11552
                                                table->s,
 
11553
                                                &my_charset_bin);
 
11554
      if (!key_part_info->field)
 
11555
        goto err;
 
11556
      key_part_info->field->init(table);
 
11557
      key_part_info->key_type=FIELDFLAG_BINARY;
 
11558
      key_part_info->type=    HA_KEYTYPE_BINARY;
 
11559
      key_part_info++;
 
11560
    }
 
11561
    /* Create a distinct key over the columns we are going to return */
 
11562
    for (i=param->hidden_field_count, reg_field=table->field + i ;
 
11563
         i < field_count;
 
11564
         i++, reg_field++, key_part_info++)
 
11565
    {
 
11566
      key_part_info->null_bit=0;
 
11567
      key_part_info->field=    *reg_field;
 
11568
      key_part_info->offset=   (*reg_field)->offset(table->record[0]);
 
11569
      key_part_info->length=   (uint16_t) (*reg_field)->pack_length();
 
11570
      /* TODO:
 
11571
        The below method of computing the key format length of the
 
11572
        key part is a copy/paste from opt_range.cc, and table.cc.
 
11573
        This should be factored out, e.g. as a method of Field.
 
11574
        In addition it is not clear if any of the Field::*_length
 
11575
        methods is supposed to compute the same length. If so, it
 
11576
        might be reused.
 
11577
      */
 
11578
      key_part_info->store_length= key_part_info->length;
 
11579
 
 
11580
      if ((*reg_field)->real_maybe_null())
 
11581
        key_part_info->store_length+= HA_KEY_NULL_LENGTH;
 
11582
      if ((*reg_field)->type() == DRIZZLE_TYPE_BLOB || 
 
11583
          (*reg_field)->real_type() == DRIZZLE_TYPE_VARCHAR)
 
11584
        key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
 
11585
 
 
11586
      key_part_info->type=     (uint8_t) (*reg_field)->key_type();
 
11587
      key_part_info->key_type =
 
11588
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
 
11589
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
 
11590
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
 
11591
        0 : FIELDFLAG_BINARY;
 
11592
    }
 
11593
  }
 
11594
 
 
11595
  if (thd->is_fatal_error)                              // If end of memory
 
11596
    goto err;                                    /* purecov: inspected */
 
11597
  share->db_record_offset= 1;
 
11598
  if (share->db_type() == myisam_hton)
 
11599
  {
 
11600
    if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
 
11601
                                &param->recinfo, select_options))
 
11602
      goto err;
 
11603
  }
 
11604
  if (open_tmp_table(table))
 
11605
    goto err;
 
11606
 
 
11607
  thd->mem_root= mem_root_save;
 
11608
 
 
11609
  return(table);
 
11610
 
 
11611
err:
 
11612
  thd->mem_root= mem_root_save;
 
11613
  free_tmp_table(thd,table);                    /* purecov: inspected */
 
11614
  if (temp_pool_slot != MY_BIT_NONE)
 
11615
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11616
  return(NULL);                         /* purecov: inspected */
 
11617
}
 
11618
 
 
11619
 
 
11620
 
 
11621
 
 
11622
/*
 
11623
  Create a temporary table to weed out duplicate rowid combinations
 
11624
 
 
11625
  SYNOPSIS
 
11626
 
 
11627
    create_duplicate_weedout_tmp_table()
 
11628
      thd
 
11629
      uniq_tuple_length_arg
 
11630
      SJ_TMP_TABLE 
 
11631
 
 
11632
  DESCRIPTION
 
11633
    Create a temporary table to weed out duplicate rowid combinations. The
 
11634
    table has a single column that is a concatenation of all rowids in the
 
11635
    combination. 
 
11636
 
 
11637
    Depending on the needed length, there are two cases:
 
11638
 
 
11639
    1. When the length of the column < max_key_length:
 
11640
 
 
11641
      CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
 
11642
 
 
11643
    2. Otherwise (not a valid SQL syntax but internally supported):
 
11644
 
 
11645
      CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
 
11646
 
 
11647
    The code in this function was produced by extraction of relevant parts
 
11648
    from create_tmp_table().
 
11649
 
 
11650
  RETURN
 
11651
    created table
 
11652
    NULL on error
 
11653
*/
 
11654
 
 
11655
TABLE *create_duplicate_weedout_tmp_table(THD *thd, 
 
11656
                                          uint uniq_tuple_length_arg,
 
11657
                                          SJ_TMP_TABLE *sjtbl)
 
11658
{
 
11659
  MEM_ROOT *mem_root_save, own_root;
 
11660
  TABLE *table;
 
11661
  TABLE_SHARE *share;
 
11662
  uint  temp_pool_slot=MY_BIT_NONE;
 
11663
  char  *tmpname,path[FN_REFLEN];
 
11664
  Field **reg_field;
 
11665
  KEY_PART_INFO *key_part_info;
 
11666
  KEY *keyinfo;
 
11667
  uchar *group_buff;
 
11668
  uchar *bitmaps;
 
11669
  uint *blob_field;
 
11670
  MI_COLUMNDEF *recinfo, *start_recinfo;
 
11671
  bool using_unique_constraint=false;
 
11672
  Field *field, *key_field;
 
11673
  uint blob_count, null_pack_length, null_count;
 
11674
  uchar *null_flags;
 
11675
  uchar *pos;
 
11676
  
 
11677
  /*
 
11678
    STEP 1: Get temporary table name
 
11679
  */
 
11680
  statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
 
11681
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
 
11682
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
 
11683
 
 
11684
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
 
11685
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
 
11686
            current_pid, temp_pool_slot);
 
11687
  else
 
11688
  {
 
11689
    /* if we run out of slots or we are not using tempool */
 
11690
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
 
11691
            thd->thread_id, thd->tmp_table++);
 
11692
  }
 
11693
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
 
11694
 
 
11695
  /* STEP 2: Figure if we'll be using a key or blob+constraint */
 
11696
  if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
 
11697
    using_unique_constraint= true;
 
11698
 
 
11699
  /* STEP 3: Allocate memory for temptable description */
 
11700
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
11701
  if (!multi_alloc_root(&own_root,
 
11702
                        &table, sizeof(*table),
 
11703
                        &share, sizeof(*share),
 
11704
                        &reg_field, sizeof(Field*) * (1+1),
 
11705
                        &blob_field, sizeof(uint)*2,
 
11706
                        &keyinfo, sizeof(*keyinfo),
 
11707
                        &key_part_info, sizeof(*key_part_info) * 2,
 
11708
                        &start_recinfo,
 
11709
                        sizeof(*recinfo)*(1*2+4),
 
11710
                        &tmpname, (uint) strlen(path)+1,
 
11711
                        &group_buff, (!using_unique_constraint ?
 
11712
                                      uniq_tuple_length_arg : 0),
 
11713
                        &bitmaps, bitmap_buffer_size(1)*2,
 
11714
                        NullS))
 
11715
  {
 
11716
    if (temp_pool_slot != MY_BIT_NONE)
 
11717
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11718
    return(NULL);
 
11719
  }
 
11720
  strmov(tmpname,path);
 
11721
  
 
11722
 
 
11723
  /* STEP 4: Create TABLE description */
 
11724
  memset((char*) table, 0, sizeof(*table));
 
11725
  memset((char*) reg_field, 0, sizeof(Field*)*2);
 
11726
 
 
11727
  table->mem_root= own_root;
 
11728
  mem_root_save= thd->mem_root;
 
11729
  thd->mem_root= &table->mem_root;
 
11730
 
 
11731
  table->field=reg_field;
 
11732
  table->alias= "weedout-tmp";
 
11733
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
 
11734
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
 
11735
  table->map=1;
 
11736
  table->temp_pool_slot = temp_pool_slot;
 
11737
  table->copy_blobs= 1;
 
11738
  table->in_use= thd;
 
11739
  table->quick_keys.init();
 
11740
  table->covering_keys.init();
 
11741
  table->keys_in_use_for_query.init();
 
11742
 
 
11743
  table->s= share;
 
11744
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
 
11745
  share->blob_field= blob_field;
 
11746
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
11747
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
 
11748
  share->table_charset= NULL;
 
11749
  share->primary_key= MAX_KEY;               // Indicate no primary key
 
11750
  share->keys_for_keyread.init();
 
11751
  share->keys_in_use.init();
 
11752
 
 
11753
  blob_count= 0;
 
11754
 
 
11755
  /* Create the field */
 
11756
  {
 
11757
    /*
 
11758
      For the sake of uniformity, always use Field_varstring.
 
11759
    */
 
11760
    field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
 
11761
                               &my_charset_bin);
 
11762
    if (!field)
 
11763
      return(0);
 
11764
    field->table= table;
 
11765
    field->key_start.init(0);
 
11766
    field->part_of_key.init(0);
 
11767
    field->part_of_sortkey.init(0);
 
11768
    field->unireg_check= Field::NONE;
 
11769
    field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
 
11770
    field->reset_fields();
 
11771
    field->init(table);
 
11772
    field->orig_table= NULL;
 
11773
     
 
11774
    field->field_index= 0;
 
11775
    
 
11776
    *(reg_field++)= field;
 
11777
    *blob_field= 0;
 
11778
    *reg_field= 0;
 
11779
 
 
11780
    share->fields= 1;
 
11781
    share->blob_fields= 0;
 
11782
  }
 
11783
 
 
11784
  uint reclength= field->pack_length();
 
11785
  if (using_unique_constraint)
 
11786
  { 
 
11787
    share->db_plugin= ha_lock_engine(0, myisam_hton);
 
11788
    table->file= get_new_handler(share, &table->mem_root,
 
11789
                                 share->db_type());
 
11790
    assert(uniq_tuple_length_arg <= table->file->max_key_length());
 
11791
  }
 
11792
  else
 
11793
  {
 
11794
    share->db_plugin= ha_lock_engine(0, heap_hton);
 
11795
    table->file= get_new_handler(share, &table->mem_root,
 
11796
                                 share->db_type());
 
11797
  }
 
11798
  if (!table->file)
 
11799
    goto err;
 
11800
 
 
11801
  null_count=1;
 
11802
  
 
11803
  null_pack_length= 1;
 
11804
  reclength += null_pack_length;
 
11805
 
 
11806
  share->reclength= reclength;
 
11807
  {
 
11808
    uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
 
11809
    share->rec_buff_length= alloc_length;
 
11810
    if (!(table->record[0]= (uchar*)
 
11811
                            alloc_root(&table->mem_root, alloc_length*3)))
 
11812
      goto err;
 
11813
    table->record[1]= table->record[0]+alloc_length;
 
11814
    share->default_values= table->record[1]+alloc_length;
 
11815
  }
 
11816
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
11817
 
 
11818
  recinfo= start_recinfo;
 
11819
  null_flags=(uchar*) table->record[0];
 
11820
  pos=table->record[0]+ null_pack_length;
 
11821
  if (null_pack_length)
 
11822
  {
 
11823
    memset((uchar*) recinfo, 0, sizeof(*recinfo));
 
11824
    recinfo->type=FIELD_NORMAL;
 
11825
    recinfo->length=null_pack_length;
 
11826
    recinfo++;
 
11827
    memset(null_flags, 255, null_pack_length);  // Set null fields
 
11828
 
 
11829
    table->null_flags= (uchar*) table->record[0];
 
11830
    share->null_fields= null_count;
 
11831
    share->null_bytes= null_pack_length;
 
11832
  }
 
11833
  null_count=1;
 
11834
 
 
11835
  {
 
11836
    //Field *field= *reg_field;
 
11837
    uint length;
 
11838
    memset((uchar*) recinfo, 0, sizeof(*recinfo));
 
11839
    field->move_field(pos,(uchar*) 0,0);
 
11840
 
 
11841
    field->reset();
 
11842
    /*
 
11843
      Test if there is a default field value. The test for ->ptr is to skip
 
11844
      'offset' fields generated by initalize_tables
 
11845
    */
 
11846
    // Initialize the table field:
 
11847
    memset(field->ptr, 0, field->pack_length());
 
11848
 
 
11849
    length=field->pack_length();
 
11850
    pos+= length;
 
11851
 
 
11852
    /* Make entry for create table */
 
11853
    recinfo->length=length;
 
11854
    if (field->flags & BLOB_FLAG)
 
11855
      recinfo->type= (int) FIELD_BLOB;
 
11856
    else
 
11857
      recinfo->type=FIELD_NORMAL;
 
11858
 
 
11859
    field->table_name= &table->alias;
 
11860
  }
 
11861
 
 
11862
  //param->recinfo=recinfo;
 
11863
  //store_record(table,s->default_values);        // Make empty default record
 
11864
 
 
11865
  if (thd->variables.tmp_table_size == ~ (uint64_t) 0)          // No limit
 
11866
    share->max_rows= ~(ha_rows) 0;
 
11867
  else
 
11868
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
 
11869
                                 min(thd->variables.tmp_table_size,
 
11870
                                     thd->variables.max_heap_table_size) :
 
11871
                                 thd->variables.tmp_table_size) /
 
11872
                                 share->reclength);
 
11873
  set_if_bigger(share->max_rows,1);             // For dummy start options
 
11874
 
 
11875
 
 
11876
  //// keyinfo= param->keyinfo;
 
11877
  if (true)
 
11878
  {
 
11879
    share->keys=1;
 
11880
    share->uniques= test(using_unique_constraint);
 
11881
    table->key_info=keyinfo;
 
11882
    keyinfo->key_part=key_part_info;
 
11883
    keyinfo->flags=HA_NOSAME;
 
11884
    keyinfo->usable_key_parts= keyinfo->key_parts= 1;
 
11885
    keyinfo->key_length=0;
 
11886
    keyinfo->rec_per_key=0;
 
11887
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11888
    keyinfo->name= (char*) "weedout_key";
 
11889
    {
 
11890
      key_part_info->null_bit=0;
 
11891
      key_part_info->field=  field;
 
11892
      key_part_info->offset= field->offset(table->record[0]);
 
11893
      key_part_info->length= (uint16_t) field->key_length();
 
11894
      key_part_info->type=   (uint8_t) field->key_type();
 
11895
      key_part_info->key_type = FIELDFLAG_BINARY;
 
11896
      if (!using_unique_constraint)
 
11897
      {
 
11898
        if (!(key_field= field->new_key_field(thd->mem_root, table,
 
11899
                                              group_buff,
 
11900
                                              field->null_ptr,
 
11901
                                              field->null_bit)))
 
11902
          goto err;
 
11903
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
 
11904
      }
 
11905
      keyinfo->key_length+=  key_part_info->length;
 
11906
    }
 
11907
  }
 
11908
 
 
11909
  if (thd->is_fatal_error)                              // If end of memory
 
11910
    goto err;
 
11911
  share->db_record_offset= 1;
 
11912
  if (share->db_type() == myisam_hton)
 
11913
  {
 
11914
    recinfo++;
 
11915
    if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
 
11916
      goto err;
 
11917
  }
 
11918
  sjtbl->start_recinfo= start_recinfo;
 
11919
  sjtbl->recinfo=       recinfo;
 
11920
  if (open_tmp_table(table))
 
11921
    goto err;
 
11922
 
 
11923
  thd->mem_root= mem_root_save;
 
11924
  return(table);
 
11925
 
 
11926
err:
 
11927
  thd->mem_root= mem_root_save;
 
11928
  free_tmp_table(thd,table);                    /* purecov: inspected */
 
11929
  if (temp_pool_slot != MY_BIT_NONE)
 
11930
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11931
  return(NULL);                         /* purecov: inspected */
 
11932
}
 
11933
 
 
11934
/****************************************************************************/
 
11935
 
 
11936
/**
 
11937
  Create a reduced TABLE object with properly set up Field list from a
 
11938
  list of field definitions.
 
11939
 
 
11940
    The created table doesn't have a table handler associated with
 
11941
    it, has no keys, no group/distinct, no copy_funcs array.
 
11942
    The sole purpose of this TABLE object is to use the power of Field
 
11943
    class to read/write data to/from table->record[0]. Then one can store
 
11944
    the record in any container (RB tree, hash, etc).
 
11945
    The table is created in THD mem_root, so are the table's fields.
 
11946
    Consequently, if you don't BLOB fields, you don't need to free it.
 
11947
 
 
11948
  @param thd         connection handle
 
11949
  @param field_list  list of column definitions
 
11950
 
 
11951
  @return
 
11952
    0 if out of memory, TABLE object in case of success
 
11953
*/
 
11954
 
 
11955
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
 
11956
{
 
11957
  uint field_count= field_list.elements;
 
11958
  uint blob_count= 0;
 
11959
  Field **field;
 
11960
  Create_field *cdef;                           /* column definition */
 
11961
  uint record_length= 0;
 
11962
  uint null_count= 0;                 /* number of columns which may be null */
 
11963
  uint null_pack_length;              /* NULL representation array length */
 
11964
  uint *blob_field;
 
11965
  uchar *bitmaps;
 
11966
  TABLE *table;
 
11967
  TABLE_SHARE *share;
 
11968
 
 
11969
  if (!multi_alloc_root(thd->mem_root,
 
11970
                        &table, sizeof(*table),
 
11971
                        &share, sizeof(*share),
 
11972
                        &field, (field_count + 1) * sizeof(Field*),
 
11973
                        &blob_field, (field_count+1) *sizeof(uint),
 
11974
                        &bitmaps, bitmap_buffer_size(field_count)*2,
 
11975
                        NullS))
 
11976
    return 0;
 
11977
 
 
11978
  memset(table, 0, sizeof(*table));
 
11979
  memset(share, 0, sizeof(*share));
 
11980
  table->field= field;
 
11981
  table->s= share;
 
11982
  share->blob_field= blob_field;
 
11983
  share->fields= field_count;
 
11984
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
11985
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
11986
 
 
11987
  /* Create all fields and calculate the total length of record */
 
11988
  List_iterator_fast<Create_field> it(field_list);
 
11989
  while ((cdef= it++))
 
11990
  {
 
11991
    *field= make_field(share, 0, cdef->length,
 
11992
                       (uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
 
11993
                       f_maybe_null(cdef->pack_flag) ? 1 : 0,
 
11994
                       cdef->pack_flag, cdef->sql_type, cdef->charset,
 
11995
                       cdef->unireg_check,
 
11996
                       cdef->interval, cdef->field_name);
 
11997
    if (!*field)
 
11998
      goto error;
 
11999
    (*field)->init(table);
 
12000
    record_length+= (*field)->pack_length();
 
12001
    if (! ((*field)->flags & NOT_NULL_FLAG))
 
12002
      null_count++;
 
12003
 
 
12004
    if ((*field)->flags & BLOB_FLAG)
 
12005
      share->blob_field[blob_count++]= (uint) (field - table->field);
 
12006
 
 
12007
    field++;
 
12008
  }
 
12009
  *field= NULL;                             /* mark the end of the list */
 
12010
  share->blob_field[blob_count]= 0;            /* mark the end of the list */
 
12011
  share->blob_fields= blob_count;
 
12012
 
 
12013
  null_pack_length= (null_count + 7)/8;
 
12014
  share->reclength= record_length + null_pack_length;
 
12015
  share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
 
12016
  table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
 
12017
  if (!table->record[0])
 
12018
    goto error;
 
12019
 
 
12020
  if (null_pack_length)
 
12021
  {
 
12022
    table->null_flags= (uchar*) table->record[0];
 
12023
    share->null_fields= null_count;
 
12024
    share->null_bytes= null_pack_length;
 
12025
  }
 
12026
 
 
12027
  table->in_use= thd;           /* field->reset() may access table->in_use */
 
12028
  {
 
12029
    /* Set up field pointers */
 
12030
    uchar *null_pos= table->record[0];
 
12031
    uchar *field_pos= null_pos + share->null_bytes;
 
12032
    uint null_bit= 1;
 
12033
 
 
12034
    for (field= table->field; *field; ++field)
 
12035
    {
 
12036
      Field *cur_field= *field;
 
12037
      if ((cur_field->flags & NOT_NULL_FLAG))
 
12038
        cur_field->move_field(field_pos);
 
12039
      else
 
12040
      {
 
12041
        cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
 
12042
        null_bit<<= 1;
 
12043
        if (null_bit == (1 << 8))
 
12044
        {
 
12045
          ++null_pos;
 
12046
          null_bit= 1;
 
12047
        }
 
12048
      }
 
12049
      cur_field->reset();
 
12050
 
 
12051
      field_pos+= cur_field->pack_length();
 
12052
    }
 
12053
  }
 
12054
  return table;
 
12055
error:
 
12056
  for (field= table->field; *field; ++field)
 
12057
    delete *field;                         /* just invokes field destructor */
 
12058
  return 0;
 
12059
}
 
12060
 
 
12061
 
 
12062
static bool open_tmp_table(TABLE *table)
 
12063
{
 
12064
  int error;
 
12065
  if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
 
12066
                                  HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
 
12067
  {
 
12068
    table->file->print_error(error,MYF(0)); /* purecov: inspected */
 
12069
    table->db_stat=0;
 
12070
    return(1);
 
12071
  }
 
12072
  (void) table->file->extra(HA_EXTRA_QUICK);            /* Faster */
 
12073
  return(0);
 
12074
}
 
12075
 
 
12076
 
 
12077
/*
 
12078
  Create MyISAM temporary table
 
12079
 
 
12080
  SYNOPSIS
 
12081
    create_myisam_tmp_table()
 
12082
      table           Table object that descrimes the table to be created
 
12083
      keyinfo         Description of the index (there is always one index)
 
12084
      start_recinfo   MyISAM's column descriptions
 
12085
      recinfo INOUT   End of MyISAM's column descriptions
 
12086
      options         Option bits
 
12087
   
 
12088
  DESCRIPTION
 
12089
    Create a MyISAM temporary table according to passed description. The is
 
12090
    assumed to have one unique index or constraint.
 
12091
 
 
12092
    The passed array or MI_COLUMNDEF structures must have this form:
 
12093
 
 
12094
      1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
 
12095
         when there are many nullable columns)
 
12096
      2. Table columns
 
12097
      3. One free MI_COLUMNDEF element (*recinfo points here)
 
12098
   
 
12099
    This function may use the free element to create hash column for unique
 
12100
    constraint.
 
12101
 
 
12102
   RETURN
 
12103
     false - OK
 
12104
     true  - Error
 
12105
*/
 
12106
 
 
12107
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
 
12108
                                    MI_COLUMNDEF *start_recinfo,
 
12109
                                    MI_COLUMNDEF **recinfo, 
 
12110
                                    uint64_t options)
 
12111
{
 
12112
  int error;
 
12113
  MI_KEYDEF keydef;
 
12114
  MI_UNIQUEDEF uniquedef;
 
12115
  TABLE_SHARE *share= table->s;
 
12116
 
 
12117
  if (share->keys)
 
12118
  {                                             // Get keys for ni_create
 
12119
    bool using_unique_constraint=0;
 
12120
    HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
 
12121
                                            sizeof(*seg) * keyinfo->key_parts);
 
12122
    if (!seg)
 
12123
      goto err;
 
12124
 
 
12125
    memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
 
12126
    if (keyinfo->key_length >= table->file->max_key_length() ||
 
12127
        keyinfo->key_parts > table->file->max_key_parts() ||
 
12128
        share->uniques)
 
12129
    {
 
12130
      /* Can't create a key; Make a unique constraint instead of a key */
 
12131
      share->keys=    0;
 
12132
      share->uniques= 1;
 
12133
      using_unique_constraint=1;
 
12134
      memset((char*) &uniquedef, 0, sizeof(uniquedef));
 
12135
      uniquedef.keysegs=keyinfo->key_parts;
 
12136
      uniquedef.seg=seg;
 
12137
      uniquedef.null_are_equal=1;
 
12138
 
 
12139
      /* Create extra column for hash value */
 
12140
      memset((uchar*) *recinfo, 0, sizeof(**recinfo));
 
12141
      (*recinfo)->type= FIELD_CHECK;
 
12142
      (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
 
12143
      (*recinfo)++;
 
12144
      share->reclength+=MI_UNIQUE_HASH_LENGTH;
 
12145
    }
 
12146
    else
 
12147
    {
 
12148
      /* Create an unique key */
 
12149
      memset((char*) &keydef, 0, sizeof(keydef));
 
12150
      keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
 
12151
      keydef.keysegs=  keyinfo->key_parts;
 
12152
      keydef.seg= seg;
 
12153
    }
 
12154
    for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
 
12155
    {
 
12156
      Field *field=keyinfo->key_part[i].field;
 
12157
      seg->flag=     0;
 
12158
      seg->language= field->charset()->number;
 
12159
      seg->length=   keyinfo->key_part[i].length;
 
12160
      seg->start=    keyinfo->key_part[i].offset;
 
12161
      if (field->flags & BLOB_FLAG)
 
12162
      {
 
12163
        seg->type=
 
12164
        ((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
 
12165
         HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
 
12166
        seg->bit_start= (uint8_t)(field->pack_length() - share->blob_ptr_size);
 
12167
        seg->flag= HA_BLOB_PART;
 
12168
        seg->length=0;                  // Whole blob in unique constraint
 
12169
      }
 
12170
      else
 
12171
      {
 
12172
        seg->type= keyinfo->key_part[i].type;
 
12173
      }
 
12174
      if (!(field->flags & NOT_NULL_FLAG))
 
12175
      {
 
12176
        seg->null_bit= field->null_bit;
 
12177
        seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
 
12178
        /*
 
12179
          We are using a GROUP BY on something that contains NULL
 
12180
          In this case we have to tell MyISAM that two NULL should
 
12181
          on INSERT be regarded at the same value
 
12182
        */
 
12183
        if (!using_unique_constraint)
 
12184
          keydef.flag|= HA_NULL_ARE_EQUAL;
 
12185
      }
 
12186
    }
 
12187
  }
 
12188
  MI_CREATE_INFO create_info;
 
12189
  memset((char*) &create_info, 0, sizeof(create_info));
 
12190
 
 
12191
  if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
 
12192
      OPTION_BIG_TABLES)
 
12193
    create_info.data_file_length= ~(uint64_t) 0;
 
12194
 
 
12195
  if ((error=mi_create(share->table_name.str, share->keys, &keydef,
 
12196
                       (uint) (*recinfo-start_recinfo),
 
12197
                       start_recinfo,
 
12198
                       share->uniques, &uniquedef,
 
12199
                       &create_info,
 
12200
                       HA_CREATE_TMP_TABLE)))
 
12201
  {
 
12202
    table->file->print_error(error,MYF(0));     /* purecov: inspected */
 
12203
    table->db_stat=0;
 
12204
    goto err;
 
12205
  }
 
12206
  status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
 
12207
  share->db_record_offset= 1;
 
12208
  return(0);
 
12209
 err:
 
12210
  return(1);
 
12211
}
 
12212
 
 
12213
 
 
12214
void
 
12215
free_tmp_table(THD *thd, TABLE *entry)
 
12216
{
 
12217
  MEM_ROOT own_root= entry->mem_root;
 
12218
  const char *save_proc_info;
 
12219
 
 
12220
  save_proc_info=thd->proc_info;
 
12221
  thd_proc_info(thd, "removing tmp table");
 
12222
 
 
12223
  if (entry->file)
 
12224
  {
 
12225
    if (entry->db_stat)
 
12226
      entry->file->ha_drop_table(entry->s->table_name.str);
 
12227
    else
 
12228
      entry->file->ha_delete_table(entry->s->table_name.str);
 
12229
    delete entry->file;
 
12230
  }
 
12231
 
 
12232
  /* free blobs */
 
12233
  for (Field **ptr=entry->field ; *ptr ; ptr++)
 
12234
    (*ptr)->free();
 
12235
  free_io_cache(entry);
 
12236
 
 
12237
  if (entry->temp_pool_slot != MY_BIT_NONE)
 
12238
    bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
 
12239
 
 
12240
  plugin_unlock(0, entry->s->db_plugin);
 
12241
 
 
12242
  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
 
12243
  thd_proc_info(thd, save_proc_info);
 
12244
 
 
12245
  return;
 
12246
}
 
12247
 
 
12248
/**
 
12249
  If a HEAP table gets full, create a MyISAM table and copy all rows
 
12250
  to this.
 
12251
*/
 
12252
 
 
12253
bool create_myisam_from_heap(THD *thd, TABLE *table,
 
12254
                             MI_COLUMNDEF *start_recinfo,
 
12255
                             MI_COLUMNDEF **recinfo, 
 
12256
                             int error, bool ignore_last_dupp_key_error)
 
12257
{
 
12258
  TABLE new_table;
 
12259
  TABLE_SHARE share;
 
12260
  const char *save_proc_info;
 
12261
  int write_err;
 
12262
 
 
12263
  if (table->s->db_type() != heap_hton || 
 
12264
      error != HA_ERR_RECORD_FILE_FULL)
 
12265
  {
 
12266
    table->file->print_error(error,MYF(0));
 
12267
    return(1);
 
12268
  }
 
12269
  new_table= *table;
 
12270
  share= *table->s;
 
12271
  new_table.s= &share;
 
12272
  new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
 
12273
  if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
 
12274
                                        new_table.s->db_type())))
 
12275
    return(1);                          // End of memory
 
12276
 
 
12277
  save_proc_info=thd->proc_info;
 
12278
  thd_proc_info(thd, "converting HEAP to MyISAM");
 
12279
 
 
12280
  if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
 
12281
                              recinfo, thd->lex->select_lex.options | 
 
12282
                                               thd->options))
 
12283
    goto err2;
 
12284
  if (open_tmp_table(&new_table))
 
12285
    goto err1;
 
12286
  if (table->file->indexes_are_disabled())
 
12287
    new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
 
12288
  table->file->ha_index_or_rnd_end();
 
12289
  table->file->ha_rnd_init(1);
 
12290
  if (table->no_rows)
 
12291
  {
 
12292
    new_table.file->extra(HA_EXTRA_NO_ROWS);
 
12293
    new_table.no_rows=1;
 
12294
  }
 
12295
 
 
12296
#ifdef TO_BE_DONE_LATER_IN_4_1
 
12297
  /*
 
12298
    To use start_bulk_insert() (which is new in 4.1) we need to find
 
12299
    all places where a corresponding end_bulk_insert() should be put.
 
12300
  */
 
12301
  table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
 
12302
  new_table.file->ha_start_bulk_insert(table->file->stats.records);
 
12303
#else
 
12304
  /* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
 
12305
  new_table.file->extra(HA_EXTRA_WRITE_CACHE);
 
12306
#endif
 
12307
 
 
12308
  /*
 
12309
    copy all old rows from heap table to MyISAM table
 
12310
    This is the only code that uses record[1] to read/write but this
 
12311
    is safe as this is a temporary MyISAM table without timestamp/autoincrement.
 
12312
  */
 
12313
  while (!table->file->rnd_next(new_table.record[1]))
 
12314
  {
 
12315
    write_err= new_table.file->ha_write_row(new_table.record[1]);
 
12316
    if (write_err)
 
12317
      goto err;
 
12318
  }
 
12319
  /* copy row that filled HEAP table */
 
12320
  if ((write_err=new_table.file->ha_write_row(table->record[0])))
 
12321
  {
 
12322
    if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
 
12323
        !ignore_last_dupp_key_error)
 
12324
      goto err;
 
12325
  }
 
12326
 
 
12327
  /* remove heap table and change to use myisam table */
 
12328
  (void) table->file->ha_rnd_end();
 
12329
  (void) table->file->close();                  // This deletes the table !
 
12330
  delete table->file;
 
12331
  table->file=0;
 
12332
  plugin_unlock(0, table->s->db_plugin);
 
12333
  share.db_plugin= my_plugin_lock(0, &share.db_plugin);
 
12334
  new_table.s= table->s;                       // Keep old share
 
12335
  *table= new_table;
 
12336
  *table->s= share;
 
12337
  
 
12338
  table->file->change_table_ptr(table, table->s);
 
12339
  table->use_all_columns();
 
12340
  if (save_proc_info)
 
12341
  {
 
12342
    const char *new_proc_info=
 
12343
      (!strcmp(save_proc_info,"Copying to tmp table") ?
 
12344
      "Copying to tmp table on disk" : save_proc_info);
 
12345
    thd_proc_info(thd, new_proc_info);
 
12346
  }
 
12347
  return(0);
 
12348
 
 
12349
 err:
 
12350
  table->file->print_error(write_err, MYF(0));
 
12351
  (void) table->file->ha_rnd_end();
 
12352
  (void) new_table.file->close();
 
12353
 err1:
 
12354
  new_table.file->ha_delete_table(new_table.s->table_name.str);
 
12355
 err2:
 
12356
  delete new_table.file;
 
12357
  thd_proc_info(thd, save_proc_info);
 
12358
  table->mem_root= new_table.mem_root;
 
12359
  return(1);
 
12360
}
 
12361
 
2968
12362
 
2969
12363
/**
2970
12364
  @details
2977
12371
  @return
2978
12372
    end_select function to use. This function can't fail.
2979
12373
*/
2980
 
Next_select_func setup_end_select_func(Join *join)
 
12374
 
 
12375
Next_select_func setup_end_select_func(JOIN *join)
2981
12376
{
2982
 
  Table *table= join->tmp_table;
2983
 
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
 
12377
  TABLE *table= join->tmp_table;
 
12378
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
2984
12379
  Next_select_func end_select;
2985
12380
 
2986
12381
  /* Set up select_end */
2987
12382
  if (table)
2988
12383
  {
2989
 
    if (table->group && tmp_tbl->sum_func_count &&
 
12384
    if (table->group && tmp_tbl->sum_func_count && 
2990
12385
        !tmp_tbl->precomputed_group_by)
2991
12386
    {
2992
 
      if (table->getShare()->sizeKeys())
 
12387
      if (table->s->keys)
2993
12388
      {
2994
 
        end_select= end_update;
 
12389
        end_select=end_update;
2995
12390
      }
2996
12391
      else
2997
12392
      {
2998
 
        end_select= end_unique_update;
 
12393
        end_select=end_unique_update;
2999
12394
      }
3000
12395
    }
3001
12396
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
3002
12397
    {
3003
 
      end_select= end_write_group;
 
12398
      end_select=end_write_group;
3004
12399
    }
3005
12400
    else
3006
12401
    {
3007
 
      end_select= end_write;
 
12402
      end_select=end_write;
3008
12403
      if (tmp_tbl->precomputed_group_by)
3009
12404
      {
3010
12405
        /*
3011
12406
          A preceding call to create_tmp_table in the case when loose
3012
12407
          index scan is used guarantees that
3013
 
          Tmp_Table_Param::items_to_copy has enough space for the group
 
12408
          TMP_TABLE_PARAM::items_to_copy has enough space for the group
3014
12409
          by functions. It is OK here to use memcpy since we copy
3015
12410
          Item_sum pointers into an array of Item pointers.
3016
12411
        */
3032
12427
  return end_select;
3033
12428
}
3034
12429
 
 
12430
 
3035
12431
/**
3036
12432
  Make a join of all tables and write it on socket or to table.
3037
12433
 
3042
12438
  @retval
3043
12439
    -1  if error should be sent
3044
12440
*/
3045
 
int do_select(Join *join, List<Item> *fields, Table *table)
 
12441
 
 
12442
static int
 
12443
do_select(JOIN *join,List<Item> *fields,TABLE *table)
3046
12444
{
3047
12445
  int rc= 0;
3048
12446
  enum_nested_loop_state error= NESTED_LOOP_OK;
3049
 
  JoinTable *join_tab= NULL;
3050
 
 
 
12447
  JOIN_TAB *join_tab= NULL;
 
12448
  
3051
12449
  join->tmp_table= table;                       /* Save for easy recursion */
3052
12450
  join->fields= fields;
3053
12451
 
3054
12452
  if (table)
3055
12453
  {
3056
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
3057
 
    table->emptyRecord();
 
12454
    VOID(table->file->extra(HA_EXTRA_WRITE_CACHE));
 
12455
    empty_record(table);
3058
12456
    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
 
    }
 
12457
        table->s->keys && !table->file->inited)
 
12458
      table->file->ha_index_init(0, 0);
3069
12459
  }
3070
12460
  /* Set up select_end */
3071
12461
  Next_select_func end_select= setup_end_select_func(join);
3086
12476
    {
3087
12477
      error= (*end_select)(join, 0, 0);
3088
12478
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
3089
 
              error= (*end_select)(join, 0, 1);
 
12479
        error= (*end_select)(join, 0, 1);
3090
12480
 
3091
12481
      /*
3092
12482
        If we don't go through evaluate_join_record(), do the counting
3094
12484
        so we don't touch it here.
3095
12485
      */
3096
12486
      join->examined_rows++;
3097
 
      join->session->row_count++;
 
12487
      join->thd->row_count++;
3098
12488
      assert(join->examined_rows <= 1);
3099
12489
    }
3100
12490
    else if (join->send_row_on_empty_set())
3124
12514
    if (!table)                                 // If sending data to client
3125
12515
    {
3126
12516
      /*
3127
 
        The following will unlock all cursors if the command wasn't an
3128
 
        update command
 
12517
        The following will unlock all cursors if the command wasn't an
 
12518
        update command
3129
12519
      */
3130
12520
      join->join_free();                        // Unlock all cursors
3131
12521
      if (join->result->send_eof())
3132
 
        rc= 1;                                  // Don't send error
 
12522
        rc= 1;                                  // Don't send error
3133
12523
    }
3134
12524
  }
3135
12525
  else
3137
12527
  if (table)
3138
12528
  {
3139
12529
    int tmp, new_errno= 0;
3140
 
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
12530
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
3141
12531
    {
3142
12532
      new_errno= tmp;
3143
12533
    }
3144
 
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
 
12534
    if ((tmp=table->file->ha_index_or_rnd_end()))
3145
12535
    {
3146
12536
      new_errno= tmp;
3147
12537
    }
3148
12538
    if (new_errno)
3149
 
      table->print_error(new_errno,MYF(0));
 
12539
      table->file->print_error(new_errno,MYF(0));
3150
12540
  }
3151
 
  return(join->session->is_error() ? -1 : rc);
 
12541
  return(join->thd->is_error() ? -1 : rc);
3152
12542
}
3153
12543
 
3154
 
enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records)
 
12544
 
 
12545
enum_nested_loop_state
 
12546
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3155
12547
{
3156
12548
  enum_nested_loop_state rc;
3157
12549
 
3162
12554
      rc= sub_select(join,join_tab,end_of_records);
3163
12555
    return rc;
3164
12556
  }
3165
 
  if (join->session->getKilled())               // If aborted by user
 
12557
  if (join->thd->killed)                // If aborted by user
3166
12558
  {
3167
 
    join->session->send_kill_message();
3168
 
    return NESTED_LOOP_KILLED;
 
12559
    join->thd->send_kill_message();
 
12560
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
3169
12561
  }
3170
12562
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3171
12563
  {
3172
 
    if (! join_tab->cache.store_record_in_cache())
 
12564
    if (!store_record_in_cache(&join_tab->cache))
3173
12565
      return NESTED_LOOP_OK;                     // There is more room in cache
3174
12566
    return flush_cached_records(join,join_tab,false);
3175
12567
  }
3182
12574
/**
3183
12575
  Retrieve records ends with a given beginning from the result of a join.
3184
12576
 
3185
 
    For a given partial join record consisting of records from the tables
 
12577
    For a given partial join record consisting of records from the tables 
3186
12578
    preceding the table join_tab in the execution plan, the function
3187
12579
    retrieves all matching full records from the result set and
3188
 
    send them to the result set stream.
 
12580
    send them to the result set stream. 
3189
12581
 
3190
12582
  @note
3191
12583
    The function effectively implements the  final (n-k) nested loops
3225
12617
    first row with t3.a=t1.a has been encountered.
3226
12618
    Thus, the second predicate P2 is supplied with a guarded value that are
3227
12619
    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
 
12620
    (table t2). When the first row with t3.a=t1.a for the  current row 
3229
12621
    of table t1  appears, the value becomes true. For now on the predicate
3230
12622
    is evaluated immediately after the row of table t2 has been read.
3231
12623
    When the first row with t3.a=t1.a has been encountered all
3233
12625
    Only when all of them are true the row is sent to the output stream.
3234
12626
    If not, the function returns to the lowest nest level that has a false
3235
12627
    attached condition.
3236
 
    The predicates from on expressions are also pushed down. If in the
 
12628
    The predicates from on expressions are also pushed down. If in the 
3237
12629
    the above example the on expression were (t3.a=t1.a AND t2.a=t1.a),
3238
12630
    then t1.a=t2.a would be pushed down to table t2, and without any
3239
12631
    guard.
3243
12635
    is complemented by nulls  for t2 and t3. Then the pushed down predicates
3244
12636
    are checked for the composed row almost in the same way as it had
3245
12637
    been done for the first row with a match. The only difference is
3246
 
    the predicates from on expressions are not checked.
 
12638
    the predicates from on expressions are not checked. 
3247
12639
 
3248
12640
  @par
3249
12641
  @b IMPLEMENTATION
3259
12651
    and a pointer to a guarding boolean variable.
3260
12652
    When the value of the guard variable is true the value of the object
3261
12653
    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
 
12654
    true. 
 
12655
    To carry out a return to a nested loop level of join table t the pointer 
3264
12656
    to t is remembered in the field 'return_tab' of the join structure.
3265
12657
    Consider the following query:
3266
12658
    @code
3277
12669
    t5.a=t3.a is found, the pushed down predicate t4.b=2 OR t4.b IS NULL
3278
12670
    becomes 'activated', as well the predicate t4.a=t2.a. But
3279
12671
    the predicate (t2.b=5 OR t2.b IS NULL) can not be checked until
3280
 
    t4.a=t2.a becomes true.
 
12672
    t4.a=t2.a becomes true. 
3281
12673
    In order not to re-evaluate the predicates that were already evaluated
3282
12674
    as attached pushed down predicates, a pointer to the the first
3283
12675
    most inner unmatched table is maintained in join_tab->first_unmatched.
3284
12676
    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.
 
12677
    this pointer for t5 is changed from t4 to t2.             
3286
12678
 
3287
12679
    @par
3288
12680
    @b STRUCTURE @b NOTES
3293
12685
  @param join      pointer to the structure providing all context info for
3294
12686
                   the query
3295
12687
  @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
 
12688
  @param end_records  true when we need to perform final steps of retrival   
3297
12689
 
3298
12690
  @return
3299
12691
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
3300
12692
*/
3301
 
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
 
12693
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
 
12694
 
 
12695
enum_nested_loop_state
 
12696
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3302
12697
{
3303
12698
  join_tab->table->null_row=0;
3304
12699
  if (end_of_records)
3306
12701
 
3307
12702
  int error;
3308
12703
  enum_nested_loop_state rc;
3309
 
  ReadRecord *info= &join_tab->read_record;
 
12704
  READ_RECORD *info= &join_tab->read_record;
 
12705
 
 
12706
  if (join_tab->flush_weedout_table)
 
12707
  {
 
12708
    do_sj_reset(join_tab->flush_weedout_table);
 
12709
  }
3310
12710
 
3311
12711
  if (join->resume_nested_loop)
3312
12712
  {
3334
12734
      /* Set first_unmatched for the last inner table of this group */
3335
12735
      join_tab->last_inner->first_unmatched= join_tab;
3336
12736
    }
3337
 
    join->session->row_count= 0;
 
12737
    join->thd->row_count= 0;
3338
12738
 
3339
12739
    error= (*join_tab->read_first_record)(join_tab);
3340
12740
    rc= evaluate_join_record(join, join_tab, error);
3341
12741
  }
3342
 
 
3343
 
  /*
3344
 
    Note: psergey has added the 2nd part of the following condition; the
 
12742
  
 
12743
  /* 
 
12744
    Note: psergey has added the 2nd part of the following condition; the 
3345
12745
    change should probably be made in 5.1, too.
3346
12746
  */
3347
12747
  while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
3359
12759
  return rc;
3360
12760
}
3361
12761
 
3362
 
int safe_index_read(JoinTable *tab)
3363
 
{
3364
 
  int error;
3365
 
  Table *table= tab->table;
3366
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
12762
 
 
12763
 
 
12764
 
 
12765
/*
 
12766
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
 
12767
 
 
12768
  SYNPOSIS
 
12769
    do_sj_dups_weedout()
 
12770
      
 
12771
  RETURN
 
12772
    -1  Error
 
12773
    1   The row combination is a duplicate (discard it)
 
12774
    0   The row combination is not a duplicate (continue)
 
12775
*/
 
12776
 
 
12777
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl) 
 
12778
{
 
12779
  int error;
 
12780
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
 
12781
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
 
12782
  uchar *ptr= sjtbl->tmp_table->record[0] + 1;
 
12783
  uchar *nulls_ptr= ptr;
 
12784
  
 
12785
  /* Put the the rowids tuple into table->record[0]: */
 
12786
 
 
12787
  // 1. Store the length 
 
12788
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
 
12789
  {
 
12790
    *ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
 
12791
    ptr++;
 
12792
  }
 
12793
  else
 
12794
  {
 
12795
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
 
12796
    ptr += 2;
 
12797
  }
 
12798
 
 
12799
  // 2. Zero the null bytes 
 
12800
  if (sjtbl->null_bytes)
 
12801
  {
 
12802
    memset(ptr, 0, sjtbl->null_bytes);
 
12803
    ptr += sjtbl->null_bytes; 
 
12804
  }
 
12805
 
 
12806
  // 3. Put the rowids
 
12807
  for (uint i=0; tab != tab_end; tab++, i++)
 
12808
  {
 
12809
    handler *h= tab->join_tab->table->file;
 
12810
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
 
12811
    {
 
12812
      /* It's a NULL-complemented row */
 
12813
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
 
12814
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
 
12815
    }
 
12816
    else
 
12817
    {
 
12818
      /* Copy the rowid value */
 
12819
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
12820
        h->position(tab->join_tab->table->record[0]);
 
12821
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
 
12822
    }
 
12823
  }
 
12824
 
 
12825
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
 
12826
  if (error)
 
12827
  {
 
12828
    /* create_myisam_from_heap will generate error if needed */
 
12829
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
12830
        create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo, 
 
12831
                                &sjtbl->recinfo, error, 1))
 
12832
      return -1;
 
12833
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
 
12834
    return 1;
 
12835
  }
 
12836
  return 0;
 
12837
}
 
12838
 
 
12839
 
 
12840
/*
 
12841
  SemiJoinDuplicateElimination: Reset the temporary table
 
12842
*/
 
12843
 
 
12844
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
 
12845
{
 
12846
  if (sj_tbl->tmp_table)
 
12847
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
 
12848
  return 0;
 
12849
}
 
12850
 
 
12851
/*
 
12852
  Process one record of the nested loop join.
 
12853
 
 
12854
    This function will evaluate parts of WHERE/ON clauses that are
 
12855
    applicable to the partial record on hand and in case of success
 
12856
    submit this record to the next level of the nested loop.
 
12857
*/
 
12858
 
 
12859
static enum_nested_loop_state
 
12860
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
12861
                     int error)
 
12862
{
 
12863
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
 
12864
  ha_rows found_records=join->found_records;
 
12865
  COND *select_cond= join_tab->select_cond;
 
12866
 
 
12867
  if (error > 0 || (join->thd->is_error()))     // Fatal error
 
12868
    return NESTED_LOOP_ERROR;
 
12869
  if (error < 0)
 
12870
    return NESTED_LOOP_NO_MORE_ROWS;
 
12871
  if (join->thd->killed)                        // Aborted by user
 
12872
  {
 
12873
    join->thd->send_kill_message();
 
12874
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
 
12875
  }
 
12876
  if (!select_cond || select_cond->val_int())
 
12877
  {
 
12878
    /*
 
12879
      There is no select condition or the attached pushed down
 
12880
      condition is true => a match is found.
 
12881
    */
 
12882
    bool found= 1;
 
12883
    while (join_tab->first_unmatched && found)
 
12884
    {
 
12885
      /*
 
12886
        The while condition is always false if join_tab is not
 
12887
        the last inner join table of an outer join operation.
 
12888
      */
 
12889
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
12890
      /*
 
12891
        Mark that a match for current outer table is found.
 
12892
        This activates push down conditional predicates attached
 
12893
        to the all inner tables of the outer join.
 
12894
      */
 
12895
      first_unmatched->found= 1;
 
12896
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
12897
      {
 
12898
        if (tab->table->reginfo.not_exists_optimize)
 
12899
          return NESTED_LOOP_NO_MORE_ROWS;
 
12900
        /* Check all predicates that has just been activated. */
 
12901
        /*
 
12902
          Actually all predicates non-guarded by first_unmatched->found
 
12903
          will be re-evaluated again. It could be fixed, but, probably,
 
12904
          it's not worth doing now.
 
12905
        */
 
12906
        if (tab->select_cond && !tab->select_cond->val_int())
 
12907
        {
 
12908
          /* The condition attached to table tab is false */
 
12909
          if (tab == join_tab)
 
12910
            found= 0;
 
12911
          else
 
12912
          {
 
12913
            /*
 
12914
              Set a return point if rejected predicate is attached
 
12915
              not to the last table of the current nest level.
 
12916
            */
 
12917
            join->return_tab= tab;
 
12918
            return NESTED_LOOP_OK;
 
12919
          }
 
12920
        }
 
12921
      }
 
12922
      /*
 
12923
        Check whether join_tab is not the last inner table
 
12924
        for another embedding outer join.
 
12925
      */
 
12926
      if ((first_unmatched= first_unmatched->first_upper) &&
 
12927
          first_unmatched->last_inner != join_tab)
 
12928
        first_unmatched= 0;
 
12929
      join_tab->first_unmatched= first_unmatched;
 
12930
    }
 
12931
 
 
12932
    JOIN_TAB *return_tab= join->return_tab;
 
12933
    join_tab->found_match= true;
 
12934
    if (join_tab->check_weed_out_table)
 
12935
    {
 
12936
      int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
 
12937
      if (res == -1)
 
12938
        return NESTED_LOOP_ERROR;
 
12939
      if (res == 1)
 
12940
        return NESTED_LOOP_OK;
 
12941
    }
 
12942
    else if (join_tab->do_firstmatch)
 
12943
    {
 
12944
      /* 
 
12945
        We should return to the join_tab->do_firstmatch after we have 
 
12946
        enumerated all the suffixes for current prefix row combination
 
12947
      */
 
12948
      return_tab= join_tab->do_firstmatch;
 
12949
    }
 
12950
 
 
12951
    /*
 
12952
      It was not just a return to lower loop level when one
 
12953
      of the newly activated predicates is evaluated as false
 
12954
      (See above join->return_tab= tab).
 
12955
    */
 
12956
    join->examined_rows++;
 
12957
    join->thd->row_count++;
 
12958
 
 
12959
    if (found)
 
12960
    {
 
12961
      enum enum_nested_loop_state rc;
 
12962
      /* A match from join_tab is found for the current partial join. */
 
12963
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
 
12964
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
12965
        return rc;
 
12966
      if (return_tab < join->return_tab)
 
12967
        join->return_tab= return_tab;
 
12968
 
 
12969
      if (join->return_tab < join_tab)
 
12970
        return NESTED_LOOP_OK;
 
12971
      /*
 
12972
        Test if this was a SELECT DISTINCT query on a table that
 
12973
        was not in the field list;  In this case we can abort if
 
12974
        we found a row, as no new rows can be added to the result.
 
12975
      */
 
12976
      if (not_used_in_distinct && found_records != join->found_records)
 
12977
        return NESTED_LOOP_NO_MORE_ROWS;
 
12978
    }
 
12979
    else
 
12980
      join_tab->read_record.file->unlock_row();
 
12981
  }
 
12982
  else
 
12983
  {
 
12984
    /*
 
12985
      The condition pushed down to the table join_tab rejects all rows
 
12986
      with the beginning coinciding with the current partial join.
 
12987
    */
 
12988
    join->examined_rows++;
 
12989
    join->thd->row_count++;
 
12990
    join_tab->read_record.file->unlock_row();
 
12991
  }
 
12992
  return NESTED_LOOP_OK;
 
12993
}
 
12994
 
 
12995
 
 
12996
/**
 
12997
 
 
12998
  @details
 
12999
    Construct a NULL complimented partial join record and feed it to the next
 
13000
    level of the nested loop. This function is used in case we have
 
13001
    an OUTER join and no matching record was found.
 
13002
*/
 
13003
 
 
13004
static enum_nested_loop_state
 
13005
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
 
13006
{
 
13007
  /*
 
13008
    The table join_tab is the first inner table of a outer join operation
 
13009
    and no matches has been found for the current outer row.
 
13010
  */
 
13011
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
 
13012
  /* Cache variables for faster loop */
 
13013
  COND *select_cond;
 
13014
  for ( ; join_tab <= last_inner_tab ; join_tab++)
 
13015
  {
 
13016
    /* Change the the values of guard predicate variables. */
 
13017
    join_tab->found= 1;
 
13018
    join_tab->not_null_compl= 0;
 
13019
    /* The outer row is complemented by nulls for each inner tables */
 
13020
    restore_record(join_tab->table,s->default_values);  // Make empty record
 
13021
    mark_as_null_row(join_tab->table);       // For group by without error
 
13022
    select_cond= join_tab->select_cond;
 
13023
    /* Check all attached conditions for inner table rows. */
 
13024
    if (select_cond && !select_cond->val_int())
 
13025
      return NESTED_LOOP_OK;
 
13026
  }
 
13027
  join_tab--;
 
13028
  /*
 
13029
    The row complemented by nulls might be the first row
 
13030
    of embedding outer joins.
 
13031
    If so, perform the same actions as in the code
 
13032
    for the first regular outer join row above.
 
13033
  */
 
13034
  for ( ; ; )
 
13035
  {
 
13036
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
13037
    if ((first_unmatched= first_unmatched->first_upper) &&
 
13038
        first_unmatched->last_inner != join_tab)
 
13039
      first_unmatched= 0;
 
13040
    join_tab->first_unmatched= first_unmatched;
 
13041
    if (!first_unmatched)
 
13042
      break;
 
13043
    first_unmatched->found= 1;
 
13044
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
13045
    {
 
13046
      if (tab->select_cond && !tab->select_cond->val_int())
 
13047
      {
 
13048
        join->return_tab= tab;
 
13049
        return NESTED_LOOP_OK;
 
13050
      }
 
13051
    }
 
13052
  }
 
13053
  /*
 
13054
    The row complemented by nulls satisfies all conditions
 
13055
    attached to inner tables.
 
13056
    Send the row complemented by nulls to be joined with the
 
13057
    remaining tables.
 
13058
  */
 
13059
  return (*join_tab->next_select)(join, join_tab+1, 0);
 
13060
}
 
13061
 
 
13062
 
 
13063
static enum_nested_loop_state
 
13064
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
 
13065
{
 
13066
  enum_nested_loop_state rc= NESTED_LOOP_OK;
 
13067
  int error;
 
13068
  READ_RECORD *info;
 
13069
 
 
13070
  join_tab->table->null_row= 0;
 
13071
  if (!join_tab->cache.records)
 
13072
    return NESTED_LOOP_OK;                      /* Nothing to do */
 
13073
  if (skip_last)
 
13074
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
 
13075
  if (join_tab->use_quick == 2)
 
13076
  {
 
13077
    if (join_tab->select->quick)
 
13078
    {                                   /* Used quick select last. reset it */
 
13079
      delete join_tab->select->quick;
 
13080
      join_tab->select->quick=0;
 
13081
    }
 
13082
  }
 
13083
 /* read through all records */
 
13084
  if ((error=join_init_read_record(join_tab)))
 
13085
  {
 
13086
    reset_cache_write(&join_tab->cache);
 
13087
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
 
13088
  }
 
13089
 
 
13090
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
 
13091
  {
 
13092
    tmp->status=tmp->table->status;
 
13093
    tmp->table->status=0;
 
13094
  }
 
13095
 
 
13096
  info= &join_tab->read_record;
 
13097
  do
 
13098
  {
 
13099
    if (join->thd->killed)
 
13100
    {
 
13101
      join->thd->send_kill_message();
 
13102
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
 
13103
    }
 
13104
    SQL_SELECT *select=join_tab->select;
 
13105
    if (rc == NESTED_LOOP_OK &&
 
13106
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
 
13107
    {
 
13108
      uint i;
 
13109
      reset_cache_read(&join_tab->cache);
 
13110
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
 
13111
      {
 
13112
        read_cached_record(join_tab);
 
13113
        if (!select || !select->skip_record())
 
13114
        {
 
13115
          int res= 0;
 
13116
          if (!join_tab->check_weed_out_table || 
 
13117
              !(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
 
13118
          {
 
13119
            rc= (join_tab->next_select)(join,join_tab+1,0);
 
13120
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
13121
            {
 
13122
              reset_cache_write(&join_tab->cache);
 
13123
              return rc;
 
13124
            }
 
13125
          }
 
13126
          if (res == -1)
 
13127
            return NESTED_LOOP_ERROR;
 
13128
        }
 
13129
      }
 
13130
    }
 
13131
  } while (!(error=info->read_record(info)));
 
13132
 
 
13133
  if (skip_last)
 
13134
    read_cached_record(join_tab);               // Restore current record
 
13135
  reset_cache_write(&join_tab->cache);
 
13136
  if (error > 0)                                // Fatal error
 
13137
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
 
13138
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
 
13139
    tmp2->table->status=tmp2->status;
 
13140
  return NESTED_LOOP_OK;
 
13141
}
 
13142
 
 
13143
 
 
13144
/*****************************************************************************
 
13145
  The different ways to read a record
 
13146
  Returns -1 if row was not found, 0 if row was found and 1 on errors
 
13147
*****************************************************************************/
 
13148
 
 
13149
/** Help function when we get some an error from the table handler. */
 
13150
 
 
13151
int report_error(TABLE *table, int error)
 
13152
{
 
13153
  if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
 
13154
  {
 
13155
    table->status= STATUS_GARBAGE;
 
13156
    return -1;                                  // key not found; ok
 
13157
  }
 
13158
  /*
 
13159
    Locking reads can legally return also these errors, do not
 
13160
    print them to the .err log
 
13161
  */
 
13162
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
 
13163
    sql_print_error("Got error %d when reading table '%s'",
 
13164
                    error, table->s->path.str);
 
13165
  table->file->print_error(error,MYF(0));
 
13166
  return 1;
 
13167
}
 
13168
 
 
13169
 
 
13170
int safe_index_read(JOIN_TAB *tab)
 
13171
{
 
13172
  int error;
 
13173
  TABLE *table= tab->table;
 
13174
  if ((error=table->file->index_read_map(table->record[0],
3367
13175
                                         tab->ref.key_buff,
3368
13176
                                         make_prev_keypart_map(tab->ref.key_parts),
3369
13177
                                         HA_READ_KEY_EXACT)))
3370
 
    return table->report_error(error);
 
13178
    return report_error(table, error);
3371
13179
  return 0;
3372
13180
}
3373
13181
 
 
13182
 
 
13183
static int
 
13184
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
 
13185
{
 
13186
  int error;
 
13187
  TABLE *table=tab->table;
 
13188
  table->const_table=1;
 
13189
  table->null_row=0;
 
13190
  table->status=STATUS_NO_RECORD;
 
13191
  
 
13192
  if (tab->type == JT_SYSTEM)
 
13193
  {
 
13194
    if ((error=join_read_system(tab)))
 
13195
    {                                           // Info for DESCRIBE
 
13196
      tab->info="const row not found";
 
13197
      /* Mark for EXPLAIN that the row was not found */
 
13198
      pos->records_read=0.0;
 
13199
      pos->ref_depend_map= 0;
 
13200
      if (!table->maybe_null || error > 0)
 
13201
        return(error);
 
13202
    }
 
13203
  }
 
13204
  else
 
13205
  {
 
13206
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
 
13207
        !table->no_keyread &&
 
13208
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
 
13209
    {
 
13210
      table->key_read=1;
 
13211
      table->file->extra(HA_EXTRA_KEYREAD);
 
13212
      tab->index= tab->ref.key;
 
13213
    }
 
13214
    error=join_read_const(tab);
 
13215
    if (table->key_read)
 
13216
    {
 
13217
      table->key_read=0;
 
13218
      table->file->extra(HA_EXTRA_NO_KEYREAD);
 
13219
    }
 
13220
    if (error)
 
13221
    {
 
13222
      tab->info="unique row not found";
 
13223
      /* Mark for EXPLAIN that the row was not found */
 
13224
      pos->records_read=0.0;
 
13225
      pos->ref_depend_map= 0;
 
13226
      if (!table->maybe_null || error > 0)
 
13227
        return(error);
 
13228
    }
 
13229
  }
 
13230
  if (*tab->on_expr_ref && !table->null_row)
 
13231
  {
 
13232
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
 
13233
      mark_as_null_row(table);  
 
13234
  }
 
13235
  if (!table->null_row)
 
13236
    table->maybe_null=0;
 
13237
 
 
13238
  /* Check appearance of new constant items in Item_equal objects */
 
13239
  JOIN *join= tab->join;
 
13240
  if (join->conds)
 
13241
    update_const_equal_items(join->conds, tab);
 
13242
  TABLE_LIST *tbl;
 
13243
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
 
13244
  {
 
13245
    TABLE_LIST *embedded;
 
13246
    TABLE_LIST *embedding= tbl;
 
13247
    do
 
13248
    {
 
13249
      embedded= embedding;
 
13250
      if (embedded->on_expr)
 
13251
         update_const_equal_items(embedded->on_expr, tab);
 
13252
      embedding= embedded->embedding;
 
13253
    }
 
13254
    while (embedding &&
 
13255
           embedding->nested_join->join_list.head() == embedded);
 
13256
  }
 
13257
 
 
13258
  return(0);
 
13259
}
 
13260
 
 
13261
 
 
13262
static int
 
13263
join_read_system(JOIN_TAB *tab)
 
13264
{
 
13265
  TABLE *table= tab->table;
 
13266
  int error;
 
13267
  if (table->status & STATUS_GARBAGE)           // If first read
 
13268
  {
 
13269
    if ((error=table->file->read_first_row(table->record[0],
 
13270
                                           table->s->primary_key)))
 
13271
    {
 
13272
      if (error != HA_ERR_END_OF_FILE)
 
13273
        return report_error(table, error);
 
13274
      mark_as_null_row(tab->table);
 
13275
      empty_record(table);                      // Make empty record
 
13276
      return -1;
 
13277
    }
 
13278
    store_record(table,record[1]);
 
13279
  }
 
13280
  else if (!table->status)                      // Only happens with left join
 
13281
    restore_record(table,record[1]);                    // restore old record
 
13282
  table->null_row=0;
 
13283
  return table->status ? -1 : 0;
 
13284
}
 
13285
 
 
13286
 
3374
13287
/**
3375
13288
  Read a (constant) table when there is at most one matching row.
3376
13289
 
3383
13296
  @retval
3384
13297
    1   Got an error (other than row not found) during read
3385
13298
*/
3386
 
int join_read_const(JoinTable *tab)
 
13299
 
 
13300
static int
 
13301
join_read_const(JOIN_TAB *tab)
3387
13302
{
3388
13303
  int error;
3389
 
  Table *table= tab->table;
 
13304
  TABLE *table= tab->table;
3390
13305
  if (table->status & STATUS_GARBAGE)           // If first read
3391
13306
  {
3392
13307
    table->status= 0;
3393
 
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3394
 
      error= HA_ERR_KEY_NOT_FOUND;
 
13308
    if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
 
13309
      error=HA_ERR_KEY_NOT_FOUND;
3395
13310
    else
3396
13311
    {
3397
 
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
3398
 
                                            (unsigned char*) tab->ref.key_buff,
 
13312
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
 
13313
                                            (uchar*) tab->ref.key_buff,
3399
13314
                                            make_prev_keypart_map(tab->ref.key_parts),
3400
13315
                                            HA_READ_KEY_EXACT);
3401
13316
    }
3402
13317
    if (error)
3403
13318
    {
3404
13319
      table->status= STATUS_NOT_FOUND;
3405
 
      tab->table->mark_as_null_row();
3406
 
      table->emptyRecord();
 
13320
      mark_as_null_row(tab->table);
 
13321
      empty_record(table);
3407
13322
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3408
 
        return table->report_error(error);
 
13323
        return report_error(table, error);
3409
13324
      return -1;
3410
13325
    }
3411
 
    table->storeRecord();
 
13326
    store_record(table,record[1]);
3412
13327
  }
3413
13328
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
3414
13329
  {
3415
13330
    table->status=0;
3416
 
    table->restoreRecord();                     // restore old record
 
13331
    restore_record(table,record[1]);                    // restore old record
3417
13332
  }
3418
13333
  table->null_row=0;
3419
13334
  return table->status ? -1 : 0;
3420
13335
}
3421
13336
 
 
13337
 
3422
13338
/*
3423
13339
  eq_ref access method implementation: "read_first" function
3424
13340
 
3425
13341
  SYNOPSIS
3426
13342
    join_read_key()
3427
 
      tab  JoinTable of the accessed table
 
13343
      tab  JOIN_TAB of the accessed table
3428
13344
 
3429
13345
  DESCRIPTION
3430
13346
    This is "read_fist" function for the "ref" access method. The difference
3432
13348
 
3433
13349
  RETURN
3434
13350
    0  - Ok
3435
 
   -1  - Row not found
 
13351
   -1  - Row not found 
3436
13352
    1  - Error
3437
13353
*/
3438
 
int join_read_key(JoinTable *tab)
 
13354
 
 
13355
static int
 
13356
join_read_key(JOIN_TAB *tab)
3439
13357
{
3440
13358
  int error;
3441
 
  Table *table= tab->table;
 
13359
  TABLE *table= tab->table;
3442
13360
 
3443
 
  if (!table->cursor->inited)
 
13361
  if (!table->file->inited)
3444
13362
  {
3445
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3446
 
    if (error != 0)
3447
 
    {
3448
 
      table->print_error(error, MYF(0));
3449
 
    }
 
13363
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3450
13364
  }
3451
13365
 
3452
 
  /* @todo Why don't we do "Late NULLs Filtering" here? */
 
13366
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3453
13367
  if (cmp_buffer_with_ref(tab) ||
3454
13368
      (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
3455
13369
  {
3458
13372
      table->status=STATUS_NOT_FOUND;
3459
13373
      return -1;
3460
13374
    }
3461
 
    error=table->cursor->index_read_map(table->getInsertRecord(),
 
13375
    error=table->file->index_read_map(table->record[0],
3462
13376
                                      tab->ref.key_buff,
3463
13377
                                      make_prev_keypart_map(tab->ref.key_parts),
3464
13378
                                      HA_READ_KEY_EXACT);
3465
13379
    if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3466
 
      return table->report_error(error);
 
13380
      return report_error(table, error);
3467
13381
  }
3468
13382
  table->null_row=0;
3469
13383
  return table->status ? -1 : 0;
3470
13384
}
3471
13385
 
 
13386
 
3472
13387
/*
3473
13388
  ref access method implementation: "read_first" function
3474
13389
 
3475
13390
  SYNOPSIS
3476
13391
    join_read_always_key()
3477
 
      tab  JoinTable of the accessed table
 
13392
      tab  JOIN_TAB of the accessed table
3478
13393
 
3479
13394
  DESCRIPTION
3480
 
    This is "read_first" function for the "ref" access method.
3481
 
 
 
13395
    This is "read_fist" function for the "ref" access method.
 
13396
   
3482
13397
    The functon must leave the index initialized when it returns.
3483
13398
    ref_or_null access implementation depends on that.
3484
13399
 
3485
13400
  RETURN
3486
13401
    0  - Ok
3487
 
   -1  - Row not found
 
13402
   -1  - Row not found 
3488
13403
    1  - Error
3489
13404
*/
3490
 
int join_read_always_key(JoinTable *tab)
 
13405
 
 
13406
static int
 
13407
join_read_always_key(JOIN_TAB *tab)
3491
13408
{
3492
13409
  int error;
3493
 
  Table *table= tab->table;
 
13410
  TABLE *table= tab->table;
3494
13411
 
3495
13412
  /* 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
 
 
 
13413
  if (!table->file->inited)
 
13414
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
13415
 
3503
13416
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3504
 
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
 
13417
  for (uint i= 0 ; i < tab->ref.key_parts ; i++)
3505
13418
  {
3506
13419
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
3507
13420
        return -1;
3508
13421
  }
3509
13422
 
3510
 
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
13423
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
3511
13424
    return -1;
3512
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
13425
  if ((error=table->file->index_read_map(table->record[0],
3513
13426
                                         tab->ref.key_buff,
3514
13427
                                         make_prev_keypart_map(tab->ref.key_parts),
3515
13428
                                         HA_READ_KEY_EXACT)))
3516
13429
  {
3517
13430
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3518
 
      return table->report_error(error);
3519
 
    return -1;
 
13431
      return report_error(table, error);
 
13432
    return -1; /* purecov: inspected */
3520
13433
  }
3521
 
 
3522
13434
  return 0;
3523
13435
}
3524
13436
 
 
13437
 
3525
13438
/**
3526
 
  This function is used when optimizing away ORDER BY in
 
13439
  This function is used when optimizing away ORDER BY in 
3527
13440
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
3528
13441
*/
3529
 
int join_read_last_key(JoinTable *tab)
 
13442
  
 
13443
static int
 
13444
join_read_last_key(JOIN_TAB *tab)
3530
13445
{
3531
13446
  int error;
3532
 
  Table *table= tab->table;
 
13447
  TABLE *table= tab->table;
3533
13448
 
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))
 
13449
  if (!table->file->inited)
 
13450
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
13451
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
3541
13452
    return -1;
3542
 
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
 
13453
  if ((error=table->file->index_read_last_map(table->record[0],
3543
13454
                                              tab->ref.key_buff,
3544
13455
                                              make_prev_keypart_map(tab->ref.key_parts))))
3545
13456
  {
3546
13457
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3547
 
      return table->report_error(error);
3548
 
    return -1;
 
13458
      return report_error(table, error);
 
13459
    return -1; /* purecov: inspected */
3549
13460
  }
3550
13461
  return 0;
3551
13462
}
3552
13463
 
3553
 
int join_no_more_records(ReadRecord *)
 
13464
 
 
13465
        /* ARGSUSED */
 
13466
static int
 
13467
join_no_more_records(READ_RECORD *info __attribute__((unused)))
3554
13468
{
3555
13469
  return -1;
3556
13470
}
3557
13471
 
3558
 
int join_read_next_same_diff(ReadRecord *info)
 
13472
static int
 
13473
join_read_next_same_diff(READ_RECORD *info)
3559
13474
{
3560
 
  Table *table= info->table;
3561
 
  JoinTable *tab=table->reginfo.join_tab;
 
13475
  TABLE *table= info->table;
 
13476
  JOIN_TAB *tab=table->reginfo.join_tab;
3562
13477
  if (tab->insideout_match_tab->found_match)
3563
13478
  {
3564
 
    KeyInfo *key= tab->table->key_info + tab->index;
3565
 
    do
 
13479
    KEY *key= tab->table->key_info + tab->index;
 
13480
    do 
3566
13481
    {
3567
13482
      int error;
3568
13483
      /* Save index tuple from record to the buffer */
3569
13484
      key_copy(tab->insideout_buf, info->record, key, 0);
3570
13485
 
3571
 
      if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
13486
      if ((error=table->file->index_next_same(table->record[0],
3572
13487
                                              tab->ref.key_buff,
3573
13488
                                              tab->ref.key_length)))
3574
13489
      {
3575
13490
        if (error != HA_ERR_END_OF_FILE)
3576
 
          return table->report_error(error);
 
13491
          return report_error(table, error);
3577
13492
        table->status= STATUS_GARBAGE;
3578
13493
        return -1;
3579
13494
      }
3580
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
13495
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3581
13496
                      tab->insideout_buf, key->key_length));
3582
13497
    tab->insideout_match_tab->found_match= 0;
3583
13498
    return 0;
3586
13501
    return join_read_next_same(info);
3587
13502
}
3588
13503
 
3589
 
int join_read_next_same(ReadRecord *info)
 
13504
static int
 
13505
join_read_next_same(READ_RECORD *info)
3590
13506
{
3591
13507
  int error;
3592
 
  Table *table= info->table;
3593
 
  JoinTable *tab=table->reginfo.join_tab;
 
13508
  TABLE *table= info->table;
 
13509
  JOIN_TAB *tab=table->reginfo.join_tab;
3594
13510
 
3595
 
  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
13511
  if ((error=table->file->index_next_same(table->record[0],
3596
13512
                                          tab->ref.key_buff,
3597
13513
                                          tab->ref.key_length)))
3598
13514
  {
3599
13515
    if (error != HA_ERR_END_OF_FILE)
3600
 
      return table->report_error(error);
 
13516
      return report_error(table, error);
3601
13517
    table->status= STATUS_GARBAGE;
3602
13518
    return -1;
3603
13519
  }
3604
 
 
3605
13520
  return 0;
3606
13521
}
3607
13522
 
3608
 
int join_read_prev_same(ReadRecord *info)
 
13523
 
 
13524
static int
 
13525
join_read_prev_same(READ_RECORD *info)
3609
13526
{
3610
13527
  int error;
3611
 
  Table *table= info->table;
3612
 
  JoinTable *tab=table->reginfo.join_tab;
 
13528
  TABLE *table= info->table;
 
13529
  JOIN_TAB *tab=table->reginfo.join_tab;
3613
13530
 
3614
 
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
3615
 
    return table->report_error(error);
 
13531
  if ((error=table->file->index_prev(table->record[0])))
 
13532
    return report_error(table, error);
3616
13533
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3617
13534
                      tab->ref.key_length))
3618
13535
  {
3622
13539
  return error;
3623
13540
}
3624
13541
 
3625
 
int join_init_quick_read_record(JoinTable *tab)
 
13542
 
 
13543
static int
 
13544
join_init_quick_read_record(JOIN_TAB *tab)
3626
13545
{
3627
13546
  if (test_if_quick_select(tab) == -1)
3628
13547
    return -1;                                  /* No possible records */
3629
13548
  return join_init_read_record(tab);
3630
13549
}
3631
13550
 
3632
 
int init_read_record_seq(JoinTable *tab)
 
13551
 
 
13552
int rr_sequential(READ_RECORD *info);
 
13553
int init_read_record_seq(JOIN_TAB *tab)
3633
13554
{
3634
 
  tab->read_record.init_reard_record_sequential();
3635
 
 
3636
 
  if (tab->read_record.cursor->startTableScan(1))
 
13555
  tab->read_record.read_record= rr_sequential;
 
13556
  if (tab->read_record.file->ha_rnd_init(1))
3637
13557
    return 1;
3638
13558
  return (*tab->read_record.read_record)(&tab->read_record);
3639
13559
}
3640
13560
 
3641
 
int test_if_quick_select(JoinTable *tab)
 
13561
static int
 
13562
test_if_quick_select(JOIN_TAB *tab)
3642
13563
{
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);
 
13564
  delete tab->select->quick;
 
13565
  tab->select->quick=0;
 
13566
  return tab->select->test_quick_select(tab->join->thd, tab->keys,
 
13567
                                        (table_map) 0, HA_POS_ERROR, 0,
 
13568
                                        false);
3647
13569
}
3648
13570
 
3649
 
int join_init_read_record(JoinTable *tab)
 
13571
 
 
13572
static int
 
13573
join_init_read_record(JOIN_TAB *tab)
3650
13574
{
3651
13575
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3652
13576
    return 1;
3653
 
 
3654
 
  if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true))
3655
 
    return 1;
3656
 
 
 
13577
  init_read_record(&tab->read_record, tab->join->thd, tab->table,
 
13578
                   tab->select,1,1);
3657
13579
  return (*tab->read_record.read_record)(&tab->read_record);
3658
13580
}
3659
13581
 
3660
 
int join_read_first(JoinTable *tab)
 
13582
 
 
13583
static int
 
13584
join_read_first(JOIN_TAB *tab)
3661
13585
{
3662
13586
  int error;
3663
 
  Table *table=tab->table;
3664
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
13587
  TABLE *table=tab->table;
 
13588
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3665
13589
      !table->no_keyread)
3666
13590
  {
3667
 
    table->key_read= 1;
3668
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
13591
    table->key_read=1;
 
13592
    table->file->extra(HA_EXTRA_KEYREAD);
3669
13593
  }
3670
 
  tab->table->status= 0;
 
13594
  tab->table->status=0;
3671
13595
  tab->read_record.table=table;
3672
 
  tab->read_record.cursor=table->cursor;
 
13596
  tab->read_record.file=table->file;
3673
13597
  tab->read_record.index=tab->index;
3674
 
  tab->read_record.record=table->getInsertRecord();
 
13598
  tab->read_record.record=table->record[0];
3675
13599
  if (tab->insideout_match_tab)
3676
13600
  {
3677
13601
    tab->read_record.do_insideout_scan= tab;
3684
13608
    tab->read_record.do_insideout_scan= 0;
3685
13609
  }
3686
13610
 
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())))
 
13611
  if (!table->file->inited)
 
13612
    table->file->ha_index_init(tab->index, tab->sorted);
 
13613
  if ((error=tab->table->file->index_first(tab->table->record[0])))
3697
13614
  {
3698
13615
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3699
 
      table->report_error(error);
 
13616
      report_error(table, error);
3700
13617
    return -1;
3701
13618
  }
3702
 
 
3703
13619
  return 0;
3704
13620
}
3705
13621
 
3706
 
int join_read_next_different(ReadRecord *info)
 
13622
 
 
13623
static int
 
13624
join_read_next_different(READ_RECORD *info)
3707
13625
{
3708
 
  JoinTable *tab= info->do_insideout_scan;
 
13626
  JOIN_TAB *tab= info->do_insideout_scan;
3709
13627
  if (tab->insideout_match_tab->found_match)
3710
13628
  {
3711
 
    KeyInfo *key= tab->table->key_info + tab->index;
3712
 
    do
 
13629
    KEY *key= tab->table->key_info + tab->index;
 
13630
    do 
3713
13631
    {
3714
13632
      int error;
3715
13633
      /* Save index tuple from record to the buffer */
3716
13634
      key_copy(tab->insideout_buf, info->record, key, 0);
3717
13635
 
3718
 
      if ((error=info->cursor->index_next(info->record)))
3719
 
        return info->table->report_error(error);
3720
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
13636
      if ((error=info->file->index_next(info->record)))
 
13637
        return report_error(info->table, error);
 
13638
      
 
13639
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3721
13640
                      tab->insideout_buf, key->key_length));
3722
13641
    tab->insideout_match_tab->found_match= 0;
3723
13642
    return 0;
3726
13645
    return join_read_next(info);
3727
13646
}
3728
13647
 
3729
 
int join_read_next(ReadRecord *info)
 
13648
 
 
13649
static int
 
13650
join_read_next(READ_RECORD *info)
3730
13651
{
3731
13652
  int error;
3732
 
  if ((error=info->cursor->index_next(info->record)))
3733
 
    return info->table->report_error(error);
 
13653
  if ((error=info->file->index_next(info->record)))
 
13654
    return report_error(info->table, error);
3734
13655
  return 0;
3735
13656
}
3736
13657
 
3737
 
int join_read_last(JoinTable *tab)
 
13658
 
 
13659
static int
 
13660
join_read_last(JOIN_TAB *tab)
3738
13661
{
3739
 
  Table *table=tab->table;
 
13662
  TABLE *table=tab->table;
3740
13663
  int error;
3741
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
13664
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3742
13665
      !table->no_keyread)
3743
13666
  {
3744
13667
    table->key_read=1;
3745
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
13668
    table->file->extra(HA_EXTRA_KEYREAD);
3746
13669
  }
3747
13670
  tab->table->status=0;
3748
13671
  tab->read_record.read_record=join_read_prev;
3749
13672
  tab->read_record.table=table;
3750
 
  tab->read_record.cursor=table->cursor;
 
13673
  tab->read_record.file=table->file;
3751
13674
  tab->read_record.index=tab->index;
3752
 
  tab->read_record.record=table->getInsertRecord();
3753
 
  if (!table->cursor->inited)
3754
 
  {
3755
 
    error= table->cursor->startIndexScan(tab->index, 1);
3756
 
    if (error != 0)
3757
 
      return table->report_error(error);
3758
 
  }
3759
 
  if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
3760
 
    return table->report_error(error);
3761
 
 
 
13675
  tab->read_record.record=table->record[0];
 
13676
  if (!table->file->inited)
 
13677
    table->file->ha_index_init(tab->index, 1);
 
13678
  if ((error= tab->table->file->index_last(tab->table->record[0])))
 
13679
    return report_error(table, error);
3762
13680
  return 0;
3763
13681
}
3764
13682
 
3765
 
int join_read_prev(ReadRecord *info)
 
13683
 
 
13684
static int
 
13685
join_read_prev(READ_RECORD *info)
3766
13686
{
3767
13687
  int error;
3768
 
  if ((error= info->cursor->index_prev(info->record)))
3769
 
    return info->table->report_error(error);
3770
 
 
 
13688
  if ((error= info->file->index_prev(info->record)))
 
13689
    return report_error(info->table, error);
3771
13690
  return 0;
3772
13691
}
3773
13692
 
3774
13693
/**
3775
13694
  Reading of key with key reference and one part that may be NULL.
3776
13695
*/
3777
 
int join_read_always_key_or_null(JoinTable *tab)
 
13696
 
 
13697
int
 
13698
join_read_always_key_or_null(JOIN_TAB *tab)
3778
13699
{
3779
13700
  int res;
3780
13701
 
3788
13709
  return safe_index_read(tab);
3789
13710
}
3790
13711
 
3791
 
int join_read_next_same_or_null(ReadRecord *info)
 
13712
 
 
13713
int
 
13714
join_read_next_same_or_null(READ_RECORD *info)
3792
13715
{
3793
13716
  int error;
3794
13717
  if ((error= join_read_next_same(info)) >= 0)
3795
13718
    return error;
3796
 
  JoinTable *tab= info->table->reginfo.join_tab;
 
13719
  JOIN_TAB *tab= info->table->reginfo.join_tab;
3797
13720
 
3798
13721
  /* Test if we have already done a read after null key */
3799
13722
  if (*tab->ref.null_ref_key)
3802
13725
  return safe_index_read(tab);                  // then read null keys
3803
13726
}
3804
13727
 
3805
 
enum_nested_loop_state end_send_group(Join *join, JoinTable *, bool end_of_records)
 
13728
 
 
13729
/*****************************************************************************
 
13730
  DESCRIPTION
 
13731
    Functions that end one nested loop iteration. Different functions
 
13732
    are used to support GROUP BY clause and to redirect records
 
13733
    to a table (e.g. in case of SELECT into a temporary table) or to the
 
13734
    network client.
 
13735
 
 
13736
  RETURN VALUES
 
13737
    NESTED_LOOP_OK           - the record has been successfully handled
 
13738
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
 
13739
                               was detected
 
13740
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
 
13741
                               the record
 
13742
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
 
13743
                               additionally, the nested loop produced the
 
13744
                               number of rows specified in the LIMIT clause
 
13745
                               for the query
 
13746
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
 
13747
                               additionally, there is a cursor and the nested
 
13748
                               loop algorithm produced the number of rows
 
13749
                               that is specified for current cursor fetch
 
13750
                               operation.
 
13751
   All return values except NESTED_LOOP_OK abort the nested loop.
 
13752
*****************************************************************************/
 
13753
 
 
13754
/* ARGSUSED */
 
13755
static enum_nested_loop_state
 
13756
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
13757
         bool end_of_records)
 
13758
{
 
13759
  if (!end_of_records)
 
13760
  {
 
13761
    int error;
 
13762
    if (join->having && join->having->val_int() == 0)
 
13763
      return(NESTED_LOOP_OK);               // Didn't match having
 
13764
    error=0;
 
13765
    if (join->do_send_rows)
 
13766
      error=join->result->send_data(*join->fields);
 
13767
    if (error)
 
13768
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
 
13769
    if (++join->send_records >= join->unit->select_limit_cnt &&
 
13770
        join->do_send_rows)
 
13771
    {
 
13772
      if (join->select_options & OPTION_FOUND_ROWS)
 
13773
      {
 
13774
        JOIN_TAB *jt=join->join_tab;
 
13775
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
 
13776
            && !join->send_group_parts && !join->having && !jt->select_cond &&
 
13777
            !(jt->select && jt->select->quick) &&
 
13778
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
13779
            (jt->ref.key < 0))
 
13780
        {
 
13781
          /* Join over all rows in table;  Return number of found rows */
 
13782
          TABLE *table=jt->table;
 
13783
 
 
13784
          join->select_options ^= OPTION_FOUND_ROWS;
 
13785
          if (table->sort.record_pointers ||
 
13786
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
 
13787
          {
 
13788
            /* Using filesort */
 
13789
            join->send_records= table->sort.found_records;
 
13790
          }
 
13791
          else
 
13792
          {
 
13793
            table->file->info(HA_STATUS_VARIABLE);
 
13794
            join->send_records= table->file->stats.records;
 
13795
          }
 
13796
        }
 
13797
        else 
 
13798
        {
 
13799
          join->do_send_rows= 0;
 
13800
          if (join->unit->fake_select_lex)
 
13801
            join->unit->fake_select_lex->select_limit= 0;
 
13802
          return(NESTED_LOOP_OK);
 
13803
        }
 
13804
      }
 
13805
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
 
13806
    }
 
13807
    else if (join->send_records >= join->fetch_limit)
 
13808
    {
 
13809
      /*
 
13810
        There is a server side cursor and all rows for
 
13811
        this fetch request are sent.
 
13812
      */
 
13813
      return(NESTED_LOOP_CURSOR_LIMIT);
 
13814
    }
 
13815
  }
 
13816
 
 
13817
  return(NESTED_LOOP_OK);
 
13818
}
 
13819
 
 
13820
 
 
13821
        /* ARGSUSED */
 
13822
enum_nested_loop_state
 
13823
end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
13824
               bool end_of_records)
3806
13825
{
3807
13826
  int idx= -1;
3808
13827
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
3810
13829
  if (!join->first_record || end_of_records ||
3811
13830
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3812
13831
  {
3813
 
    if (join->first_record ||
 
13832
    if (join->first_record || 
3814
13833
        (end_of_records && !join->group && !join->group_optimized_away))
3815
13834
    {
3816
13835
      if (idx < (int) join->send_group_parts)
3817
13836
      {
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();
 
13837
        int error=0;
 
13838
        {
 
13839
          if (!join->first_record)
 
13840
          {
 
13841
            List_iterator_fast<Item> it(*join->fields);
 
13842
            Item *item;
 
13843
            /* No matching rows for group function */
 
13844
            join->clear();
3826
13845
 
3827
13846
            while ((item= it++))
3828
13847
              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;
 
13848
          }
 
13849
          if (join->having && join->having->val_int() == 0)
 
13850
            error= -1;                          // Didn't satisfy having
 
13851
          else
 
13852
          {
 
13853
            if (join->do_send_rows)
 
13854
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
13855
            join->send_records++;
 
13856
          }
 
13857
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
13858
          {
 
13859
            if (join->rollup_send_data((uint) (idx+1)))
 
13860
              error= 1;
 
13861
          }
 
13862
        }
 
13863
        if (error > 0)
 
13864
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
 
13865
        if (end_of_records)
 
13866
          return(NESTED_LOOP_OK);
 
13867
        if (join->send_records >= join->unit->select_limit_cnt &&
 
13868
            join->do_send_rows)
 
13869
        {
 
13870
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
13871
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
13872
          join->do_send_rows=0;
 
13873
          join->unit->select_limit_cnt = HA_POS_ERROR;
3855
13874
        }
3856
13875
        else if (join->send_records >= join->fetch_limit)
3857
13876
        {
3870
13889
    else
3871
13890
    {
3872
13891
      if (end_of_records)
3873
 
        return(NESTED_LOOP_OK);
 
13892
        return(NESTED_LOOP_OK);
3874
13893
      join->first_record=1;
3875
 
      test_if_item_cache_changed(join->group_fields);
 
13894
      VOID(test_if_item_cache_changed(join->group_fields));
3876
13895
    }
3877
13896
    if (idx < (int) join->send_group_parts)
3878
13897
    {
3882
13901
      */
3883
13902
      copy_fields(&join->tmp_table_param);
3884
13903
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3885
 
        return(NESTED_LOOP_ERROR);
 
13904
        return(NESTED_LOOP_ERROR);
3886
13905
      return(ok_code);
3887
13906
    }
3888
13907
  }
3891
13910
  return(NESTED_LOOP_OK);
3892
13911
}
3893
13912
 
3894
 
enum_nested_loop_state end_write_group(Join *join, JoinTable *, bool end_of_records)
3895
 
{
3896
 
  Table *table=join->tmp_table;
 
13913
 
 
13914
        /* ARGSUSED */
 
13915
enum_nested_loop_state
 
13916
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
13917
          bool end_of_records)
 
13918
{
 
13919
  TABLE *table=join->tmp_table;
 
13920
 
 
13921
  if (join->thd->killed)                        // Aborted by user
 
13922
  {
 
13923
    join->thd->send_kill_message();
 
13924
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
13925
  }
 
13926
  if (!end_of_records)
 
13927
  {
 
13928
    copy_fields(&join->tmp_table_param);
 
13929
    copy_funcs(join->tmp_table_param.items_to_copy);
 
13930
#ifdef TO_BE_DELETED
 
13931
    if (!table->uniques)                        // If not unique handling
 
13932
    {
 
13933
      /* Copy null values from group to row */
 
13934
      ORDER   *group;
 
13935
      for (group=table->group ; group ; group=group->next)
 
13936
      {
 
13937
        Item *item= *group->item;
 
13938
        if (item->maybe_null)
 
13939
        {
 
13940
          Field *field=item->get_tmp_table_field();
 
13941
          field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0);
 
13942
        }
 
13943
      }
 
13944
    }
 
13945
#endif
 
13946
    if (!join->having || join->having->val_int())
 
13947
    {
 
13948
      int error;
 
13949
      join->found_records++;
 
13950
      if ((error=table->file->ha_write_row(table->record[0])))
 
13951
      {
 
13952
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
 
13953
          goto end;
 
13954
        if (create_myisam_from_heap(join->thd, table,
 
13955
                                    join->tmp_table_param.start_recinfo,
 
13956
                                    &join->tmp_table_param.recinfo,
 
13957
                                    error, 1))
 
13958
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
 
13959
        table->s->uniques=0;                    // To ensure rows are the same
 
13960
      }
 
13961
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
 
13962
          join->do_send_rows)
 
13963
      {
 
13964
        if (!(join->select_options & OPTION_FOUND_ROWS))
 
13965
          return(NESTED_LOOP_QUERY_LIMIT);
 
13966
        join->do_send_rows=0;
 
13967
        join->unit->select_limit_cnt = HA_POS_ERROR;
 
13968
        return(NESTED_LOOP_OK);
 
13969
      }
 
13970
    }
 
13971
  }
 
13972
end:
 
13973
  return(NESTED_LOOP_OK);
 
13974
}
 
13975
 
 
13976
/* ARGSUSED */
 
13977
/** Group by searching after group record and updating it if possible. */
 
13978
 
 
13979
static enum_nested_loop_state
 
13980
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
13981
           bool end_of_records)
 
13982
{
 
13983
  TABLE *table=join->tmp_table;
 
13984
  ORDER   *group;
 
13985
  int     error;
 
13986
 
 
13987
  if (end_of_records)
 
13988
    return(NESTED_LOOP_OK);
 
13989
  if (join->thd->killed)                        // Aborted by user
 
13990
  {
 
13991
    join->thd->send_kill_message();
 
13992
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
13993
  }
 
13994
 
 
13995
  join->found_records++;
 
13996
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
13997
  /* Make a key of group index */
 
13998
  for (group=table->group ; group ; group=group->next)
 
13999
  {
 
14000
    Item *item= *group->item;
 
14001
    item->save_org_in_field(group->field);
 
14002
    /* Store in the used key if the field was 0 */
 
14003
    if (item->maybe_null)
 
14004
      group->buff[-1]= (char) group->field->is_null();
 
14005
  }
 
14006
  if (!table->file->index_read_map(table->record[1],
 
14007
                                   join->tmp_table_param.group_buff,
 
14008
                                   HA_WHOLE_KEY,
 
14009
                                   HA_READ_KEY_EXACT))
 
14010
  {                                             /* Update old record */
 
14011
    restore_record(table,record[1]);
 
14012
    update_tmptable_sum_func(join->sum_funcs,table);
 
14013
    if ((error=table->file->ha_update_row(table->record[1],
 
14014
                                          table->record[0])))
 
14015
    {
 
14016
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
14017
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14018
    }
 
14019
    return(NESTED_LOOP_OK);
 
14020
  }
 
14021
 
 
14022
  /*
 
14023
    Copy null bits from group key to table
 
14024
    We can't copy all data as the key may have different format
 
14025
    as the row data (for example as with VARCHAR keys)
 
14026
  */
 
14027
  KEY_PART_INFO *key_part;
 
14028
  for (group=table->group,key_part=table->key_info[0].key_part;
 
14029
       group ;
 
14030
       group=group->next,key_part++)
 
14031
  {
 
14032
    if (key_part->null_bit)
 
14033
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
 
14034
  }
 
14035
  init_tmptable_sum_functions(join->sum_funcs);
 
14036
  copy_funcs(join->tmp_table_param.items_to_copy);
 
14037
  if ((error=table->file->ha_write_row(table->record[0])))
 
14038
  {
 
14039
    if (create_myisam_from_heap(join->thd, table,
 
14040
                                join->tmp_table_param.start_recinfo,
 
14041
                                &join->tmp_table_param.recinfo,
 
14042
                                error, 0))
 
14043
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
 
14044
    /* Change method to update rows */
 
14045
    table->file->ha_index_init(0, 0);
 
14046
    join->join_tab[join->tables-1].next_select=end_unique_update;
 
14047
  }
 
14048
  join->send_records++;
 
14049
  return(NESTED_LOOP_OK);
 
14050
}
 
14051
 
 
14052
 
 
14053
/** Like end_update, but this is done with unique constraints instead of keys.  */
 
14054
 
 
14055
static enum_nested_loop_state
 
14056
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
14057
                  bool end_of_records)
 
14058
{
 
14059
  TABLE *table=join->tmp_table;
 
14060
  int     error;
 
14061
 
 
14062
  if (end_of_records)
 
14063
    return(NESTED_LOOP_OK);
 
14064
  if (join->thd->killed)                        // Aborted by user
 
14065
  {
 
14066
    join->thd->send_kill_message();
 
14067
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
14068
  }
 
14069
 
 
14070
  init_tmptable_sum_functions(join->sum_funcs);
 
14071
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
14072
  copy_funcs(join->tmp_table_param.items_to_copy);
 
14073
 
 
14074
  if (!(error=table->file->ha_write_row(table->record[0])))
 
14075
    join->send_records++;                       // New group
 
14076
  else
 
14077
  {
 
14078
    if ((int) table->file->get_dup_key(error) < 0)
 
14079
    {
 
14080
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
14081
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14082
    }
 
14083
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
14084
    {
 
14085
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
14086
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14087
    }
 
14088
    restore_record(table,record[1]);
 
14089
    update_tmptable_sum_func(join->sum_funcs,table);
 
14090
    if ((error=table->file->ha_update_row(table->record[1],
 
14091
                                          table->record[0])))
 
14092
    {
 
14093
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
14094
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14095
    }
 
14096
  }
 
14097
  return(NESTED_LOOP_OK);
 
14098
}
 
14099
 
 
14100
 
 
14101
        /* ARGSUSED */
 
14102
enum_nested_loop_state
 
14103
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
14104
                bool end_of_records)
 
14105
{
 
14106
  TABLE *table=join->tmp_table;
3897
14107
  int     idx= -1;
3898
14108
 
3899
 
  if (join->session->getKilled())
 
14109
  if (join->thd->killed)
3900
14110
  {                                             // Aborted by user
3901
 
    join->session->send_kill_message();
3902
 
    return NESTED_LOOP_KILLED;
 
14111
    join->thd->send_kill_message();
 
14112
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
3903
14113
  }
3904
14114
  if (!join->first_record || end_of_records ||
3905
14115
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3909
14119
      int send_group_parts= join->send_group_parts;
3910
14120
      if (idx < send_group_parts)
3911
14121
      {
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;
 
14122
        if (!join->first_record)
 
14123
        {
 
14124
          /* No matching rows for group function */
 
14125
          join->clear();
 
14126
        }
 
14127
        copy_sum_funcs(join->sum_funcs,
 
14128
                       join->sum_funcs_end[send_group_parts]);
 
14129
        if (!join->having || join->having->val_int())
 
14130
        {
 
14131
          int error= table->file->ha_write_row(table->record[0]);
 
14132
          if (error && create_myisam_from_heap(join->thd, table,
 
14133
                                               join->tmp_table_param.start_recinfo,
 
14134
                                                &join->tmp_table_param.recinfo,
 
14135
                                               error, 0))
 
14136
            return(NESTED_LOOP_ERROR);
 
14137
        }
 
14138
        if (join->rollup.state != ROLLUP::STATE_NONE)
 
14139
        {
 
14140
          if (join->rollup_write_data((uint) (idx+1), table))
 
14141
            return(NESTED_LOOP_ERROR);
 
14142
        }
 
14143
        if (end_of_records)
 
14144
          return(NESTED_LOOP_OK);
3935
14145
      }
3936
14146
    }
3937
14147
    else
3938
14148
    {
3939
14149
      if (end_of_records)
3940
 
        return NESTED_LOOP_OK;
 
14150
        return(NESTED_LOOP_OK);
3941
14151
      join->first_record=1;
3942
 
      test_if_item_cache_changed(join->group_fields);
 
14152
      VOID(test_if_item_cache_changed(join->group_fields));
3943
14153
    }
3944
14154
    if (idx < (int) join->send_group_parts)
3945
14155
    {
3946
14156
      copy_fields(&join->tmp_table_param);
3947
 
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3948
 
        return NESTED_LOOP_ERROR;
 
14157
      copy_funcs(join->tmp_table_param.items_to_copy);
3949
14158
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3950
 
        return NESTED_LOOP_ERROR;
3951
 
      return NESTED_LOOP_OK;
 
14159
        return(NESTED_LOOP_ERROR);
 
14160
      return(NESTED_LOOP_OK);
3952
14161
    }
3953
14162
  }
3954
14163
  if (update_sum_func(join->sum_funcs))
3955
 
    return NESTED_LOOP_ERROR;
3956
 
  return NESTED_LOOP_OK;
 
14164
    return(NESTED_LOOP_ERROR);
 
14165
  return(NESTED_LOOP_OK);
3957
14166
}
3958
14167
 
 
14168
 
3959
14169
/*****************************************************************************
3960
14170
  Remove calculation with tables that aren't yet read. Remove also tests
3961
14171
  against fields that are read through key where the table is not a
3962
14172
  outer join table.
3963
14173
  We can't remove tests that are made against columns which are stored
3964
14174
  in sorted order.
 
14175
*****************************************************************************/
 
14176
 
 
14177
/**
3965
14178
  @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)
 
14179
    1 if right_item is used removable reference key on left_item
 
14180
*/
 
14181
 
 
14182
static bool test_if_ref(Item_field *left_item,Item *right_item)
3970
14183
{
3971
14184
  Field *field=left_item->field;
3972
14185
  // 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)
 
14186
  if (!field->table->const_table && !field->table->maybe_null)
3974
14187
  {
3975
 
    Item *ref_item=part_of_refkey(field->getTable(),field);
 
14188
    Item *ref_item=part_of_refkey(field->table,field);
3976
14189
    if (ref_item && ref_item->eq(right_item,1))
3977
14190
    {
3978
14191
      right_item= right_item->real_item();
3979
14192
      if (right_item->type() == Item::FIELD_ITEM)
3980
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
14193
        return (field->eq_def(((Item_field *) right_item)->field));
3981
14194
      /* remove equalities injected by IN->EXISTS transformation */
3982
14195
      else if (right_item->type() == Item::CACHE_ITEM)
3983
14196
        return ((Item_cache *)right_item)->eq_def (field);
3984
14197
      if (right_item->const_item() && !(right_item->is_null()))
3985
14198
      {
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
 
14199
        /*
 
14200
          We can remove binary fields and numerical fields except float,
 
14201
          as float comparison isn't 100 % secure
 
14202
          We have to keep normal strings to be able to check for end spaces
3990
14203
 
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
 
        }
 
14204
          sergefp: the above seems to be too restrictive. Counterexample:
 
14205
            create table t100 (v varchar(10), key(v)) default charset=latin1;
 
14206
            insert into t100 values ('a'),('a ');
 
14207
            explain select * from t100 where v='a';
 
14208
          The EXPLAIN shows 'using Where'. Running the query returns both
 
14209
          rows, so it seems there are no problems with endspace in the most
 
14210
          frequent case?
 
14211
        */
 
14212
        if (field->binary() &&
 
14213
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
 
14214
            field->decimals() == 0)
 
14215
        {
 
14216
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
14217
        }
4005
14218
      }
4006
14219
    }
4007
14220
  }
4008
 
  return 0;
 
14221
  return 0;                                     // keep test
 
14222
}
 
14223
 
 
14224
/**
 
14225
   @brief Replaces an expression destructively inside the expression tree of
 
14226
   the WHERE clase.
 
14227
 
 
14228
   @note Because of current requirements for semijoin flattening, we do not
 
14229
   need to recurse here, hence this function will only examine the top-level
 
14230
   AND conditions. (see JOIN::prepare, comment above the line 
 
14231
   'if (do_materialize)'
 
14232
   
 
14233
   @param join The top-level query.
 
14234
   @param old_cond The expression to be replaced.
 
14235
   @param new_cond The expression to be substituted.
 
14236
   @param do_fix_fields If true, Item::fix_fields(THD*, Item**) is called for
 
14237
   the new expression.
 
14238
   @return <code>true</code> if there was an error, <code>false</code> if
 
14239
   successful.
 
14240
*/
 
14241
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
14242
                                       Item *new_cond, bool do_fix_fields)
 
14243
{
 
14244
  if (join->conds == old_cond) {
 
14245
    join->conds= new_cond;
 
14246
    if (do_fix_fields)
 
14247
      new_cond->fix_fields(join->thd, &join->conds);
 
14248
    return false;
 
14249
  }
 
14250
  
 
14251
  if (join->conds->type() == Item::COND_ITEM) {
 
14252
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
 
14253
    Item *item;
 
14254
    while ((item= li++))
 
14255
      if (item == old_cond) 
 
14256
      {
 
14257
        li.replace(new_cond);
 
14258
        if (do_fix_fields)
 
14259
          new_cond->fix_fields(join->thd, li.ref());
 
14260
        return false;
 
14261
      }
 
14262
  }
 
14263
 
 
14264
  return true;
4009
14265
}
4010
14266
 
4011
14267
/*
4012
14268
  Extract a condition that can be checked after reading given table
4013
 
 
 
14269
  
4014
14270
  SYNOPSIS
4015
14271
    make_cond_for_table()
4016
14272
      cond         Condition to analyze
4017
14273
      tables       Tables for which "current field values" are available
4018
 
      used_table   Table that we're extracting the condition for (may
 
14274
      used_table   Table that we're extracting the condition for (may 
4019
14275
                   also include PSEUDO_TABLE_BITS
4020
14276
 
4021
14277
  DESCRIPTION
4025
14281
 
4026
14282
    The function assumes that
4027
14283
      - Constant parts of the condition has already been checked.
4028
 
      - Condition that could be checked for tables in 'tables' has already
 
14284
      - Condition that could be checked for tables in 'tables' has already 
4029
14285
        been checked.
4030
 
 
 
14286
        
4031
14287
    The function takes into account that some parts of the condition are
4032
14288
    guaranteed to be true by employed 'ref' access methods (the code that
4033
14289
    does this is located at the end, search down for "EQ_FUNC").
4034
14290
 
4035
14291
 
4036
 
  SEE ALSO
 
14292
  SEE ALSO 
4037
14293
    make_cond_for_info_schema uses similar algorithm
4038
14294
 
4039
14295
  RETURN
4040
14296
    Extracted condition
4041
14297
*/
4042
 
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
 
14298
 
 
14299
static COND *
 
14300
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
 
14301
                    bool exclude_expensive_cond)
4043
14302
{
4044
14303
  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()))
 
14304
      /*
 
14305
        Exclude constant conditions not checked at optimization time if
 
14306
        the table we are pushing conditions to is the first one.
 
14307
        As a result, such conditions are not considered as already checked
 
14308
        and will be checked at execution time, attached to the first table.
 
14309
      */
 
14310
      !((used_table & 1) && cond->is_expensive()))
4052
14311
    return (COND*) 0;                           // Already checked
4053
14312
  if (cond->type() == Item::COND_ITEM)
4054
14313
  {
4057
14316
      /* Create new top level AND item */
4058
14317
      Item_cond_and *new_cond=new Item_cond_and;
4059
14318
      if (!new_cond)
4060
 
        return (COND*) 0;
4061
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
14319
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
14320
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4062
14321
      Item *item;
4063
14322
      while ((item=li++))
4064
14323
      {
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);
 
14324
        Item *fix=make_cond_for_table(item,tables,used_table,
 
14325
                                      exclude_expensive_cond);
 
14326
        if (fix)
 
14327
          new_cond->argument_list()->push_back(fix);
4069
14328
      }
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;
 
14329
      switch (new_cond->argument_list()->elements) {
 
14330
      case 0:
 
14331
        return (COND*) 0;                       // Always true
 
14332
      case 1:
 
14333
        return new_cond->argument_list()->head();
 
14334
      default:
 
14335
        /*
 
14336
          Item_cond_and do not need fix_fields for execution, its parameters
 
14337
          are fixed or do not need fix_fields, too
 
14338
        */
 
14339
        new_cond->quick_fix_field();
 
14340
        new_cond->used_tables_cache=
 
14341
          ((Item_cond_and*) cond)->used_tables_cache &
 
14342
          tables;
 
14343
        return new_cond;
4084
14344
      }
4085
14345
    }
4086
14346
    else
4087
14347
    {                                           // Or list
4088
14348
      Item_cond_or *new_cond=new Item_cond_or;
4089
14349
      if (!new_cond)
4090
 
        return (COND*) 0;
4091
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
14350
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
14351
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4092
14352
      Item *item;
4093
14353
      while ((item=li++))
4094
14354
      {
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);
 
14355
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
14356
        if (!fix)
 
14357
          return (COND*) 0;                     // Always true
 
14358
        new_cond->argument_list()->push_back(fix);
4099
14359
      }
4100
14360
      /*
4101
 
        Item_cond_and do not need fix_fields for execution, its parameters
4102
 
        are fixed or do not need fix_fields, too
 
14361
        Item_cond_and do not need fix_fields for execution, its parameters
 
14362
        are fixed or do not need fix_fields, too
4103
14363
      */
4104
14364
      new_cond->quick_fix_field();
4105
14365
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4124
14384
  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
4125
14385
    return cond;                                // Not boolean op
4126
14386
 
4127
 
  /*
 
14387
  /* 
4128
14388
    Remove equalities that are guaranteed to be true by use of 'ref' access
4129
14389
    method
4130
14390
  */
4132
14392
  {
4133
14393
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
4134
14394
    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))
 
14395
    if (left_item->type() == Item::FIELD_ITEM &&
 
14396
        test_if_ref((Item_field*) left_item,right_item))
4136
14397
    {
4137
14398
      cond->marker=3;                   // Checked when read
4138
14399
      return (COND*) 0;
4139
14400
    }
4140
 
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
 
14401
    if (right_item->type() == Item::FIELD_ITEM &&
 
14402
        test_if_ref((Item_field*) right_item,left_item))
4141
14403
    {
4142
14404
      cond->marker=3;                   // Checked when read
4143
14405
      return (COND*) 0;
4147
14409
  return cond;
4148
14410
}
4149
14411
 
4150
 
static Item *part_of_refkey(Table *table,Field *field)
 
14412
 
 
14413
static Item *
 
14414
part_of_refkey(TABLE *table,Field *field)
4151
14415
{
4152
14416
  if (!table->reginfo.join_tab)
4153
14417
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
4154
14418
 
4155
 
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
 
14419
  uint ref_parts=table->reginfo.join_tab->ref.key_parts;
4156
14420
  if (ref_parts)
4157
14421
  {
4158
 
    KeyPartInfo *key_part=
 
14422
    KEY_PART_INFO *key_part=
4159
14423
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
4160
 
    uint32_t part;
 
14424
    uint part;
4161
14425
 
4162
14426
    for (part=0 ; part < ref_parts ; part++)
4163
14427
    {
4166
14430
    }
4167
14431
 
4168
14432
    for (part=0 ; part < ref_parts ; part++,key_part++)
4169
 
    {
4170
14433
      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
 
      {
 
14434
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
4176
14435
        return table->reginfo.join_tab->ref.items[part];
4177
 
      }
4178
 
    }
4179
14436
  }
4180
14437
  return (Item*) 0;
4181
14438
}
4182
14439
 
 
14440
 
4183
14441
/**
4184
 
  Test if one can use the key to resolve order_st BY.
 
14442
  Test if one can use the key to resolve ORDER BY.
4185
14443
 
4186
14444
  @param order                 Sort order
4187
14445
  @param table                 Table to sort
4200
14458
  @retval
4201
14459
    -1   Reverse key can be used
4202
14460
*/
4203
 
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
14461
 
 
14462
static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
 
14463
                                uint *used_key_parts)
4204
14464
{
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;
 
14465
  KEY_PART_INFO *key_part,*key_part_end;
 
14466
  key_part=table->key_info[idx].key_part;
 
14467
  key_part_end=key_part+table->key_info[idx].key_parts;
4209
14468
  key_part_map const_key_parts=table->const_key_parts[idx];
4210
 
  int reverse= 0;
 
14469
  int reverse=0;
4211
14470
  bool on_primary_key= false;
4212
14471
 
4213
14472
  for (; order ; order=order->next, const_key_parts>>=1)
4220
14479
      These are already skipped in the ORDER BY by const_expression_in_where()
4221
14480
    */
4222
14481
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
4223
 
      key_part++;
 
14482
      key_part++; 
4224
14483
 
4225
14484
    if (key_part == key_part_end)
4226
14485
    {
4227
 
      /*
 
14486
      /* 
4228
14487
        We are at the end of the key. Check if the engine has the primary
4229
14488
        key as a suffix to the secondary keys. If it has continue to check
4230
14489
        the primary key as a suffix.
4231
14490
      */
4232
14491
      if (!on_primary_key &&
4233
 
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4234
 
          table->getShare()->hasPrimaryKey())
 
14492
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
 
14493
          table->s->primary_key != MAX_KEY)
4235
14494
      {
4236
14495
        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()];
 
14496
        key_part= table->key_info[table->s->primary_key].key_part;
 
14497
        key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
 
14498
        const_key_parts=table->const_key_parts[table->s->primary_key];
4240
14499
 
4241
14500
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4242
 
          key_part++;
 
14501
          key_part++; 
4243
14502
        /*
4244
14503
         The primary and secondary key parts were all const (i.e. there's
4245
14504
         one row).  The sorting doesn't matter.
4263
14522
    key_part++;
4264
14523
  }
4265
14524
  *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) &
 
14525
    (uint) (key_part - table->key_info[idx].key_part);
 
14526
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
4268
14527
                         HA_READ_PREV))
4269
14528
    reverse= 0;                                 // Index can't be used
4270
14529
  return(reverse);
4271
14530
}
4272
14531
 
 
14532
 
 
14533
uint find_shortest_key(TABLE *table, const key_map *usable_keys)
 
14534
{
 
14535
  uint min_length= (uint) ~0;
 
14536
  uint best= MAX_KEY;
 
14537
  if (!usable_keys->is_clear_all())
 
14538
  {
 
14539
    for (uint nr=0; nr < table->s->keys ; nr++)
 
14540
    {
 
14541
      if (usable_keys->is_set(nr))
 
14542
      {
 
14543
        if (table->key_info[nr].key_length < min_length)
 
14544
        {
 
14545
          min_length=table->key_info[nr].key_length;
 
14546
          best=nr;
 
14547
        }
 
14548
      }
 
14549
    }
 
14550
  }
 
14551
  return best;
 
14552
}
 
14553
 
4273
14554
/**
4274
14555
  Test if a second key is the subkey of the first one.
4275
14556
 
4285
14566
  @retval
4286
14567
    0   no sub key
4287
14568
*/
4288
 
inline bool is_subkey(KeyPartInfo *key_part,
4289
 
                      KeyPartInfo *ref_key_part,
4290
 
                      KeyPartInfo *ref_key_part_end)
 
14569
 
 
14570
inline bool 
 
14571
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
 
14572
          KEY_PART_INFO *ref_key_part_end)
4291
14573
{
4292
14574
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4293
 
    if (! key_part->field->eq(ref_key_part->field))
 
14575
    if (!key_part->field->eq(ref_key_part->field))
4294
14576
      return 0;
4295
14577
  return 1;
4296
14578
}
4306
14588
    - MAX_KEY                   If we can't use other key
4307
14589
    - the number of found key   Otherwise
4308
14590
*/
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)
 
14591
 
 
14592
static uint
 
14593
test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts,
 
14594
               const key_map *usable_keys)
4314
14595
{
4315
 
  uint32_t nr;
4316
 
  uint32_t min_length= UINT32_MAX;
4317
 
  uint32_t best= MAX_KEY;
4318
 
  uint32_t not_used;
4319
 
  KeyPartInfo *ref_key_part= table->key_info[ref].key_part;
4320
 
  KeyPartInfo *ref_key_part_end= ref_key_part + ref_key_parts;
 
14596
  uint nr;
 
14597
  uint min_length= (uint) ~0;
 
14598
  uint best= MAX_KEY;
 
14599
  uint not_used;
 
14600
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
 
14601
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
4321
14602
 
4322
 
  for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
 
14603
  for (nr= 0 ; nr < table->s->keys ; nr++)
4323
14604
  {
4324
 
    if (usable_keys->test(nr) &&
 
14605
    if (usable_keys->is_set(nr) &&
4325
14606
        table->key_info[nr].key_length < min_length &&
4326
14607
        table->key_info[nr].key_parts >= ref_key_parts &&
4327
14608
        is_subkey(table->key_info[nr].key_part, ref_key_part,
4335
14616
  return best;
4336
14617
}
4337
14618
 
 
14619
 
4338
14620
/**
4339
14621
  Check if GROUP BY/DISTINCT can be optimized away because the set is
4340
14622
  already known to be distinct.
4353
14635
    of the table are referenced by a list : either the select list
4354
14636
    through find_field_in_item_list or GROUP BY list through
4355
14637
    find_field_in_order_list.
4356
 
    If the above holds and the key parts cannot contain NULLs then we
 
14638
    If the above holds and the key parts cannot contain NULLs then we 
4357
14639
    can safely remove the GROUP BY/DISTINCT,
4358
14640
    as no result set can be more distinct than an unique key.
4359
14641
 
4366
14648
  @retval
4367
14649
    0                    not found.
4368
14650
*/
4369
 
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
 
14651
 
 
14652
static bool
 
14653
list_contains_unique_index(TABLE *table,
 
14654
                          bool (*find_func) (Field *, void *), void *data)
4370
14655
{
4371
 
  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
 
14656
  for (uint keynr= 0; keynr < table->s->keys; keynr++)
4372
14657
  {
4373
 
    if (keynr == table->getShare()->getPrimaryKey() ||
 
14658
    if (keynr == table->s->primary_key ||
4374
14659
         (table->key_info[keynr].flags & HA_NOSAME))
4375
14660
    {
4376
 
      KeyInfo *keyinfo= table->key_info + keynr;
4377
 
      KeyPartInfo *key_part= NULL;
4378
 
      KeyPartInfo *key_part_end= NULL;
 
14661
      KEY *keyinfo= table->key_info + keynr;
 
14662
      KEY_PART_INFO *key_part, *key_part_end;
4379
14663
 
4380
14664
      for (key_part=keyinfo->key_part,
4381
14665
           key_part_end=key_part+ keyinfo->key_parts;
4382
14666
           key_part < key_part_end;
4383
14667
           key_part++)
4384
14668
      {
4385
 
        if (key_part->field->maybe_null() ||
4386
 
            ! find_func(key_part->field, data))
 
14669
        if (key_part->field->maybe_null() || 
 
14670
            !find_func(key_part->field, data))
4387
14671
          break;
4388
14672
      }
4389
14673
      if (key_part == key_part_end)
4393
14677
  return 0;
4394
14678
}
4395
14679
 
 
14680
 
4396
14681
/**
4397
14682
  Helper function for list_contains_unique_index.
4398
 
  Find a field reference in a list of order_st structures.
 
14683
  Find a field reference in a list of ORDER structures.
4399
14684
  Finds a direct reference of the Field in the list.
4400
14685
 
4401
14686
  @param field                The field to search for.
4402
 
  @param data                 order_st *.The list to search in
 
14687
  @param data                 ORDER *.The list to search in
4403
14688
 
4404
14689
  @retval
4405
14690
    1                    found
4406
14691
  @retval
4407
14692
    0                    not found.
4408
14693
*/
4409
 
bool find_field_in_order_list (Field *field, void *data)
 
14694
 
 
14695
static bool
 
14696
find_field_in_order_list (Field *field, void *data)
4410
14697
{
4411
 
  Order *group= (Order *) data;
 
14698
  ORDER *group= (ORDER *) data;
4412
14699
  bool part_found= 0;
4413
 
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
14700
  for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4414
14701
  {
4415
14702
    Item *item= (*tmp_group->item)->real_item();
4416
14703
    if (item->type() == Item::FIELD_ITEM &&
4423
14710
  return part_found;
4424
14711
}
4425
14712
 
 
14713
 
4426
14714
/**
4427
14715
  Helper function for list_contains_unique_index.
4428
14716
  Find a field reference in a dynamic list of Items.
4436
14724
  @retval
4437
14725
    0                    not found.
4438
14726
*/
4439
 
bool find_field_in_item_list (Field *field, void *data)
 
14727
 
 
14728
static bool
 
14729
find_field_in_item_list (Field *field, void *data)
4440
14730
{
4441
14731
  List<Item> *fields= (List<Item> *) data;
4442
14732
  bool part_found= 0;
4443
 
  List<Item>::iterator li(fields->begin());
 
14733
  List_iterator<Item> li(*fields);
4444
14734
  Item *item;
4445
14735
 
4446
14736
  while ((item= li++))
4455
14745
  return part_found;
4456
14746
}
4457
14747
 
 
14748
 
4458
14749
/**
4459
14750
  Test if we can skip the ORDER BY by using an index.
4460
14751
 
4466
14757
      no_changes
4467
14758
      map
4468
14759
 
4469
 
  If we can use an index, the JoinTable / tab->select struct
 
14760
  If we can use an index, the JOIN_TAB / tab->select struct
4470
14761
  is changed to use the index.
4471
14762
 
4472
14763
  The index must cover all fields in <order>, or it will not be considered.
4473
14764
 
4474
14765
  @todo
4475
 
    - sergeyp: Results of all index merge selects actually are ordered
 
14766
    - sergeyp: Results of all index merge selects actually are ordered 
4476
14767
    by clustered PK values.
4477
14768
 
4478
14769
  @retval
4480
14771
  @retval
4481
14772
    1    We can use an index.
4482
14773
*/
4483
 
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
14774
 
 
14775
static bool
 
14776
test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
 
14777
                        bool no_changes, const key_map *map)
4484
14778
{
4485
 
  int32_t ref_key;
4486
 
  uint32_t ref_key_parts;
 
14779
  int ref_key;
 
14780
  uint ref_key_parts;
4487
14781
  int order_direction;
4488
 
  uint32_t used_key_parts;
4489
 
  Table *table=tab->table;
4490
 
  optimizer::SqlSelect *select= tab->select;
 
14782
  uint used_key_parts;
 
14783
  TABLE *table=tab->table;
 
14784
  SQL_SELECT *select=tab->select;
4491
14785
  key_map usable_keys;
4492
 
  optimizer::QuickSelectInterface *save_quick= NULL;
 
14786
  QUICK_SELECT_I *save_quick= 0;
4493
14787
 
4494
14788
  /*
4495
 
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
 
14789
    Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
4496
14790
    been taken into account.
4497
14791
  */
4498
14792
  usable_keys= *map;
4499
14793
 
4500
 
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
14794
  for (ORDER *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4501
14795
  {
4502
14796
    Item *item= (*tmp_order->item)->real_item();
4503
14797
    if (item->type() != Item::FIELD_ITEM)
4504
14798
    {
4505
 
      usable_keys.reset();
 
14799
      usable_keys.clear_all();
4506
14800
      return(0);
4507
14801
    }
4508
 
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
4509
 
    if (usable_keys.none())
 
14802
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
 
14803
    if (usable_keys.is_clear_all())
4510
14804
      return(0);                                        // No usable keys
4511
14805
  }
4512
14806
 
4516
14810
  {
4517
14811
    ref_key=       tab->ref.key;
4518
14812
    ref_key_parts= tab->ref.key_parts;
4519
 
    if (tab->type == AM_REF_OR_NULL)
 
14813
    if (tab->type == JT_REF_OR_NULL)
4520
14814
      return(0);
4521
14815
  }
4522
 
  else if (select && select->quick)             // Range found by optimizer/range
 
14816
  else if (select && select->quick)             // Range found by opt_range
4523
14817
  {
4524
14818
    int quick_type= select->quick->get_type();
4525
14819
    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
 
14820
    /* 
 
14821
      assume results are not ordered when index merge is used 
 
14822
      TODO: sergeyp: Results of all index merge selects actually are ordered 
4529
14823
      by clustered PK values.
4530
14824
    */
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)
 
14825
  
 
14826
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || 
 
14827
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
 
14828
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
4535
14829
      return(0);
4536
14830
    ref_key=       select->quick->index;
4537
14831
    ref_key_parts= select->quick->used_key_parts;
4542
14836
    /*
4543
14837
      We come here when there is a REF key.
4544
14838
    */
4545
 
    if (! usable_keys.test(ref_key))
 
14839
    if (!usable_keys.is_set(ref_key))
4546
14840
    {
4547
14841
      /*
4548
 
        We come here when ref_key is not among usable_keys
 
14842
        We come here when ref_key is not among usable_keys
4549
14843
      */
4550
 
      uint32_t new_ref_key;
 
14844
      uint new_ref_key;
4551
14845
      /*
4552
 
        If using index only read, only consider other possible index only
4553
 
        keys
 
14846
        If using index only read, only consider other possible index only
 
14847
        keys
4554
14848
      */
4555
 
      if (table->covering_keys.test(ref_key))
4556
 
        usable_keys&= table->covering_keys;
 
14849
      if (table->covering_keys.is_set(ref_key))
 
14850
        usable_keys.intersect(table->covering_keys);
4557
14851
      if (tab->pre_idx_push_select_cond)
4558
14852
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
4559
14853
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
4560
14854
                                       &usable_keys)) < MAX_KEY)
4561
14855
      {
4562
 
        /* Found key that can be used to retrieve data in sorted order */
4563
 
        if (tab->ref.key >= 0)
4564
 
        {
 
14856
        /* Found key that can be used to retrieve data in sorted order */
 
14857
        if (tab->ref.key >= 0)
 
14858
        {
4565
14859
          /*
4566
 
            We'll use ref access method on key new_ref_key. In general case
 
14860
            We'll use ref access method on key new_ref_key. In general case 
4567
14861
            the index search tuple for new_ref_key will be different (e.g.
4568
14862
            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".
 
14863
            (part1, part2(N), ...) and the WHERE clause contains 
 
14864
            "part1 = const1 AND part2=const2". 
4571
14865
            So we build tab->ref from scratch here.
4572
14866
          */
4573
 
          optimizer::KeyUse *keyuse= tab->keyuse;
4574
 
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
 
14867
          KEYUSE *keyuse= tab->keyuse;
 
14868
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
4575
14869
            keyuse++;
4576
14870
 
4577
 
          if (create_ref_for_key(tab->join, tab, keyuse,
 
14871
          if (create_ref_for_key(tab->join, tab, keyuse, 
4578
14872
                                 tab->join->const_table_map))
4579
14873
            return(0);
4580
 
        }
4581
 
        else
4582
 
        {
 
14874
        }
 
14875
        else
 
14876
        {
4583
14877
          /*
4584
 
            The range optimizer constructed QuickRange for ref_key, and
 
14878
            The range optimizer constructed QUICK_RANGE for ref_key, and
4585
14879
            we want to use instead new_ref_key as the index. We can't
4586
14880
            just change the index of the quick select, because this may
4587
14881
            result in an incosistent QUICK_SELECT object. Below we
4589
14883
            parameres are set correctly by the range optimizer.
4590
14884
           */
4591
14885
          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.
 
14886
          new_ref_key_map.clear_all();  // Force the creation of quick select
 
14887
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
4594
14888
 
4595
 
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
 
14889
          if (select->test_quick_select(tab->join->thd, new_ref_key_map, 0,
4596
14890
                                        (tab->join->select_options &
4597
14891
                                         OPTION_FOUND_ROWS) ?
4598
14892
                                        HA_POS_ERROR :
4600
14894
                                        true) <=
4601
14895
              0)
4602
14896
            return(0);
4603
 
        }
 
14897
        }
4604
14898
        ref_key= new_ref_key;
4605
14899
      }
4606
14900
    }
4607
14901
    /* Check if we get the rows in requested sorted order by using the key */
4608
 
    if (usable_keys.test(ref_key) &&
 
14902
    if (usable_keys.is_set(ref_key) &&
4609
14903
        (order_direction= test_if_order_by_key(order,table,ref_key,
4610
14904
                                               &used_key_parts)))
4611
14905
      goto check_reverse_order;
4615
14909
      Check whether there is an index compatible with the given order
4616
14910
      usage of which is cheaper than usage of the ref_key index (ref_key>=0)
4617
14911
      or a table scan.
4618
 
      It may be the case if order_st/GROUP BY is used with LIMIT.
 
14912
      It may be the case if ORDER/GROUP BY is used with LIMIT.
4619
14913
    */
4620
 
    uint32_t nr;
 
14914
    uint nr;
4621
14915
    key_map keys;
4622
 
    uint32_t best_key_parts= 0;
 
14916
    uint best_key_parts= 0;
4623
14917
    int best_key_direction= 0;
4624
14918
    ha_rows best_records= 0;
4625
14919
    double read_time;
4626
14920
    int best_key= -1;
4627
14921
    bool is_best_covering= false;
4628
14922
    double fanout= 1;
4629
 
    Join *join= tab->join;
4630
 
    uint32_t tablenr= tab - join->join_tab;
4631
 
    ha_rows table_records= table->cursor->stats.records;
 
14923
    JOIN *join= tab->join;
 
14924
    uint tablenr= tab - join->join_tab;
 
14925
    ha_rows table_records= table->file->stats.records;
4632
14926
    bool group= join->group && order == join->group_list;
4633
 
    optimizer::Position cur_pos;
4634
14927
 
4635
14928
    /*
4636
14929
      If not used with LIMIT, only use keys if the whole query can be
4639
14932
    */
4640
14933
    if (select_limit >= table_records)
4641
14934
    {
4642
 
      /*
4643
 
        filesort() and join cache are usually faster than reading in
 
14935
      /* 
 
14936
        filesort() and join cache are usually faster than reading in 
4644
14937
        index order and not using join cache
4645
14938
        */
4646
 
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
 
14939
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
4647
14940
        return(0);
4648
 
      keys= *table->cursor->keys_to_use_for_scanning();
4649
 
      keys|= table->covering_keys;
 
14941
      keys= *table->file->keys_to_use_for_scanning();
 
14942
      keys.merge(table->covering_keys);
4650
14943
 
4651
14944
      /*
4652
 
        We are adding here also the index specified in FORCE INDEX clause,
4653
 
        if any.
4654
 
        This is to allow users to use index in order_st BY.
 
14945
        We are adding here also the index specified in FORCE INDEX clause, 
 
14946
        if any.
 
14947
        This is to allow users to use index in ORDER BY.
4655
14948
      */
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;
 
14949
      if (table->force_index) 
 
14950
        keys.merge(group ? table->keys_in_use_for_group_by :
 
14951
                           table->keys_in_use_for_order_by);
 
14952
      keys.intersect(usable_keys);
4660
14953
    }
4661
14954
    else
4662
14955
      keys= usable_keys;
4663
14956
 
4664
 
    cur_pos= join->getPosFromOptimalPlan(tablenr);
4665
 
    read_time= cur_pos.getCost();
4666
 
    for (uint32_t i= tablenr+1; i < join->tables; i++)
4667
 
    {
4668
 
      cur_pos= join->getPosFromOptimalPlan(i);
4669
 
      fanout*= cur_pos.getFanout(); // fanout is always >= 1
4670
 
    }
 
14957
    read_time= join->best_positions[tablenr].read_time;
 
14958
    for (uint i= tablenr+1; i < join->tables; i++)
 
14959
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
4671
14960
 
4672
 
    for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
 
14961
    for (nr=0; nr < table->s->keys ; nr++)
4673
14962
    {
4674
14963
      int direction;
4675
 
      if (keys.test(nr) &&
 
14964
      if (keys.is_set(nr) &&
4676
14965
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
4677
14966
      {
4678
 
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
4679
 
 
4680
 
        /*
 
14967
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
 
14968
        
 
14969
        /* 
4681
14970
          Don't use an index scan with ORDER BY without limit.
4682
14971
          For GROUP BY without limit always use index scan
4683
 
          if there is a suitable index.
 
14972
          if there is a suitable index. 
4684
14973
          Why we hold to this asymmetry hardly can be explained
4685
14974
          rationally. It's easy to demonstrate that using
4686
14975
          temporary table + filesort could be cheaper for grouping
4687
14976
          queries too.
4688
 
        */
 
14977
        */ 
4689
14978
        if (is_covering ||
4690
 
            select_limit != HA_POS_ERROR ||
 
14979
            select_limit != HA_POS_ERROR || 
4691
14980
            (ref_key < 0 && (group || table->force_index)))
4692
 
        {
 
14981
        { 
4693
14982
          double rec_per_key;
4694
14983
          double index_scan_time;
4695
 
          KeyInfo *keyinfo= tab->table->key_info+nr;
 
14984
          KEY *keyinfo= tab->table->key_info+nr;
4696
14985
          if (select_limit == HA_POS_ERROR)
4697
14986
            select_limit= table_records;
4698
14987
          if (group)
4699
14988
          {
4700
14989
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
4701
 
            set_if_bigger(rec_per_key, 1.0);
 
14990
            set_if_bigger(rec_per_key, 1);
4702
14991
            /*
4703
14992
              With a grouping query each group containing on average
4704
14993
              rec_per_key records produces only one row that will
4705
14994
              be included into the result set.
4706
 
            */
 
14995
            */  
4707
14996
            if (select_limit > table_records/rec_per_key)
4708
14997
                select_limit= table_records;
4709
14998
            else
4710
14999
              select_limit= (ha_rows) (select_limit*rec_per_key);
4711
15000
          }
4712
 
          /*
 
15001
          /* 
4713
15002
            If tab=tk is not the last joined table tn then to get first
4714
15003
            L records from the result set we can expect to retrieve
4715
15004
            only L/fanout(tk,tn) where fanout(tk,tn) says how many
4718
15007
            So the estimate for L/fanout(tk,tn) will be too optimistic
4719
15008
            and as result we'll choose an index scan when using ref/range
4720
15009
            access + filesort will be cheaper.
4721
 
          */
 
15010
          */
4722
15011
          select_limit= (ha_rows) (select_limit < fanout ?
4723
15012
                                   1 : select_limit/fanout);
4724
15013
          /*
4725
15014
            We assume that each of the tested indexes is not correlated
4726
15015
            with ref_key. Thus, to select first N records we have to scan
4727
 
            N/selectivity(ref_key) index entries.
 
15016
            N/selectivity(ref_key) index entries. 
4728
15017
            selectivity(ref_key) = #scanned_records/#table_records =
4729
15018
            table->quick_condition_rows/table_records.
4730
15019
            In any case we can't select more than #table_records.
4731
 
            N/(table->quick_condition_rows/table_records) > table_records
 
15020
            N/(table->quick_condition_rows/table_records) > table_records 
4732
15021
            <=> N > table->quick_condition_rows.
4733
 
          */
 
15022
          */ 
4734
15023
          if (select_limit > table->quick_condition_rows)
4735
15024
            select_limit= table_records;
4736
15025
          else
4738
15027
                                     (double) table_records /
4739
15028
                                      table->quick_condition_rows);
4740
15029
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
4741
 
          set_if_bigger(rec_per_key, 1.0);
 
15030
          set_if_bigger(rec_per_key, 1);
4742
15031
          /*
4743
15032
            Here we take into account the fact that rows are
4744
15033
            accessed in sequences rec_per_key records in each.
4745
15034
            Rows in such a sequence are supposed to be ordered
4746
15035
            by rowid/primary key. When reading the data
4747
15036
            in a sequence we'll touch not more pages than the
4748
 
            table cursor contains.
 
15037
            table file contains.
4749
15038
            TODO. Use the formula for a disk sweep sequential access
4750
 
            to calculate the cost of accessing data rows for one
 
15039
            to calculate the cost of accessing data rows for one 
4751
15040
            index entry.
4752
 
          */
 
15041
          */
4753
15042
          index_scan_time= select_limit/rec_per_key *
4754
 
                           min(rec_per_key, table->cursor->scan_time());
 
15043
                           min(rec_per_key, table->file->scan_time());
4755
15044
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
4756
15045
              index_scan_time < read_time)
4757
15046
          {
4758
15047
            ha_rows quick_records= table_records;
4759
15048
            if (is_best_covering && !is_covering)
4760
15049
              continue;
4761
 
            if (table->quick_keys.test(nr))
 
15050
            if (table->quick_keys.is_set(nr))
4762
15051
              quick_records= table->quick_rows[nr];
4763
15052
            if (best_key < 0 ||
4764
15053
                (select_limit <= min(quick_records,best_records) ?
4769
15058
              best_key_parts= keyinfo->key_parts;
4770
15059
              best_records= quick_records;
4771
15060
              is_best_covering= is_covering;
4772
 
              best_key_direction= direction;
 
15061
              best_key_direction= direction; 
4773
15062
            }
4774
 
          }
4775
 
        }
 
15063
          }   
 
15064
        }      
4776
15065
      }
4777
15066
    }
4778
15067
    if (best_key >= 0)
4779
15068
    {
4780
15069
      bool quick_created= false;
4781
 
      if (table->quick_keys.test(best_key) && best_key != ref_key)
 
15070
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
4782
15071
      {
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,
 
15072
        key_map map;
 
15073
        map.clear_all();       // Force the creation of quick select
 
15074
        map.set_bit(best_key); // only best_key.
 
15075
        quick_created=         
 
15076
          select->test_quick_select(join->thd, map, 0,
4788
15077
                                    join->select_options & OPTION_FOUND_ROWS ?
4789
15078
                                    HA_POS_ERROR :
4790
15079
                                    join->unit->select_limit_cnt,
4793
15082
      if (!no_changes)
4794
15083
      {
4795
15084
        if (!quick_created)
4796
 
        {
 
15085
        {
4797
15086
          tab->index= best_key;
4798
15087
          tab->read_first_record= best_key_direction > 0 ?
4799
15088
                                  join_read_first:join_read_last;
4800
 
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
 
15089
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
4801
15090
          if (select && select->quick)
4802
15091
          {
4803
 
            safe_delete(select->quick);
 
15092
            delete select->quick;
 
15093
            select->quick= 0;
4804
15094
          }
4805
 
          if (table->covering_keys.test(best_key))
 
15095
          if (table->covering_keys.is_set(best_key))
4806
15096
          {
4807
15097
            table->key_read=1;
4808
 
            table->cursor->extra(HA_EXTRA_KEYREAD);
 
15098
            table->file->extra(HA_EXTRA_KEYREAD);
4809
15099
          }
4810
 
          table->cursor->ha_index_or_rnd_end();
 
15100
          table->file->ha_index_or_rnd_end();
4811
15101
          if (join->select_options & SELECT_DESCRIBE)
4812
15102
          {
4813
15103
            tab->ref.key= -1;
4814
15104
            tab->ref.key_parts= 0;
4815
 
            if (select_limit < table_records)
 
15105
            if (select_limit < table_records) 
4816
15106
              tab->limit= select_limit;
4817
15107
          }
4818
15108
        }
4819
 
        else if (tab->type != AM_ALL)
 
15109
        else if (tab->type != JT_ALL)
4820
15110
        {
4821
15111
          /*
4822
15112
            We're about to use a quick access to the table.
4824
15114
            method is actually used.
4825
15115
          */
4826
15116
          assert(tab->select->quick);
4827
 
          tab->type= AM_ALL;
 
15117
          tab->type=JT_ALL;
4828
15118
          tab->use_quick=1;
4829
15119
          tab->ref.key= -1;
4830
15120
          tab->ref.key_parts=0;         // Don't use ref key.
4831
15121
          tab->read_first_record= join_init_read_record;
 
15122
          /*
 
15123
            TODO: update the number of records in join->best_positions[tablenr]
 
15124
          */
4832
15125
        }
4833
15126
      }
4834
15127
      used_key_parts= best_key_parts;
4835
15128
      order_direction= best_key_direction;
4836
15129
    }
4837
15130
    else
4838
 
      return(0);
4839
 
  }
 
15131
      return(0); 
 
15132
  } 
4840
15133
 
4841
 
check_reverse_order:
 
15134
check_reverse_order:                  
4842
15135
  if (order_direction == -1)            // If ORDER BY ... DESC
4843
15136
  {
4844
15137
    if (select && select->quick)
4845
15138
    {
4846
15139
      /*
4847
 
        Don't reverse the sort order, if it's already done.
 
15140
        Don't reverse the sort order, if it's already done.
4848
15141
        (In some cases test_if_order_by_key() can be called multiple times
4849
15142
      */
4850
 
      if (! select->quick->reverse_sorted())
 
15143
      if (!select->quick->reverse_sorted())
4851
15144
      {
4852
 
        optimizer::QuickSelectDescending *tmp= NULL;
 
15145
        QUICK_SELECT_DESC *tmp;
4853
15146
        bool error= false;
4854
15147
        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)
 
15148
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
 
15149
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
15150
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
15151
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
4859
15152
        {
4860
15153
          tab->limit= 0;
4861
15154
          select->quick= save_quick;
4862
 
          return 0; // Use filesort
 
15155
          return(0);                   // Use filesort
4863
15156
        }
4864
 
 
 
15157
            
4865
15158
        /* 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;
 
15159
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
 
15160
                                    used_key_parts, &error);
 
15161
        if (!tmp || error)
 
15162
        {
 
15163
          delete tmp;
4872
15164
          select->quick= save_quick;
4873
15165
          tab->limit= 0;
4874
 
          return 0; // Reverse sort not supported
4875
 
        }
4876
 
        select->quick= tmp;
 
15166
          return(0);            // Reverse sort not supported
 
15167
        }
 
15168
        select->quick=tmp;
4877
15169
      }
4878
15170
    }
4879
 
    else if (tab->type != AM_NEXT &&
 
15171
    else if (tab->type != JT_NEXT && 
4880
15172
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
4881
15173
    {
4882
15174
      /*
4883
 
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
 
15175
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
4884
15176
 
4885
 
        Use a traversal function that starts by reading the last row
4886
 
        with key part (A) and then traverse the index backwards.
 
15177
        Use a traversal function that starts by reading the last row
 
15178
        with key part (A) and then traverse the index backwards.
4887
15179
      */
4888
15180
      tab->read_first_record= join_read_last_key;
4889
15181
      tab->read_record.read_record= join_read_prev_same;
4891
15183
  }
4892
15184
  else if (select && select->quick)
4893
15185
    select->quick->sorted= 1;
4894
 
  return 1;
 
15186
  return(1);
4895
15187
}
4896
15188
 
 
15189
 
4897
15190
/*
4898
15191
  If not selecting by given key, create an index how records should be read
4899
15192
 
4900
15193
  SYNOPSIS
4901
15194
   create_sort_index()
4902
 
     session            Thread Cursor
 
15195
     thd                Thread handler
4903
15196
     tab                Table to sort (in join structure)
4904
15197
     order              How table should be sorted
4905
15198
     filesort_limit     Max number of rows that needs to be sorted
4906
15199
     select_limit       Max number of rows in final output
4907
15200
                        Used to decide if we should use index or not
4908
 
     is_order_by        true if we are sorting on order_st BY, false if GROUP BY
4909
 
                        Used to decide if we should use index or not
 
15201
     is_order_by        true if we are sorting on ORDER BY, false if GROUP BY
 
15202
                        Used to decide if we should use index or not     
4910
15203
 
4911
15204
 
4912
15205
  IMPLEMENTATION
4913
15206
   - If there is an index that can be used, 'tab' is modified to use
4914
15207
     this index.
4915
 
   - If no index, create with filesort() an index cursor that can be used to
 
15208
   - If no index, create with filesort() an index file that can be used to
4916
15209
     retrieve rows in order (should be done with 'read_record').
4917
15210
     The sorted data is stored in tab->table and will be freed when calling
4918
 
     tab->table->free_io_cache().
 
15211
     free_io_cache(tab->table).
4919
15212
 
4920
15213
  RETURN VALUES
4921
15214
    0           ok
4922
15215
    -1          Some fatal error
4923
15216
    1           No records
4924
15217
*/
4925
 
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
15218
 
 
15219
static int
 
15220
create_sort_index(THD *thd, JOIN *join, ORDER *order,
 
15221
                  ha_rows filesort_limit, ha_rows select_limit,
 
15222
                  bool is_order_by)
4926
15223
{
4927
 
  uint32_t length= 0;
 
15224
  uint length= 0;
4928
15225
  ha_rows examined_rows;
4929
 
  Table *table;
4930
 
  optimizer::SqlSelect *select= NULL;
4931
 
  JoinTable *tab;
 
15226
  TABLE *table;
 
15227
  SQL_SELECT *select;
 
15228
  JOIN_TAB *tab;
4932
15229
 
4933
15230
  if (join->tables == join->const_tables)
4934
15231
    return(0);                          // One row, no need to sort
4942
15239
    is going to be used as it is applied now only for one table queries
4943
15240
    with covering indexes.
4944
15241
  */
4945
 
  if ((order != join->group_list ||
 
15242
  if ((order != join->group_list || 
4946
15243
       !(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,
 
15244
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
 
15245
      test_if_skip_sort_order(tab,order,select_limit,0, 
4949
15246
                              is_order_by ?  &table->keys_in_use_for_order_by :
4950
15247
                              &table->keys_in_use_for_group_by))
4951
15248
    return(0);
4952
 
  for (Order *ord= join->order; ord; ord= ord->next)
 
15249
  for (ORDER *ord= join->order; ord; ord= ord->next)
4953
15250
    length++;
4954
 
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
4955
 
  {
4956
 
    return(-1);
4957
 
  }
 
15251
  if (!(join->sortorder= 
 
15252
        make_unireg_sortorder(order, &length, join->sortorder)))
 
15253
    goto err;                           /* purecov: inspected */
4958
15254
 
4959
 
  table->sort.io_cache= new internal::IO_CACHE;
 
15255
  table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
 
15256
                                             MYF(MY_WME | MY_ZEROFILL));
4960
15257
  table->status=0;                              // May be wrong if quick_select
4961
15258
 
4962
15259
  // If table has a range, move it to select
4966
15263
    {
4967
15264
      select->quick=tab->quick;
4968
15265
      tab->quick=0;
4969
 
      /*
 
15266
      /* 
4970
15267
        We can only use 'Only index' if quick key is same as ref_key
4971
15268
        and in index_merge 'Only index' cannot be used
4972
15269
      */
4973
 
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
 
15270
      if (table->key_read && ((uint) tab->ref.key != select->quick->index))
4974
15271
      {
4975
 
        table->key_read=0;
4976
 
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
15272
        table->key_read=0;
 
15273
        table->file->extra(HA_EXTRA_NO_KEYREAD);
4977
15274
      }
4978
15275
    }
4979
15276
    else
4980
15277
    {
4981
15278
      /*
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.
 
15279
        We have a ref on a const;  Change this to a range that filesort
 
15280
        can use.
 
15281
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
15282
        field, quick will contain an empty record set.
4986
15283
      */
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
 
      }
 
15284
      if (!(select->quick= (get_quick_select_for_ref(thd, table, &tab->ref, 
 
15285
                                                     tab->found_records))))
 
15286
        goto err;
4994
15287
    }
4995
15288
  }
4996
15289
 
4997
 
  if (table->getShare()->getType())
4998
 
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
 
15290
  /* Fill schema tables with data before filesort if it's necessary */
 
15291
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
15292
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
 
15293
    goto err;
4999
15294
 
5000
 
  FileSort filesort(*session);
5001
 
  table->sort.found_records=filesort.run(table,join->sortorder, length,
5002
 
                                         select, filesort_limit, 0,
5003
 
                                         examined_rows);
 
15295
  if (table->s->tmp_table)
 
15296
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
 
15297
  table->sort.found_records=filesort(thd, table,join->sortorder, length,
 
15298
                                     select, filesort_limit, 0,
 
15299
                                     &examined_rows);
5004
15300
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5005
15301
  if (select)
5006
15302
  {
5010
15306
  tab->select_cond=0;
5011
15307
  tab->last_inner= 0;
5012
15308
  tab->first_unmatched= 0;
5013
 
  tab->type= AM_ALL;                            // Read with normal read_record
 
15309
  tab->type=JT_ALL;                             // Read with normal read_record
5014
15310
  tab->read_first_record= join_init_read_record;
5015
15311
  tab->join->examined_rows+=examined_rows;
5016
15312
  if (table->key_read)                          // Restore if we used indexes
5017
15313
  {
5018
15314
    table->key_read=0;
5019
 
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
15315
    table->file->extra(HA_EXTRA_NO_KEYREAD);
5020
15316
  }
5021
 
 
5022
15317
  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;
 
15318
err:
 
15319
  return(-1);
 
15320
}
 
15321
 
 
15322
/*****************************************************************************
 
15323
  Remove duplicates from tmp table
 
15324
  This should be recoded to add a unique index to the table and remove
 
15325
  duplicates
 
15326
  Table is a locked single thread table
 
15327
  fields is the number of fields to check (from the end)
 
15328
*****************************************************************************/
 
15329
 
 
15330
static bool compare_record(TABLE *table, Field **ptr)
 
15331
{
 
15332
  for (; *ptr ; ptr++)
 
15333
  {
 
15334
    if ((*ptr)->cmp_offset(table->s->rec_buff_length))
 
15335
      return 1;
 
15336
  }
 
15337
  return 0;
 
15338
}
 
15339
 
 
15340
static bool copy_blobs(Field **ptr)
 
15341
{
 
15342
  for (; *ptr ; ptr++)
 
15343
  {
 
15344
    if ((*ptr)->flags & BLOB_FLAG)
 
15345
      if (((Field_blob *) (*ptr))->copy())
 
15346
        return 1;                               // Error
 
15347
  }
 
15348
  return 0;
 
15349
}
 
15350
 
 
15351
static void free_blobs(Field **ptr)
 
15352
{
 
15353
  for (; *ptr ; ptr++)
 
15354
  {
 
15355
    if ((*ptr)->flags & BLOB_FLAG)
 
15356
      ((Field_blob *) (*ptr))->free();
 
15357
  }
 
15358
}
 
15359
 
 
15360
 
 
15361
static int
 
15362
remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having)
 
15363
{
 
15364
  int error;
 
15365
  ulong reclength,offset;
 
15366
  uint field_count;
 
15367
  THD *thd= join->thd;
 
15368
 
 
15369
  entry->reginfo.lock_type=TL_WRITE;
 
15370
 
 
15371
  /* Calculate how many saved fields there is in list */
 
15372
  field_count=0;
 
15373
  List_iterator<Item> it(fields);
 
15374
  Item *item;
 
15375
  while ((item=it++))
 
15376
  {
 
15377
    if (item->get_tmp_table_field() && ! item->const_item())
 
15378
      field_count++;
 
15379
  }
 
15380
 
 
15381
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) 
 
15382
  {                    // only const items with no OPTION_FOUND_ROWS
 
15383
    join->unit->select_limit_cnt= 1;            // Only send first row
 
15384
    return(0);
 
15385
  }
 
15386
  Field **first_field=entry->field+entry->s->fields - field_count;
 
15387
  offset= (field_count ? 
 
15388
           entry->field[entry->s->fields - field_count]->
 
15389
           offset(entry->record[0]) : 0);
 
15390
  reclength=entry->s->reclength-offset;
 
15391
 
 
15392
  free_io_cache(entry);                         // Safety
 
15393
  entry->file->info(HA_STATUS_VARIABLE);
 
15394
  if (entry->s->db_type() == heap_hton ||
 
15395
      (!entry->s->blob_fields &&
 
15396
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
 
15397
        thd->variables.sortbuff_size)))
 
15398
    error=remove_dup_with_hash_index(join->thd, entry,
 
15399
                                     field_count, first_field,
 
15400
                                     reclength, having);
 
15401
  else
 
15402
    error=remove_dup_with_compare(join->thd, entry, first_field, offset,
 
15403
                                  having);
 
15404
 
 
15405
  free_blobs(first_field);
 
15406
  return(error);
 
15407
}
 
15408
 
 
15409
 
 
15410
static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field,
 
15411
                                   ulong offset, Item *having)
 
15412
{
 
15413
  handler *file=table->file;
5028
15414
  char *org_record,*new_record;
5029
 
  unsigned char *record;
 
15415
  uchar *record;
5030
15416
  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);
 
15417
  ulong reclength= table->s->reclength-offset;
 
15418
 
 
15419
  org_record=(char*) (record=table->record[0])+offset;
 
15420
  new_record=(char*) table->record[1]+offset;
 
15421
 
 
15422
  file->ha_rnd_init(1);
 
15423
  error=file->rnd_next(record);
5040
15424
  for (;;)
5041
15425
  {
5042
 
    if (session->getKilled())
 
15426
    if (thd->killed)
5043
15427
    {
5044
 
      session->send_kill_message();
 
15428
      thd->send_kill_message();
5045
15429
      error=0;
5046
15430
      goto err;
5047
15431
    }
5048
15432
    if (error)
5049
15433
    {
5050
15434
      if (error == HA_ERR_RECORD_DELETED)
5051
 
        continue;
 
15435
        continue;
5052
15436
      if (error == HA_ERR_END_OF_FILE)
5053
 
        break;
 
15437
        break;
5054
15438
      goto err;
5055
15439
    }
5056
15440
    if (having && !having->val_int())
5057
15441
    {
5058
 
      if ((error=cursor->deleteRecord(record)))
5059
 
        goto err;
5060
 
      error=cursor->rnd_next(record);
 
15442
      if ((error=file->ha_delete_row(record)))
 
15443
        goto err;
 
15444
      error=file->rnd_next(record);
5061
15445
      continue;
5062
15446
    }
5063
15447
    if (copy_blobs(first_field))
5068
15452
    }
5069
15453
    memcpy(new_record,org_record,reclength);
5070
15454
 
5071
 
    /* Read through rest of cursor and mark duplicated rows deleted */
 
15455
    /* Read through rest of file and mark duplicated rows deleted */
5072
15456
    bool found=0;
5073
15457
    for (;;)
5074
15458
    {
5075
 
      if ((error=cursor->rnd_next(record)))
 
15459
      if ((error=file->rnd_next(record)))
5076
15460
      {
5077
 
        if (error == HA_ERR_RECORD_DELETED)
5078
 
          continue;
5079
 
        if (error == HA_ERR_END_OF_FILE)
5080
 
          break;
5081
 
        goto err;
 
15461
        if (error == HA_ERR_RECORD_DELETED)
 
15462
          continue;
 
15463
        if (error == HA_ERR_END_OF_FILE)
 
15464
          break;
 
15465
        goto err;
5082
15466
      }
5083
 
      if (table->compare_record(first_field) == 0)
 
15467
      if (compare_record(table, first_field) == 0)
5084
15468
      {
5085
 
        if ((error=cursor->deleteRecord(record)))
5086
 
          goto err;
 
15469
        if ((error=file->ha_delete_row(record)))
 
15470
          goto err;
5087
15471
      }
5088
15472
      else if (!found)
5089
15473
      {
5090
 
        found= 1;
5091
 
        cursor->position(record);       // Remember position
 
15474
        found=1;
 
15475
        file->position(record); // Remember position
5092
15476
      }
5093
15477
    }
5094
15478
    if (!found)
5095
 
      break;                                    // End of cursor
5096
 
    /* Move current position to the next row */
5097
 
    error= cursor->rnd_pos(record, cursor->ref);
 
15479
      break;                                    // End of file
 
15480
    /* Restart search on next row */
 
15481
    error=file->restart_rnd_next(record,file->ref);
5098
15482
  }
5099
15483
 
5100
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
15484
  file->extra(HA_EXTRA_NO_CACHE);
5101
15485
  return(0);
5102
15486
err:
5103
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
15487
  file->extra(HA_EXTRA_NO_CACHE);
5104
15488
  if (error)
5105
 
    table->print_error(error,MYF(0));
 
15489
    file->print_error(error,MYF(0));
5106
15490
  return(1);
5107
15491
}
5108
15492
 
 
15493
 
5109
15494
/**
5110
15495
  Generate a hash index for each row to quickly find duplicate rows.
5111
15496
 
5112
15497
  @note
5113
15498
    Note that this will not work on tables with blobs!
5114
15499
*/
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)
 
15500
 
 
15501
static int remove_dup_with_hash_index(THD *thd, TABLE *table,
 
15502
                                      uint field_count,
 
15503
                                      Field **first_field,
 
15504
                                      ulong key_length,
 
15505
                                      Item *having)
5121
15506
{
5122
 
  unsigned char *key_pos, *record=table->getInsertRecord();
 
15507
  uchar *key_buffer, *key_pos, *record=table->record[0];
5123
15508
  int error;
5124
 
  Cursor *cursor= table->cursor;
5125
 
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5126
 
  uint32_t *field_length;
 
15509
  handler *file= table->file;
 
15510
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
 
15511
  uint *field_lengths,*field_length;
5127
15512
  HASH hash;
5128
 
  std::vector<unsigned char> key_buffer;
5129
 
  std::vector<uint32_t> field_lengths;
5130
15513
 
5131
 
  key_buffer.resize((key_length + extra_length) * (long) cursor->stats.records);
5132
 
  field_lengths.resize(field_count);
 
15514
  if (!my_multi_malloc(MYF(MY_WME),
 
15515
                       &key_buffer,
 
15516
                       (uint) ((key_length + extra_length) *
 
15517
                               (long) file->stats.records),
 
15518
                       &field_lengths,
 
15519
                       (uint) (field_count*sizeof(*field_lengths)),
 
15520
                       NullS))
 
15521
    return(1);
5133
15522
 
5134
15523
  {
5135
15524
    Field **ptr;
5136
 
    uint32_t total_length= 0;
5137
 
 
5138
 
    for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
 
15525
    ulong total_length= 0;
 
15526
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
5139
15527
    {
5140
 
      uint32_t length= (*ptr)->sort_length();
 
15528
      uint length= (*ptr)->sort_length();
5141
15529
      (*field_length++)= length;
5142
15530
      total_length+= length;
5143
15531
    }
5146
15534
    extra_length= ALIGN_SIZE(key_length)-key_length;
5147
15535
  }
5148
15536
 
5149
 
  if (hash_init(&hash, &my_charset_bin, (uint32_t) cursor->stats.records, 0,
 
15537
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, 
5150
15538
                key_length, (hash_get_key) 0, 0, 0))
5151
15539
  {
 
15540
    my_free((char*) key_buffer,MYF(0));
5152
15541
    return(1);
5153
15542
  }
5154
15543
 
5155
 
  if ((error= cursor->startTableScan(1)))
5156
 
    goto err;
5157
 
 
5158
 
  key_pos= &key_buffer[0];
 
15544
  file->ha_rnd_init(1);
 
15545
  key_pos=key_buffer;
5159
15546
  for (;;)
5160
15547
  {
5161
 
    unsigned char *org_key_pos;
5162
 
    if (session->getKilled())
 
15548
    uchar *org_key_pos;
 
15549
    if (thd->killed)
5163
15550
    {
5164
 
      session->send_kill_message();
 
15551
      thd->send_kill_message();
5165
15552
      error=0;
5166
15553
      goto err;
5167
15554
    }
5168
 
    if ((error=cursor->rnd_next(record)))
 
15555
    if ((error=file->rnd_next(record)))
5169
15556
    {
5170
15557
      if (error == HA_ERR_RECORD_DELETED)
5171
 
        continue;
 
15558
        continue;
5172
15559
      if (error == HA_ERR_END_OF_FILE)
5173
 
        break;
 
15560
        break;
5174
15561
      goto err;
5175
15562
    }
5176
15563
    if (having && !having->val_int())
5177
15564
    {
5178
 
      if ((error=cursor->deleteRecord(record)))
5179
 
        goto err;
 
15565
      if ((error=file->ha_delete_row(record)))
 
15566
        goto err;
5180
15567
      continue;
5181
15568
    }
5182
15569
 
5183
15570
    /* copy fields to key buffer */
5184
15571
    org_key_pos= key_pos;
5185
 
    field_length= &field_lengths[0];
 
15572
    field_length=field_lengths;
5186
15573
    for (Field **ptr= first_field ; *ptr ; ptr++)
5187
15574
    {
5188
15575
      (*ptr)->sort_string(key_pos,*field_length);
5192
15579
    if (hash_search(&hash, org_key_pos, key_length))
5193
15580
    {
5194
15581
      /* Duplicated found ; Remove the row */
5195
 
      if ((error=cursor->deleteRecord(record)))
5196
 
        goto err;
 
15582
      if ((error=file->ha_delete_row(record)))
 
15583
        goto err;
5197
15584
    }
5198
15585
    else
5199
15586
      (void) my_hash_insert(&hash, org_key_pos);
5200
15587
    key_pos+=extra_length;
5201
15588
  }
 
15589
  my_free((char*) key_buffer,MYF(0));
5202
15590
  hash_free(&hash);
5203
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5204
 
  (void) cursor->endTableScan();
 
15591
  file->extra(HA_EXTRA_NO_CACHE);
 
15592
  (void) file->ha_rnd_end();
5205
15593
  return(0);
5206
15594
 
5207
15595
err:
 
15596
  my_free((char*) key_buffer,MYF(0));
5208
15597
  hash_free(&hash);
5209
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5210
 
  (void) cursor->endTableScan();
 
15598
  file->extra(HA_EXTRA_NO_CACHE);
 
15599
  (void) file->ha_rnd_end();
5211
15600
  if (error)
5212
 
    table->print_error(error,MYF(0));
 
15601
    file->print_error(error,MYF(0));
5213
15602
  return(1);
5214
15603
}
5215
15604
 
5216
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
 
15605
 
 
15606
SORT_FIELD *make_unireg_sortorder(ORDER *order, uint *length,
 
15607
                                  SORT_FIELD *sortorder)
5217
15608
{
5218
 
  uint32_t count;
5219
 
  SortField *sort,*pos;
 
15609
  uint count;
 
15610
  SORT_FIELD *sort,*pos;
5220
15611
 
5221
15612
  count=0;
5222
 
  for (Order *tmp = order; tmp; tmp=tmp->next)
 
15613
  for (ORDER *tmp = order; tmp; tmp=tmp->next)
5223
15614
    count++;
5224
15615
  if (!sortorder)
5225
 
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
 
15616
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
5226
15617
                                       (max(count, *length) + 1));
5227
15618
  pos= sort= sortorder;
5228
15619
 
5249
15640
  return(sort);
5250
15641
}
5251
15642
 
 
15643
 
 
15644
/*****************************************************************************
 
15645
  Fill join cache with packed records
 
15646
  Records are stored in tab->cache.buffer and last record in
 
15647
  last record is stored with pointers to blobs to support very big
 
15648
  records
 
15649
******************************************************************************/
 
15650
 
 
15651
static int
 
15652
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count)
 
15653
{
 
15654
  register unsigned int i;
 
15655
  unsigned int length, blobs;
 
15656
  size_t size;
 
15657
  CACHE_FIELD *copy,**blob_ptr;
 
15658
  JOIN_CACHE  *cache;
 
15659
  JOIN_TAB *join_tab;
 
15660
 
 
15661
  cache= &tables[table_count].cache;
 
15662
  cache->fields=blobs=0;
 
15663
 
 
15664
  join_tab=tables;
 
15665
  for (i=0 ; i < table_count ; i++,join_tab++)
 
15666
  {
 
15667
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
15668
      calc_used_field_length(thd, join_tab);
 
15669
    cache->fields+=join_tab->used_fields;
 
15670
    blobs+=join_tab->used_blobs;
 
15671
 
 
15672
    /* SemiJoinDuplicateElimination: reserve space for rowid */
 
15673
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
15674
    {
 
15675
      cache->fields++;
 
15676
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
 
15677
    }
 
15678
  }
 
15679
  if (!(cache->field=(CACHE_FIELD*)
 
15680
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
 
15681
 
 
15682
                  sizeof(CACHE_FIELD*))))
 
15683
  {
 
15684
    my_free((uchar*) cache->buff,MYF(0));               /* purecov: inspected */
 
15685
    cache->buff=0;                              /* purecov: inspected */
 
15686
    return(1);                          /* purecov: inspected */
 
15687
  }
 
15688
  copy=cache->field;
 
15689
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
 
15690
    (cache->field+cache->fields+table_count*2);
 
15691
 
 
15692
  length=0;
 
15693
  for (i=0 ; i < table_count ; i++)
 
15694
  {
 
15695
    uint null_fields=0,used_fields;
 
15696
    Field **f_ptr,*field;
 
15697
    MY_BITMAP *read_set= tables[i].table->read_set;
 
15698
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
 
15699
         used_fields ;
 
15700
         f_ptr++)
 
15701
    {
 
15702
      field= *f_ptr;
 
15703
      if (bitmap_is_set(read_set, field->field_index))
 
15704
      {
 
15705
        used_fields--;
 
15706
        length+=field->fill_cache_field(copy);
 
15707
        if (copy->blob_field)
 
15708
          (*blob_ptr++)=copy;
 
15709
        if (field->maybe_null())
 
15710
          null_fields++;
 
15711
        copy->get_rowid= NULL;
 
15712
        copy++;
 
15713
      }
 
15714
    }
 
15715
    /* Copy null bits from table */
 
15716
    if (null_fields && tables[i].table->s->null_fields)
 
15717
    {                                           /* must copy null bits */
 
15718
      copy->str= tables[i].table->null_flags;
 
15719
      copy->length= tables[i].table->s->null_bytes;
 
15720
      copy->strip=0;
 
15721
      copy->blob_field=0;
 
15722
      copy->get_rowid= NULL;
 
15723
      length+=copy->length;
 
15724
      copy++;
 
15725
      cache->fields++;
 
15726
    }
 
15727
    /* If outer join table, copy null_row flag */
 
15728
    if (tables[i].table->maybe_null)
 
15729
    {
 
15730
      copy->str= (uchar*) &tables[i].table->null_row;
 
15731
      copy->length=sizeof(tables[i].table->null_row);
 
15732
      copy->strip=0;
 
15733
      copy->blob_field=0;
 
15734
      copy->get_rowid= NULL;
 
15735
      length+=copy->length;
 
15736
      copy++;
 
15737
      cache->fields++;
 
15738
    }
 
15739
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
 
15740
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
15741
    {
 
15742
      copy->str= tables[i].table->file->ref;
 
15743
      copy->length= tables[i].table->file->ref_length;
 
15744
      copy->strip=0;
 
15745
      copy->blob_field=0;
 
15746
      copy->get_rowid= NULL;
 
15747
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
15748
      {
 
15749
        /* We will need to call h->position(): */
 
15750
        copy->get_rowid= tables[i].table;
 
15751
        /* And those after us won't have to: */
 
15752
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
 
15753
      }
 
15754
      copy++;
 
15755
    }
 
15756
  }
 
15757
 
 
15758
  cache->length=length+blobs*sizeof(char*);
 
15759
  cache->blobs=blobs;
 
15760
  *blob_ptr=0;                                  /* End sequentel */
 
15761
  size=max(thd->variables.join_buff_size, cache->length);
 
15762
  if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
 
15763
    return(1);                          /* Don't use cache */ /* purecov: inspected */
 
15764
  cache->end=cache->buff+size;
 
15765
  reset_cache_write(cache);
 
15766
  return(0);
 
15767
}
 
15768
 
 
15769
 
 
15770
static ulong
 
15771
used_blob_length(CACHE_FIELD **ptr)
 
15772
{
 
15773
  uint length,blob_length;
 
15774
  for (length=0 ; *ptr ; ptr++)
 
15775
  {
 
15776
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
 
15777
    length+=blob_length;
 
15778
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
 
15779
  }
 
15780
  return length;
 
15781
}
 
15782
 
 
15783
 
 
15784
static bool
 
15785
store_record_in_cache(JOIN_CACHE *cache)
 
15786
{
 
15787
  uint length;
 
15788
  uchar *pos;
 
15789
  CACHE_FIELD *copy,*end_field;
 
15790
  bool last_record;
 
15791
 
 
15792
  pos=cache->pos;
 
15793
  end_field=cache->field+cache->fields;
 
15794
 
 
15795
  length=cache->length;
 
15796
  if (cache->blobs)
 
15797
    length+=used_blob_length(cache->blob_ptr);
 
15798
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
15799
    cache->ptr_record=cache->records;
 
15800
  /*
 
15801
    There is room in cache. Put record there
 
15802
  */
 
15803
  cache->records++;
 
15804
  for (copy=cache->field ; copy < end_field; copy++)
 
15805
  {
 
15806
    if (copy->blob_field)
 
15807
    {
 
15808
      if (last_record)
 
15809
      {
 
15810
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), 
 
15811
                                    copy->blob_field->charset());
 
15812
        pos+=copy->length+sizeof(char*);
 
15813
      }
 
15814
      else
 
15815
      {
 
15816
        copy->blob_field->get_image(pos, copy->length, // blob length
 
15817
                                    copy->blob_field->charset());
 
15818
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
15819
        pos+=copy->length+copy->blob_length;
 
15820
      }
 
15821
    }
 
15822
    else
 
15823
    {
 
15824
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
 
15825
      if (copy->get_rowid)
 
15826
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
 
15827
 
 
15828
      if (copy->strip)
 
15829
      {
 
15830
        uchar *str,*end;
 
15831
        for (str=copy->str,end= str+copy->length;
 
15832
             end > str && end[-1] == ' ' ;
 
15833
             end--) ;
 
15834
        length=(uint) (end-str);
 
15835
        memcpy(pos+2, str, length);
 
15836
        int2store(pos, length);
 
15837
        pos+= length+2;
 
15838
      }
 
15839
      else
 
15840
      {
 
15841
        memcpy(pos,copy->str,copy->length);
 
15842
        pos+=copy->length;
 
15843
      }
 
15844
    }
 
15845
  }
 
15846
  cache->pos=pos;
 
15847
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
15848
}
 
15849
 
 
15850
 
 
15851
static void
 
15852
reset_cache_read(JOIN_CACHE *cache)
 
15853
{
 
15854
  cache->record_nr=0;
 
15855
  cache->pos=cache->buff;
 
15856
}
 
15857
 
 
15858
 
 
15859
static void reset_cache_write(JOIN_CACHE *cache)
 
15860
{
 
15861
  reset_cache_read(cache);
 
15862
  cache->records= 0;
 
15863
  cache->ptr_record= (uint) ~0;
 
15864
}
 
15865
 
 
15866
 
 
15867
static void
 
15868
read_cached_record(JOIN_TAB *tab)
 
15869
{
 
15870
  uchar *pos;
 
15871
  uint length;
 
15872
  bool last_record;
 
15873
  CACHE_FIELD *copy,*end_field;
 
15874
 
 
15875
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
 
15876
  pos=tab->cache.pos;
 
15877
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
 
15878
       copy < end_field;
 
15879
       copy++)
 
15880
  {
 
15881
    if (copy->blob_field)
 
15882
    {
 
15883
      if (last_record)
 
15884
      {
 
15885
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
 
15886
                                    copy->blob_field->charset());
 
15887
        pos+=copy->length+sizeof(char*);
 
15888
      }
 
15889
      else
 
15890
      {
 
15891
        copy->blob_field->set_ptr(pos, pos+copy->length);
 
15892
        pos+=copy->length+copy->blob_field->get_length();
 
15893
      }
 
15894
    }
 
15895
    else
 
15896
    {
 
15897
      if (copy->strip)
 
15898
      {
 
15899
        length= uint2korr(pos);
 
15900
        memcpy(copy->str, pos+2, length);
 
15901
        memset(copy->str+length, ' ', copy->length-length);
 
15902
        pos+= 2 + length;
 
15903
      }
 
15904
      else
 
15905
      {
 
15906
        memcpy(copy->str,pos,copy->length);
 
15907
        pos+=copy->length;
 
15908
      }
 
15909
    }
 
15910
  }
 
15911
  tab->cache.pos=pos;
 
15912
  return;
 
15913
}
 
15914
 
 
15915
 
5252
15916
/*
5253
15917
  eq_ref: Create the lookup key and check if it is the same as saved key
5254
15918
 
5255
15919
  SYNOPSIS
5256
15920
    cmp_buffer_with_ref()
5257
15921
      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
 
15922
 
 
15923
  DESCRIPTION 
 
15924
    Used by eq_ref access method: create the index lookup key and check if 
5261
15925
    we've used this key at previous lookup (If yes, we don't need to repeat
5262
15926
    the lookup - the record has been already fetched)
5263
15927
 
5264
 
  RETURN
 
15928
  RETURN 
5265
15929
    true   No cached record for the key, or failed to create the key (due to
5266
15930
           out-of-domain error)
5267
 
    false  The created key is the same as the previous one (and the record
 
15931
    false  The created key is the same as the previous one (and the record 
5268
15932
           is already in table->record)
5269
15933
*/
5270
 
static bool cmp_buffer_with_ref(JoinTable *tab)
 
15934
 
 
15935
static bool
 
15936
cmp_buffer_with_ref(JOIN_TAB *tab)
5271
15937
{
5272
15938
  bool no_prev_key;
5273
15939
  if (!tab->ref.disable_cache)
5278
15944
      memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
5279
15945
    }
5280
15946
  }
5281
 
  else
 
15947
  else 
5282
15948
    no_prev_key= true;
5283
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) ||
 
15949
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, tab->table,
 
15950
                                            &tab->ref)) ||
5284
15951
      no_prev_key)
5285
15952
    return 1;
5286
15953
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
5287
15954
    != 0;
5288
15955
}
5289
15956
 
5290
 
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
 
15957
 
 
15958
bool
 
15959
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
5291
15960
{
5292
 
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
5293
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
15961
  enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
 
15962
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
15963
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
5294
15964
  bool result= 0;
5295
15965
 
5296
 
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
 
15966
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
5297
15967
  {
5298
15968
    if ((*copy)->copy() & 1)
5299
15969
    {
5301
15971
      break;
5302
15972
    }
5303
15973
  }
5304
 
  session->count_cuted_fields= save_count_cuted_fields;
 
15974
  thd->count_cuted_fields= save_count_cuted_fields;
 
15975
  dbug_tmp_restore_column_map(table->write_set, old_map);
5305
15976
  return result;
5306
15977
}
5307
15978
 
 
15979
 
5308
15980
/*****************************************************************************
5309
15981
  Group and order functions
5310
15982
*****************************************************************************/
5312
15984
/**
5313
15985
  Resolve an ORDER BY or GROUP BY column reference.
5314
15986
 
5315
 
  Given a column reference (represented by 'order') from a GROUP BY or order_st
 
15987
  Given a column reference (represented by 'order') from a GROUP BY or ORDER
5316
15988
  BY clause, find the actual column it represents. If the column being
5317
15989
  resolved is from the GROUP BY clause, the procedure searches the SELECT
5318
15990
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
5325
15997
 
5326
15998
  ref_pointer_array and all_fields are updated.
5327
15999
 
5328
 
  @param[in] session                 Pointer to current thread structure
 
16000
  @param[in] thd                     Pointer to current thread structure
5329
16001
  @param[in,out] ref_pointer_array  All select, group and order by fields
5330
16002
  @param[in] tables                 List of tables to search in (usually
5331
16003
    FROM clause)
5334
16006
    SELECT list)
5335
16007
  @param[in,out] all_fields         All select, group and order by fields
5336
16008
  @param[in] is_group_field         True if order is a GROUP field, false if
5337
 
    order_st by field
 
16009
    ORDER by field
5338
16010
 
5339
16011
  @retval
5340
16012
    false if OK
5341
16013
  @retval
5342
16014
    true  if error occurred
5343
16015
*/
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)
 
16016
 
 
16017
static bool
 
16018
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16019
                   ORDER *order, List<Item> &fields, List<Item> &all_fields,
 
16020
                   bool is_group_field)
5351
16021
{
5352
 
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
 
16022
  Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */
5353
16023
  Item::Type order_item_type;
5354
16024
  Item **select_item; /* The corresponding item from the SELECT clause. */
5355
16025
  Field *from_field;  /* The corresponding field from the FROM clause. */
5356
 
  uint32_t counter;
 
16026
  uint counter;
5357
16027
  enum_resolution_type resolution;
5358
16028
 
5359
16029
  /*
5362
16032
  */
5363
16033
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
5364
16034
  {                                             /* Order by position */
5365
 
    uint32_t count= (uint32_t) order_item->val_int();
 
16035
    uint count= (uint) order_item->val_int();
5366
16036
    if (!count || count > fields.elements)
5367
16037
    {
5368
16038
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
5369
 
               order_item->full_name(), session->where());
 
16039
               order_item->full_name(), thd->where);
5370
16040
      return true;
5371
16041
    }
5372
16042
    order->item= ref_pointer_array + count - 1;
5375
16045
    order->counter_used= 1;
5376
16046
    return false;
5377
16047
  }
5378
 
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5379
 
  select_item= find_item_in_list(session, order_item, fields, &counter,
 
16048
  /* Lookup the current GROUP/ORDER field in the SELECT clause. */
 
16049
  select_item= find_item_in_list(order_item, fields, &counter,
5380
16050
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
5381
16051
  if (!select_item)
5382
16052
    return true; /* The item is not unique, or some other error occured. */
5392
16062
      for this name (in case if we would perform lookup in all tables).
5393
16063
    */
5394
16064
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
5395
 
        order_item->fix_fields(session, order->item))
 
16065
        order_item->fix_fields(thd, order->item))
5396
16066
      return true;
5397
16067
 
5398
16068
    /* Lookup the current GROUP field in the FROM clause. */
5401
16071
    if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
5402
16072
        order_item_type == Item::REF_ITEM)
5403
16073
    {
5404
 
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
5405
 
                                       NULL, &view_ref, IGNORE_ERRORS, false);
 
16074
      from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables,
 
16075
                                       NULL, &view_ref, IGNORE_ERRORS, true,
 
16076
                                       false);
5406
16077
      if (!from_field)
5407
16078
        from_field= (Field*) not_found_field;
5408
16079
    }
5440
16111
        warning so the user knows that the field from the FROM clause
5441
16112
        overshadows the column reference from the SELECT list.
5442
16113
      */
5443
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
16114
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5444
16115
                          ER(ER_NON_UNIQ_ERROR),
5445
16116
                          ((Item_ident*) order_item)->field_name,
5446
 
                          session->where());
 
16117
                          current_thd->where);
5447
16118
    }
5448
16119
  }
5449
16120
 
5460
16131
    arguments for which fix_fields already was called.
5461
16132
  */
5462
16133
  if (!order_item->fixed &&
5463
 
      (order_item->fix_fields(session, order->item) ||
 
16134
      (order_item->fix_fields(thd, order->item) ||
5464
16135
       (order_item= *order->item)->check_cols(1) ||
5465
 
       session->is_fatal_error))
 
16136
       thd->is_fatal_error))
5466
16137
    return true; /* Wrong field. */
5467
16138
 
5468
 
  uint32_t el= all_fields.elements;
 
16139
  uint el= all_fields.elements;
5469
16140
  all_fields.push_front(order_item); /* Add new field to field list. */
5470
16141
  ref_pointer_array[el]= order_item;
5471
16142
  order->item= ref_pointer_array + el;
5472
16143
  return false;
5473
16144
}
5474
16145
 
 
16146
 
5475
16147
/**
5476
16148
  Change order to point at item in select list.
5477
16149
 
5478
16150
  If item isn't a number and doesn't exits in the select list, add it the
5479
16151
  the field list.
5480
16152
*/
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)
 
16153
 
 
16154
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16155
                List<Item> &fields, List<Item> &all_fields, ORDER *order)
5487
16156
{
5488
 
  session->setWhere("order clause");
 
16157
  thd->where="order clause";
5489
16158
  for (; order; order=order->next)
5490
16159
  {
5491
 
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
 
16160
    if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
5492
16161
                           all_fields, false))
5493
16162
      return 1;
5494
16163
  }
5495
16164
  return 0;
5496
16165
}
5497
16166
 
 
16167
 
5498
16168
/**
5499
16169
  Intitialize the GROUP BY list.
5500
16170
 
5501
 
  @param session                        Thread Cursor
 
16171
  @param thd                    Thread handler
5502
16172
  @param ref_pointer_array      We store references to all fields that was
5503
16173
                               not in 'fields' here.
5504
16174
  @param fields         All fields in the select part. Any item in
5520
16190
  @retval
5521
16191
    1  error (probably out of memory)
5522
16192
*/
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)
 
16193
 
 
16194
int
 
16195
setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16196
            List<Item> &fields, List<Item> &all_fields, ORDER *order,
 
16197
            bool *hidden_group_fields)
5530
16198
{
5531
16199
  *hidden_group_fields=0;
5532
 
  Order *ord;
 
16200
  ORDER *ord;
5533
16201
 
5534
16202
  if (!order)
5535
16203
    return 0;                           /* Everything is ok */
5536
16204
 
5537
 
  uint32_t org_fields=all_fields.elements;
 
16205
  uint org_fields=all_fields.elements;
5538
16206
 
5539
 
  session->setWhere("group statement");
 
16207
  thd->where="group statement";
5540
16208
  for (ord= order; ord; ord= ord->next)
5541
16209
  {
5542
 
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
 
16210
    if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields,
5543
16211
                           all_fields, true))
5544
16212
      return 1;
5545
16213
    (*ord->item)->marker= UNDEF_POS;            /* Mark found */
5569
16237
    Item *item;
5570
16238
    Item_field *field;
5571
16239
    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());
 
16240
    List_iterator<Item> li(fields);
 
16241
    List_iterator<Item_field> naf_it(thd->lex->current_select->non_agg_fields);
5574
16242
 
5575
16243
    field= naf_it++;
5576
16244
    while (field && (item=li++))
5596
16264
            if ((*ord->item)->eq((Item*)field, 0))
5597
16265
              goto next_field;
5598
16266
          /*
5599
 
            @todo change ER_WRONG_FIELD_WITH_GROUP to more detailed ER_NON_GROUPING_FIELD_USED
 
16267
            TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed
 
16268
            ER_NON_GROUPING_FIELD_USED
5600
16269
          */
5601
16270
          my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
5602
16271
          return 1;
5618
16287
  Try to use the fields in the order given by 'order' to allow one to
5619
16288
  optimize away 'order by'.
5620
16289
*/
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)
 
16290
 
 
16291
static ORDER *
 
16292
create_distinct_group(THD *thd, Item **ref_pointer_array,
 
16293
                      ORDER *order_list, List<Item> &fields,
 
16294
                      List<Item> &all_fields __attribute__((unused)),
 
16295
                      bool *all_order_by_fields_used)
5627
16296
{
5628
 
  List<Item>::iterator li(fields.begin());
 
16297
  List_iterator<Item> li(fields);
5629
16298
  Item *item;
5630
 
  Order *order,*group,**prev;
 
16299
  ORDER *order,*group,**prev;
5631
16300
 
5632
16301
  *all_order_by_fields_used= 1;
5633
16302
  while ((item=li++))
5638
16307
  {
5639
16308
    if (order->in_field_list)
5640
16309
    {
5641
 
      Order *ord=(Order*) session->getMemRoot()->duplicate((char*) order,sizeof(Order));
 
16310
      ORDER *ord=(ORDER*) thd->memdup((char*) order,sizeof(ORDER));
5642
16311
      if (!ord)
5643
 
        return 0;
 
16312
        return 0;
5644
16313
      *prev=ord;
5645
16314
      prev= &ord->next;
5646
16315
      (*ord->item)->marker=1;
5649
16318
      *all_order_by_fields_used= 0;
5650
16319
  }
5651
16320
 
5652
 
  li= fields.begin();
 
16321
  li.rewind();
5653
16322
  while ((item=li++))
5654
16323
  {
5655
16324
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5656
16325
    {
5657
 
      /*
5658
 
        Don't put duplicate columns from the SELECT list into the
 
16326
      /* 
 
16327
        Don't put duplicate columns from the SELECT list into the 
5659
16328
        GROUP BY list.
5660
16329
      */
5661
 
      Order *ord_iter;
 
16330
      ORDER *ord_iter;
5662
16331
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5663
16332
        if ((*ord_iter->item)->eq(item, 1))
5664
16333
          goto next_item;
5665
 
 
5666
 
      Order *ord=(Order*) session->calloc(sizeof(Order));
 
16334
      
 
16335
      ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER));
5667
16336
      if (!ord)
5668
 
        return 0;
 
16337
        return 0;
5669
16338
 
5670
16339
      /*
5671
16340
        We have here only field_list (not all_field_list), so we can use
5684
16353
  return group;
5685
16354
}
5686
16355
 
 
16356
 
5687
16357
/**
5688
16358
  Update join with count of the different type of fields.
5689
16359
*/
5690
 
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
 
16360
 
 
16361
void
 
16362
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, 
 
16363
                  List<Item> &fields, bool reset_with_sum_func)
5691
16364
{
5692
 
  List<Item>::iterator li(fields.begin());
 
16365
  List_iterator<Item> li(fields);
5693
16366
  Item *field;
5694
16367
 
5695
16368
  param->field_count=param->sum_func_count=param->func_count=
5704
16377
    {
5705
16378
      if (! field->const_item())
5706
16379
      {
5707
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
16380
        Item_sum *sum_item=(Item_sum*) field->real_item();
5708
16381
        if (!sum_item->depended_from() ||
5709
16382
            sum_item->depended_from() == select_lex)
5710
16383
        {
5712
16385
            param->quick_group=0;                       // UDF SUM function
5713
16386
          param->sum_func_count++;
5714
16387
 
5715
 
          for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
 
16388
          for (uint i=0 ; i < sum_item->arg_count ; i++)
5716
16389
          {
5717
16390
            if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
5718
16391
              param->field_count++;
5727
16400
    {
5728
16401
      param->func_count++;
5729
16402
      if (reset_with_sum_func)
5730
 
        field->with_sum_func=0;
5731
 
    }
5732
 
  }
5733
 
}
 
16403
        field->with_sum_func=0;
 
16404
    }
 
16405
  }
 
16406
}
 
16407
 
 
16408
 
 
16409
/**
 
16410
  Return 1 if second is a subpart of first argument.
 
16411
 
 
16412
  If first parts has different direction, change it to second part
 
16413
  (group is sorted like order)
 
16414
*/
 
16415
 
 
16416
static bool
 
16417
test_if_subpart(ORDER *a,ORDER *b)
 
16418
{
 
16419
  for (; a && b; a=a->next,b=b->next)
 
16420
  {
 
16421
    if ((*a->item)->eq(*b->item,1))
 
16422
      a->asc=b->asc;
 
16423
    else
 
16424
      return 0;
 
16425
  }
 
16426
  return test(!b);
 
16427
}
 
16428
 
 
16429
/**
 
16430
  Return table number if there is only one table in sort order
 
16431
  and group and order is compatible, else return 0.
 
16432
*/
 
16433
 
 
16434
static TABLE *
 
16435
get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables)
 
16436
{
 
16437
  table_map map= (table_map) 0;
 
16438
 
 
16439
  if (!a)
 
16440
    a=b;                                        // Only one need to be given
 
16441
  else if (!b)
 
16442
    b=a;
 
16443
 
 
16444
  for (; a && b; a=a->next,b=b->next)
 
16445
  {
 
16446
    if (!(*a->item)->eq(*b->item,1))
 
16447
      return(0);
 
16448
    map|=a->item[0]->used_tables();
 
16449
  }
 
16450
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
 
16451
    return(0);
 
16452
 
 
16453
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
 
16454
  if (map != tables->table->map)
 
16455
    return(0);                          // More than one table
 
16456
  return(tables->table);
 
16457
}
 
16458
 
 
16459
 
 
16460
/**
 
16461
  calc how big buffer we need for comparing group entries.
 
16462
*/
 
16463
 
 
16464
static void
 
16465
calc_group_buffer(JOIN *join,ORDER *group)
 
16466
{
 
16467
  uint key_length=0, parts=0, null_parts=0;
 
16468
 
 
16469
  if (group)
 
16470
    join->group= 1;
 
16471
  for (; group ; group=group->next)
 
16472
  {
 
16473
    Item *group_item= *group->item;
 
16474
    Field *field= group_item->get_tmp_table_field();
 
16475
    if (field)
 
16476
    {
 
16477
      enum_field_types type;
 
16478
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
 
16479
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
 
16480
      else if (type == DRIZZLE_TYPE_VARCHAR)
 
16481
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
 
16482
      else
 
16483
        key_length+= field->pack_length();
 
16484
    }
 
16485
    else
 
16486
    { 
 
16487
      switch (group_item->result_type()) {
 
16488
      case REAL_RESULT:
 
16489
        key_length+= sizeof(double);
 
16490
        break;
 
16491
      case INT_RESULT:
 
16492
        key_length+= sizeof(int64_t);
 
16493
        break;
 
16494
      case DECIMAL_RESULT:
 
16495
        key_length+= my_decimal_get_binary_size(group_item->max_length - 
 
16496
                                                (group_item->decimals ? 1 : 0),
 
16497
                                                group_item->decimals);
 
16498
        break;
 
16499
      case STRING_RESULT:
 
16500
      {
 
16501
        enum enum_field_types type= group_item->field_type();
 
16502
        /*
 
16503
          As items represented as DATE/TIME fields in the group buffer
 
16504
          have STRING_RESULT result type, we increase the length 
 
16505
          by 8 as maximum pack length of such fields.
 
16506
        */
 
16507
        if (type == DRIZZLE_TYPE_TIME ||
 
16508
            type == DRIZZLE_TYPE_NEWDATE ||
 
16509
            type == DRIZZLE_TYPE_DATETIME ||
 
16510
            type == DRIZZLE_TYPE_TIMESTAMP)
 
16511
        {
 
16512
          key_length+= 8;
 
16513
        }
 
16514
        else
 
16515
        {
 
16516
          /*
 
16517
            Group strings are taken as varstrings and require an length field.
 
16518
            A field is not yet created by create_tmp_field()
 
16519
            and the sizes should match up.
 
16520
          */
 
16521
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
 
16522
        }
 
16523
        break;
 
16524
      }
 
16525
      default:
 
16526
        /* This case should never be choosen */
 
16527
        assert(0);
 
16528
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
16529
      }
 
16530
    }
 
16531
    parts++;
 
16532
    if (group_item->maybe_null)
 
16533
      null_parts++;
 
16534
  }
 
16535
  join->tmp_table_param.group_length=key_length+null_parts;
 
16536
  join->tmp_table_param.group_parts=parts;
 
16537
  join->tmp_table_param.group_null_parts=null_parts;
 
16538
}
 
16539
 
 
16540
 
 
16541
/**
 
16542
  allocate group fields or take prepared (cached).
 
16543
 
 
16544
  @param main_join   join of current select
 
16545
  @param curr_join   current join (join of current select or temporary copy
 
16546
                     of it)
 
16547
 
 
16548
  @retval
 
16549
    0   ok
 
16550
  @retval
 
16551
    1   failed
 
16552
*/
 
16553
 
 
16554
static bool
 
16555
make_group_fields(JOIN *main_join, JOIN *curr_join)
 
16556
{
 
16557
  if (main_join->group_fields_cache.elements)
 
16558
  {
 
16559
    curr_join->group_fields= main_join->group_fields_cache;
 
16560
    curr_join->sort_and_group= 1;
 
16561
  }
 
16562
  else
 
16563
  {
 
16564
    if (alloc_group_fields(curr_join, curr_join->group_list))
 
16565
      return (1);
 
16566
    main_join->group_fields_cache= curr_join->group_fields;
 
16567
  }
 
16568
  return (0);
 
16569
}
 
16570
 
 
16571
 
 
16572
/**
 
16573
  Get a list of buffers for saveing last group.
 
16574
 
 
16575
  Groups are saved in reverse order for easyer check loop.
 
16576
*/
 
16577
 
 
16578
static bool
 
16579
alloc_group_fields(JOIN *join,ORDER *group)
 
16580
{
 
16581
  if (group)
 
16582
  {
 
16583
    for (; group ; group=group->next)
 
16584
    {
 
16585
      Cached_item *tmp=new_Cached_item(join->thd, *group->item, false);
 
16586
      if (!tmp || join->group_fields.push_front(tmp))
 
16587
        return true;
 
16588
    }
 
16589
  }
 
16590
  join->sort_and_group=1;                       /* Mark for do_select */
 
16591
  return false;
 
16592
}
 
16593
 
5734
16594
 
5735
16595
/*
5736
16596
  Test if a single-row cache of items changed, and update the cache.
5738
16598
  @details Test if a list of items that typically represents a result
5739
16599
  row has changed. If the value of some item changed, update the cached
5740
16600
  value for this item.
5741
 
 
 
16601
  
5742
16602
  @param list list of <item, cached_value> pairs stored as Cached_item.
5743
16603
 
5744
16604
  @return -1 if no item changed
5745
16605
  @return index of the first item that changed
5746
16606
*/
 
16607
 
5747
16608
int test_if_item_cache_changed(List<Cached_item> &list)
5748
16609
{
5749
 
  List<Cached_item>::iterator li(list.begin());
 
16610
  List_iterator<Cached_item> li(list);
5750
16611
  int idx= -1,i;
5751
16612
  Cached_item *buff;
5752
16613
 
5758
16619
  return(idx);
5759
16620
}
5760
16621
 
 
16622
 
5761
16623
/**
5762
16624
  Setup copy_fields to save fields at start of new group.
5763
16625
 
5767
16629
  Change old item_field to use a new field with points at saved fieldvalue
5768
16630
  This function is only called before use of send_fields.
5769
16631
 
5770
 
  @param session                   Session pointer
 
16632
  @param thd                   THD pointer
5771
16633
  @param param                 temporary table parameters
5772
16634
  @param ref_pointer_array     array of pointers to top elements of filed list
5773
16635
  @param res_selected_fields   new list of items of select item list
5786
16648
  @retval
5787
16649
    !=0   error
5788
16650
*/
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)
 
16651
 
 
16652
bool
 
16653
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
 
16654
                  Item **ref_pointer_array,
 
16655
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
 
16656
                  uint elements, List<Item> &all_fields)
5796
16657
{
5797
16658
  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());
 
16659
  List_iterator_fast<Item> li(all_fields);
 
16660
  Copy_field *copy= NULL;
 
16661
  res_selected_fields.empty();
 
16662
  res_all_fields.empty();
 
16663
  List_iterator_fast<Item> itr(res_all_fields);
5803
16664
  List<Item> extra_funcs;
5804
 
  uint32_t i, border= all_fields.elements - elements;
 
16665
  uint i, border= all_fields.elements - elements;
5805
16666
 
5806
 
  if (param->field_count &&
5807
 
      !(copy=param->copy_field= new CopyField[param->field_count]))
 
16667
  if (param->field_count && 
 
16668
      !(copy=param->copy_field= new Copy_field[param->field_count]))
5808
16669
    goto err2;
5809
16670
 
5810
 
  param->copy_funcs.clear();
 
16671
  param->copy_funcs.empty();
5811
16672
  for (i= 0; (pos= li++); i++)
5812
16673
  {
5813
16674
    Field *field;
5814
 
    unsigned char *tmp;
 
16675
    uchar *tmp;
5815
16676
    Item *real_pos= pos->real_item();
5816
16677
    if (real_pos->type() == Item::FIELD_ITEM)
5817
16678
    {
5818
16679
      Item_field *item;
5819
 
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
5820
 
        goto err;
 
16680
      if (!(item= new Item_field(thd, ((Item_field*) real_pos))))
 
16681
        goto err;
5821
16682
      if (pos->type() == Item::REF_ITEM)
5822
16683
      {
5823
16684
        /* preserve the names of the ref when dereferncing */
5829
16690
      pos= item;
5830
16691
      if (item->field->flags & BLOB_FLAG)
5831
16692
      {
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;
 
16693
        if (!(pos= new Item_copy_string(pos)))
 
16694
          goto err;
 
16695
       /*
 
16696
         Item_copy_string::copy for function can call 
 
16697
         Item_copy_string::val_int for blob via Item_ref.
 
16698
         But if Item_copy_string::copy for blob isn't called before,
 
16699
         it's value will be wrong
 
16700
         so let's insert Item_copy_string for blobs in the beginning of 
 
16701
         copy_funcs
 
16702
         (to see full test case look at having.test, BUG #4358) 
 
16703
       */
 
16704
        if (param->copy_funcs.push_front(pos))
 
16705
          goto err;
5845
16706
      }
5846
16707
      else
5847
16708
      {
 
16709
        /* 
 
16710
           set up save buffer and change result_field to point at 
 
16711
           saved value
 
16712
        */
 
16713
        field= item->field;
 
16714
        item->result_field=field->new_field(thd->mem_root,field->table, 1);
5848
16715
        /*
5849
 
          set up save buffer and change result_field to point at
5850
 
          saved value
 
16716
          We need to allocate one extra byte for null handling and
 
16717
          another extra byte to not get warnings from purify in
 
16718
          Field_varstring::val_int
5851
16719
        */
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;
 
16720
        if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
 
16721
          goto err;
5861
16722
        if (copy)
5862
16723
        {
5863
16724
          copy->set(tmp, item->result_field);
5864
16725
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
5865
 
#ifdef HAVE_VALGRIND
 
16726
#ifdef HAVE_purify
5866
16727
          copy->to_ptr[copy->from_length]= 0;
5867
16728
#endif
5868
16729
          copy++;
5876
16737
             !real_pos->with_sum_func)
5877
16738
    {                                           // Save for send fields
5878
16739
      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(*))
 
16740
      /* TODO:
 
16741
         In most cases this result will be sent to the user.
 
16742
         This should be changed to use copy_int or copy_real depending
 
16743
         on how the value is to be used: In some cases this may be an
 
16744
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
5884
16745
      */
5885
16746
      if (!(pos=new Item_copy_string(pos)))
5886
 
        goto err;
5887
 
      if (i < border)                           // HAVING, order_st and GROUP BY
 
16747
        goto err;
 
16748
      if (i < border)                           // HAVING, ORDER and GROUP BY
5888
16749
      {
5889
16750
        if (extra_funcs.push_back(pos))
5890
16751
          goto err;
5891
16752
      }
5892
16753
      else if (param->copy_funcs.push_back(pos))
5893
 
        goto err;
 
16754
        goto err;
5894
16755
    }
5895
16756
    res_all_fields.push_back(pos);
5896
16757
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
5909
16770
 
5910
16771
  return(0);
5911
16772
 
5912
 
err:
 
16773
 err:
5913
16774
  if (copy)
5914
16775
    delete [] param->copy_field;                        // This is never 0
5915
16776
  param->copy_field=0;
5917
16778
  return(true);
5918
16779
}
5919
16780
 
 
16781
 
5920
16782
/**
5921
16783
  Make a copy of all simple SELECT'ed items.
5922
16784
 
5923
16785
  This is done at the start of a new group so that we can retrieve
5924
16786
  these later when the group changes.
5925
16787
*/
5926
 
void copy_fields(Tmp_Table_Param *param)
 
16788
 
 
16789
void
 
16790
copy_fields(TMP_TABLE_PARAM *param)
5927
16791
{
5928
 
  CopyField *ptr= param->copy_field;
5929
 
  CopyField *end= param->copy_field_end;
 
16792
  Copy_field *ptr=param->copy_field;
 
16793
  Copy_field *end=param->copy_field_end;
5930
16794
 
5931
16795
  for (; ptr != end; ptr++)
5932
16796
    (*ptr->do_copy)(ptr);
5933
16797
 
5934
 
  List<Item>::iterator it(param->copy_funcs.begin());
 
16798
  List_iterator_fast<Item> it(param->copy_funcs);
5935
16799
  Item_copy_string *item;
5936
16800
  while ((item = (Item_copy_string*) it++))
5937
16801
    item->copy();
5938
16802
}
5939
16803
 
 
16804
 
 
16805
/**
 
16806
  Make an array of pointers to sum_functions to speed up
 
16807
  sum_func calculation.
 
16808
 
 
16809
  @retval
 
16810
    0   ok
 
16811
  @retval
 
16812
    1   Error
 
16813
*/
 
16814
 
 
16815
bool JOIN::alloc_func_list()
 
16816
{
 
16817
  uint func_count, group_parts;
 
16818
 
 
16819
  func_count= tmp_table_param.sum_func_count;
 
16820
  /*
 
16821
    If we are using rollup, we need a copy of the summary functions for
 
16822
    each level
 
16823
  */
 
16824
  if (rollup.state != ROLLUP::STATE_NONE)
 
16825
    func_count*= (send_group_parts+1);
 
16826
 
 
16827
  group_parts= send_group_parts;
 
16828
  /*
 
16829
    If distinct, reserve memory for possible
 
16830
    disctinct->group_by optimization
 
16831
  */
 
16832
  if (select_distinct)
 
16833
  {
 
16834
    group_parts+= fields_list.elements;
 
16835
    /*
 
16836
      If the ORDER clause is specified then it's possible that
 
16837
      it also will be optimized, so reserve space for it too
 
16838
    */
 
16839
    if (order)
 
16840
    {
 
16841
      ORDER *ord;
 
16842
      for (ord= order; ord; ord= ord->next)
 
16843
        group_parts++;
 
16844
    }
 
16845
  }
 
16846
 
 
16847
  /* This must use calloc() as rollup_make_fields depends on this */
 
16848
  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
 
16849
                                      sizeof(Item_sum***) * (group_parts+1));
 
16850
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
 
16851
  return(sum_funcs == 0);
 
16852
}
 
16853
 
 
16854
 
 
16855
/**
 
16856
  Initialize 'sum_funcs' array with all Item_sum objects.
 
16857
 
 
16858
  @param field_list        All items
 
16859
  @param send_fields       Items in select list
 
16860
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
 
16861
  @param recompute         Set to true if sum_funcs must be recomputed
 
16862
 
 
16863
  @retval
 
16864
    0  ok
 
16865
  @retval
 
16866
    1  error
 
16867
*/
 
16868
 
 
16869
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
 
16870
                              bool before_group_by, bool recompute)
 
16871
{
 
16872
  List_iterator_fast<Item> it(field_list);
 
16873
  Item_sum **func;
 
16874
  Item *item;
 
16875
 
 
16876
  if (*sum_funcs && !recompute)
 
16877
    return(false); /* We have already initialized sum_funcs. */
 
16878
 
 
16879
  func= sum_funcs;
 
16880
  while ((item=it++))
 
16881
  {
 
16882
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
16883
        (!((Item_sum*) item)->depended_from() ||
 
16884
         ((Item_sum *)item)->depended_from() == select_lex))
 
16885
      *func++= (Item_sum*) item;
 
16886
  }
 
16887
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
16888
  {
 
16889
    rollup.state= ROLLUP::STATE_READY;
 
16890
    if (rollup_make_fields(field_list, send_fields, &func))
 
16891
      return(true);                     // Should never happen
 
16892
  }
 
16893
  else if (rollup.state == ROLLUP::STATE_NONE)
 
16894
  {
 
16895
    for (uint i=0 ; i <= send_group_parts ;i++)
 
16896
      sum_funcs_end[i]= func;
 
16897
  }
 
16898
  else if (rollup.state == ROLLUP::STATE_READY)
 
16899
    return(false);                         // Don't put end marker
 
16900
  *func=0;                                      // End marker
 
16901
  return(false);
 
16902
}
 
16903
 
 
16904
 
5940
16905
/**
5941
16906
  Change all funcs and sum_funcs to fields in tmp table, and create
5942
16907
  new list of all items.
5943
16908
 
5944
 
  @param session                   Session pointer
 
16909
  @param thd                   THD pointer
5945
16910
  @param ref_pointer_array     array of pointers to top elements of filed list
5946
16911
  @param res_selected_fields   new list of items of select item list
5947
16912
  @param res_all_fields        new list of all items
5953
16918
  @retval
5954
16919
    !=0   error
5955
16920
*/
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)
 
16921
 
 
16922
static bool
 
16923
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
16924
                         List<Item> &res_selected_fields,
 
16925
                         List<Item> &res_all_fields,
 
16926
                         uint elements, List<Item> &all_fields)
5962
16927
{
5963
 
  List<Item>::iterator it(all_fields.begin());
 
16928
  List_iterator_fast<Item> it(all_fields);
5964
16929
  Item *item_field,*item;
5965
16930
 
5966
 
  res_selected_fields.clear();
5967
 
  res_all_fields.clear();
 
16931
  res_selected_fields.empty();
 
16932
  res_all_fields.empty();
5968
16933
 
5969
 
  uint32_t i, border= all_fields.elements - elements;
 
16934
  uint i, border= all_fields.elements - elements;
5970
16935
  for (i= 0; (item= it++); i++)
5971
16936
  {
5972
16937
    Field *field;
5979
16944
    {
5980
16945
      if (item->type() == Item::FIELD_ITEM)
5981
16946
      {
5982
 
        item_field= item->get_tmp_table_item(session);
 
16947
        item_field= item->get_tmp_table_item(thd);
5983
16948
      }
5984
16949
      else if ((field= item->get_tmp_table_field()))
5985
16950
      {
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
 
16951
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
16952
          item_field= ((Item_sum*) item)->result_item(field);
 
16953
        else
 
16954
          item_field= (Item*) new Item_field(field);
 
16955
        if (!item_field)
 
16956
          return(true);                    // Fatal error
5992
16957
 
5993
16958
        if (item->real_item()->type() != Item::FIELD_ITEM)
5994
16959
          field->orig_table= 0;
5995
 
        item_field->name= item->name;
 
16960
        item_field->name= item->name;
5996
16961
        if (item->type() == Item::REF_ITEM)
5997
16962
        {
5998
16963
          Item_field *ifield= (Item_field *) item_field;
6002
16967
        }
6003
16968
      }
6004
16969
      else
6005
 
        item_field= item;
 
16970
        item_field= item;
6006
16971
    }
6007
16972
    res_all_fields.push_back(item_field);
6008
16973
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6009
16974
      item_field;
6010
16975
  }
6011
16976
 
6012
 
  List<Item>::iterator itr(res_all_fields.begin());
 
16977
  List_iterator_fast<Item> itr(res_all_fields);
6013
16978
  for (i= 0; i < border; i++)
6014
16979
    itr++;
6015
16980
  itr.sublist(res_selected_fields, elements);
6016
16981
  return(false);
6017
16982
}
6018
16983
 
 
16984
 
6019
16985
/**
6020
16986
  Change all sum_func refs to fields to point at fields in tmp table.
6021
16987
  Change all funcs to be fields in tmp table.
6022
16988
 
6023
 
  @param session                   Session pointer
 
16989
  @param thd                   THD pointer
6024
16990
  @param ref_pointer_array     array of pointers to top elements of filed list
6025
16991
  @param res_selected_fields   new list of items of select item list
6026
16992
  @param res_all_fields        new list of all items
6032
16998
  @retval
6033
16999
    1   error
6034
17000
*/
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)
 
17001
 
 
17002
static bool
 
17003
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
17004
                          List<Item> &res_selected_fields,
 
17005
                          List<Item> &res_all_fields, uint elements,
 
17006
                          List<Item> &all_fields)
6041
17007
{
6042
 
  List<Item>::iterator it(all_fields.begin());
 
17008
  List_iterator_fast<Item> it(all_fields);
6043
17009
  Item *item, *new_item;
6044
 
  res_selected_fields.clear();
6045
 
  res_all_fields.clear();
 
17010
  res_selected_fields.empty();
 
17011
  res_all_fields.empty();
6046
17012
 
6047
 
  uint32_t i, border= all_fields.elements - elements;
 
17013
  uint i, border= all_fields.elements - elements;
6048
17014
  for (i= 0; (item= it++); i++)
6049
17015
  {
6050
 
    res_all_fields.push_back(new_item= item->get_tmp_table_item(session));
 
17016
    res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
6051
17017
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6052
17018
      new_item;
6053
17019
  }
6054
17020
 
6055
 
  List<Item>::iterator itr(res_all_fields.begin());
 
17021
  List_iterator_fast<Item> itr(res_all_fields);
6056
17022
  for (i= 0; i < border; i++)
6057
17023
    itr++;
6058
17024
  itr.sublist(res_selected_fields, elements);
6059
17025
 
6060
 
  return session->is_fatal_error;
 
17026
  return thd->is_fatal_error;
6061
17027
}
6062
17028
 
 
17029
 
 
17030
 
6063
17031
/******************************************************************************
6064
17032
  Code for calculating functions
6065
17033
******************************************************************************/
6066
17034
 
 
17035
 
6067
17036
/**
6068
17037
  Call ::setup for all sum functions.
6069
17038
 
6070
 
  @param session           thread Cursor
 
17039
  @param thd           thread handler
6071
17040
  @param func_ptr      sum function list
6072
17041
 
6073
17042
  @retval
6075
17044
  @retval
6076
17045
    true   error
6077
17046
*/
6078
 
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
 
17047
 
 
17048
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
6079
17049
{
6080
17050
  Item_sum *func;
6081
17051
  while ((func= *(func_ptr++)))
6082
17052
  {
6083
 
    if (func->setup(session))
 
17053
    if (func->setup(thd))
6084
17054
      return(true);
6085
17055
  }
6086
17056
  return(false);
6087
17057
}
6088
17058
 
6089
 
void init_tmptable_sum_functions(Item_sum **func_ptr)
 
17059
 
 
17060
static void
 
17061
init_tmptable_sum_functions(Item_sum **func_ptr)
6090
17062
{
6091
17063
  Item_sum *func;
6092
17064
  while ((func= *(func_ptr++)))
6093
17065
    func->reset_field();
6094
17066
}
6095
17067
 
 
17068
 
6096
17069
/** Update record 0 in tmp_table from record 1. */
6097
 
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
 
17070
 
 
17071
static void
 
17072
update_tmptable_sum_func(Item_sum **func_ptr,
 
17073
                         TABLE *tmp_table __attribute__((unused)))
6098
17074
{
6099
17075
  Item_sum *func;
6100
17076
  while ((func= *(func_ptr++)))
6101
17077
    func->update_field();
6102
17078
}
6103
17079
 
 
17080
 
6104
17081
/** Copy result of sum functions to record in tmp_table. */
6105
 
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
17082
 
 
17083
static void
 
17084
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
6106
17085
{
6107
17086
  for (; func_ptr != end_ptr ; func_ptr++)
6108
17087
    (void) (*func_ptr)->save_in_result_field(1);
6109
17088
  return;
6110
17089
}
6111
17090
 
6112
 
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
17091
 
 
17092
static bool
 
17093
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6113
17094
{
6114
17095
  for (; func_ptr != end_ptr ;func_ptr++)
6115
17096
  {
6125
17106
  return 0;
6126
17107
}
6127
17108
 
6128
 
bool update_sum_func(Item_sum **func_ptr)
 
17109
 
 
17110
static bool
 
17111
update_sum_func(Item_sum **func_ptr)
6129
17112
{
6130
17113
  Item_sum *func;
6131
17114
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6135
17118
}
6136
17119
 
6137
17120
/** Copy result of functions to record in tmp_table. */
6138
 
bool copy_funcs(Item **func_ptr, const Session *session)
 
17121
 
 
17122
void
 
17123
copy_funcs(Item **func_ptr)
6139
17124
{
6140
17125
  Item *func;
6141
17126
  for (; (func = *func_ptr) ; func_ptr++)
6142
 
  {
6143
17127
    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
 
}
 
17128
}
 
17129
 
 
17130
 
 
17131
/**
 
17132
  Create a condition for a const reference and add this to the
 
17133
  currenct select for the table.
 
17134
*/
 
17135
 
 
17136
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
 
17137
{
 
17138
  if (!join_tab->ref.key_parts)
 
17139
    return(false);
 
17140
 
 
17141
  Item_cond_and *cond=new Item_cond_and();
 
17142
  TABLE *table=join_tab->table;
 
17143
  int error;
 
17144
  if (!cond)
 
17145
    return(true);
 
17146
 
 
17147
  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
 
17148
  {
 
17149
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
 
17150
                              fieldnr-1];
 
17151
    Item *value=join_tab->ref.items[i];
 
17152
    cond->add(new Item_func_equal(new Item_field(field), value));
 
17153
  }
 
17154
  if (thd->is_fatal_error)
 
17155
    return(true);
 
17156
 
 
17157
  if (!cond->fixed)
 
17158
    cond->fix_fields(thd, (Item**)&cond);
 
17159
  if (join_tab->select)
 
17160
  {
 
17161
    error=(int) cond->add(join_tab->select->cond);
 
17162
    join_tab->select_cond=join_tab->select->cond=cond;
 
17163
  }
 
17164
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
 
17165
                                          &error)))
 
17166
    join_tab->select_cond=cond;
 
17167
 
 
17168
  return(error ? true : false);
 
17169
}
 
17170
 
6155
17171
 
6156
17172
/**
6157
17173
  Free joins of subselect of this select.
6158
17174
 
6159
 
  @param session      Session pointer
6160
 
  @param select   pointer to Select_Lex which subselects joins we will free
 
17175
  @param thd      THD pointer
 
17176
  @param select   pointer to st_select_lex which subselects joins we will free
6161
17177
*/
6162
 
void free_underlaid_joins(Session *, Select_Lex *select)
 
17178
 
 
17179
void free_underlaid_joins(THD *thd __attribute__((unused)),
 
17180
                          SELECT_LEX *select)
6163
17181
{
6164
 
  for (Select_Lex_Unit *unit= select->first_inner_unit();
 
17182
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
6165
17183
       unit;
6166
17184
       unit= unit->next_unit())
6167
17185
    unit->cleanup();
6183
17201
  @b EXAMPLES
6184
17202
    @code
6185
17203
      SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP
6186
 
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP
 
17204
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP 
6187
17205
  @endcode
6188
17206
 
6189
17207
  @b IMPLEMENTATION
6196
17214
    This substitution is needed GROUP BY queries with ROLLUP if
6197
17215
    SELECT list contains expressions over group by attributes.
6198
17216
 
6199
 
  @param session                  reference to the context
 
17217
  @param thd                  reference to the context
6200
17218
  @param expr                 expression to make replacement
6201
17219
  @param group_list           list of references to group by items
6202
17220
  @param changed        out:  returns 1 if item contains a replaced field item
6203
17221
 
6204
17222
  @todo
6205
 
    - @todo Some functions are not null-preserving. For those functions
6206
 
    updating of the maybe_null attribute is an overkill.
 
17223
    - TODO: Some functions are not null-preserving. For those functions
 
17224
    updating of the maybe_null attribute is an overkill. 
6207
17225
 
6208
17226
  @retval
6209
17227
    0   if ok
6210
17228
  @retval
6211
17229
    1   on error
6212
17230
*/
6213
 
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
 
17231
 
 
17232
static bool change_group_ref(THD *thd, Item_func *expr, ORDER *group_list,
 
17233
                             bool *changed)
6214
17234
{
6215
17235
  if (expr->arg_count)
6216
17236
  {
6217
 
    Name_resolution_context *context= &session->getLex()->current_select->context;
 
17237
    Name_resolution_context *context= &thd->lex->current_select->context;
6218
17238
    Item **arg,**arg_end;
6219
17239
    bool arg_changed= false;
6220
17240
    for (arg= expr->arguments(),
6224
17244
      Item *item= *arg;
6225
17245
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6226
17246
      {
6227
 
        Order *group_tmp;
 
17247
        ORDER *group_tmp;
6228
17248
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6229
17249
        {
6230
17250
          if (item->eq(*group_tmp->item,0))
6233
17253
            if (!(new_item= new Item_ref(context, group_tmp->item, 0,
6234
17254
                                        item->name)))
6235
17255
              return 1;                                 // fatal_error is set
6236
 
            session->change_item_tree(arg, new_item);
 
17256
            thd->change_item_tree(arg, new_item);
6237
17257
            arg_changed= true;
6238
17258
          }
6239
17259
        }
6240
17260
      }
6241
17261
      else if (item->type() == Item::FUNC_ITEM)
6242
17262
      {
6243
 
        if (change_group_ref(session, (Item_func *) item, group_list, &arg_changed))
 
17263
        if (change_group_ref(thd, (Item_func *) item, group_list, &arg_changed))
6244
17264
          return 1;
6245
17265
      }
6246
17266
    }
6254
17274
}
6255
17275
 
6256
17276
 
6257
 
static void print_table_array(Session *session, String *str, TableList **table,
6258
 
                              TableList **end)
6259
 
{
6260
 
  (*table)->print(session, str, QT_ORDINARY);
6261
 
 
6262
 
  for (TableList **tbl= table + 1; tbl < end; tbl++)
6263
 
  {
6264
 
    TableList *curr= *tbl;
 
17277
/** Allocate memory needed for other rollup functions. */
 
17278
 
 
17279
bool JOIN::rollup_init()
 
17280
{
 
17281
  uint i,j;
 
17282
  Item **ref_array;
 
17283
 
 
17284
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
 
17285
  rollup.state= ROLLUP::STATE_INITED;
 
17286
 
 
17287
  /*
 
17288
    Create pointers to the different sum function groups
 
17289
    These are updated by rollup_make_fields()
 
17290
  */
 
17291
  tmp_table_param.group_parts= send_group_parts;
 
17292
 
 
17293
  if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
 
17294
                                                sizeof(Item**) +
 
17295
                                                sizeof(List<Item>) +
 
17296
                                                ref_pointer_array_size)
 
17297
                                                * send_group_parts )))
 
17298
    return 1;
 
17299
  
 
17300
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
17301
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
17302
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
17303
 
 
17304
  /*
 
17305
    Prepare space for field list for the different levels
 
17306
    These will be filled up in rollup_make_fields()
 
17307
  */
 
17308
  for (i= 0 ; i < send_group_parts ; i++)
 
17309
  {
 
17310
    rollup.null_items[i]= new (thd->mem_root) Item_null_result();
 
17311
    List<Item> *rollup_fields= &rollup.fields[i];
 
17312
    rollup_fields->empty();
 
17313
    rollup.ref_pointer_arrays[i]= ref_array;
 
17314
    ref_array+= all_fields.elements;
 
17315
  }
 
17316
  for (i= 0 ; i < send_group_parts; i++)
 
17317
  {
 
17318
    for (j=0 ; j < fields_list.elements ; j++)
 
17319
      rollup.fields[i].push_back(rollup.null_items[i]);
 
17320
  }
 
17321
  List_iterator<Item> it(all_fields);
 
17322
  Item *item;
 
17323
  while ((item= it++))
 
17324
  {
 
17325
    ORDER *group_tmp;
 
17326
    bool found_in_group= 0;
 
17327
 
 
17328
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
 
17329
    {
 
17330
      if (*group_tmp->item == item)
 
17331
      {
 
17332
        item->maybe_null= 1;
 
17333
        found_in_group= 1;
 
17334
        if (item->const_item())
 
17335
        {
 
17336
          /*
 
17337
            For ROLLUP queries each constant item referenced in GROUP BY list
 
17338
            is wrapped up into an Item_func object yielding the same value
 
17339
            as the constant item. The objects of the wrapper class are never
 
17340
            considered as constant items and besides they inherit all
 
17341
            properties of the Item_result_field class.
 
17342
            This wrapping allows us to ensure writing constant items
 
17343
            into temporary tables whenever the result of the ROLLUP
 
17344
            operation has to be written into a temporary table, e.g. when
 
17345
            ROLLUP is used together with DISTINCT in the SELECT list.
 
17346
            Usually when creating temporary tables for a intermidiate
 
17347
            result we do not include fields for constant expressions.
 
17348
          */           
 
17349
          Item* new_item= new Item_func_rollup_const(item);
 
17350
          if (!new_item)
 
17351
            return 1;
 
17352
          new_item->fix_fields(thd, (Item **) 0);
 
17353
          thd->change_item_tree(it.ref(), new_item);
 
17354
          for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
 
17355
          { 
 
17356
            if (*tmp->item == item)
 
17357
              thd->change_item_tree(tmp->item, new_item);
 
17358
          }
 
17359
        }
 
17360
      }
 
17361
    }
 
17362
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
 
17363
    {
 
17364
      bool changed= false;
 
17365
      if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
 
17366
        return 1;
 
17367
      /*
 
17368
        We have to prevent creation of a field in a temporary table for
 
17369
        an expression that contains GROUP BY attributes.
 
17370
        Marking the expression item as 'with_sum_func' will ensure this.
 
17371
      */ 
 
17372
      if (changed)
 
17373
        item->with_sum_func= 1;
 
17374
    }
 
17375
  }
 
17376
  return 0;
 
17377
}
 
17378
  
 
17379
 
 
17380
/**
 
17381
  Fill up rollup structures with pointers to fields to use.
 
17382
 
 
17383
  Creates copies of item_sum items for each sum level.
 
17384
 
 
17385
  @param fields_arg             List of all fields (hidden and real ones)
 
17386
  @param sel_fields             Pointer to selected fields
 
17387
  @param func                   Store here a pointer to all fields
 
17388
 
 
17389
  @retval
 
17390
    0   if ok;
 
17391
    In this case func is pointing to next not used element.
 
17392
  @retval
 
17393
    1    on error
 
17394
*/
 
17395
 
 
17396
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
 
17397
                              Item_sum ***func)
 
17398
{
 
17399
  List_iterator_fast<Item> it(fields_arg);
 
17400
  Item *first_field= sel_fields.head();
 
17401
  uint level;
 
17402
 
 
17403
  /*
 
17404
    Create field lists for the different levels
 
17405
 
 
17406
    The idea here is to have a separate field list for each rollup level to
 
17407
    avoid all runtime checks of which columns should be NULL.
 
17408
 
 
17409
    The list is stored in reverse order to get sum function in such an order
 
17410
    in func that it makes it easy to reset them with init_sum_functions()
 
17411
 
 
17412
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
17413
 
 
17414
    rollup.fields[0] will contain list where a,b,c is NULL
 
17415
    rollup.fields[1] will contain list where b,c is NULL
 
17416
    ...
 
17417
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
 
17418
    ...
 
17419
    sum_funcs_end[0] points to all sum functions
 
17420
    sum_funcs_end[1] points to all sum functions, except grand totals
 
17421
    ...
 
17422
  */
 
17423
 
 
17424
  for (level=0 ; level < send_group_parts ; level++)
 
17425
  {
 
17426
    uint i;
 
17427
    uint pos= send_group_parts - level -1;
 
17428
    bool real_fields= 0;
 
17429
    Item *item;
 
17430
    List_iterator<Item> new_it(rollup.fields[pos]);
 
17431
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
17432
    ORDER *start_group;
 
17433
 
 
17434
    /* Point to first hidden field */
 
17435
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
17436
 
 
17437
    /* Remember where the sum functions ends for the previous level */
 
17438
    sum_funcs_end[pos+1]= *func;
 
17439
 
 
17440
    /* Find the start of the group for this level */
 
17441
    for (i= 0, start_group= group_list ;
 
17442
         i++ < pos ;
 
17443
         start_group= start_group->next)
 
17444
      ;
 
17445
 
 
17446
    it.rewind();
 
17447
    while ((item= it++))
 
17448
    {
 
17449
      if (item == first_field)
 
17450
      {
 
17451
        real_fields= 1;                         // End of hidden fields
 
17452
        ref_array= ref_array_start;
 
17453
      }
 
17454
 
 
17455
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
17456
          (!((Item_sum*) item)->depended_from() ||
 
17457
           ((Item_sum *)item)->depended_from() == select_lex))
 
17458
          
 
17459
      {
 
17460
        /*
 
17461
          This is a top level summary function that must be replaced with
 
17462
          a sum function that is reset for this level.
 
17463
 
 
17464
          NOTE: This code creates an object which is not that nice in a
 
17465
          sub select.  Fortunately it's not common to have rollup in
 
17466
          sub selects.
 
17467
        */
 
17468
        item= item->copy_or_same(thd);
 
17469
        ((Item_sum*) item)->make_unique();
 
17470
        *(*func)= (Item_sum*) item;
 
17471
        (*func)++;
 
17472
      }
 
17473
      else 
 
17474
      {
 
17475
        /* Check if this is something that is part of this group by */
 
17476
        ORDER *group_tmp;
 
17477
        for (group_tmp= start_group, i= pos ;
 
17478
             group_tmp ; group_tmp= group_tmp->next, i++)
 
17479
        {
 
17480
          if (*group_tmp->item == item)
 
17481
          {
 
17482
            /*
 
17483
              This is an element that is used by the GROUP BY and should be
 
17484
              set to NULL in this level
 
17485
            */
 
17486
            Item_null_result *null_item= new (thd->mem_root) Item_null_result();
 
17487
            if (!null_item)
 
17488
              return 1;
 
17489
            item->maybe_null= 1;                // Value will be null sometimes
 
17490
            null_item->result_field= item->get_tmp_table_field();
 
17491
            item= null_item;
 
17492
            break;
 
17493
          }
 
17494
        }
 
17495
      }
 
17496
      *ref_array= item;
 
17497
      if (real_fields)
 
17498
      {
 
17499
        (void) new_it++;                        // Point to next item
 
17500
        new_it.replace(item);                   // Replace previous
 
17501
        ref_array++;
 
17502
      }
 
17503
      else
 
17504
        ref_array--;
 
17505
    }
 
17506
  }
 
17507
  sum_funcs_end[0]= *func;                      // Point to last function
 
17508
  return 0;
 
17509
}
 
17510
 
 
17511
/**
 
17512
  Send all rollup levels higher than the current one to the client.
 
17513
 
 
17514
  @b SAMPLE
 
17515
    @code
 
17516
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
17517
  @endcode
 
17518
 
 
17519
  @param idx            Level we are on:
 
17520
                        - 0 = Total sum level
 
17521
                        - 1 = First group changed  (a)
 
17522
                        - 2 = Second group changed (a,b)
 
17523
 
 
17524
  @retval
 
17525
    0   ok
 
17526
  @retval
 
17527
    1   If send_data_failed()
 
17528
*/
 
17529
 
 
17530
int JOIN::rollup_send_data(uint idx)
 
17531
{
 
17532
  uint i;
 
17533
  for (i= send_group_parts ; i-- > idx ; )
 
17534
  {
 
17535
    /* Get reference pointers to sum functions in place */
 
17536
    memcpy((char*) ref_pointer_array,
 
17537
           (char*) rollup.ref_pointer_arrays[i],
 
17538
           ref_pointer_array_size);
 
17539
    if ((!having || having->val_int()))
 
17540
    {
 
17541
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
17542
          result->send_data(rollup.fields[i]))
 
17543
        return 1;
 
17544
      send_records++;
 
17545
    }
 
17546
  }
 
17547
  /* Restore ref_pointer_array */
 
17548
  set_items_ref_array(current_ref_pointer_array);
 
17549
  return 0;
 
17550
}
 
17551
 
 
17552
/**
 
17553
  Write all rollup levels higher than the current one to a temp table.
 
17554
 
 
17555
  @b SAMPLE
 
17556
    @code
 
17557
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
 
17558
  @endcode
 
17559
 
 
17560
  @param idx                 Level we are on:
 
17561
                               - 0 = Total sum level
 
17562
                               - 1 = First group changed  (a)
 
17563
                               - 2 = Second group changed (a,b)
 
17564
  @param table               reference to temp table
 
17565
 
 
17566
  @retval
 
17567
    0   ok
 
17568
  @retval
 
17569
    1   if write_data_failed()
 
17570
*/
 
17571
 
 
17572
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
 
17573
{
 
17574
  uint i;
 
17575
  for (i= send_group_parts ; i-- > idx ; )
 
17576
  {
 
17577
    /* Get reference pointers to sum functions in place */
 
17578
    memcpy((char*) ref_pointer_array,
 
17579
           (char*) rollup.ref_pointer_arrays[i],
 
17580
           ref_pointer_array_size);
 
17581
    if ((!having || having->val_int()))
 
17582
    {
 
17583
      int write_error;
 
17584
      Item *item;
 
17585
      List_iterator_fast<Item> it(rollup.fields[i]);
 
17586
      while ((item= it++))
 
17587
      {
 
17588
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
 
17589
          item->save_in_result_field(1);
 
17590
      }
 
17591
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
 
17592
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
 
17593
      {
 
17594
        if (create_myisam_from_heap(thd, table_arg, 
 
17595
                                    tmp_table_param.start_recinfo,
 
17596
                                    &tmp_table_param.recinfo,
 
17597
                                    write_error, 0))
 
17598
          return 1;                  
 
17599
      }
 
17600
    }
 
17601
  }
 
17602
  /* Restore ref_pointer_array */
 
17603
  set_items_ref_array(current_ref_pointer_array);
 
17604
  return 0;
 
17605
}
 
17606
 
 
17607
/**
 
17608
  clear results if there are not rows found for group
 
17609
  (end_send_group/end_write_group)
 
17610
*/
 
17611
 
 
17612
void JOIN::clear()
 
17613
{
 
17614
  clear_tables(this);
 
17615
  copy_fields(&tmp_table_param);
 
17616
 
 
17617
  if (sum_funcs)
 
17618
  {
 
17619
    Item_sum *func, **func_ptr= sum_funcs;
 
17620
    while ((func= *(func_ptr++)))
 
17621
      func->clear();
 
17622
  }
 
17623
}
 
17624
 
 
17625
/**
 
17626
  EXPLAIN handling.
 
17627
 
 
17628
  Send a description about what how the select will be done to stdout.
 
17629
*/
 
17630
 
 
17631
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
 
17632
                     bool distinct,const char *message)
 
17633
{
 
17634
  List<Item> field_list;
 
17635
  List<Item> item_list;
 
17636
  THD *thd=join->thd;
 
17637
  select_result *result=join->result;
 
17638
  Item *item_null= new Item_null();
 
17639
  CHARSET_INFO *cs= system_charset_info;
 
17640
  int quick_type;
 
17641
  /* Don't log this into the slow query log */
 
17642
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
 
17643
  join->unit->offset_limit_cnt= 0;
 
17644
 
 
17645
  /* 
 
17646
    NOTE: the number/types of items pushed into item_list must be in sync with
 
17647
    EXPLAIN column types as they're "defined" in THD::send_explain_fields()
 
17648
  */
 
17649
  if (message)
 
17650
  {
 
17651
    item_list.push_back(new Item_int((int32_t)
 
17652
                                     join->select_lex->select_number));
 
17653
    item_list.push_back(new Item_string(join->select_lex->type,
 
17654
                                        strlen(join->select_lex->type), cs));
 
17655
    for (uint i=0 ; i < 7; i++)
 
17656
      item_list.push_back(item_null);
 
17657
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
17658
      item_list.push_back(item_null);
 
17659
  
 
17660
    item_list.push_back(new Item_string(message,strlen(message),cs));
 
17661
    if (result->send_data(item_list))
 
17662
      join->error= 1;
 
17663
  }
 
17664
  else if (join->select_lex == join->unit->fake_select_lex)
 
17665
  {
 
17666
    /* 
 
17667
      here we assume that the query will return at least two rows, so we
 
17668
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
 
17669
      and no filesort will be actually done, but executing all selects in
 
17670
      the UNION to provide precise EXPLAIN information will hardly be
 
17671
      appreciated :)
 
17672
    */
 
17673
    char table_name_buffer[NAME_LEN];
 
17674
    item_list.empty();
 
17675
    /* id */
 
17676
    item_list.push_back(new Item_null);
 
17677
    /* select_type */
 
17678
    item_list.push_back(new Item_string(join->select_lex->type,
 
17679
                                        strlen(join->select_lex->type),
 
17680
                                        cs));
 
17681
    /* table */
 
17682
    {
 
17683
      SELECT_LEX *sl= join->unit->first_select();
 
17684
      uint len= 6, lastop= 0;
 
17685
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
 
17686
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
 
17687
      {
 
17688
        len+= lastop;
 
17689
        lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
 
17690
                         "%u,", sl->select_number);
 
17691
      }
 
17692
      if (sl || len + lastop >= NAME_LEN)
 
17693
      {
 
17694
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
 
17695
        len+= 4;
 
17696
      }
 
17697
      else
 
17698
      {
 
17699
        len+= lastop;
 
17700
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
 
17701
      }
 
17702
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
17703
    }
 
17704
    /* type */
 
17705
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
 
17706
                                          strlen(join_type_str[JT_ALL]),
 
17707
                                          cs));
 
17708
    /* possible_keys */
 
17709
    item_list.push_back(item_null);
 
17710
    /* key*/
 
17711
    item_list.push_back(item_null);
 
17712
    /* key_len */
 
17713
    item_list.push_back(item_null);
 
17714
    /* ref */
 
17715
    item_list.push_back(item_null);
 
17716
    /* in_rows */
 
17717
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
17718
      item_list.push_back(item_null);
 
17719
    /* rows */
 
17720
    item_list.push_back(item_null);
 
17721
    /* extra */
 
17722
    if (join->unit->global_parameters->order_list.first)
 
17723
      item_list.push_back(new Item_string("Using filesort",
 
17724
                                          14, cs));
 
17725
    else
 
17726
      item_list.push_back(new Item_string("", 0, cs));
 
17727
 
 
17728
    if (result->send_data(item_list))
 
17729
      join->error= 1;
 
17730
  }
 
17731
  else
 
17732
  {
 
17733
    table_map used_tables=0;
 
17734
    for (uint i=0 ; i < join->tables ; i++)
 
17735
    {
 
17736
      JOIN_TAB *tab=join->join_tab+i;
 
17737
      TABLE *table=tab->table;
 
17738
      TABLE_LIST *table_list= tab->table->pos_in_table_list;
 
17739
      char buff[512]; 
 
17740
      char buff1[512], buff2[512], buff3[512];
 
17741
      char keylen_str_buf[64];
 
17742
      String extra(buff, sizeof(buff),cs);
 
17743
      char table_name_buffer[NAME_LEN];
 
17744
      String tmp1(buff1,sizeof(buff1),cs);
 
17745
      String tmp2(buff2,sizeof(buff2),cs);
 
17746
      String tmp3(buff3,sizeof(buff3),cs);
 
17747
      extra.length(0);
 
17748
      tmp1.length(0);
 
17749
      tmp2.length(0);
 
17750
      tmp3.length(0);
 
17751
 
 
17752
      quick_type= -1;
 
17753
      item_list.empty();
 
17754
      /* id */
 
17755
      item_list.push_back(new Item_uint((uint32_t)
 
17756
                                       join->select_lex->select_number));
 
17757
      /* select_type */
 
17758
      item_list.push_back(new Item_string(join->select_lex->type,
 
17759
                                          strlen(join->select_lex->type),
 
17760
                                          cs));
 
17761
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
 
17762
      {
 
17763
        quick_type= tab->select->quick->get_type();
 
17764
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
 
17765
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
 
17766
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
 
17767
          tab->type = JT_INDEX_MERGE;
 
17768
        else
 
17769
          tab->type = JT_RANGE;
 
17770
      }
 
17771
      /* table */
 
17772
      if (table->derived_select_number)
 
17773
      {
 
17774
        /* Derived table name generation */
 
17775
        int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
 
17776
                          "<derived%u>",
 
17777
                          table->derived_select_number);
 
17778
        item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
17779
      }
 
17780
      else
 
17781
      {
 
17782
        TABLE_LIST *real_table= table->pos_in_table_list; 
 
17783
        item_list.push_back(new Item_string(real_table->alias,
 
17784
                                            strlen(real_table->alias),
 
17785
                                            cs));
 
17786
      }
 
17787
      /* "type" column */
 
17788
      item_list.push_back(new Item_string(join_type_str[tab->type],
 
17789
                                          strlen(join_type_str[tab->type]),
 
17790
                                          cs));
 
17791
      /* Build "possible_keys" value and add it to item_list */
 
17792
      if (!tab->keys.is_clear_all())
 
17793
      {
 
17794
        uint j;
 
17795
        for (j=0 ; j < table->s->keys ; j++)
 
17796
        {
 
17797
          if (tab->keys.is_set(j))
 
17798
          {
 
17799
            if (tmp1.length())
 
17800
              tmp1.append(',');
 
17801
            tmp1.append(table->key_info[j].name, 
 
17802
                        strlen(table->key_info[j].name),
 
17803
                        system_charset_info);
 
17804
          }
 
17805
        }
 
17806
      }
 
17807
      if (tmp1.length())
 
17808
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
 
17809
      else
 
17810
        item_list.push_back(item_null);
 
17811
 
 
17812
      /* Build "key", "key_len", and "ref" values and add them to item_list */
 
17813
      if (tab->ref.key_parts)
 
17814
      {
 
17815
        KEY *key_info=table->key_info+ tab->ref.key;
 
17816
        register uint length;
 
17817
        item_list.push_back(new Item_string(key_info->name,
 
17818
                                            strlen(key_info->name),
 
17819
                                            system_charset_info));
 
17820
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) - 
 
17821
                keylen_str_buf;
 
17822
        item_list.push_back(new Item_string(keylen_str_buf, length,
 
17823
                                            system_charset_info));
 
17824
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
 
17825
        {
 
17826
          if (tmp2.length())
 
17827
            tmp2.append(',');
 
17828
          tmp2.append((*ref)->name(), strlen((*ref)->name()),
 
17829
                      system_charset_info);
 
17830
        }
 
17831
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
17832
      }
 
17833
      else if (tab->type == JT_NEXT)
 
17834
      {
 
17835
        KEY *key_info=table->key_info+ tab->index;
 
17836
        register uint length;
 
17837
        item_list.push_back(new Item_string(key_info->name,
 
17838
                                            strlen(key_info->name),cs));
 
17839
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) - 
 
17840
                keylen_str_buf;
 
17841
        item_list.push_back(new Item_string(keylen_str_buf, 
 
17842
                                            length,
 
17843
                                            system_charset_info));
 
17844
        item_list.push_back(item_null);
 
17845
      }
 
17846
      else if (tab->select && tab->select->quick)
 
17847
      {
 
17848
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
 
17849
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
17850
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
 
17851
        item_list.push_back(item_null);
 
17852
      }
 
17853
      else
 
17854
      {
 
17855
        if (table_list->schema_table &&
 
17856
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
17857
        {
 
17858
          const char *tmp_buff;
 
17859
          int f_idx;
 
17860
          if (table_list->has_db_lookup_value)
 
17861
          {
 
17862
            f_idx= table_list->schema_table->idx_field1;
 
17863
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
17864
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
17865
          }          
 
17866
          if (table_list->has_table_lookup_value)
 
17867
          {
 
17868
            if (table_list->has_db_lookup_value)
 
17869
              tmp2.append(',');
 
17870
            f_idx= table_list->schema_table->idx_field2;
 
17871
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
17872
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
17873
          }
 
17874
          if (tmp2.length())
 
17875
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
17876
          else
 
17877
            item_list.push_back(item_null);
 
17878
        }
 
17879
        else
 
17880
          item_list.push_back(item_null);
 
17881
        item_list.push_back(item_null);
 
17882
        item_list.push_back(item_null);
 
17883
      }
 
17884
      
 
17885
      /* Add "rows" field to item_list. */
 
17886
      if (table_list->schema_table)
 
17887
      {
 
17888
        /* in_rows */
 
17889
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
17890
          item_list.push_back(item_null);
 
17891
        /* rows */
 
17892
        item_list.push_back(item_null);
 
17893
      }
 
17894
      else
 
17895
      {
 
17896
        double examined_rows;
 
17897
        if (tab->select && tab->select->quick)
 
17898
          examined_rows= rows2double(tab->select->quick->records);
 
17899
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
 
17900
          examined_rows= rows2double(tab->limit ? tab->limit : 
 
17901
                                     tab->table->file->records());
 
17902
        else
 
17903
          examined_rows= join->best_positions[i].records_read; 
 
17904
 
 
17905
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows, 
 
17906
                                         MY_INT64_NUM_DECIMAL_DIGITS));
 
17907
 
 
17908
        /* Add "filtered" field to item_list. */
 
17909
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
17910
        {
 
17911
          float f= 0.0; 
 
17912
          if (examined_rows)
 
17913
            f= (float) (100.0 * join->best_positions[i].records_read /
 
17914
                        examined_rows);
 
17915
          item_list.push_back(new Item_float(f, 2));
 
17916
        }
 
17917
      }
 
17918
 
 
17919
      /* Build "Extra" field and add it to item_list. */
 
17920
      bool key_read=table->key_read;
 
17921
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
 
17922
          table->covering_keys.is_set(tab->index))
 
17923
        key_read=1;
 
17924
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
 
17925
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
 
17926
        key_read=1;
 
17927
        
 
17928
      if (tab->info)
 
17929
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
 
17930
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
 
17931
      {
 
17932
        if (tab->packed_info & TAB_INFO_USING_INDEX)
 
17933
          extra.append(STRING_WITH_LEN("; Using index"));
 
17934
        if (tab->packed_info & TAB_INFO_USING_WHERE)
 
17935
          extra.append(STRING_WITH_LEN("; Using where"));
 
17936
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
 
17937
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
17938
        /* Skip initial "; "*/
 
17939
        const char *str= extra.ptr();
 
17940
        uint32_t len= extra.length();
 
17941
        if (len)
 
17942
        {
 
17943
          str += 2;
 
17944
          len -= 2;
 
17945
        }
 
17946
        item_list.push_back(new Item_string(str, len, cs));
 
17947
      }
 
17948
      else
 
17949
      {
 
17950
        uint keyno= MAX_KEY;
 
17951
        if (tab->ref.key_parts)
 
17952
          keyno= tab->ref.key;
 
17953
        else if (tab->select && tab->select->quick)
 
17954
          keyno = tab->select->quick->index;
 
17955
 
 
17956
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
 
17957
            table->file->pushed_idx_cond)
 
17958
          extra.append(STRING_WITH_LEN("; Using index condition"));
 
17959
 
 
17960
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
 
17961
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
17962
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
 
17963
        {
 
17964
          extra.append(STRING_WITH_LEN("; Using "));
 
17965
          tab->select->quick->add_info_string(&extra);
 
17966
        }
 
17967
          if (tab->select)
 
17968
        {
 
17969
          if (tab->use_quick == 2)
 
17970
          {
 
17971
            /* 4 bits per 1 hex digit + terminating '\0' */
 
17972
            char buf[MAX_KEY / 4 + 1];
 
17973
            extra.append(STRING_WITH_LEN("; Range checked for each "
 
17974
                                         "record (index map: 0x"));
 
17975
            extra.append(tab->keys.print(buf));
 
17976
            extra.append(')');
 
17977
          }
 
17978
          else if (tab->select->cond)
 
17979
          {
 
17980
            const COND *pushed_cond= tab->table->file->pushed_cond;
 
17981
 
 
17982
            if (thd->variables.engine_condition_pushdown && pushed_cond)
 
17983
            {
 
17984
              extra.append(STRING_WITH_LEN("; Using where with pushed "
 
17985
                                           "condition"));
 
17986
              if (thd->lex->describe & DESCRIBE_EXTENDED)
 
17987
              {
 
17988
                extra.append(STRING_WITH_LEN(": "));
 
17989
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
 
17990
              }
 
17991
            }
 
17992
            else
 
17993
              extra.append(STRING_WITH_LEN("; Using where"));
 
17994
          }
 
17995
        }
 
17996
        if (key_read)
 
17997
        {
 
17998
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
17999
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
 
18000
          else
 
18001
            extra.append(STRING_WITH_LEN("; Using index"));
 
18002
        }
 
18003
        if (table->reginfo.not_exists_optimize)
 
18004
          extra.append(STRING_WITH_LEN("; Not exists"));
 
18005
          
 
18006
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
 
18007
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
 
18008
             HA_MRR_USE_DEFAULT_IMPL))
 
18009
        {
 
18010
          extra.append(STRING_WITH_LEN("; Using MRR"));
 
18011
        }
 
18012
 
 
18013
        if (table_list->schema_table &&
 
18014
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
18015
        {
 
18016
          if (!table_list->table_open_method)
 
18017
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
 
18018
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
 
18019
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
 
18020
          else
 
18021
            extra.append(STRING_WITH_LEN("; Open_full_table"));
 
18022
          if (table_list->has_db_lookup_value &&
 
18023
              table_list->has_table_lookup_value)
 
18024
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
 
18025
          else if (table_list->has_db_lookup_value ||
 
18026
                   table_list->has_table_lookup_value)
 
18027
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
 
18028
          else
 
18029
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
 
18030
        }
 
18031
        if (need_tmp_table)
 
18032
        {
 
18033
          need_tmp_table=0;
 
18034
          extra.append(STRING_WITH_LEN("; Using temporary"));
 
18035
        }
 
18036
        if (need_order)
 
18037
        {
 
18038
          need_order=0;
 
18039
          extra.append(STRING_WITH_LEN("; Using filesort"));
 
18040
        }
 
18041
        if (distinct & test_all_bits(used_tables,thd->used_tables))
 
18042
          extra.append(STRING_WITH_LEN("; Distinct"));
 
18043
 
 
18044
        if (tab->insideout_match_tab)
 
18045
        {
 
18046
          extra.append(STRING_WITH_LEN("; LooseScan"));
 
18047
        }
 
18048
 
 
18049
        if (tab->flush_weedout_table)
 
18050
          extra.append(STRING_WITH_LEN("; Start temporary"));
 
18051
        else if (tab->check_weed_out_table)
 
18052
          extra.append(STRING_WITH_LEN("; End temporary"));
 
18053
        else if (tab->do_firstmatch)
 
18054
        {
 
18055
          extra.append(STRING_WITH_LEN("; FirstMatch("));
 
18056
          TABLE *prev_table=tab->do_firstmatch->table;
 
18057
          if (prev_table->derived_select_number)
 
18058
          {
 
18059
            char namebuf[NAME_LEN];
 
18060
            /* Derived table name generation */
 
18061
            int len= snprintf(namebuf, sizeof(namebuf)-1,
 
18062
                              "<derived%u>",
 
18063
                              prev_table->derived_select_number);
 
18064
            extra.append(namebuf, len);
 
18065
          }
 
18066
          else
 
18067
            extra.append(prev_table->pos_in_table_list->alias);
 
18068
          extra.append(STRING_WITH_LEN(")"));
 
18069
        }
 
18070
 
 
18071
        for (uint part= 0; part < tab->ref.key_parts; part++)
 
18072
        {
 
18073
          if (tab->ref.cond_guards[part])
 
18074
          {
 
18075
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
18076
            break;
 
18077
          }
 
18078
        }
 
18079
 
 
18080
        if (i > 0 && tab[-1].next_select == sub_select_cache)
 
18081
          extra.append(STRING_WITH_LEN("; Using join buffer"));
 
18082
 
 
18083
        /* Skip initial "; "*/
 
18084
        const char *str= extra.ptr();
 
18085
        uint32_t len= extra.length();
 
18086
        if (len)
 
18087
        {
 
18088
          str += 2;
 
18089
          len -= 2;
 
18090
        }
 
18091
        item_list.push_back(new Item_string(str, len, cs));
 
18092
      }
 
18093
      // For next iteration
 
18094
      used_tables|=table->map;
 
18095
      if (result->send_data(item_list))
 
18096
        join->error= 1;
 
18097
    }
 
18098
  }
 
18099
  for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
 
18100
       unit;
 
18101
       unit= unit->next_unit())
 
18102
  {
 
18103
    if (mysql_explain_union(thd, unit, result))
 
18104
      return;
 
18105
  }
 
18106
  return;
 
18107
}
 
18108
 
 
18109
 
 
18110
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
 
18111
{
 
18112
  bool res= 0;
 
18113
  SELECT_LEX *first= unit->first_select();
 
18114
 
 
18115
  for (SELECT_LEX *sl= first;
 
18116
       sl;
 
18117
       sl= sl->next_select())
 
18118
  {
 
18119
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
 
18120
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
 
18121
    sl->type= (((&thd->lex->select_lex)==sl)?
 
18122
               (sl->first_inner_unit() || sl->next_select() ? 
 
18123
                "PRIMARY" : "SIMPLE"):
 
18124
               ((sl == first)?
 
18125
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
 
18126
                 "DERIVED":
 
18127
                 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
18128
                  "DEPENDENT SUBQUERY":
 
18129
                  (uncacheable?"UNCACHEABLE SUBQUERY":
 
18130
                   "SUBQUERY"))):
 
18131
                ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
18132
                 "DEPENDENT UNION":
 
18133
                 uncacheable?"UNCACHEABLE UNION":
 
18134
                 "UNION")));
 
18135
    sl->options|= SELECT_DESCRIBE;
 
18136
  }
 
18137
  if (unit->is_union())
 
18138
  {
 
18139
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
 
18140
    unit->fake_select_lex->type= "UNION RESULT";
 
18141
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
 
18142
    if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
 
18143
      res= unit->exec();
 
18144
    res|= unit->cleanup();
 
18145
  }
 
18146
  else
 
18147
  {
 
18148
    thd->lex->current_select= first;
 
18149
    unit->set_limit(unit->global_parameters);
 
18150
    res= mysql_select(thd, &first->ref_pointer_array,
 
18151
                        (TABLE_LIST*) first->table_list.first,
 
18152
                        first->with_wild, first->item_list,
 
18153
                        first->where,
 
18154
                        first->order_list.elements +
 
18155
                        first->group_list.elements,
 
18156
                        (ORDER*) first->order_list.first,
 
18157
                        (ORDER*) first->group_list.first,
 
18158
                        first->having,
 
18159
                        (ORDER*) thd->lex->proc_list.first,
 
18160
                        first->options | thd->options | SELECT_DESCRIBE,
 
18161
                        result, unit, first);
 
18162
  }
 
18163
  return(res || thd->is_error());
 
18164
}
 
18165
 
 
18166
 
 
18167
static void print_table_array(THD *thd, String *str, TABLE_LIST **table, 
 
18168
                              TABLE_LIST **end)
 
18169
{
 
18170
  (*table)->print(thd, str, QT_ORDINARY);
 
18171
 
 
18172
  for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
 
18173
  {
 
18174
    TABLE_LIST *curr= *tbl;
6265
18175
    if (curr->outer_join)
6266
18176
    {
6267
18177
      /* MySQL converts right to left joins */
6269
18179
    }
6270
18180
    else if (curr->straight)
6271
18181
      str->append(STRING_WITH_LEN(" straight_join "));
 
18182
    else if (curr->sj_inner_tables)
 
18183
      str->append(STRING_WITH_LEN(" semi join "));
6272
18184
    else
6273
18185
      str->append(STRING_WITH_LEN(" join "));
6274
 
    curr->print(session, str, QT_ORDINARY);
 
18186
    curr->print(thd, str, QT_ORDINARY);
6275
18187
    if (curr->on_expr)
6276
18188
    {
6277
18189
      str->append(STRING_WITH_LEN(" on("));
6281
18193
  }
6282
18194
}
6283
18195
 
 
18196
 
6284
18197
/**
6285
18198
  Print joins from the FROM clause.
6286
 
  @param session     thread Cursor
 
18199
  @param thd     thread handler
6287
18200
  @param str     string where table should be printed
6288
18201
  @param tables  list of tables in join
6289
18202
  @query_type    type of the query is being generated
6290
18203
*/
6291
 
void print_join(Session *session, String *str,
6292
 
                List<TableList> *tables, enum_query_type)
 
18204
 
 
18205
static void print_join(THD *thd,
 
18206
                       String *str,
 
18207
                       List<TABLE_LIST> *tables,
 
18208
                       enum_query_type query_type __attribute__((unused)))
6293
18209
{
6294
18210
  /* 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*) *
 
18211
  List_iterator_fast<TABLE_LIST> ti(*tables);
 
18212
  TABLE_LIST **table= (TABLE_LIST **)thd->alloc(sizeof(TABLE_LIST*) *
6297
18213
                                                tables->elements);
6298
18214
  if (table == 0)
6299
18215
    return;  // out of memory
6300
18216
 
6301
 
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
 
18217
  for (TABLE_LIST **t= table + (tables->elements - 1); t >= table; t--)
6302
18218
    *t= ti++;
 
18219
  
 
18220
  /* 
 
18221
    If the first table is a semi-join nest, swap it with something that is
 
18222
    not a semi-join nest.
 
18223
  */
 
18224
  if ((*table)->sj_inner_tables)
 
18225
  {
 
18226
    TABLE_LIST **end= table + tables->elements;
 
18227
    for (TABLE_LIST **t2= table; t2!=end; t2++)
 
18228
    {
 
18229
      if (!(*t2)->sj_inner_tables)
 
18230
      {
 
18231
        TABLE_LIST *tmp= *t2;
 
18232
        *t2= *table;
 
18233
        *table= tmp;
 
18234
        break;
 
18235
      }
 
18236
    }
 
18237
  }
6303
18238
  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
 
 
 
18239
  print_table_array(thd, str, table, table + tables->elements);
 
18240
}
 
18241
 
 
18242
 
 
18243
/**
 
18244
  @brief Print an index hint
 
18245
 
 
18246
  @details Prints out the USE|FORCE|IGNORE index hint.
 
18247
 
 
18248
  @param      thd         the current thread
 
18249
  @param[out] str         appends the index hint here
 
18250
  @param      hint        what the hint is (as string : "USE INDEX"|
 
18251
                          "FORCE INDEX"|"IGNORE INDEX")
 
18252
  @param      hint_length the length of the string in 'hint'
 
18253
  @param      indexes     a list of index names for the hint
 
18254
*/
 
18255
 
 
18256
void 
 
18257
Index_hint::print(THD *thd, String *str)
 
18258
{
 
18259
  switch (type)
 
18260
  {
 
18261
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
 
18262
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
 
18263
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
 
18264
  }
 
18265
  str->append (STRING_WITH_LEN(" ("));
 
18266
  if (key_name.length)
 
18267
  {
 
18268
    if (thd && !my_strnncoll(system_charset_info,
 
18269
                             (const uchar *)key_name.str, key_name.length, 
 
18270
                             (const uchar *)primary_key_name, 
 
18271
                             strlen(primary_key_name)))
 
18272
      str->append(primary_key_name);
 
18273
    else
 
18274
      append_identifier(thd, str, key_name.str, key_name.length);
 
18275
  }
 
18276
  str->append(')');
 
18277
}
 
18278
 
 
18279
 
 
18280
/**
 
18281
  Print table as it should be in join list.
 
18282
 
 
18283
  @param str   string where table should be printed
 
18284
*/
 
18285
 
 
18286
void TABLE_LIST::print(THD *thd, String *str, enum_query_type query_type)
 
18287
{
 
18288
  if (nested_join)
 
18289
  {
 
18290
    str->append('(');
 
18291
    print_join(thd, str, &nested_join->join_list, query_type);
 
18292
    str->append(')');
 
18293
  }
 
18294
  else
 
18295
  {
 
18296
    const char *cmp_name;                         // Name to compare with alias
 
18297
    if (derived)
 
18298
    {
 
18299
      // A derived table
 
18300
      str->append('(');
 
18301
      derived->print(str, query_type);
 
18302
      str->append(')');
 
18303
      cmp_name= "";                               // Force printing of alias
 
18304
    }
 
18305
    else
 
18306
    {
 
18307
      // A normal table
 
18308
      {
 
18309
        append_identifier(thd, str, db, db_length);
 
18310
        str->append('.');
 
18311
      }
 
18312
      if (schema_table)
 
18313
      {
 
18314
        append_identifier(thd, str, schema_table_name,
 
18315
                          strlen(schema_table_name));
 
18316
        cmp_name= schema_table_name;
 
18317
      }
 
18318
      else
 
18319
      {
 
18320
        append_identifier(thd, str, table_name, table_name_length);
 
18321
        cmp_name= table_name;
 
18322
      }
 
18323
    }
 
18324
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
 
18325
    {
 
18326
      char t_alias_buff[MAX_ALIAS_NAME];
 
18327
      const char *t_alias= alias;
 
18328
 
 
18329
      str->append(' ');
 
18330
      if (lower_case_table_names== 1)
 
18331
      {
 
18332
        if (alias && alias[0])
 
18333
        {
 
18334
          strmov(t_alias_buff, alias);
 
18335
          my_casedn_str(files_charset_info, t_alias_buff);
 
18336
          t_alias= t_alias_buff;
 
18337
        }
 
18338
      }
 
18339
 
 
18340
      append_identifier(thd, str, t_alias, strlen(t_alias));
 
18341
    }
 
18342
 
 
18343
    if (index_hints)
 
18344
    {
 
18345
      List_iterator<Index_hint> it(*index_hints);
 
18346
      Index_hint *hint;
 
18347
 
 
18348
      while ((hint= it++))
 
18349
      {
 
18350
        str->append (STRING_WITH_LEN(" "));
 
18351
        hint->print (thd, str);
 
18352
      }
 
18353
    }
 
18354
  }
 
18355
}
 
18356
 
 
18357
 
 
18358
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
 
18359
{
 
18360
  /* QQ: thd may not be set for sub queries, but this should be fixed */
 
18361
  if (!thd)
 
18362
    thd= current_thd;
6313
18363
 
6314
18364
  str->append(STRING_WITH_LEN("select "));
6315
18365
 
6316
18366
  /* First add options */
6317
18367
  if (options & SELECT_STRAIGHT_JOIN)
6318
18368
    str->append(STRING_WITH_LEN("straight_join "));
 
18369
  if ((thd->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
 
18370
      (this == &thd->lex->select_lex))
 
18371
    str->append(STRING_WITH_LEN("high_priority "));
6319
18372
  if (options & SELECT_DISTINCT)
6320
18373
    str->append(STRING_WITH_LEN("distinct "));
6321
18374
  if (options & SELECT_SMALL_RESULT)
6329
18382
 
6330
18383
  //Item List
6331
18384
  bool first= 1;
6332
 
  List<Item>::iterator it(item_list.begin());
 
18385
  List_iterator_fast<Item> it(item_list);
6333
18386
  Item *item;
6334
18387
  while ((item= it++))
6335
18388
  {
6342
18395
 
6343
18396
  /*
6344
18397
    from clause
6345
 
    @todo support USING/FORCE/IGNORE index
 
18398
    TODO: support USING/FORCE/IGNORE index
6346
18399
  */
6347
18400
  if (table_list.elements)
6348
18401
  {
6349
18402
    str->append(STRING_WITH_LEN(" from "));
6350
18403
    /* go through join tree */
6351
 
    print_join(session, str, &top_join_list, query_type);
 
18404
    print_join(thd, str, &top_join_list, query_type);
6352
18405
  }
6353
18406
  else if (where)
6354
18407
  {
6355
18408
    /*
6356
 
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as
 
18409
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as 
6357
18410
      "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
6358
18411
    */
6359
18412
    str->append(STRING_WITH_LEN(" from DUAL "));
6376
18429
  if (group_list.elements)
6377
18430
  {
6378
18431
    str->append(STRING_WITH_LEN(" group by "));
6379
 
    print_order(str, (Order *) group_list.first, query_type);
 
18432
    print_order(str, (ORDER *) group_list.first, query_type);
6380
18433
    switch (olap)
6381
18434
    {
6382
18435
      case CUBE_TYPE:
6407
18460
  if (order_list.elements)
6408
18461
  {
6409
18462
    str->append(STRING_WITH_LEN(" order by "));
6410
 
    print_order(str, (Order *) order_list.first, query_type);
 
18463
    print_order(str, (ORDER *) order_list.first, query_type);
6411
18464
  }
6412
18465
 
6413
18466
  // limit
6414
 
  print_limit(session, str, query_type);
 
18467
  print_limit(thd, str, query_type);
6415
18468
 
6416
18469
  // PROCEDURE unsupported here
6417
18470
}
6418
18471
 
 
18472
 
 
18473
/**
 
18474
  change select_result object of JOIN.
 
18475
 
 
18476
  @param res            new select_result object
 
18477
 
 
18478
  @retval
 
18479
    false   OK
 
18480
  @retval
 
18481
    true    error
 
18482
*/
 
18483
 
 
18484
bool JOIN::change_result(select_result *res)
 
18485
{
 
18486
  result= res;
 
18487
  if (result->prepare(fields_list, select_lex->master_unit()) ||
 
18488
                     result->prepare2())
 
18489
  {
 
18490
    return(true);
 
18491
  }
 
18492
  return(false);
 
18493
}
 
18494
 
6419
18495
/**
6420
18496
  @} (end of group Query_Optimizer)
6421
18497
*/
6422
 
 
6423
 
} /* namespace drizzled */